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

via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 19 09:17:24 PST 2024


================
@@ -1378,6 +1378,47 @@ static Value *simplifyUsingControlFlow(InstCombiner &Self, PHINode &PN,
   return nullptr;
 }
 
+// Fold  iv = phi(start, iv.next = iv2.next + start)
+// where iv2 = phi(iv2.start, iv2.next = iv2 + iv2.step)
+// to    iv = iv2 + 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_Add(m_Specific(V1), m_BinOp(Iv2Next))) ||
----------------
goldsteinn wrote:

What about using `ConstantExpr::getBinOpIdentity`?

Then really its just change the `!match(Iv2Start, m_Zero())` -> `!match(Iv2Start, m_SpecificInt(Identity))`

If you want to leave `AllowRHSConstant` as a todo, then all that is left is `m_c_Add` -> `m_c_BinOp`.

Seems roughly same impl complexity + more principalled.

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


More information about the llvm-commits mailing list