2 ; RUN: opt < %s -force-vector-width=2 -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(6) + insertelement(3)) / 2 = 5
21 ; CHECK: Scalarizing and predicating: %tmp4 = udiv i32 %tmp2, %tmp3
22 ; CHECK: Found an estimated cost of 5 for VF 2 For instruction: %tmp4 = udiv i32 %tmp2, %tmp3
24 define i32 @predicated_udiv(i32* %a, i32* %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, i32* %a, i64 %i
32 %tmp1 = getelementptr inbounds i32, i32* %b, i64 %i
33 %tmp2 = load i32, i32* %tmp0, align 4
34 %tmp3 = load i32, i32* %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(3)) / 2 = 3
62 ; CHECK: Scalarizing and predicating: store i32 %tmp2, i32* %tmp0, align 4
63 ; CHECK: Found an estimated cost of 3 for VF 2 For instruction: store i32 %tmp2, i32* %tmp0, align 4
65 define void @predicated_store(i32* %a, i1 %c, i32 %x, i64 %n) {
70 %i = phi i64 [ 0, %entry ], [ %i.next, %for.inc ]
71 %tmp0 = getelementptr inbounds i32, i32* %a, i64 %i
72 %tmp1 = load i32, i32* %tmp0, align 4
73 %tmp2 = add nsw i32 %tmp1, %x
74 br i1 %c, label %if.then, label %for.inc
77 store i32 %tmp2, i32* %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_udiv_scalarized_operand
91 ; This test checks that we correctly compute the cost of the predicated udiv
92 ; instruction and the add instruction it uses. The add is scalarized and sunk
93 ; inside the predicated block. If we assume the block probability is 50%, we
94 ; compute the cost as:
97 ; (add(2) + extractelement(3)) / 2 = 2
99 ; (udiv(2) + extractelement(3) + insertelement(3)) / 2 = 4
101 ; CHECK: Scalarizing: %tmp3 = add nsw i32 %tmp2, %x
102 ; CHECK: Scalarizing and predicating: %tmp4 = udiv i32 %tmp2, %tmp3
103 ; CHECK: Found an estimated cost of 2 for VF 2 For instruction: %tmp3 = add nsw i32 %tmp2, %x
104 ; CHECK: Found an estimated cost of 4 for VF 2 For instruction: %tmp4 = udiv i32 %tmp2, %tmp3
106 define i32 @predicated_udiv_scalarized_operand(i32* %a, i1 %c, i32 %x, i64 %n) {
111 %i = phi i64 [ 0, %entry ], [ %i.next, %for.inc ]
112 %r = phi i32 [ 0, %entry ], [ %tmp6, %for.inc ]
113 %tmp0 = getelementptr inbounds i32, i32* %a, i64 %i
114 %tmp2 = load i32, i32* %tmp0, align 4
115 br i1 %c, label %if.then, label %for.inc
118 %tmp3 = add nsw i32 %tmp2, %x
119 %tmp4 = udiv i32 %tmp2, %tmp3
123 %tmp5 = phi i32 [ %tmp2, %for.body ], [ %tmp4, %if.then]
124 %tmp6 = add i32 %r, %tmp5
125 %i.next = add nuw nsw i64 %i, 1
126 %cond = icmp slt i64 %i.next, %n
127 br i1 %cond, label %for.body, label %for.end
130 %tmp7 = phi i32 [ %tmp6, %for.inc ]
134 ; CHECK-LABEL: predicated_store_scalarized_operand
136 ; This test checks that we correctly compute the cost of the predicated store
137 ; instruction and the add instruction it uses. The add is scalarized and sunk
138 ; inside the predicated block. If we assume the block probability is 50%, we
139 ; compute the cost as:
142 ; (add(2) + extractelement(3)) / 2 = 2
146 ; CHECK: Scalarizing: %tmp2 = add nsw i32 %tmp1, %x
147 ; CHECK: Scalarizing and predicating: store i32 %tmp2, i32* %tmp0, align 4
148 ; CHECK: Found an estimated cost of 2 for VF 2 For instruction: %tmp2 = add nsw i32 %tmp1, %x
149 ; CHECK: Found an estimated cost of 2 for VF 2 For instruction: store i32 %tmp2, i32* %tmp0, align 4
151 define void @predicated_store_scalarized_operand(i32* %a, i1 %c, i32 %x, i64 %n) {
156 %i = phi i64 [ 0, %entry ], [ %i.next, %for.inc ]
157 %tmp0 = getelementptr inbounds i32, i32* %a, i64 %i
158 %tmp1 = load i32, i32* %tmp0, align 4
159 br i1 %c, label %if.then, label %for.inc
162 %tmp2 = add nsw i32 %tmp1, %x
163 store i32 %tmp2, i32* %tmp0, align 4
167 %i.next = add nuw nsw i64 %i, 1
168 %cond = icmp slt i64 %i.next, %n
169 br i1 %cond, label %for.body, label %for.end
175 ; CHECK-LABEL: predication_multi_context
177 ; This test checks that we correctly compute the cost of multiple predicated
178 ; instructions in the same block. The sdiv, udiv, and store must be scalarized
179 ; and predicated. The sub feeding the store is scalarized and sunk inside the
180 ; store's predicated block. However, the add feeding the sdiv and udiv cannot
181 ; be sunk and is not scalarized. If we assume the block probability is 50%, we
182 ; compute the cost as:
187 ; (sdiv(2) + extractelement(6) + insertelement(3)) / 2 = 5
189 ; (udiv(2) + extractelement(6) + insertelement(3)) / 2 = 5
191 ; (sub(2) + extractelement(3)) / 2 = 2
195 ; CHECK-NOT: Scalarizing: %tmp2 = add i32 %tmp1, %x
196 ; CHECK: Scalarizing and predicating: %tmp3 = sdiv i32 %tmp1, %tmp2
197 ; CHECK: Scalarizing and predicating: %tmp4 = udiv i32 %tmp3, %tmp2
198 ; CHECK: Scalarizing: %tmp5 = sub i32 %tmp4, %x
199 ; CHECK: Scalarizing and predicating: store i32 %tmp5, i32* %tmp0, align 4
200 ; CHECK: Found an estimated cost of 1 for VF 2 For instruction: %tmp2 = add i32 %tmp1, %x
201 ; CHECK: Found an estimated cost of 5 for VF 2 For instruction: %tmp3 = sdiv i32 %tmp1, %tmp2
202 ; CHECK: Found an estimated cost of 5 for VF 2 For instruction: %tmp4 = udiv i32 %tmp3, %tmp2
203 ; CHECK: Found an estimated cost of 2 for VF 2 For instruction: %tmp5 = sub i32 %tmp4, %x
204 ; CHECK: Found an estimated cost of 2 for VF 2 For instruction: store i32 %tmp5, i32* %tmp0, align 4
206 define void @predication_multi_context(i32* %a, i1 %c, i32 %x, i64 %n) {
211 %i = phi i64 [ 0, %entry ], [ %i.next, %for.inc ]
212 %tmp0 = getelementptr inbounds i32, i32* %a, i64 %i
213 %tmp1 = load i32, i32* %tmp0, align 4
214 br i1 %c, label %if.then, label %for.inc
217 %tmp2 = add i32 %tmp1, %x
218 %tmp3 = sdiv i32 %tmp1, %tmp2
219 %tmp4 = udiv i32 %tmp3, %tmp2
220 %tmp5 = sub i32 %tmp4, %x
221 store i32 %tmp5, i32* %tmp0, align 4
225 %i.next = add nuw nsw i64 %i, 1
226 %cond = icmp slt i64 %i.next, %n
227 br i1 %cond, label %for.body, label %for.end