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

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 20 08:02:12 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))) ||
----------------
dtcxzyw wrote:

`m_c_BinOp` also matches non-commutative binops. Is this behavior expected?
https://github.com/llvm/llvm-project/blob/8f7ae64ea108de54d9aad963c55e1aef7dc62b86/llvm/include/llvm/IR/PatternMatch.h#L911-L927
https://github.com/llvm/llvm-project/blob/8f7ae64ea108de54d9aad963c55e1aef7dc62b86/llvm/include/llvm/IR/PatternMatch.h#L2410-L2414

It doesn't cause miscompilation because `ConstantExpr::getBinOpIdentity` returns null for non-commutative binops.



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


More information about the llvm-commits mailing list