[SCEV] Construct GEP expression more efficiently (NFCI)
authorNikita Popov <nikita.ppv@gmail.com>
Sun, 1 Nov 2020 17:34:01 +0000 (18:34 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Sun, 1 Nov 2020 18:00:57 +0000 (19:00 +0100)
Instead of performing a sequence of pairwise additions, directly
construct a multi-operand add expression.

This should be NFC modulo any SCEV canonicalization deficiencies.

llvm/lib/Analysis/ScalarEvolution.cpp

index d214fce..aa3e92c 100644 (file)
@@ -3421,9 +3421,9 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
   SCEV::NoWrapFlags Wrap = GEP->isInBounds() ? SCEV::FlagNSW
                                              : SCEV::FlagAnyWrap;
 
-  const SCEV *TotalOffset = getZero(IntIdxTy);
   Type *CurTy = GEP->getType();
   bool FirstIter = true;
+  SmallVector<const SCEV *, 4> AddOps{BaseExpr};
   for (const SCEV *IndexExpr : IndexExprs) {
     // Compute the (potentially symbolic) offset in bytes for this index.
     if (StructType *STy = dyn_cast<StructType>(CurTy)) {
@@ -3431,9 +3431,7 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
       ConstantInt *Index = cast<SCEVConstant>(IndexExpr)->getValue();
       unsigned FieldNo = Index->getZExtValue();
       const SCEV *FieldOffset = getOffsetOfExpr(IntIdxTy, STy, FieldNo);
-
-      // Add the field offset to the running total offset.
-      TotalOffset = getAddExpr(TotalOffset, FieldOffset);
+      AddOps.push_back(FieldOffset);
 
       // Update CurTy to the type of the field at Index.
       CurTy = STy->getTypeAtIndex(Index);
@@ -3454,14 +3452,12 @@ ScalarEvolution::getGEPExpr(GEPOperator *GEP,
 
       // Multiply the index by the element size to compute the element offset.
       const SCEV *LocalOffset = getMulExpr(IndexExpr, ElementSize, Wrap);
-
-      // Add the element offset to the running total offset.
-      TotalOffset = getAddExpr(TotalOffset, LocalOffset);
+      AddOps.push_back(LocalOffset);
     }
   }
 
-  // Add the total offset from all the GEP indices to the base.
-  return getAddExpr(BaseExpr, TotalOffset, Wrap);
+  // Add the base and all the offsets together.
+  return getAddExpr(AddOps, Wrap);
 }
 
 std::tuple<SCEV *, FoldingSetNodeID, void *>