Skip to content

Commit a4b44c0

Browse files
[InstCombine] Canonicalize icmp ult (add X, C2), C expressions
`icmp ult (add X, C2), C` can be folded to `icmp ne (and X, C), 2C`, subject to `C == -C2` and C2 being a power of 2. Proofs: https://alive2.llvm.org/ce/z/P-VVmQ. Fixes: #75613.
1 parent 3ad31e1 commit a4b44c0

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -3130,6 +3130,13 @@ Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,
31303130
return new ICmpInst(ICmpInst::ICMP_EQ, Builder.CreateAnd(X, -C),
31313131
ConstantExpr::getNeg(cast<Constant>(Y)));
31323132

3133+
// X+C2 <u C -> (X & C) == 2C
3134+
// iff C == -(C2)
3135+
// C2 is a power of 2
3136+
if (Pred == ICmpInst::ICMP_ULT && C2->isPowerOf2() && C == -*C2)
3137+
return new ICmpInst(ICmpInst::ICMP_NE, Builder.CreateAnd(X, C),
3138+
ConstantInt::get(Ty, C * 2));
3139+
31333140
// X+C >u C2 -> (X & ~C2) != C
31343141
// iff C & C2 == 0
31353142
// C2+1 is a power of 2

llvm/test/Transforms/InstCombine/icmp-add.ll

+78
Original file line numberDiff line numberDiff line change
@@ -3023,4 +3023,82 @@ define i1 @icmp_addnuw_nonzero_fail_multiuse(i32 %x, i32 %y) {
30233023
ret i1 %c
30243024
}
30253025

3026+
define i1 @ult_add_C2_pow2_C_neg(i8 %x) {
3027+
; CHECK-LABEL: @ult_add_C2_pow2_C_neg(
3028+
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], -32
3029+
; CHECK-NEXT: [[C:%.*]] = icmp ne i8 [[TMP1]], -64
3030+
; CHECK-NEXT: ret i1 [[C]]
3031+
;
3032+
%i = add i8 %x, 32
3033+
%c = icmp ult i8 %i, -32
3034+
ret i1 %c
3035+
}
3036+
3037+
define i1 @ult_add_nsw_C2_pow2_C_neg(i8 %x) {
3038+
; CHECK-LABEL: @ult_add_nsw_C2_pow2_C_neg(
3039+
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], -32
3040+
; CHECK-NEXT: [[C:%.*]] = icmp ne i8 [[TMP1]], -64
3041+
; CHECK-NEXT: ret i1 [[C]]
3042+
;
3043+
%i = add nsw i8 %x, 32
3044+
%c = icmp ult i8 %i, -32
3045+
ret i1 %c
3046+
}
3047+
3048+
define i1 @ult_add_nuw_nsw_C2_pow2_C_neg(i8 %x) {
3049+
; CHECK-LABEL: @ult_add_nuw_nsw_C2_pow2_C_neg(
3050+
; CHECK-NEXT: [[C:%.*]] = icmp ult i8 [[X:%.*]], -64
3051+
; CHECK-NEXT: ret i1 [[C]]
3052+
;
3053+
%i = add nuw nsw i8 %x, 32
3054+
%c = icmp ult i8 %i, -32
3055+
ret i1 %c
3056+
}
3057+
3058+
define i1 @ult_add_C2_neg_C_pow2(i8 %x) {
3059+
; CHECK-LABEL: @ult_add_C2_neg_C_pow2(
3060+
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], -32
3061+
; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[TMP1]], 32
3062+
; CHECK-NEXT: ret i1 [[C]]
3063+
;
3064+
%i = add i8 %x, -32
3065+
%c = icmp ult i8 %i, 32
3066+
ret i1 %c
3067+
}
3068+
3069+
define <2 x i1> @ult_add_C2_pow2_C_neg_vec(<2 x i8> %x) {
3070+
; CHECK-LABEL: @ult_add_C2_pow2_C_neg_vec(
3071+
; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], <i8 -32, i8 -32>
3072+
; CHECK-NEXT: [[C:%.*]] = icmp ne <2 x i8> [[TMP1]], <i8 -64, i8 -64>
3073+
; CHECK-NEXT: ret <2 x i1> [[C]]
3074+
;
3075+
%i = add <2 x i8> %x, <i8 32, i8 32>
3076+
%c = icmp ult <2 x i8> %i, <i8 -32, i8 -32>
3077+
ret <2 x i1> %c
3078+
}
3079+
3080+
define i1 @ult_add_C2_pow2_C_neg_multiuse(i8 %x) {
3081+
; CHECK-LABEL: @ult_add_C2_pow2_C_neg_multiuse(
3082+
; CHECK-NEXT: [[I:%.*]] = add i8 [[X:%.*]], 32
3083+
; CHECK-NEXT: [[C:%.*]] = icmp ult i8 [[I]], -32
3084+
; CHECK-NEXT: call void @use(i8 [[I]])
3085+
; CHECK-NEXT: ret i1 [[C]]
3086+
;
3087+
%i = add i8 %x, 32
3088+
%c = icmp ult i8 %i, -32
3089+
call void @use(i8 %i)
3090+
ret i1 %c
3091+
}
3092+
3093+
define i1 @uge_add_C2_pow2_C_neg(i8 %x) {
3094+
; CHECK-LABEL: @uge_add_C2_pow2_C_neg(
3095+
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X:%.*]], -32
3096+
; CHECK-NEXT: [[C:%.*]] = icmp eq i8 [[TMP1]], -64
3097+
; CHECK-NEXT: ret i1 [[C]]
3098+
;
3099+
%i = add i8 %x, 32
3100+
%c = icmp uge i8 %i, -32
3101+
ret i1 %c
3102+
}
3103+
30263104
declare void @llvm.assume(i1)

0 commit comments

Comments
 (0)