1; RUN: opt < %s -loop-vectorize -force-vector-interleave=1 -force-vector-width=8 -S | FileCheck %s
2
3; int int_inc;
4;
5;int induction_with_global(int init, int *restrict A, int N) {
6;  int x = init;
7;  for (int i=0;i<N;i++){
8;    A[i] = x;
9;    x += int_inc;
10;  }
11;  return x;
12;}
13
14; CHECK-LABEL: @induction_with_global(
15; CHECK:       for.body.lr.ph:
16; CHECK-NEXT:    [[TMP0:%.*]] = load i32, i32* @int_inc, align 4
17; CHECK:       vector.ph:
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
28; CHECK:       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
39
40target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
41
42
43@int_inc = common global i32 0, align 4
44
45define i32 @induction_with_global(i32 %init, i32* noalias nocapture %A, i32 %N) {
46entry:
47  %cmp4 = icmp sgt i32 %N, 0
48  br i1 %cmp4, label %for.body.lr.ph, label %for.end
49
50for.body.lr.ph:                                   ; preds = %entry
51  %0 = load i32, i32* @int_inc, align 4
52  %1 = mul i32 %0, %N
53  br label %for.body
54
55for.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
65
66for.end.loopexit:                                 ; preds = %for.body
67  %2 = add i32 %1, %init
68  br label %for.end
69
70for.end:                                          ; preds = %for.end.loopexit, %entry
71  %x.0.lcssa = phi i32 [ %init, %entry ], [ %2, %for.end.loopexit ]
72  ret i32 %x.0.lcssa
73}
74
75
76;int induction_with_loop_inv(int init, int *restrict A, int N, int M) {
77;  int x = init;
78;  for (int j = 0; j < M; j++) {
79;    for (int i=0; i<N; i++){
80;      A[i] = x;
81;      x += j; // induction step is a loop invariant variable
82;    }
83;  }
84;  return x;
85;}
86
87; CHECK-LABEL: @induction_with_loop_inv(
88; CHECK:       vector.ph:
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
99; CHECK:       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
110
111define i32 @induction_with_loop_inv(i32 %init, i32* noalias nocapture %A, i32 %N, i32 %M) {
112entry:
113  %cmp10 = icmp sgt i32 %M, 0
114  br i1 %cmp10, label %for.cond1.preheader.lr.ph, label %for.end6
115
116for.cond1.preheader.lr.ph:                        ; preds = %entry
117  %cmp27 = icmp sgt i32 %N, 0
118  br label %for.cond1.preheader
119
120for.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
125
126for.body3.preheader:                              ; preds = %for.cond1.preheader
127  br label %for.body3
128
129for.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
139
140for.inc4.loopexit:                                ; preds = %for.body3
141  %0 = add i32 %x.011, %indvars.iv15
142  br label %for.inc4
143
144for.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
150
151for.end6.loopexit:                                ; preds = %for.inc4
152  %x.1.lcssa.lcssa = phi i32 [ %x.1.lcssa, %for.inc4 ]
153  br label %for.end6
154
155for.end6:                                         ; preds = %for.end6.loopexit, %entry
156  %x.0.lcssa = phi i32 [ %init, %entry ], [ %x.1.lcssa.lcssa, %for.end6.loopexit ]
157  ret i32 %x.0.lcssa
158}
159
160
161; CHECK-LABEL: @non_primary_iv_loop_inv_trunc(
162; CHECK:       vector.ph:
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
183
184define void @non_primary_iv_loop_inv_trunc(i32* %a, i64 %n, i64 %step) {
185entry:
186  br label %for.body
187
188for.body:
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
198
199for.end:
200  ret void
201}
202