Skip to content

[InstCombine] Prefer to keep power-of-2 constants when combining ashr exact and slt/ult of a constant #86111

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

Merged
merged 1 commit into from
May 10, 2024
Merged
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
10 changes: 10 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,16 @@ Instruction *InstCombinerImpl::foldICmpShrConstant(ICmpInst &Cmp,
// those conditions rather than checking them. This is difficult because of
// undef/poison (PR34838).
if (IsAShr && Shr->hasOneUse()) {
if (IsExact && (Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) &&
(C - 1).isPowerOf2() && C.countLeadingZeros() > ShAmtVal) {
// When C - 1 is a power of two and the transform can be legally
// performed, prefer this form so the produced constant is close to a
// power of two.
// icmp slt/ult (ashr exact X, ShAmtC), C
// --> icmp slt/ult X, (C - 1) << ShAmtC) + 1
APInt ShiftedC = (C - 1).shl(ShAmtVal) + 1;
return new ICmpInst(Pred, X, ConstantInt::get(ShrTy, ShiftedC));
}
if (IsExact || Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_ULT) {
// When ShAmtC can be shifted losslessly:
// icmp PRED (ashr exact X, ShAmtC), C --> icmp PRED X, (C << ShAmtC)
Expand Down
10 changes: 4 additions & 6 deletions llvm/test/Transforms/InstCombine/icmp-shr-lt-gt.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3379,7 +3379,7 @@ define i1 @ashrslt_01_01_exact(i4 %x) {

define i1 @ashrslt_01_02_exact(i4 %x) {
; CHECK-LABEL: @ashrslt_01_02_exact(
; CHECK-NEXT: [[C:%.*]] = icmp slt i4 [[X:%.*]], 4
; CHECK-NEXT: [[C:%.*]] = icmp slt i4 [[X:%.*]], 3
; CHECK-NEXT: ret i1 [[C]]
;
%s = ashr exact i4 %x, 1
Expand All @@ -3389,7 +3389,7 @@ define i1 @ashrslt_01_02_exact(i4 %x) {

define i1 @ashrslt_01_03_exact(i4 %x) {
; CHECK-LABEL: @ashrslt_01_03_exact(
; CHECK-NEXT: [[C:%.*]] = icmp slt i4 [[X:%.*]], 6
; CHECK-NEXT: [[C:%.*]] = icmp slt i4 [[X:%.*]], 5
; CHECK-NEXT: ret i1 [[C]]
;
%s = ashr exact i4 %x, 1
Expand Down Expand Up @@ -3800,11 +3800,9 @@ define i1 @ashrslt_03_15_exact(i4 %x) {
ret i1 %c
}

; TODO: The resulting compared constant can be safely replaced with one that
; is closer to a power of two.
define i1 @ashr_slt_exact_near_pow2_cmpval(i8 %x) {
; CHECK-LABEL: @ashr_slt_exact_near_pow2_cmpval(
; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[X:%.*]], 10
; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[X:%.*]], 9
; CHECK-NEXT: ret i1 [[C]]
;
%s = ashr exact i8 %x, 1
Expand All @@ -3814,7 +3812,7 @@ define i1 @ashr_slt_exact_near_pow2_cmpval(i8 %x) {

define i1 @ashr_ult_exact_near_pow2_cmpval(i8 %x) {
; CHECK-LABEL: @ashr_ult_exact_near_pow2_cmpval(
; CHECK-NEXT: [[C:%.*]] = icmp ult i8 [[X:%.*]], 10
; CHECK-NEXT: [[C:%.*]] = icmp ult i8 [[X:%.*]], 9
; CHECK-NEXT: ret i1 [[C]]
;
%s = ashr exact i8 %x, 1
Expand Down
Loading