[PATCH] D149150: [SCEV] Support GEP in createSimpleAffineAddRec() (NFCI)

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 25 06:00:06 PDT 2023


nikic created this revision.
nikic added reviewers: mkazantsev, fhahn, reames.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Currently, all GEP addrecs go through the generic createAddRecFromPHI() code. I think it would make sense to support at least basic constant increment GEPs in createSimpleAffineAddRec(), which is what this patch implements.

This gives a pretty small compile-time improvement: http://llvm-compile-time-tracker.com/compare.php?from=f30f34e6ba411c43315f9065860a51c23ce4136a&to=446d1e1504232493002f3b7c4d436e35e5965c86&stat=instructions:u Maybe it's not worth it.


https://reviews.llvm.org/D149150

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp


Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -5682,27 +5682,38 @@
   assert(L && L->getHeader() == PN->getParent());
   assert(BEValueV && StartValueV);
 
-  auto BO = MatchBinaryOp(BEValueV, getDataLayout(), AC, DT, PN);
-  if (!BO)
-    return nullptr;
-
-  if (BO->Opcode != Instruction::Add)
-    return nullptr;
-
   const SCEV *Accum = nullptr;
-  if (BO->LHS == PN && L->isLoopInvariant(BO->RHS))
-    Accum = getSCEV(BO->RHS);
-  else if (BO->RHS == PN && L->isLoopInvariant(BO->LHS))
-    Accum = getSCEV(BO->LHS);
+  SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
 
-  if (!Accum)
-    return nullptr;
+  const DataLayout &DL = getDataLayout();
+  auto BO = MatchBinaryOp(BEValueV, DL, AC, DT, PN);
+  if (BO && BO->Opcode == Instruction::Add) {
+    if (BO->LHS == PN && L->isLoopInvariant(BO->RHS))
+      Accum = getSCEV(BO->RHS);
+    else if (BO->RHS == PN && L->isLoopInvariant(BO->LHS))
+      Accum = getSCEV(BO->LHS);
+    else
+      return nullptr;
 
-  SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
-  if (BO->IsNUW)
-    Flags = setFlags(Flags, SCEV::FlagNUW);
-  if (BO->IsNSW)
-    Flags = setFlags(Flags, SCEV::FlagNSW);
+    if (BO->IsNUW)
+      Flags = setFlags(Flags, SCEV::FlagNUW);
+    if (BO->IsNSW)
+      Flags = setFlags(Flags, SCEV::FlagNSW);
+  } else if (auto *GEP = dyn_cast<GetElementPtrInst>(BEValueV);
+             GEP && GEP->getPointerOperand() == PN &&
+             GEP->hasAllConstantIndices()) {
+    APInt Offset(DL.getIndexTypeSizeInBits(PN->getType()), 0);
+    if (!GEP->accumulateConstantOffset(DL, Offset))
+      return nullptr;
+    Accum = getConstant(Offset);
+    if (GEP->isInBounds()) {
+      Flags = setFlags(Flags, SCEV::FlagNW);
+      if (Offset.isNonNegative())
+        Flags = setFlags(Flags, SCEV::FlagNUW);
+    }
+  } else {
+    return nullptr;
+  }
 
   const SCEV *StartVal = getSCEV(StartValueV);
   const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149150.516766.patch
Type: text/x-patch
Size: 2095 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230425/8707204b/attachment.bin>


More information about the llvm-commits mailing list