blob: b5994e4531aea4057a8a7e0fd31fe6380e71e67b [file] [log] [blame]
Sanjay Patela19b7482018-01-18 23:11:24 +00001; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
Duncan Sands6c7a52c2010-12-21 08:49:00 +00002; RUN: opt < %s -instsimplify -S | FileCheck %s
3
4define i32 @add1(i32 %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +00005; CHECK-LABEL: @add1(
Sanjay Patela19b7482018-01-18 23:11:24 +00006; CHECK-NEXT: ret i32 [[X:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +00007;
Duncan Sands6c7a52c2010-12-21 08:49:00 +00008; (X + -1) + 1 -> X
9 %l = add i32 %x, -1
10 %r = add i32 %l, 1
11 ret i32 %r
Duncan Sands6c7a52c2010-12-21 08:49:00 +000012}
13
14define i32 @and1(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +000015; CHECK-LABEL: @and1(
Sanjay Patela19b7482018-01-18 23:11:24 +000016; CHECK-NEXT: [[L:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +000017; CHECK-NEXT: ret i32 [[L]]
18;
Duncan Sands6c7a52c2010-12-21 08:49:00 +000019; (X & Y) & X -> X & Y
20 %l = and i32 %x, %y
21 %r = and i32 %l, %x
22 ret i32 %r
Duncan Sands6c7a52c2010-12-21 08:49:00 +000023}
24
25define i32 @and2(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +000026; CHECK-LABEL: @and2(
Sanjay Patela19b7482018-01-18 23:11:24 +000027; CHECK-NEXT: [[R:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +000028; CHECK-NEXT: ret i32 [[R]]
29;
Duncan Sands6c7a52c2010-12-21 08:49:00 +000030; X & (X & Y) -> X & Y
31 %r = and i32 %x, %y
32 %l = and i32 %x, %r
33 ret i32 %l
Duncan Sands6c7a52c2010-12-21 08:49:00 +000034}
35
36define i32 @or1(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +000037; CHECK-LABEL: @or1(
Sanjay Patela19b7482018-01-18 23:11:24 +000038; CHECK-NEXT: [[L:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +000039; CHECK-NEXT: ret i32 [[L]]
40;
Duncan Sands6c7a52c2010-12-21 08:49:00 +000041; (X | Y) | X -> X | Y
42 %l = or i32 %x, %y
43 %r = or i32 %l, %x
44 ret i32 %r
Duncan Sands6c7a52c2010-12-21 08:49:00 +000045}
46
47define i32 @or2(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +000048; CHECK-LABEL: @or2(
Sanjay Patela19b7482018-01-18 23:11:24 +000049; CHECK-NEXT: [[R:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +000050; CHECK-NEXT: ret i32 [[R]]
51;
Duncan Sands6c7a52c2010-12-21 08:49:00 +000052; X | (X | Y) -> X | Y
53 %r = or i32 %x, %y
54 %l = or i32 %x, %r
55 ret i32 %l
Duncan Sands6c7a52c2010-12-21 08:49:00 +000056}
57
58define i32 @xor1(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +000059; CHECK-LABEL: @xor1(
Sanjay Patela19b7482018-01-18 23:11:24 +000060; CHECK-NEXT: ret i32 [[Y:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +000061;
Duncan Sands6c7a52c2010-12-21 08:49:00 +000062; (X ^ Y) ^ X = Y
63 %l = xor i32 %x, %y
64 %r = xor i32 %l, %x
65 ret i32 %r
Duncan Sands6c7a52c2010-12-21 08:49:00 +000066}
67
68define i32 @xor2(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +000069; CHECK-LABEL: @xor2(
Sanjay Patela19b7482018-01-18 23:11:24 +000070; CHECK-NEXT: ret i32 [[Y:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +000071;
Duncan Sands6c7a52c2010-12-21 08:49:00 +000072; X ^ (X ^ Y) = Y
73 %r = xor i32 %x, %y
74 %l = xor i32 %x, %r
75 ret i32 %l
Duncan Sands6c7a52c2010-12-21 08:49:00 +000076}
Duncan Sandsd6f1a952011-01-14 15:26:10 +000077
78define i32 @sub1(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +000079; CHECK-LABEL: @sub1(
Sanjay Patela19b7482018-01-18 23:11:24 +000080; CHECK-NEXT: ret i32 [[Y:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +000081;
Duncan Sandsd6f1a952011-01-14 15:26:10 +000082 %d = sub i32 %x, %y
83 %r = sub i32 %x, %d
84 ret i32 %r
Duncan Sandsd6f1a952011-01-14 15:26:10 +000085}
Duncan Sands99589d02011-01-18 11:50:19 +000086
87define i32 @sub2(i32 %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +000088; CHECK-LABEL: @sub2(
Sanjay Patela19b7482018-01-18 23:11:24 +000089; CHECK-NEXT: ret i32 -1
Sanjay Patel69632442016-03-25 20:12:25 +000090;
Duncan Sands99589d02011-01-18 11:50:19 +000091; X - (X + 1) -> -1
92 %xp1 = add i32 %x, 1
93 %r = sub i32 %x, %xp1
94 ret i32 %r
Duncan Sands99589d02011-01-18 11:50:19 +000095}
96
97define i32 @sub3(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +000098; CHECK-LABEL: @sub3(
Sanjay Patela19b7482018-01-18 23:11:24 +000099; CHECK-NEXT: ret i32 [[X:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +0000100;
Duncan Sands99589d02011-01-18 11:50:19 +0000101; ((X + 1) + Y) - (Y + 1) -> X
102 %xp1 = add i32 %x, 1
103 %lhs = add i32 %xp1, %y
104 %rhs = add i32 %y, 1
105 %r = sub i32 %lhs, %rhs
106 ret i32 %r
Duncan Sands99589d02011-01-18 11:50:19 +0000107}
Duncan Sands771e82a2011-01-28 16:51:11 +0000108
Duncan Sands771e82a2011-01-28 16:51:11 +0000109; (no overflow X * Y) / Y -> X
Sanjay Patela19b7482018-01-18 23:11:24 +0000110
111define i32 @mulnsw_sdiv(i32 %x, i32 %y) {
112; CHECK-LABEL: @mulnsw_sdiv(
113; CHECK-NEXT: ret i32 [[X:%.*]]
114;
Duncan Sands771e82a2011-01-28 16:51:11 +0000115 %mul = mul nsw i32 %x, %y
116 %r = sdiv i32 %mul, %y
117 ret i32 %r
Duncan Sands771e82a2011-01-28 16:51:11 +0000118}
119
Sanjay Patela19b7482018-01-18 23:11:24 +0000120define <2 x i32> @mulnsw_sdiv_commute(<2 x i32> %x, <2 x i32> %y) {
121; CHECK-LABEL: @mulnsw_sdiv_commute(
122; CHECK-NEXT: ret <2 x i32> [[X:%.*]]
123;
124 %mul = mul nsw <2 x i32> %y, %x
125 %r = sdiv <2 x i32> %mul, %y
126 ret <2 x i32> %r
127}
128
129; (no overflow X * Y) / Y -> X
130
131define <2 x i8> @mulnuw_udiv(<2 x i8> %x, <2 x i8> %y) {
132; CHECK-LABEL: @mulnuw_udiv(
133; CHECK-NEXT: ret <2 x i8> [[X:%.*]]
134;
135 %mul = mul nuw <2 x i8> %x, %y
136 %r = udiv <2 x i8> %mul, %y
137 ret <2 x i8> %r
138}
139
140define i32 @mulnuw_udiv_commute(i32 %x, i32 %y) {
141; CHECK-LABEL: @mulnuw_udiv_commute(
142; CHECK-NEXT: ret i32 [[X:%.*]]
143;
144 %mul = mul nuw i32 %y, %x
145 %r = udiv i32 %mul, %y
146 ret i32 %r
147}
148
149; (((X / Y) * Y) / Y) -> X / Y
150
151define i32 @sdiv_mul_sdiv(i32 %x, i32 %y) {
152; CHECK-LABEL: @sdiv_mul_sdiv(
153; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[X:%.*]], [[Y:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +0000154; CHECK-NEXT: ret i32 [[DIV]]
155;
Duncan Sands771e82a2011-01-28 16:51:11 +0000156 %div = sdiv i32 %x, %y
157 %mul = mul i32 %div, %y
158 %r = sdiv i32 %mul, %y
159 ret i32 %r
Duncan Sands771e82a2011-01-28 16:51:11 +0000160}
161
Sanjay Patela19b7482018-01-18 23:11:24 +0000162define i32 @sdiv_mul_sdiv_commute(i32 %x, i32 %y) {
163; CHECK-LABEL: @sdiv_mul_sdiv_commute(
164; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[X:%.*]], [[Y:%.*]]
165; CHECK-NEXT: ret i32 [[DIV]]
166;
167 %div = sdiv i32 %x, %y
168 %mul = mul i32 %y, %div
169 %r = sdiv i32 %mul, %y
170 ret i32 %r
171}
172
173; (((X / Y) * Y) / Y) -> X / Y
174
175define i32 @udiv_mul_udiv(i32 %x, i32 %y) {
176; CHECK-LABEL: @udiv_mul_udiv(
177; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[X:%.*]], [[Y:%.*]]
178; CHECK-NEXT: ret i32 [[DIV]]
179;
180 %div = udiv i32 %x, %y
181 %mul = mul i32 %div, %y
182 %r = udiv i32 %mul, %y
183 ret i32 %r
184}
185
186define i32 @udiv_mul_udiv_commute(i32 %x, i32 %y) {
187; CHECK-LABEL: @udiv_mul_udiv_commute(
188; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[X:%.*]], [[Y:%.*]]
189; CHECK-NEXT: ret i32 [[DIV]]
190;
191 %div = udiv i32 %x, %y
192 %mul = mul i32 %y, %div
193 %r = udiv i32 %mul, %y
194 ret i32 %r
195}
196
Duncan Sands771e82a2011-01-28 16:51:11 +0000197define i32 @sdiv3(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000198; CHECK-LABEL: @sdiv3(
Sanjay Patela19b7482018-01-18 23:11:24 +0000199; CHECK-NEXT: ret i32 0
Sanjay Patel69632442016-03-25 20:12:25 +0000200;
Duncan Sands771e82a2011-01-28 16:51:11 +0000201; (X rem Y) / Y -> 0
202 %rem = srem i32 %x, %y
203 %div = sdiv i32 %rem, %y
204 ret i32 %div
Duncan Sands771e82a2011-01-28 16:51:11 +0000205}
206
Duncan Sandsb67edc62011-01-30 18:03:50 +0000207define i32 @sdiv4(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000208; CHECK-LABEL: @sdiv4(
Sanjay Patela19b7482018-01-18 23:11:24 +0000209; CHECK-NEXT: ret i32 [[X:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +0000210;
Duncan Sandsb67edc62011-01-30 18:03:50 +0000211; (X / Y) * Y -> X if the division is exact
212 %div = sdiv exact i32 %x, %y
213 %mul = mul i32 %div, %y
214 ret i32 %mul
Duncan Sandsb67edc62011-01-30 18:03:50 +0000215}
216
217define i32 @sdiv5(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000218; CHECK-LABEL: @sdiv5(
Sanjay Patela19b7482018-01-18 23:11:24 +0000219; CHECK-NEXT: ret i32 [[X:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +0000220;
Duncan Sandsb67edc62011-01-30 18:03:50 +0000221; Y * (X / Y) -> X if the division is exact
222 %div = sdiv exact i32 %x, %y
223 %mul = mul i32 %y, %div
224 ret i32 %mul
Duncan Sandsb67edc62011-01-30 18:03:50 +0000225}
226
Duncan Sands771e82a2011-01-28 16:51:11 +0000227define i32 @udiv3(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000228; CHECK-LABEL: @udiv3(
Sanjay Patela19b7482018-01-18 23:11:24 +0000229; CHECK-NEXT: ret i32 0
Sanjay Patel69632442016-03-25 20:12:25 +0000230;
Duncan Sands771e82a2011-01-28 16:51:11 +0000231; (X rem Y) / Y -> 0
232 %rem = urem i32 %x, %y
233 %div = udiv i32 %rem, %y
234 ret i32 %div
Duncan Sands771e82a2011-01-28 16:51:11 +0000235}
Chris Lattner6e57b152011-02-06 22:05:31 +0000236
237define i32 @udiv4(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000238; CHECK-LABEL: @udiv4(
Sanjay Patela19b7482018-01-18 23:11:24 +0000239; CHECK-NEXT: ret i32 [[X:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +0000240;
Chris Lattner6e57b152011-02-06 22:05:31 +0000241; (X / Y) * Y -> X if the division is exact
242 %div = udiv exact i32 %x, %y
243 %mul = mul i32 %div, %y
244 ret i32 %mul
Chris Lattner6e57b152011-02-06 22:05:31 +0000245}
246
247define i32 @udiv5(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000248; CHECK-LABEL: @udiv5(
Sanjay Patela19b7482018-01-18 23:11:24 +0000249; CHECK-NEXT: ret i32 [[X:%.*]]
Sanjay Patel69632442016-03-25 20:12:25 +0000250;
Chris Lattner6e57b152011-02-06 22:05:31 +0000251; Y * (X / Y) -> X if the division is exact
252 %div = udiv exact i32 %x, %y
253 %mul = mul i32 %y, %div
254 ret i32 %mul
Chris Lattner6e57b152011-02-06 22:05:31 +0000255}
256
Duncan Sands395ac42d2012-03-13 14:07:05 +0000257define i16 @trunc1(i32 %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000258; CHECK-LABEL: @trunc1(
Sanjay Patela19b7482018-01-18 23:11:24 +0000259; CHECK-NEXT: ret i16 1
Sanjay Patel69632442016-03-25 20:12:25 +0000260;
Duncan Sands395ac42d2012-03-13 14:07:05 +0000261 %y = add i32 %x, 1
262 %tx = trunc i32 %x to i16
263 %ty = trunc i32 %y to i16
264 %d = sub i16 %ty, %tx
265 ret i16 %d
Duncan Sands395ac42d2012-03-13 14:07:05 +0000266}