1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
3 ; RUN: opt -passes=loop-vectorize -S -mtriple=aarch64 -mattr=+sve -debug-only=loop-vectorize \
4 ; RUN: -prefer-predicate-over-epilogue=scalar-epilogue < %s 2>&1 | FileCheck %s
6 target triple = "aarch64-unknown-linux-gnu"
8 ; CHECK-LABEL: LV: Checking a loop in 'pointer_induction_used_as_vector'
9 ; CHECK-NOT: LV: Found {{.*}} scalar instruction: %ptr.iv.2.next = getelementptr inbounds i8, ptr %ptr.iv.2, i64 1
11 ; CHECK: VPlan 'Initial VPlan for VF={vscale x 2},UF>=1' {
12 ; CHECK-NEXT: Live-in vp<[[VFxUF:%.+]]> = VF * UF
13 ; CHECK-NEXT: Live-in vp<[[VEC_TC:%.+]]> = vector-trip-count
14 ; CHECK-NEXT: Live-in ir<%N> = original trip-count
16 ; CHECK-NEXT: vector.ph:
17 ; CHECK-NEXT: Successor(s): vector loop
19 ; CHECK-NEXT: <x1> vector loop: {
20 ; CHECK-NEXT: vector.body:
21 ; CHECK-NEXT: EMIT vp<[[CAN_IV:%.+]]> = CANONICAL-INDUCTION
22 ; CHECK-NEXT: EMIT ir<%ptr.iv.1> = WIDEN-POINTER-INDUCTION ir<%start.1>, 8
23 ; CHECK-NEXT: EMIT ir<%ptr.iv.2> = WIDEN-POINTER-INDUCTION ir<%start.2>, 1
24 ; CHECK-NEXT: WIDEN-GEP Var[Inv] ir<%ptr.iv.2.next> = getelementptr inbounds ir<%ptr.iv.2>, ir<1>
25 ; CHECK-NEXT: vp<[[VEC_PTR:%.+]]> = vector-pointer ir<%ptr.iv.1>
26 ; CHECK-NEXT: WIDEN store vp<[[VEC_PTR]]>, ir<%ptr.iv.2.next>
27 ; CHECK-NEXT: vp<[[VEC_PTR2:%.+]]> = vector-pointer ir<%ptr.iv.2>
28 ; CHECK-NEXT: WIDEN ir<%lv> = load vp<[[VEC_PTR2]]>
29 ; CHECK-NEXT: WIDEN ir<%add> = add ir<%lv>, ir<1>
30 ; CHECK-NEXT: vp<[[VEC_PTR3:%.+]]> = vector-pointer ir<%ptr.iv.2>
31 ; CHECK-NEXT: WIDEN store vp<[[VEC_PTR3]]>, ir<%add>
32 ; CHECK-NEXT: EMIT vp<[[CAN_IV_NEXT:%.+]]> = add nuw vp<[[CAN_IV]]>, vp<[[VFxUF]]>
33 ; CHECK-NEXT: EMIT branch-on-count vp<[[CAN_IV_NEXT]]>, vp<[[VEC_TC]]>
34 ; CHECK-NEXT: No successors
37 ; In the test below the pointer phi %ptr.iv.2 is used as
38 ; 1. As a uniform address for the load, and
39 ; 2. Non-uniform use by the getelementptr which is stored. This requires the
41 define void @pointer_induction_used_as_vector(ptr noalias %start.1, ptr noalias %start.2, i64 %N) {
42 ; CHECK-LABEL: @pointer_induction_used_as_vector(
44 ; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
45 ; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[TMP0]], 2
46 ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N:%.*]], [[TMP1]]
47 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
49 ; CHECK-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
50 ; CHECK-NEXT: [[TMP3:%.*]] = mul i64 [[TMP2]], 2
51 ; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[N]], [[TMP3]]
52 ; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[N]], [[N_MOD_VF]]
53 ; CHECK-NEXT: [[TMP4:%.*]] = mul i64 [[N_VEC]], 8
54 ; CHECK-NEXT: [[IND_END:%.*]] = getelementptr i8, ptr [[START_1:%.*]], i64 [[TMP4]]
55 ; CHECK-NEXT: [[IND_END2:%.*]] = getelementptr i8, ptr [[START_2:%.*]], i64 [[N_VEC]]
56 ; CHECK-NEXT: [[TMP20:%.*]] = call i64 @llvm.vscale.i64()
57 ; CHECK-NEXT: [[TMP21:%.*]] = mul i64 [[TMP20]], 2
58 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
60 ; CHECK-NEXT: [[POINTER_PHI:%.*]] = phi ptr [ [[START_2]], [[VECTOR_PH]] ], [ [[PTR_IND:%.*]], [[VECTOR_BODY]] ]
61 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
62 ; CHECK-NEXT: [[TMP5:%.*]] = add i64 [[INDEX]], 0
63 ; CHECK-NEXT: [[TMP6:%.*]] = mul i64 [[TMP5]], 8
64 ; CHECK-NEXT: [[NEXT_GEP:%.*]] = getelementptr i8, ptr [[START_1]], i64 [[TMP6]]
65 ; CHECK-NEXT: [[TMP7:%.*]] = call i64 @llvm.vscale.i64()
66 ; CHECK-NEXT: [[TMP8:%.*]] = mul i64 [[TMP7]], 2
67 ; CHECK-NEXT: [[TMP9:%.*]] = mul i64 [[TMP8]], 1
68 ; CHECK-NEXT: [[TMP10:%.*]] = mul i64 1, [[TMP9]]
69 ; CHECK-NEXT: [[TMP11:%.*]] = mul i64 [[TMP8]], 0
70 ; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <vscale x 2 x i64> poison, i64 [[TMP11]], i64 0
71 ; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <vscale x 2 x i64> [[DOTSPLATINSERT]], <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
72 ; CHECK-NEXT: [[TMP12:%.*]] = call <vscale x 2 x i64> @llvm.experimental.stepvector.nxv2i64()
73 ; CHECK-NEXT: [[TMP13:%.*]] = add <vscale x 2 x i64> [[DOTSPLAT]], [[TMP12]]
74 ; CHECK-NEXT: [[VECTOR_GEP:%.*]] = mul <vscale x 2 x i64> [[TMP13]], shufflevector (<vscale x 2 x i64> insertelement (<vscale x 2 x i64> poison, i64 1, i64 0), <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer)
75 ; CHECK-NEXT: [[TMP14:%.*]] = getelementptr i8, ptr [[POINTER_PHI]], <vscale x 2 x i64> [[VECTOR_GEP]]
76 ; CHECK-NEXT: [[TMP15:%.*]] = getelementptr inbounds i8, <vscale x 2 x ptr> [[TMP14]], i64 1
77 ; CHECK-NEXT: [[TMP16:%.*]] = getelementptr ptr, ptr [[NEXT_GEP]], i32 0
78 ; CHECK-NEXT: store <vscale x 2 x ptr> [[TMP15]], ptr [[TMP16]], align 8
79 ; CHECK-NEXT: [[TMP17:%.*]] = extractelement <vscale x 2 x ptr> [[TMP14]], i32 0
80 ; CHECK-NEXT: [[TMP18:%.*]] = getelementptr i8, ptr [[TMP17]], i32 0
81 ; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 2 x i8>, ptr [[TMP18]], align 1
82 ; CHECK-NEXT: [[TMP19:%.*]] = add <vscale x 2 x i8> [[WIDE_LOAD]], shufflevector (<vscale x 2 x i8> insertelement (<vscale x 2 x i8> poison, i8 1, i64 0), <vscale x 2 x i8> poison, <vscale x 2 x i32> zeroinitializer)
83 ; CHECK-NEXT: store <vscale x 2 x i8> [[TMP19]], ptr [[TMP18]], align 1
84 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP21]]
85 ; CHECK-NEXT: [[PTR_IND]] = getelementptr i8, ptr [[POINTER_PHI]], i64 [[TMP10]]
86 ; CHECK-NEXT: [[TMP22:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
87 ; CHECK-NEXT: br i1 [[TMP22]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
88 ; CHECK: middle.block:
89 ; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[N]], [[N_VEC]]
90 ; CHECK-NEXT: br i1 [[CMP_N]], label [[EXIT:%.*]], label [[SCALAR_PH]]
92 ; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
93 ; CHECK-NEXT: [[BC_RESUME_VAL1:%.*]] = phi ptr [ [[IND_END]], [[MIDDLE_BLOCK]] ], [ [[START_1]], [[ENTRY]] ]
94 ; CHECK-NEXT: [[BC_RESUME_VAL3:%.*]] = phi ptr [ [[IND_END2]], [[MIDDLE_BLOCK]] ], [ [[START_2]], [[ENTRY]] ]
95 ; CHECK-NEXT: br label [[LOOP_BODY:%.*]]
97 ; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], [[LOOP_BODY]] ]
98 ; CHECK-NEXT: [[PTR_IV_1:%.*]] = phi ptr [ [[BC_RESUME_VAL1]], [[SCALAR_PH]] ], [ [[PTR_IV_1_NEXT:%.*]], [[LOOP_BODY]] ]
99 ; CHECK-NEXT: [[PTR_IV_2:%.*]] = phi ptr [ [[BC_RESUME_VAL3]], [[SCALAR_PH]] ], [ [[PTR_IV_2_NEXT:%.*]], [[LOOP_BODY]] ]
100 ; CHECK-NEXT: [[PTR_IV_1_NEXT]] = getelementptr inbounds ptr, ptr [[PTR_IV_1]], i64 1
101 ; CHECK-NEXT: [[PTR_IV_2_NEXT]] = getelementptr inbounds i8, ptr [[PTR_IV_2]], i64 1
102 ; CHECK-NEXT: store ptr [[PTR_IV_2_NEXT]], ptr [[PTR_IV_1]], align 8
103 ; CHECK-NEXT: [[LV:%.*]] = load i8, ptr [[PTR_IV_2]], align 1
104 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[LV]], 1
105 ; CHECK-NEXT: store i8 [[ADD]], ptr [[PTR_IV_2]], align 1
106 ; CHECK-NEXT: [[IV_NEXT]] = add nuw i64 [[IV]], 1
107 ; CHECK-NEXT: [[C:%.*]] = icmp ne i64 [[IV_NEXT]], [[N]]
108 ; CHECK-NEXT: br i1 [[C]], label [[LOOP_BODY]], label [[EXIT]], !llvm.loop [[LOOP3:![0-9]+]]
110 ; CHECK-NEXT: ret void
117 loop.body: ; preds = %loop.body, %entry
118 %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop.body ]
119 %ptr.iv.1 = phi ptr [ %start.1, %entry ], [ %ptr.iv.1.next, %loop.body ]
120 %ptr.iv.2 = phi ptr [ %start.2, %entry ], [ %ptr.iv.2.next, %loop.body ]
121 %ptr.iv.1.next = getelementptr inbounds ptr, ptr %ptr.iv.1, i64 1
122 %ptr.iv.2.next = getelementptr inbounds i8, ptr %ptr.iv.2, i64 1
123 store ptr %ptr.iv.2.next, ptr %ptr.iv.1, align 8
124 %lv = load i8, ptr %ptr.iv.2, align 1
126 store i8 %add, ptr %ptr.iv.2, align 1
127 %iv.next = add nuw i64 %iv, 1
128 %c = icmp ne i64 %iv.next, %N
129 br i1 %c, label %loop.body, label %exit, !llvm.loop !0
131 exit: ; preds = %loop.body
135 define void @pointer_induction(ptr noalias %start, i64 %N) {
136 ; CHECK-LABEL: @pointer_induction(
138 ; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[N:%.*]], 1
139 ; CHECK-NEXT: [[TMP1:%.*]] = call i64 @llvm.vscale.i64()
140 ; CHECK-NEXT: [[TMP2:%.*]] = mul i64 [[TMP1]], 2
141 ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[TMP0]], [[TMP2]]
142 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
144 ; CHECK-NEXT: [[TMP3:%.*]] = call i64 @llvm.vscale.i64()
145 ; CHECK-NEXT: [[TMP4:%.*]] = mul i64 [[TMP3]], 2
146 ; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[TMP0]], [[TMP4]]
147 ; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[TMP0]], [[N_MOD_VF]]
148 ; CHECK-NEXT: [[IND_END:%.*]] = getelementptr i8, ptr [[START:%.*]], i64 [[N_VEC]]
149 ; CHECK-NEXT: [[TMP16:%.*]] = call i64 @llvm.vscale.i64()
150 ; CHECK-NEXT: [[TMP17:%.*]] = mul i64 [[TMP16]], 2
151 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
152 ; CHECK: vector.body:
153 ; CHECK-NEXT: [[POINTER_PHI:%.*]] = phi ptr [ [[START]], [[VECTOR_PH]] ], [ [[PTR_IND:%.*]], [[VECTOR_BODY]] ]
154 ; CHECK-NEXT: [[INDEX2:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
155 ; CHECK-NEXT: [[TMP5:%.*]] = call i64 @llvm.vscale.i64()
156 ; CHECK-NEXT: [[TMP6:%.*]] = mul i64 [[TMP5]], 2
157 ; CHECK-NEXT: [[TMP7:%.*]] = mul i64 [[TMP6]], 1
158 ; CHECK-NEXT: [[TMP8:%.*]] = mul i64 1, [[TMP7]]
159 ; CHECK-NEXT: [[TMP9:%.*]] = mul i64 [[TMP6]], 0
160 ; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <vscale x 2 x i64> poison, i64 [[TMP9]], i64 0
161 ; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <vscale x 2 x i64> [[DOTSPLATINSERT]], <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
162 ; CHECK-NEXT: [[TMP10:%.*]] = call <vscale x 2 x i64> @llvm.experimental.stepvector.nxv2i64()
163 ; CHECK-NEXT: [[TMP11:%.*]] = add <vscale x 2 x i64> [[DOTSPLAT]], [[TMP10]]
164 ; CHECK-NEXT: [[VECTOR_GEP:%.*]] = mul <vscale x 2 x i64> [[TMP11]], shufflevector (<vscale x 2 x i64> insertelement (<vscale x 2 x i64> poison, i64 1, i64 0), <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer)
165 ; CHECK-NEXT: [[TMP12:%.*]] = getelementptr i8, ptr [[POINTER_PHI]], <vscale x 2 x i64> [[VECTOR_GEP]]
166 ; CHECK-NEXT: [[TMP13:%.*]] = extractelement <vscale x 2 x ptr> [[TMP12]], i32 0
167 ; CHECK-NEXT: [[TMP14:%.*]] = getelementptr i8, ptr [[TMP13]], i32 0
168 ; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <vscale x 2 x i8>, ptr [[TMP14]], align 1
169 ; CHECK-NEXT: [[TMP15:%.*]] = add <vscale x 2 x i8> [[WIDE_LOAD]], shufflevector (<vscale x 2 x i8> insertelement (<vscale x 2 x i8> poison, i8 1, i64 0), <vscale x 2 x i8> poison, <vscale x 2 x i32> zeroinitializer)
170 ; CHECK-NEXT: store <vscale x 2 x i8> [[TMP15]], ptr [[TMP14]], align 1
171 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX2]], [[TMP17]]
172 ; CHECK-NEXT: [[PTR_IND]] = getelementptr i8, ptr [[POINTER_PHI]], i64 [[TMP8]]
173 ; CHECK-NEXT: [[TMP18:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
174 ; CHECK-NEXT: br i1 [[TMP18]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
175 ; CHECK: middle.block:
176 ; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[TMP0]], [[N_VEC]]
177 ; CHECK-NEXT: br i1 [[CMP_N]], label [[END:%.*]], label [[SCALAR_PH]]
179 ; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi ptr [ [[IND_END]], [[MIDDLE_BLOCK]] ], [ [[START]], [[ENTRY:%.*]] ]
180 ; CHECK-NEXT: [[BC_RESUME_VAL1:%.*]] = phi i64 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY]] ]
181 ; CHECK-NEXT: br label [[FOR_BODY:%.*]]
183 ; CHECK-NEXT: [[PTR_PHI:%.*]] = phi ptr [ [[PTR_PHI_NEXT:%.*]], [[FOR_BODY]] ], [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ]
184 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ [[INDEX_NXT:%.*]], [[FOR_BODY]] ], [ [[BC_RESUME_VAL1]], [[SCALAR_PH]] ]
185 ; CHECK-NEXT: [[INDEX_NXT]] = add i64 [[INDEX]], 1
186 ; CHECK-NEXT: [[TMP19:%.*]] = load i8, ptr [[PTR_PHI]], align 1
187 ; CHECK-NEXT: [[ADD:%.*]] = add i8 [[TMP19]], 1
188 ; CHECK-NEXT: store i8 [[ADD]], ptr [[PTR_PHI]], align 1
189 ; CHECK-NEXT: [[PTR_PHI_NEXT]] = getelementptr inbounds i8, ptr [[PTR_PHI]], i64 1
190 ; CHECK-NEXT: [[CMP_I_NOT:%.*]] = icmp eq ptr [[PTR_PHI_NEXT]], [[START]]
191 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[INDEX]], [[N]]
192 ; CHECK-NEXT: br i1 [[CMP]], label [[FOR_BODY]], label [[END]], !llvm.loop [[LOOP5:![0-9]+]]
194 ; CHECK-NEXT: ret void
200 %ptr.phi = phi ptr [ %ptr.phi.next, %for.body ], [ %start, %entry ]
201 %index = phi i64 [ %index_nxt, %for.body ], [ 0, %entry ]
202 %index_nxt = add i64 %index, 1
203 %0 = load i8, ptr %ptr.phi, align 1
205 store i8 %add, ptr %ptr.phi
206 %ptr.phi.next = getelementptr inbounds i8, ptr %ptr.phi, i64 1
207 %cmp.i.not = icmp eq ptr %ptr.phi.next, %start
208 %cmp = icmp ult i64 %index, %N
209 br i1 %cmp, label %for.body, label %end, !llvm.loop !0
215 attributes #0 = {"target-features"="+sve"}
217 !0 = distinct !{!0, !1, !2, !3}
218 !1 = !{!"llvm.loop.interleave.count", i32 1}
219 !2 = !{!"llvm.loop.vectorize.width", i32 2}
220 !3 = !{!"llvm.loop.vectorize.scalable.enable", i1 true}
222 !5 = distinct !{ !5, !6 }
223 !6 = distinct !{ !7 }
224 !7 = distinct !{ !7, !6 }