[llvm] [ValueTracking] Add `matchSimpleBinaryIntrinsicRecurrence` helper (PR #145964)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 27 03:45:59 PDT 2025


================
@@ -9119,6 +9120,28 @@ bool llvm::matchSimpleRecurrence(const BinaryOperator *I, PHINode *&P,
   return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
 }
 
+bool llvm::matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I,
+                                                PHINode *&P, Value *&Init,
+                                                Value *&Invariant) {
+  IntrinsicInst *II = nullptr;
+  P = dyn_cast<PHINode>(I->getArgOperand(0));
+  if (!P)
+    P = dyn_cast<PHINode>(I->getArgOperand(1));
+
+  return P &&
+         matchTwoInputRecurrence<IntrinsicInst>(
+             P, II, Init, Invariant,
+             [](const IntrinsicInst *I) {
+               Intrinsic::ID IID = I->getIntrinsicID();
+               return isa<SaturatingInst, MinMaxIntrinsic>(I) ||
+                      IID == Intrinsic::minnum || IID == Intrinsic::maxnum ||
+                      IID == Intrinsic::minimum || IID == Intrinsic::maximum ||
+                      IID == Intrinsic::minimumnum ||
+                      IID == Intrinsic::maximumnum;
+             }) &&
----------------
antoniofrighetto wrote:

Not sure got this, the caller here for checking the intrinsic IID, the one to be set, should be matchSimpleBinaryIntrinsicRecurrence itself. The lambda is used to check whether the intrinsic found in matchTwoInputRecurrence is the expected one. We can check that `I` is a two argument intrinsic, but we would still need to check, once matchTwoInputRecurrence returns, that the new intrinsic is the expected one.

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


More information about the llvm-commits mailing list