blob: 99160cf4b67a67dc160a0cf0ffa0454d16d2eeea [file] [log] [blame]
Anna Thomas2d33ce72018-08-21 13:02:09 +00001; RUN: opt < %s -loop-vectorize -force-vector-width=4 -dce -instcombine -S | FileCheck %s
2
3target 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"
4
5; tests skipping iterations within a VF through break/continue/gotos.
6
7; int test(int *A, int Length) {
8; for (int i = 0; i < Length; i++) {
9; if (A[i] > 10.0) goto end;
10; A[i] = 0;
11; }
12; end:
13; return 0;
14; }
15; CHECK-LABEL: test1(
16; CHECK-NOT: <4 x i32>
17define i32 @test1(i32* nocapture %A, i32 %Length) {
18entry:
19 %cmp8 = icmp sgt i32 %Length, 0
20 br i1 %cmp8, label %for.body.preheader, label %end
21
22for.body.preheader: ; preds = %entry
23 br label %for.body
24
25for.body: ; preds = %for.body.preheader, %if.else
26 %indvars.iv = phi i64 [ %indvars.iv.next, %if.else ], [ 0, %for.body.preheader ]
27 %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
28 %0 = load i32, i32* %arrayidx, align 4, !tbaa !15
29 %cmp1 = icmp sgt i32 %0, 10
30 br i1 %cmp1, label %end.loopexit, label %if.else
31
32if.else: ; preds = %for.body
33 store i32 0, i32* %arrayidx, align 4, !tbaa !15
34 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
35 %1 = trunc i64 %indvars.iv.next to i32
36 %cmp = icmp slt i32 %1, %Length
37 br i1 %cmp, label %for.body, label %end.loopexit
38
39end.loopexit: ; preds = %if.else, %for.body
40 br label %end
41
42end: ; preds = %end.loopexit, %entry
43 ret i32 0
44}
45
46; We don't use anything from within the loop at the early exit path
47; so we do not need to know which iteration caused the early exit path.
48; bool test2(int *A, int Length, int K) {
49; for (int i = 0; i < Length; i++) {
50; if (A[i] == K) return true;
51; }
52; return false;
53; }
54; TODO: Today we do not vectorize this, but we could teach the vectorizer.
55; CHECK-LABEL: test2(
56; CHECK-NOT: <4 x i32>
57define i32 @test2(i32* nocapture %A, i32 %Length, i32 %K) {
58entry:
59 %cmp8 = icmp sgt i32 %Length, 0
60 br i1 %cmp8, label %for.body.preheader, label %end
61
62for.body.preheader: ; preds = %entry
63 br label %for.body
64
65for.body: ; preds = %for.body.preheader, %if.else
66 %indvars.iv = phi i64 [ %indvars.iv.next, %if.else ], [ 0, %for.body.preheader ]
67 %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
68 %ld = load i32, i32* %arrayidx, align 4
69 %cmp1 = icmp eq i32 %ld, %K
70 br i1 %cmp1, label %end.loopexit, label %if.else
71
72if.else: ; preds = %for.body
73 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
74 %trunc = trunc i64 %indvars.iv.next to i32
75 %cmp = icmp slt i32 %trunc, %Length
76 br i1 %cmp, label %for.body, label %end.loopexit
77
78end.loopexit: ; preds = %if.else, %for.body
79 %result.lcssa = phi i32 [ 1, %for.body ], [ 0, %if.else ]
80 br label %end
81
82end: ; preds = %end.loopexit, %entry
83 %result = phi i32 [ %result.lcssa, %end.loopexit ], [ 0, %entry ]
84 ret i32 %result
85}
86
87; We use the IV in the early exit
88; so we need to know which iteration caused the early exit path.
89; int test3(int *A, int Length, int K) {
90; for (int i = 0; i < Length; i++) {
91; if (A[i] == K) return i;
92; }
93; return -1;
94; }
95; TODO: Today we do not vectorize this, but we could teach the vectorizer.
96; CHECK-LABEL: test3(
97; CHECK-NOT: <4 x i32>
98define i32 @test3(i32* nocapture %A, i32 %Length, i32 %K) {
99entry:
100 %cmp8 = icmp sgt i32 %Length, 0
101 br i1 %cmp8, label %for.body.preheader, label %end
102
103for.body.preheader: ; preds = %entry
104 br label %for.body
105
106for.body: ; preds = %for.body.preheader, %if.else
107 %indvars.iv = phi i64 [ %indvars.iv.next, %if.else ], [ 0, %for.body.preheader ]
108 %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
109 %ld = load i32, i32* %arrayidx, align 4
110 %cmp1 = icmp eq i32 %ld, %K
111 br i1 %cmp1, label %end.loopexit, label %if.else
112
113if.else: ; preds = %for.body
114 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
115 %trunc = trunc i64 %indvars.iv.next to i32
116 %cmp = icmp slt i32 %trunc, %Length
117 br i1 %cmp, label %for.body, label %end.loopexit
118
119end.loopexit: ; preds = %if.else, %for.body
120 %result.lcssa = phi i64 [ %indvars.iv, %for.body ], [ -1, %if.else ]
121 %res.trunc = trunc i64 %result.lcssa to i32
122 br label %end
123
124end: ; preds = %end.loopexit, %entry
125 %result = phi i32 [ %res.trunc, %end.loopexit ], [ -1, %entry ]
126 ret i32 %result
127}
128
129; bool test4(int *A, int Length, int K, int J) {
130; for (int i = 0; i < Length; i++) {
131; if (A[i] == K) continue;
132; A[i] = J;
133; }
134; }
135; For this test, we vectorize and generate predicated stores to A[i].
136; CHECK-LABEL: test4(
137; CHECK: <4 x i32>
138define void @test4(i32* nocapture %A, i32 %Length, i32 %K, i32 %J) {
139entry:
140 %cmp8 = icmp sgt i32 %Length, 0
141 br i1 %cmp8, label %for.body.preheader, label %end.loopexit
142
143for.body.preheader: ; preds = %entry
144 br label %for.body
145
146for.body: ; preds = %for.body.preheader, %if.else
147 %indvars.iv = phi i64 [ %indvars.iv.next, %latch ], [ 0, %for.body.preheader ]
148 %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
149 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
150 %trunc = trunc i64 %indvars.iv.next to i32
151 %ld = load i32, i32* %arrayidx, align 4
152 %cmp1 = icmp eq i32 %ld, %K
153 br i1 %cmp1, label %latch, label %if.else
154
155if.else:
156 store i32 %J, i32* %arrayidx, align 4
157 br label %latch
158
159latch: ; preds = %for.body
160 %cmp = icmp slt i32 %trunc, %Length
161 br i1 %cmp, label %for.body, label %end.loopexit
162
163end.loopexit: ; preds = %if.else, %for.body
164 ret void
165}
166!15 = !{!16, !16, i64 0}
167!16 = !{!"int", !17, i64 0}
168!17 = !{!"omnipotent char", !18, i64 0}
169!18 = !{!"Simple C/C++ TBAA"}