1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -div-rem-pairs -S -mtriple=x86_64-unknown-unknown | FileCheck %s
4 declare void @foo(i32, i32)
6 define void @decompose_illegal_srem_same_block(i32 %a, i32 %b) {
7 ; CHECK-LABEL: @decompose_illegal_srem_same_block(
8 ; CHECK-NEXT: [[REM:%.*]] = srem i32 [[A:%.*]], [[B:%.*]]
9 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[A]], [[B]]
10 ; CHECK-NEXT: call void @foo(i32 [[REM]], i32 [[DIV]])
11 ; CHECK-NEXT: ret void
13 %rem = srem i32 %a, %b
14 %div = sdiv i32 %a, %b
15 call void @foo(i32 %rem, i32 %div)
19 define void @decompose_illegal_urem_same_block(i32 %a, i32 %b) {
20 ; CHECK-LABEL: @decompose_illegal_urem_same_block(
21 ; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[A:%.*]], [[B:%.*]]
22 ; CHECK-NEXT: [[REM:%.*]] = urem i32 [[A]], [[B]]
23 ; CHECK-NEXT: call void @foo(i32 [[REM]], i32 [[DIV]])
24 ; CHECK-NEXT: ret void
26 %div = udiv i32 %a, %b
27 %rem = urem i32 %a, %b
28 call void @foo(i32 %rem, i32 %div)
32 ; Hoist and optionally decompose the sdiv because it's safe and free.
33 ; PR31028 - https://bugs.llvm.org/show_bug.cgi?id=31028
35 define i32 @hoist_sdiv(i32 %a, i32 %b) {
36 ; CHECK-LABEL: @hoist_sdiv(
38 ; CHECK-NEXT: [[REM:%.*]] = srem i32 [[A:%.*]], [[B:%.*]]
39 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[A]], [[B]]
40 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[REM]], 42
41 ; CHECK-NEXT: br i1 [[CMP]], label [[IF:%.*]], label [[END:%.*]]
43 ; CHECK-NEXT: br label [[END]]
45 ; CHECK-NEXT: [[RET:%.*]] = phi i32 [ [[DIV]], [[IF]] ], [ 3, [[ENTRY:%.*]] ]
46 ; CHECK-NEXT: ret i32 [[RET]]
49 %rem = srem i32 %a, %b
50 %cmp = icmp eq i32 %rem, 42
51 br i1 %cmp, label %if, label %end
54 %div = sdiv i32 %a, %b
58 %ret = phi i32 [ %div, %if ], [ 3, %entry ]
62 ; Hoist and optionally decompose the udiv because it's safe and free.
64 define i64 @hoist_udiv(i64 %a, i64 %b) {
65 ; CHECK-LABEL: @hoist_udiv(
67 ; CHECK-NEXT: [[REM:%.*]] = urem i64 [[A:%.*]], [[B:%.*]]
68 ; CHECK-NEXT: [[DIV:%.*]] = udiv i64 [[A]], [[B]]
69 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[REM]], 42
70 ; CHECK-NEXT: br i1 [[CMP]], label [[IF:%.*]], label [[END:%.*]]
72 ; CHECK-NEXT: br label [[END]]
74 ; CHECK-NEXT: [[RET:%.*]] = phi i64 [ [[DIV]], [[IF]] ], [ 3, [[ENTRY:%.*]] ]
75 ; CHECK-NEXT: ret i64 [[RET]]
78 %rem = urem i64 %a, %b
79 %cmp = icmp eq i64 %rem, 42
80 br i1 %cmp, label %if, label %end
83 %div = udiv i64 %a, %b
87 %ret = phi i64 [ %div, %if ], [ 3, %entry ]
91 ; Hoist the srem if it's safe and free, otherwise decompose it.
93 define i16 @hoist_srem(i16 %a, i16 %b) {
94 ; CHECK-LABEL: @hoist_srem(
96 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i16 [[A:%.*]], [[B:%.*]]
97 ; CHECK-NEXT: [[REM:%.*]] = srem i16 [[A]], [[B]]
98 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i16 [[DIV]], 42
99 ; CHECK-NEXT: br i1 [[CMP]], label [[IF:%.*]], label [[END:%.*]]
101 ; CHECK-NEXT: br label [[END]]
103 ; CHECK-NEXT: [[RET:%.*]] = phi i16 [ [[REM]], [[IF]] ], [ 3, [[ENTRY:%.*]] ]
104 ; CHECK-NEXT: ret i16 [[RET]]
107 %div = sdiv i16 %a, %b
108 %cmp = icmp eq i16 %div, 42
109 br i1 %cmp, label %if, label %end
112 %rem = srem i16 %a, %b
116 %ret = phi i16 [ %rem, %if ], [ 3, %entry ]
120 ; Hoist the urem if it's safe and free, otherwise decompose it.
122 define i8 @hoist_urem(i8 %a, i8 %b) {
123 ; CHECK-LABEL: @hoist_urem(
125 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[A:%.*]], [[B:%.*]]
126 ; CHECK-NEXT: [[REM:%.*]] = urem i8 [[A]], [[B]]
127 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[DIV]], 42
128 ; CHECK-NEXT: br i1 [[CMP]], label [[IF:%.*]], label [[END:%.*]]
130 ; CHECK-NEXT: br label [[END]]
132 ; CHECK-NEXT: [[RET:%.*]] = phi i8 [ [[REM]], [[IF]] ], [ 3, [[ENTRY:%.*]] ]
133 ; CHECK-NEXT: ret i8 [[RET]]
136 %div = udiv i8 %a, %b
137 %cmp = icmp eq i8 %div, 42
138 br i1 %cmp, label %if, label %end
141 %rem = urem i8 %a, %b
145 %ret = phi i8 [ %rem, %if ], [ 3, %entry ]
149 ; Be careful with RAUW/invalidation if this is a srem-of-srem.
151 define i32 @srem_of_srem_unexpanded(i32 %X, i32 %Y, i32 %Z) {
152 ; CHECK-LABEL: @srem_of_srem_unexpanded(
153 ; CHECK-NEXT: [[T0:%.*]] = mul nsw i32 [[Z:%.*]], [[Y:%.*]]
154 ; CHECK-NEXT: [[T1:%.*]] = sdiv i32 [[X:%.*]], [[T0]]
155 ; CHECK-NEXT: [[T2:%.*]] = mul nsw i32 [[T0]], [[T1]]
156 ; CHECK-NEXT: [[T3:%.*]] = srem i32 [[X]], [[T0]]
157 ; CHECK-NEXT: [[T4:%.*]] = sdiv i32 [[T3]], [[Y]]
158 ; CHECK-NEXT: [[T5:%.*]] = mul nsw i32 [[T4]], [[Y]]
159 ; CHECK-NEXT: [[T6:%.*]] = srem i32 [[T3]], [[Y]]
160 ; CHECK-NEXT: ret i32 [[T6]]
162 %t0 = mul nsw i32 %Z, %Y
163 %t1 = sdiv i32 %X, %t0
164 %t2 = mul nsw i32 %t0, %t1
165 %t3 = srem i32 %X, %t0
166 %t4 = sdiv i32 %t3, %Y
167 %t5 = mul nsw i32 %t4, %Y
168 %t6 = srem i32 %t3, %Y
171 define i32 @srem_of_srem_expanded(i32 %X, i32 %Y, i32 %Z) {
172 ; CHECK-LABEL: @srem_of_srem_expanded(
173 ; CHECK-NEXT: [[T0:%.*]] = mul nsw i32 [[Z:%.*]], [[Y:%.*]]
174 ; CHECK-NEXT: [[T1:%.*]] = sdiv i32 [[X:%.*]], [[T0]]
175 ; CHECK-NEXT: [[T2:%.*]] = mul nsw i32 [[T0]], [[T1]]
176 ; CHECK-NEXT: [[T3_RECOMPOSED:%.*]] = srem i32 [[X]], [[T0]]
177 ; CHECK-NEXT: [[T4:%.*]] = sdiv i32 [[T3_RECOMPOSED]], [[Y]]
178 ; CHECK-NEXT: [[T5:%.*]] = mul nsw i32 [[T4]], [[Y]]
179 ; CHECK-NEXT: [[T6_RECOMPOSED:%.*]] = srem i32 [[T3_RECOMPOSED]], [[Y]]
180 ; CHECK-NEXT: ret i32 [[T6_RECOMPOSED]]
182 %t0 = mul nsw i32 %Z, %Y
183 %t1 = sdiv i32 %X, %t0
184 %t2 = mul nsw i32 %t0, %t1
185 %t3 = sub nsw i32 %X, %t2
186 %t4 = sdiv i32 %t3, %Y
187 %t5 = mul nsw i32 %t4, %Y
188 %t6 = sub nsw i32 %t3, %t5
192 ; If the ops don't match, don't do anything: signedness.
194 define i32 @dont_hoist_udiv(i32 %a, i32 %b) {
195 ; CHECK-LABEL: @dont_hoist_udiv(
197 ; CHECK-NEXT: [[REM:%.*]] = srem i32 [[A:%.*]], [[B:%.*]]
198 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[REM]], 42
199 ; CHECK-NEXT: br i1 [[CMP]], label [[IF:%.*]], label [[END:%.*]]
201 ; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[A]], [[B]]
202 ; CHECK-NEXT: br label [[END]]
204 ; CHECK-NEXT: [[RET:%.*]] = phi i32 [ [[DIV]], [[IF]] ], [ 3, [[ENTRY:%.*]] ]
205 ; CHECK-NEXT: ret i32 [[RET]]
208 %rem = srem i32 %a, %b
209 %cmp = icmp eq i32 %rem, 42
210 br i1 %cmp, label %if, label %end
213 %div = udiv i32 %a, %b
217 %ret = phi i32 [ %div, %if ], [ 3, %entry ]
221 ; If the ops don't match, don't do anything: operation.
223 define i32 @dont_hoist_srem(i32 %a, i32 %b) {
224 ; CHECK-LABEL: @dont_hoist_srem(
226 ; CHECK-NEXT: [[REM:%.*]] = urem i32 [[A:%.*]], [[B:%.*]]
227 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[REM]], 42
228 ; CHECK-NEXT: br i1 [[CMP]], label [[IF:%.*]], label [[END:%.*]]
230 ; CHECK-NEXT: [[REM2:%.*]] = srem i32 [[A]], [[B]]
231 ; CHECK-NEXT: br label [[END]]
233 ; CHECK-NEXT: [[RET:%.*]] = phi i32 [ [[REM2]], [[IF]] ], [ 3, [[ENTRY:%.*]] ]
234 ; CHECK-NEXT: ret i32 [[RET]]
237 %rem = urem i32 %a, %b
238 %cmp = icmp eq i32 %rem, 42
239 br i1 %cmp, label %if, label %end
242 %rem2 = srem i32 %a, %b
246 %ret = phi i32 [ %rem2, %if ], [ 3, %entry ]
250 ; If the ops don't match, don't do anything: operands.
252 define i32 @dont_hoist_sdiv(i32 %a, i32 %b, i32 %c) {
253 ; CHECK-LABEL: @dont_hoist_sdiv(
255 ; CHECK-NEXT: [[REM:%.*]] = srem i32 [[A:%.*]], [[B:%.*]]
256 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[REM]], 42
257 ; CHECK-NEXT: br i1 [[CMP]], label [[IF:%.*]], label [[END:%.*]]
259 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[A]], [[C:%.*]]
260 ; CHECK-NEXT: br label [[END]]
262 ; CHECK-NEXT: [[RET:%.*]] = phi i32 [ [[DIV]], [[IF]] ], [ 3, [[ENTRY:%.*]] ]
263 ; CHECK-NEXT: ret i32 [[RET]]
266 %rem = srem i32 %a, %b
267 %cmp = icmp eq i32 %rem, 42
268 br i1 %cmp, label %if, label %end
271 %div = sdiv i32 %a, %c
275 %ret = phi i32 [ %div, %if ], [ 3, %entry ]
279 ; If the target doesn't have a unified div/rem op for the type, decompose rem in-place to mul+sub.
281 define i128 @dont_hoist_urem(i128 %a, i128 %b) {
282 ; CHECK-LABEL: @dont_hoist_urem(
284 ; CHECK-NEXT: [[A_FROZEN:%.*]] = freeze i128 [[A:%.*]]
285 ; CHECK-NEXT: [[B_FROZEN:%.*]] = freeze i128 [[B:%.*]]
286 ; CHECK-NEXT: [[DIV:%.*]] = udiv i128 [[A_FROZEN]], [[B_FROZEN]]
287 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i128 [[DIV]], 42
288 ; CHECK-NEXT: br i1 [[CMP]], label [[IF:%.*]], label [[END:%.*]]
290 ; CHECK-NEXT: [[TMP0:%.*]] = mul i128 [[DIV]], [[B_FROZEN]]
291 ; CHECK-NEXT: [[REM_DECOMPOSED:%.*]] = sub i128 [[A_FROZEN]], [[TMP0]]
292 ; CHECK-NEXT: br label [[END]]
294 ; CHECK-NEXT: [[RET:%.*]] = phi i128 [ [[REM_DECOMPOSED]], [[IF]] ], [ 3, [[ENTRY:%.*]] ]
295 ; CHECK-NEXT: ret i128 [[RET]]
298 %div = udiv i128 %a, %b
299 %cmp = icmp eq i128 %div, 42
300 br i1 %cmp, label %if, label %end
303 %rem = urem i128 %a, %b
307 %ret = phi i128 [ %rem, %if ], [ 3, %entry ]
311 ; We don't hoist if one op does not dominate the other,
312 ; but we could hoist both ops to the common predecessor block?
314 define i32 @no_domination(i1 %cmp, i32 %a, i32 %b) {
315 ; CHECK-LABEL: @no_domination(
317 ; CHECK-NEXT: br i1 [[CMP:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
319 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i32 [[A:%.*]], [[B:%.*]]
320 ; CHECK-NEXT: br label [[END:%.*]]
322 ; CHECK-NEXT: [[REM:%.*]] = srem i32 [[A]], [[B]]
323 ; CHECK-NEXT: br label [[END]]
325 ; CHECK-NEXT: [[RET:%.*]] = phi i32 [ [[DIV]], [[IF]] ], [ [[REM]], [[ELSE]] ]
326 ; CHECK-NEXT: ret i32 [[RET]]
329 br i1 %cmp, label %if, label %else
332 %div = sdiv i32 %a, %b
336 %rem = srem i32 %a, %b
340 %ret = phi i32 [ %div, %if ], [ %rem, %else ]