Skip to content

Commit 66a876c

Browse files
committed
"Recommit "[ConstraintElim] Simplify cmp after uadd.sat/usub.sat (#135603)"
1 parent f4bf37b commit 66a876c

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,8 @@ void State::addInfoFor(BasicBlock &BB) {
11391139
// when simplifying uses of the min/max intrinsics.
11401140
[[fallthrough]];
11411141
case Intrinsic::abs:
1142+
case Intrinsic::uadd_sat:
1143+
case Intrinsic::usub_sat:
11421144
if (!isGuaranteedNotToBePoison(&I))
11431145
break;
11441146
WorkList.push_back(FactOrCheck::getInstFact(DT.getNode(&BB), &I));
@@ -1896,6 +1898,20 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
18961898
AddFact(Pred, MinMax, MinMax->getRHS());
18971899
continue;
18981900
}
1901+
if (auto *USatI = dyn_cast<SaturatingInst>(CB.Inst)) {
1902+
switch (USatI->getIntrinsicID()) {
1903+
default:
1904+
llvm_unreachable("Unexpected intrinsic.");
1905+
case Intrinsic::uadd_sat:
1906+
AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getLHS());
1907+
AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getRHS());
1908+
break;
1909+
case Intrinsic::usub_sat:
1910+
AddFact(ICmpInst::ICMP_ULE, USatI, USatI->getLHS());
1911+
break;
1912+
}
1913+
continue;
1914+
}
18991915
}
19001916

19011917
Value *A = nullptr, *B = nullptr;

llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ define i1 @uadd_sat_uge(i64 noundef %a, i64 noundef %b) {
88
; CHECK-LABEL: define i1 @uadd_sat_uge(
99
; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
1010
; CHECK-NEXT: [[ADD_SAT:%.*]] = call i64 @llvm.uadd.sat.i64(i64 [[A]], i64 [[B]])
11-
; CHECK-NEXT: [[CMP1:%.*]] = icmp uge i64 [[ADD_SAT]], [[A]]
12-
; CHECK-NEXT: [[CMP2:%.*]] = icmp uge i64 [[ADD_SAT]], [[B]]
13-
; CHECK-NEXT: [[CMP:%.*]] = and i1 [[CMP1]], [[CMP2]]
11+
; CHECK-NEXT: [[CMP:%.*]] = and i1 true, true
1412
; CHECK-NEXT: ret i1 [[CMP]]
1513
;
1614
%add.sat = call i64 @llvm.uadd.sat.i64(i64 %a, i64 %b)
@@ -24,8 +22,7 @@ define i1 @usub_sat_ule_lhs(i64 noundef %a, i64 noundef %b) {
2422
; CHECK-LABEL: define i1 @usub_sat_ule_lhs(
2523
; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
2624
; CHECK-NEXT: [[SUB_SAT:%.*]] = call i64 @llvm.usub.sat.i64(i64 [[A]], i64 [[B]])
27-
; CHECK-NEXT: [[CMP:%.*]] = icmp ule i64 [[SUB_SAT]], [[A]]
28-
; CHECK-NEXT: ret i1 [[CMP]]
25+
; CHECK-NEXT: ret i1 true
2926
;
3027
%sub.sat = call i64 @llvm.usub.sat.i64(i64 %a, i64 %b)
3128
%cmp = icmp ule i64 %sub.sat, %a

0 commit comments

Comments
 (0)