[llvm] [LoopInterchange] Support inner-loop simple reductions via UndoSimpleReduction (PR #172970)

Yingying Wang via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 24 19:09:47 PST 2025


================
@@ -1035,14 +1040,71 @@ findInnerReductionPhi(Loop *L, Value *V,
         return PHI;
       else
         return nullptr;
+    }
+  }
 
       return nullptr;
     }
+
+static PHINode *getCounterFromInc(Value *IncV, Loop *L) {
+  Instruction *IncI = dyn_cast<Instruction>(IncV);
+  if (!IncI)
+    return nullptr;
+
+  if (IncI->getOpcode() != Instruction::Add &&
+      IncI->getOpcode() != Instruction::Sub)
+    return nullptr;
+
+  PHINode *Phi = dyn_cast<PHINode>(IncI->getOperand(0));
+  if (Phi && Phi->getParent() == L->getHeader()) {
+    return Phi;
+  }
+
+  // Allow add/sub to be commuted.
+  Phi = dyn_cast<PHINode>(IncI->getOperand(1));
+  if (Phi && Phi->getParent() == L->getHeader()) {
+    return Phi;
   }
 
   return nullptr;
 }
 
+/// UndoSimpleReduction requires the first_iteration check, so look for
+/// the IV used for the loop exit condition
+static PHINode *findCounterIV(Loop *L) {
----------------
buggfg wrote:

After defining First_Iteration as a PHI node, there is no longer a need for `findCounterIV()`.

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


More information about the llvm-commits mailing list