[PATCH] Add more opportunities for constant folding in ScalarEvolution.

Nick Lewycky nlewycky at google.com
Tue Dec 2 16:04:03 PST 2014


================
Comment at: lib/Analysis/ScalarEvolution.cpp:2210
@@ -2209,1 +2209,3 @@
 
+/// Determine if any of the operands in this SCEV are a constant or if
+/// any of the add or multiply expressions in this SCEV contain a constant.
----------------
I forget; are expr's guaranteed to be "GroupByComplexity"'d? If so, ContainsConstantSomewhere can be optimized greatly. If there's any constant, it's guaranteed to be in a group at the end. You only recurse on Add and Mul, so if you've gone past those (to umax, smax, unknown or could-not-compute, you can stop).

Also, please iterate with a worklist instead of recursing. Create a SmallVector<SCEV*>, push your starting element into it, use pop_back_val to grab the latest one off the top of the list. If you're worried about revisiting the same thing multiple times, add a SmallSet<SCEV*> and test whether inserting the new element succeeds -- if it doesn't, it's already in the set.

================
Comment at: lib/Analysis/ScalarEvolution.cpp:2272-2273
@@ +2271,4 @@
+        if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
+          // if any of Add's ops are Adds or Muls with a constant
+          //  then apply this transformation as well
+          if (Add->getNumOperands() == 2)
----------------
Capitalize 'If', remove extra space before 'then'.

http://reviews.llvm.org/D6487






More information about the llvm-commits mailing list