[PATCH] D136417: [LSR] Allow terminating condition fold on start values by BinaryOperator

Yueh-Ting (eop) Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 20 21:19:52 PDT 2022


eopXD created this revision.
eopXD added reviewers: JojoR, fhahn, Meinersbur, craig.topper.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
eopXD requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Orginally only GEP instructions are allowed, this patch allows BinaryOperator
instructions to pass too.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136417

Files:
  llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp


Index: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -6677,13 +6677,15 @@
             AddRec->getOperand(0)->getType()));
     const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
     SCEVExpander Expander(SE, DL, "lsr_fold_term_cond");
+    if (!Expander.isSafeToExpand(TermValueS))
+      return false;
 
-    // TODO: Right now we limit the phi node to help the folding be of a start
-    // value of getelementptr. We can extend to any kinds of IV as long as it is
-    // an affine AddRec. Add a switch to cover more types of instructions here
-    // and down in the actual transformation.
-    return Expander.isSafeToExpand(TermValueS) &&
-           isa<GetElementPtrInst>(PN.getIncomingValueForBlock(LoopPreheader));
+    Value *StartValue = PN.getIncomingValueForBlock(LoopPreheader);
+    // Note: Expanding more types of StartValue requires corresponding change
+    // for `TypeToExpand` below
+    if (isa<GetElementPtrInst>(StartValue) || isa<BinaryOperator>(StartValue))
+      return true;
+    return false;
   };
 
   PHINode *ToFold = nullptr;
@@ -6810,8 +6812,16 @@
       SCEVExpanderCleaner ExpCleaner(Expander);
 
       // Create new terminating value at loop header
-      GetElementPtrInst *StartValueGEP = cast<GetElementPtrInst>(StartValue);
-      Type *PtrTy = StartValueGEP->getPointerOperand()->getType();
+      Type *TypeToExpand = nullptr;
+      if (isa<GetElementPtrInst>(StartValue)) {
+        TypeToExpand =
+            cast<GetElementPtrInst>(StartValue)->getPointerOperandType();
+      } else if (isa<BinaryOperator>(StartValue)) {
+        TypeToExpand = StartValue->getType();
+      } else {
+        llvm_unreachable("Unhandled type should be guarded by IsToHelpFold "
+                         "under canFoldTermCondOfLoop");
+      }
 
       const SCEV *BECount = SE.getBackedgeTakenCount(L);
       const SCEVAddRecExpr *AddRec =
@@ -6831,7 +6841,7 @@
       Changed = true;
       NumTermFold++;
 
-      Value *TermValue = Expander.expandCodeFor(TermValueS, PtrTy,
+      Value *TermValue = Expander.expandCodeFor(TermValueS, TypeToExpand,
                                                 LoopPreheader->getTerminator());
 
       LLVM_DEBUG(dbgs() << "Start value of new term-cond phi-node:\n"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136417.469466.patch
Type: text/x-patch
Size: 2452 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221021/f57a8a47/attachment.bin>


More information about the llvm-commits mailing list