Skip to content

Commit 1446952

Browse files
authored
Merge pull request #20111 from atrick/critedge-util
NFC: SILBasicBlock utilties for handling critical edges.
2 parents cd920b6 + 2aa8427 commit 1446952

19 files changed

+372
-330
lines changed

include/swift/SIL/BasicBlockUtils.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,40 @@
1313
#ifndef SWIFT_SIL_DEADENDBLOCKS_H
1414
#define SWIFT_SIL_DEADENDBLOCKS_H
1515

16+
#include "swift/SIL/SILValue.h"
1617
#include "llvm/ADT/SetVector.h"
18+
#include "llvm/ADT/SmallVector.h"
1719

1820
namespace swift {
1921

2022
class SILFunction;
2123
class SILBasicBlock;
24+
class TermInst;
25+
class DominanceInfo;
26+
class SILLoopInfo;
27+
28+
/// \brief Replace a branch target.
29+
///
30+
/// \param T The terminating instruction to modify.
31+
/// \param edgeIdx The successor edges index that will be replaced.
32+
/// \param newDest The new target block.
33+
/// \param preserveArgs If set, preserve arguments on the replaced edge.
34+
void changeBranchTarget(TermInst *T, unsigned edgeIdx, SILBasicBlock *newDest,
35+
bool preserveArgs);
36+
37+
/// Returns the arguments values on the specified CFG edge. If necessary, may
38+
/// add create new SILPHIArguments, using `NewEdgeBB` as the placeholder.
39+
void getEdgeArgs(TermInst *T, unsigned edgeIdx, SILBasicBlock *newEdgeBB,
40+
llvm::SmallVectorImpl<SILValue> &args);
41+
42+
/// \brief Splits the edge from terminator.
43+
///
44+
/// Also updates dominance and loop information if not null.
45+
///
46+
/// Returns the newly created basic block.
47+
SILBasicBlock *splitEdge(TermInst *T, unsigned edgeIdx,
48+
DominanceInfo *DT = nullptr,
49+
SILLoopInfo *LI = nullptr);
2250

2351
/// \brief Merge a basic block ending in a branch with its successor
2452
/// if possible.

include/swift/SIL/SILBuilder.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,18 @@ class SILBuilder {
172172
/// Build instructions before the given insertion point, inheriting the debug
173173
/// location.
174174
///
175-
/// Clients should prefer this constructor.
175+
/// SILBuilderContext must outlive this SILBuilder instance.
176176
SILBuilder(SILInstruction *I, const SILDebugScope *DS, SILBuilderContext &C)
177177
: TempContext(C.getModule()), C(C), F(I->getFunction()) {
178178
assert(DS && "instruction has no debug scope");
179179
setCurrentDebugScope(DS);
180180
setInsertionPoint(I);
181181
}
182182

183+
/// Build instructions before the given insertion point, inheriting the debug
184+
/// location.
185+
///
186+
/// SILBuilderContext must outlive this SILBuilder instance.
183187
SILBuilder(SILBasicBlock *BB, const SILDebugScope *DS, SILBuilderContext &C)
184188
: TempContext(C.getModule()), C(C), F(BB->getParent()) {
185189
assert(DS && "block has no debug scope");
@@ -371,6 +375,16 @@ class SILBuilder {
371375
/// continuation block.
372376
SILBasicBlock *splitBlockForFallthrough();
373377

378+
/// Convenience for creating a fall-through basic block on-the-fly without
379+
/// affecting the insertion point.
380+
SILBasicBlock *createFallthroughBlock(SILLocation loc,
381+
SILBasicBlock *targetBB) {
382+
auto *newBB = F->createBasicBlock();
383+
SILBuilder(newBB, this->getCurrentDebugScope(), this->getBuilderContext())
384+
.createBranch(loc, targetBB);
385+
return newBB;
386+
}
387+
374388
//===--------------------------------------------------------------------===//
375389
// SILInstruction Creation Methods
376390
//===--------------------------------------------------------------------===//

include/swift/SIL/SILCloner.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,13 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
285285
///
286286
/// Assumes that `isValueCloned` is true.
287287
SILValue getOpValue(SILValue Value) {
288-
return asImpl().remapValue(Value);
288+
return asImpl().getMappedValue(Value);
289289
}
290290
template <size_t N, typename ArrayRefType>
291291
SmallVector<SILValue, N> getOpValueArray(ArrayRefType Values) {
292292
SmallVector<SILValue, N> Ret(Values.size());
293293
for (unsigned i = 0, e = Values.size(); i != e; ++i)
294-
Ret[i] = asImpl().remapValue(Values[i]);
294+
Ret[i] = asImpl().getMappedValue(Values[i]);
295295
return Ret;
296296
}
297297

@@ -351,7 +351,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
351351
ProtocolConformanceRef remapConformance(Type Ty, ProtocolConformanceRef C) {
352352
return C;
353353
}
354-
SILValue remapValue(SILValue Value);
354+
SILValue getMappedValue(SILValue Value);
355355
SILFunction *remapFunction(SILFunction *Func) { return Func; }
356356
SILBasicBlock *remapBasicBlock(SILBasicBlock *BB);
357357
void postProcess(SILInstruction *Orig, SILInstruction *Cloned);
@@ -472,7 +472,7 @@ class SILClonerWithScopes : public SILCloner<ImplClass> {
472472

473473
template<typename ImplClass>
474474
SILValue
475-
SILCloner<ImplClass>::remapValue(SILValue Value) {
475+
SILCloner<ImplClass>::getMappedValue(SILValue Value) {
476476
auto VI = ValueMap.find(Value);
477477
if (VI != ValueMap.end())
478478
return VI->second;

include/swift/SILOptimizer/Utils/CFG.h

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,14 @@ TermInst *addNewEdgeValueToBranch(TermInst *Branch, SILBasicBlock *Dest,
4545
TermInst *changeEdgeValue(TermInst *Branch, SILBasicBlock *Dest, size_t Idx,
4646
SILValue Val);
4747

48-
/// \brief Replace a branch target.
49-
///
50-
/// \param T The terminating instruction to modify.
51-
/// \param EdgeIdx The successor edges index that will be replaced.
52-
/// \param NewDest The new target block.
53-
/// \param PreserveArgs If set, preserve arguments on the replaced edge.
54-
void changeBranchTarget(TermInst *T, unsigned EdgeIdx, SILBasicBlock *NewDest,
55-
bool PreserveArgs);
56-
5748
/// \brief Replace a branch target.
5849
///
5950
/// \param T The terminating instruction to modify.
6051
/// \param OldDest The successor block that will be replaced.
6152
/// \param NewDest The new target block.
6253
/// \param PreserveArgs If set, preserve arguments on the replaced edge.
63-
void replaceBranchTarget(TermInst *T, SILBasicBlock *OldDest, SILBasicBlock *NewDest,
64-
bool PreserveArgs);
54+
void replaceBranchTarget(TermInst *T, SILBasicBlock *OldDest,
55+
SILBasicBlock *NewDest, bool PreserveArgs);
6556

6657
/// \brief Check if the edge from the terminator is critical.
6758
bool isCriticalEdge(TermInst *T, unsigned EdgeIdx);
@@ -85,13 +76,9 @@ SILBasicBlock *splitIfCriticalEdge(SILBasicBlock *From, SILBasicBlock *To,
8576
DominanceInfo *DT = nullptr,
8677
SILLoopInfo *LI = nullptr);
8778

88-
/// \brief Splits the edge from terminator.
89-
///
90-
/// Updates dominance information and loop information if not null.
91-
/// Returns the newly created basic block.
92-
SILBasicBlock *splitEdge(TermInst *T, unsigned EdgeIdx,
93-
DominanceInfo *DT = nullptr,
94-
SILLoopInfo *LI = nullptr);
79+
/// Splits all critical edges originating from `fromBB`.
80+
bool splitCriticalEdgesFrom(SILBasicBlock *fromBB, DominanceInfo *DT = nullptr,
81+
SILLoopInfo *LI = nullptr);
9582

9683
/// \brief Splits the edges between two basic blocks.
9784
///

include/swift/SILOptimizer/Utils/Local.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ class BaseThreadingCloner : public SILClonerWithScopes<BaseThreadingCloner> {
322322

323323
SILBasicBlock *remapBasicBlock(SILBasicBlock *BB) { return BB; }
324324

325-
SILValue remapValue(SILValue Value) {
325+
SILValue getMappedValue(SILValue Value) {
326326
// If this is a use of an instruction in another block, then just use it.
327327
if (auto SI = Value->getDefiningInstruction()) {
328328
if (SI->getParent() != FromBB)
@@ -334,8 +334,7 @@ class BaseThreadingCloner : public SILClonerWithScopes<BaseThreadingCloner> {
334334
assert(isa<SILUndef>(Value) && "Unexpected Value kind");
335335
return Value;
336336
}
337-
338-
return SILCloner<BaseThreadingCloner>::remapValue(Value);
337+
return SILCloner<BaseThreadingCloner>::getMappedValue(Value);
339338
}
340339

341340
void postProcess(SILInstruction *Orig, SILInstruction *Cloned) {

0 commit comments

Comments
 (0)