[llvm] [ValueTracking] Add `matchSimpleBinaryIntrinsicRecurrence` helper (PR #145964)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 26 13:44:31 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-analysis
Author: Antonio Frighetto (antoniofrighetto)
<details>
<summary>Changes</summary>
Similarly to what it is being done to match simple recurrence cycle relations, attempt to match value-accumulating recurrences of kind:
```
%umax.acc = phi i8 [ %umax, %backedge ], [ %a, %entry ]
%umax = call i8 @<!-- -->llvm.umax.i8(i8 %umax.acc, i8 %b)
```
Preliminary work to let InstCombine avoid folding such recurrences, so that simple loop-invariant computation may get hoisted. Minor opportunity to refactor out code as well.
---
Full diff: https://github.com/llvm/llvm-project/pull/145964.diff
2 Files Affected:
- (modified) llvm/include/llvm/Analysis/ValueTracking.h (+16)
- (modified) llvm/lib/Analysis/ValueTracking.cpp (+49-30)
``````````diff
diff --git a/llvm/include/llvm/Analysis/ValueTracking.h b/llvm/include/llvm/Analysis/ValueTracking.h
index c804f551f5a75..384e9915a3bca 100644
--- a/llvm/include/llvm/Analysis/ValueTracking.h
+++ b/llvm/include/llvm/Analysis/ValueTracking.h
@@ -21,6 +21,7 @@
#include "llvm/IR/FMF.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
+#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/Support/Compiler.h"
#include <cassert>
@@ -965,6 +966,21 @@ LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
LLVM_ABI bool matchSimpleRecurrence(const BinaryOperator *I, PHINode *&P,
Value *&Start, Value *&Step);
+/// Attempt to match a simple value-accumulating recurrence of the form:
+/// %llvm.intrinsic.acc = phi Ty [%Init, %Entry], [%llvm.intrinsic, %backedge]
+/// %llvm.intrinsic = call Ty @llvm.intrinsic(%Invariant, %llvm.intrinsic.acc)
+/// OR
+/// %llvm.intrinsic.acc = phi Ty [%Init, %Entry], [%llvm.intrinsic, %backedge]
+/// %llvm.intrinsic = call Ty @llvm.intrinsic(%llvm.intrinsic.acc, %Invariant)
+///
+/// The recurrence relation is of kind:
+/// X_0 = %a (initial value),
+/// X_i = call @llvm.binary.intrinsic(X_i-1, %b)
+/// Where both %a and %b may be loop-invariant.
+LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I,
+ PHINode *&P, Value *&Init,
+ Value *&Invariant);
+
/// Return true if RHS is known to be implied true by LHS. Return false if
/// RHS is known to be implied false by LHS. Otherwise, return std::nullopt if
/// no implication can be made. A & B must be i1 (boolean) values or a vector of
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 93c22212a27ce..fe831236aef54 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -9070,46 +9070,48 @@ llvm::canConvertToMinOrMaxIntrinsic(ArrayRef<Value *> VL) {
return {Intrinsic::not_intrinsic, false};
}
-bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
- Value *&Start, Value *&Step) {
+template <typename InstTy>
+static bool
+matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init,
+ Value *&OtherOp,
+ std::function<bool(const InstTy *)> Pred = nullptr) {
// Handle the case of a simple two-predecessor recurrence PHI.
// There's a lot more that could theoretically be done here, but
// this is sufficient to catch some interesting cases.
// TODO: Expand list -- gep, uadd.sat etc.
- if (P->getNumIncomingValues() != 2)
+ if (PN->getNumIncomingValues() != 2)
return false;
- for (unsigned i = 0; i != 2; ++i) {
- Value *L = P->getIncomingValue(i);
- Value *R = P->getIncomingValue(!i);
- auto *LU = dyn_cast<BinaryOperator>(L);
- if (!LU)
- continue;
- Value *LL = LU->getOperand(0);
- Value *LR = LU->getOperand(1);
-
- // Find a recurrence.
- if (LL == P)
- L = LR;
- else if (LR == P)
- L = LL;
- else
- continue; // Check for recurrence with L and R flipped.
-
- // We have matched a recurrence of the form:
- // %iv = [R, %entry], [%iv.next, %backedge]
- // %iv.next = binop %iv, L
- // OR
- // %iv = [R, %entry], [%iv.next, %backedge]
- // %iv.next = binop L, %iv
- BO = LU;
- Start = R;
- Step = L;
- return true;
+ for (unsigned I = 0; I != 2; ++I) {
+ if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I))) {
+ if (Pred && !(Pred)(Operation))
+ continue;
+
+ Value *LHS = Operation->getOperand(0);
+ Value *RHS = Operation->getOperand(1);
+ if (LHS != PN && RHS != PN)
+ continue;
+
+ Inst = Operation;
+ Init = PN->getIncomingValue(!I);
+ OtherOp = (LHS == PN) ? RHS : LHS;
+ return true;
+ }
}
return false;
}
+bool llvm::matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO,
+ Value *&Start, Value *&Step) {
+ // We have matched a recurrence of the form:
+ // %iv = [Start, %entry], [%iv.next, %backedge]
+ // %iv.next = binop %iv, Step
+ // Or:
+ // %iv = [Start, %entry], [%iv.next, %backedge]
+ // %iv.next = binop Step, %iv
+ return matchTwoInputRecurrence(P, BO, Start, Step);
+}
+
bool llvm::matchSimpleRecurrence(const BinaryOperator *I, PHINode *&P,
Value *&Start, Value *&Step) {
BinaryOperator *BO = nullptr;
@@ -9119,6 +9121,23 @@ 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));
+
+ std::function<bool(const IntrinsicInst *)> IsBinaryOrMinMaxFn =
+ [](const IntrinsicInst *I) {
+ return isa<BinaryOpIntrinsic, MinMaxIntrinsic>(I);
+ };
+ return P &&
+ matchTwoInputRecurrence(P, II, Init, Invariant, IsBinaryOrMinMaxFn) &&
+ II == I;
+}
+
/// Return true if "icmp Pred LHS RHS" is always true.
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
const Value *RHS) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/145964
More information about the llvm-commits
mailing list