[InstCombine] fold signbit check of X | (X -1)

There may be some other patterns like this or a generalization,
but this is an example that I noticed would definitely regress
with a planned follow-up to D111410.

https://alive2.llvm.org/ce/z/GVpQDb
This commit is contained in:
Sanjay Patel 2021-10-11 15:21:51 -04:00
parent 518ec39de7
commit 59441c7329
3 changed files with 27 additions and 16 deletions

View File

@ -1950,6 +1950,17 @@ Instruction *InstCombinerImpl::foldICmpOrConstant(ICmpInst &Cmp,
}
}
// (X | (X-1)) s< 0 --> X < 1
// (X | (X-1)) s> -1 --> X > 0
Value *X;
bool TrueIfSigned;
if (isSignBitCheck(Pred, C, TrueIfSigned) &&
match(Or, m_c_Or(m_Add(m_Value(X), m_AllOnes()), m_Deferred(X)))) {
auto NewPred = TrueIfSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_SGT;
Constant *NewC = ConstantInt::get(X->getType(), TrueIfSigned ? 1 : 0);
return new ICmpInst(NewPred, X, NewC);
}
if (!Cmp.isEquality() || !C.isZero() || !Or->hasOneUse())
return nullptr;

View File

@ -220,9 +220,7 @@ define i1 @eq_const_mask_use2(i8 %x, i8 %y) {
define <2 x i1> @decrement_slt_0(<2 x i8> %x) {
; CHECK-LABEL: @decrement_slt_0(
; CHECK-NEXT: [[DEC:%.*]] = add <2 x i8> [[X:%.*]], <i8 -1, i8 -1>
; CHECK-NEXT: [[OR:%.*]] = or <2 x i8> [[DEC]], [[X]]
; CHECK-NEXT: [[R:%.*]] = icmp slt <2 x i8> [[OR]], zeroinitializer
; CHECK-NEXT: [[R:%.*]] = icmp slt <2 x i8> [[X:%.*]], <i8 1, i8 1>
; CHECK-NEXT: ret <2 x i1> [[R]]
;
%dec = add <2 x i8> %x, <i8 -1, i8 -1>
@ -236,8 +234,7 @@ define i1 @decrement_slt_0_commute_use1(i8 %px) {
; CHECK-NEXT: [[X:%.*]] = mul i8 [[PX:%.*]], 42
; CHECK-NEXT: [[DEC:%.*]] = add i8 [[X]], -1
; CHECK-NEXT: call void @use(i8 [[DEC]])
; CHECK-NEXT: [[OR:%.*]] = or i8 [[X]], [[DEC]]
; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[OR]], 0
; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[X]], 1
; CHECK-NEXT: ret i1 [[R]]
;
%x = mul i8 %px, 42 ; thwart complexity-based canonicalization
@ -253,7 +250,7 @@ define i1 @decrement_slt_0_use2(i8 %x) {
; CHECK-NEXT: [[DEC:%.*]] = add i8 [[X:%.*]], -1
; CHECK-NEXT: [[OR:%.*]] = or i8 [[DEC]], [[X]]
; CHECK-NEXT: call void @use(i8 [[OR]])
; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[OR]], 0
; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[X]], 1
; CHECK-NEXT: ret i1 [[R]]
;
%dec = add i8 %x, -1
@ -263,6 +260,8 @@ define i1 @decrement_slt_0_use2(i8 %x) {
ret i1 %r
}
; negative test - wrong cmp constant
define i1 @decrement_slt_n1(i8 %x) {
; CHECK-LABEL: @decrement_slt_n1(
; CHECK-NEXT: [[DEC:%.*]] = add i8 [[X:%.*]], -1
@ -276,6 +275,8 @@ define i1 @decrement_slt_n1(i8 %x) {
ret i1 %r
}
; negative test - wrong add constant
define i1 @not_decrement_slt_0(i8 %x) {
; CHECK-LABEL: @not_decrement_slt_0(
; CHECK-NEXT: [[DEC:%.*]] = add i8 [[X:%.*]], -2
@ -293,9 +294,7 @@ define i1 @not_decrement_slt_0(i8 %x) {
define <2 x i1> @decrement_sgt_n1(<2 x i8> %x) {
; CHECK-LABEL: @decrement_sgt_n1(
; CHECK-NEXT: [[DEC:%.*]] = add <2 x i8> [[X:%.*]], <i8 -1, i8 -1>
; CHECK-NEXT: [[OR:%.*]] = or <2 x i8> [[DEC]], [[X]]
; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i8> [[OR]], <i8 -1, i8 -1>
; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i8> [[X:%.*]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[R]]
;
%dec = add <2 x i8> %x, <i8 -1, i8 -1>
@ -309,8 +308,7 @@ define i1 @decrement_sgt_n1_commute_use1(i8 %px) {
; CHECK-NEXT: [[X:%.*]] = mul i8 [[PX:%.*]], 42
; CHECK-NEXT: [[DEC:%.*]] = add i8 [[X]], -1
; CHECK-NEXT: call void @use(i8 [[DEC]])
; CHECK-NEXT: [[OR:%.*]] = or i8 [[X]], [[DEC]]
; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[OR]], -1
; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[X]], 0
; CHECK-NEXT: ret i1 [[R]]
;
%x = mul i8 %px, 42 ; thwart complexity-based canonicalization
@ -326,7 +324,7 @@ define i1 @decrement_sgt_n1_use2(i8 %x) {
; CHECK-NEXT: [[DEC:%.*]] = add i8 [[X:%.*]], -1
; CHECK-NEXT: [[OR:%.*]] = or i8 [[DEC]], [[X]]
; CHECK-NEXT: call void @use(i8 [[OR]])
; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[OR]], -1
; CHECK-NEXT: [[R:%.*]] = icmp sgt i8 [[X]], 0
; CHECK-NEXT: ret i1 [[R]]
;
%dec = add i8 %x, -1
@ -336,6 +334,8 @@ define i1 @decrement_sgt_n1_use2(i8 %x) {
ret i1 %r
}
; negative test - wrong cmp constant
define i1 @decrement_sgt_0(i8 %x) {
; CHECK-LABEL: @decrement_sgt_0(
; CHECK-NEXT: [[DEC:%.*]] = add i8 [[X:%.*]], -1
@ -349,6 +349,8 @@ define i1 @decrement_sgt_0(i8 %x) {
ret i1 %r
}
; negative test - wrong add constant
define i1 @not_decrement_sgt_n1(i8 %x) {
; CHECK-LABEL: @not_decrement_sgt_n1(
; CHECK-NEXT: [[DEC:%.*]] = add i8 [[X:%.*]], -2

View File

@ -1447,10 +1447,8 @@ define i8 @lshr_bitwidth_mask(i8 %x, i8 %y) {
define i1 @cmp_overlap(i32 %x) {
; CHECK-LABEL: @cmp_overlap(
; CHECK-NEXT: [[NOTSUB:%.*]] = add i32 [[X:%.*]], -1
; CHECK-NEXT: [[TMP1:%.*]] = or i32 [[NOTSUB]], [[X]]
; CHECK-NEXT: [[TMP2:%.*]] = icmp slt i32 [[TMP1]], 0
; CHECK-NEXT: ret i1 [[TMP2]]
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 1
; CHECK-NEXT: ret i1 [[TMP1]]
;
%isneg = icmp slt i32 %x, 0
%negx = sub i32 0, %x