1; RUN: opt < %s -loop-vectorize -force-vector-interleave=2 -force-vector-width=4 -S | FileCheck %s
2
3target 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"
4
5; Make sure consecutive vector generates correct negative indices.
6; PR15882
7
8; CHECK: %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
9; CHECK: %offset.idx = sub i64 %startval, %index
10; CHECK: %[[a0:.+]] = add i64 %offset.idx, 0
11; CHECK: %[[a4:.+]] = add i64 %offset.idx, -4
12
13define i32 @reverse_induction_i64(i64 %startval, i32 * %ptr) {
14entry:
15  br label %for.body
16
17for.body:
18  %add.i7 = phi i64 [ %startval, %entry ], [ %add.i, %for.body ]
19  %i.06 = phi i32 [ 0, %entry ], [ %inc4, %for.body ]
20  %redux5 = phi i32 [ 0, %entry ], [ %inc.redux, %for.body ]
21  %add.i = add i64 %add.i7, -1
22  %kind_.i = getelementptr inbounds i32, i32* %ptr, i64 %add.i
23  %tmp.i1 = load i32, i32* %kind_.i, align 4
24  %inc.redux = add i32 %tmp.i1, %redux5
25  %inc4 = add i32 %i.06, 1
26  %exitcond = icmp ne i32 %inc4, 1024
27  br i1 %exitcond, label %for.body, label %loopend
28
29loopend:
30  ret i32 %inc.redux
31}
32
33; CHECK-LABEL: @reverse_induction_i128(
34; CHECK: %index = phi i128 [ 0, %vector.ph ], [ %index.next, %vector.body ]
35; CHECK: %offset.idx = sub i128 %startval, %index
36; CHECK: %[[a0:.+]] = add i128 %offset.idx, 0
37; CHECK: %[[a4:.+]] = add i128 %offset.idx, -4
38
39define i32 @reverse_induction_i128(i128 %startval, i32 * %ptr) {
40entry:
41  br label %for.body
42
43for.body:
44  %add.i7 = phi i128 [ %startval, %entry ], [ %add.i, %for.body ]
45  %i.06 = phi i32 [ 0, %entry ], [ %inc4, %for.body ]
46  %redux5 = phi i32 [ 0, %entry ], [ %inc.redux, %for.body ]
47  %add.i = add i128 %add.i7, -1
48  %kind_.i = getelementptr inbounds i32, i32* %ptr, i128 %add.i
49  %tmp.i1 = load i32, i32* %kind_.i, align 4
50  %inc.redux = add i32 %tmp.i1, %redux5
51  %inc4 = add i32 %i.06, 1
52  %exitcond = icmp ne i32 %inc4, 1024
53  br i1 %exitcond, label %for.body, label %loopend
54
55loopend:
56  ret i32 %inc.redux
57}
58
59; CHECK-LABEL: @reverse_induction_i16(
60; CHECK: %index = phi i32 [ 0, %vector.ph ], [ %index.next, %vector.body ]
61; CHECK: %offset.idx = sub i16 %startval, {{.*}}
62; CHECK: %[[a0:.+]] = add i16 %offset.idx, 0
63; CHECK: %[[a4:.+]] = add i16 %offset.idx, -4
64
65define i32 @reverse_induction_i16(i16 %startval, i32 * %ptr) {
66entry:
67  br label %for.body
68
69for.body:
70  %add.i7 = phi i16 [ %startval, %entry ], [ %add.i, %for.body ]
71  %i.06 = phi i32 [ 0, %entry ], [ %inc4, %for.body ]
72  %redux5 = phi i32 [ 0, %entry ], [ %inc.redux, %for.body ]
73  %add.i = add i16 %add.i7, -1
74  %kind_.i = getelementptr inbounds i32, i32* %ptr, i16 %add.i
75  %tmp.i1 = load i32, i32* %kind_.i, align 4
76  %inc.redux = add i32 %tmp.i1, %redux5
77  %inc4 = add i32 %i.06, 1
78  %exitcond = icmp ne i32 %inc4, 1024
79  br i1 %exitcond, label %for.body, label %loopend
80
81loopend:
82  ret i32 %inc.redux
83}
84
85
86@a = common global [1024 x i32] zeroinitializer, align 16
87
88; We incorrectly transformed this loop into an empty one because we left the
89; induction variable in i8 type and truncated the exit value 1024 to 0.
90; int a[1024];
91;
92; void fail() {
93;   int reverse_induction = 1023;
94;   unsigned char forward_induction = 0;
95;   while ((reverse_induction) >= 0) {
96;     forward_induction++;
97;     a[reverse_induction] = forward_induction;
98;     --reverse_induction;
99;   }
100; }
101
102; CHECK-LABEL: @reverse_forward_induction_i64_i8(
103; CHECK: %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
104; CHECK: %offset.idx = sub i64 1023, %index
105; CHECK: %[[a0:.+]] = add i64 %offset.idx, 0
106; CHECK: %[[a4:.+]] = add i64 %offset.idx, -4
107
108define void @reverse_forward_induction_i64_i8() {
109entry:
110  br label %while.body
111
112while.body:
113  %indvars.iv = phi i64 [ 1023, %entry ], [ %indvars.iv.next, %while.body ]
114  %forward_induction.05 = phi i8 [ 0, %entry ], [ %inc, %while.body ]
115  %inc = add i8 %forward_induction.05, 1
116  %conv = zext i8 %inc to i32
117  %arrayidx = getelementptr inbounds [1024 x i32], [1024 x i32]* @a, i64 0, i64 %indvars.iv
118  store i32 %conv, i32* %arrayidx, align 4
119  %indvars.iv.next = add i64 %indvars.iv, -1
120  %0 = trunc i64 %indvars.iv to i32
121  %cmp = icmp sgt i32 %0, 0
122  br i1 %cmp, label %while.body, label %while.end
123
124while.end:
125  ret void
126}
127
128; CHECK-LABEL: @reverse_forward_induction_i64_i8_signed(
129; CHECK: %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
130; CHECK: %offset.idx = sub i64 1023, %index
131; CHECK: %[[a0:.+]] = add i64 %offset.idx, 0
132; CHECK: %[[a4:.+]] = add i64 %offset.idx, -4
133
134define void @reverse_forward_induction_i64_i8_signed() {
135entry:
136  br label %while.body
137
138while.body:
139  %indvars.iv = phi i64 [ 1023, %entry ], [ %indvars.iv.next, %while.body ]
140  %forward_induction.05 = phi i8 [ -127, %entry ], [ %inc, %while.body ]
141  %inc = add i8 %forward_induction.05, 1
142  %conv = sext i8 %inc to i32
143  %arrayidx = getelementptr inbounds [1024 x i32], [1024 x i32]* @a, i64 0, i64 %indvars.iv
144  store i32 %conv, i32* %arrayidx, align 4
145  %indvars.iv.next = add i64 %indvars.iv, -1
146  %0 = trunc i64 %indvars.iv to i32
147  %cmp = icmp sgt i32 %0, 0
148  br i1 %cmp, label %while.body, label %while.end
149
150while.end:
151  ret void
152}
153