[llvm] [InstCombine] Fold dependent IVs (PR #81151)

via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 20 07:17:04 PST 2024


================
@@ -1378,6 +1378,58 @@ static Value *simplifyUsingControlFlow(InstCombiner &Self, PHINode &PN,
   return nullptr;
 }
 
+// Fold  iv = phi(start, iv.next = iv2.next op start)
+// where iv2 = phi(iv2.start, iv2.next = iv2 + iv2.step)
+// and   iv2.start op start = start
+// to    iv = iv2 op start
+static Value *foldDependentIVs(PHINode &PN, IRBuilderBase &Builder) {
+  BasicBlock *BB = PN.getParent();
+  if (PN.getNumIncomingValues() != 2)
+    return nullptr;
+
+  Value *Start;
+  Instruction *IvNext;
+  BinaryOperator *Iv2Next;
+  auto MatchOuterIV = [&](Value *V1, Value *V2) {
+    if (match(V2, m_c_BinOp(m_Specific(V1), m_BinOp(Iv2Next))) ||
----------------
goldsteinn wrote:

nit: Maybe add a todo for detecting if `Iv2Next` is rhs/lhs to cover more binops (like sub).

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


More information about the llvm-commits mailing list