@@ -185,7 +185,6 @@ bool SimplifyCFG::threadEdge(const ThreadInfo &ti) {
185
185
return false ;
186
186
187
187
Cloner.cloneBranchTarget (SrcTerm);
188
-
189
188
// We have copied the threaded block into the edge.
190
189
auto *clonedSrc = Cloner.getNewBB ();
191
190
SmallVector<SILBasicBlock *, 4 > clonedSuccessors (
@@ -209,7 +208,6 @@ bool SimplifyCFG::threadEdge(const ThreadInfo &ti) {
209
208
ThreadedSuccessorBlock, Args);
210
209
211
210
CondTerm->eraseFromParent ();
212
-
213
211
} else {
214
212
// Get the enum element and the destination block of the block we jump
215
213
// thread.
@@ -219,18 +217,24 @@ bool SimplifyCFG::threadEdge(const ThreadInfo &ti) {
219
217
// Instantiate the payload if necessary.
220
218
SILBuilderWithScope Builder (SEI);
221
219
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
+ }
234
238
} else {
235
239
Builder.createBranch (SEI->getLoc (), ThreadedSuccessorBlock,
236
240
ArrayRef<SILValue>());
@@ -497,11 +501,14 @@ bool SimplifyCFG::simplifyThreadedTerminators() {
497
501
if (auto *EI = dyn_cast<EnumInst>(SEI->getOperand ())) {
498
502
LLVM_DEBUG (llvm::dbgs () << " simplify threaded " << *SEI);
499
503
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 {
504
510
SILBuilderWithScope (SEI).createBranch (SEI->getLoc (), LiveBlock);
511
+ }
505
512
SEI->eraseFromParent ();
506
513
if (EI->use_empty ())
507
514
EI->eraseFromParent ();
0 commit comments