Skip to content

Commit 4d8e849

Browse files
[ConstraintElim] Add facts for llvm.abs >= 0 (#79070)
Add facts for llvm.abs >= 0. https://alive2.llvm.org/ce/z/GXnMHu
1 parent b98db44 commit 4d8e849

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1755,7 +1755,10 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
17551755
if (!CB.isConditionFact()) {
17561756
Value *X;
17571757
if (match(CB.Inst, m_Intrinsic<Intrinsic::abs>(m_Value(X)))) {
1758-
// TODO: Add CB.Inst >= 0 fact.
1758+
// If is_int_min_poison is true then we may assume llvm.abs >= 0.
1759+
if (cast<ConstantInt>(CB.Inst->getOperand(1))->isOne())
1760+
AddFact(CmpInst::ICMP_SGE, CB.Inst,
1761+
ConstantInt::get(CB.Inst->getType(), 0));
17591762
AddFact(CmpInst::ICMP_SGE, CB.Inst, X);
17601763
continue;
17611764
}

llvm/test/Transforms/ConstraintElimination/abs.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,7 @@ define i1 @abs_is_nonnegative_int_min_is_poison(i32 %arg) {
142142
; CHECK-LABEL: define i1 @abs_is_nonnegative_int_min_is_poison(
143143
; CHECK-SAME: i32 [[ARG:%.*]]) {
144144
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
145-
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS]], 0
146-
; CHECK-NEXT: ret i1 [[CMP]]
145+
; CHECK-NEXT: ret i1 true
147146
;
148147
%abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
149148
%cmp = icmp sge i32 %abs, 0
@@ -153,8 +152,7 @@ define i1 @abs_is_nonnegative_int_min_is_poison(i32 %arg) {
153152
define i1 @abs_is_nonnegative_constant_arg() {
154153
; CHECK-LABEL: define i1 @abs_is_nonnegative_constant_arg() {
155154
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 -3, i1 true)
156-
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS]], 0
157-
; CHECK-NEXT: ret i1 [[CMP]]
155+
; CHECK-NEXT: ret i1 true
158156
;
159157
%abs = tail call i32 @llvm.abs.i32(i32 -3, i1 true)
160158
%cmp = icmp sge i32 %abs, 0

0 commit comments

Comments
 (0)