Skip to content

Commit 3388de1

Browse files
committedMar 8, 2017
[LV] Select legal insert point when fixing first-order recurrences
Because IRBuilder performs constant-folding, it's not guaranteed that an instruction in the original loop map to an instruction in the vector loop. It could map to a constant vector instead. The handling of first-order recurrences was incorrectly making this assumption when setting the IRBuilder's insert point. llvm-svn: 297302
1 parent 5698b2a commit 3388de1

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed
 

‎llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4284,15 +4284,17 @@ void InnerLoopVectorizer::fixFirstOrderRecurrence(PHINode *Phi) {
42844284
auto *VecPhi = Builder.CreatePHI(VectorInit->getType(), 2, "vector.recur");
42854285
VecPhi->addIncoming(VectorInit, LoopVectorPreHeader);
42864286

4287-
// Get the vectorized previous value. We ensured the previous values was an
4288-
// instruction when detecting the recurrence.
4287+
// Get the vectorized previous value.
42894288
auto &PreviousParts = getVectorValue(Previous);
42904289

4291-
// Set the insertion point to be after this instruction. We ensured the
4292-
// previous value dominated all uses of the phi when detecting the
4293-
// recurrence.
4294-
Builder.SetInsertPoint(
4295-
&*++BasicBlock::iterator(cast<Instruction>(PreviousParts[UF - 1])));
4290+
// Set the insertion point after the previous value if it is an instruction.
4291+
// Note that the previous value may have been constant-folded so it is not
4292+
// guaranteed to be an instruction in the vector loop.
4293+
if (LI->getLoopFor(LoopVectorBody)->isLoopInvariant(PreviousParts[UF - 1]))
4294+
Builder.SetInsertPoint(&*LoopVectorBody->getFirstInsertionPt());
4295+
else
4296+
Builder.SetInsertPoint(
4297+
&*++BasicBlock::iterator(cast<Instruction>(PreviousParts[UF - 1])));
42964298

42974299
// We will construct a vector for the recurrence by combining the values for
42984300
// the current and previous iterations. This is the required shuffle mask.

‎llvm/test/Transforms/LoopVectorize/AArch64/first-order-recurrence.ll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,25 @@ scalar.body:
327327
for.end:
328328
ret void
329329
}
330+
331+
; UNROLL-NO-IC-LABEL: @constant_folded_previous_value(
332+
; UNROLL-NO-IC: vector.body:
333+
; UNROLL-NO-IC: [[VECTOR_RECUR:%.*]] = phi <4 x i64> [ <i64 undef, i64 undef, i64 undef, i64 0>, %vector.ph ], [ <i64 1, i64 1, i64 1, i64 1>, %vector.body ]
334+
; UNROLL-NO-IC-NEXT: [[TMP0:%.*]] = shufflevector <4 x i64> [[VECTOR_RECUR]], <4 x i64> <i64 1, i64 1, i64 1, i64 1>, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
335+
; UNROLL-NO-IC: br i1 {{.*}}, label %middle.block, label %vector.body
336+
;
337+
define void @constant_folded_previous_value() {
338+
entry:
339+
br label %scalar.body
340+
341+
scalar.body:
342+
%i = phi i64 [ 0, %entry ], [ %i.next, %scalar.body ]
343+
%tmp2 = phi i64 [ 0, %entry ], [ %tmp3, %scalar.body ]
344+
%tmp3 = add i64 0, 1
345+
%i.next = add nuw nsw i64 %i, 1
346+
%cond = icmp eq i64 %i.next, undef
347+
br i1 %cond, label %for.end, label %scalar.body
348+
349+
for.end:
350+
ret void
351+
}

0 commit comments

Comments
 (0)