1 ; RUN: opt < %s -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, i32* @int_inc, align 4
18 ; CHECK: [[DOTSPLATINSERT:%.*]] = insertelement <8 x i32> undef, i32 %init, i32 0
19 ; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT]], <8 x i32> undef, <8 x i32> zeroinitializer
20 ; CHECK-NEXT: [[DOTSPLATINSERT2:%.*]] = insertelement <8 x i32> undef, i32 [[TMP0]], i32 0
21 ; CHECK-NEXT: [[DOTSPLAT3:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT2]], <8 x i32> undef, <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> undef, i32 [[TMP7]], i32 0
26 ; CHECK-NEXT: [[DOTSPLAT6:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT5]], <8 x i32> undef, <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, i32* [[A:%.*]], i64 [[TMP8]]
33 ; CHECK-NEXT: [[TMP10:%.*]] = getelementptr inbounds i32, i32* [[TMP9]], i32 0
34 ; CHECK-NEXT: [[TMP11:%.*]] = bitcast i32* [[TMP10]] to <8 x i32>*
35 ; CHECK-NEXT: store <8 x i32> %vec.ind, <8 x i32>* [[TMP11]], align 4
36 ; CHECK: %index.next = add i64 %index, 8
37 ; CHECK-NEXT: %vec.ind.next = add <8 x i32> %vec.ind, [[DOTSPLAT6]]
38 ; CHECK: br i1 {{.*}}, label %middle.block, label %vector.body
40 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
43 @int_inc = common global i32 0, align 4
45 define i32 @induction_with_global(i32 %init, i32* noalias nocapture %A, i32 %N) {
47 %cmp4 = icmp sgt i32 %N, 0
48 br i1 %cmp4, label %for.body.lr.ph, label %for.end
50 for.body.lr.ph: ; preds = %entry
51 %0 = load i32, i32* @int_inc, align 4
55 for.body: ; preds = %for.body, %for.body.lr.ph
56 %indvars.iv = phi i64 [ 0, %for.body.lr.ph ], [ %indvars.iv.next, %for.body ]
57 %x.05 = phi i32 [ %init, %for.body.lr.ph ], [ %add, %for.body ]
58 %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
59 store i32 %x.05, i32* %arrayidx, align 4
60 %add = add nsw i32 %0, %x.05
61 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
62 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
63 %exitcond = icmp eq i32 %lftr.wideiv, %N
64 br i1 %exitcond, label %for.end.loopexit, label %for.body
66 for.end.loopexit: ; preds = %for.body
67 %2 = add i32 %1, %init
70 for.end: ; preds = %for.end.loopexit, %entry
71 %x.0.lcssa = phi i32 [ %init, %entry ], [ %2, %for.end.loopexit ]
76 ;int induction_with_loop_inv(int init, int *restrict A, int N, int M) {
78 ; for (int j = 0; j < M; j++) {
79 ; for (int i=0; i<N; i++){
81 ; x += j; // induction step is a loop invariant variable
87 ; CHECK-LABEL: @induction_with_loop_inv(
89 ; CHECK: [[DOTSPLATINSERT:%.*]] = insertelement <8 x i32> undef, i32 %x.011, i32 0
90 ; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT]], <8 x i32> undef, <8 x i32> zeroinitializer
91 ; CHECK-NEXT: [[DOTSPLATINSERT2:%.*]] = insertelement <8 x i32> undef, i32 %j.012, i32 0
92 ; CHECK-NEXT: [[DOTSPLAT3:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT2]], <8 x i32> undef, <8 x i32> zeroinitializer
93 ; CHECK-NEXT: [[TMP4:%.*]] = mul <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>, [[DOTSPLAT3]]
94 ; CHECK-NEXT: [[INDUCTION4:%.*]] = add <8 x i32> [[DOTSPLAT]], [[TMP4]]
95 ; CHECK-NEXT: [[TMP5:%.*]] = mul i32 %j.012, 8
96 ; CHECK-NEXT: [[DOTSPLATINSERT5:%.*]] = insertelement <8 x i32> undef, i32 [[TMP5]], i32 0
97 ; CHECK-NEXT: [[DOTSPLAT6:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT5]], <8 x i32> undef, <8 x i32> zeroinitializer
98 ; CHECK-NEXT: br label %vector.body
100 ; CHECK-NEXT: %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
101 ; CHECK-NEXT: %vec.ind = phi <8 x i32> [ [[INDUCTION4]], %vector.ph ], [ %vec.ind.next, %vector.body ]
102 ; CHECK: [[TMP6:%.*]] = add i64 %index, 0
103 ; CHECK-NEXT: [[TMP7:%.*]] = getelementptr inbounds i32, i32* [[A:%.*]], i64 [[TMP6]]
104 ; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds i32, i32* [[TMP7]], i32 0
105 ; CHECK-NEXT: [[TMP9:%.*]] = bitcast i32* [[TMP8]] to <8 x i32>*
106 ; CHECK-NEXT: store <8 x i32> %vec.ind, <8 x i32>* [[TMP9]], align 4
107 ; CHECK: %index.next = add i64 %index, 8
108 ; CHECK-NEXT: %vec.ind.next = add <8 x i32> %vec.ind, [[DOTSPLAT6]]
109 ; CHECK: br i1 {{.*}}, label %middle.block, label %vector.body
111 define i32 @induction_with_loop_inv(i32 %init, i32* noalias nocapture %A, i32 %N, i32 %M) {
113 %cmp10 = icmp sgt i32 %M, 0
114 br i1 %cmp10, label %for.cond1.preheader.lr.ph, label %for.end6
116 for.cond1.preheader.lr.ph: ; preds = %entry
117 %cmp27 = icmp sgt i32 %N, 0
118 br label %for.cond1.preheader
120 for.cond1.preheader: ; preds = %for.inc4, %for.cond1.preheader.lr.ph
121 %indvars.iv15 = phi i32 [ 0, %for.cond1.preheader.lr.ph ], [ %indvars.iv.next16, %for.inc4 ]
122 %j.012 = phi i32 [ 0, %for.cond1.preheader.lr.ph ], [ %inc5, %for.inc4 ]
123 %x.011 = phi i32 [ %init, %for.cond1.preheader.lr.ph ], [ %x.1.lcssa, %for.inc4 ]
124 br i1 %cmp27, label %for.body3.preheader, label %for.inc4
126 for.body3.preheader: ; preds = %for.cond1.preheader
129 for.body3: ; preds = %for.body3.preheader, %for.body3
130 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body3 ], [ 0, %for.body3.preheader ]
131 %x.18 = phi i32 [ %add, %for.body3 ], [ %x.011, %for.body3.preheader ]
132 %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
133 store i32 %x.18, i32* %arrayidx, align 4
134 %add = add nsw i32 %x.18, %j.012
135 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
136 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
137 %exitcond = icmp eq i32 %lftr.wideiv, %N
138 br i1 %exitcond, label %for.inc4.loopexit, label %for.body3
140 for.inc4.loopexit: ; preds = %for.body3
141 %0 = add i32 %x.011, %indvars.iv15
144 for.inc4: ; preds = %for.inc4.loopexit, %for.cond1.preheader
145 %x.1.lcssa = phi i32 [ %x.011, %for.cond1.preheader ], [ %0, %for.inc4.loopexit ]
146 %inc5 = add nuw nsw i32 %j.012, 1
147 %indvars.iv.next16 = add i32 %indvars.iv15, %N
148 %exitcond17 = icmp eq i32 %inc5, %M
149 br i1 %exitcond17, label %for.end6.loopexit, label %for.cond1.preheader
151 for.end6.loopexit: ; preds = %for.inc4
152 %x.1.lcssa.lcssa = phi i32 [ %x.1.lcssa, %for.inc4 ]
155 for.end6: ; preds = %for.end6.loopexit, %entry
156 %x.0.lcssa = phi i32 [ %init, %entry ], [ %x.1.lcssa.lcssa, %for.end6.loopexit ]
161 ; CHECK-LABEL: @non_primary_iv_loop_inv_trunc(
163 ; CHECK: [[TMP3:%.*]] = trunc i64 %step to i32
164 ; CHECK-NEXT: [[DOTSPLATINSERT5:%.*]] = insertelement <8 x i32> undef, i32 [[TMP3]], i32 0
165 ; CHECK-NEXT: [[DOTSPLAT6:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT5]], <8 x i32> undef, <8 x i32> zeroinitializer
166 ; CHECK-NEXT: [[TMP4:%.*]] = mul <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>, [[DOTSPLAT6]]
167 ; CHECK-NEXT: [[INDUCTION7:%.*]] = add <8 x i32> zeroinitializer, [[TMP4]]
168 ; CHECK-NEXT: [[TMP5:%.*]] = mul i32 [[TMP3]], 8
169 ; CHECK-NEXT: [[DOTSPLATINSERT8:%.*]] = insertelement <8 x i32> undef, i32 [[TMP5]], i32 0
170 ; CHECK-NEXT: [[DOTSPLAT9:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT8]], <8 x i32> undef, <8 x i32> zeroinitializer
171 ; CHECK-NEXT: br label %vector.body
172 ; CHECK: vector.body:
173 ; CHECK-NEXT: %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
174 ; CHECK: [[VEC_IND10:%.*]] = phi <8 x i32> [ [[INDUCTION7]], %vector.ph ], [ [[VEC_IND_NEXT11:%.*]], %vector.body ]
175 ; CHECK: [[TMP6:%.*]] = add i64 %index, 0
176 ; CHECK-NEXT: [[TMP7:%.*]] = getelementptr inbounds i32, i32* [[A:%.*]], i64 [[TMP6]]
177 ; CHECK-NEXT: [[TMP8:%.*]] = getelementptr inbounds i32, i32* [[TMP7]], i32 0
178 ; CHECK-NEXT: [[TMP9:%.*]] = bitcast i32* [[TMP8]] to <8 x i32>*
179 ; CHECK-NEXT: store <8 x i32> [[VEC_IND10]], <8 x i32>* [[TMP9]], align 4
180 ; CHECK-NEXT: %index.next = add i64 %index, 8
181 ; CHECK: [[VEC_IND_NEXT11]] = add <8 x i32> [[VEC_IND10]], [[DOTSPLAT9]]
182 ; CHECK: br i1 {{.*}}, label %middle.block, label %vector.body
184 define void @non_primary_iv_loop_inv_trunc(i32* %a, i64 %n, i64 %step) {
189 %i = phi i64 [ %i.next, %for.body ], [ 0, %entry ]
190 %j = phi i64 [ %j.next, %for.body ], [ 0, %entry ]
191 %tmp0 = getelementptr inbounds i32, i32* %a, i64 %i
192 %tmp1 = trunc i64 %j to i32
193 store i32 %tmp1, i32* %tmp0, align 4
194 %i.next = add nuw nsw i64 %i, 1
195 %j.next = add nuw nsw i64 %j, %step
196 %cond = icmp slt i64 %i.next, %n
197 br i1 %cond, label %for.body, label %for.end