Skip to content

Commit 29bfcd0

Browse files
committed
[VPlan] Invert condition if needed when creating inner regions.
1 parent 1ff931e commit 29bfcd0

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,17 @@ static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB) {
420420
auto *PreheaderVPBB = HeaderVPB->getPredecessors()[0];
421421
auto *LatchVPBB = HeaderVPB->getPredecessors()[1];
422422

423+
// We are canonicalizing the successors of the latch when introducing the
424+
// region. We will exit the region of the latch condition is true; invert the
425+
// original condition if the original CFG branches to the header on true.
426+
if (!LatchVPBB->getSingleSuccessor() &&
427+
LatchVPBB->getSuccessors()[0] == HeaderVPB) {
428+
auto *Term = cast<VPBasicBlock>(LatchVPBB)->getTerminator();
429+
auto *Not = new VPInstruction(VPInstruction::Not, {Term->getOperand(0)});
430+
Not->insertBefore(Term);
431+
Term->setOperand(0, Not);
432+
}
433+
423434
VPBlockUtils::disconnectBlocks(PreheaderVPBB, HeaderVPB);
424435
VPBlockUtils::disconnectBlocks(LatchVPBB, HeaderVPB);
425436
VPBlockBase *Succ = LatchVPBB->getSingleSuccessor();

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ bool VPlanTransforms::tryToConvertVPInstructionsToVPRecipes(
5555
make_early_inc_range(make_range(VPBB->begin(), EndIter))) {
5656

5757
VPValue *VPV = Ingredient.getVPSingleValue();
58+
if (!VPV->getUnderlyingValue())
59+
continue;
60+
5861
Instruction *Inst = cast<Instruction>(VPV->getUnderlyingValue());
5962

6063
VPRecipeBase *NewRecipe = nullptr;

llvm/test/Transforms/LoopVectorize/outer-loop-inner-latch-successors.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
@A = common global [1024 x i64] zeroinitializer, align 16
55
@B = common global [1024 x i64] zeroinitializer, align 16
66

7-
; FIXME: The exit condition of the inner loop is incorrect when vectorizing.
87
define void @inner_latch_header_first_successor(i64 %N, i32 %c, i64 %M) {
98
; CHECK-LABEL: define void @inner_latch_header_first_successor(
109
; CHECK-SAME: i64 [[N:%.*]], i32 [[C:%.*]], i64 [[M:%.*]]) {
@@ -35,8 +34,9 @@ define void @inner_latch_header_first_successor(i64 %N, i32 %c, i64 %M) {
3534
; CHECK-NEXT: [[TMP3]] = add nsw <4 x i64> [[TMP2]], [[VEC_PHI4]]
3635
; CHECK-NEXT: [[TMP4]] = add nuw nsw <4 x i64> [[VEC_PHI]], splat (i64 1)
3736
; CHECK-NEXT: [[TMP5:%.*]] = icmp ne <4 x i64> [[TMP4]], [[BROADCAST_SPLAT2]]
38-
; CHECK-NEXT: [[TMP6:%.*]] = extractelement <4 x i1> [[TMP5]], i32 0
39-
; CHECK-NEXT: br i1 [[TMP6]], label %[[VECTOR_LATCH]], label %[[INNER3]]
37+
; CHECK-NEXT: [[TMP6:%.*]] = xor <4 x i1> [[TMP5]], splat (i1 true)
38+
; CHECK-NEXT: [[TMP9:%.*]] = extractelement <4 x i1> [[TMP6]], i32 0
39+
; CHECK-NEXT: br i1 [[TMP9]], label %[[VECTOR_LATCH]], label %[[INNER3]]
4040
; CHECK: [[VECTOR_LATCH]]:
4141
; CHECK-NEXT: [[VEC_PHI6:%.*]] = phi <4 x i64> [ [[TMP3]], %[[INNER3]] ]
4242
; CHECK-NEXT: call void @llvm.masked.scatter.v4i64.v4p0(<4 x i64> [[VEC_PHI6]], <4 x ptr> [[TMP0]], i32 4, <4 x i1> splat (i1 true))

0 commit comments

Comments
 (0)