tree-optimization/109791 - expand &x + off for niter compute

Message ID 20230525085951.1DA233858430@sourceware.org
State New
Headers
Series tree-optimization/109791 - expand &x + off for niter compute |

Commit Message

Richard Biener May 25, 2023, 8:59 a.m. UTC
  The following makes expand_simple_operations expand POINTER_PLUS_EXPRs
with variable offset when the base is invariant.  That will allow
to simplify address differences to offset differences in some cases.
Note the patch doesn't follow the variable off chain as I don't have
a testcase showing that's beneficial.

Bootstrapped and tested on x86_64-unknown-linux-gnu.

I'm going to push this when the last patch in the series tests OK
and the testcase from PR109791 then can be added.

	PR tree-optimization/109791
	* tree-ssa-loop-niter.cc (expand_simple_operations): Expand &x + off.
---
 gcc/tree-ssa-loop-niter.cc | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
  

Patch

diff --git a/gcc/tree-ssa-loop-niter.cc b/gcc/tree-ssa-loop-niter.cc
index 5d398b67e68..159fdc8fb85 100644
--- a/gcc/tree-ssa-loop-niter.cc
+++ b/gcc/tree-ssa-loop-niter.cc
@@ -2817,12 +2817,17 @@  expand_simple_operations (tree expr, tree stop, hash_map<tree, tree> &cache)
 	return expr;
       /* Fallthru.  */
     case POINTER_PLUS_EXPR:
-      /* And increments and decrements by a constant are simple.  */
+      /* And increments and decrements by a constant are simple.
+	 Also expand increments from an invariant base (but do not follow
+	 a variable offset).  */
       e1 = gimple_assign_rhs2 (stmt);
-      if (!is_gimple_min_invariant (e1))
+      if (is_gimple_min_invariant (e1))
+	ee = expand_simple_operations (e, stop, cache);
+      else if (is_gimple_min_invariant (e))
+	ee = e;
+      else
 	return expr;
 
-      ee = expand_simple_operations (e, stop, cache);
       return fold_build2 (code, TREE_TYPE (expr), ee, e1);
 
     default: