Skip to content

Commit d121d7d

Browse files
authored
Merge pull request swiftlang#35428 from eeckstein/fix-blocklist-api
SIL: move all the block-list modifying APIs to SILFunction.
2 parents bfe0a00 + b735178 commit d121d7d

File tree

24 files changed

+97
-104
lines changed

24 files changed

+97
-104
lines changed

include/swift/SIL/SILBasicBlock.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
5656

5757
void operator delete(void *Ptr, size_t) = delete;
5858

59-
SILBasicBlock(SILFunction *F, SILBasicBlock *relativeToBB, bool after);
59+
SILBasicBlock(SILFunction *parent) : Parent(parent), PredList(nullptr) { }
6060

6161
public:
6262
~SILBasicBlock();
@@ -143,12 +143,6 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
143143
/// without a terminator.
144144
SILBasicBlock *split(iterator I);
145145

146-
/// Move the basic block to after the specified basic block in the IR.
147-
///
148-
/// Assumes that the basic blocks must reside in the same function. In asserts
149-
/// builds, an assert verifies that this is true.
150-
void moveAfter(SILBasicBlock *After);
151-
152146
/// Moves the instruction to the iterator in this basic block.
153147
void moveTo(SILBasicBlock::iterator To, SILInstruction *I);
154148

include/swift/SIL/SILBuilder.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -354,25 +354,6 @@ class SILBuilder {
354354
// CFG Manipulation
355355
//===--------------------------------------------------------------------===//
356356

357-
/// moveBlockTo - Move a block to immediately before the given iterator.
358-
void moveBlockTo(SILBasicBlock *BB, SILFunction::iterator IP) {
359-
assert(SILFunction::iterator(BB) != IP && "moving block before itself?");
360-
SILFunction *F = BB->getParent();
361-
auto &Blocks = F->getBlocks();
362-
Blocks.remove(BB);
363-
Blocks.insert(IP, BB);
364-
}
365-
366-
/// moveBlockTo - Move \p BB to immediately before \p Before.
367-
void moveBlockTo(SILBasicBlock *BB, SILBasicBlock *Before) {
368-
moveBlockTo(BB, Before->getIterator());
369-
}
370-
371-
/// moveBlockToEnd - Reorder a block to the end of its containing function.
372-
void moveBlockToEnd(SILBasicBlock *BB) {
373-
moveBlockTo(BB, BB->getParent()->end());
374-
}
375-
376357
/// Move the insertion point to the end of the given block.
377358
///
378359
/// Assumes that no insertion point is currently active.

include/swift/SIL/SILFunction.h

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,12 @@ class SILFunction
959959
SILType mapTypeIntoContext(SILType type) const;
960960

961961
/// Converts the given function definition to a declaration.
962-
void convertToDeclaration();
962+
void convertToDeclaration() {
963+
assert(isDefinition() && "Can only convert definitions to declarations");
964+
clear();
965+
}
966+
967+
void clear();
963968

964969
/// Return the identity substitutions necessary to forward this call if it is
965970
/// generic.
@@ -969,9 +974,6 @@ class SILFunction
969974
// Block List Access
970975
//===--------------------------------------------------------------------===//
971976

972-
BlockListType &getBlocks() { return BlockList; }
973-
const BlockListType &getBlocks() const { return BlockList; }
974-
975977
using iterator = BlockListType::iterator;
976978
using reverse_iterator = BlockListType::reverse_iterator;
977979
using const_iterator = BlockListType::const_iterator;
@@ -995,9 +997,38 @@ class SILFunction
995997
SILBasicBlock *createBasicBlockAfter(SILBasicBlock *afterBB);
996998
SILBasicBlock *createBasicBlockBefore(SILBasicBlock *beforeBB);
997999

998-
/// Splice the body of \p F into this function at end.
999-
void spliceBody(SILFunction *F) {
1000-
getBlocks().splice(begin(), F->getBlocks());
1000+
/// Removes and destroys \p BB;
1001+
void eraseBlock(SILBasicBlock *BB) {
1002+
assert(BB->getParent() == this);
1003+
BlockList.erase(BB);
1004+
}
1005+
1006+
/// Transfer all blocks of \p F into this function, at the begin of the block
1007+
/// list.
1008+
void moveAllBlocksFromOtherFunction(SILFunction *F) {
1009+
BlockList.splice(begin(), F->BlockList);
1010+
}
1011+
1012+
/// Transfer \p blockInOtherFunction of another function into this function,
1013+
/// before \p insertPointInThisFunction.
1014+
void moveBlockFromOtherFunction(SILBasicBlock *blockInOtherFunction,
1015+
iterator insertPointInThisFunction) {
1016+
SILFunction *otherFunc = blockInOtherFunction->getParent();
1017+
assert(otherFunc != this);
1018+
BlockList.splice(insertPointInThisFunction, otherFunc->BlockList,
1019+
blockInOtherFunction);
1020+
}
1021+
1022+
/// Move block \p BB to immediately before the iterator \p IP.
1023+
///
1024+
/// The block must be part of this function.
1025+
void moveBlockBefore(SILBasicBlock *BB, SILFunction::iterator IP);
1026+
1027+
/// Move block \p BB to immediately after block \p After.
1028+
///
1029+
/// The block must be part of this function.
1030+
void moveBlockAfter(SILBasicBlock *BB, SILBasicBlock *After) {
1031+
moveBlockBefore(BB, std::next(After->getIterator()));
10011032
}
10021033

10031034
/// Return the unique basic block containing a return inst if it

include/swift/SILOptimizer/Utils/PerformanceInlinerUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ class ShortestPathAnalysis {
460460
analyzeLoopsRecursively(Loop, 1);
461461
}
462462
// Compute the distances for the function itself.
463-
solveDataFlow(F->getBlocks(), 0);
463+
solveDataFlow(*F, 0);
464464
}
465465

466466
/// Returns the length of the shortest path of the block \p BB in the loop

lib/SIL/IR/SILBasicBlock.cpp

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,6 @@ using namespace swift;
3131
// SILBasicBlock Implementation
3232
//===----------------------------------------------------------------------===//
3333

34-
SILBasicBlock::SILBasicBlock(SILFunction *parent, SILBasicBlock *relativeToBB,
35-
bool after)
36-
: Parent(parent), PredList(nullptr) {
37-
if (!relativeToBB) {
38-
parent->getBlocks().push_back(this);
39-
} else if (after) {
40-
parent->getBlocks().insertAfter(relativeToBB->getIterator(), this);
41-
} else {
42-
parent->getBlocks().insert(relativeToBB->getIterator(), this);
43-
}
44-
}
4534
SILBasicBlock::~SILBasicBlock() {
4635
if (!getParent()) {
4736
assert(ArgumentList.empty() &&
@@ -134,7 +123,7 @@ SILBasicBlock::iterator SILBasicBlock::erase(SILInstruction *I) {
134123

135124
/// This method unlinks 'self' from the containing SILFunction and deletes it.
136125
void SILBasicBlock::eraseFromParent() {
137-
getParent()->getBlocks().erase(this);
126+
getParent()->eraseBlock(this);
138127
}
139128

140129
void SILBasicBlock::cloneArgumentList(SILBasicBlock *Other) {
@@ -289,23 +278,13 @@ void SILBasicBlock::eraseArgument(int Index) {
289278
/// stay as part of the original basic block. The old basic block is left
290279
/// without a terminator.
291280
SILBasicBlock *SILBasicBlock::split(iterator I) {
292-
SILBasicBlock *New =
293-
new (Parent->getModule()) SILBasicBlock(Parent, this, /*after*/true);
281+
SILBasicBlock *New = Parent->createBasicBlockAfter(this);
294282
// Move all of the specified instructions from the original basic block into
295283
// the new basic block.
296284
New->InstList.splice(New->end(), InstList, I, end());
297285
return New;
298286
}
299287

300-
/// Move the basic block to after the specified basic block in the IR.
301-
void SILBasicBlock::moveAfter(SILBasicBlock *After) {
302-
assert(getParent() && getParent() == After->getParent() &&
303-
"Blocks must be in the same function");
304-
auto InsertPt = std::next(SILFunction::iterator(After));
305-
auto &BlkList = getParent()->getBlocks();
306-
BlkList.splice(InsertPt, BlkList, this);
307-
}
308-
309288
void SILBasicBlock::moveTo(SILBasicBlock::iterator To, SILInstruction *I) {
310289
assert(I->getParent() != this && "Must move from different basic block");
311290
InstList.splice(To, I->getParent()->InstList, I);

lib/SIL/IR/SILFunction.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,17 +357,29 @@ bool SILFunction::isWeakImported() const {
357357
}
358358

359359
SILBasicBlock *SILFunction::createBasicBlock() {
360-
return new (getModule()) SILBasicBlock(this, nullptr, false);
360+
SILBasicBlock *newBlock = new (getModule()) SILBasicBlock(this);
361+
BlockList.push_back(newBlock);
362+
return newBlock;
361363
}
362364

363365
SILBasicBlock *SILFunction::createBasicBlockAfter(SILBasicBlock *afterBB) {
364-
assert(afterBB);
365-
return new (getModule()) SILBasicBlock(this, afterBB, /*after*/ true);
366+
SILBasicBlock *newBlock = new (getModule()) SILBasicBlock(this);
367+
BlockList.insertAfter(afterBB->getIterator(), newBlock);
368+
return newBlock;
366369
}
367370

368371
SILBasicBlock *SILFunction::createBasicBlockBefore(SILBasicBlock *beforeBB) {
369-
assert(beforeBB);
370-
return new (getModule()) SILBasicBlock(this, beforeBB, /*after*/ false);
372+
SILBasicBlock *newBlock = new (getModule()) SILBasicBlock(this);
373+
BlockList.insert(beforeBB->getIterator(), newBlock);
374+
return newBlock;
375+
}
376+
377+
void SILFunction::moveBlockBefore(SILBasicBlock *BB, SILFunction::iterator IP) {
378+
assert(BB->getParent() == this);
379+
if (SILFunction::iterator(BB) == IP)
380+
return;
381+
BlockList.remove(BB);
382+
BlockList.insert(IP, BB);
371383
}
372384

373385
//===----------------------------------------------------------------------===//
@@ -642,10 +654,9 @@ bool SILFunction::isExternallyUsedSymbol() const {
642654
getModule().isWholeModule());
643655
}
644656

645-
void SILFunction::convertToDeclaration() {
646-
assert(isDefinition() && "Can only convert definitions to declarations");
657+
void SILFunction::clear() {
647658
dropAllReferences();
648-
getBlocks().clear();
659+
BlockList.clear();
649660
}
650661

651662
SubstitutionMap SILFunction::getForwardingSubstitutionMap() {

lib/SIL/IR/SILModule.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,8 @@ void SILModule::eraseFunction(SILFunction *F) {
511511

512512
// This opens dead-function-removal opportunities for called functions.
513513
// (References are not needed anymore.)
514-
F->dropAllReferences();
514+
F->clear();
515515
F->dropDynamicallyReplacedFunction();
516-
F->getBlocks().clear();
517516
// Drop references for any _specialize(target:) functions.
518517
F->clearSpecializeAttrs();
519518
}

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5714,8 +5714,7 @@ bool SILParser::parseSILBasicBlock(SILBuilder &B) {
57145714

57155715
// Make sure the block is at the end of the function so that forward
57165716
// references don't affect block layout.
5717-
F->getBlocks().remove(BB);
5718-
F->getBlocks().push_back(BB);
5717+
F->moveBlockBefore(BB, F->end());
57195718

57205719
B.setInsertionPoint(BB);
57215720
do {

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5528,7 +5528,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
55285528
}
55295529

55305530
// Otherwise, verify the body of the function.
5531-
verifyEntryBlock(&*F->getBlocks().begin());
5531+
verifyEntryBlock(F->getEntryBlock());
55325532
verifyEpilogBlocks(F);
55335533
verifyFlowSensitiveRules(F);
55345534
verifyBranches(F);

lib/SILGen/SILGenEpilog.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ prepareForEpilogBlockEmission(SILGenFunction &SGF, SILLocation topLevel,
146146

147147
// Move the epilog block to the end of the ordinary section.
148148
auto endOfOrdinarySection = SGF.StartOfPostmatter;
149-
SGF.B.moveBlockTo(epilogBB, endOfOrdinarySection);
149+
SGF.F.moveBlockBefore(epilogBB, endOfOrdinarySection);
150150

151151
// Emit the epilog into the epilog bb. Its arguments are the
152152
// direct results.
@@ -282,7 +282,7 @@ static bool prepareExtraEpilog(SILGenFunction &SGF, JumpDest &dest,
282282
// Reposition the block to the end of the postmatter section
283283
// unless we're emitting into a single predecessor.
284284
if (reposition) {
285-
SGF.B.moveBlockTo(epilogBB, SGF.F.end());
285+
SGF.F.moveBlockBefore(epilogBB, SGF.F.end());
286286
}
287287

288288
SGF.B.setInsertionPoint(epilogBB);

0 commit comments

Comments
 (0)