14e68bc09SSanjay Patel; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2cee313d2SEric Christopher; RUN: opt < %s -loop-vectorize -force-vector-interleave=1 -force-vector-width=4 -dce -instcombine -S | FileCheck %s
34cd19a6eSFangrui Song; RUN: opt < %s -loop-vectorize -disable-basic-aa -S -pass-remarks-analysis='loop-vectorize' 2>&1 | FileCheck %s -check-prefix=FORCED_OPTSIZE
4cee313d2SEric Christopher
5cee313d2SEric Christophertarget 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"
6cee313d2SEric Christopher
7cee313d2SEric Christopher; Make sure we vectorize this loop:
8cee313d2SEric Christopher; int foo(float *a, float *b, int n) {
9cee313d2SEric Christopher;   for (int i=0; i<n; ++i)
10cee313d2SEric Christopher;     a[i] = b[i] * 3;
11cee313d2SEric Christopher; }
12cee313d2SEric Christopher
13cee313d2SEric Christopherdefine i32 @foo(float* nocapture %a, float* nocapture %b, i32 %n) nounwind uwtable ssp {
14cee313d2SEric Christopher; CHECK-LABEL: @foo(
15cee313d2SEric Christopher; CHECK-NEXT:  entry:
16*b7315ffcSFlorian Hahn; CHECK-NEXT:    [[B2:%.*]] = ptrtoint float* [[B:%.*]] to i64, [[DBG4:!dbg !.*]]
17*b7315ffcSFlorian Hahn; CHECK-NEXT:    [[A1:%.*]] = ptrtoint float* [[A:%.*]] to i64, [[DBG4]]
18*b7315ffcSFlorian Hahn; CHECK-NEXT:    [[CMP6:%.*]] = icmp sgt i32 [[N:%.*]], 0, [[DBG4]]
199e0c3565SSanjay Patel; CHECK-NEXT:    br i1 [[CMP6]], label [[FOR_BODY_PREHEADER:%.*]], label [[FOR_END:%.*]], [[DBG4]]
20cee313d2SEric Christopher; CHECK:       for.body.preheader:
219e0c3565SSanjay Patel; CHECK-NEXT:    [[TMP0:%.*]] = add i32 [[N]], -1, [[DBG9:!dbg !.*]]
229e0c3565SSanjay Patel; CHECK-NEXT:    [[TMP1:%.*]] = zext i32 [[TMP0]] to i64, [[DBG9]]
239e0c3565SSanjay Patel; CHECK-NEXT:    [[TMP2:%.*]] = add nuw nsw i64 [[TMP1]], 1, [[DBG9]]
249e0c3565SSanjay Patel; CHECK-NEXT:    [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[TMP0]], 3, [[DBG9]]
259e0c3565SSanjay Patel; CHECK-NEXT:    br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK:%.*]], [[DBG9]]
26cee313d2SEric Christopher; CHECK:       vector.memcheck:
27*b7315ffcSFlorian Hahn; CHECK-NEXT:    [[TMP3:%.*]] = sub i64 [[A1]], [[B2]], [[DBG9]]
28*b7315ffcSFlorian Hahn; CHECK-NEXT:    [[DIFF_CHECK:%.*]] = icmp ult i64 [[TMP3]], 16, [[DBG9]]
29*b7315ffcSFlorian Hahn; CHECK-NEXT:    br i1 [[DIFF_CHECK]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]], [[DBG9]]
30cee313d2SEric Christopher; CHECK:       vector.ph:
314e68bc09SSanjay Patel; CHECK-NEXT:    [[N_VEC:%.*]] = and i64 [[TMP2]], 8589934588, [[DBG9]]
329e0c3565SSanjay Patel; CHECK-NEXT:    br label [[VECTOR_BODY:%.*]], [[DBG9]]
33cee313d2SEric Christopher; CHECK:       vector.body:
349e0c3565SSanjay Patel; CHECK-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ], [[DBG9]]
359e0c3565SSanjay Patel; CHECK-NEXT:    [[TMP6:%.*]] = getelementptr inbounds float, float* [[B]], i64 [[INDEX]], [[DBG9]]
369e0c3565SSanjay Patel; CHECK-NEXT:    [[TMP7:%.*]] = bitcast float* [[TMP6]] to <4 x float>*, [[DBG9]]
37*b7315ffcSFlorian Hahn; CHECK-NEXT:    [[WIDE_LOAD:%.*]] = load <4 x float>, <4 x float>* [[TMP7]], align 4, [[DBG9]]
389e0c3565SSanjay Patel; CHECK-NEXT:    [[TMP8:%.*]] = fmul <4 x float> [[WIDE_LOAD]], <float 3.000000e+00, float 3.000000e+00, float 3.000000e+00, float 3.000000e+00>, [[DBG9]]
399e0c3565SSanjay Patel; CHECK-NEXT:    [[TMP9:%.*]] = getelementptr inbounds float, float* [[A]], i64 [[INDEX]], [[DBG9]]
409e0c3565SSanjay Patel; CHECK-NEXT:    [[TMP10:%.*]] = bitcast float* [[TMP9]] to <4 x float>*, [[DBG9]]
41*b7315ffcSFlorian Hahn; CHECK-NEXT:    store <4 x float> [[TMP8]], <4 x float>* [[TMP10]], align 4, [[DBG9]]
4223c2f2e6SFlorian Hahn; CHECK-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4, [[DBG9]]
439e0c3565SSanjay Patel; CHECK-NEXT:    [[TMP11:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]], [[DBG9]]
449e0c3565SSanjay Patel; CHECK-NEXT:    br i1 [[TMP11]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], [[DBG9]], [[LOOP15:!llvm.loop !.*]]
45cee313d2SEric Christopher; CHECK:       middle.block:
469e0c3565SSanjay Patel; CHECK-NEXT:    [[CMP_N:%.*]] = icmp eq i64 [[TMP2]], [[N_VEC]], [[DBG9]]
479e0c3565SSanjay Patel; CHECK-NEXT:    br i1 [[CMP_N]], label [[FOR_END_LOOPEXIT:%.*]], label [[SCALAR_PH]], [[DBG9]]
48cee313d2SEric Christopher; CHECK:       scalar.ph:
499e0c3565SSanjay Patel; CHECK-NEXT:    [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[FOR_BODY_PREHEADER]] ], [ 0, [[VECTOR_MEMCHECK]] ], [[DBG9]]
509e0c3565SSanjay Patel; CHECK-NEXT:    br label [[FOR_BODY:%.*]], [[DBG9]]
51cee313d2SEric Christopher; CHECK:       for.body:
529e0c3565SSanjay Patel; CHECK-NEXT:    [[INDVARS_IV:%.*]] = phi i64 [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY]] ], [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [[DBG9]]
539e0c3565SSanjay Patel; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds float, float* [[B]], i64 [[INDVARS_IV]], [[DBG9]]
549e0c3565SSanjay Patel; CHECK-NEXT:    [[TMP12:%.*]] = load float, float* [[ARRAYIDX]], align 4, [[DBG9]]
559e0c3565SSanjay Patel; CHECK-NEXT:    [[MUL:%.*]] = fmul float [[TMP12]], 3.000000e+00, [[DBG9]]
569e0c3565SSanjay Patel; CHECK-NEXT:    [[ARRAYIDX2:%.*]] = getelementptr inbounds float, float* [[A]], i64 [[INDVARS_IV]], [[DBG9]]
579e0c3565SSanjay Patel; CHECK-NEXT:    store float [[MUL]], float* [[ARRAYIDX2]], align 4, [[DBG9]]
589e0c3565SSanjay Patel; CHECK-NEXT:    [[INDVARS_IV_NEXT]] = add i64 [[INDVARS_IV]], 1, [[DBG9]]
599e0c3565SSanjay Patel; CHECK-NEXT:    [[LFTR_WIDEIV:%.*]] = trunc i64 [[INDVARS_IV_NEXT]] to i32, [[DBG9]]
609e0c3565SSanjay Patel; CHECK-NEXT:    [[EXITCOND:%.*]] = icmp eq i32 [[LFTR_WIDEIV]], [[N]], [[DBG9]]
619e0c3565SSanjay Patel; CHECK-NEXT:    br i1 [[EXITCOND]], label [[FOR_END_LOOPEXIT]], label [[FOR_BODY]], [[DBG9]], [[LOOP17:!llvm.loop !.*]]
62cee313d2SEric Christopher; CHECK:       for.end.loopexit:
639e0c3565SSanjay Patel; CHECK-NEXT:    br label [[FOR_END]], [[DBG18:!dbg !.*]]
64cee313d2SEric Christopher; CHECK:       for.end:
659e0c3565SSanjay Patel; CHECK-NEXT:    ret i32 undef, [[DBG18]]
66cee313d2SEric Christopher;
67cee313d2SEric Christopherentry:
68cee313d2SEric Christopher  %cmp6 = icmp sgt i32 %n, 0, !dbg !6
69cee313d2SEric Christopher  br i1 %cmp6, label %for.body, label %for.end, !dbg !6
70cee313d2SEric Christopher
71cee313d2SEric Christopherfor.body:                                         ; preds = %entry, %for.body
72cee313d2SEric Christopher  %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ], !dbg !7
73cee313d2SEric Christopher  %arrayidx = getelementptr inbounds float, float* %b, i64 %indvars.iv, !dbg !7
74cee313d2SEric Christopher  %0 = load float, float* %arrayidx, align 4, !dbg !7
75cee313d2SEric Christopher  %mul = fmul float %0, 3.000000e+00, !dbg !7
76cee313d2SEric Christopher  %arrayidx2 = getelementptr inbounds float, float* %a, i64 %indvars.iv, !dbg !7
77cee313d2SEric Christopher  store float %mul, float* %arrayidx2, align 4, !dbg !7
78cee313d2SEric Christopher  %indvars.iv.next = add i64 %indvars.iv, 1, !dbg !7
79cee313d2SEric Christopher  %lftr.wideiv = trunc i64 %indvars.iv.next to i32, !dbg !7
80cee313d2SEric Christopher  %exitcond = icmp eq i32 %lftr.wideiv, %n, !dbg !7
81cee313d2SEric Christopher  br i1 %exitcond, label %for.end, label %for.body, !dbg !7
82cee313d2SEric Christopher
83cee313d2SEric Christopherfor.end:                                          ; preds = %for.body, %entry
84cee313d2SEric Christopher  ret i32 undef, !dbg !8
85cee313d2SEric Christopher}
86cee313d2SEric Christopher
87cee313d2SEric Christopher; Make sure that we try to vectorize loops with a runtime check if the
88cee313d2SEric Christopher; dependency check fails.
89cee313d2SEric Christopher
90cee313d2SEric Christopher; CHECK-LABEL: test_runtime_check
91cee313d2SEric Christopher; CHECK:      <4 x float>
92cee313d2SEric Christopherdefine void @test_runtime_check(float* %a, float %b, i64 %offset, i64 %offset2, i64 %n) {
93cee313d2SEric Christopherentry:
94cee313d2SEric Christopher  br label %for.body
95cee313d2SEric Christopher
96cee313d2SEric Christopherfor.body:
97cee313d2SEric Christopher  %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
98cee313d2SEric Christopher  %ind.sum = add i64 %iv, %offset
99cee313d2SEric Christopher  %arr.idx = getelementptr inbounds float, float* %a, i64 %ind.sum
100cee313d2SEric Christopher  %l1 = load float, float* %arr.idx, align 4
101cee313d2SEric Christopher  %ind.sum2 = add i64 %iv, %offset2
102cee313d2SEric Christopher  %arr.idx2 = getelementptr inbounds float, float* %a, i64 %ind.sum2
103cee313d2SEric Christopher  %l2 = load float, float* %arr.idx2, align 4
104cee313d2SEric Christopher  %m = fmul fast float %b, %l2
105cee313d2SEric Christopher  %ad = fadd fast float %l1, %m
106cee313d2SEric Christopher  store float %ad, float* %arr.idx, align 4
107cee313d2SEric Christopher  %iv.next = add nuw nsw i64 %iv, 1
108cee313d2SEric Christopher  %exitcond = icmp eq i64 %iv.next, %n
109cee313d2SEric Christopher  br i1 %exitcond, label %loopexit, label %for.body
110cee313d2SEric Christopher
111cee313d2SEric Christopherloopexit:
112cee313d2SEric Christopher  ret void
113cee313d2SEric Christopher}
114cee313d2SEric Christopher
115cee313d2SEric Christopher; Check we do not generate runtime checks if we found a known dependence preventing
116cee313d2SEric Christopher; vectorization. In this case, it is a read of c[i-1] followed by a write of c[i].
117cee313d2SEric Christopher; The runtime checks would always fail.
118cee313d2SEric Christopher
119cee313d2SEric Christopher; void test_runtime_check2(float *a, float b, unsigned offset, unsigned offset2, unsigned n, float *c) {
120cee313d2SEric Christopher;   for (unsigned i = 1; i < n; i++) {
121cee313d2SEric Christopher;     a[i+o1] += a[i+o2] + b;
122cee313d2SEric Christopher;     c[i] = c[i-1] + b;
123cee313d2SEric Christopher;   }
124cee313d2SEric Christopher; }
125cee313d2SEric Christopher;
126cee313d2SEric Christopher; CHECK-LABEL: test_runtime_check2
127cee313d2SEric Christopher; CHECK-NOT:      <4 x float>
128cee313d2SEric Christopherdefine void @test_runtime_check2(float* %a, float %b, i64 %offset, i64 %offset2, i64 %n, float* %c) {
129cee313d2SEric Christopherentry:
130cee313d2SEric Christopher  br label %for.body
131cee313d2SEric Christopher
132cee313d2SEric Christopherfor.body:
133cee313d2SEric Christopher  %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ]
134cee313d2SEric Christopher  %ind.sum = add i64 %iv, %offset
135cee313d2SEric Christopher  %arr.idx = getelementptr inbounds float, float* %a, i64 %ind.sum
136cee313d2SEric Christopher  %l1 = load float, float* %arr.idx, align 4
137cee313d2SEric Christopher  %ind.sum2 = add i64 %iv, %offset2
138cee313d2SEric Christopher  %arr.idx2 = getelementptr inbounds float, float* %a, i64 %ind.sum2
139cee313d2SEric Christopher  %l2 = load float, float* %arr.idx2, align 4
140cee313d2SEric Christopher  %m = fmul fast float %b, %l2
141cee313d2SEric Christopher  %ad = fadd fast float %l1, %m
142cee313d2SEric Christopher  store float %ad, float* %arr.idx, align 4
143cee313d2SEric Christopher  %c.ind = add i64 %iv, -1
144cee313d2SEric Christopher  %c.idx = getelementptr inbounds float, float* %c, i64 %c.ind
145cee313d2SEric Christopher  %lc = load float, float* %c.idx, align 4
146cee313d2SEric Christopher  %vc = fadd float %lc, 1.0
147cee313d2SEric Christopher  %c.idx2 = getelementptr inbounds float, float* %c, i64 %iv
148cee313d2SEric Christopher  store float %vc, float* %c.idx2
149cee313d2SEric Christopher  %iv.next = add nuw nsw i64 %iv, 1
150cee313d2SEric Christopher  %exitcond = icmp eq i64 %iv.next, %n
151cee313d2SEric Christopher  br i1 %exitcond, label %loopexit, label %for.body
152cee313d2SEric Christopher
153cee313d2SEric Christopherloopexit:
154cee313d2SEric Christopher  ret void
155cee313d2SEric Christopher}
156cee313d2SEric Christopher
157cee313d2SEric Christopher; CHECK: !9 = !DILocation(line: 101, column: 1, scope: !{{.*}})
158cee313d2SEric Christopher
1590fcb3afbSSjoerd Meijerdefine dso_local void @forced_optsize(i64* noalias nocapture readonly %x_p, i64* noalias nocapture readonly %y_p, i64* noalias nocapture %z_p) minsize optsize {
1600fcb3afbSSjoerd Meijer;
1617bf299c8SAyal Zaks; FORCED_OPTSIZE: remark: <unknown>:0:0: loop not vectorized: runtime pointer checks needed. Enable vectorization of this loop with '#pragma clang loop vectorize(enable)' when compiling with -Os/-Oz
1620fcb3afbSSjoerd Meijer; FORCED_OPTSIZE-LABEL: @forced_optsize(
1637bf299c8SAyal Zaks; FORCED_OPTSIZE-NOT:       vector.body:
1640fcb3afbSSjoerd Meijer;
1650fcb3afbSSjoerd Meijerentry:
1660fcb3afbSSjoerd Meijer  br label %for.body
1670fcb3afbSSjoerd Meijer
1680fcb3afbSSjoerd Meijerfor.cond.cleanup:
1690fcb3afbSSjoerd Meijer  ret void
1700fcb3afbSSjoerd Meijer
1710fcb3afbSSjoerd Meijerfor.body:
1720fcb3afbSSjoerd Meijer  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
1730fcb3afbSSjoerd Meijer  %arrayidx = getelementptr inbounds i64, i64* %x_p, i64 %indvars.iv
1740fcb3afbSSjoerd Meijer  %0 = load i64, i64* %arrayidx, align 8
1750fcb3afbSSjoerd Meijer  %arrayidx2 = getelementptr inbounds i64, i64* %y_p, i64 %indvars.iv
1760fcb3afbSSjoerd Meijer  %1 = load i64, i64* %arrayidx2, align 8
1770fcb3afbSSjoerd Meijer  %add = add nsw i64 %1, %0
1780fcb3afbSSjoerd Meijer  %arrayidx4 = getelementptr inbounds i64, i64* %z_p, i64 %indvars.iv
1790fcb3afbSSjoerd Meijer  store i64 %add, i64* %arrayidx4, align 8
1800fcb3afbSSjoerd Meijer  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
1810fcb3afbSSjoerd Meijer  %exitcond = icmp eq i64 %indvars.iv.next, 128
1820fcb3afbSSjoerd Meijer  br i1 %exitcond, label %for.cond.cleanup, label %for.body, !llvm.loop !12
1830fcb3afbSSjoerd Meijer}
1840fcb3afbSSjoerd Meijer
185cee313d2SEric Christopher!llvm.module.flags = !{!0, !1}
186cee313d2SEric Christopher!llvm.dbg.cu = !{!9}
187cee313d2SEric Christopher!0 = !{i32 2, !"Dwarf Version", i32 4}
188cee313d2SEric Christopher!1 = !{i32 2, !"Debug Info Version", i32 3}
189cee313d2SEric Christopher
190cee313d2SEric Christopher!2 = !{}
191cee313d2SEric Christopher!3 = !DISubroutineType(types: !2)
192cee313d2SEric Christopher!4 = !DIFile(filename: "test.cpp", directory: "/tmp")
193cee313d2SEric Christopher!5 = distinct !DISubprogram(name: "foo", scope: !4, file: !4, line: 99, type: !3, isLocal: false, isDefinition: true, scopeLine: 100, flags: DIFlagPrototyped, isOptimized: false, unit: !9, retainedNodes: !2)
194cee313d2SEric Christopher!6 = !DILocation(line: 100, column: 1, scope: !5)
195cee313d2SEric Christopher!7 = !DILocation(line: 101, column: 1, scope: !5)
196cee313d2SEric Christopher!8 = !DILocation(line: 102, column: 1, scope: !5)
197cee313d2SEric Christopher!9 = distinct !DICompileUnit(language: DW_LANG_C99, producer: "clang",
198cee313d2SEric Christopher                             file: !10,
199cee313d2SEric Christopher                             isOptimized: true, flags: "-O2",
200cee313d2SEric Christopher                             splitDebugFilename: "abc.debug", emissionKind: 2)
201cee313d2SEric Christopher!10 = !DIFile(filename: "path/to/file", directory: "/path/to/dir")
202cee313d2SEric Christopher!11 = !{i32 2, !"Debug Info Version", i32 3}
2030fcb3afbSSjoerd Meijer!12 = distinct !{!12, !13, !14}
2040fcb3afbSSjoerd Meijer!13 = !{!"llvm.loop.vectorize.width", i32 2}
2050fcb3afbSSjoerd Meijer!14 = !{!"llvm.loop.vectorize.enable", i1 true}
206