Skip to content

Commit b54b3e8

Browse files
authored
Recommit "[ConstraintElim] Simplify cmp after uadd.sat/usub.sat (#135603)" (#136467)
1 parent a5fef2a commit b54b3e8

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,9 @@ void State::addInfoFor(BasicBlock &BB) {
11351135
// TODO: handle llvm.abs as well
11361136
WorkList.push_back(
11371137
FactOrCheck::getCheck(DT.getNode(&BB), cast<CallInst>(&I)));
1138+
[[fallthrough]];
1139+
case Intrinsic::uadd_sat:
1140+
case Intrinsic::usub_sat:
11381141
// TODO: Check if it is possible to instead only added the min/max facts
11391142
// when simplifying uses of the min/max intrinsics.
11401143
if (!isGuaranteedNotToBePoison(&I))
@@ -1896,6 +1899,20 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
18961899
AddFact(Pred, MinMax, MinMax->getRHS());
18971900
continue;
18981901
}
1902+
if (auto *USatI = dyn_cast<SaturatingInst>(CB.Inst)) {
1903+
switch (USatI->getIntrinsicID()) {
1904+
default:
1905+
llvm_unreachable("Unexpected intrinsic.");
1906+
case Intrinsic::uadd_sat:
1907+
AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getLHS());
1908+
AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getRHS());
1909+
break;
1910+
case Intrinsic::usub_sat:
1911+
AddFact(ICmpInst::ICMP_ULE, USatI, USatI->getLHS());
1912+
break;
1913+
}
1914+
continue;
1915+
}
18991916
}
19001917

19011918
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)