Skip to content

Commit ca1c46b

Browse files
committed
OSSA: SimplifyCFG. Add DeadEndBlocks and pass it to jump-threading.
1 parent 8e4c27d commit ca1c46b

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/SIL/SILUndef.h"
2323
#include "swift/SIL/TerminatorUtils.h"
2424
#include "swift/SIL/BasicBlockDatastructures.h"
25+
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
2526
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
2627
#include "swift/SILOptimizer/Analysis/ProgramTerminationAnalysis.h"
2728
#include "swift/SILOptimizer/Analysis/SimplifyInstruction.h"
@@ -91,6 +92,12 @@ class SimplifyCFG {
9192
SILFunction &Fn;
9293
SILPassManager *PM;
9394

95+
// DeadEndBlocks remains conservatively valid across updates that rewrite
96+
// branches and remove edges. Any transformation that adds a block must call
97+
// updateForReachableBlock(). Removing a block causes a dangling pointer
98+
// within DeadEndBlocks, but this pointer can't be accessed by queries.
99+
DeadEndBlocks *deBlocks = nullptr;
100+
94101
// WorklistList is the actual list that we iterate over (for determinism).
95102
// Slots may be null, which should be ignored.
96103
SmallVector<SILBasicBlock *, 32> WorklistList;
@@ -700,7 +707,7 @@ bool SimplifyCFG::dominatorBasedSimplify(DominanceAnalysis *DA) {
700707
// Do dominator based simplification of terminator condition. This does not
701708
// and MUST NOT change the CFG without updating the dominator tree to
702709
// reflect such change.
703-
if (tryCheckedCastBrJumpThreading(&Fn, DT, BlocksForWorklist,
710+
if (tryCheckedCastBrJumpThreading(&Fn, DT, deBlocks, BlocksForWorklist,
704711
EnableOSSARewriteTerminator)) {
705712
for (auto BB: BlocksForWorklist)
706713
addToWorklist(BB);
@@ -1204,7 +1211,7 @@ bool SimplifyCFG::simplifyBranchOperands(OperandValueArrayRef Operands) {
12041211
// All of our interesting simplifications are on single-value instructions
12051212
// for now.
12061213
if (auto *I = dyn_cast<SingleValueInstruction>(*O)) {
1207-
simplifyAndReplaceAllSimplifiedUsesAndErase(I, callbacks);
1214+
simplifyAndReplaceAllSimplifiedUsesAndErase(I, callbacks, deBlocks);
12081215
}
12091216
}
12101217
return callbacks.hadCallbackInvocation();
@@ -3373,6 +3380,16 @@ bool SimplifyCFG::run() {
33733380
// First remove any block not reachable from the entry.
33743381
bool Changed = removeUnreachableBlocks(Fn);
33753382

3383+
DeadEndBlocksAnalysis *deBlocksAnalysis =
3384+
PM->getAnalysis<DeadEndBlocksAnalysis>();
3385+
if (Changed) {
3386+
// Eliminate unreachable blocks from deBlocks. This isn't strictly necessary
3387+
// but avoids excess dangling pointers in deBlocks.
3388+
deBlocksAnalysis->invalidate(&Fn,
3389+
SILAnalysis::InvalidationKind::Everything);
3390+
}
3391+
deBlocks = deBlocksAnalysis->get(&Fn);
3392+
33763393
// Find the set of loop headers. We don't want to jump-thread through headers.
33773394
findLoopHeaders();
33783395

@@ -3391,11 +3408,15 @@ bool SimplifyCFG::run() {
33913408

33923409
// Do simplifications that require the dominator tree to be accurate.
33933410
DominanceAnalysis *DA = PM->getAnalysis<DominanceAnalysis>();
3394-
33953411
if (Changed) {
33963412
// Force dominator recomputation since we modified the cfg.
33973413
DA->invalidate(&Fn, SILAnalysis::InvalidationKind::Everything);
3414+
// Eliminate unreachable blocks from deBlocks. This isn't strictly necessary
3415+
// but avoids excess dangling pointers in deBlocks.
3416+
deBlocksAnalysis->invalidate(&Fn,
3417+
SILAnalysis::InvalidationKind::Everything);
33983418
}
3419+
deBlocks = deBlocksAnalysis->get(&Fn);
33993420

34003421
Changed |= dominatorBasedSimplify(DA);
34013422

lib/SILOptimizer/Utils/CheckedCastBrJumpThreading.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class CheckedCastBrJumpThreading {
4242
// Dominator information to be used.
4343
DominanceInfo *DT;
4444

45+
// DeadEndBlocks is used by OwnershipRAUW and incrementally updated within
46+
// CheckedCastBrJumpThreading.
47+
//
48+
// TODO: incrementally update dead-end blocks during SimplifyCFG so it doesn't
49+
// need to be recomputed each time tryCheckedCastBrJumpThreading is called.
50+
DeadEndBlocks *deBlocks;
51+
4552
// Enable non-trivial terminator rewriting in OSSA.
4653
bool EnableOSSARewriteTerminator;
4754

@@ -123,10 +130,10 @@ class CheckedCastBrJumpThreading {
123130

124131
public:
125132
CheckedCastBrJumpThreading(
126-
SILFunction *Fn, DominanceInfo *DT,
133+
SILFunction *Fn, DominanceInfo *DT, DeadEndBlocks *deBlocks,
127134
SmallVectorImpl<SILBasicBlock *> &BlocksForWorklist,
128135
bool EnableOSSARewriteTerminator)
129-
: Fn(Fn), DT(DT),
136+
: Fn(Fn), DT(DT), deBlocks(deBlocks),
130137
EnableOSSARewriteTerminator(EnableOSSARewriteTerminator),
131138
BlocksForWorklist(BlocksForWorklist), BlocksToEdit(Fn),
132139
BlocksToClone(Fn) {}
@@ -673,7 +680,7 @@ void CheckedCastBrJumpThreading::optimizeFunction() {
673680
Fn->verifyCriticalEdges();
674681

675682
for (Edit *edit : Edits) {
676-
BasicBlockCloner Cloner(edit->CCBBlock);
683+
BasicBlockCloner Cloner(edit->CCBBlock, deBlocks);
677684
if (!Cloner.canCloneBlock())
678685
continue;
679686

@@ -698,11 +705,12 @@ void CheckedCastBrJumpThreading::optimizeFunction() {
698705
namespace swift {
699706

700707
bool tryCheckedCastBrJumpThreading(
701-
SILFunction *Fn, DominanceInfo *DT,
708+
SILFunction *Fn, DominanceInfo *DT, DeadEndBlocks *deBlocks,
702709
SmallVectorImpl<SILBasicBlock *> &BlocksForWorklist,
703710
bool EnableOSSARewriteTerminator) {
704711

705-
CheckedCastBrJumpThreading CCBJumpThreading(Fn, DT, BlocksForWorklist,
712+
CheckedCastBrJumpThreading CCBJumpThreading(Fn, DT, deBlocks,
713+
BlocksForWorklist,
706714
EnableOSSARewriteTerminator);
707715
CCBJumpThreading.optimizeFunction();
708716
return !BlocksForWorklist.empty();

0 commit comments

Comments
 (0)