Skip to content

Commit 44b7f85

Browse files
fhahnLukacma
authored andcommitted
[ConstraintElim] Apply add with neg constant first during decomp. (llvm#164791)
We have dedicated decomposition logic for (add %x, -C), but if we have (add nsw %x, -C) we will first apply the generic logic for NSWAdd, which gives worse results for negative constants in practice. Update the code to first apply the pattern with negative constants. Helps to remove a number of runtime checks in practice: dtcxzyw/llvm-opt-benchmark#2968 Alive2 proofs for the test changes: https://alive2.llvm.org/ce/z/JfR2Ma PR: llvm#164791
1 parent a35f1fc commit 44b7f85

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,16 @@ static Decomposition decompose(Value *V,
614614
return {V, IsKnownNonNegative};
615615
}
616616

617+
if (match(V, m_Add(m_Value(Op0), m_ConstantInt(CI))) && CI->isNegative() &&
618+
canUseSExt(CI)) {
619+
Preconditions.emplace_back(
620+
CmpInst::ICMP_UGE, Op0,
621+
ConstantInt::get(Op0->getType(), CI->getSExtValue() * -1));
622+
if (auto Decomp = MergeResults(Op0, CI, true))
623+
return *Decomp;
624+
return {V, IsKnownNonNegative};
625+
}
626+
617627
if (match(V, m_NSWAdd(m_Value(Op0), m_Value(Op1)))) {
618628
if (!isKnownNonNegative(Op0, DL))
619629
Preconditions.emplace_back(CmpInst::ICMP_SGE, Op0,
@@ -627,16 +637,6 @@ static Decomposition decompose(Value *V,
627637
return {V, IsKnownNonNegative};
628638
}
629639

630-
if (match(V, m_Add(m_Value(Op0), m_ConstantInt(CI))) && CI->isNegative() &&
631-
canUseSExt(CI)) {
632-
Preconditions.emplace_back(
633-
CmpInst::ICMP_UGE, Op0,
634-
ConstantInt::get(Op0->getType(), CI->getSExtValue() * -1));
635-
if (auto Decomp = MergeResults(Op0, CI, true))
636-
return *Decomp;
637-
return {V, IsKnownNonNegative};
638-
}
639-
640640
// Decompose or as an add if there are no common bits between the operands.
641641
if (match(V, m_DisjointOr(m_Value(Op0), m_ConstantInt(CI)))) {
642642
if (auto Decomp = MergeResults(Op0, CI, IsSigned))

llvm/test/Transforms/ConstraintElimination/add-nsw.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,8 +757,7 @@ define i1 @add_neg_1_known_sge_ult_1(i32 %a) {
757757
; CHECK-NEXT: [[A_SGE:%.*]] = icmp sge i32 [[A:%.*]], 1
758758
; CHECK-NEXT: call void @llvm.assume(i1 [[A_SGE]])
759759
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[A]], -1
760-
; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[SUB]], [[A]]
761-
; CHECK-NEXT: ret i1 [[C]]
760+
; CHECK-NEXT: ret i1 true
762761
;
763762
entry:
764763
%a.sge = icmp sge i32 %a, 1
@@ -823,8 +822,7 @@ define i1 @add_neg_3_known_sge_ult_1(i32 %a) {
823822
; CHECK-NEXT: [[A_SGE:%.*]] = icmp sge i32 [[A:%.*]], 3
824823
; CHECK-NEXT: call void @llvm.assume(i1 [[A_SGE]])
825824
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[A]], -3
826-
; CHECK-NEXT: [[C:%.*]] = icmp ult i32 [[SUB]], [[A]]
827-
; CHECK-NEXT: ret i1 [[C]]
825+
; CHECK-NEXT: ret i1 true
828826
;
829827
entry:
830828
%a.sge = icmp sge i32 %a, 3

llvm/test/Transforms/ConstraintElimination/gep-arithmetic-add.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,7 @@ define i1 @gep_count_add_1_sge_known_ult_1(i32 %count, ptr %p) {
389389
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[COUNT]], -1
390390
; CHECK-NEXT: [[SUB_EXT:%.*]] = zext i32 [[SUB]] to i64
391391
; CHECK-NEXT: [[GEP_SUB:%.*]] = getelementptr inbounds i32, ptr [[P]], i64 [[SUB_EXT]]
392-
; CHECK-NEXT: [[C:%.*]] = icmp ult ptr [[GEP_SUB]], [[GEP_COUNT]]
393-
; CHECK-NEXT: ret i1 [[C]]
392+
; CHECK-NEXT: ret i1 true
394393
;
395394
entry:
396395
%sge = icmp sge i32 %count, 1
@@ -415,8 +414,7 @@ define i1 @gep_count_add_1_sge_known_uge_1(i32 %count, ptr %p) {
415414
; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[COUNT]], -1
416415
; CHECK-NEXT: [[SUB_EXT:%.*]] = zext i32 [[SUB]] to i64
417416
; CHECK-NEXT: [[GEP_SUB:%.*]] = getelementptr inbounds i32, ptr [[P]], i64 [[SUB_EXT]]
418-
; CHECK-NEXT: [[C:%.*]] = icmp uge ptr [[GEP_SUB]], [[P]]
419-
; CHECK-NEXT: ret i1 [[C]]
417+
; CHECK-NEXT: ret i1 true
420418
;
421419
entry:
422420
%sge = icmp sge i32 %count, 1

0 commit comments

Comments
 (0)