Skip to content

Commit f605097

Browse files
committed
!fixup move new code to VPlanConstruction.cpp
1 parent 99ad49e commit f605097

File tree

3 files changed

+99
-77
lines changed

3 files changed

+99
-77
lines changed

llvm/lib/Transforms/Vectorize/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_llvm_component_library(LLVMVectorize
2222
VectorCombine.cpp
2323
VPlan.cpp
2424
VPlanAnalysis.cpp
25+
VPlanConstruction.cpp
2526
VPlanHCFGBuilder.cpp
2627
VPlanRecipes.cpp
2728
VPlanSLP.cpp
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//===-- VPlanConstruction.cpp - Transforms for initial VPlan construction -===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file implements transforms for initial VPlan construction
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "LoopVectorizationPlanner.h"
15+
#include "VPlan.h"
16+
#include "VPlanCFG.h"
17+
#include "VPlanTransforms.h"
18+
#include "llvm/Analysis/LoopInfo.h"
19+
#include "llvm/Analysis/ScalarEvolution.h"
20+
21+
using namespace llvm;
22+
23+
void VPlanTransforms::introduceTopLevelVectorLoopRegion(
24+
VPlan &Plan, Type *InductionTy, PredicatedScalarEvolution &PSE,
25+
bool RequiresScalarEpilogueCheck, bool TailFolded, Loop *TheLoop) {
26+
// TODO: Generalize to introduce all loop regions.
27+
auto *HeaderVPBB = cast<VPBasicBlock>(Plan.getEntry()->getSingleSuccessor());
28+
VPBlockUtils::disconnectBlocks(Plan.getEntry(), HeaderVPBB);
29+
30+
VPBasicBlock *OriginalLatch =
31+
cast<VPBasicBlock>(HeaderVPBB->getSinglePredecessor());
32+
VPBlockUtils::disconnectBlocks(OriginalLatch, HeaderVPBB);
33+
VPBasicBlock *VecPreheader = Plan.createVPBasicBlock("vector.ph");
34+
VPBlockUtils::connectBlocks(Plan.getEntry(), VecPreheader);
35+
assert(OriginalLatch->getNumSuccessors() == 0 && "expected no predecessors");
36+
37+
// Create SCEV and VPValue for the trip count.
38+
// We use the symbolic max backedge-taken-count, which works also when
39+
// vectorizing loops with uncountable early exits.
40+
const SCEV *BackedgeTakenCountSCEV = PSE.getSymbolicMaxBackedgeTakenCount();
41+
assert(!isa<SCEVCouldNotCompute>(BackedgeTakenCountSCEV) &&
42+
"Invalid loop count");
43+
ScalarEvolution &SE = *PSE.getSE();
44+
const SCEV *TripCount = SE.getTripCountFromExitCount(BackedgeTakenCountSCEV,
45+
InductionTy, TheLoop);
46+
Plan.setTripCount(
47+
vputils::getOrCreateVPValueForSCEVExpr(Plan, TripCount, SE));
48+
49+
// Create VPRegionBlock, with existing header and new empty latch block, to be
50+
// filled
51+
VPBasicBlock *LatchVPBB = Plan.createVPBasicBlock("vector.latch");
52+
VPBlockUtils::insertBlockAfter(LatchVPBB, OriginalLatch);
53+
auto *TopRegion = Plan.createVPRegionBlock(
54+
HeaderVPBB, LatchVPBB, "vector loop", false /*isReplicator*/);
55+
for (VPBlockBase *VPBB : vp_depth_first_shallow(HeaderVPBB))
56+
VPBB->setParent(TopRegion);
57+
58+
VPBlockUtils::insertBlockAfter(TopRegion, VecPreheader);
59+
VPBasicBlock *MiddleVPBB = Plan.createVPBasicBlock("middle.block");
60+
VPBlockUtils::insertBlockAfter(MiddleVPBB, TopRegion);
61+
62+
VPBasicBlock *ScalarPH = Plan.createVPBasicBlock("scalar.ph");
63+
VPBlockUtils::connectBlocks(ScalarPH, Plan.getScalarHeader());
64+
if (!RequiresScalarEpilogueCheck) {
65+
VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH);
66+
return;
67+
}
68+
69+
// If needed, add a check in the middle block to see if we have completed
70+
// all of the iterations in the first vector loop. Three cases:
71+
// 1) If (N - N%VF) == N, then we *don't* need to run the remainder.
72+
// Thus if tail is to be folded, we know we don't need to run the
73+
// remainder and we can set the condition to true.
74+
// 2) If we require a scalar epilogue, there is no conditional branch as
75+
// we unconditionally branch to the scalar preheader. Do nothing.
76+
// 3) Otherwise, construct a runtime check.
77+
BasicBlock *IRExitBlock = TheLoop->getUniqueLatchExitBlock();
78+
auto *VPExitBlock = Plan.getExitBlock(IRExitBlock);
79+
// The connection order corresponds to the operands of the conditional branch.
80+
VPBlockUtils::insertBlockAfter(VPExitBlock, MiddleVPBB);
81+
VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH);
82+
83+
auto *ScalarLatchTerm = TheLoop->getLoopLatch()->getTerminator();
84+
// Here we use the same DebugLoc as the scalar loop latch terminator instead
85+
// of the corresponding compare because they may have ended up with
86+
// different line numbers and we want to avoid awkward line stepping while
87+
// debugging. Eg. if the compare has got a line number inside the loop.
88+
VPBuilder Builder(MiddleVPBB);
89+
VPValue *Cmp =
90+
TailFolded
91+
? Plan.getOrAddLiveIn(ConstantInt::getTrue(
92+
IntegerType::getInt1Ty(TripCount->getType()->getContext())))
93+
: Builder.createICmp(CmpInst::ICMP_EQ, Plan.getTripCount(),
94+
&Plan.getVectorTripCount(),
95+
ScalarLatchTerm->getDebugLoc(), "cmp.n");
96+
Builder.createNaryOp(VPInstruction::BranchOnCond, {Cmp},
97+
ScalarLatchTerm->getDebugLoc());
98+
}

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -32,83 +32,6 @@
3232

3333
using namespace llvm;
3434

35-
void VPlanTransforms::introduceTopLevelVectorLoopRegion(
36-
VPlan &Plan, Type *InductionTy, PredicatedScalarEvolution &PSE,
37-
bool RequiresScalarEpilogueCheck, bool TailFolded, Loop *TheLoop) {
38-
// TODO: Generalize to introduce all loop regions.
39-
auto *HeaderVPBB = cast<VPBasicBlock>(Plan.getEntry()->getSingleSuccessor());
40-
VPBlockUtils::disconnectBlocks(Plan.getEntry(), HeaderVPBB);
41-
42-
VPBasicBlock *OriginalLatch =
43-
cast<VPBasicBlock>(HeaderVPBB->getSinglePredecessor());
44-
VPBlockUtils::disconnectBlocks(OriginalLatch, HeaderVPBB);
45-
VPBasicBlock *VecPreheader = Plan.createVPBasicBlock("vector.ph");
46-
VPBlockUtils::connectBlocks(Plan.getEntry(), VecPreheader);
47-
assert(OriginalLatch->getNumSuccessors() == 0 && "expected no predecessors");
48-
49-
// Create SCEV and VPValue for the trip count.
50-
// We use the symbolic max backedge-taken-count, which works also when
51-
// vectorizing loops with uncountable early exits.
52-
const SCEV *BackedgeTakenCountSCEV = PSE.getSymbolicMaxBackedgeTakenCount();
53-
assert(!isa<SCEVCouldNotCompute>(BackedgeTakenCountSCEV) &&
54-
"Invalid loop count");
55-
ScalarEvolution &SE = *PSE.getSE();
56-
const SCEV *TripCount = SE.getTripCountFromExitCount(BackedgeTakenCountSCEV,
57-
InductionTy, TheLoop);
58-
Plan.setTripCount(
59-
vputils::getOrCreateVPValueForSCEVExpr(Plan, TripCount, SE));
60-
61-
// Create VPRegionBlock, with existing header and new empty latch block, to be
62-
// filled
63-
VPBasicBlock *LatchVPBB = Plan.createVPBasicBlock("vector.latch");
64-
VPBlockUtils::insertBlockAfter(LatchVPBB, OriginalLatch);
65-
auto *TopRegion = Plan.createVPRegionBlock(
66-
HeaderVPBB, LatchVPBB, "vector loop", false /*isReplicator*/);
67-
for (VPBlockBase *VPBB : vp_depth_first_shallow(HeaderVPBB))
68-
VPBB->setParent(TopRegion);
69-
70-
VPBlockUtils::insertBlockAfter(TopRegion, VecPreheader);
71-
VPBasicBlock *MiddleVPBB = Plan.createVPBasicBlock("middle.block");
72-
VPBlockUtils::insertBlockAfter(MiddleVPBB, TopRegion);
73-
74-
VPBasicBlock *ScalarPH = Plan.createVPBasicBlock("scalar.ph");
75-
VPBlockUtils::connectBlocks(ScalarPH, Plan.getScalarHeader());
76-
if (!RequiresScalarEpilogueCheck) {
77-
VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH);
78-
return;
79-
}
80-
81-
// If needed, add a check in the middle block to see if we have completed
82-
// all of the iterations in the first vector loop. Three cases:
83-
// 1) If (N - N%VF) == N, then we *don't* need to run the remainder.
84-
// Thus if tail is to be folded, we know we don't need to run the
85-
// remainder and we can set the condition to true.
86-
// 2) If we require a scalar epilogue, there is no conditional branch as
87-
// we unconditionally branch to the scalar preheader. Do nothing.
88-
// 3) Otherwise, construct a runtime check.
89-
BasicBlock *IRExitBlock = TheLoop->getUniqueLatchExitBlock();
90-
auto *VPExitBlock = Plan.getExitBlock(IRExitBlock);
91-
// The connection order corresponds to the operands of the conditional branch.
92-
VPBlockUtils::insertBlockAfter(VPExitBlock, MiddleVPBB);
93-
VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH);
94-
95-
auto *ScalarLatchTerm = TheLoop->getLoopLatch()->getTerminator();
96-
// Here we use the same DebugLoc as the scalar loop latch terminator instead
97-
// of the corresponding compare because they may have ended up with
98-
// different line numbers and we want to avoid awkward line stepping while
99-
// debugging. Eg. if the compare has got a line number inside the loop.
100-
VPBuilder Builder(MiddleVPBB);
101-
VPValue *Cmp =
102-
TailFolded
103-
? Plan.getOrAddLiveIn(ConstantInt::getTrue(
104-
IntegerType::getInt1Ty(TripCount->getType()->getContext())))
105-
: Builder.createICmp(CmpInst::ICMP_EQ, Plan.getTripCount(),
106-
&Plan.getVectorTripCount(),
107-
ScalarLatchTerm->getDebugLoc(), "cmp.n");
108-
Builder.createNaryOp(VPInstruction::BranchOnCond, {Cmp},
109-
ScalarLatchTerm->getDebugLoc());
110-
}
111-
11235
void VPlanTransforms::VPInstructionsToVPRecipes(
11336
VPlanPtr &Plan,
11437
function_ref<const InductionDescriptor *(PHINode *)>

0 commit comments

Comments
 (0)