aboutsummaryrefslogtreecommitdiff
path: root/llvm/test/Transforms/LoopVectorize/pr38697.ll
blob: 72b698613c6e9d59b4369a676d452db045e9721e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
; RUN: opt -loop-vectorize -force-vector-width=2 -S < %s 2>&1 | FileCheck %s
; RUN: opt -indvars -S < %s 2>&1 | FileCheck %s -check-prefix=INDVARCHECK

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

; Produced from test-case:
;
; void testCountIncrLoop(unsigned char *ptr, int lim, int count, int val)
; {
;   int inx = 0;
;   for (int outer_i = 0; outer_i < lim; ++outer_i) {
;     if (count > 0) { // At runtime, 'count' is 0, so the following code is dead.
;       int result = val;
;       int tmp = count;
;
;       while (tmp < 8) {
;         result += val >> tmp;
;         tmp += count;
;       }
;
;       ptr[inx++] = (unsigned char) result;
;     }
;   }
; }
;
; No explicit division appears in the input, but a division is generated during
; vectorization, and that division is a division-by-0 when the input 'count'
; is 0, so it cannot be hoisted above the guard of 'count > 0'.

; Verify that a 'udiv' does not appear in the 'loop1.preheader' block, and that
; a 'udiv' has been inserted at the top of the 'while.body.preheader' block.
define void @testCountIncrLoop(i8* %ptr, i32 %lim, i32 %count, i32 %val) {
; CHECK-LABEL: @testCountIncrLoop(
; CHECK-NEXT:  entry:
; CHECK:       loop1.preheader:
; CHECK-NOT:     udiv
; CHECK:       loop1.body:
; CHECK:       while.cond.preheader:
; CHECK:       while.body.preheader:
; CHECK-NEXT:    [[TMP1:%.*]] = udiv i32 [[TMP0:%.*]], [[COUNT:%.*]]
; CHECK:       vector.ph:
; CHECK:       exit:
; CHECK:         ret void
;
entry:
  %cmp1 = icmp sgt i32 %lim, 0
  br i1 %cmp1, label %loop1.preheader, label %exit

loop1.preheader:                                  ; preds = %entry
  %cmp2 = icmp sgt i32 %count, 0
  %cmp4 = icmp slt i32 %count, 8
  br label %loop1.body

loop1.body:                                       ; preds = %loop1.inc, %loop1.preheader
  %outer_i = phi i32 [ 0, %loop1.preheader ], [ %outer_i.1, %loop1.inc ]
  %inx.1 = phi i32 [ 0, %loop1.preheader ], [ %inx.2, %loop1.inc ]
  br i1 %cmp2, label %while.cond.preheader, label %loop1.inc

while.cond.preheader:                             ; preds = %loop1.body
  br i1 %cmp4, label %while.body, label %while.end

while.body:                                       ; preds = %while.body, %while.cond.preheader
  %tmp = phi i32 [ %add3, %while.body ], [ %count, %while.cond.preheader ]
  %result.1 = phi i32 [ %add, %while.body ], [ %val, %while.cond.preheader ]
  %shr = ashr i32 %val, %tmp
  %add = add nsw i32 %shr, %result.1
  %add3 = add nsw i32 %tmp, %count
  %cmp3 = icmp slt i32 %add3, 8
  br i1 %cmp3, label %while.body, label %while.end

while.end:                                        ; preds = %while.body, %while.cond.preheader
  %result.0.lcssa = phi i32 [ %val, %while.cond.preheader ], [ %add, %while.body ]
  %conv = trunc i32 %result.0.lcssa to i8
  %inc = add nsw i32 %inx.1, 1
  %idxprom = sext i32 %inx.1 to i64
  %arrayidx = getelementptr inbounds i8, i8* %ptr, i64 %idxprom
  store i8 %conv, i8* %arrayidx, align 1
  br label %loop1.inc

loop1.inc:                                        ; preds = %while.end, %loop1.body
  %inx.2 = phi i32 [ %inc, %while.end ], [ %inx.1, %loop1.body ]
  %outer_i.1 = add nuw nsw i32 %outer_i, 1
  %exitcond = icmp eq i32 %outer_i.1, %lim
  br i1 %exitcond, label %exit, label %loop1.body

exit:                                             ; preds = %loop1.inc, %entry
  ret void
}

; These next tests are all based on the following source code, with slight
; variations on the calculation of 'incr' (all of which are loop-invariant
; divisions, but only some of which can be safely hoisted):
;
; uint32_t foo(uint32_t *ptr, uint32_t start1, uint32_t start2) {
;   uint32_t counter1, counter2;
;   uint32_t val = start1;
;   for (counter1 = 1; counter1 < 100; ++counter1) {
;     uint32_t index = 0;
;     val += ptr[index];
;     for (counter2 = start2; counter2 < 10; ++counter2) {
;       // Division is loop invariant, and denominator is guaranteed non-zero:
;       // Safe to hoist it out of the inner loop.
;       uint32_t incr = 16 / counter1;
;       index += incr;
;       val += ptr[index];
;     }
;   }
;   return val;
; }

; This version is as written above, where 'incr' is '16/counter1', and it is
; guaranted that 'counter1' is always non-zero.  So it is safe to hoist the
; division from the inner loop to the preheader.
;
; Verify that the 'udiv' is hoisted to the preheader, and is not in the loop body.
define i32 @NonZeroDivHoist(i32* nocapture readonly %ptr, i32 %start1, i32 %start2) {
; INDVARCHECK-LABEL: @NonZeroDivHoist(
; INDVARCHECK-NEXT:  entry:
; INDVARCHECK:       for.body3.lr.ph:
; INDVARCHECK-NEXT:    [[TMP0:%.*]] = udiv i64 16, [[INDVARS_IV:%.*]]
; INDVARCHECK-NEXT:    br label [[FOR_BODY3:%.*]]
; INDVARCHECK:       for.body3:
; INDVARCHECK-NOT:     udiv
; INDVARCHECK:       for.end10:
;
entry:
  br label %for.cond

for.cond:                                         ; preds = %for.end, %entry
  %val.0 = phi i32 [ %start1, %entry ], [ %val.1.lcssa, %for.end ]
  %counter1.0 = phi i32 [ 1, %entry ], [ %inc9, %for.end ]
  %cmp = icmp ult i32 %counter1.0, 100
  br i1 %cmp, label %for.body, label %for.end10

for.body:                                         ; preds = %for.cond
  %tmp = load i32, i32* %ptr, align 4
  %add = add i32 %tmp, %val.0
  %cmp224 = icmp ult i32 %start2, 10
  br i1 %cmp224, label %for.body3.lr.ph, label %for.end

for.body3.lr.ph:                                  ; preds = %for.body
  br label %for.body3

for.body3:                                        ; preds = %for.body3, %for.body3.lr.ph
  %index.027 = phi i32 [ 0, %for.body3.lr.ph ], [ %add4, %for.body3 ]
  %val.126 = phi i32 [ %add, %for.body3.lr.ph ], [ %add7, %for.body3 ]
  %counter2.025 = phi i32 [ %start2, %for.body3.lr.ph ], [ %inc, %for.body3 ]
  %div = udiv i32 16, %counter1.0
  %add4 = add i32 %div, %index.027
  %idxprom5 = zext i32 %add4 to i64
  %arrayidx6 = getelementptr inbounds i32, i32* %ptr, i64 %idxprom5
  %tmp1 = load i32, i32* %arrayidx6, align 4
  %add7 = add i32 %tmp1, %val.126
  %inc = add i32 %counter2.025, 1
  %cmp2 = icmp ult i32 %inc, 10
  br i1 %cmp2, label %for.body3, label %for.cond1.for.end_crit_edge

for.cond1.for.end_crit_edge:                      ; preds = %for.body3
  %split = phi i32 [ %add7, %for.body3 ]
  br label %for.end

for.end:                                          ; preds = %for.cond1.for.end_crit_edge, %for.body
  %val.1.lcssa = phi i32 [ %split, %for.cond1.for.end_crit_edge ], [ %add, %for.body ]
  %inc9 = add i32 %counter1.0, 1
  br label %for.cond

for.end10:                                        ; preds = %for.cond
  %val.0.lcssa = phi i32 [ %val.0, %for.cond ]
  ret i32 %val.0.lcssa
}

; This version is identical to the above 'NonZeroDivHoist' case, except the
; outer ('counter1') loop starts at the unknown value of 'start1' rather than 1,
; and so it is illegal to hoist the division because if 'start1' is 0, hoisting
; it would incorrectly cause a divide-by-zero trap.
;
; Verify that the 'udiv' is not hoisted to the preheader, and it remains in the
; loop body.
define i32 @ZeroDivNoHoist(i32* nocapture readonly %ptr, i32 %start1, i32 %start2) {
; INDVARCHECK-LABEL: @ZeroDivNoHoist(
; INDVARCHECK-NEXT:  entry:
; INDVARCHECK-NOT:     udiv
; INDVARCHECK:       for.body3:
; INDVARCHECK:         [[TMP1:%.*]] = udiv i64 16, [[INDVARS_IV:%.*]]
; INDVARCHECK:       for.cond1.for.end_crit_edge:
;
entry:
  br label %for.cond

for.cond:                                         ; preds = %for.end, %entry
  %val.0 = phi i32 [ %start1, %entry ], [ %val.1.lcssa, %for.end ]
  %counter1.0 = phi i32 [ %start1, %entry ], [ %inc9, %for.end ]
  %cmp = icmp ult i32 %counter1.0, 100
  br i1 %cmp, label %for.body, label %for.end10

for.body:                                         ; preds = %for.cond
  %tmp = load i32, i32* %ptr, align 4
  %add = add i32 %tmp, %val.0
  %cmp224 = icmp ult i32 %start2, 10
  br i1 %cmp224, label %for.body3.lr.ph, label %for.end

for.body3.lr.ph:                                  ; preds = %for.body
  br label %for.body3

for.body3:                                        ; preds = %for.body3, %for.body3.lr.ph
  %index.027 = phi i32 [ 0, %for.body3.lr.ph ], [ %add4, %for.body3 ]
  %val.126 = phi i32 [ %add, %for.body3.lr.ph ], [ %add7, %for.body3 ]
  %counter2.025 = phi i32 [ %start2, %for.body3.lr.ph ], [ %inc, %for.body3 ]
  %div = udiv i32 16, %counter1.0
  %add4 = add i32 %div, %index.027
  %idxprom5 = zext i32 %add4 to i64
  %arrayidx6 = getelementptr inbounds i32, i32* %ptr, i64 %idxprom5
  %tmp1 = load i32, i32* %arrayidx6, align 4
  %add7 = add i32 %tmp1, %val.126
  %inc = add i32 %counter2.025, 1
  %cmp2 = icmp ult i32 %inc, 10
  br i1 %cmp2, label %for.body3, label %for.cond1.for.end_crit_edge

for.cond1.for.end_crit_edge:                      ; preds = %for.body3
  %split = phi i32 [ %add7, %for.body3 ]
  br label %for.end

for.end:                                          ; preds = %for.cond1.for.end_crit_edge, %for.body
  %val.1.lcssa = phi i32 [ %split, %for.cond1.for.end_crit_edge ], [ %add, %for.body ]
  %inc9 = add i32 %counter1.0, 1
  br label %for.cond

for.end10:                                        ; preds = %for.cond
  %val.0.lcssa = phi i32 [ %val.0, %for.cond ]
  ret i32 %val.0.lcssa
}

; This version is has a clearly safe division by a non-zero constant (16).  The
; division is transformed to a logical-shift-right of 4, and it is safely
; hoisted.
;
; Verify that the division-operation is hoisted, and that it appears as a
; right-shift ('lshr') rather than an explicit division.
define i32 @DivBy16Hoist(i32* nocapture readonly %ptr, i32 %start1, i32 %start2) {
; INDVARCHECK-LABEL: @DivBy16Hoist(
; INDVARCHECK-NEXT:  entry:
; INDVARCHECK:       for.cond:
; INDVARCHECK:         [[TMP1:%.*]] = lshr i64 [[INDVARS_IV:%.*]], 4
; INDVARCHECK:       for.body:
; INDVARCHECK-NOT:     lshr
; INDVARCHECK-NOT:     udiv
; INDVARCHECK:       for.end10:
;
entry:
  br label %for.cond

for.cond:                                         ; preds = %for.end, %entry
  %val.0 = phi i32 [ %start1, %entry ], [ %val.1.lcssa, %for.end ]
  %counter1.0 = phi i32 [ %start1, %entry ], [ %inc9, %for.end ]
  %cmp = icmp ult i32 %counter1.0, 100
  br i1 %cmp, label %for.body, label %for.end10

for.body:                                         ; preds = %for.cond
  %tmp = load i32, i32* %ptr, align 4
  %add = add i32 %tmp, %val.0
  %cmp224 = icmp ult i32 %start2, 10
  br i1 %cmp224, label %for.body3.lr.ph, label %for.end

for.body3.lr.ph:                                  ; preds = %for.body
  br label %for.body3

for.body3:                                        ; preds = %for.body3, %for.body3.lr.ph
  %index.027 = phi i32 [ 0, %for.body3.lr.ph ], [ %add4, %for.body3 ]
  %val.126 = phi i32 [ %add, %for.body3.lr.ph ], [ %add7, %for.body3 ]
  %counter2.025 = phi i32 [ %start2, %for.body3.lr.ph ], [ %inc, %for.body3 ]
  %div = udiv i32 %counter1.0, 16
  %add4 = add i32 %div, %index.027
  %idxprom5 = zext i32 %add4 to i64
  %arrayidx6 = getelementptr inbounds i32, i32* %ptr, i64 %idxprom5
  %tmp1 = load i32, i32* %arrayidx6, align 4
  %add7 = add i32 %tmp1, %val.126
  %inc = add i32 %counter2.025, 1
  %cmp2 = icmp ult i32 %inc, 10
  br i1 %cmp2, label %for.body3, label %for.cond1.for.end_crit_edge

for.cond1.for.end_crit_edge:                      ; preds = %for.body3
  %split = phi i32 [ %add7, %for.body3 ]
  br label %for.end

for.end:                                          ; preds = %for.cond1.for.end_crit_edge, %for.body
  %val.1.lcssa = phi i32 [ %split, %for.cond1.for.end_crit_edge ], [ %add, %for.body ]
  %inc9 = add i32 %counter1.0, 1
  br label %for.cond

for.end10:                                        ; preds = %for.cond
  %val.0.lcssa = phi i32 [ %val.0, %for.cond ]
  ret i32 %val.0.lcssa
}

; This version is has a clearly safe division by a non-zero constant (17).  The
; division is safely hoisted, as it was in the 'DivBy16Hoist' verison, but here
; it remains a division, rather than being transformed to a right-shift.
;
; Verify that the division-operation is hoisted.
define i32 @DivBy17Hoist(i32* nocapture readonly %ptr, i32 %start1, i32 %start2) {
; INDVARCHECK-LABEL: @DivBy17Hoist(
; INDVARCHECK-NEXT:  entry:
; INDVARCHECK:       for.cond:
; INDVARCHECK:         [[TMP1:%.*]] = udiv i64 [[INDVARS_IV:%.*]], 17
; INDVARCHECK:       for.body:
; INDVARCHECK-NOT:     udiv
; INDVARCHECK:       for.end10:
;
entry:
  br label %for.cond

for.cond:                                         ; preds = %for.end, %entry
  %val.0 = phi i32 [ %start1, %entry ], [ %val.1.lcssa, %for.end ]
  %counter1.0 = phi i32 [ %start1, %entry ], [ %inc9, %for.end ]
  %cmp = icmp ult i32 %counter1.0, 100
  br i1 %cmp, label %for.body, label %for.end10

for.body:                                         ; preds = %for.cond
  %tmp = load i32, i32* %ptr, align 4
  %add = add i32 %tmp, %val.0
  %cmp224 = icmp ult i32 %start2, 10
  br i1 %cmp224, label %for.body3.lr.ph, label %for.end

for.body3.lr.ph:                                  ; preds = %for.body
  br label %for.body3

for.body3:                                        ; preds = %for.body3, %for.body3.lr.ph
  %index.027 = phi i32 [ 0, %for.body3.lr.ph ], [ %add4, %for.body3 ]
  %val.126 = phi i32 [ %add, %for.body3.lr.ph ], [ %add7, %for.body3 ]
  %counter2.025 = phi i32 [ %start2, %for.body3.lr.ph ], [ %inc, %for.body3 ]
  %div = udiv i32 %counter1.0, 17
  %add4 = add i32 %div, %index.027
  %idxprom5 = zext i32 %add4 to i64
  %arrayidx6 = getelementptr inbounds i32, i32* %ptr, i64 %idxprom5
  %tmp1 = load i32, i32* %arrayidx6, align 4
  %add7 = add i32 %tmp1, %val.126
  %inc = add i32 %counter2.025, 1
  %cmp2 = icmp ult i32 %inc, 10
  br i1 %cmp2, label %for.body3, label %for.cond1.for.end_crit_edge

for.cond1.for.end_crit_edge:                      ; preds = %for.body3
  %split = phi i32 [ %add7, %for.body3 ]
  br label %for.end

for.end:                                          ; preds = %for.cond1.for.end_crit_edge, %for.body
  %val.1.lcssa = phi i32 [ %split, %for.cond1.for.end_crit_edge ], [ %add, %for.body ]
  %inc9 = add i32 %counter1.0, 1
  br label %for.cond

for.end10:                                        ; preds = %for.cond
  %val.0.lcssa = phi i32 [ %val.0, %for.cond ]
  ret i32 %val.0.lcssa
}