1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=div-rem-pairs -S -mtriple=mips-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: [[DIV:%.*]] = sdiv i32 [[A:%.*]], [[B:%.*]]
9 ; CHECK-NEXT: [[T0:%.*]] = mul i32 [[DIV]], [[B]]
10 ; CHECK-NEXT: [[REM_RECOMPOSED:%.*]] = srem i32 [[A]], [[B]]
11 ; CHECK-NEXT: call void @foo(i32 [[REM_RECOMPOSED]], i32 [[DIV]])
12 ; CHECK-NEXT: ret void
14 %div = sdiv i32 %a, %b
15 %t0 = mul i32 %div, %b
16 %rem = sub i32 %a, %t0
17 call void @foo(i32 %rem, i32 %div)
21 define void @decompose_illegal_urem_same_block(i32 %a, i32 %b) {
22 ; CHECK-LABEL: @decompose_illegal_urem_same_block(
23 ; CHECK-NEXT: [[DIV:%.*]] = udiv i32 [[A:%.*]], [[B:%.*]]
24 ; CHECK-NEXT: [[T0:%.*]] = mul i32 [[DIV]], [[B]]
25 ; CHECK-NEXT: [[REM_RECOMPOSED:%.*]] = urem i32 [[A]], [[B]]
26 ; CHECK-NEXT: call void @foo(i32 [[REM_RECOMPOSED]], i32 [[DIV]])
27 ; CHECK-NEXT: ret void
29 %div = udiv i32 %a, %b
30 %t0 = mul i32 %div, %b
31 %rem = sub i32 %a, %t0
32 call void @foo(i32 %rem, i32 %div)
36 ; Recompose and hoist the srem if it's safe and free, otherwise keep as-is..
38 define i16 @hoist_srem(i16 %a, i16 %b) {
39 ; CHECK-LABEL: @hoist_srem(
41 ; CHECK-NEXT: [[DIV:%.*]] = sdiv i16 [[A:%.*]], [[B:%.*]]
42 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i16 [[DIV]], 42
43 ; CHECK-NEXT: br i1 [[CMP]], label [[IF:%.*]], label [[END:%.*]]
45 ; CHECK-NEXT: [[T0:%.*]] = mul i16 [[DIV]], [[B]]
46 ; CHECK-NEXT: [[REM:%.*]] = sub i16 [[A]], [[T0]]
47 ; CHECK-NEXT: br label [[END]]
49 ; CHECK-NEXT: [[RET:%.*]] = phi i16 [ [[REM]], [[IF]] ], [ 3, [[ENTRY:%.*]] ]
50 ; CHECK-NEXT: ret i16 [[RET]]
53 %div = sdiv i16 %a, %b
54 %cmp = icmp eq i16 %div, 42
55 br i1 %cmp, label %if, label %end
58 %t0 = mul i16 %div, %b
59 %rem = sub i16 %a, %t0
63 %ret = phi i16 [ %rem, %if ], [ 3, %entry ]
67 ; Recompose and hoist the urem if it's safe and free, otherwise keep as-is..
69 define i8 @hoist_urem(i8 %a, i8 %b) {
70 ; CHECK-LABEL: @hoist_urem(
72 ; CHECK-NEXT: [[DIV:%.*]] = udiv i8 [[A:%.*]], [[B:%.*]]
73 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[DIV]], 42
74 ; CHECK-NEXT: br i1 [[CMP]], label [[IF:%.*]], label [[END:%.*]]
76 ; CHECK-NEXT: [[T0:%.*]] = mul i8 [[DIV]], [[B]]
77 ; CHECK-NEXT: [[REM:%.*]] = sub i8 [[A]], [[T0]]
78 ; CHECK-NEXT: br label [[END]]
80 ; CHECK-NEXT: [[RET:%.*]] = phi i8 [ [[REM]], [[IF]] ], [ 3, [[ENTRY:%.*]] ]
81 ; CHECK-NEXT: ret i8 [[RET]]
85 %cmp = icmp eq i8 %div, 42
86 br i1 %cmp, label %if, label %end
94 %ret = phi i8 [ %rem, %if ], [ 3, %entry ]
98 ; Be careful with RAUW/invalidation if this is a srem-of-srem.
100 define i32 @srem_of_srem_unexpanded(i32 %X, i32 %Y, i32 %Z) {
101 ; CHECK-LABEL: @srem_of_srem_unexpanded(
102 ; CHECK-NEXT: [[T0:%.*]] = mul nsw i32 [[Z:%.*]], [[Y:%.*]]
103 ; CHECK-NEXT: [[T1:%.*]] = sdiv i32 [[X:%.*]], [[T0]]
104 ; CHECK-NEXT: [[T2:%.*]] = mul nsw i32 [[T0]], [[T1]]
105 ; CHECK-NEXT: [[T3:%.*]] = srem i32 [[X]], [[T0]]
106 ; CHECK-NEXT: [[T4:%.*]] = sdiv i32 [[T3]], [[Y]]
107 ; CHECK-NEXT: [[T5:%.*]] = mul nsw i32 [[T4]], [[Y]]
108 ; CHECK-NEXT: [[T6:%.*]] = srem i32 [[T3]], [[Y]]
109 ; CHECK-NEXT: ret i32 [[T6]]
111 %t0 = mul nsw i32 %Z, %Y
112 %t1 = sdiv i32 %X, %t0
113 %t2 = mul nsw i32 %t0, %t1
114 %t3 = srem i32 %X, %t0
115 %t4 = sdiv i32 %t3, %Y
116 %t5 = mul nsw i32 %t4, %Y
117 %t6 = srem i32 %t3, %Y
120 define i32 @srem_of_srem_expanded(i32 %X, i32 %Y, i32 %Z) {
121 ; CHECK-LABEL: @srem_of_srem_expanded(
122 ; CHECK-NEXT: [[T0:%.*]] = mul nsw i32 [[Z:%.*]], [[Y:%.*]]
123 ; CHECK-NEXT: [[T1:%.*]] = sdiv i32 [[X:%.*]], [[T0]]
124 ; CHECK-NEXT: [[T2:%.*]] = mul nsw i32 [[T0]], [[T1]]
125 ; CHECK-NEXT: [[T3_RECOMPOSED:%.*]] = srem i32 [[X]], [[T0]]
126 ; CHECK-NEXT: [[T4:%.*]] = sdiv i32 [[T3_RECOMPOSED]], [[Y]]
127 ; CHECK-NEXT: [[T5:%.*]] = mul nsw i32 [[T4]], [[Y]]
128 ; CHECK-NEXT: [[T6_RECOMPOSED:%.*]] = srem i32 [[T3_RECOMPOSED]], [[Y]]
129 ; CHECK-NEXT: ret i32 [[T6_RECOMPOSED]]
131 %t0 = mul nsw i32 %Z, %Y
132 %t1 = sdiv i32 %X, %t0
133 %t2 = mul nsw i32 %t0, %t1
134 %t3 = sub nsw i32 %X, %t2
135 %t4 = sdiv i32 %t3, %Y
136 %t5 = mul nsw i32 %t4, %Y
137 %t6 = sub nsw i32 %t3, %t5
141 ; If the target doesn't have a unified div/rem op for the type, keep decomposed rem
143 define i128 @dont_hoist_urem(i128 %a, i128 %b) {
144 ; CHECK-LABEL: @dont_hoist_urem(
146 ; CHECK-NEXT: [[DIV:%.*]] = udiv i128 [[A:%.*]], [[B:%.*]]
147 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i128 [[DIV]], 42
148 ; CHECK-NEXT: br i1 [[CMP]], label [[IF:%.*]], label [[END:%.*]]
150 ; CHECK-NEXT: [[T0:%.*]] = mul i128 [[DIV]], [[B]]
151 ; CHECK-NEXT: [[REM:%.*]] = sub i128 [[A]], [[T0]]
152 ; CHECK-NEXT: br label [[END]]
154 ; CHECK-NEXT: [[RET:%.*]] = phi i128 [ [[REM]], [[IF]] ], [ 3, [[ENTRY:%.*]] ]
155 ; CHECK-NEXT: ret i128 [[RET]]
158 %div = udiv i128 %a, %b
159 %cmp = icmp eq i128 %div, 42
160 br i1 %cmp, label %if, label %end
163 %t0 = mul i128 %div, %b
164 %rem = sub i128 %a, %t0
168 %ret = phi i128 [ %rem, %if ], [ 3, %entry ]