Skip to content

Commit ff1f419

Browse files
authored
Merge pull request #38456 from atrick/ossa-jumpthread-test
OSSA: migrate & organize simplify-cfg tests
2 parents 00786d9 + 59ad8c9 commit ff1f419

28 files changed

+3475
-158
lines changed

include/swift/SIL/BasicBlockUtils.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ class DeadEndBlocks {
8787
/// Used to determine if we need to verify a DeadEndBlocks.
8888
bool isComputed() const { return didComputeValue; }
8989

90+
/// Add any (new) blocks that are backward-reachable from \p reachableBB to
91+
/// the set of reachable blocks.
92+
void updateForReachableBlock(SILBasicBlock *reachableBB);
93+
9094
const SILFunction *getFunction() const { return f; }
9195

9296
/// Performs a simple check if \p block (or its single successor) ends in an
@@ -95,6 +99,9 @@ class DeadEndBlocks {
9599
/// This handles the common case of failure-handling blocks, which e.g.
96100
/// contain a call to fatalError().
97101
static bool triviallyEndsInUnreachable(SILBasicBlock *block);
102+
103+
protected:
104+
void propagateNewlyReachableBlocks(unsigned startIdx);
98105
};
99106

100107
/// Compute joint-postdominating set for \p dominatingBlock and \p

include/swift/SIL/SILArgument.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class SILArgument : public ValueBase {
183183

184184
/// Return the terminator instruction for which this argument is a result,
185185
/// otherwise return nullptr.
186-
TermInst *getTerminatorForResultArg() const;
186+
TermInst *getTerminatorForResult() const;
187187

188188
/// Return the SILArgumentKind of this argument.
189189
SILArgumentKind getKind() const {
@@ -299,7 +299,7 @@ class SILPhiArgument : public SILArgument {
299299

300300
/// Return the terminator instruction for which this argument is a result,
301301
/// otherwise return nullptr.
302-
TermInst *getTerminatorForResultArg() const;
302+
TermInst *getTerminatorForResult() const;
303303

304304
static bool classof(const SILInstruction *) = delete;
305305
static bool classof(const SILUndef *) = delete;
@@ -445,10 +445,10 @@ inline TermInst *SILArgument::getSingleTerminator() const {
445445
llvm_unreachable("Covered switch is not covered?!");
446446
}
447447

448-
inline TermInst *SILArgument::getTerminatorForResultArg() const {
448+
inline TermInst *SILArgument::getTerminatorForResult() const {
449449
switch (getKind()) {
450450
case SILArgumentKind::SILPhiArgument:
451-
return cast<SILPhiArgument>(this)->getTerminatorForResultArg();
451+
return cast<SILPhiArgument>(this)->getTerminatorForResult();
452452
case SILArgumentKind::SILFunctionArgument:
453453
return nullptr;
454454
}

include/swift/SILOptimizer/Utils/BasicBlockOptUtils.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ class BasicBlockCloner : public SILCloner<BasicBlockCloner> {
170170
/// state is reset each time analyzeAddressProjections is called.
171171
SinkAddressProjections sinkProj;
172172

173+
// If available, the current DeadEndBlocks for incremental update.
174+
DeadEndBlocks *deBlocks;
175+
173176
public:
174177
/// An ordered list of old to new available value pairs.
175178
///
@@ -178,8 +181,8 @@ class BasicBlockCloner : public SILCloner<BasicBlockCloner> {
178181
SmallVector<std::pair<SILValue, SILValue>, 16> availVals;
179182

180183
// Clone blocks starting at `origBB`, within the same function.
181-
BasicBlockCloner(SILBasicBlock *origBB)
182-
: SILCloner(*origBB->getParent()), origBB(origBB) {}
184+
BasicBlockCloner(SILBasicBlock *origBB, DeadEndBlocks *deBlocks = nullptr)
185+
: SILCloner(*origBB->getParent()), origBB(origBB), deBlocks(deBlocks) {}
183186

184187
bool canCloneBlock() {
185188
for (auto &inst : *origBB) {
@@ -218,6 +221,12 @@ class BasicBlockCloner : public SILCloner<BasicBlockCloner> {
218221
successorBBs.reserve(origBB->getSuccessors().size());
219222
llvm::copy(origBB->getSuccessors(), std::back_inserter(successorBBs));
220223
cloneReachableBlocks(origBB, successorBBs, insertAfterBB);
224+
225+
if (deBlocks) {
226+
for (auto *succBB : successorBBs) {
227+
deBlocks->updateForReachableBlock(succBB);
228+
}
229+
}
221230
}
222231

223232
/// Clone the given branch instruction's destination block, splitting

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
namespace swift {
3535

3636
class DominanceInfo;
37+
class DeadEndBlocks;
3738
template <class T> class NullablePtr;
3839

3940
/// Transform a Use Range (Operand*) into a User Range (SILInstruction *)
@@ -202,8 +203,9 @@ SILLinkage getSpecializedLinkage(SILFunction *f, SILLinkage linkage);
202203
/// Tries to perform jump-threading on all checked_cast_br instruction in
203204
/// function \p Fn.
204205
bool tryCheckedCastBrJumpThreading(
205-
SILFunction *fn, DominanceInfo *dt,
206-
SmallVectorImpl<SILBasicBlock *> &blocksForWorklist);
206+
SILFunction *fn, DominanceInfo *dt, DeadEndBlocks *deBlocks,
207+
SmallVectorImpl<SILBasicBlock *> &blocksForWorklist,
208+
bool EnableOSSARewriteTerminator);
207209

208210
/// A utility for deleting one or more instructions belonging to a function, and
209211
/// cleaning up any dead code resulting from deleting those instructions. Use

lib/SIL/IR/SILArgument.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ TermInst *SILPhiArgument::getSingleTerminator() const {
316316
return const_cast<SILBasicBlock *>(predBlock)->getTerminator();
317317
}
318318

319-
TermInst *SILPhiArgument::getTerminatorForResultArg() const {
319+
TermInst *SILPhiArgument::getTerminatorForResult() const {
320320
if (auto *termInst = getSingleTerminator()) {
321321
if (!isa<BranchInst>(termInst) && !isa<CondBranchInst>(termInst))
322322
return termInst;

lib/SIL/Utils/BasicBlockUtils.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ void swift::getEdgeArgs(TermInst *T, unsigned edgeIdx, SILBasicBlock *newEdgeBB,
174174
if (!succBB->getNumArguments())
175175
return;
176176
args.push_back(newEdgeBB->createPhiArgument(
177-
succBB->getArgument(0)->getType(), OwnershipKind::Owned));
177+
succBB->getArgument(0)->getType(),
178+
succBB->getArgument(0)->getOwnershipKind()));
178179
return;
179180
}
180181

@@ -186,7 +187,8 @@ void swift::getEdgeArgs(TermInst *T, unsigned edgeIdx, SILBasicBlock *newEdgeBB,
186187
if (!succBB->getNumArguments())
187188
return;
188189
args.push_back(newEdgeBB->createPhiArgument(
189-
succBB->getArgument(0)->getType(), OwnershipKind::Owned));
190+
succBB->getArgument(0)->getType(),
191+
succBB->getArgument(0)->getOwnershipKind()));
190192
return;
191193
}
192194

@@ -366,6 +368,15 @@ void swift::mergeBasicBlockWithSingleSuccessor(SILBasicBlock *BB,
366368
// DeadEndBlocks
367369
//===----------------------------------------------------------------------===//
368370

371+
// Propagate the reachability up the control flow graph.
372+
void DeadEndBlocks::propagateNewlyReachableBlocks(unsigned startIdx) {
373+
for (unsigned idx = startIdx; idx < reachableBlocks.size(); ++idx) {
374+
const SILBasicBlock *bb = reachableBlocks[idx];
375+
for (SILBasicBlock *predBB : bb->getPredecessorBlocks())
376+
reachableBlocks.insert(predBB);
377+
}
378+
}
379+
369380
void DeadEndBlocks::compute() {
370381
assert(reachableBlocks.empty() && "Computed twice");
371382

@@ -377,13 +388,19 @@ void DeadEndBlocks::compute() {
377388
if (TI->isFunctionExiting())
378389
reachableBlocks.insert(&BB);
379390
}
380-
// Propagate the reachability up the control flow graph.
381-
unsigned Idx = 0;
382-
while (Idx < reachableBlocks.size()) {
383-
const SILBasicBlock *BB = reachableBlocks[Idx++];
384-
for (SILBasicBlock *Pred : BB->getPredecessorBlocks())
385-
reachableBlocks.insert(Pred);
391+
propagateNewlyReachableBlocks(0);
392+
}
393+
394+
void DeadEndBlocks::updateForReachableBlock(SILBasicBlock *reachableBB) {
395+
if (!didComputeValue)
396+
return;
397+
398+
assert(reachableBlocks.count(reachableBB));
399+
unsigned numReachable = reachableBlocks.size();
400+
for (SILBasicBlock *predBB : reachableBB->getPredecessorBlocks()) {
401+
reachableBlocks.insert(predBB);
386402
}
403+
propagateNewlyReachableBlocks(numReachable);
387404
}
388405

389406
bool DeadEndBlocks::triviallyEndsInUnreachable(SILBasicBlock *block) {

0 commit comments

Comments
 (0)