[PATCH] D147963: [LV] Use VPValue for SCEV expansion in fixupIVUsers (NFCI).

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 10 12:45:43 PDT 2023


fhahn created this revision.
fhahn added reviewers: Ayal, gilr, rengolin.
Herald added subscribers: StephenFan, rogfer01, bollu, hiraditya.
Herald added a project: All.
fhahn requested review of this revision.
Herald added subscribers: pcwang-thead, vkmr.
Herald added a project: LLVM.

The step is already expanded in the VPlan. Use this expansion instead.
This is a step towards modeling fixing up IV users in VPlan.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147963

Files:
  llvm/lib/Transforms/Vectorize/LoopVectorize.cpp


Index: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
===================================================================
--- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -582,7 +582,7 @@
   void fixupIVUsers(PHINode *OrigPhi, const InductionDescriptor &II,
                     Value *VectorTripCount, Value *EndValue,
                     BasicBlock *MiddleBlock, BasicBlock *VectorHeader,
-                    VPlan &Plan);
+                    VPlan &Plan, VPTransformState &State);
 
   /// Handle all cross-iteration phis in the header.
   void fixCrossIterationPHIs(VPTransformState &State);
@@ -3356,7 +3356,8 @@
                                        const InductionDescriptor &II,
                                        Value *VectorTripCount, Value *EndValue,
                                        BasicBlock *MiddleBlock,
-                                       BasicBlock *VectorHeader, VPlan &Plan) {
+                                       BasicBlock *VectorHeader, VPlan &Plan,
+                                       VPTransformState &State) {
   // There are two kinds of external IV usages - those that use the value
   // computed in the last iteration (the PHI) and those that use the penultimate
   // value (the value that feeds into the phi from the loop latch).
@@ -3380,11 +3381,32 @@
   // An external user of the penultimate value need to see EndValue - Step.
   // The simplest way to get this is to recompute it from the constituent SCEVs,
   // that is Start + (Step * (CRD - 1)).
+  Value *Step = nullptr;
   for (User *U : OrigPhi->users()) {
     auto *UI = cast<Instruction>(U);
     if (!OrigLoop->contains(UI)) {
       assert(isa<PHINode>(UI) && "Expected LCSSA form");
 
+      if (!Step) {
+        if (auto *S = dyn_cast<SCEVConstant>(II.getStep()))
+          Step = S->getValue();
+        else if (auto *S = dyn_cast<SCEVUnknown>(II.getStep()))
+          Step = S->getValue();
+        else {
+          // All steps for induction should have been expanded earlier, so this
+          // only looks up existing VPValues.
+          VPBasicBlock *Preheader = Plan.getEntry()->getEntryBasicBlock();
+          for (auto &R : *Preheader) {
+            auto *StepR = dyn_cast<VPExpandSCEVRecipe>(&R);
+            if (StepR && StepR->getSCEV() == II.getStep()) {
+              Step = State.get(StepR, 0);
+              break;
+            }
+          }
+          assert(Step && "need a step here");
+        }
+      }
+
       IRBuilder<> B(MiddleBlock->getTerminator());
 
       // Fast-math-flags propagate from the original induction instruction.
@@ -3394,8 +3416,7 @@
       Value *CountMinusOne = B.CreateSub(
           VectorTripCount, ConstantInt::get(VectorTripCount->getType(), 1));
       CountMinusOne->setName("cmo");
-      Value *Step = CreateStepValue(II.getStep(), *PSE.getSE(),
-                                    VectorHeader->getTerminator());
+
       Value *Escape =
           emitTransformedIndex(B, CountMinusOne, II.getStartValue(), Step, II);
       Escape->setName("ind.escape");
@@ -3754,7 +3775,7 @@
       fixupIVUsers(Entry.first, Entry.second,
                    getOrCreateVectorTripCount(VectorLoop->getLoopPreheader()),
                    IVEndValues[Entry.first], LoopMiddleBlock,
-                   VectorLoop->getHeader(), Plan);
+                   VectorLoop->getHeader(), Plan, State);
   }
 
   // Fix LCSSA phis not already fixed earlier. Extracts may need to be generated


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147963.512226.patch
Type: text/x-patch
Size: 3532 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230410/aa3c2d95/attachment.bin>


More information about the llvm-commits mailing list