blob: 335df32a5054a44feac00b0f91e1d82e59532de9 [file] [log] [blame]
Sanjay Patel69632442016-03-25 20:12:25 +00001; NOTE: Assertions have been autogenerated by 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 Patel69632442016-03-25 20:12:25 +00006; CHECK: ret i32 %x
7;
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 Patel69632442016-03-25 20:12:25 +000016; CHECK: [[L:%.*]] = and i32 %x, %y
17; 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 Patel69632442016-03-25 20:12:25 +000027; CHECK: [[R:%.*]] = and i32 %x, %y
28; 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 Patel69632442016-03-25 20:12:25 +000038; CHECK: [[L:%.*]] = or i32 %x, %y
39; 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 Patel69632442016-03-25 20:12:25 +000049; CHECK: [[R:%.*]] = or i32 %x, %y
50; 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 Patel69632442016-03-25 20:12:25 +000060; CHECK: ret i32 %y
61;
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 Patel69632442016-03-25 20:12:25 +000070; CHECK: ret i32 %y
71;
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 Patel69632442016-03-25 20:12:25 +000080; CHECK: ret i32 %y
81;
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 Patel69632442016-03-25 20:12:25 +000089; CHECK: ret i32 -1
90;
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 Patel69632442016-03-25 20:12:25 +000099; CHECK: ret i32 %x
100;
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
109define i32 @sdiv1(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000110; CHECK-LABEL: @sdiv1(
Sanjay Patel69632442016-03-25 20:12:25 +0000111; CHECK: ret i32 %x
112;
Duncan Sands771e82a2011-01-28 16:51:11 +0000113; (no overflow X * Y) / Y -> X
114 %mul = mul nsw i32 %x, %y
115 %r = sdiv i32 %mul, %y
116 ret i32 %r
Duncan Sands771e82a2011-01-28 16:51:11 +0000117}
118
119define i32 @sdiv2(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000120; CHECK-LABEL: @sdiv2(
Sanjay Patel69632442016-03-25 20:12:25 +0000121; CHECK: [[DIV:%.*]] = sdiv i32 %x, %y
122; CHECK-NEXT: ret i32 [[DIV]]
123;
Duncan Sands771e82a2011-01-28 16:51:11 +0000124; (((X / Y) * Y) / Y) -> X / Y
125 %div = sdiv i32 %x, %y
126 %mul = mul i32 %div, %y
127 %r = sdiv i32 %mul, %y
128 ret i32 %r
Duncan Sands771e82a2011-01-28 16:51:11 +0000129}
130
131define i32 @sdiv3(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000132; CHECK-LABEL: @sdiv3(
Sanjay Patel69632442016-03-25 20:12:25 +0000133; CHECK: ret i32 0
134;
Duncan Sands771e82a2011-01-28 16:51:11 +0000135; (X rem Y) / Y -> 0
136 %rem = srem i32 %x, %y
137 %div = sdiv i32 %rem, %y
138 ret i32 %div
Duncan Sands771e82a2011-01-28 16:51:11 +0000139}
140
Duncan Sandsb67edc62011-01-30 18:03:50 +0000141define i32 @sdiv4(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000142; CHECK-LABEL: @sdiv4(
Sanjay Patel69632442016-03-25 20:12:25 +0000143; CHECK: ret i32 %x
144;
Duncan Sandsb67edc62011-01-30 18:03:50 +0000145; (X / Y) * Y -> X if the division is exact
146 %div = sdiv exact i32 %x, %y
147 %mul = mul i32 %div, %y
148 ret i32 %mul
Duncan Sandsb67edc62011-01-30 18:03:50 +0000149}
150
151define i32 @sdiv5(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000152; CHECK-LABEL: @sdiv5(
Sanjay Patel69632442016-03-25 20:12:25 +0000153; CHECK: ret i32 %x
154;
Duncan Sandsb67edc62011-01-30 18:03:50 +0000155; Y * (X / Y) -> X if the division is exact
156 %div = sdiv exact i32 %x, %y
157 %mul = mul i32 %y, %div
158 ret i32 %mul
Duncan Sandsb67edc62011-01-30 18:03:50 +0000159}
160
Chris Lattner6e57b152011-02-06 22:05:31 +0000161
Duncan Sands771e82a2011-01-28 16:51:11 +0000162define i32 @udiv1(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000163; CHECK-LABEL: @udiv1(
Sanjay Patel69632442016-03-25 20:12:25 +0000164; CHECK: ret i32 %x
165;
Duncan Sands771e82a2011-01-28 16:51:11 +0000166; (no overflow X * Y) / Y -> X
167 %mul = mul nuw i32 %x, %y
168 %r = udiv i32 %mul, %y
169 ret i32 %r
Duncan Sands771e82a2011-01-28 16:51:11 +0000170}
171
172define i32 @udiv2(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000173; CHECK-LABEL: @udiv2(
Sanjay Patel69632442016-03-25 20:12:25 +0000174; CHECK: [[DIV:%.*]] = udiv i32 %x, %y
175; CHECK-NEXT: ret i32 [[DIV]]
176;
Duncan Sands771e82a2011-01-28 16:51:11 +0000177; (((X / Y) * Y) / Y) -> X / Y
178 %div = udiv i32 %x, %y
179 %mul = mul i32 %div, %y
180 %r = udiv i32 %mul, %y
181 ret i32 %r
Duncan Sands771e82a2011-01-28 16:51:11 +0000182}
183
184define i32 @udiv3(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000185; CHECK-LABEL: @udiv3(
Sanjay Patel69632442016-03-25 20:12:25 +0000186; CHECK: ret i32 0
187;
Duncan Sands771e82a2011-01-28 16:51:11 +0000188; (X rem Y) / Y -> 0
189 %rem = urem i32 %x, %y
190 %div = udiv i32 %rem, %y
191 ret i32 %div
Duncan Sands771e82a2011-01-28 16:51:11 +0000192}
Chris Lattner6e57b152011-02-06 22:05:31 +0000193
194define i32 @udiv4(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000195; CHECK-LABEL: @udiv4(
Sanjay Patel69632442016-03-25 20:12:25 +0000196; CHECK: ret i32 %x
197;
Chris Lattner6e57b152011-02-06 22:05:31 +0000198; (X / Y) * Y -> X if the division is exact
199 %div = udiv exact i32 %x, %y
200 %mul = mul i32 %div, %y
201 ret i32 %mul
Chris Lattner6e57b152011-02-06 22:05:31 +0000202}
203
204define i32 @udiv5(i32 %x, i32 %y) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000205; CHECK-LABEL: @udiv5(
Sanjay Patel69632442016-03-25 20:12:25 +0000206; CHECK: ret i32 %x
207;
Chris Lattner6e57b152011-02-06 22:05:31 +0000208; Y * (X / Y) -> X if the division is exact
209 %div = udiv exact i32 %x, %y
210 %mul = mul i32 %y, %div
211 ret i32 %mul
Chris Lattner6e57b152011-02-06 22:05:31 +0000212}
213
Duncan Sands395ac42d2012-03-13 14:07:05 +0000214define i16 @trunc1(i32 %x) {
Stephen Linc1c7a132013-07-14 01:42:54 +0000215; CHECK-LABEL: @trunc1(
Sanjay Patel69632442016-03-25 20:12:25 +0000216; CHECK: ret i16 1
217;
Duncan Sands395ac42d2012-03-13 14:07:05 +0000218 %y = add i32 %x, 1
219 %tx = trunc i32 %x to i16
220 %ty = trunc i32 %y to i16
221 %d = sub i16 %ty, %tx
222 ret i16 %d
Duncan Sands395ac42d2012-03-13 14:07:05 +0000223}