1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2 ; RUN: opt < %s -force-vector-width=2 -force-vector-interleave=2 -passes=loop-vectorize -S | FileCheck %s
4 target datalayout = "e-m:e-i64:64-i128:128-n32:64-S128"
7 ; This test ensures that we don't generate trivially dead instructions prior to
8 ; instruction simplification. We don't need to generate instructions
9 ; corresponding to the original induction variable update or branch condition,
10 ; since we rewrite the loop structure.
12 define i64 @dead_instructions_01(ptr %a, i64 %n) {
13 ; CHECK-LABEL: define i64 @dead_instructions_01(
14 ; CHECK-SAME: ptr [[A:%.*]], i64 [[N:%.*]]) {
15 ; CHECK-NEXT: [[ENTRY:.*]]:
16 ; CHECK-NEXT: [[SMAX:%.*]] = call i64 @llvm.smax.i64(i64 [[N]], i64 1)
17 ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[SMAX]], 4
18 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
19 ; CHECK: [[VECTOR_PH]]:
20 ; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[SMAX]], 4
21 ; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[SMAX]], [[N_MOD_VF]]
22 ; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
23 ; CHECK: [[VECTOR_BODY]]:
24 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
25 ; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <2 x i64> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP6:%.*]], %[[VECTOR_BODY]] ]
26 ; CHECK-NEXT: [[VEC_PHI1:%.*]] = phi <2 x i64> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP7:%.*]], %[[VECTOR_BODY]] ]
27 ; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[INDEX]], 0
28 ; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i64, ptr [[A]], i64 [[TMP0]]
29 ; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds i64, ptr [[TMP2]], i32 0
30 ; CHECK-NEXT: [[TMP5:%.*]] = getelementptr inbounds i64, ptr [[TMP2]], i32 2
31 ; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x i64>, ptr [[TMP4]], align 8
32 ; CHECK-NEXT: [[WIDE_LOAD2:%.*]] = load <2 x i64>, ptr [[TMP5]], align 8
33 ; CHECK-NEXT: [[TMP6]] = add <2 x i64> [[WIDE_LOAD]], [[VEC_PHI]]
34 ; CHECK-NEXT: [[TMP7]] = add <2 x i64> [[WIDE_LOAD2]], [[VEC_PHI1]]
35 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
36 ; CHECK-NEXT: [[TMP8:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
37 ; CHECK-NEXT: br i1 [[TMP8]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
38 ; CHECK: [[MIDDLE_BLOCK]]:
39 ; CHECK-NEXT: [[BIN_RDX:%.*]] = add <2 x i64> [[TMP7]], [[TMP6]]
40 ; CHECK-NEXT: [[TMP9:%.*]] = call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> [[BIN_RDX]])
41 ; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 [[SMAX]], [[N_VEC]]
42 ; CHECK-NEXT: br i1 [[CMP_N]], label %[[FOR_END:.*]], label %[[SCALAR_PH]]
43 ; CHECK: [[SCALAR_PH]]:
44 ; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
45 ; CHECK-NEXT: [[BC_MERGE_RDX:%.*]] = phi i64 [ [[TMP9]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
46 ; CHECK-NEXT: br label %[[FOR_BODY:.*]]
47 ; CHECK: [[FOR_BODY]]:
48 ; CHECK-NEXT: [[I:%.*]] = phi i64 [ [[I_NEXT:%.*]], %[[FOR_BODY]] ], [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ]
49 ; CHECK-NEXT: [[R:%.*]] = phi i64 [ [[TMP4:%.*]], %[[FOR_BODY]] ], [ [[BC_MERGE_RDX]], %[[SCALAR_PH]] ]
50 ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds i64, ptr [[A]], i64 [[I]]
51 ; CHECK-NEXT: [[TMP2:%.*]] = load i64, ptr [[TMP0]], align 8
52 ; CHECK-NEXT: [[TMP4]] = add i64 [[TMP2]], [[R]]
53 ; CHECK-NEXT: [[I_NEXT]] = add nuw nsw i64 [[I]], 1
54 ; CHECK-NEXT: [[COND:%.*]] = icmp slt i64 [[I_NEXT]], [[N]]
55 ; CHECK-NEXT: br i1 [[COND]], label %[[FOR_BODY]], label %[[FOR_END]], !llvm.loop [[LOOP3:![0-9]+]]
57 ; CHECK-NEXT: [[TMP5:%.*]] = phi i64 [ [[TMP4]], %[[FOR_BODY]] ], [ [[TMP9]], %[[MIDDLE_BLOCK]] ]
58 ; CHECK-NEXT: ret i64 [[TMP5]]
64 %i = phi i64 [ %i.next, %for.body ], [ 0, %entry ]
65 %r = phi i64 [ %tmp2, %for.body ], [ 0, %entry ]
66 %tmp0 = getelementptr inbounds i64, ptr %a, i64 %i
67 %tmp1 = load i64, ptr %tmp0, align 8
68 %tmp2 = add i64 %tmp1, %r
69 %i.next = add nuw nsw i64 %i, 1
70 %cond = icmp slt i64 %i.next, %n
71 br i1 %cond, label %for.body, label %for.end
74 %tmp3 = phi i64 [ %tmp2, %for.body ]
80 ; This test ensures that the primary induction is not considered dead when
81 ; acting as the 'add' of another induction, and otherwise feeding only its own
82 ; 'add' (recognized earlier as 'dead'), when the tail of the loop is folded by
83 ; masking. Such masking uses the primary induction.
85 define void @pr47390(ptr %a) {
86 ; CHECK-LABEL: define void @pr47390(
87 ; CHECK-SAME: ptr [[A:%.*]]) {
88 ; CHECK-NEXT: [[ENTRY:.*]]:
89 ; CHECK-NEXT: br i1 false, label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
90 ; CHECK: [[VECTOR_PH]]:
91 ; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
92 ; CHECK: [[VECTOR_BODY]]:
93 ; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
94 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
95 ; CHECK-NEXT: [[TMP0:%.*]] = icmp eq i32 [[INDEX_NEXT]], 8
96 ; CHECK-NEXT: br i1 [[TMP0]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP4:![0-9]+]]
97 ; CHECK: [[MIDDLE_BLOCK]]:
98 ; CHECK-NEXT: br i1 true, label %[[EXIT:.*]], label %[[SCALAR_PH]]
99 ; CHECK: [[SCALAR_PH]]:
100 ; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ 8, %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
101 ; CHECK-NEXT: [[BC_RESUME_VAL1:%.*]] = phi i32 [ 7, %[[MIDDLE_BLOCK]] ], [ -1, %[[ENTRY]] ]
102 ; CHECK-NEXT: [[BC_RESUME_VAL2:%.*]] = phi i32 [ 9, %[[MIDDLE_BLOCK]] ], [ 1, %[[ENTRY]] ]
103 ; CHECK-NEXT: br label %[[LOOP:.*]]
105 ; CHECK-NEXT: ret void
107 ; CHECK-NEXT: [[PRIMARY:%.*]] = phi i32 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[PRIMARY_ADD:%.*]], %[[LOOP]] ]
108 ; CHECK-NEXT: [[USE_PRIMARY:%.*]] = phi i32 [ [[BC_RESUME_VAL1]], %[[SCALAR_PH]] ], [ [[PRIMARY]], %[[LOOP]] ]
109 ; CHECK-NEXT: [[SECONDARY:%.*]] = phi i32 [ [[BC_RESUME_VAL2]], %[[SCALAR_PH]] ], [ [[SECONDARY_ADD:%.*]], %[[LOOP]] ]
110 ; CHECK-NEXT: [[PRIMARY_ADD]] = add i32 [[PRIMARY]], 1
111 ; CHECK-NEXT: [[SECONDARY_ADD]] = add i32 [[SECONDARY]], 1
112 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i32, ptr [[A]], i32 [[SECONDARY]]
113 ; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 8
114 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[SECONDARY]], 5
115 ; CHECK-NEXT: br i1 [[CMP]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP5:![0-9]+]]
124 %primary = phi i32 [ 0, %entry ], [ %primary_add, %loop ]
125 %use_primary = phi i32 [ -1, %entry ], [ %primary, %loop ]
126 %secondary = phi i32 [ 1, %entry ], [ %secondary_add, %loop ]
127 %primary_add = add i32 %primary, 1
128 %secondary_add = add i32 %secondary, 1
129 %gep = getelementptr inbounds i32, ptr %a, i32 %secondary
130 %load = load i32, ptr %gep, align 8
131 %cmp = icmp eq i32 %secondary, 5
132 br i1 %cmp, label %exit, label %loop
135 ; Test with a dead load and dead vector poiner.
136 define void @dead_load_and_vector_pointer(ptr %a, ptr %b) {
137 ; CHECK-LABEL: define void @dead_load_and_vector_pointer(
138 ; CHECK-SAME: ptr [[A:%.*]], ptr [[B:%.*]]) {
139 ; CHECK-NEXT: [[ENTRY:.*]]:
140 ; CHECK-NEXT: br i1 false, label %[[SCALAR_PH:.*]], label %[[VECTOR_MEMCHECK:.*]]
141 ; CHECK: [[VECTOR_MEMCHECK]]:
142 ; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[A]], i64 516
143 ; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i8, ptr [[B]], i64 516
144 ; CHECK-NEXT: [[BOUND0:%.*]] = icmp ult ptr [[A]], [[SCEVGEP1]]
145 ; CHECK-NEXT: [[BOUND1:%.*]] = icmp ult ptr [[B]], [[SCEVGEP]]
146 ; CHECK-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
147 ; CHECK-NEXT: br i1 [[FOUND_CONFLICT]], label %[[SCALAR_PH]], label %[[VECTOR_PH:.*]]
148 ; CHECK: [[VECTOR_PH]]:
149 ; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
150 ; CHECK: [[VECTOR_BODY]]:
151 ; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
152 ; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[INDEX]], 0
153 ; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i32, ptr [[A]], i32 [[TMP0]]
154 ; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds i32, ptr [[TMP2]], i32 0
155 ; CHECK-NEXT: [[TMP5:%.*]] = getelementptr inbounds i32, ptr [[TMP2]], i32 2
156 ; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <2 x i32>, ptr [[TMP4]], align 8, !alias.scope [[META6:![0-9]+]], !noalias [[META9:![0-9]+]]
157 ; CHECK-NEXT: [[WIDE_LOAD2:%.*]] = load <2 x i32>, ptr [[TMP5]], align 8, !alias.scope [[META6]], !noalias [[META9]]
158 ; CHECK-NEXT: [[TMP6:%.*]] = add <2 x i32> [[WIDE_LOAD]], splat (i32 1)
159 ; CHECK-NEXT: [[TMP7:%.*]] = add <2 x i32> [[WIDE_LOAD2]], splat (i32 1)
160 ; CHECK-NEXT: store <2 x i32> [[TMP6]], ptr [[TMP4]], align 4, !alias.scope [[META6]], !noalias [[META9]]
161 ; CHECK-NEXT: store <2 x i32> [[TMP7]], ptr [[TMP5]], align 4, !alias.scope [[META6]], !noalias [[META9]]
162 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
163 ; CHECK-NEXT: [[TMP12:%.*]] = icmp eq i32 [[INDEX_NEXT]], 128
164 ; CHECK-NEXT: br i1 [[TMP12]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP11:![0-9]+]]
165 ; CHECK: [[MIDDLE_BLOCK]]:
166 ; CHECK-NEXT: br i1 false, label %[[EXIT:.*]], label %[[SCALAR_PH]]
167 ; CHECK: [[SCALAR_PH]]:
168 ; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ 128, %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ], [ 0, %[[VECTOR_MEMCHECK]] ]
169 ; CHECK-NEXT: br label %[[LOOP:.*]]
171 ; CHECK-NEXT: ret void
173 ; CHECK-NEXT: [[PRIMARY:%.*]] = phi i32 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[PRIMARY_ADD:%.*]], %[[LOOP]] ]
174 ; CHECK-NEXT: [[PRIMARY_ADD]] = add i32 [[PRIMARY]], 1
175 ; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i32, ptr [[A]], i32 [[PRIMARY]]
176 ; CHECK-NEXT: [[LOAD:%.*]] = load i32, ptr [[GEP]], align 8
177 ; CHECK-NEXT: [[ADD:%.*]] = add i32 [[LOAD]], 1
178 ; CHECK-NEXT: store i32 [[ADD]], ptr [[GEP]], align 4
179 ; CHECK-NEXT: [[GEP_B:%.*]] = getelementptr inbounds i32, ptr [[B]], i32 [[PRIMARY]]
180 ; CHECK-NEXT: [[LOAD2:%.*]] = load i32, ptr [[GEP_B]], align 4
181 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[PRIMARY]], 128
182 ; CHECK-NEXT: br i1 [[CMP]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP12:![0-9]+]]
191 %primary = phi i32 [ 0, %entry ], [ %primary_add, %loop ]
192 %primary_add = add i32 %primary, 1
193 %gep = getelementptr inbounds i32, ptr %a, i32 %primary
194 %load = load i32, ptr %gep, align 8
195 %add = add i32 %load, 1
196 store i32 %add, ptr %gep
197 %gep.b = getelementptr inbounds i32, ptr %b, i32 %primary
198 %load2 = load i32, ptr %gep.b
199 %cmp = icmp eq i32 %primary, 128
200 br i1 %cmp, label %exit, label %loop
203 ; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
204 ; CHECK: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
205 ; CHECK: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
206 ; CHECK: [[LOOP3]] = distinct !{[[LOOP3]], [[META2]], [[META1]]}
207 ; CHECK: [[LOOP4]] = distinct !{[[LOOP4]], [[META1]], [[META2]]}
208 ; CHECK: [[LOOP5]] = distinct !{[[LOOP5]], [[META2]], [[META1]]}
209 ; CHECK: [[META6]] = !{[[META7:![0-9]+]]}
210 ; CHECK: [[META7]] = distinct !{[[META7]], [[META8:![0-9]+]]}
211 ; CHECK: [[META8]] = distinct !{[[META8]], !"LVerDomain"}
212 ; CHECK: [[META9]] = !{[[META10:![0-9]+]]}
213 ; CHECK: [[META10]] = distinct !{[[META10]], [[META8]]}
214 ; CHECK: [[LOOP11]] = distinct !{[[LOOP11]], [[META1]], [[META2]]}
215 ; CHECK: [[LOOP12]] = distinct !{[[LOOP12]], [[META1]]}