Skip to content

Commit 93d6f93

Browse files
committed
Fixes to SimplifyCFG::threadEdge for OSSA
1 parent 05c7d64 commit 93d6f93

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

+25-18
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ bool SimplifyCFG::threadEdge(const ThreadInfo &ti) {
185185
return false;
186186

187187
Cloner.cloneBranchTarget(SrcTerm);
188-
189188
// We have copied the threaded block into the edge.
190189
auto *clonedSrc = Cloner.getNewBB();
191190
SmallVector<SILBasicBlock *, 4> clonedSuccessors(
@@ -209,7 +208,6 @@ bool SimplifyCFG::threadEdge(const ThreadInfo &ti) {
209208
ThreadedSuccessorBlock, Args);
210209

211210
CondTerm->eraseFromParent();
212-
213211
} else {
214212
// Get the enum element and the destination block of the block we jump
215213
// thread.
@@ -219,18 +217,24 @@ bool SimplifyCFG::threadEdge(const ThreadInfo &ti) {
219217
// Instantiate the payload if necessary.
220218
SILBuilderWithScope Builder(SEI);
221219
if (!ThreadedSuccessorBlock->args_empty()) {
222-
auto EnumVal = SEI->getOperand();
223-
auto EnumTy = EnumVal->getType();
224-
auto Loc = SEI->getLoc();
225-
auto Ty = EnumTy.getEnumElementType(ti.EnumCase, SEI->getModule(),
226-
Builder.getTypeExpansionContext());
227-
SILValue UED(
228-
Builder.createUncheckedEnumData(Loc, EnumVal, ti.EnumCase, Ty));
229-
assert(UED->getType()
230-
== (*ThreadedSuccessorBlock->args_begin())->getType()
231-
&& "Argument types must match");
232-
Builder.createBranch(SEI->getLoc(), ThreadedSuccessorBlock, {UED});
233-
220+
if (ti.EnumCase->hasAssociatedValues() &&
221+
(!SEI->hasDefault() ||
222+
ThreadedSuccessorBlock != SEI->getDefaultBB())) {
223+
auto EnumVal = SEI->getOperand();
224+
auto EnumTy = EnumVal->getType();
225+
auto Loc = SEI->getLoc();
226+
auto Ty = EnumTy.getEnumElementType(ti.EnumCase, SEI->getModule(),
227+
Builder.getTypeExpansionContext());
228+
SILValue UED(
229+
Builder.createUncheckedEnumData(Loc, EnumVal, ti.EnumCase, Ty));
230+
assert(UED->getType() ==
231+
(*ThreadedSuccessorBlock->args_begin())->getType() &&
232+
"Argument types must match");
233+
Builder.createBranch(SEI->getLoc(), ThreadedSuccessorBlock, {UED});
234+
} else {
235+
assert(SEI->getDefaultBB() == ThreadedSuccessorBlock);
236+
Builder.createBranch(SEI->getLoc(), ThreadedSuccessorBlock, SEI->getOperand());
237+
}
234238
} else {
235239
Builder.createBranch(SEI->getLoc(), ThreadedSuccessorBlock,
236240
ArrayRef<SILValue>());
@@ -497,11 +501,14 @@ bool SimplifyCFG::simplifyThreadedTerminators() {
497501
if (auto *EI = dyn_cast<EnumInst>(SEI->getOperand())) {
498502
LLVM_DEBUG(llvm::dbgs() << "simplify threaded " << *SEI);
499503
auto *LiveBlock = SEI->getCaseDestination(EI->getElement());
500-
if (EI->hasOperand() && !LiveBlock->args_empty())
501-
SILBuilderWithScope(SEI)
502-
.createBranch(SEI->getLoc(), LiveBlock, EI->getOperand());
503-
else
504+
if (EI->hasOperand() && !LiveBlock->args_empty()) {
505+
SILBuilderWithScope(SEI).createBranch(SEI->getLoc(), LiveBlock,
506+
EI->getOperand());
507+
} else if (!LiveBlock->args_empty()) {
508+
SILBuilderWithScope(SEI).createBranch(SEI->getLoc(), LiveBlock, {EI});
509+
} else {
504510
SILBuilderWithScope(SEI).createBranch(SEI->getLoc(), LiveBlock);
511+
}
505512
SEI->eraseFromParent();
506513
if (EI->use_empty())
507514
EI->eraseFromParent();

0 commit comments

Comments
 (0)