# Use EVRP within DOM to discover more constants

Message ID 49e896d4-813b-b833-c7ed-23b5ac0c5cd3@redhat.com New show Use EVRP within DOM to discover more constants show

## Commit Message

Jeff Law Aug. 6, 2018, 8:14 p.m.
```Now that we have EVRP analysis available within DOM, we can query it to
see if an SSA_NAME has a singleton constant range.  If so, we can
substitute the constant for the SSA_NAME.

This is most helpful in assignments (for GIMPLE_COND we'd simplify it
later anyway)...  Consider this:

_930 = _631 > 5;
_952 = _638 != 0B;
_933 = _930 & _952;
_929 = _631 <= 10;
_1658 = _929 & _933;
if (_1658 != 0)

We already have some info about _631 before we enter this block via the
EVRP analysis:

pushing new range for _631: [1, 10]  EQUIVALENCES: { _631 } (1 elements)

Knowing the range for _631 results in _929 having a constant value 1.

That in turn allows us to turn the assignment for _1658 into a simple
copy (we're dealing with booleans here):

Optimizing statement _1658 = _929 & _933;
Replaced '_929' with constant '1'
gimple_simplified to _1658 = _933;
Folded to: _1658 = _933;

That in turn allows us to propagate _933 into the use of _1658:

Optimizing statement if (_1658 != 0)
Replaced '_1658' with variable '_933'
gimple_simplified to if (_933 != 0)
Folded to: if (_933 != 0)

It triggers a few thousand times during a bootstrap.  Many of those
would have been caught later anyway, so it's not as impressive as it
might sound.  BUt it does seem to generate consistent small improvements
in the resulting code.

Bootstrapped and regression tested on x86_64-linux-gnu.  Installing on
the trunk.

Jeff
* tree-ssa-dom.c (dom_opt_dom_walker::optimize_stmt): Pass down
the vr_values instance to cprop_into_stmt.
(cprop_into_stmt): Pass vr_values instance down to cprop_operand.
(cprop_operand): Also query EVRP to determine if OP is a constant.
```

## Patch

```diff --git a/gcc/tree-ssa-dom.c b/gcc/tree-ssa-dom.c
index a6f176c..b936fd3 100644
--- a/gcc/tree-ssa-dom.c
+++ b/gcc/tree-ssa-dom.c
@@ -1700,7 +1712,7 @@  record_equivalences_from_stmt (gimple *stmt, int may_optimize_p,
CONST_AND_COPIES.  */

static void
-cprop_operand (gimple *stmt, use_operand_p op_p)
+cprop_operand (gimple *stmt, use_operand_p op_p, vr_values *vr_values)
{
tree val;
tree op = USE_FROM_PTR (op_p);
@@ -1709,6 +1721,9 @@  cprop_operand (gimple *stmt, use_operand_p op_p)
copy of some other variable, use the value or copy stored in
CONST_AND_COPIES.  */
val = SSA_NAME_VALUE (op);
+  if (!val)
+    val = vr_values->op_with_constant_singleton_value_range (op);
+
if (val && val != op)
{
/* Do not replace hard register operands in asm statements.  */
@@ -1765,7 +1780,7 @@  cprop_operand (gimple *stmt, use_operand_p op_p)
vdef_ops of STMT.  */

static void
-cprop_into_stmt (gimple *stmt)
+cprop_into_stmt (gimple *stmt, vr_values *vr_values)
{
use_operand_p op_p;
ssa_op_iter iter;
@@ -1782,7 +1797,7 @@  cprop_into_stmt (gimple *stmt)
operands.  */
if (old_op != last_copy_propagated_op)
{
-	  cprop_operand (stmt, op_p);
+	  cprop_operand (stmt, op_p, vr_values);

tree new_op = USE_FROM_PTR (op_p);
if (new_op != old_op && TREE_CODE (new_op) == SSA_NAME)
@@ -1925,7 +1940,7 @@  dom_opt_dom_walker::optimize_stmt (basic_block bb, gimple_stmt_iterator si)
opt_stats.num_stmts++;

/* Const/copy propagate into USES, VUSES and the RHS of VDEFs.  */
-  cprop_into_stmt (stmt);
+  cprop_into_stmt (stmt, evrp_range_analyzer.get_vr_values ());

/* If the statement has been modified with constant replacements,
fold its RHS before checking for redundant computations.  */

```