1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
4 define i32 @foo(i32 %x, i32 %y) {
6 ; CHECK-NEXT: [[A:%.*]] = and i32 [[X:%.*]], 7
7 ; CHECK-NEXT: [[B:%.*]] = and i32 [[Y:%.*]], 7
8 ; CHECK-NEXT: [[C:%.*]] = mul nuw nsw i32 [[A]], [[B]]
9 ; CHECK-NEXT: [[D:%.*]] = shl nuw i32 [[C]], 26
10 ; CHECK-NEXT: [[E:%.*]] = ashr exact i32 [[D]], 26
11 ; CHECK-NEXT: ret i32 [[E]]
21 ; PR48683 'Quadratic Reciprocity' - and(mul(x,x),2) -> 0
23 define i1 @PR48683(i32 %x) {
24 ; CHECK-LABEL: @PR48683(
25 ; CHECK-NEXT: ret i1 false
29 %c = icmp ne i32 %b, 0
33 define <4 x i1> @PR48683_vec(<4 x i32> %x) {
34 ; CHECK-LABEL: @PR48683_vec(
35 ; CHECK-NEXT: ret <4 x i1> zeroinitializer
37 %a = mul <4 x i32> %x, %x
38 %b = and <4 x i32> %a, <i32 2, i32 2, i32 2, i32 2>
39 %c = icmp ne <4 x i32> %b, zeroinitializer
43 define <4 x i1> @PR48683_vec_undef(<4 x i32> %x) {
44 ; CHECK-LABEL: @PR48683_vec_undef(
45 ; CHECK-NEXT: [[A:%.*]] = mul <4 x i32> [[X:%.*]], [[X]]
46 ; CHECK-NEXT: [[B:%.*]] = and <4 x i32> [[A]], <i32 2, i32 2, i32 2, i32 undef>
47 ; CHECK-NEXT: [[C:%.*]] = icmp ne <4 x i32> [[B]], zeroinitializer
48 ; CHECK-NEXT: ret <4 x i1> [[C]]
50 %a = mul <4 x i32> %x, %x
51 %b = and <4 x i32> %a, <i32 2, i32 2, i32 2, i32 undef>
52 %c = icmp ne <4 x i32> %b, zeroinitializer
56 ; mul(x,x) - bit[1] is 0, but if demanding the other bits the source must not be undef
58 define i64 @combine_mul_self_demandedbits(i64 %x) {
59 ; CHECK-LABEL: @combine_mul_self_demandedbits(
60 ; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[X:%.*]], [[X]]
61 ; CHECK-NEXT: [[TMP2:%.*]] = and i64 [[TMP1]], -3
62 ; CHECK-NEXT: ret i64 [[TMP2]]
69 define <4 x i32> @combine_mul_self_demandedbits_vector(<4 x i32> %x) {
70 ; CHECK-LABEL: @combine_mul_self_demandedbits_vector(
71 ; CHECK-NEXT: [[TMP1:%.*]] = freeze <4 x i32> [[X:%.*]]
72 ; CHECK-NEXT: [[TMP2:%.*]] = mul <4 x i32> [[TMP1]], [[TMP1]]
73 ; CHECK-NEXT: ret <4 x i32> [[TMP2]]
75 %1 = freeze <4 x i32> %x
76 %2 = mul <4 x i32> %1, %1
77 %3 = and <4 x i32> %2, <i32 -3, i32 -3, i32 -3, i32 -3>
81 define <vscale x 2 x i32> @combine_mul_self_demandedbits_vector2(<vscale x 2 x i32> %x) {
82 ; CHECK-LABEL: @combine_mul_self_demandedbits_vector2(
83 ; CHECK-NEXT: [[TMP1:%.*]] = freeze <vscale x 2 x i32> [[X:%.*]]
84 ; CHECK-NEXT: [[TMP2:%.*]] = mul <vscale x 2 x i32> [[TMP1]], [[TMP1]]
85 ; CHECK-NEXT: ret <vscale x 2 x i32> [[TMP2]]
87 %1 = freeze <vscale x 2 x i32> %x
88 %2 = mul <vscale x 2 x i32> %1, %1
89 %3 = and <vscale x 2 x i32> %2, shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 -3, i32 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
90 ret <vscale x 2 x i32> %3
93 define i8 @one_demanded_bit(i8 %x) {
94 ; CHECK-LABEL: @one_demanded_bit(
95 ; CHECK-NEXT: [[M:%.*]] = shl i8 [[X:%.*]], 6
96 ; CHECK-NEXT: [[R:%.*]] = or i8 [[M]], -65
97 ; CHECK-NEXT: ret i8 [[R]]
99 %m = mul i8 %x, 192 ; 0b1100_0000
100 %r = or i8 %m, 191 ; 0b1011_1111
104 define <2 x i8> @one_demanded_bit_splat(<2 x i8> %x) {
105 ; CHECK-LABEL: @one_demanded_bit_splat(
106 ; CHECK-NEXT: [[M:%.*]] = shl <2 x i8> [[X:%.*]], <i8 5, i8 5>
107 ; CHECK-NEXT: [[R:%.*]] = and <2 x i8> [[M]], <i8 32, i8 32>
108 ; CHECK-NEXT: ret <2 x i8> [[R]]
110 %m = mul <2 x i8> %x, <i8 160, i8 160> ; 0b1010_0000
111 %r = and <2 x i8> %m, <i8 32, i8 32> ; 0b0010_0000
115 define i67 @one_demanded_low_bit(i67 %x) {
116 ; CHECK-LABEL: @one_demanded_low_bit(
117 ; CHECK-NEXT: [[R:%.*]] = and i67 [[X:%.*]], 1
118 ; CHECK-NEXT: ret i67 [[R]]
120 %m = mul i67 %x, -63 ; any odd number will do
125 define i33 @squared_one_demanded_low_bit(i33 %x) {
126 ; CHECK-LABEL: @squared_one_demanded_low_bit(
127 ; CHECK-NEXT: [[TMP1:%.*]] = and i33 [[X:%.*]], 1
128 ; CHECK-NEXT: ret i33 [[TMP1]]
130 %mul = mul i33 %x, %x
131 %and = and i33 %mul, 1
135 define <2 x i8> @squared_one_demanded_low_bit_splat(<2 x i8> %x) {
136 ; CHECK-LABEL: @squared_one_demanded_low_bit_splat(
137 ; CHECK-NEXT: [[AND:%.*]] = or <2 x i8> [[X:%.*]], <i8 -2, i8 -2>
138 ; CHECK-NEXT: ret <2 x i8> [[AND]]
140 %mul = mul <2 x i8> %x, %x
141 %and = or <2 x i8> %mul, <i8 254, i8 254>
145 define i33 @squared_demanded_2_low_bits(i33 %x) {
146 ; CHECK-LABEL: @squared_demanded_2_low_bits(
147 ; CHECK-NEXT: [[TMP1:%.*]] = and i33 [[X:%.*]], 1
148 ; CHECK-NEXT: ret i33 [[TMP1]]
150 %mul = mul i33 %x, %x
151 %and = and i33 %mul, 3
155 define <2 x i8> @squared_demanded_2_low_bits_splat(<2 x i8> %x) {
156 ; CHECK-LABEL: @squared_demanded_2_low_bits_splat(
157 ; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], <i8 1, i8 1>
158 ; CHECK-NEXT: [[AND:%.*]] = or disjoint <2 x i8> [[TMP1]], <i8 -4, i8 -4>
159 ; CHECK-NEXT: ret <2 x i8> [[AND]]
161 %mul = mul <2 x i8> %x, %x
162 %and = or <2 x i8> %mul, <i8 252, i8 252>
168 define i33 @squared_demanded_3_low_bits(i33 %x) {
169 ; CHECK-LABEL: @squared_demanded_3_low_bits(
170 ; CHECK-NEXT: [[MUL:%.*]] = mul i33 [[X:%.*]], [[X]]
171 ; CHECK-NEXT: [[AND:%.*]] = and i33 [[MUL]], 7
172 ; CHECK-NEXT: ret i33 [[AND]]
174 %mul = mul i33 %x, %x
175 %and = and i33 %mul, 7
179 ; Instcombine should be able to simplify mul operator.
182 define i64 @scalar_mul_bit_x0_y0(i64 %x, i64 %y) {
183 ; CHECK-LABEL: @scalar_mul_bit_x0_y0(
184 ; CHECK-NEXT: [[AND2:%.*]] = and i64 [[Y:%.*]], 1
185 ; CHECK-NEXT: [[MUL:%.*]] = and i64 [[AND2]], [[X:%.*]]
186 ; CHECK-NEXT: ret i64 [[MUL]]
188 %and1 = and i64 %x, 1
189 %and2 = and i64 %y, 1
190 %mul = mul i64 %and1, %and2
194 declare void @use(i64)
196 define i64 @scalar_mul_bit_x0_y0_uses(i64 %x, i64 %y) {
197 ; CHECK-LABEL: @scalar_mul_bit_x0_y0_uses(
198 ; CHECK-NEXT: [[AND1:%.*]] = and i64 [[X:%.*]], 1
199 ; CHECK-NEXT: call void @use(i64 [[AND1]])
200 ; CHECK-NEXT: [[AND2:%.*]] = and i64 [[Y:%.*]], 1
201 ; CHECK-NEXT: call void @use(i64 [[AND2]])
202 ; CHECK-NEXT: [[MUL:%.*]] = and i64 [[AND2]], [[X]]
203 ; CHECK-NEXT: ret i64 [[MUL]]
205 %and1 = and i64 %x, 1
206 call void @use(i64 %and1)
207 %and2 = and i64 %y, 1
208 call void @use(i64 %and2)
209 %mul = mul i64 %and1, %and2
214 define i64 @scalar_mul_bit_x0_y1(i64 %x, i64 %y) {
215 ; CHECK-LABEL: @scalar_mul_bit_x0_y1(
216 ; CHECK-NEXT: [[AND2:%.*]] = and i64 [[Y:%.*]], 2
217 ; CHECK-NEXT: [[TMP1:%.*]] = and i64 [[X:%.*]], 1
218 ; CHECK-NEXT: [[DOTNOT:%.*]] = icmp eq i64 [[TMP1]], 0
219 ; CHECK-NEXT: [[MUL:%.*]] = select i1 [[DOTNOT]], i64 0, i64 [[AND2]]
220 ; CHECK-NEXT: ret i64 [[MUL]]
222 %and1 = and i64 %x, 1
223 %and2 = and i64 %y, 2
224 %mul = mul i64 %and1, %and2
228 define i64 @scalar_mul_bit_x0_yC(i64 %x, i64 %y, i64 %c) {
229 ; CHECK-LABEL: @scalar_mul_bit_x0_yC(
230 ; CHECK-NEXT: [[AND2:%.*]] = and i64 [[Y:%.*]], [[C:%.*]]
231 ; CHECK-NEXT: [[TMP1:%.*]] = and i64 [[X:%.*]], 1
232 ; CHECK-NEXT: [[DOTNOT:%.*]] = icmp eq i64 [[TMP1]], 0
233 ; CHECK-NEXT: [[MUL:%.*]] = select i1 [[DOTNOT]], i64 0, i64 [[AND2]]
234 ; CHECK-NEXT: ret i64 [[MUL]]
236 %and1 = and i64 %x, 1
237 %and2 = and i64 %y, %c
238 %mul = mul i64 %and1, %and2
243 define <2 x i64> @vector_mul_bit_x0_y0(<2 x i64> %x, <2 x i64> %y) {
244 ; CHECK-LABEL: @vector_mul_bit_x0_y0(
245 ; CHECK-NEXT: [[AND2:%.*]] = and <2 x i64> [[Y:%.*]], <i64 1, i64 1>
246 ; CHECK-NEXT: [[MUL:%.*]] = and <2 x i64> [[AND2]], [[X:%.*]]
247 ; CHECK-NEXT: ret <2 x i64> [[MUL]]
249 %and1 = and <2 x i64> %x, <i64 1, i64 1>
250 %and2 = and <2 x i64> %y, <i64 1, i64 1>
251 %mul = mul <2 x i64> %and1, %and2