[llvm] [SCEV] Avoid erase+insert in constant folding (PR #101642)

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 15 08:55:55 PDT 2024


================
@@ -844,34 +844,39 @@ static const SCEV *
 constantFoldAndGroupOps(ScalarEvolution &SE, LoopInfo &LI, DominatorTree &DT,
                         SmallVectorImpl<const SCEV *> &Ops, FoldT Fold,
                         IsIdentityT IsIdentity, IsAbsorberT IsAbsorber) {
-  const SCEVConstant *Folded = nullptr;
+  bool HasConst = false;
   for (unsigned Idx = 0; Idx < Ops.size();) {
     const SCEV *Op = Ops[Idx];
     if (const auto *C = dyn_cast<SCEVConstant>(Op)) {
-      if (!Folded)
-        Folded = C;
-      else
-        Folded = cast<SCEVConstant>(
-            SE.getConstant(Fold(Folded->getAPInt(), C->getAPInt())));
-      Ops.erase(Ops.begin() + Idx);
-      continue;
+      if (!HasConst) {
----------------
preames wrote:

Reversing the if-else here would bring the fallthrough closer to the index advance, and would minorly help readability.  

https://github.com/llvm/llvm-project/pull/101642


More information about the llvm-commits mailing list