Skip to content

Commit

Permalink
[ConstraintElim] Add facts for llvm.abs >= 0 (#79070)
Browse files Browse the repository at this point in the history
Add facts for llvm.abs >= 0.

https://alive2.llvm.org/ce/z/GXnMHu
  • Loading branch information
alexander-shaposhnikov committed Feb 6, 2024
1 parent b98db44 commit 4d8e849
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
5 changes: 4 additions & 1 deletion llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,10 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
if (!CB.isConditionFact()) {
Value *X;
if (match(CB.Inst, m_Intrinsic<Intrinsic::abs>(m_Value(X)))) {
// TODO: Add CB.Inst >= 0 fact.
// If is_int_min_poison is true then we may assume llvm.abs >= 0.
if (cast<ConstantInt>(CB.Inst->getOperand(1))->isOne())
AddFact(CmpInst::ICMP_SGE, CB.Inst,
ConstantInt::get(CB.Inst->getType(), 0));
AddFact(CmpInst::ICMP_SGE, CB.Inst, X);
continue;
}
Expand Down
6 changes: 2 additions & 4 deletions llvm/test/Transforms/ConstraintElimination/abs.ll
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ define i1 @abs_is_nonnegative_int_min_is_poison(i32 %arg) {
; CHECK-LABEL: define i1 @abs_is_nonnegative_int_min_is_poison(
; CHECK-SAME: i32 [[ARG:%.*]]) {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 [[ARG]], i1 true)
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS]], 0
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
%abs = tail call i32 @llvm.abs.i32(i32 %arg, i1 true)
%cmp = icmp sge i32 %abs, 0
Expand All @@ -153,8 +152,7 @@ define i1 @abs_is_nonnegative_int_min_is_poison(i32 %arg) {
define i1 @abs_is_nonnegative_constant_arg() {
; CHECK-LABEL: define i1 @abs_is_nonnegative_constant_arg() {
; CHECK-NEXT: [[ABS:%.*]] = tail call i32 @llvm.abs.i32(i32 -3, i1 true)
; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[ABS]], 0
; CHECK-NEXT: ret i1 [[CMP]]
; CHECK-NEXT: ret i1 true
;
%abs = tail call i32 @llvm.abs.i32(i32 -3, i1 true)
%cmp = icmp sge i32 %abs, 0
Expand Down

0 comments on commit 4d8e849

Please sign in to comment.