1 ; RUN: opt < %s -loop-vectorize -force-vector-width=4 -dce -instcombine -S | FileCheck %s
3 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
5 ; tests skipping iterations within a VF through break/continue/gotos.
7 ; The main difficulty in vectorizing these loops in test1,test2 and test3 is
8 ; safely speculating that the widened load of A[i] should not fault if the
9 ; scalarized loop does not fault. For example, the
10 ; original load in the scalar loop may not fault, but the last iteration of the
11 ; vectorized load can fault (if it crosses a page boudary for example).
12 ; This last vector iteration is where *one* of the
13 ; scalar iterations lead to the early exit.
15 ; int test(int *A, int Length) {
16 ; for (int i = 0; i < Length; i++) {
17 ; if (A[i] > 10.0) goto end;
24 ; CHECK-NOT: <4 x i32>
25 define i32 @test1(i32* nocapture %A, i32 %Length) {
27 %cmp8 = icmp sgt i32 %Length, 0
28 br i1 %cmp8, label %for.body.preheader, label %end
30 for.body.preheader: ; preds = %entry
33 for.body: ; preds = %for.body.preheader, %if.else
34 %indvars.iv = phi i64 [ %indvars.iv.next, %if.else ], [ 0, %for.body.preheader ]
35 %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
36 %0 = load i32, i32* %arrayidx, align 4, !tbaa !15
37 %cmp1 = icmp sgt i32 %0, 10
38 br i1 %cmp1, label %end.loopexit, label %if.else
40 if.else: ; preds = %for.body
41 store i32 0, i32* %arrayidx, align 4, !tbaa !15
42 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
43 %1 = trunc i64 %indvars.iv.next to i32
44 %cmp = icmp slt i32 %1, %Length
45 br i1 %cmp, label %for.body, label %end.loopexit
47 end.loopexit: ; preds = %if.else, %for.body
50 end: ; preds = %end.loopexit, %entry
54 ; We don't use anything from within the loop at the early exit path
55 ; so we do not need to know which iteration caused the early exit path.
56 ; bool test2(int *A, int Length, int K) {
57 ; for (int i = 0; i < Length; i++) {
58 ; if (A[i] == K) return true;
62 ; TODO: Today we do not vectorize this, but we could teach the vectorizer, once
63 ; the hard part of proving/speculating A[i:VF - 1] loads does not fault is handled by the
67 ; CHECK-NOT: <4 x i32>
68 define i32 @test2(i32* nocapture %A, i32 %Length, i32 %K) {
70 %cmp8 = icmp sgt i32 %Length, 0
71 br i1 %cmp8, label %for.body.preheader, label %end
73 for.body.preheader: ; preds = %entry
76 for.body: ; preds = %for.body.preheader, %if.else
77 %indvars.iv = phi i64 [ %indvars.iv.next, %if.else ], [ 0, %for.body.preheader ]
78 %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
79 %ld = load i32, i32* %arrayidx, align 4
80 %cmp1 = icmp eq i32 %ld, %K
81 br i1 %cmp1, label %end.loopexit, label %if.else
83 if.else: ; preds = %for.body
84 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
85 %trunc = trunc i64 %indvars.iv.next to i32
86 %cmp = icmp slt i32 %trunc, %Length
87 br i1 %cmp, label %for.body, label %end.loopexit
89 end.loopexit: ; preds = %if.else, %for.body
90 %result.lcssa = phi i32 [ 1, %for.body ], [ 0, %if.else ]
93 end: ; preds = %end.loopexit, %entry
94 %result = phi i32 [ %result.lcssa, %end.loopexit ], [ 0, %entry ]
98 ; We use the IV in the early exit
99 ; so we need to know which iteration caused the early exit path.
100 ; int test3(int *A, int Length, int K) {
101 ; for (int i = 0; i < Length; i++) {
102 ; if (A[i] == K) return i;
106 ; TODO: Today we do not vectorize this, but we could teach the vectorizer (once
107 ; we handle the speculation safety of the widened load).
108 ; CHECK-LABEL: test3(
109 ; CHECK-NOT: <4 x i32>
110 define i32 @test3(i32* nocapture %A, i32 %Length, i32 %K) {
112 %cmp8 = icmp sgt i32 %Length, 0
113 br i1 %cmp8, label %for.body.preheader, label %end
115 for.body.preheader: ; preds = %entry
118 for.body: ; preds = %for.body.preheader, %if.else
119 %indvars.iv = phi i64 [ %indvars.iv.next, %if.else ], [ 0, %for.body.preheader ]
120 %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
121 %ld = load i32, i32* %arrayidx, align 4
122 %cmp1 = icmp eq i32 %ld, %K
123 br i1 %cmp1, label %end.loopexit, label %if.else
125 if.else: ; preds = %for.body
126 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
127 %trunc = trunc i64 %indvars.iv.next to i32
128 %cmp = icmp slt i32 %trunc, %Length
129 br i1 %cmp, label %for.body, label %end.loopexit
131 end.loopexit: ; preds = %if.else, %for.body
132 %result.lcssa = phi i64 [ %indvars.iv, %for.body ], [ -1, %if.else ]
133 %res.trunc = trunc i64 %result.lcssa to i32
136 end: ; preds = %end.loopexit, %entry
137 %result = phi i32 [ %res.trunc, %end.loopexit ], [ -1, %entry ]
141 ; bool test4(int *A, int Length, int K, int J) {
142 ; for (int i = 0; i < Length; i++) {
143 ; if (A[i] == K) continue;
147 ; For this test, we vectorize and generate predicated stores to A[i].
148 ; CHECK-LABEL: test4(
150 define void @test4(i32* nocapture %A, i32 %Length, i32 %K, i32 %J) {
152 %cmp8 = icmp sgt i32 %Length, 0
153 br i1 %cmp8, label %for.body.preheader, label %end.loopexit
155 for.body.preheader: ; preds = %entry
158 for.body: ; preds = %for.body.preheader, %if.else
159 %indvars.iv = phi i64 [ %indvars.iv.next, %latch ], [ 0, %for.body.preheader ]
160 %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
161 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
162 %trunc = trunc i64 %indvars.iv.next to i32
163 %ld = load i32, i32* %arrayidx, align 4
164 %cmp1 = icmp eq i32 %ld, %K
165 br i1 %cmp1, label %latch, label %if.else
168 store i32 %J, i32* %arrayidx, align 4
171 latch: ; preds = %for.body
172 %cmp = icmp slt i32 %trunc, %Length
173 br i1 %cmp, label %for.body, label %end.loopexit
175 end.loopexit: ; preds = %if.else, %for.body
178 !15 = !{!16, !16, i64 0}
179 !16 = !{!"int", !17, i64 0}
180 !17 = !{!"omnipotent char", !18, i64 0}
181 !18 = !{!"Simple C/C++ TBAA"}