12
12
// / components and steps:
13
13
//
14
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
17
- // / in the plain CFG.
15
+ // / faithfully represents the CFG in the incoming IR.
18
16
// / NOTE: At this point, there is a direct correspondence between all the
19
17
// / VPBasicBlocks created for the initial plain CFG and the incoming
20
18
// / BasicBlocks. However, this might change in the future.
@@ -57,12 +55,8 @@ class PlainCFGBuilder {
57
55
// Hold phi node's that need to be fixed once the plain CFG has been built.
58
56
SmallVector<PHINode *, 8 > PhisToFix;
59
57
60
- // / Maps loops in the original IR to their corresponding region.
61
- DenseMap<Loop *, VPRegionBlock *> Loop2Region;
62
-
63
58
// Utility functions.
64
59
void setVPBBPredsFromBB (VPBasicBlock *VPBB, BasicBlock *BB);
65
- void setRegionPredsFromBB (VPRegionBlock *VPBB, BasicBlock *BB);
66
60
void fixHeaderPhis ();
67
61
VPBasicBlock *getOrCreateVPBB (BasicBlock *BB);
68
62
#ifndef NDEBUG
@@ -83,25 +77,6 @@ class PlainCFGBuilder {
83
77
// Set predecessors of \p VPBB in the same order as they are in \p BB. \p VPBB
84
78
// must have no predecessors.
85
79
void PlainCFGBuilder::setVPBBPredsFromBB (VPBasicBlock *VPBB, BasicBlock *BB) {
86
- auto GetLatchOfExit = [this ](BasicBlock *BB) -> BasicBlock * {
87
- auto *SinglePred = BB->getSinglePredecessor ();
88
- Loop *LoopForBB = LI->getLoopFor (BB);
89
- if (!SinglePred || LI->getLoopFor (SinglePred) == LoopForBB)
90
- return nullptr ;
91
- // The input IR must be in loop-simplify form, ensuring a single predecessor
92
- // for exit blocks.
93
- assert (SinglePred == LI->getLoopFor (SinglePred)->getLoopLatch () &&
94
- " SinglePred must be the only loop latch" );
95
- return SinglePred;
96
- };
97
- if (auto *LatchBB = GetLatchOfExit (BB)) {
98
- auto *PredRegion = getOrCreateVPBB (LatchBB)->getParent ();
99
- assert (VPBB == cast<VPBasicBlock>(PredRegion->getSingleSuccessor ()) &&
100
- " successor must already be set for PredRegion; it must have VPBB "
101
- " as single successor" );
102
- VPBB->setPredecessors ({PredRegion});
103
- return ;
104
- }
105
80
// Collect VPBB predecessors.
106
81
SmallVector<VPBlockBase *, 2 > VPBBPreds;
107
82
for (BasicBlock *Pred : predecessors (BB))
@@ -113,13 +88,6 @@ static bool isHeaderBB(BasicBlock *BB, Loop *L) {
113
88
return L && BB == L->getHeader ();
114
89
}
115
90
116
- void PlainCFGBuilder::setRegionPredsFromBB (VPRegionBlock *Region,
117
- BasicBlock *BB) {
118
- // BB is a loop header block. Connect the region to the loop preheader.
119
- Loop *LoopOfBB = LI->getLoopFor (BB);
120
- Region->setPredecessors ({getOrCreateVPBB (LoopOfBB->getLoopPredecessor ())});
121
- }
122
-
123
91
// Add operands to VPInstructions representing phi nodes from the input IR.
124
92
void PlainCFGBuilder::fixHeaderPhis () {
125
93
for (auto *Phi : PhisToFix) {
@@ -150,19 +118,6 @@ static bool isHeaderVPBB(VPBasicBlock *VPBB) {
150
118
return VPBB->getParent () && VPBB->getParent ()->getEntry () == VPBB;
151
119
}
152
120
153
- // / Return true of \p L loop is contained within \p OuterLoop.
154
- static bool doesContainLoop (const Loop *L, const Loop *OuterLoop) {
155
- if (L->getLoopDepth () < OuterLoop->getLoopDepth ())
156
- return false ;
157
- const Loop *P = L;
158
- while (P) {
159
- if (P == OuterLoop)
160
- return true ;
161
- P = P->getParentLoop ();
162
- }
163
- return false ;
164
- }
165
-
166
121
// Create a new empty VPBasicBlock for an incoming BasicBlock in the region
167
122
// corresponding to the containing loop or retrieve an existing one if it was
168
123
// already created. If no region exists yet for the loop containing \p BB, a new
@@ -178,28 +133,6 @@ VPBasicBlock *PlainCFGBuilder::getOrCreateVPBB(BasicBlock *BB) {
178
133
LLVM_DEBUG (dbgs () << " Creating VPBasicBlock for " << Name << " \n " );
179
134
VPBasicBlock *VPBB = Plan.createVPBasicBlock (Name);
180
135
BB2VPBB[BB] = VPBB;
181
-
182
- // Get or create a region for the loop containing BB, except for the top
183
- // region of TheLoop which is created later.
184
- Loop *LoopOfBB = LI->getLoopFor (BB);
185
- if (!LoopOfBB || LoopOfBB == TheLoop || !doesContainLoop (LoopOfBB, TheLoop))
186
- return VPBB;
187
-
188
- auto *RegionOfVPBB = Loop2Region.lookup (LoopOfBB);
189
- if (!isHeaderBB (BB, LoopOfBB)) {
190
- assert (RegionOfVPBB &&
191
- " Region should have been created by visiting header earlier" );
192
- VPBB->setParent (RegionOfVPBB);
193
- return VPBB;
194
- }
195
-
196
- assert (!RegionOfVPBB &&
197
- " First visit of a header basic block expects to register its region." );
198
- // Handle a header - take care of its Region.
199
- RegionOfVPBB = Plan.createVPRegionBlock (Name.str (), false /* isReplicator*/ );
200
- RegionOfVPBB->setParent (Loop2Region[LoopOfBB->getParentLoop ()]);
201
- RegionOfVPBB->setEntry (VPBB);
202
- Loop2Region[LoopOfBB] = RegionOfVPBB;
203
136
return VPBB;
204
137
}
205
138
@@ -376,15 +309,13 @@ void PlainCFGBuilder::buildPlainCFG(
376
309
for (BasicBlock *BB : RPO) {
377
310
// Create or retrieve the VPBasicBlock for this BB.
378
311
VPBasicBlock *VPBB = getOrCreateVPBB (BB);
379
- VPRegionBlock *Region = VPBB->getParent ();
380
312
Loop *LoopForBB = LI->getLoopFor (BB);
381
313
// Set VPBB predecessors in the same order as they are in the incoming BB.
382
314
if (!isHeaderBB (BB, LoopForBB)) {
383
315
setVPBBPredsFromBB (VPBB, BB);
384
- } else if (Region) {
385
- // BB is a loop header and there's a corresponding region, set the
386
- // predecessor for it.
387
- setRegionPredsFromBB (Region, BB);
316
+ } else if (LoopForBB != TheLoop) {
317
+ VPBB->setPredecessors ({getOrCreateVPBB (LoopForBB->getLoopPredecessor ()),
318
+ getOrCreateVPBB (LoopForBB->getLoopLatch ())});
388
319
}
389
320
390
321
// Create VPInstructions for BB.
@@ -423,21 +354,11 @@ void PlainCFGBuilder::buildPlainCFG(
423
354
BasicBlock *IRSucc1 = BI->getSuccessor (1 );
424
355
VPBasicBlock *Successor0 = getOrCreateVPBB (IRSucc0);
425
356
VPBasicBlock *Successor1 = getOrCreateVPBB (IRSucc1);
426
- if (BB == LoopForBB->getLoopLatch ()) {
427
- // For a latch we need to set the successor of the region rather than that
428
- // of VPBB and it should be set to the exit, i.e., non-header successor,
429
- // except for the top region, which is handled elsewhere.
430
- assert (LoopForBB != TheLoop &&
431
- " Latch of the top region should have been handled earlier" );
432
- Region->setOneSuccessor (isHeaderVPBB (Successor0) ? Successor1
433
- : Successor0);
434
- Region->setExiting (VPBB);
435
- continue ;
436
- }
437
357
438
- // Don't connect any blocks outside the current loop except the latch for
439
- // now. The latch is handled above.
440
- if (LoopForBB) {
358
+ // Don't connect any blocks outside the current loop except the latch, which
359
+ // is handled below.
360
+ if (LoopForBB &&
361
+ (LoopForBB == TheLoop || BB != LoopForBB->getLoopLatch ())) {
441
362
if (!LoopForBB->contains (IRSucc0)) {
442
363
VPBB->setOneSuccessor (Successor1);
443
364
continue ;
@@ -461,16 +382,11 @@ void PlainCFGBuilder::buildPlainCFG(
461
382
462
383
for (const auto &[IRBB, VPB] : BB2VPBB)
463
384
VPB2IRBB[VPB] = IRBB;
385
+
386
+ LLVM_DEBUG (Plan.setName (" Plain CFG\n " ); dbgs () << Plan);
464
387
}
465
388
466
389
void VPlanHCFGBuilder::buildPlainCFG () {
467
390
PlainCFGBuilder PCFGBuilder (TheLoop, LI, Plan);
468
391
PCFGBuilder.buildPlainCFG (VPB2IRBB);
469
392
}
470
-
471
- // Public interface to build a H-CFG.
472
- void VPlanHCFGBuilder::buildHierarchicalCFG () {
473
- // Build Top Region enclosing the plain CFG.
474
- buildPlainCFG ();
475
- LLVM_DEBUG (Plan.setName (" HCFGBuilder: Plain CFG\n " ); dbgs () << Plan);
476
- }
0 commit comments