1 //===-- VPlanHCFGBuilder.cpp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements the construction of a VPlan-based Hierarchical CFG
11 /// (H-CFG) for an incoming IR. This construction comprises the following
12 /// components and steps:
14 /// 1. PlainCFGBuilder class: builds a plain VPBasicBlock-based CFG that
15 /// faithfully represents the CFG in the incoming IR. A VPRegionBlock (Top
16 /// Region) is created to enclose and serve as parent of all the VPBasicBlocks
18 /// NOTE: At this point, there is a direct correspondence between all the
19 /// VPBasicBlocks created for the initial plain CFG and the incoming
20 /// BasicBlocks. However, this might change in the future.
22 //===----------------------------------------------------------------------===//
24 #include "VPlanHCFGBuilder.h"
25 #include "LoopVectorizationPlanner.h"
26 #include "llvm/Analysis/LoopIterator.h"
28 #define DEBUG_TYPE "loop-vectorize"
33 // Class that is used to build the plain CFG for the incoming IR.
34 class PlainCFGBuilder
{
36 // The outermost loop of the input loop nest considered for vectorization.
39 // Loop Info analysis.
42 // Vectorization plan that we are working on.
45 // Builder of the VPlan instruction-level representation.
46 VPBuilder VPIRBuilder
;
48 // NOTE: The following maps are intentionally destroyed after the plain CFG
49 // construction because subsequent VPlan-to-VPlan transformation may
51 // Map incoming BasicBlocks to their newly-created VPBasicBlocks.
52 DenseMap
<BasicBlock
*, VPBasicBlock
*> BB2VPBB
;
53 // Map incoming Value definitions to their newly-created VPValues.
54 DenseMap
<Value
*, VPValue
*> IRDef2VPValue
;
56 // Hold phi node's that need to be fixed once the plain CFG has been built.
57 SmallVector
<PHINode
*, 8> PhisToFix
;
59 /// Maps loops in the original IR to their corresponding region.
60 DenseMap
<Loop
*, VPRegionBlock
*> Loop2Region
;
63 void setVPBBPredsFromBB(VPBasicBlock
*VPBB
, BasicBlock
*BB
);
64 void setRegionPredsFromBB(VPRegionBlock
*VPBB
, BasicBlock
*BB
);
66 VPBasicBlock
*getOrCreateVPBB(BasicBlock
*BB
);
68 bool isExternalDef(Value
*Val
);
70 VPValue
*getOrCreateVPOperand(Value
*IRVal
);
71 void createVPInstructionsForVPBB(VPBasicBlock
*VPBB
, BasicBlock
*BB
);
74 PlainCFGBuilder(Loop
*Lp
, LoopInfo
*LI
, VPlan
&P
)
75 : TheLoop(Lp
), LI(LI
), Plan(P
) {}
77 /// Build plain CFG for TheLoop and connects it to Plan's entry.
80 } // anonymous namespace
82 // Set predecessors of \p VPBB in the same order as they are in \p BB. \p VPBB
83 // must have no predecessors.
84 void PlainCFGBuilder::setVPBBPredsFromBB(VPBasicBlock
*VPBB
, BasicBlock
*BB
) {
85 auto GetLatchOfExit
= [this](BasicBlock
*BB
) -> BasicBlock
* {
86 auto *SinglePred
= BB
->getSinglePredecessor();
87 Loop
*LoopForBB
= LI
->getLoopFor(BB
);
88 if (!SinglePred
|| LI
->getLoopFor(SinglePred
) == LoopForBB
)
90 // The input IR must be in loop-simplify form, ensuring a single predecessor
92 assert(SinglePred
== LI
->getLoopFor(SinglePred
)->getLoopLatch() &&
93 "SinglePred must be the only loop latch");
96 if (auto *LatchBB
= GetLatchOfExit(BB
)) {
97 auto *PredRegion
= getOrCreateVPBB(LatchBB
)->getParent();
98 assert(VPBB
== cast
<VPBasicBlock
>(PredRegion
->getSingleSuccessor()) &&
99 "successor must already be set for PredRegion; it must have VPBB "
100 "as single successor");
101 VPBB
->setPredecessors({PredRegion
});
104 // Collect VPBB predecessors.
105 SmallVector
<VPBlockBase
*, 2> VPBBPreds
;
106 for (BasicBlock
*Pred
: predecessors(BB
))
107 VPBBPreds
.push_back(getOrCreateVPBB(Pred
));
108 VPBB
->setPredecessors(VPBBPreds
);
111 static bool isHeaderBB(BasicBlock
*BB
, Loop
*L
) {
112 return L
&& BB
== L
->getHeader();
115 void PlainCFGBuilder::setRegionPredsFromBB(VPRegionBlock
*Region
,
117 // BB is a loop header block. Connect the region to the loop preheader.
118 Loop
*LoopOfBB
= LI
->getLoopFor(BB
);
119 Region
->setPredecessors({getOrCreateVPBB(LoopOfBB
->getLoopPredecessor())});
122 // Add operands to VPInstructions representing phi nodes from the input IR.
123 void PlainCFGBuilder::fixPhiNodes() {
124 for (auto *Phi
: PhisToFix
) {
125 assert(IRDef2VPValue
.count(Phi
) && "Missing VPInstruction for PHINode.");
126 VPValue
*VPVal
= IRDef2VPValue
[Phi
];
127 assert(isa
<VPWidenPHIRecipe
>(VPVal
) &&
128 "Expected WidenPHIRecipe for phi node.");
129 auto *VPPhi
= cast
<VPWidenPHIRecipe
>(VPVal
);
130 assert(VPPhi
->getNumOperands() == 0 &&
131 "Expected VPInstruction with no operands.");
133 Loop
*L
= LI
->getLoopFor(Phi
->getParent());
134 if (isHeaderBB(Phi
->getParent(), L
)) {
135 // For header phis, make sure the incoming value from the loop
136 // predecessor is the first operand of the recipe.
137 assert(Phi
->getNumOperands() == 2);
138 BasicBlock
*LoopPred
= L
->getLoopPredecessor();
140 getOrCreateVPOperand(Phi
->getIncomingValueForBlock(LoopPred
)),
142 BasicBlock
*LoopLatch
= L
->getLoopLatch();
144 getOrCreateVPOperand(Phi
->getIncomingValueForBlock(LoopLatch
)),
149 for (unsigned I
= 0; I
!= Phi
->getNumOperands(); ++I
)
150 VPPhi
->addIncoming(getOrCreateVPOperand(Phi
->getIncomingValue(I
)),
151 BB2VPBB
[Phi
->getIncomingBlock(I
)]);
155 static bool isHeaderVPBB(VPBasicBlock
*VPBB
) {
156 return VPBB
->getParent() && VPBB
->getParent()->getEntry() == VPBB
;
159 // Create a new empty VPBasicBlock for an incoming BasicBlock in the region
160 // corresponding to the containing loop or retrieve an existing one if it was
161 // already created. If no region exists yet for the loop containing \p BB, a new
163 VPBasicBlock
*PlainCFGBuilder::getOrCreateVPBB(BasicBlock
*BB
) {
164 if (auto *VPBB
= BB2VPBB
.lookup(BB
)) {
165 // Retrieve existing VPBB.
170 LLVM_DEBUG(dbgs() << "Creating VPBasicBlock for " << BB
->getName() << "\n");
171 VPBasicBlock
*VPBB
= new VPBasicBlock(BB
->getName());
174 // Get or create a region for the loop containing BB.
175 Loop
*LoopOfBB
= LI
->getLoopFor(BB
);
179 VPRegionBlock
*RegionOfBB
= Loop2Region
.lookup(LoopOfBB
);
180 assert((RegionOfBB
!= nullptr) ^ isHeaderBB(BB
, LoopOfBB
) &&
181 "region must exist or BB must be a loop header");
183 VPBB
->setParent(RegionOfBB
);
185 // If BB's loop is nested inside another loop within VPlan's scope, the
186 // header of that enclosing loop was already visited and its region
187 // constructed and recorded in Loop2Region. That region is now set as the
188 // parent of VPBB's region. Otherwise it is set to null.
189 auto *RegionOfVPBB
= new VPRegionBlock(
190 LoopOfBB
->getHeader()->getName().str(), false /*isReplicator*/);
191 RegionOfVPBB
->setParent(Loop2Region
[LoopOfBB
->getParentLoop()]);
192 RegionOfVPBB
->setEntry(VPBB
);
193 Loop2Region
[LoopOfBB
] = RegionOfVPBB
;
199 // Return true if \p Val is considered an external definition. An external
200 // definition is either:
201 // 1. A Value that is not an Instruction. This will be refined in the future.
202 // 2. An Instruction that is outside of the CFG snippet represented in VPlan,
203 // i.e., is not part of: a) the loop nest, b) outermost loop PH and, c)
204 // outermost loop exits.
205 bool PlainCFGBuilder::isExternalDef(Value
*Val
) {
206 // All the Values that are not Instructions are considered external
207 // definitions for now.
208 Instruction
*Inst
= dyn_cast
<Instruction
>(Val
);
212 BasicBlock
*InstParent
= Inst
->getParent();
213 assert(InstParent
&& "Expected instruction parent.");
215 // Check whether Instruction definition is in loop PH.
216 BasicBlock
*PH
= TheLoop
->getLoopPreheader();
217 assert(PH
&& "Expected loop pre-header.");
219 if (InstParent
== PH
)
220 // Instruction definition is in outermost loop PH.
223 // Check whether Instruction definition is in the loop exit.
224 BasicBlock
*Exit
= TheLoop
->getUniqueExitBlock();
225 assert(Exit
&& "Expected loop with single exit.");
226 if (InstParent
== Exit
) {
227 // Instruction definition is in outermost loop exit.
231 // Check whether Instruction definition is in loop body.
232 return !TheLoop
->contains(Inst
);
236 // Create a new VPValue or retrieve an existing one for the Instruction's
237 // operand \p IRVal. This function must only be used to create/retrieve VPValues
238 // for *Instruction's operands* and not to create regular VPInstruction's. For
239 // the latter, please, look at 'createVPInstructionsForVPBB'.
240 VPValue
*PlainCFGBuilder::getOrCreateVPOperand(Value
*IRVal
) {
241 auto VPValIt
= IRDef2VPValue
.find(IRVal
);
242 if (VPValIt
!= IRDef2VPValue
.end())
243 // Operand has an associated VPInstruction or VPValue that was previously
245 return VPValIt
->second
;
247 // Operand doesn't have a previously created VPInstruction/VPValue. This
248 // means that operand is:
249 // A) a definition external to VPlan,
250 // B) any other Value without specific representation in VPlan.
251 // For now, we use VPValue to represent A and B and classify both as external
252 // definitions. We may introduce specific VPValue subclasses for them in the
254 assert(isExternalDef(IRVal
) && "Expected external definition as operand.");
256 // A and B: Create VPValue and add it to the pool of external definitions and
257 // to the Value->VPValue map.
258 VPValue
*NewVPVal
= Plan
.getVPValueOrAddLiveIn(IRVal
);
259 IRDef2VPValue
[IRVal
] = NewVPVal
;
263 // Create new VPInstructions in a VPBasicBlock, given its BasicBlock
264 // counterpart. This function must be invoked in RPO so that the operands of a
265 // VPInstruction in \p BB have been visited before (except for Phi nodes).
266 void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock
*VPBB
,
268 VPIRBuilder
.setInsertPoint(VPBB
);
269 for (Instruction
&InstRef
: *BB
) {
270 Instruction
*Inst
= &InstRef
;
272 // There shouldn't be any VPValue for Inst at this point. Otherwise, we
273 // visited Inst when we shouldn't, breaking the RPO traversal order.
274 assert(!IRDef2VPValue
.count(Inst
) &&
275 "Instruction shouldn't have been visited.");
277 if (auto *Br
= dyn_cast
<BranchInst
>(Inst
)) {
278 // Conditional branch instruction are represented using BranchOnCond
280 if (Br
->isConditional()) {
281 VPValue
*Cond
= getOrCreateVPOperand(Br
->getCondition());
283 new VPInstruction(VPInstruction::BranchOnCond
, {Cond
}));
286 // Skip the rest of the Instruction processing for Branch instructions.
291 if (auto *Phi
= dyn_cast
<PHINode
>(Inst
)) {
292 // Phi node's operands may have not been visited at this point. We create
293 // an empty VPInstruction that we will fix once the whole plain CFG has
295 NewVPV
= new VPWidenPHIRecipe(Phi
);
296 VPBB
->appendRecipe(cast
<VPWidenPHIRecipe
>(NewVPV
));
297 PhisToFix
.push_back(Phi
);
299 // Translate LLVM-IR operands into VPValue operands and set them in the
300 // new VPInstruction.
301 SmallVector
<VPValue
*, 4> VPOperands
;
302 for (Value
*Op
: Inst
->operands())
303 VPOperands
.push_back(getOrCreateVPOperand(Op
));
305 // Build VPInstruction for any arbitrary Instruction without specific
306 // representation in VPlan.
307 NewVPV
= cast
<VPInstruction
>(
308 VPIRBuilder
.createNaryOp(Inst
->getOpcode(), VPOperands
, Inst
));
311 IRDef2VPValue
[Inst
] = NewVPV
;
315 // Main interface to build the plain CFG.
316 void PlainCFGBuilder::buildPlainCFG() {
317 // 1. Scan the body of the loop in a topological order to visit each basic
318 // block after having visited its predecessor basic blocks. Create a VPBB for
319 // each BB and link it to its successor and predecessor VPBBs. Note that
320 // predecessors must be set in the same order as they are in the incomming IR.
321 // Otherwise, there might be problems with existing phi nodes and algorithm
322 // based on predecessors traversal.
324 // Loop PH needs to be explicitly visited since it's not taken into account by
326 BasicBlock
*ThePreheaderBB
= TheLoop
->getLoopPreheader();
327 assert((ThePreheaderBB
->getTerminator()->getNumSuccessors() == 1) &&
328 "Unexpected loop preheader");
329 // buildPlainCFG needs to be called after createInitialVPlan, which creates
330 // the initial skeleton (including the preheader VPBB). buildPlainCFG builds
331 // the CFG for the loop nest and hooks it up to the initial skeleton.
332 VPBasicBlock
*ThePreheaderVPBB
= Plan
.getEntry();
333 BB2VPBB
[ThePreheaderBB
] = ThePreheaderVPBB
;
334 ThePreheaderVPBB
->setName("vector.ph");
335 for (auto &I
: *ThePreheaderBB
) {
336 if (I
.getType()->isVoidTy())
338 IRDef2VPValue
[&I
] = Plan
.getVPValueOrAddLiveIn(&I
);
340 // Create region (and header block) for the outer loop, so that we can link
342 VPBlockBase
*HeaderVPBB
= getOrCreateVPBB(TheLoop
->getHeader());
343 HeaderVPBB
->setName("vector.body");
344 ThePreheaderVPBB
->setOneSuccessor(HeaderVPBB
->getParent());
346 LoopBlocksRPO
RPO(TheLoop
);
349 for (BasicBlock
*BB
: RPO
) {
350 // Create or retrieve the VPBasicBlock for this BB and create its
352 VPBasicBlock
*VPBB
= getOrCreateVPBB(BB
);
353 VPRegionBlock
*Region
= VPBB
->getParent();
354 createVPInstructionsForVPBB(VPBB
, BB
);
355 Loop
*LoopForBB
= LI
->getLoopFor(BB
);
356 // Set VPBB predecessors in the same order as they are in the incoming BB.
357 if (!isHeaderBB(BB
, LoopForBB
))
358 setVPBBPredsFromBB(VPBB
, BB
);
360 // BB is a loop header, set the predecessor for the region.
361 assert(isHeaderVPBB(VPBB
) && "isHeaderBB and isHeaderVPBB disagree");
362 setRegionPredsFromBB(Region
, BB
);
365 // Set VPBB successors. We create empty VPBBs for successors if they don't
366 // exist already. Recipes will be created when the successor is visited
367 // during the RPO traversal.
368 auto *BI
= cast
<BranchInst
>(BB
->getTerminator());
369 unsigned NumSuccs
= succ_size(BB
);
371 auto *Successor
= getOrCreateVPBB(BB
->getSingleSuccessor());
372 VPBB
->setOneSuccessor(isHeaderVPBB(Successor
)
373 ? Successor
->getParent()
374 : static_cast<VPBlockBase
*>(Successor
));
377 assert(BI
->isConditional() && NumSuccs
== 2 && BI
->isConditional() &&
378 "block must have conditional branch with 2 successors");
379 // Look up the branch condition to get the corresponding VPValue
380 // representing the condition bit in VPlan (which may be in another VPBB).
381 assert(IRDef2VPValue
.contains(BI
->getCondition()) &&
382 "Missing condition bit in IRDef2VPValue!");
383 VPBasicBlock
*Successor0
= getOrCreateVPBB(BI
->getSuccessor(0));
384 VPBasicBlock
*Successor1
= getOrCreateVPBB(BI
->getSuccessor(1));
385 if (!LoopForBB
|| BB
!= LoopForBB
->getLoopLatch()) {
386 VPBB
->setTwoSuccessors(Successor0
, Successor1
);
389 // For a latch we need to set the successor of the region rather than that
390 // of VPBB and it should be set to the exit, i.e., non-header successor.
391 Region
->setOneSuccessor(isHeaderVPBB(Successor0
) ? Successor1
: Successor0
);
392 Region
->setExiting(VPBB
);
395 // 2. Process outermost loop exit. We created an empty VPBB for the loop
396 // single exit BB during the RPO traversal of the loop body but Instructions
397 // weren't visited because it's not part of the loop.
398 BasicBlock
*LoopExitBB
= TheLoop
->getUniqueExitBlock();
399 assert(LoopExitBB
&& "Loops with multiple exits are not supported.");
400 VPBasicBlock
*LoopExitVPBB
= BB2VPBB
[LoopExitBB
];
401 // Loop exit was already set as successor of the loop exiting BB.
402 // We only set its predecessor VPBB now.
403 setVPBBPredsFromBB(LoopExitVPBB
, LoopExitBB
);
405 // 3. The whole CFG has been built at this point so all the input Values must
406 // have a VPlan couterpart. Fix VPlan phi nodes by adding their corresponding
411 void VPlanHCFGBuilder::buildPlainCFG() {
412 PlainCFGBuilder
PCFGBuilder(TheLoop
, LI
, Plan
);
413 PCFGBuilder
.buildPlainCFG();
416 // Public interface to build a H-CFG.
417 void VPlanHCFGBuilder::buildHierarchicalCFG() {
418 // Build Top Region enclosing the plain CFG.
420 LLVM_DEBUG(Plan
.setName("HCFGBuilder: Plain CFG\n"); dbgs() << Plan
);
422 VPRegionBlock
*TopRegion
= Plan
.getVectorLoopRegion();
423 Verifier
.verifyHierarchicalCFG(TopRegion
);
425 // Compute plain CFG dom tree for VPLInfo.
426 VPDomTree
.recalculate(Plan
);
427 LLVM_DEBUG(dbgs() << "Dominator Tree after building the plain CFG.\n";
428 VPDomTree
.print(dbgs()));