Skip to content

Commit fed966f

Browse files
committed
[OpenMPIRBuilder] Implicitly defined control blocks. NFC.
Do not explicitly store the BasicBlocks for Preheader, Body and After inside CanonicalLoopInfo, but look the up when needed using their position relative to the other loop control blocks. By definition, instructions inside these are not managed by CanonicalLoopInfo (except terminator for Preheader) hence it makes sense to think of them as connections to the CanonicalLoopInfo instead of part of the CanonicalLoopInfo itself. In particular for Preheader, it makes using SplitBasicBlock easier since inserting control flow at an InsertPoint may otherwise require updating the CanonicalLoopInfo's Preheader because the branch that jumps to the header is moved to another BasicBlock. Reviewed By: ftynse Differential Revision: https://reviews.llvm.org/D114368
1 parent c5fef77 commit fed966f

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h

+6-9
Original file line numberDiff line numberDiff line change
@@ -1408,13 +1408,10 @@ class CanonicalLoopInfo {
14081408
friend class OpenMPIRBuilder;
14091409

14101410
private:
1411-
BasicBlock *Preheader = nullptr;
14121411
BasicBlock *Header = nullptr;
14131412
BasicBlock *Cond = nullptr;
1414-
BasicBlock *Body = nullptr;
14151413
BasicBlock *Latch = nullptr;
14161414
BasicBlock *Exit = nullptr;
1417-
BasicBlock *After = nullptr;
14181415

14191416
/// Add the control blocks of this loop to \p BBs.
14201417
///
@@ -1436,10 +1433,7 @@ class CanonicalLoopInfo {
14361433
/// Code that must be execute before any loop iteration can be emitted here,
14371434
/// such as computing the loop trip count and begin lifetime markers. Code in
14381435
/// the preheader is not considered part of the canonical loop.
1439-
BasicBlock *getPreheader() const {
1440-
assert(isValid() && "Requires a valid canonical loop");
1441-
return Preheader;
1442-
}
1436+
BasicBlock *getPreheader() const;
14431437

14441438
/// The header is the entry for each iteration. In the canonical control flow,
14451439
/// it only contains the PHINode for the induction variable.
@@ -1460,7 +1454,7 @@ class CanonicalLoopInfo {
14601454
/// eventually branch to the \p Latch block.
14611455
BasicBlock *getBody() const {
14621456
assert(isValid() && "Requires a valid canonical loop");
1463-
return Body;
1457+
return cast<BranchInst>(Cond->getTerminator())->getSuccessor(0);
14641458
}
14651459

14661460
/// Reaching the latch indicates the end of the loop body code. In the
@@ -1484,7 +1478,7 @@ class CanonicalLoopInfo {
14841478
/// statements/cancellations).
14851479
BasicBlock *getAfter() const {
14861480
assert(isValid() && "Requires a valid canonical loop");
1487-
return After;
1481+
return Exit->getSingleSuccessor();
14881482
}
14891483

14901484
/// Returns the llvm::Value containing the number of loop iterations. It must
@@ -1515,18 +1509,21 @@ class CanonicalLoopInfo {
15151509
/// Return the insertion point for user code before the loop.
15161510
OpenMPIRBuilder::InsertPointTy getPreheaderIP() const {
15171511
assert(isValid() && "Requires a valid canonical loop");
1512+
BasicBlock *Preheader = getPreheader();
15181513
return {Preheader, std::prev(Preheader->end())};
15191514
};
15201515

15211516
/// Return the insertion point for user code in the body.
15221517
OpenMPIRBuilder::InsertPointTy getBodyIP() const {
15231518
assert(isValid() && "Requires a valid canonical loop");
1519+
BasicBlock *Body = getBody();
15241520
return {Body, Body->begin()};
15251521
};
15261522

15271523
/// Return the insertion point for user code after the loop.
15281524
OpenMPIRBuilder::InsertPointTy getAfterIP() const {
15291525
assert(isValid() && "Requires a valid canonical loop");
1526+
BasicBlock *After = getAfter();
15301527
return {After, After->begin()};
15311528
};
15321529

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

+27-16
Original file line numberDiff line numberDiff line change
@@ -1329,13 +1329,10 @@ CanonicalLoopInfo *OpenMPIRBuilder::createLoopSkeleton(
13291329
LoopInfos.emplace_front();
13301330
CanonicalLoopInfo *CL = &LoopInfos.front();
13311331

1332-
CL->Preheader = Preheader;
13331332
CL->Header = Header;
13341333
CL->Cond = Cond;
1335-
CL->Body = Body;
13361334
CL->Latch = Latch;
13371335
CL->Exit = Exit;
1338-
CL->After = After;
13391336

13401337
#ifndef NDEBUG
13411338
CL->assertOK();
@@ -1359,7 +1356,7 @@ OpenMPIRBuilder::createCanonicalLoop(const LocationDescription &Loc,
13591356
// Split the loop at the insertion point: Branch to the preheader and move
13601357
// every following instruction to after the loop (the After BB). Also, the
13611358
// new successor is the loop's after block.
1362-
Builder.CreateBr(CL->Preheader);
1359+
Builder.CreateBr(CL->getPreheader());
13631360
After->getInstList().splice(After->begin(), BB->getInstList(),
13641361
Builder.GetInsertPoint(), BB->end());
13651362
After->replaceSuccessorsPhiUsesWith(BB, After);
@@ -1791,6 +1788,12 @@ OpenMPIRBuilder::collapseLoops(DebugLoc DL, ArrayRef<CanonicalLoopInfo *> Loops,
17911788
BasicBlock *OrigAfter = Outermost->getAfter();
17921789
Function *F = OrigPreheader->getParent();
17931790

1791+
// Loop control blocks that may become orphaned later.
1792+
SmallVector<BasicBlock *, 12> OldControlBBs;
1793+
OldControlBBs.reserve(6 * Loops.size());
1794+
for (CanonicalLoopInfo *Loop : Loops)
1795+
Loop->collectControlBlocks(OldControlBBs);
1796+
17941797
// Setup the IRBuilder for inserting the trip count computation.
17951798
Builder.SetCurrentDebugLocation(DL);
17961799
if (ComputeIP.isSet())
@@ -1886,10 +1889,6 @@ OpenMPIRBuilder::collapseLoops(DebugLoc DL, ArrayRef<CanonicalLoopInfo *> Loops,
18861889
Loops[i]->getIndVar()->replaceAllUsesWith(NewIndVars[i]);
18871890

18881891
// Remove unused parts of the input loops.
1889-
SmallVector<BasicBlock *, 12> OldControlBBs;
1890-
OldControlBBs.reserve(6 * Loops.size());
1891-
for (CanonicalLoopInfo *Loop : Loops)
1892-
Loop->collectControlBlocks(OldControlBBs);
18931892
removeUnusedBlocksFromParent(OldControlBBs);
18941893

18951894
for (CanonicalLoopInfo *L : Loops)
@@ -1915,6 +1914,12 @@ OpenMPIRBuilder::tileLoops(DebugLoc DL, ArrayRef<CanonicalLoopInfo *> Loops,
19151914
BasicBlock *InnerEnter = InnermostLoop->getBody();
19161915
BasicBlock *InnerLatch = InnermostLoop->getLatch();
19171916

1917+
// Loop control blocks that may become orphaned later.
1918+
SmallVector<BasicBlock *, 12> OldControlBBs;
1919+
OldControlBBs.reserve(6 * Loops.size());
1920+
for (CanonicalLoopInfo *Loop : Loops)
1921+
Loop->collectControlBlocks(OldControlBBs);
1922+
19181923
// Collect original trip counts and induction variable to be accessible by
19191924
// index. Also, the structure of the original loops is not preserved during
19201925
// the construction of the tiled loops, so do it before we scavenge the BBs of
@@ -2074,10 +2079,6 @@ OpenMPIRBuilder::tileLoops(DebugLoc DL, ArrayRef<CanonicalLoopInfo *> Loops,
20742079
}
20752080

20762081
// Remove unused parts of the original loops.
2077-
SmallVector<BasicBlock *, 12> OldControlBBs;
2078-
OldControlBBs.reserve(6 * Loops.size());
2079-
for (CanonicalLoopInfo *Loop : Loops)
2080-
Loop->collectControlBlocks(OldControlBBs);
20812082
removeUnusedBlocksFromParent(OldControlBBs);
20822083

20832084
for (CanonicalLoopInfo *L : Loops)
@@ -3321,7 +3322,16 @@ void CanonicalLoopInfo::collectControlBlocks(
33213322
// flow. For consistency, this also means we do not add the Body block, which
33223323
// is just the entry to the body code.
33233324
BBs.reserve(BBs.size() + 6);
3324-
BBs.append({Preheader, Header, Cond, Latch, Exit, After});
3325+
BBs.append({getPreheader(), Header, Cond, Latch, Exit, getAfter()});
3326+
}
3327+
3328+
BasicBlock *CanonicalLoopInfo::getPreheader() const {
3329+
assert(isValid() && "Requires a valid canonical loop");
3330+
for (BasicBlock *Pred : predecessors(Header)) {
3331+
if (Pred != Latch)
3332+
return Pred;
3333+
}
3334+
llvm_unreachable("Missing preheader");
33253335
}
33263336

33273337
void CanonicalLoopInfo::assertOK() const {
@@ -3330,6 +3340,10 @@ void CanonicalLoopInfo::assertOK() const {
33303340
if (!isValid())
33313341
return;
33323342

3343+
BasicBlock *Preheader = getPreheader();
3344+
BasicBlock *Body = getBody();
3345+
BasicBlock *After = getAfter();
3346+
33333347
// Verify standard control-flow we use for OpenMP loops.
33343348
assert(Preheader);
33353349
assert(isa<BranchInst>(Preheader->getTerminator()) &&
@@ -3415,11 +3429,8 @@ void CanonicalLoopInfo::assertOK() const {
34153429
}
34163430

34173431
void CanonicalLoopInfo::invalidate() {
3418-
Preheader = nullptr;
34193432
Header = nullptr;
34203433
Cond = nullptr;
3421-
Body = nullptr;
34223434
Latch = nullptr;
34233435
Exit = nullptr;
3424-
After = nullptr;
34253436
}

0 commit comments

Comments
 (0)