1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
3 ; RUN: opt < %s -passes='function(loop-vectorize,instcombine)' -force-vector-width=2 -force-vector-interleave=1 -debug-only=loop-vectorize -disable-output -print-after=instcombine 2>&1 | FileCheck %s
5 target datalayout = "e-m:e-i64:64-i128:128-n32:64-S128"
7 ; CHECK-LABEL: phi_two_incoming_values
8 ; CHECK: LV: Found an estimated cost of 1 for VF 2 For instruction: %i = phi i64 [ %i.next, %if.end ], [ 0, %entry ]
9 ; CHECK: LV: Found an estimated cost of 1 for VF 2 For instruction: %tmp5 = phi i32 [ %tmp1, %for.body ], [ %tmp4, %if.then ]
11 define void @phi_two_incoming_values(ptr noalias %a, ptr noalias %b, i64 %n) {
12 ; CHECK-LABEL: define void @phi_two_incoming_values(
13 ; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], i64 [[N:%.*]]) {
14 ; CHECK-NEXT: [[ENTRY:.*]]:
15 ; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[N]], 1
16 ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP0]], 2
17 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
18 ; CHECK: [[VECTOR_PH]]:
19 ; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[TMP0]], -2
20 ; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
21 ; CHECK: [[VECTOR_BODY]]:
22 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
23 ; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 [[INDEX]]
24 ; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x i32>, ptr [[TMP2]], align 4
25 ; CHECK-NEXT: [[TMP3:%.*]] = getelementptr inbounds i32, ptr [[B]], i64 [[INDEX]]
26 ; CHECK-NEXT: [[TMP4:%.*]] = icmp sgt <2 x i32> [[WIDE_LOAD]], zeroinitializer
27 ; CHECK-NEXT: [[TMP5:%.*]] = zext <2 x i1> [[TMP4]] to <2 x i32>
28 ; CHECK-NEXT: [[PREDPHI:%.*]] = add <2 x i32> [[WIDE_LOAD]], [[TMP5]]
29 ; CHECK-NEXT: store <2 x i32> [[PREDPHI]], ptr [[TMP3]], align 4
30 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
31 ; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
32 ; CHECK-NEXT: br i1 [[TMP6]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
33 ; CHECK: [[MIDDLE_BLOCK]]:
34 ; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[TMP0]], [[N_VEC]]
35 ; CHECK-NEXT: br i1 [[CMP_N]], label %[[FOR_END:.*]], label %[[SCALAR_PH]]
36 ; CHECK: [[SCALAR_PH]]:
37 ; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
38 ; CHECK-NEXT: br label %[[FOR_BODY:.*]]
39 ; CHECK: [[FOR_BODY]]:
40 ; CHECK-NEXT: [[I:%.*]] = phi i64 [ [[I_NEXT:%.*]], %[[IF_END:.*]] ], [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ]
41 ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 [[I]]
42 ; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[TMP0]], align 4
43 ; CHECK-NEXT: [[TMP3:%.*]] = getelementptr inbounds i32, ptr [[B]], i64 [[I]]
44 ; CHECK-NEXT: [[TMP4:%.*]] = icmp sgt i32 [[TMP2]], 0
45 ; CHECK-NEXT: br i1 [[TMP4]], label %[[IF_THEN:.*]], label %[[IF_END]]
47 ; CHECK-NEXT: [[TMP5:%.*]] = add nuw i32 [[TMP2]], 1
48 ; CHECK-NEXT: br label %[[IF_END]]
50 ; CHECK-NEXT: [[TMP6:%.*]] = phi i32 [ [[TMP2]], %[[FOR_BODY]] ], [ [[TMP5]], %[[IF_THEN]] ]
51 ; CHECK-NEXT: store i32 [[TMP6]], ptr [[TMP3]], align 4
52 ; CHECK-NEXT: [[I_NEXT]] = add i64 [[I]], 1
53 ; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[I]], [[N]]
54 ; CHECK-NEXT: br i1 [[COND]], label %[[FOR_END]], label %[[FOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
56 ; CHECK-NEXT: ret void
62 %i = phi i64 [ %i.next, %if.end ], [ 0, %entry ]
63 %tmp0 = getelementptr inbounds i32, ptr %a, i64 %i
64 %tmp1 = load i32, ptr %tmp0, align 4
65 %tmp2 = getelementptr inbounds i32, ptr %b, i64 %i
66 %tmp3 = icmp sgt i32 %tmp1, 0
67 br i1 %tmp3, label %if.then, label %if.end
70 %tmp4 = add i32 %tmp1, 1
74 %tmp5 = phi i32 [ %tmp1, %for.body ], [ %tmp4, %if.then ]
75 store i32 %tmp5, ptr %tmp2, align 4
76 %i.next = add i64 %i, 1
77 %cond = icmp eq i64 %i, %n
78 br i1 %cond, label %for.end, label %for.body
84 ; CHECK-LABEL: phi_three_incoming_values
85 ; CHECK: LV: Found an estimated cost of 1 for VF 2 For instruction: %i = phi i64 [ %i.next, %if.end ], [ 0, %entry ]
86 ; CHECK: LV: Found an estimated cost of 2 for VF 2 For instruction: %tmp8 = phi i32 [ 9, %for.body ], [ 3, %if.then ], [ %tmp7, %if.else ]
88 define void @phi_three_incoming_values(ptr noalias %a, ptr noalias %b, i64 %n) {
89 ; CHECK-LABEL: define void @phi_three_incoming_values(
90 ; CHECK-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], i64 [[N:%.*]]) {
91 ; CHECK-NEXT: [[ENTRY:.*]]:
92 ; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[N]], 1
93 ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP0]], 2
94 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
95 ; CHECK: [[VECTOR_PH]]:
96 ; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[TMP0]], -2
97 ; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
98 ; CHECK: [[VECTOR_BODY]]:
99 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
100 ; CHECK-NEXT: [[TMP3:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 [[INDEX]]
101 ; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x i32>, ptr [[TMP3]], align 4
102 ; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds i32, ptr [[B]], i64 [[INDEX]]
103 ; CHECK-NEXT: [[WIDE_LOAD2:%.*]] = load <2 x i32>, ptr [[TMP4]], align 4
104 ; CHECK-NEXT: [[TMP5:%.*]] = icmp sgt <2 x i32> [[WIDE_LOAD]], [[WIDE_LOAD2]]
105 ; CHECK-NEXT: [[TMP6:%.*]] = icmp slt <2 x i32> [[WIDE_LOAD]], splat (i32 20)
106 ; CHECK-NEXT: [[TMP7:%.*]] = icmp slt <2 x i32> [[WIDE_LOAD2]], splat (i32 4)
107 ; CHECK-NEXT: [[TMP8:%.*]] = select <2 x i1> [[TMP7]], <2 x i32> splat (i32 4), <2 x i32> splat (i32 5)
108 ; CHECK-NEXT: [[PREDPHI:%.*]] = select <2 x i1> [[TMP6]], <2 x i32> [[TMP8]], <2 x i32> splat (i32 3)
109 ; CHECK-NEXT: [[PREDPHI3:%.*]] = select <2 x i1> [[TMP5]], <2 x i32> [[PREDPHI]], <2 x i32> splat (i32 9)
110 ; CHECK-NEXT: store <2 x i32> [[PREDPHI3]], ptr [[TMP3]], align 4
111 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
112 ; CHECK-NEXT: [[TMP9:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
113 ; CHECK-NEXT: br i1 [[TMP9]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
114 ; CHECK: [[MIDDLE_BLOCK]]:
115 ; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[TMP0]], [[N_VEC]]
116 ; CHECK-NEXT: br i1 [[CMP_N]], label %[[FOR_END:.*]], label %[[SCALAR_PH]]
117 ; CHECK: [[SCALAR_PH]]:
118 ; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
119 ; CHECK-NEXT: br label %[[FOR_BODY:.*]]
120 ; CHECK: [[FOR_BODY]]:
121 ; CHECK-NEXT: [[I:%.*]] = phi i64 [ [[I_NEXT:%.*]], %[[IF_END:.*]] ], [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ]
122 ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds i32, ptr [[A]], i64 [[I]]
123 ; CHECK-NEXT: [[TMP3:%.*]] = load i32, ptr [[TMP0]], align 4
124 ; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds i32, ptr [[B]], i64 [[I]]
125 ; CHECK-NEXT: [[TMP5:%.*]] = load i32, ptr [[TMP4]], align 4
126 ; CHECK-NEXT: [[TMP6:%.*]] = icmp sgt i32 [[TMP3]], [[TMP5]]
127 ; CHECK-NEXT: br i1 [[TMP6]], label %[[IF_THEN:.*]], label %[[IF_END]]
128 ; CHECK: [[IF_THEN]]:
129 ; CHECK-NEXT: [[TMP7:%.*]] = icmp sgt i32 [[TMP3]], 19
130 ; CHECK-NEXT: br i1 [[TMP7]], label %[[IF_END]], label %[[IF_ELSE:.*]]
131 ; CHECK: [[IF_ELSE]]:
132 ; CHECK-NEXT: [[TMP8:%.*]] = icmp slt i32 [[TMP5]], 4
133 ; CHECK-NEXT: [[TMP9:%.*]] = select i1 [[TMP8]], i32 4, i32 5
134 ; CHECK-NEXT: br label %[[IF_END]]
136 ; CHECK-NEXT: [[TMP10:%.*]] = phi i32 [ 9, %[[FOR_BODY]] ], [ 3, %[[IF_THEN]] ], [ [[TMP9]], %[[IF_ELSE]] ]
137 ; CHECK-NEXT: store i32 [[TMP10]], ptr [[TMP0]], align 4
138 ; CHECK-NEXT: [[I_NEXT]] = add i64 [[I]], 1
139 ; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[I]], [[N]]
140 ; CHECK-NEXT: br i1 [[COND]], label %[[FOR_END]], label %[[FOR_BODY]], !llvm.loop [[LOOP5:![0-9]+]]
141 ; CHECK: [[FOR_END]]:
142 ; CHECK-NEXT: ret void
148 %i = phi i64 [ %i.next, %if.end ], [ 0, %entry ]
149 %tmp0 = getelementptr inbounds i32, ptr %a, i64 %i
150 %tmp1 = load i32, ptr %tmp0, align 4
151 %tmp2 = getelementptr inbounds i32, ptr %b, i64 %i
152 %tmp3 = load i32, ptr %tmp2, align 4
153 %tmp4 = icmp sgt i32 %tmp1, %tmp3
154 br i1 %tmp4, label %if.then, label %if.end
157 %tmp5 = icmp sgt i32 %tmp1, 19
158 br i1 %tmp5, label %if.end, label %if.else
161 %tmp6 = icmp slt i32 %tmp3, 4
162 %tmp7 = select i1 %tmp6, i32 4, i32 5
166 %tmp8 = phi i32 [ 9, %for.body ], [ 3, %if.then ], [ %tmp7, %if.else ]
167 store i32 %tmp8, ptr %tmp0, align 4
168 %i.next = add i64 %i, 1
169 %cond = icmp eq i64 %i, %n
170 br i1 %cond, label %for.end, label %for.body
176 ; Test case for https://github.com/llvm/llvm-project/issues/113794.
177 define i32 @red_phi_0(i32 %start, ptr %src) {
178 ; CHECK-LABEL: define i32 @red_phi_0(
179 ; CHECK-SAME: i32 [[START:%.*]], ptr [[SRC:%.*]]) {
180 ; CHECK-NEXT: [[ENTRY:.*:]]
181 ; CHECK-NEXT: br i1 false, label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
182 ; CHECK: [[VECTOR_PH]]:
183 ; CHECK-NEXT: [[TMP0:%.*]] = insertelement <2 x i32> <i32 poison, i32 0>, i32 [[START]], i64 0
184 ; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
185 ; CHECK: [[VECTOR_BODY]]:
186 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
187 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
188 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i64 [[INDEX_NEXT]], 100
189 ; CHECK-NEXT: br i1 [[TMP1]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP6:![0-9]+]]
190 ; CHECK: [[MIDDLE_BLOCK]]:
191 ; CHECK-NEXT: [[TMP2:%.*]] = call i32 @llvm.vector.reduce.add.v2i32(<2 x i32> [[TMP0]])
192 ; CHECK-NEXT: br i1 true, label %[[EXIT:.*]], label %[[SCALAR_PH]]
193 ; CHECK: [[SCALAR_PH]]:
194 ; CHECK-NEXT: br label %[[LOOP:.*]]
196 ; CHECK-NEXT: br i1 poison, label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP7:![0-9]+]]
198 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ poison, %[[LOOP]] ], [ [[TMP2]], %[[MIDDLE_BLOCK]] ]
199 ; CHECK-NEXT: ret i32 [[RES]]
205 %red = phi i32 [ %start, %entry ], [ %red.next, %loop ]
206 %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
207 %red.next = add i32 0, %red
208 %iv.next = add i64 %iv, 1
209 %exitcond.not = icmp eq i64 %iv.next, 100
210 br i1 %exitcond.not, label %exit, label %loop
213 %res = phi i32 [ %red.next, %loop ]