1*2e14900dSBjorn Pettersson; RUN: opt < %s -passes=loop-vectorize -force-vector-interleave=1 -force-vector-width=8 -S | FileCheck %s
2cee313d2SEric Christopher
3cee313d2SEric Christopher; int int_inc;
4cee313d2SEric Christopher;
5cee313d2SEric Christopher;int induction_with_global(int init, int *restrict A, int N) {
6cee313d2SEric Christopher;  int x = init;
7cee313d2SEric Christopher;  for (int i=0;i<N;i++){
8cee313d2SEric Christopher;    A[i] = x;
9cee313d2SEric Christopher;    x += int_inc;
10cee313d2SEric Christopher;  }
11cee313d2SEric Christopher;  return x;
12cee313d2SEric Christopher;}
13cee313d2SEric Christopher
14cee313d2SEric Christopher; CHECK-LABEL: @induction_with_global(
15cee313d2SEric Christopher; CHECK:       for.body.lr.ph:
16cee313d2SEric Christopher; CHECK-NEXT:    [[TMP0:%.*]] = load i32, i32* @int_inc, align 4
17cee313d2SEric Christopher; CHECK:       vector.ph:
18278aa65cSJuneyoung Lee; CHECK:         [[DOTSPLATINSERT:%.*]] = insertelement <8 x i32> poison, i32 %init, i32 0
19278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLAT:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT]], <8 x i32> poison, <8 x i32> zeroinitializer
20278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLATINSERT2:%.*]] = insertelement <8 x i32> poison, i32 [[TMP0]], i32 0
21278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLAT3:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT2]], <8 x i32> poison, <8 x i32> zeroinitializer
22cee313d2SEric Christopher; CHECK-NEXT:    [[TMP6:%.*]] = mul <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>, [[DOTSPLAT3]]
23cee313d2SEric Christopher; CHECK-NEXT:    [[INDUCTION4:%.*]] = add <8 x i32> [[DOTSPLAT]], [[TMP6]]
24cee313d2SEric Christopher; CHECK-NEXT:    [[TMP7:%.*]] = mul i32 [[TMP0]], 8
25278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLATINSERT5:%.*]] = insertelement <8 x i32> poison, i32 [[TMP7]], i32 0
26278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLAT6:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT5]], <8 x i32> poison, <8 x i32> zeroinitializer
27cee313d2SEric Christopher; CHECK-NEXT:    br label %vector.body
28cee313d2SEric Christopher; CHECK:       vector.body:
29cee313d2SEric Christopher; CHECK-NEXT:    %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
30cee313d2SEric Christopher; CHECK-NEXT:    %vec.ind = phi <8 x i32> [ [[INDUCTION4]], %vector.ph ], [ %vec.ind.next, %vector.body ]
31cee313d2SEric Christopher; CHECK:         [[TMP8:%.*]] = add i64 %index, 0
32cee313d2SEric Christopher; CHECK-NEXT:    [[TMP9:%.*]] = getelementptr inbounds i32, i32* [[A:%.*]], i64 [[TMP8]]
33cee313d2SEric Christopher; CHECK-NEXT:    [[TMP10:%.*]] = getelementptr inbounds i32, i32* [[TMP9]], i32 0
34cee313d2SEric Christopher; CHECK-NEXT:    [[TMP11:%.*]] = bitcast i32* [[TMP10]] to <8 x i32>*
35cee313d2SEric Christopher; CHECK-NEXT:    store <8 x i32> %vec.ind, <8 x i32>* [[TMP11]], align 4
3623c2f2e6SFlorian Hahn; CHECK:         %index.next = add nuw i64 %index, 8
37cee313d2SEric Christopher; CHECK-NEXT:    %vec.ind.next = add <8 x i32> %vec.ind, [[DOTSPLAT6]]
38cee313d2SEric Christopher; CHECK:         br i1 {{.*}}, label %middle.block, label %vector.body
39cee313d2SEric Christopher
40cee313d2SEric Christophertarget datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
41cee313d2SEric Christopher
42cee313d2SEric Christopher
43cee313d2SEric Christopher@int_inc = common global i32 0, align 4
44cee313d2SEric Christopher
45cee313d2SEric Christopherdefine i32 @induction_with_global(i32 %init, i32* noalias nocapture %A, i32 %N) {
46cee313d2SEric Christopherentry:
47cee313d2SEric Christopher  %cmp4 = icmp sgt i32 %N, 0
48cee313d2SEric Christopher  br i1 %cmp4, label %for.body.lr.ph, label %for.end
49cee313d2SEric Christopher
50cee313d2SEric Christopherfor.body.lr.ph:                                   ; preds = %entry
51cee313d2SEric Christopher  %0 = load i32, i32* @int_inc, align 4
52cee313d2SEric Christopher  %1 = mul i32 %0, %N
53cee313d2SEric Christopher  br label %for.body
54cee313d2SEric Christopher
55cee313d2SEric Christopherfor.body:                                         ; preds = %for.body, %for.body.lr.ph
56cee313d2SEric Christopher  %indvars.iv = phi i64 [ 0, %for.body.lr.ph ], [ %indvars.iv.next, %for.body ]
57cee313d2SEric Christopher  %x.05 = phi i32 [ %init, %for.body.lr.ph ], [ %add, %for.body ]
58cee313d2SEric Christopher  %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
59cee313d2SEric Christopher  store i32 %x.05, i32* %arrayidx, align 4
60cee313d2SEric Christopher  %add = add nsw i32 %0, %x.05
61cee313d2SEric Christopher  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
62cee313d2SEric Christopher  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
63cee313d2SEric Christopher  %exitcond = icmp eq i32 %lftr.wideiv, %N
64cee313d2SEric Christopher  br i1 %exitcond, label %for.end.loopexit, label %for.body
65cee313d2SEric Christopher
66cee313d2SEric Christopherfor.end.loopexit:                                 ; preds = %for.body
67cee313d2SEric Christopher  %2 = add i32 %1, %init
68cee313d2SEric Christopher  br label %for.end
69cee313d2SEric Christopher
70cee313d2SEric Christopherfor.end:                                          ; preds = %for.end.loopexit, %entry
71cee313d2SEric Christopher  %x.0.lcssa = phi i32 [ %init, %entry ], [ %2, %for.end.loopexit ]
72cee313d2SEric Christopher  ret i32 %x.0.lcssa
73cee313d2SEric Christopher}
74cee313d2SEric Christopher
75cee313d2SEric Christopher
76cee313d2SEric Christopher;int induction_with_loop_inv(int init, int *restrict A, int N, int M) {
77cee313d2SEric Christopher;  int x = init;
78cee313d2SEric Christopher;  for (int j = 0; j < M; j++) {
79cee313d2SEric Christopher;    for (int i=0; i<N; i++){
80cee313d2SEric Christopher;      A[i] = x;
81cee313d2SEric Christopher;      x += j; // induction step is a loop invariant variable
82cee313d2SEric Christopher;    }
83cee313d2SEric Christopher;  }
84cee313d2SEric Christopher;  return x;
85cee313d2SEric Christopher;}
86cee313d2SEric Christopher
87cee313d2SEric Christopher; CHECK-LABEL: @induction_with_loop_inv(
88cee313d2SEric Christopher; CHECK:       vector.ph:
89278aa65cSJuneyoung Lee; CHECK:         [[DOTSPLATINSERT:%.*]] = insertelement <8 x i32> poison, i32 %x.011, i32 0
90278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLAT:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT]], <8 x i32> poison, <8 x i32> zeroinitializer
91278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLATINSERT2:%.*]] = insertelement <8 x i32> poison, i32 %j.012, i32 0
92278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLAT3:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT2]], <8 x i32> poison, <8 x i32> zeroinitializer
93cee313d2SEric Christopher; CHECK-NEXT:    [[TMP4:%.*]] = mul <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>, [[DOTSPLAT3]]
94cee313d2SEric Christopher; CHECK-NEXT:    [[INDUCTION4:%.*]] = add <8 x i32> [[DOTSPLAT]], [[TMP4]]
95cee313d2SEric Christopher; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 %j.012, 8
96278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLATINSERT5:%.*]] = insertelement <8 x i32> poison, i32 [[TMP5]], i32 0
97278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLAT6:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT5]], <8 x i32> poison, <8 x i32> zeroinitializer
98cee313d2SEric Christopher; CHECK-NEXT:    br label %vector.body
99cee313d2SEric Christopher; CHECK:       vector.body:
100cee313d2SEric Christopher; CHECK-NEXT:    %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
101cee313d2SEric Christopher; CHECK-NEXT:    %vec.ind = phi <8 x i32> [ [[INDUCTION4]], %vector.ph ], [ %vec.ind.next, %vector.body ]
102cee313d2SEric Christopher; CHECK:         [[TMP6:%.*]] = add i64 %index, 0
103cee313d2SEric Christopher; CHECK-NEXT:    [[TMP7:%.*]] = getelementptr inbounds i32, i32* [[A:%.*]], i64 [[TMP6]]
104cee313d2SEric Christopher; CHECK-NEXT:    [[TMP8:%.*]] = getelementptr inbounds i32, i32* [[TMP7]], i32 0
105cee313d2SEric Christopher; CHECK-NEXT:    [[TMP9:%.*]] = bitcast i32* [[TMP8]] to <8 x i32>*
106cee313d2SEric Christopher; CHECK-NEXT:    store <8 x i32> %vec.ind, <8 x i32>* [[TMP9]], align 4
10723c2f2e6SFlorian Hahn; CHECK:         %index.next = add nuw i64 %index, 8
108cee313d2SEric Christopher; CHECK-NEXT:    %vec.ind.next = add <8 x i32> %vec.ind, [[DOTSPLAT6]]
109cee313d2SEric Christopher; CHECK:         br i1 {{.*}}, label %middle.block, label %vector.body
110cee313d2SEric Christopher
111cee313d2SEric Christopherdefine i32 @induction_with_loop_inv(i32 %init, i32* noalias nocapture %A, i32 %N, i32 %M) {
112cee313d2SEric Christopherentry:
113cee313d2SEric Christopher  %cmp10 = icmp sgt i32 %M, 0
114cee313d2SEric Christopher  br i1 %cmp10, label %for.cond1.preheader.lr.ph, label %for.end6
115cee313d2SEric Christopher
116cee313d2SEric Christopherfor.cond1.preheader.lr.ph:                        ; preds = %entry
117cee313d2SEric Christopher  %cmp27 = icmp sgt i32 %N, 0
118cee313d2SEric Christopher  br label %for.cond1.preheader
119cee313d2SEric Christopher
120cee313d2SEric Christopherfor.cond1.preheader:                              ; preds = %for.inc4, %for.cond1.preheader.lr.ph
121cee313d2SEric Christopher  %indvars.iv15 = phi i32 [ 0, %for.cond1.preheader.lr.ph ], [ %indvars.iv.next16, %for.inc4 ]
122cee313d2SEric Christopher  %j.012 = phi i32 [ 0, %for.cond1.preheader.lr.ph ], [ %inc5, %for.inc4 ]
123cee313d2SEric Christopher  %x.011 = phi i32 [ %init, %for.cond1.preheader.lr.ph ], [ %x.1.lcssa, %for.inc4 ]
124cee313d2SEric Christopher  br i1 %cmp27, label %for.body3.preheader, label %for.inc4
125cee313d2SEric Christopher
126cee313d2SEric Christopherfor.body3.preheader:                              ; preds = %for.cond1.preheader
127cee313d2SEric Christopher  br label %for.body3
128cee313d2SEric Christopher
129cee313d2SEric Christopherfor.body3:                                        ; preds = %for.body3.preheader, %for.body3
130cee313d2SEric Christopher  %indvars.iv = phi i64 [ %indvars.iv.next, %for.body3 ], [ 0, %for.body3.preheader ]
131cee313d2SEric Christopher  %x.18 = phi i32 [ %add, %for.body3 ], [ %x.011, %for.body3.preheader ]
132cee313d2SEric Christopher  %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv
133cee313d2SEric Christopher  store i32 %x.18, i32* %arrayidx, align 4
134cee313d2SEric Christopher  %add = add nsw i32 %x.18, %j.012
135cee313d2SEric Christopher  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
136cee313d2SEric Christopher  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
137cee313d2SEric Christopher  %exitcond = icmp eq i32 %lftr.wideiv, %N
138cee313d2SEric Christopher  br i1 %exitcond, label %for.inc4.loopexit, label %for.body3
139cee313d2SEric Christopher
140cee313d2SEric Christopherfor.inc4.loopexit:                                ; preds = %for.body3
141cee313d2SEric Christopher  %0 = add i32 %x.011, %indvars.iv15
142cee313d2SEric Christopher  br label %for.inc4
143cee313d2SEric Christopher
144cee313d2SEric Christopherfor.inc4:                                         ; preds = %for.inc4.loopexit, %for.cond1.preheader
145cee313d2SEric Christopher  %x.1.lcssa = phi i32 [ %x.011, %for.cond1.preheader ], [ %0, %for.inc4.loopexit ]
146cee313d2SEric Christopher  %inc5 = add nuw nsw i32 %j.012, 1
147cee313d2SEric Christopher  %indvars.iv.next16 = add i32 %indvars.iv15, %N
148cee313d2SEric Christopher  %exitcond17 = icmp eq i32 %inc5, %M
149cee313d2SEric Christopher  br i1 %exitcond17, label %for.end6.loopexit, label %for.cond1.preheader
150cee313d2SEric Christopher
151cee313d2SEric Christopherfor.end6.loopexit:                                ; preds = %for.inc4
152cee313d2SEric Christopher  %x.1.lcssa.lcssa = phi i32 [ %x.1.lcssa, %for.inc4 ]
153cee313d2SEric Christopher  br label %for.end6
154cee313d2SEric Christopher
155cee313d2SEric Christopherfor.end6:                                         ; preds = %for.end6.loopexit, %entry
156cee313d2SEric Christopher  %x.0.lcssa = phi i32 [ %init, %entry ], [ %x.1.lcssa.lcssa, %for.end6.loopexit ]
157cee313d2SEric Christopher  ret i32 %x.0.lcssa
158cee313d2SEric Christopher}
159cee313d2SEric Christopher
160cee313d2SEric Christopher
161cee313d2SEric Christopher; CHECK-LABEL: @non_primary_iv_loop_inv_trunc(
162cee313d2SEric Christopher; CHECK:       vector.ph:
163cee313d2SEric Christopher; CHECK:         [[TMP3:%.*]] = trunc i64 %step to i32
164278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLATINSERT5:%.*]] = insertelement <8 x i32> poison, i32 [[TMP3]], i32 0
165278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLAT6:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT5]], <8 x i32> poison, <8 x i32> zeroinitializer
166cee313d2SEric Christopher; CHECK-NEXT:    [[TMP4:%.*]] = mul <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>, [[DOTSPLAT6]]
167101aaf62SRoman Lebedev; CHECK-NEXT:    [[INDUCTION7:%.*]] = add <8 x i32> zeroinitializer, [[TMP4]]
168cee313d2SEric Christopher; CHECK-NEXT:    [[TMP5:%.*]] = mul i32 [[TMP3]], 8
169278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLATINSERT8:%.*]] = insertelement <8 x i32> poison, i32 [[TMP5]], i32 0
170278aa65cSJuneyoung Lee; CHECK-NEXT:    [[DOTSPLAT9:%.*]] = shufflevector <8 x i32> [[DOTSPLATINSERT8]], <8 x i32> poison, <8 x i32> zeroinitializer
171cee313d2SEric Christopher; CHECK-NEXT:    br label %vector.body
172cee313d2SEric Christopher; CHECK:       vector.body:
173cee313d2SEric Christopher; CHECK-NEXT:    %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
174cee313d2SEric Christopher; CHECK:         [[VEC_IND10:%.*]] = phi <8 x i32> [ [[INDUCTION7]], %vector.ph ], [ [[VEC_IND_NEXT11:%.*]], %vector.body ]
175cee313d2SEric Christopher; CHECK:         [[TMP6:%.*]] = add i64 %index, 0
176cee313d2SEric Christopher; CHECK-NEXT:    [[TMP7:%.*]] = getelementptr inbounds i32, i32* [[A:%.*]], i64 [[TMP6]]
177cee313d2SEric Christopher; CHECK-NEXT:    [[TMP8:%.*]] = getelementptr inbounds i32, i32* [[TMP7]], i32 0
178cee313d2SEric Christopher; CHECK-NEXT:    [[TMP9:%.*]] = bitcast i32* [[TMP8]] to <8 x i32>*
179cee313d2SEric Christopher; CHECK-NEXT:    store <8 x i32> [[VEC_IND10]], <8 x i32>* [[TMP9]], align 4
18023c2f2e6SFlorian Hahn; CHECK-NEXT:    %index.next = add nuw i64 %index, 8
181cee313d2SEric Christopher; CHECK:         [[VEC_IND_NEXT11]] = add <8 x i32> [[VEC_IND10]], [[DOTSPLAT9]]
182cee313d2SEric Christopher; CHECK:         br i1 {{.*}}, label %middle.block, label %vector.body
183cee313d2SEric Christopher
184cee313d2SEric Christopherdefine void @non_primary_iv_loop_inv_trunc(i32* %a, i64 %n, i64 %step) {
185cee313d2SEric Christopherentry:
186cee313d2SEric Christopher  br label %for.body
187cee313d2SEric Christopher
188cee313d2SEric Christopherfor.body:
189cee313d2SEric Christopher  %i = phi i64 [ %i.next, %for.body ], [ 0, %entry ]
190cee313d2SEric Christopher  %j = phi i64 [ %j.next, %for.body ], [ 0, %entry ]
191cee313d2SEric Christopher  %tmp0 = getelementptr inbounds i32, i32* %a, i64 %i
192cee313d2SEric Christopher  %tmp1 = trunc i64 %j to i32
193cee313d2SEric Christopher  store i32 %tmp1, i32* %tmp0, align 4
194cee313d2SEric Christopher  %i.next = add nuw nsw i64 %i, 1
195cee313d2SEric Christopher  %j.next = add nuw nsw i64 %j, %step
196cee313d2SEric Christopher  %cond = icmp slt i64 %i.next, %n
197cee313d2SEric Christopher  br i1 %cond, label %for.body, label %for.end
198cee313d2SEric Christopher
199cee313d2SEric Christopherfor.end:
200cee313d2SEric Christopher  ret void
201cee313d2SEric Christopher}
202