Skip to content

Commit 0936195

Browse files
authored
[InstCombine] Drop samesign in InstCombine (#112480)
Closes #112476.
1 parent 3ef630a commit 0936195

File tree

5 files changed

+47
-2
lines changed

5 files changed

+47
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,7 @@ Instruction *InstCombinerImpl::foldICmpAndShift(ICmpInst &Cmp,
17381738

17391739
// Compute X & (C2 << Y).
17401740
Value *NewAnd = Builder.CreateAnd(Shift->getOperand(0), NewShift);
1741-
return replaceOperand(Cmp, 0, NewAnd);
1741+
return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1));
17421742
}
17431743

17441744
return nullptr;
@@ -1844,7 +1844,7 @@ Instruction *InstCombinerImpl::foldICmpAndConstConst(ICmpInst &Cmp,
18441844
/*HasNUW=*/true),
18451845
One, Or->getName());
18461846
Value *NewAnd = Builder.CreateAnd(A, NewOr, And->getName());
1847-
return replaceOperand(Cmp, 0, NewAnd);
1847+
return new ICmpInst(Cmp.getPredicate(), NewAnd, Cmp.getOperand(1));
18481848
}
18491849
}
18501850
}

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,7 @@ Instruction *InstCombinerImpl::foldSelectEqualityTest(SelectInst &Sel) {
14481448
m_c_SpecificICmp(ICmpInst::ICMP_EQ, m_Specific(X), m_Specific(Y))))
14491449
return nullptr;
14501450

1451+
cast<ICmpInst>(XeqY)->setSameSign(false);
14511452
return replaceInstUsesWith(Sel, XeqY);
14521453
}
14531454

llvm/test/Transforms/InstCombine/icmp-and-shift.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,19 @@ define i1 @test_shr_and_1_ne_0(i32 %a, i32 %b) {
619619
ret i1 %cmp
620620
}
621621

622+
define i1 @test_shr_and_1_ne_0_samesign(i32 %a, i32 %b) {
623+
; CHECK-LABEL: @test_shr_and_1_ne_0_samesign(
624+
; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i32 1, [[B:%.*]]
625+
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[A:%.*]], [[TMP1]]
626+
; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[TMP2]], 0
627+
; CHECK-NEXT: ret i1 [[CMP]]
628+
;
629+
%shr = lshr i32 %a, %b
630+
%and = and i32 %shr, 1
631+
%cmp = icmp samesign ne i32 %and, 0
632+
ret i1 %cmp
633+
}
634+
622635
define i1 @test_const_shr_and_1_ne_0(i32 %b) {
623636
; CHECK-LABEL: @test_const_shr_and_1_ne_0(
624637
; CHECK-NEXT: [[TMP1:%.*]] = shl nuw i32 1, [[B:%.*]]

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ entry:
3333
ret i1 %equal
3434
}
3535

36+
define i1 @icmp_equality_test_constant_samesign(i42 %X, i42 %Y) {
37+
; CHECK-LABEL: @icmp_equality_test_constant_samesign(
38+
; CHECK-NEXT: entry:
39+
; CHECK-NEXT: [[XEQY:%.*]] = icmp eq i42 [[X:%.*]], [[Y:%.*]]
40+
; CHECK-NEXT: ret i1 [[XEQY]]
41+
;
42+
entry:
43+
%XeqC = icmp eq i42 %X, -42
44+
%YeqC = icmp eq i42 %Y, -42
45+
%XeqY = icmp samesign eq i42 %X, %Y
46+
%not.YeqC = xor i1 %YeqC, true
47+
%and = select i1 %not.YeqC, i1 %XeqY, i1 false
48+
%equal = select i1 %XeqC, i1 %YeqC, i1 %and
49+
ret i1 %equal
50+
}
51+
3652
define i1 @icmp_equality_test_swift_optional_pointers(i64 %X, i64 %Y) {
3753
; CHECK-LABEL: @icmp_equality_test_swift_optional_pointers(
3854
; CHECK-NEXT: entry:

llvm/test/Transforms/InstCombine/icmp.ll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3203,6 +3203,21 @@ define i1 @icmp_and_or_lshr(i32 %x, i32 %y) {
32033203
ret i1 %ret
32043204
}
32053205

3206+
define i1 @icmp_and_or_lshr_samesign(i32 %x, i32 %y) {
3207+
; CHECK-LABEL: @icmp_and_or_lshr_samesign(
3208+
; CHECK-NEXT: [[SHF1:%.*]] = shl nuw i32 1, [[Y:%.*]]
3209+
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[SHF1]], 1
3210+
; CHECK-NEXT: [[AND3:%.*]] = and i32 [[X:%.*]], [[OR2]]
3211+
; CHECK-NEXT: [[RET:%.*]] = icmp ne i32 [[AND3]], 0
3212+
; CHECK-NEXT: ret i1 [[RET]]
3213+
;
3214+
%shf = lshr i32 %x, %y
3215+
%or = or i32 %shf, %x
3216+
%and = and i32 %or, 1
3217+
%ret = icmp samesign ne i32 %and, 0
3218+
ret i1 %ret
3219+
}
3220+
32063221
define <2 x i1> @icmp_and_or_lshr_vec(<2 x i32> %x, <2 x i32> %y) {
32073222
; CHECK-LABEL: @icmp_and_or_lshr_vec(
32083223
; CHECK-NEXT: [[SHF:%.*]] = lshr <2 x i32> [[X:%.*]], [[Y:%.*]]

0 commit comments

Comments
 (0)