2 ; RUN: opt < %s -force-vector-width=2 -passes=loop-vectorize -debug-only=loop-vectorize -disable-output 2>&1 | FileCheck %s
4 target datalayout = "e-m:e-i64:64-i128:128-n32:64-S128"
5 target triple = "aarch64--linux-gnu"
7 ; Check predication-related cost calculations, including scalarization overhead
8 ; and block probability scaling. Note that the functionality being tested is
9 ; not specific to AArch64. We specify a target to get actual values for the
12 ; CHECK-LABEL: predicated_udiv
14 ; This test checks that we correctly compute the cost of the predicated udiv
15 ; instruction. If we assume the block probability is 50%, we compute the cost
19 ; (udiv(2) + extractelement(8) + insertelement(4)) / 2 = 7
21 ; CHECK: Scalarizing and predicating: %tmp4 = udiv i32 %tmp2, %tmp3
22 ; CHECK: Found an estimated cost of 7 for VF 2 For instruction: %tmp4 = udiv i32 %tmp2, %tmp3
24 define i32 @predicated_udiv(ptr %a, ptr %b, i1 %c, i64 %n) {
29 %i = phi i64 [ 0, %entry ], [ %i.next, %for.inc ]
30 %r = phi i32 [ 0, %entry ], [ %tmp6, %for.inc ]
31 %tmp0 = getelementptr inbounds i32, ptr %a, i64 %i
32 %tmp1 = getelementptr inbounds i32, ptr %b, i64 %i
33 %tmp2 = load i32, ptr %tmp0, align 4
34 %tmp3 = load i32, ptr %tmp1, align 4
35 br i1 %c, label %if.then, label %for.inc
38 %tmp4 = udiv i32 %tmp2, %tmp3
42 %tmp5 = phi i32 [ %tmp3, %for.body ], [ %tmp4, %if.then]
43 %tmp6 = add i32 %r, %tmp5
44 %i.next = add nuw nsw i64 %i, 1
45 %cond = icmp slt i64 %i.next, %n
46 br i1 %cond, label %for.body, label %for.end
49 %tmp7 = phi i32 [ %tmp6, %for.inc ]
53 ; CHECK-LABEL: predicated_store
55 ; This test checks that we correctly compute the cost of the predicated store
56 ; instruction. If we assume the block probability is 50%, we compute the cost
60 ; (store(4) + extractelement(4)) / 2 = 4
62 ; CHECK: Scalarizing and predicating: store i32 %tmp2, ptr %tmp0, align 4
63 ; CHECK: Found an estimated cost of 4 for VF 2 For instruction: store i32 %tmp2, ptr %tmp0, align 4
65 define void @predicated_store(ptr %a, i1 %c, i32 %x, i64 %n) {
70 %i = phi i64 [ 0, %entry ], [ %i.next, %for.inc ]
71 %tmp0 = getelementptr inbounds i32, ptr %a, i64 %i
72 %tmp1 = load i32, ptr %tmp0, align 4
73 %tmp2 = add nsw i32 %tmp1, %x
74 br i1 %c, label %if.then, label %for.inc
77 store i32 %tmp2, ptr %tmp0, align 4
81 %i.next = add nuw nsw i64 %i, 1
82 %cond = icmp slt i64 %i.next, %n
83 br i1 %cond, label %for.body, label %for.end
89 ; CHECK-LABEL: predicated_store_phi
91 ; Same as predicate_store except we use a pointer PHI to maintain the address
93 ; CHECK: Found scalar instruction: %addr = phi ptr [ %a, %entry ], [ %addr.next, %for.inc ]
94 ; CHECK: Found scalar instruction: %addr.next = getelementptr inbounds i32, ptr %addr, i64 1
95 ; CHECK: Scalarizing and predicating: store i32 %tmp2, ptr %addr, align 4
96 ; CHECK: Found an estimated cost of 0 for VF 2 For instruction: %addr = phi ptr [ %a, %entry ], [ %addr.next, %for.inc ]
97 ; CHECK: Found an estimated cost of 4 for VF 2 For instruction: store i32 %tmp2, ptr %addr, align 4
99 define void @predicated_store_phi(ptr %a, i1 %c, i32 %x, i64 %n) {
104 %i = phi i64 [ 0, %entry ], [ %i.next, %for.inc ]
105 %addr = phi ptr [ %a, %entry ], [ %addr.next, %for.inc ]
106 %tmp1 = load i32, ptr %addr, align 4
107 %tmp2 = add nsw i32 %tmp1, %x
108 br i1 %c, label %if.then, label %for.inc
111 store i32 %tmp2, ptr %addr, align 4
115 %i.next = add nuw nsw i64 %i, 1
116 %cond = icmp slt i64 %i.next, %n
117 %addr.next = getelementptr inbounds i32, ptr %addr, i64 1
118 br i1 %cond, label %for.body, label %for.end
124 ; CHECK-LABEL: predicated_udiv_scalarized_operand
126 ; This test checks that we correctly compute the cost of the predicated udiv
127 ; instruction and the add instruction it uses. The add is scalarized and sunk
128 ; inside the predicated block. If we assume the block probability is 50%, we
129 ; compute the cost as:
132 ; (add(2) + extractelement(4)) / 2 = 3
134 ; (udiv(2) + extractelement(4) + insertelement(4)) / 2 = 5
136 ; CHECK: Scalarizing: %tmp3 = add nsw i32 %tmp2, %x
137 ; CHECK: Scalarizing and predicating: %tmp4 = udiv i32 %tmp2, %tmp3
138 ; CHECK: Found an estimated cost of 3 for VF 2 For instruction: %tmp3 = add nsw i32 %tmp2, %x
139 ; CHECK: Found an estimated cost of 5 for VF 2 For instruction: %tmp4 = udiv i32 %tmp2, %tmp3
141 define i32 @predicated_udiv_scalarized_operand(ptr %a, i1 %c, i32 %x, i64 %n) {
146 %i = phi i64 [ 0, %entry ], [ %i.next, %for.inc ]
147 %r = phi i32 [ 0, %entry ], [ %tmp6, %for.inc ]
148 %tmp0 = getelementptr inbounds i32, ptr %a, i64 %i
149 %tmp2 = load i32, ptr %tmp0, align 4
150 br i1 %c, label %if.then, label %for.inc
153 %tmp3 = add nsw i32 %tmp2, %x
154 %tmp4 = udiv i32 %tmp2, %tmp3
158 %tmp5 = phi i32 [ %tmp2, %for.body ], [ %tmp4, %if.then]
159 %tmp6 = add i32 %r, %tmp5
160 %i.next = add nuw nsw i64 %i, 1
161 %cond = icmp slt i64 %i.next, %n
162 br i1 %cond, label %for.body, label %for.end
165 %tmp7 = phi i32 [ %tmp6, %for.inc ]
169 ; CHECK-LABEL: predicated_store_scalarized_operand
171 ; This test checks that we correctly compute the cost of the predicated store
172 ; instruction and the add instruction it uses. The add is scalarized and sunk
173 ; inside the predicated block. If we assume the block probability is 50%, we
174 ; compute the cost as:
177 ; (add(2) + extractelement(4)) / 2 = 3
181 ; CHECK: Scalarizing: %tmp2 = add nsw i32 %tmp1, %x
182 ; CHECK: Scalarizing and predicating: store i32 %tmp2, ptr %tmp0, align 4
183 ; CHECK: Found an estimated cost of 3 for VF 2 For instruction: %tmp2 = add nsw i32 %tmp1, %x
184 ; CHECK: Found an estimated cost of 2 for VF 2 For instruction: store i32 %tmp2, ptr %tmp0, align 4
186 define void @predicated_store_scalarized_operand(ptr %a, i1 %c, i32 %x, i64 %n) {
191 %i = phi i64 [ 0, %entry ], [ %i.next, %for.inc ]
192 %tmp0 = getelementptr inbounds i32, ptr %a, i64 %i
193 %tmp1 = load i32, ptr %tmp0, align 4
194 br i1 %c, label %if.then, label %for.inc
197 %tmp2 = add nsw i32 %tmp1, %x
198 store i32 %tmp2, ptr %tmp0, align 4
202 %i.next = add nuw nsw i64 %i, 1
203 %cond = icmp slt i64 %i.next, %n
204 br i1 %cond, label %for.body, label %for.end
210 ; CHECK-LABEL: predication_multi_context
212 ; This test checks that we correctly compute the cost of multiple predicated
213 ; instructions in the same block. The sdiv, udiv, and store must be scalarized
214 ; and predicated. The sub feeding the store is scalarized and sunk inside the
215 ; store's predicated block. However, the add feeding the sdiv and udiv cannot
216 ; be sunk and is not scalarized. If we assume the block probability is 50%, we
217 ; compute the cost as:
222 ; (sdiv(2) + extractelement(8) + insertelement(4)) / 2 = 7
224 ; (udiv(2) + extractelement(8) + insertelement(4)) / 2 = 7
226 ; (sub(2) + extractelement(4)) / 2 = 3
230 ; CHECK-NOT: Scalarizing: %tmp2 = add i32 %tmp1, %x
231 ; CHECK: Scalarizing and predicating: %tmp3 = sdiv i32 %tmp1, %tmp2
232 ; CHECK: Scalarizing and predicating: %tmp4 = udiv i32 %tmp3, %tmp2
233 ; CHECK: Scalarizing: %tmp5 = sub i32 %tmp4, %x
234 ; CHECK: Scalarizing and predicating: store i32 %tmp5, ptr %tmp0, align 4
235 ; CHECK: Found an estimated cost of 1 for VF 2 For instruction: %tmp2 = add i32 %tmp1, %x
236 ; CHECK: Found an estimated cost of 7 for VF 2 For instruction: %tmp3 = sdiv i32 %tmp1, %tmp2
237 ; CHECK: Found an estimated cost of 7 for VF 2 For instruction: %tmp4 = udiv i32 %tmp3, %tmp2
238 ; CHECK: Found an estimated cost of 3 for VF 2 For instruction: %tmp5 = sub i32 %tmp4, %x
239 ; CHECK: Found an estimated cost of 2 for VF 2 For instruction: store i32 %tmp5, ptr %tmp0, align 4
241 define void @predication_multi_context(ptr %a, i1 %c, i32 %x, i64 %n) {
246 %i = phi i64 [ 0, %entry ], [ %i.next, %for.inc ]
247 %tmp0 = getelementptr inbounds i32, ptr %a, i64 %i
248 %tmp1 = load i32, ptr %tmp0, align 4
249 br i1 %c, label %if.then, label %for.inc
252 %tmp2 = add i32 %tmp1, %x
253 %tmp3 = sdiv i32 %tmp1, %tmp2
254 %tmp4 = udiv i32 %tmp3, %tmp2
255 %tmp5 = sub i32 %tmp4, %x
256 store i32 %tmp5, ptr %tmp0, align 4
260 %i.next = add nuw nsw i64 %i, 1
261 %cond = icmp slt i64 %i.next, %n
262 br i1 %cond, label %for.body, label %for.end