This patch is a NFC, but while working on the second patch I noticed that when
folding an expression in a gassign in SCEV we check whether it's a unary or
binary operation and then individually analyze the components and then fold the
results using the original operator.

SCEV however can already handle expressions recursively in instantiate_scev_r,
so this is replicating some code.

Instead convert the gimple to a tree expression with gimple_assign_rhs_to_tree
and pass that to instantiate_scev_r.

I will note that gimple_assign_rhs_to_tree also handle ternary expressions.

Bootstrapped Regtested on aarch64-none-linux-gnu,
arm-none-linux-gnueabihf, x86_64-pc-linux-gnu
-m32, -m64 and no issues.

Ok for master?

Thanks,
Tamar

gcc/ChangeLog:

        * tree-scalar-evolution.cc (instantiate_scev_name): Use
        gimple_assign_rhs_to_tree.

---
diff --git a/gcc/tree-scalar-evolution.cc b/gcc/tree-scalar-evolution.cc
index 
f524786f33be6ae46f04c68c796610a8ecfafede..710a40921acf15b3dd1b130629c38083e551c2be
 100644
--- a/gcc/tree-scalar-evolution.cc
+++ b/gcc/tree-scalar-evolution.cc
@@ -281,6 +281,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "dumpfile.h"
 #include "tree-ssa-propagate.h"
 #include "gimple-fold.h"
+#include "cfgexpand.h"
 #include "tree-into-ssa.h"
 #include "builtins.h"
 #include "case-cfn-macros.h"
@@ -2385,40 +2386,9 @@ instantiate_scev_name (edge instantiate_below,
     {
       gimple *def = SSA_NAME_DEF_STMT (chrec);
       if (gassign *ass = dyn_cast <gassign *> (def))
-       {
-         switch (gimple_assign_rhs_class (ass))
-           {
-           case GIMPLE_UNARY_RHS:
-             {
-               tree op0 = instantiate_scev_r (instantiate_below, 
evolution_loop,
-                                              inner_loop, gimple_assign_rhs1 
(ass),
-                                              fold_conversions, size_expr);
-               if (op0 == chrec_dont_know)
-                 return chrec_dont_know;
-               res = fold_build1 (gimple_assign_rhs_code (ass),
-                                  TREE_TYPE (chrec), op0);
-               break;
-             }
-           case GIMPLE_BINARY_RHS:
-             {
-               tree op0 = instantiate_scev_r (instantiate_below, 
evolution_loop,
-                                              inner_loop, gimple_assign_rhs1 
(ass),
-                                              fold_conversions, size_expr);
-               if (op0 == chrec_dont_know)
-                 return chrec_dont_know;
-               tree op1 = instantiate_scev_r (instantiate_below, 
evolution_loop,
-                                              inner_loop, gimple_assign_rhs2 
(ass),
-                                              fold_conversions, size_expr);
-               if (op1 == chrec_dont_know)
-                 return chrec_dont_know;
-               res = fold_build2 (gimple_assign_rhs_code (ass),
-                                  TREE_TYPE (chrec), op0, op1);
-               break;
-             }
-           default:
-             res = chrec_dont_know;
-           }
-       }
+       res = instantiate_scev_r (instantiate_below, evolution_loop,
+                                 inner_loop, gimple_assign_rhs_to_tree (ass),
+                                 fold_conversions, size_expr);
       else
        res = chrec_dont_know;
       global_cache->set (si, res);


-- 
diff --git a/gcc/tree-scalar-evolution.cc b/gcc/tree-scalar-evolution.cc
index f524786f33be6ae46f04c68c796610a8ecfafede..710a40921acf15b3dd1b130629c38083e551c2be 100644
--- a/gcc/tree-scalar-evolution.cc
+++ b/gcc/tree-scalar-evolution.cc
@@ -281,6 +281,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "dumpfile.h"
 #include "tree-ssa-propagate.h"
 #include "gimple-fold.h"
+#include "cfgexpand.h"
 #include "tree-into-ssa.h"
 #include "builtins.h"
 #include "case-cfn-macros.h"
@@ -2385,40 +2386,9 @@ instantiate_scev_name (edge instantiate_below,
     {
       gimple *def = SSA_NAME_DEF_STMT (chrec);
       if (gassign *ass = dyn_cast <gassign *> (def))
-	{
-	  switch (gimple_assign_rhs_class (ass))
-	    {
-	    case GIMPLE_UNARY_RHS:
-	      {
-		tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
-					       inner_loop, gimple_assign_rhs1 (ass),
-					       fold_conversions, size_expr);
-		if (op0 == chrec_dont_know)
-		  return chrec_dont_know;
-		res = fold_build1 (gimple_assign_rhs_code (ass),
-				   TREE_TYPE (chrec), op0);
-		break;
-	      }
-	    case GIMPLE_BINARY_RHS:
-	      {
-		tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
-					       inner_loop, gimple_assign_rhs1 (ass),
-					       fold_conversions, size_expr);
-		if (op0 == chrec_dont_know)
-		  return chrec_dont_know;
-		tree op1 = instantiate_scev_r (instantiate_below, evolution_loop,
-					       inner_loop, gimple_assign_rhs2 (ass),
-					       fold_conversions, size_expr);
-		if (op1 == chrec_dont_know)
-		  return chrec_dont_know;
-		res = fold_build2 (gimple_assign_rhs_code (ass),
-				   TREE_TYPE (chrec), op0, op1);
-		break;
-	      }
-	    default:
-	      res = chrec_dont_know;
-	    }
-	}
+	res = instantiate_scev_r (instantiate_below, evolution_loop,
+				  inner_loop, gimple_assign_rhs_to_tree (ass),
+				  fold_conversions, size_expr);
       else
 	res = chrec_dont_know;
       global_cache->set (si, res);

Reply via email to