1; RUN: opt < %s -debug-only=loop-vectorize -loop-vectorize -vectorizer-maximize-bandwidth -O2 -mtriple=x86_64-unknown-linux -S 2>&1 | FileCheck %s
2; RUN: opt < %s -debug-only=loop-vectorize -loop-vectorize -vectorizer-maximize-bandwidth -O2 -mtriple=x86_64-unknown-linux -mattr=+avx512f -S 2>&1 | FileCheck %s --check-prefix=AVX512F
3; REQUIRES: asserts
4
5@a = global [1024 x i8] zeroinitializer, align 16
6@b = global [1024 x i8] zeroinitializer, align 16
7
8define i32 @foo() {
9; This function has a loop of SAD pattern. Here we check when VF = 16 the
10; register usage doesn't exceed 16.
11;
12; CHECK-LABEL: foo
13; CHECK:      LV(REG): VF = 8
14; CHECK-NEXT: LV(REG): Found max usage: 7
15; CHECK:      LV(REG): VF = 16
16; CHECK-NEXT: LV(REG): Found max usage: 13
17
18entry:
19  br label %for.body
20
21for.cond.cleanup:
22  %add.lcssa = phi i32 [ %add, %for.body ]
23  ret i32 %add.lcssa
24
25for.body:
26  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
27  %s.015 = phi i32 [ 0, %entry ], [ %add, %for.body ]
28  %arrayidx = getelementptr inbounds [1024 x i8], [1024 x i8]* @a, i64 0, i64 %indvars.iv
29  %0 = load i8, i8* %arrayidx, align 1
30  %conv = zext i8 %0 to i32
31  %arrayidx2 = getelementptr inbounds [1024 x i8], [1024 x i8]* @b, i64 0, i64 %indvars.iv
32  %1 = load i8, i8* %arrayidx2, align 1
33  %conv3 = zext i8 %1 to i32
34  %sub = sub nsw i32 %conv, %conv3
35  %ispos = icmp sgt i32 %sub, -1
36  %neg = sub nsw i32 0, %sub
37  %2 = select i1 %ispos, i32 %sub, i32 %neg
38  %add = add nsw i32 %2, %s.015
39  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
40  %exitcond = icmp eq i64 %indvars.iv.next, 1024
41  br i1 %exitcond, label %for.cond.cleanup, label %for.body
42}
43
44define i32 @goo() {
45; For indvars.iv used in a computating chain only feeding into getelementptr or cmp,
46; it will not have vector version and the vector register usage will not exceed the
47; available vector register number.
48; CHECK-LABEL: goo
49; CHECK:      LV(REG): VF = 8
50; CHECK-NEXT: LV(REG): Found max usage: 7
51; CHECK:      LV(REG): VF = 16
52; CHECK-NEXT: LV(REG): Found max usage: 13
53entry:
54  br label %for.body
55
56for.cond.cleanup:                                 ; preds = %for.body
57  %add.lcssa = phi i32 [ %add, %for.body ]
58  ret i32 %add.lcssa
59
60for.body:                                         ; preds = %for.body, %entry
61  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
62  %s.015 = phi i32 [ 0, %entry ], [ %add, %for.body ]
63  %tmp1 = add nsw i64 %indvars.iv, 3
64  %arrayidx = getelementptr inbounds [1024 x i8], [1024 x i8]* @a, i64 0, i64 %tmp1
65  %tmp = load i8, i8* %arrayidx, align 1
66  %conv = zext i8 %tmp to i32
67  %tmp2 = add nsw i64 %indvars.iv, 2
68  %arrayidx2 = getelementptr inbounds [1024 x i8], [1024 x i8]* @b, i64 0, i64 %tmp2
69  %tmp3 = load i8, i8* %arrayidx2, align 1
70  %conv3 = zext i8 %tmp3 to i32
71  %sub = sub nsw i32 %conv, %conv3
72  %ispos = icmp sgt i32 %sub, -1
73  %neg = sub nsw i32 0, %sub
74  %tmp4 = select i1 %ispos, i32 %sub, i32 %neg
75  %add = add nsw i32 %tmp4, %s.015
76  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
77  %exitcond = icmp eq i64 %indvars.iv.next, 1024
78  br i1 %exitcond, label %for.cond.cleanup, label %for.body
79}
80
81define i64 @bar(i64* nocapture %a) {
82; CHECK-LABEL: bar
83; CHECK:       LV(REG): VF = 2
84; CHECK:       LV(REG): Found max usage: 3
85;
86entry:
87  br label %for.body
88
89for.cond.cleanup:
90  %add2.lcssa = phi i64 [ %add2, %for.body ]
91  ret i64 %add2.lcssa
92
93for.body:
94  %i.012 = phi i64 [ 0, %entry ], [ %inc, %for.body ]
95  %s.011 = phi i64 [ 0, %entry ], [ %add2, %for.body ]
96  %arrayidx = getelementptr inbounds i64, i64* %a, i64 %i.012
97  %0 = load i64, i64* %arrayidx, align 8
98  %add = add nsw i64 %0, %i.012
99  store i64 %add, i64* %arrayidx, align 8
100  %add2 = add nsw i64 %add, %s.011
101  %inc = add nuw nsw i64 %i.012, 1
102  %exitcond = icmp eq i64 %inc, 1024
103  br i1 %exitcond, label %for.cond.cleanup, label %for.body
104}
105
106@d = external global [0 x i64], align 8
107@e = external global [0 x i32], align 4
108@c = external global [0 x i32], align 4
109
110define void @hoo(i32 %n) {
111; For c[i] = e[d[i]] in the loop, e[d[i]] is not consecutive but its index %tmp can
112; be gathered into a vector. For VF == 16, the vector version of %tmp will be <16 x i64>
113; so the max usage of AVX512 vector register will be 2.
114; AVX512F-LABEL: bar
115; AVX512F:       LV(REG): VF = 16
116; AVX512F:       LV(REG): Found max usage: 2
117;
118entry:
119  br label %for.body
120
121for.body:                                         ; preds = %for.body, %entry
122  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
123  %arrayidx = getelementptr inbounds [0 x i64], [0 x i64]* @d, i64 0, i64 %indvars.iv
124  %tmp = load i64, i64* %arrayidx, align 8
125  %arrayidx1 = getelementptr inbounds [0 x i32], [0 x i32]* @e, i64 0, i64 %tmp
126  %tmp1 = load i32, i32* %arrayidx1, align 4
127  %arrayidx3 = getelementptr inbounds [0 x i32], [0 x i32]* @c, i64 0, i64 %indvars.iv
128  store i32 %tmp1, i32* %arrayidx3, align 4
129  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
130  %exitcond = icmp eq i64 %indvars.iv.next, 10000
131  br i1 %exitcond, label %for.end, label %for.body
132
133for.end:                                          ; preds = %for.body
134  ret void
135}
136