1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
4 define i32 @add1(i32 %x) {
6 ; CHECK-NEXT: ret i32 [[X:%.*]]
14 define i32 @and1(i32 %x, i32 %y) {
16 ; CHECK-NEXT: [[L:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
17 ; CHECK-NEXT: ret i32 [[L]]
19 ; (X & Y) & X -> X & Y
25 define i32 @and2(i32 %x, i32 %y) {
27 ; CHECK-NEXT: [[R:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
28 ; CHECK-NEXT: ret i32 [[R]]
30 ; X & (X & Y) -> X & Y
36 define i32 @or1(i32 %x, i32 %y) {
38 ; CHECK-NEXT: [[L:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
39 ; CHECK-NEXT: ret i32 [[L]]
41 ; (X | Y) | X -> X | Y
47 define i32 @or2(i32 %x, i32 %y) {
49 ; CHECK-NEXT: [[R:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
50 ; CHECK-NEXT: ret i32 [[R]]
52 ; X | (X | Y) -> X | Y
58 define i32 @xor1(i32 %x, i32 %y) {
60 ; CHECK-NEXT: ret i32 [[Y:%.*]]
68 define i32 @xor2(i32 %x, i32 %y) {
70 ; CHECK-NEXT: ret i32 [[Y:%.*]]
78 define i32 @sub1(i32 %x, i32 %y) {
80 ; CHECK-NEXT: ret i32 [[Y:%.*]]
87 define i32 @sub2(i32 %x) {
89 ; CHECK-NEXT: ret i32 -1
97 define i32 @sub3(i32 %x, i32 %y) {
99 ; CHECK-NEXT: ret i32 [[X:%.*]]
101 ; ((X + 1) + Y) - (Y + 1) -> X
103 %lhs = add i32 %xp1, %y
105 %r = sub i32 %lhs, %rhs
109 ; (no overflow X * Y) / Y -> X
111 define i32 @mulnsw_sdiv(i32 %x, i32 %y) {
112 ; CHECK-LABEL: @mulnsw_sdiv(
113 ; CHECK-NEXT: ret i32 [[X:%.*]]
115 %mul = mul nsw i32 %x, %y
116 %r = sdiv i32 %mul, %y
120 define <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:%.*]]
124 %mul = mul nsw <2 x i32> %y, %x
125 %r = sdiv <2 x i32> %mul, %y
129 ; (no overflow X * Y) / Y -> X
131 define <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:%.*]]
135 %mul = mul nuw <2 x i8> %x, %y
136 %r = udiv <2 x i8> %mul, %y
140 define i32 @mulnuw_udiv_commute(i32 %x, i32 %y) {
141 ; CHECK-LABEL: @mulnuw_udiv_commute(
142 ; CHECK-NEXT: ret i32 [[X:%.*]]
144 %mul = mul nuw i32 %y, %x
145 %r = udiv i32 %mul, %y
149 ; (((X / Y) * Y) / Y) -> X / Y
151 define i32 @sdiv_mul_sdiv(i32 %x, i32 %y) {
152 ; CHECK-LABEL: @sdiv_mul_sdiv(
153 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[X:%.*]], [[Y:%.*]]
154 ; CHECK-NEXT: ret i32 [[DIV]]
156 %div = sdiv i32 %x, %y
157 %mul = mul i32 %div, %y
158 %r = sdiv i32 %mul, %y
162 define 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]]
167 %div = sdiv i32 %x, %y
168 %mul = mul i32 %y, %div
169 %r = sdiv i32 %mul, %y
173 ; (((X / Y) * Y) / Y) -> X / Y
175 define 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]]
180 %div = udiv i32 %x, %y
181 %mul = mul i32 %div, %y
182 %r = udiv i32 %mul, %y
186 define 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]]
191 %div = udiv i32 %x, %y
192 %mul = mul i32 %y, %div
193 %r = udiv i32 %mul, %y
197 define i32 @sdiv3(i32 %x, i32 %y) {
198 ; CHECK-LABEL: @sdiv3(
199 ; CHECK-NEXT: ret i32 0
202 %rem = srem i32 %x, %y
203 %div = sdiv i32 %rem, %y
207 define i32 @sdiv4(i32 %x, i32 %y) {
208 ; CHECK-LABEL: @sdiv4(
209 ; CHECK-NEXT: ret i32 [[X:%.*]]
211 ; (X / Y) * Y -> X if the division is exact
212 %div = sdiv exact i32 %x, %y
213 %mul = mul i32 %div, %y
217 define i32 @sdiv5(i32 %x, i32 %y) {
218 ; CHECK-LABEL: @sdiv5(
219 ; CHECK-NEXT: ret i32 [[X:%.*]]
221 ; Y * (X / Y) -> X if the division is exact
222 %div = sdiv exact i32 %x, %y
223 %mul = mul i32 %y, %div
227 define i32 @udiv3(i32 %x, i32 %y) {
228 ; CHECK-LABEL: @udiv3(
229 ; CHECK-NEXT: ret i32 0
232 %rem = urem i32 %x, %y
233 %div = udiv i32 %rem, %y
237 define i32 @udiv4(i32 %x, i32 %y) {
238 ; CHECK-LABEL: @udiv4(
239 ; CHECK-NEXT: ret i32 [[X:%.*]]
241 ; (X / Y) * Y -> X if the division is exact
242 %div = udiv exact i32 %x, %y
243 %mul = mul i32 %div, %y
247 define i32 @udiv5(i32 %x, i32 %y) {
248 ; CHECK-LABEL: @udiv5(
249 ; CHECK-NEXT: ret i32 [[X:%.*]]
251 ; Y * (X / Y) -> X if the division is exact
252 %div = udiv exact i32 %x, %y
253 %mul = mul i32 %y, %div
257 define i16 @trunc1(i32 %x) {
258 ; CHECK-LABEL: @trunc1(
259 ; CHECK-NEXT: ret i16 1
262 %tx = trunc i32 %x to i16
263 %ty = trunc i32 %y to i16
264 %d = sub i16 %ty, %tx