Skip to content

Recommit "[ConstraintElim] Simplify cmp after uadd.sat/usub.sat (#135603)" #136467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: users/el-ev/04-18-_constraintelim_fix_poison_check_before_adding_intrinsic_facts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,8 @@ void State::addInfoFor(BasicBlock &BB) {
// when simplifying uses of the min/max intrinsics.
[[fallthrough]];
case Intrinsic::abs:
case Intrinsic::uadd_sat:
case Intrinsic::usub_sat:
if (!isGuaranteedNotToBePoison(&I))
break;
WorkList.push_back(FactOrCheck::getInstFact(DT.getNode(&BB), &I));
Expand Down Expand Up @@ -1896,6 +1898,20 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
AddFact(Pred, MinMax, MinMax->getRHS());
continue;
}
if (auto *USatI = dyn_cast<SaturatingInst>(CB.Inst)) {
switch (USatI->getIntrinsicID()) {
default:
llvm_unreachable("Unexpected intrinsic.");
case Intrinsic::uadd_sat:
AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getLHS());
AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getRHS());
break;
case Intrinsic::usub_sat:
AddFact(ICmpInst::ICMP_ULE, USatI, USatI->getLHS());
break;
}
continue;
}
}

Value *A = nullptr, *B = nullptr;
Expand Down
7 changes: 2 additions & 5 deletions llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ define i1 @uadd_sat_uge(i64 noundef %a, i64 noundef %b) {
; CHECK-LABEL: define i1 @uadd_sat_uge(
; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
; CHECK-NEXT: [[ADD_SAT:%.*]] = call i64 @llvm.uadd.sat.i64(i64 [[A]], i64 [[B]])
; CHECK-NEXT: [[CMP1:%.*]] = icmp uge i64 [[ADD_SAT]], [[A]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp uge i64 [[ADD_SAT]], [[B]]
; CHECK-NEXT: [[CMP:%.*]] = and i1 [[CMP1]], [[CMP2]]
; CHECK-NEXT: [[CMP:%.*]] = and i1 true, true
; CHECK-NEXT: ret i1 [[CMP]]
;
%add.sat = call i64 @llvm.uadd.sat.i64(i64 %a, i64 %b)
Expand All @@ -24,8 +22,7 @@ define i1 @usub_sat_ule_lhs(i64 noundef %a, i64 noundef %b) {
; CHECK-LABEL: define i1 @usub_sat_ule_lhs(
; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
; CHECK-NEXT: [[SUB_SAT:%.*]] = call i64 @llvm.usub.sat.i64(i64 [[A]], i64 [[B]])
; CHECK-NEXT: [[CMP:%.*]] = icmp ule i64 [[SUB_SAT]], [[A]]
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
%sub.sat = call i64 @llvm.usub.sat.i64(i64 %a, i64 %b)
%cmp = icmp ule i64 %sub.sat, %a
Expand Down
Loading