[llvm] [LoopVectorize] Add support for vectorisation of simple early exit loops (PR #88385)

Sjoerd Meijer via llvm-commits llvm-commits at lists.llvm.org
Wed May 1 03:08:08 PDT 2024


================
@@ -9928,15 +9928,74 @@ static void checkMixedPrecision(Loop *L, OptimizationRemarkEmitter *ORE) {
   }
 }
 
-static bool areRuntimeChecksProfitable(GeneratedRTChecks &Checks,
-                                       VectorizationFactor &VF,
-                                       std::optional<unsigned> VScale, Loop *L,
-                                       ScalarEvolution &SE,
-                                       ScalarEpilogueLowering SEL) {
+static InstructionCost calculateEarlyExitCost(const TargetTransformInfo *TTI,
+                                              LoopVectorizationLegality *Legal,
+                                              Loop *L, ElementCount VF) {
+  unsigned NumCttzElemCalls = 0;
+  BasicBlock *OrigEarlyExitingBlock = Legal->getSpeculativeEarlyExitingBlock();
+  BasicBlock *OrigLoopLatch = L->getLoopLatch();
+  for (const auto &Entry : Legal->getInductionVars()) {
+    PHINode *OrigPhi = Entry.first;
+    Value *PostInc = OrigPhi->getIncomingValueForBlock(OrigLoopLatch);
+
+    for (User *U : PostInc->users()) {
+      // This assumes if it's not in the loop then it must be the normal
+      // exit block. However, it could be a user in an early exit block
+      // different to the latch's exit block.
+      auto *UI = cast<Instruction>(U);
+      if (!L->contains(UI)) {
+        PHINode *PHI = dyn_cast<PHINode>(UI);
+        assert(PHI && "Expected LCSSA form");
+        int Index = PHI->getBasicBlockIndex(OrigEarlyExitingBlock);
+        if (Index != -1 && PHI->getIncomingValue(Index) == PostInc)
+          NumCttzElemCalls++;
+      }
+    }
+
+    for (User *U : OrigPhi->users()) {
----------------
sjoerdmeijer wrote:

Nit: this could be lamba, to share code with the previous for-loop that is almost the same?

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


More information about the llvm-commits mailing list