1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -instcombine -S < %s | FileCheck %s
6 define i32 @test1(i32 %a, i32 %b) nounwind readnone {
8 ; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[B:%.*]], [[A:%.*]]
9 ; CHECK-NEXT: [[TMP2:%.*]] = xor i32 [[TMP1]], -1
10 ; CHECK-NEXT: [[DOTLOBIT_NOT:%.*]] = lshr i32 [[TMP2]], 31
11 ; CHECK-NEXT: ret i32 [[DOTLOBIT_NOT]]
13 %t0 = icmp sgt i32 %a, -1
14 %t1 = icmp slt i32 %b, 0
16 %t3 = zext i1 %t2 to i32
20 ; TODO: This optimizes partially but not all the way.
21 define i32 @test2(i32 %a, i32 %b) nounwind readnone {
22 ; CHECK-LABEL: @test2(
23 ; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
24 ; CHECK-NEXT: [[TMP2:%.*]] = lshr i32 [[TMP1]], 3
25 ; CHECK-NEXT: [[DOTLOBIT:%.*]] = and i32 [[TMP2]], 1
26 ; CHECK-NEXT: [[TMP3:%.*]] = xor i32 [[DOTLOBIT]], 1
27 ; CHECK-NEXT: ret i32 [[TMP3]]
31 %t2 = icmp eq i32 %t0, %t1
32 %t3 = zext i1 %t2 to i32
36 define i32 @test3(i32 %a, i32 %b) nounwind readnone {
37 ; CHECK-LABEL: @test3(
38 ; CHECK-NEXT: [[T2_UNSHIFTED:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
39 ; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[T2_UNSHIFTED]], -1
40 ; CHECK-NEXT: [[T2_UNSHIFTED_LOBIT_NOT:%.*]] = lshr i32 [[TMP1]], 31
41 ; CHECK-NEXT: ret i32 [[T2_UNSHIFTED_LOBIT_NOT]]
45 %t2 = icmp eq i32 %t0, %t1
46 %t3 = zext i1 %t2 to i32
50 ; TODO this should optimize but doesn't due to missing vector support in InstCombiner::foldICmpEquality.
51 define <2 x i32> @test3vec(<2 x i32> %a, <2 x i32> %b) nounwind readnone {
52 ; CHECK-LABEL: @test3vec(
53 ; CHECK-NEXT: [[T0:%.*]] = lshr <2 x i32> [[A:%.*]], <i32 31, i32 31>
54 ; CHECK-NEXT: [[T1:%.*]] = lshr <2 x i32> [[B:%.*]], <i32 31, i32 31>
55 ; CHECK-NEXT: [[T2:%.*]] = icmp eq <2 x i32> [[T0]], [[T1]]
56 ; CHECK-NEXT: [[T3:%.*]] = zext <2 x i1> [[T2]] to <2 x i32>
57 ; CHECK-NEXT: ret <2 x i32> [[T3]]
59 %t0 = lshr <2 x i32> %a, <i32 31, i32 31>
60 %t1 = lshr <2 x i32> %b, <i32 31, i32 31>
61 %t2 = icmp eq <2 x i32> %t0, %t1
62 %t3 = zext <2 x i1> %t2 to <2 x i32>
66 ; Variation on @test3: checking the 2nd bit in a situation where the 5th bit
68 define i32 @test3i(i32 %a, i32 %b) nounwind readnone {
69 ; CHECK-LABEL: @test3i(
70 ; CHECK-NEXT: [[T01:%.*]] = xor i32 [[A:%.*]], [[B:%.*]]
71 ; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[T01]], -1
72 ; CHECK-NEXT: [[T4:%.*]] = lshr i32 [[TMP1]], 31
73 ; CHECK-NEXT: ret i32 [[T4]]
79 %t4 = icmp eq i32 %t2, %t3
80 %t5 = zext i1 %t4 to i32
84 define i1 @test4a(i32 %a) {
85 ; CHECK-LABEL: @test4a(
86 ; CHECK-NEXT: [[C:%.*]] = icmp slt i32 [[A:%.*]], 1
87 ; CHECK-NEXT: ret i1 [[C]]
92 %signum = or i32 %l, %r
93 %c = icmp slt i32 %signum, 1
97 define <2 x i1> @test4a_vec(<2 x i32> %a) {
98 ; CHECK-LABEL: @test4a_vec(
99 ; CHECK-NEXT: [[C:%.*]] = icmp slt <2 x i32> [[A:%.*]], <i32 1, i32 1>
100 ; CHECK-NEXT: ret <2 x i1> [[C]]
102 %l = ashr <2 x i32> %a, <i32 31, i32 31>
103 %na = sub <2 x i32> zeroinitializer, %a
104 %r = lshr <2 x i32> %na, <i32 31, i32 31>
105 %signum = or <2 x i32> %l, %r
106 %c = icmp slt <2 x i32> %signum, <i32 1, i32 1>
110 define i1 @test4b(i64 %a) {
111 ; CHECK-LABEL: @test4b(
112 ; CHECK-NEXT: [[C:%.*]] = icmp slt i64 [[A:%.*]], 1
113 ; CHECK-NEXT: ret i1 [[C]]
117 %r = lshr i64 %na, 63
118 %signum = or i64 %l, %r
119 %c = icmp slt i64 %signum, 1
123 define i1 @test4c(i64 %a) {
124 ; CHECK-LABEL: @test4c(
125 ; CHECK-NEXT: [[C:%.*]] = icmp slt i64 [[A:%.*]], 1
126 ; CHECK-NEXT: ret i1 [[C]]
130 %r = lshr i64 %na, 63
131 %signum = or i64 %l, %r
132 %signum.trunc = trunc i64 %signum to i32
133 %c = icmp slt i32 %signum.trunc, 1
137 define <2 x i1> @test4c_vec(<2 x i64> %a) {
138 ; CHECK-LABEL: @test4c_vec(
139 ; CHECK-NEXT: [[C:%.*]] = icmp slt <2 x i64> [[A:%.*]], <i64 1, i64 1>
140 ; CHECK-NEXT: ret <2 x i1> [[C]]
142 %l = ashr <2 x i64> %a, <i64 63, i64 63>
143 %na = sub <2 x i64> zeroinitializer, %a
144 %r = lshr <2 x i64> %na, <i64 63, i64 63>
145 %signum = or <2 x i64> %l, %r
146 %signum.trunc = trunc <2 x i64> %signum to <2 x i32>
147 %c = icmp slt <2 x i32> %signum.trunc, <i32 1, i32 1>
153 define i1 @shift_trunc_signbit_test(i32 %x) {
154 ; CHECK-LABEL: @shift_trunc_signbit_test(
155 ; CHECK-NEXT: [[R:%.*]] = icmp slt i32 [[X:%.*]], 0
156 ; CHECK-NEXT: ret i1 [[R]]
158 %sh = lshr i32 %x, 24
159 %tr = trunc i32 %sh to i8
160 %r = icmp slt i8 %tr, 0
164 define <2 x i1> @shift_trunc_signbit_test_vec_uses(<2 x i17> %x, <2 x i17>* %p1, <2 x i13>* %p2) {
165 ; CHECK-LABEL: @shift_trunc_signbit_test_vec_uses(
166 ; CHECK-NEXT: [[SH:%.*]] = lshr <2 x i17> [[X:%.*]], <i17 4, i17 4>
167 ; CHECK-NEXT: store <2 x i17> [[SH]], <2 x i17>* [[P1:%.*]], align 8
168 ; CHECK-NEXT: [[TR:%.*]] = trunc <2 x i17> [[SH]] to <2 x i13>
169 ; CHECK-NEXT: store <2 x i13> [[TR]], <2 x i13>* [[P2:%.*]], align 4
170 ; CHECK-NEXT: [[R:%.*]] = icmp sgt <2 x i17> [[X]], <i17 -1, i17 -1>
171 ; CHECK-NEXT: ret <2 x i1> [[R]]
173 %sh = lshr <2 x i17> %x, <i17 4, i17 4>
174 store <2 x i17> %sh, <2 x i17>* %p1
175 %tr = trunc <2 x i17> %sh to <2 x i13>
176 store <2 x i13> %tr, <2 x i13>* %p2
177 %r = icmp sgt <2 x i13> %tr, <i13 -1, i13 -1>
183 define i1 @shift_trunc_wrong_shift(i32 %x) {
184 ; CHECK-LABEL: @shift_trunc_wrong_shift(
185 ; CHECK-NEXT: [[SH:%.*]] = lshr i32 [[X:%.*]], 23
186 ; CHECK-NEXT: [[TR:%.*]] = trunc i32 [[SH]] to i8
187 ; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[TR]], 0
188 ; CHECK-NEXT: ret i1 [[R]]
190 %sh = lshr i32 %x, 23
191 %tr = trunc i32 %sh to i8
192 %r = icmp slt i8 %tr, 0
198 define i1 @shift_trunc_wrong_cmp(i32 %x) {
199 ; CHECK-LABEL: @shift_trunc_wrong_cmp(
200 ; CHECK-NEXT: [[SH:%.*]] = lshr i32 [[X:%.*]], 24
201 ; CHECK-NEXT: [[TR:%.*]] = trunc i32 [[SH]] to i8
202 ; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[TR]], 1
203 ; CHECK-NEXT: ret i1 [[R]]
205 %sh = lshr i32 %x, 24
206 %tr = trunc i32 %sh to i8
207 %r = icmp slt i8 %tr, 1