1 ; RUN: opt < %s -passes=loop-vectorize -force-vector-interleave=1 -force-vector-width=8 -S | FileCheck %s
5 ;int induction_with_global(int init, int *restrict A, int N) {
7 ; for (int i=0;i<N;i++){
14 ; CHECK-LABEL: @induction_with_global(
15 ; CHECK: for.body.lr.ph:
16 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr @int_inc, align 4
18 ; CHECK: [[DOTSPLATINSERT:%.*]] = insertelement <8 x i32> poison, i32 %init, i64 0
19 ; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT]], <8 x i32> poison, <8 x i32> zeroinitializer
20 ; CHECK-NEXT: [[DOTSPLATINSERT2:%.*]] = insertelement <8 x i32> poison, i32 [[TMP0]], i64 0
21 ; CHECK-NEXT: [[DOTSPLAT3:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT2]], <8 x i32> poison, <8 x i32> zeroinitializer
22 ; CHECK-NEXT: [[TMP6:%.*]] = mul <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>, [[DOTSPLAT3]]
23 ; CHECK-NEXT: [[INDUCTION4:%.*]] = add <8 x i32> [[DOTSPLAT]], [[TMP6]]
24 ; CHECK-NEXT: [[TMP7:%.*]] = mul i32 [[TMP0]], 8
25 ; CHECK-NEXT: [[DOTSPLATINSERT5:%.*]] = insertelement <8 x i32> poison, i32 [[TMP7]], i64 0
26 ; CHECK-NEXT: [[DOTSPLAT6:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT5]], <8 x i32> poison, <8 x i32> zeroinitializer
27 ; CHECK-NEXT: br label %vector.body
29 ; CHECK-NEXT: %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
30 ; CHECK-NEXT: %vec.ind = phi <8 x i32> [ [[INDUCTION4]], %vector.ph ], [ %vec.ind.next, %vector.body ]
31 ; CHECK: [[TMP8:%.*]] = add i64 %index, 0
32 ; CHECK-NEXT: [[TMP9:%.*]] = getelementptr inbounds i32, ptr [[A:%.*]], i64 [[TMP8]]
33 ; CHECK-NEXT: [[TMP10:%.*]] = getelementptr inbounds i32, ptr [[TMP9]], i32 0
34 ; CHECK-NEXT: store <8 x i32> %vec.ind, ptr [[TMP10]], align 4
35 ; CHECK: %index.next = add nuw i64 %index, 8
36 ; CHECK-NEXT: %vec.ind.next = add <8 x i32> %vec.ind, [[DOTSPLAT6]]
37 ; CHECK: br i1 {{.*}}, label %middle.block, label %vector.body
39 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
42 @int_inc = common global i32 0, align 4
44 define i32 @induction_with_global(i32 %init, ptr noalias nocapture %A, i32 %N) {
46 %cmp4 = icmp sgt i32 %N, 0
47 br i1 %cmp4, label %for.body.lr.ph, label %for.end
49 for.body.lr.ph: ; preds = %entry
50 %0 = load i32, ptr @int_inc, align 4
54 for.body: ; preds = %for.body, %for.body.lr.ph
55 %indvars.iv = phi i64 [ 0, %for.body.lr.ph ], [ %indvars.iv.next, %for.body ]
56 %x.05 = phi i32 [ %init, %for.body.lr.ph ], [ %add, %for.body ]
57 %arrayidx = getelementptr inbounds i32, ptr %A, i64 %indvars.iv
58 store i32 %x.05, ptr %arrayidx, align 4
59 %add = add nsw i32 %0, %x.05
60 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
61 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
62 %exitcond = icmp eq i32 %lftr.wideiv, %N
63 br i1 %exitcond, label %for.end.loopexit, label %for.body
65 for.end.loopexit: ; preds = %for.body
66 %2 = add i32 %1, %init
69 for.end: ; preds = %for.end.loopexit, %entry
70 %x.0.lcssa = phi i32 [ %init, %entry ], [ %2, %for.end.loopexit ]
75 ;int induction_with_loop_inv(int init, int *restrict A, int N, int M) {
77 ; for (int j = 0; j < M; j++) {
78 ; for (int i=0; i<N; i++){
80 ; x += j; // induction step is a loop invariant variable
86 ; CHECK-LABEL: @induction_with_loop_inv(
88 ; CHECK: [[DOTSPLATINSERT:%.*]] = insertelement <8 x i32> poison, i32 %x.011, i64 0
89 ; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT]], <8 x i32> poison, <8 x i32> zeroinitializer
90 ; CHECK-NEXT: [[DOTSPLATINSERT2:%.*]] = insertelement <8 x i32> poison, i32 %j.012, i64 0
91 ; CHECK-NEXT: [[DOTSPLAT3:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT2]], <8 x i32> poison, <8 x i32> zeroinitializer
92 ; CHECK-NEXT: [[TMP4:%.*]] = mul <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>, [[DOTSPLAT3]]
93 ; CHECK-NEXT: [[INDUCTION4:%.*]] = add <8 x i32> [[DOTSPLAT]], [[TMP4]]
94 ; CHECK-NEXT: [[TMP5:%.*]] = mul i32 %j.012, 8
95 ; CHECK-NEXT: [[DOTSPLATINSERT5:%.*]] = insertelement <8 x i32> poison, i32 [[TMP5]], i64 0
96 ; CHECK-NEXT: [[DOTSPLAT6:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT5]], <8 x i32> poison, <8 x i32> zeroinitializer
97 ; CHECK-NEXT: br label %vector.body
99 ; CHECK-NEXT: %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
100 ; CHECK-NEXT: %vec.ind = phi <8 x i32> [ [[INDUCTION4]], %vector.ph ], [ %vec.ind.next, %vector.body ]
101 ; CHECK: [[TMP6:%.*]] = add i64 %index, 0
102 ; CHECK-NEXT: [[TMP7:%.*]] = getelementptr inbounds i32, ptr [[A:%.*]], i64 [[TMP6]]
103 ; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds i32, ptr [[TMP7]], i32 0
104 ; CHECK-NEXT: store <8 x i32> %vec.ind, ptr [[TMP8]], align 4
105 ; CHECK: %index.next = add nuw i64 %index, 8
106 ; CHECK-NEXT: %vec.ind.next = add <8 x i32> %vec.ind, [[DOTSPLAT6]]
107 ; CHECK: br i1 {{.*}}, label %middle.block, label %vector.body
109 define i32 @induction_with_loop_inv(i32 %init, ptr noalias nocapture %A, i32 %N, i32 %M) {
111 %cmp10 = icmp sgt i32 %M, 0
112 br i1 %cmp10, label %for.cond1.preheader.lr.ph, label %for.end6
114 for.cond1.preheader.lr.ph: ; preds = %entry
115 %cmp27 = icmp sgt i32 %N, 0
116 br label %for.cond1.preheader
118 for.cond1.preheader: ; preds = %for.inc4, %for.cond1.preheader.lr.ph
119 %indvars.iv15 = phi i32 [ 0, %for.cond1.preheader.lr.ph ], [ %indvars.iv.next16, %for.inc4 ]
120 %j.012 = phi i32 [ 0, %for.cond1.preheader.lr.ph ], [ %inc5, %for.inc4 ]
121 %x.011 = phi i32 [ %init, %for.cond1.preheader.lr.ph ], [ %x.1.lcssa, %for.inc4 ]
122 br i1 %cmp27, label %for.body3.preheader, label %for.inc4
124 for.body3.preheader: ; preds = %for.cond1.preheader
127 for.body3: ; preds = %for.body3.preheader, %for.body3
128 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body3 ], [ 0, %for.body3.preheader ]
129 %x.18 = phi i32 [ %add, %for.body3 ], [ %x.011, %for.body3.preheader ]
130 %arrayidx = getelementptr inbounds i32, ptr %A, i64 %indvars.iv
131 store i32 %x.18, ptr %arrayidx, align 4
132 %add = add nsw i32 %x.18, %j.012
133 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
134 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
135 %exitcond = icmp eq i32 %lftr.wideiv, %N
136 br i1 %exitcond, label %for.inc4.loopexit, label %for.body3
138 for.inc4.loopexit: ; preds = %for.body3
139 %0 = add i32 %x.011, %indvars.iv15
142 for.inc4: ; preds = %for.inc4.loopexit, %for.cond1.preheader
143 %x.1.lcssa = phi i32 [ %x.011, %for.cond1.preheader ], [ %0, %for.inc4.loopexit ]
144 %inc5 = add nuw nsw i32 %j.012, 1
145 %indvars.iv.next16 = add i32 %indvars.iv15, %N
146 %exitcond17 = icmp eq i32 %inc5, %M
147 br i1 %exitcond17, label %for.end6.loopexit, label %for.cond1.preheader
149 for.end6.loopexit: ; preds = %for.inc4
150 %x.1.lcssa.lcssa = phi i32 [ %x.1.lcssa, %for.inc4 ]
153 for.end6: ; preds = %for.end6.loopexit, %entry
154 %x.0.lcssa = phi i32 [ %init, %entry ], [ %x.1.lcssa.lcssa, %for.end6.loopexit ]
159 ; CHECK-LABEL: @non_primary_iv_loop_inv_trunc(
161 ; CHECK: [[TMP3:%.*]] = trunc i64 %step to i32
162 ; CHECK-NEXT: [[DOTSPLATINSERT5:%.*]] = insertelement <8 x i32> poison, i32 [[TMP3]], i64 0
163 ; CHECK-NEXT: [[DOTSPLAT6:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT5]], <8 x i32> poison, <8 x i32> zeroinitializer
164 ; CHECK-NEXT: [[TMP4:%.*]] = mul <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>, [[DOTSPLAT6]]
165 ; CHECK-NEXT: [[INDUCTION7:%.*]] = add <8 x i32> zeroinitializer, [[TMP4]]
166 ; CHECK-NEXT: [[TMP5:%.*]] = mul i32 [[TMP3]], 8
167 ; CHECK-NEXT: [[DOTSPLATINSERT8:%.*]] = insertelement <8 x i32> poison, i32 [[TMP5]], i64 0
168 ; CHECK-NEXT: [[DOTSPLAT9:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT8]], <8 x i32> poison, <8 x i32> zeroinitializer
169 ; CHECK-NEXT: br label %vector.body
170 ; CHECK: vector.body:
171 ; CHECK-NEXT: %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
172 ; CHECK: [[VEC_IND10:%.*]] = phi <8 x i32> [ [[INDUCTION7]], %vector.ph ], [ [[VEC_IND_NEXT11:%.*]], %vector.body ]
173 ; CHECK: [[TMP6:%.*]] = add i64 %index, 0
174 ; CHECK-NEXT: [[TMP7:%.*]] = getelementptr inbounds i32, ptr [[A:%.*]], i64 [[TMP6]]
175 ; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds i32, ptr [[TMP7]], i32 0
176 ; CHECK-NEXT: store <8 x i32> [[VEC_IND10]], ptr [[TMP8]], align 4
177 ; CHECK-NEXT: %index.next = add nuw i64 %index, 8
178 ; CHECK: [[VEC_IND_NEXT11]] = add <8 x i32> [[VEC_IND10]], [[DOTSPLAT9]]
179 ; CHECK: br i1 {{.*}}, label %middle.block, label %vector.body
181 define void @non_primary_iv_loop_inv_trunc(ptr %a, i64 %n, i64 %step) {
186 %i = phi i64 [ %i.next, %for.body ], [ 0, %entry ]
187 %j = phi i64 [ %j.next, %for.body ], [ 0, %entry ]
188 %tmp0 = getelementptr inbounds i32, ptr %a, i64 %i
189 %tmp1 = trunc i64 %j to i32
190 store i32 %tmp1, ptr %tmp0, align 4
191 %i.next = add nuw nsw i64 %i, 1
192 %j.next = add nuw nsw i64 %j, %step
193 %cond = icmp slt i64 %i.next, %n
194 br i1 %cond, label %for.body, label %for.end
200 ; CHECK-LABEL: @iv_no_binary_op_in_descriptor(
202 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
203 ; CHECK: vector.body:
204 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %vector.ph ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
205 ; CHECK-NEXT: [[VEC_IND:%.*]] = phi <8 x i64> [ <i64 0, i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7>, %vector.ph ], [ [[VEC_IND_NEXT:%.*]], [[VECTOR_BODY]] ]
206 ; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[INDEX]], 0
207 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i64, ptr [[DST:%.*]], i64 [[TMP0]]
208 ; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i64, ptr [[TMP1]], i32 0
209 ; CHECK-NEXT: store <8 x i64> [[VEC_IND]], ptr [[TMP2]], align 8
210 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 8
211 ; CHECK-NEXT: [[VEC_IND_NEXT]] = add <8 x i64> [[VEC_IND]], splat (i64 8)
212 ; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1000
213 ; CHECK-NEXT: br i1 [[TMP3]], label %middle.block, label [[VECTOR_BODY]]
215 define void @iv_no_binary_op_in_descriptor(i1 %c, ptr %dst) {
217 br label %loop.header
220 %iv = phi i64 [ 0, %entry ], [ %iv.next.p, %loop.latch ]
221 %gep = getelementptr inbounds i64, ptr %dst, i64 %iv
222 store i64 %iv, ptr %gep, align 8
223 %iv.next = add i64 %iv, 1
227 %iv.next.p = phi i64 [ %iv.next, %loop.header ]
228 %exitcond.not = icmp eq i64 %iv.next.p, 1000
229 br i1 %exitcond.not, label %exit, label %loop.header