[llvm] [ConstraintElim] Simplify `MinMaxIntrinsic` (PR #75306)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sat Dec 23 10:06:32 PST 2023
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/75306
>From e46c8f176f9830f0604ae3c85e5f9c1c0e9e9f5e Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 13 Dec 2023 23:41:16 +0800
Subject: [PATCH 1/2] [ConstraintElim] Add pre-commit tests for PR75155. NFC.
---
.../ConstraintElimination/minmax.ll | 265 ++++++++++++++++++
1 file changed, 265 insertions(+)
diff --git a/llvm/test/Transforms/ConstraintElimination/minmax.ll b/llvm/test/Transforms/ConstraintElimination/minmax.ll
index a31cf6845ad67d..952285b5e0b37c 100644
--- a/llvm/test/Transforms/ConstraintElimination/minmax.ll
+++ b/llvm/test/Transforms/ConstraintElimination/minmax.ll
@@ -341,6 +341,271 @@ end:
ret i32 0
}
+; Test from PR75155
+define i32 @simplify_slt_smax_val(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @simplify_slt_smax_val
+; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: start:
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A]], [[B]]
+; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; CHECK: then:
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 1
+; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[B]], i32 [[ADD]])
+; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK: else:
+; CHECK-NEXT: ret i32 -1
+;
+start:
+ %cmp = icmp slt i32 %a, %b
+ br i1 %cmp, label %then, label %else
+then:
+ %add = add nsw i32 %a, 1
+ %max = call i32 @llvm.smax.i32(i32 %b, i32 %add)
+ ret i32 %max
+else:
+ ret i32 -1
+}
+
+define i32 @simplify_slt_smax_val_commuted(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @simplify_slt_smax_val_commuted
+; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: start:
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A]], [[B]]
+; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; CHECK: then:
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 1
+; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[ADD]], i32 [[B]])
+; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK: else:
+; CHECK-NEXT: ret i32 -1
+;
+start:
+ %cmp = icmp slt i32 %a, %b
+ br i1 %cmp, label %then, label %else
+then:
+ %add = add nsw i32 %a, 1
+ %max = call i32 @llvm.smax.i32(i32 %add, i32 %b)
+ ret i32 %max
+else:
+ ret i32 -1
+}
+
+define i32 @simplify_slt_smax_val_at_use(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @simplify_slt_smax_val_at_use
+; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: start:
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A]], [[B]]
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 1
+; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[B]], i32 [[ADD]])
+; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; CHECK: then:
+; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK: else:
+; CHECK-NEXT: ret i32 -1
+;
+start:
+ %cmp = icmp slt i32 %a, %b
+ %add = add nsw i32 %a, 1
+ %max = call i32 @llvm.smax.i32(i32 %b, i32 %add)
+ br i1 %cmp, label %then, label %else
+then:
+ ret i32 %max
+else:
+ ret i32 -1
+}
+
+define i32 @simplify_sgt_smax_val(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @simplify_sgt_smax_val
+; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: start:
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[A]], [[B]]
+; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; CHECK: then:
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 1
+; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[B]], i32 [[ADD]])
+; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK: else:
+; CHECK-NEXT: ret i32 -1
+;
+start:
+ %cmp = icmp sgt i32 %a, %b
+ br i1 %cmp, label %then, label %else
+then:
+ %add = add nsw i32 %a, 1
+ %max = call i32 @llvm.smax.i32(i32 %b, i32 %add)
+ ret i32 %max
+else:
+ ret i32 -1
+}
+
+define i32 @simplify_sle_smax_val(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @simplify_sle_smax_val
+; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: start:
+; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[A]], [[B]]
+; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; CHECK: then:
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 1
+; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[B]], i32 [[ADD]])
+; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK: else:
+; CHECK-NEXT: ret i32 -1
+;
+start:
+ %cmp = icmp sle i32 %a, %b
+ br i1 %cmp, label %then, label %else
+then:
+ %add = add nsw i32 %a, 1
+ %max = call i32 @llvm.smax.i32(i32 %b, i32 %add)
+ ret i32 %max
+else:
+ ret i32 -1
+}
+
+define i32 @simplify_sge_smax_val(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @simplify_sge_smax_val
+; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: start:
+; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[A]], [[B]]
+; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; CHECK: then:
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 1
+; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[B]], i32 [[ADD]])
+; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK: else:
+; CHECK-NEXT: ret i32 -1
+;
+start:
+ %cmp = icmp sge i32 %a, %b
+ br i1 %cmp, label %then, label %else
+then:
+ %add = add nsw i32 %a, 1
+ %max = call i32 @llvm.smax.i32(i32 %b, i32 %add)
+ ret i32 %max
+else:
+ ret i32 -1
+}
+
+define i32 @simplify_ult_umax_val(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @simplify_ult_umax_val
+; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: start:
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[A]], [[B]]
+; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; CHECK: then:
+; CHECK-NEXT: [[ADD:%.*]] = add nuw i32 [[A]], 1
+; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.umax.i32(i32 [[B]], i32 [[ADD]])
+; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK: else:
+; CHECK-NEXT: ret i32 -1
+;
+start:
+ %cmp = icmp ult i32 %a, %b
+ br i1 %cmp, label %then, label %else
+then:
+ %add = add nuw i32 %a, 1
+ %max = call i32 @llvm.umax.i32(i32 %b, i32 %add)
+ ret i32 %max
+else:
+ ret i32 -1
+}
+
+define i32 @simplify_slt_smin_val(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @simplify_slt_smin_val
+; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: start:
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A]], [[B]]
+; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; CHECK: then:
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 1
+; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smin.i32(i32 [[B]], i32 [[ADD]])
+; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK: else:
+; CHECK-NEXT: ret i32 -1
+;
+start:
+ %cmp = icmp slt i32 %a, %b
+ br i1 %cmp, label %then, label %else
+then:
+ %add = add nsw i32 %a, 1
+ %max = call i32 @llvm.smin.i32(i32 %b, i32 %add)
+ ret i32 %max
+else:
+ ret i32 -1
+}
+
+define i32 @simplify_ult_umin_val(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @simplify_ult_umin_val
+; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: start:
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[A]], [[B]]
+; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; CHECK: then:
+; CHECK-NEXT: [[ADD:%.*]] = add nuw i32 [[A]], 1
+; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.umin.i32(i32 [[B]], i32 [[ADD]])
+; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK: else:
+; CHECK-NEXT: ret i32 -1
+;
+start:
+ %cmp = icmp ult i32 %a, %b
+ br i1 %cmp, label %then, label %else
+then:
+ %add = add nuw i32 %a, 1
+ %max = call i32 @llvm.umin.i32(i32 %b, i32 %add)
+ ret i32 %max
+else:
+ ret i32 -1
+}
+
+define i32 @simplify_slt_smax_val_fail1(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @simplify_slt_smax_val_fail1
+; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: start:
+; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[A]], [[B]]
+; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; CHECK: then:
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 2
+; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[B]], i32 [[ADD]])
+; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK: else:
+; CHECK-NEXT: ret i32 -1
+;
+start:
+ %cmp = icmp slt i32 %a, %b
+ br i1 %cmp, label %then, label %else
+then:
+ %add = add nsw i32 %a, 2
+ %max = call i32 @llvm.smax.i32(i32 %b, i32 %add)
+ ret i32 %max
+else:
+ ret i32 -1
+}
+
+define i32 @simplify_ult_smax_val_fail2(i32 %a, i32 %b) {
+; CHECK-LABEL: define i32 @simplify_ult_smax_val_fail2
+; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT: start:
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[A]], [[B]]
+; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
+; CHECK: then:
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 1
+; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[B]], i32 [[ADD]])
+; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK: else:
+; CHECK-NEXT: ret i32 -1
+;
+start:
+ %cmp = icmp ult i32 %a, %b
+ br i1 %cmp, label %then, label %else
+then:
+ %add = add nsw i32 %a, 1
+ %max = call i32 @llvm.smax.i32(i32 %b, i32 %add)
+ ret i32 %max
+else:
+ ret i32 -1
+}
+
declare i32 @llvm.smin.i32(i32, i32)
declare i32 @llvm.smax.i32(i32, i32)
declare i32 @llvm.umin.i32(i32, i32)
>From b37cdf8e6485c9c5b3e830370af6709910537d0c Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sun, 24 Dec 2023 01:56:40 +0800
Subject: [PATCH 2/2] [ConstraintElim] Simplify `MinMaxIntrinsic`
---
.../Scalar/ConstraintElimination.cpp | 47 +++++++++++++++++++
.../ConstraintElimination/minmax.ll | 21 +++------
2 files changed, 54 insertions(+), 14 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 98cfadddee8efb..b725f3bb46cdde 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1009,6 +1009,8 @@ void State::addInfoFor(BasicBlock &BB) {
if (isa<MinMaxIntrinsic>(&I)) {
WorkList.push_back(FactOrCheck::getInstFact(DT.getNode(&BB), &I));
+ WorkList.push_back(
+ FactOrCheck::getCheck(DT.getNode(&BB), cast<CallInst>(&I)));
continue;
}
@@ -1349,6 +1351,47 @@ static bool checkAndReplaceCondition(
return false;
}
+static bool checkAndReplaceMinMax(MinMaxIntrinsic *MinMax, ConstraintInfo &Info,
+ unsigned NumIn, unsigned NumOut,
+ Instruction *ContextInst,
+ Module *ReproducerModule,
+ ArrayRef<ReproducerEntry> ReproducerCondStack,
+ DominatorTree &DT,
+ SmallVectorImpl<Instruction *> &ToRemove) {
+ auto ReplaceMinMaxWithOperand = [&](MinMaxIntrinsic *MinMax, bool UseLHS) {
+ // TODO: generate reproducer for min/max.
+ MinMax->replaceUsesWithIf(MinMax->getOperand(UseLHS ? 0 : 1),
+ [&DT, NumIn, NumOut, ContextInst](Use &U) {
+ auto *UserI = getContextInstForUse(U);
+ auto *DTN = DT.getNode(UserI->getParent());
+ if (!DTN || DTN->getDFSNumIn() < NumIn ||
+ DTN->getDFSNumOut() > NumOut)
+ return false;
+ if (UserI->getParent() ==
+ ContextInst->getParent() &&
+ UserI->comesBefore(ContextInst))
+ return false;
+
+ return true;
+ });
+ NumCondsRemoved++;
+ if (MinMax->use_empty())
+ ToRemove.push_back(MinMax);
+ return true;
+ };
+
+ if (auto ImpliedCondition = checkCondition(
+ MinMax->getPredicate(), MinMax->getOperand(0), MinMax->getOperand(1),
+ MinMax, Info, NumIn, NumOut, ContextInst))
+ return ReplaceMinMaxWithOperand(MinMax, *ImpliedCondition);
+ if (auto ImpliedCondition = checkCondition(
+ ICmpInst::getNonStrictPredicate(MinMax->getPredicate()),
+ MinMax->getOperand(0), MinMax->getOperand(1), MinMax, Info, NumIn,
+ NumOut, ContextInst))
+ return ReplaceMinMaxWithOperand(MinMax, *ImpliedCondition);
+ return false;
+}
+
static void
removeEntryFromStack(const StackEntry &E, ConstraintInfo &Info,
Module *ReproducerModule,
@@ -1640,6 +1683,10 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
ReproducerCondStack, DFSInStack);
}
Changed |= Simplified;
+ } else if (auto *MinMax = dyn_cast<MinMaxIntrinsic>(Inst)) {
+ Changed |= checkAndReplaceMinMax(
+ MinMax, Info, CB.NumIn, CB.NumOut, CB.getContextInst(),
+ ReproducerModule.get(), ReproducerCondStack, S.DT, ToRemove);
}
continue;
}
diff --git a/llvm/test/Transforms/ConstraintElimination/minmax.ll b/llvm/test/Transforms/ConstraintElimination/minmax.ll
index 952285b5e0b37c..bbb337eaf3e26d 100644
--- a/llvm/test/Transforms/ConstraintElimination/minmax.ll
+++ b/llvm/test/Transforms/ConstraintElimination/minmax.ll
@@ -350,8 +350,7 @@ define i32 @simplify_slt_smax_val(i32 %a, i32 %b) {
; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK: then:
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 1
-; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[B]], i32 [[ADD]])
-; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK-NEXT: ret i32 [[B]]
; CHECK: else:
; CHECK-NEXT: ret i32 -1
;
@@ -374,8 +373,7 @@ define i32 @simplify_slt_smax_val_commuted(i32 %a, i32 %b) {
; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK: then:
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 1
-; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[ADD]], i32 [[B]])
-; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK-NEXT: ret i32 [[B]]
; CHECK: else:
; CHECK-NEXT: ret i32 -1
;
@@ -422,8 +420,7 @@ define i32 @simplify_sgt_smax_val(i32 %a, i32 %b) {
; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK: then:
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 1
-; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[B]], i32 [[ADD]])
-; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK-NEXT: ret i32 [[ADD]]
; CHECK: else:
; CHECK-NEXT: ret i32 -1
;
@@ -470,8 +467,7 @@ define i32 @simplify_sge_smax_val(i32 %a, i32 %b) {
; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK: then:
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 1
-; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smax.i32(i32 [[B]], i32 [[ADD]])
-; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK-NEXT: ret i32 [[ADD]]
; CHECK: else:
; CHECK-NEXT: ret i32 -1
;
@@ -494,8 +490,7 @@ define i32 @simplify_ult_umax_val(i32 %a, i32 %b) {
; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK: then:
; CHECK-NEXT: [[ADD:%.*]] = add nuw i32 [[A]], 1
-; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.umax.i32(i32 [[B]], i32 [[ADD]])
-; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK-NEXT: ret i32 [[B]]
; CHECK: else:
; CHECK-NEXT: ret i32 -1
;
@@ -518,8 +513,7 @@ define i32 @simplify_slt_smin_val(i32 %a, i32 %b) {
; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK: then:
; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[A]], 1
-; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.smin.i32(i32 [[B]], i32 [[ADD]])
-; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK-NEXT: ret i32 [[ADD]]
; CHECK: else:
; CHECK-NEXT: ret i32 -1
;
@@ -542,8 +536,7 @@ define i32 @simplify_ult_umin_val(i32 %a, i32 %b) {
; CHECK-NEXT: br i1 [[CMP]], label [[THEN:%.*]], label [[ELSE:%.*]]
; CHECK: then:
; CHECK-NEXT: [[ADD:%.*]] = add nuw i32 [[A]], 1
-; CHECK-NEXT: [[MAX:%.*]] = call i32 @llvm.umin.i32(i32 [[B]], i32 [[ADD]])
-; CHECK-NEXT: ret i32 [[MAX]]
+; CHECK-NEXT: ret i32 [[ADD]]
; CHECK: else:
; CHECK-NEXT: ret i32 -1
;
More information about the llvm-commits
mailing list