Skip to content

Commit bc36b2a

Browse files
committed
Improve handling of unreachable blocks in mem2reg.
We were handling regular uses, but not handling promotions in things like debug_value_addr. This was exposed by some pass ordering changes I have in an upcoming commit.
1 parent 9b4f24e commit bc36b2a

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,8 @@ StackAllocationPromoter::getLiveInValue(BlockSet &PhiBlocks,
568568
return BB->getBBArg(BB->getNumBBArg()-1);
569569
}
570570

571-
assert(DT->getNode(BB) && "Block is not in dominator tree!");
571+
if (BB->pred_empty() || !DT->getNode(BB))
572+
return SILUndef::get(ASI->getElementType(), ASI->getModule());
572573

573574
// No phi for this value in this block means that the value flowing
574575
// out of the immediate dominator reaches here.
@@ -610,14 +611,10 @@ void StackAllocationPromoter::fixBranchesAndUses(BlockSet &PhiBlocks) {
610611
// examining is a value, replace it with undef. Either way, delete
611612
// the instruction and move on.
612613
SILBasicBlock *BB = LI->getParent();
613-
if (BB->pred_empty() || !DT->getNode(BB)) {
614-
Def = SILUndef::get(ASI->getElementType(), ASI->getModule());
615-
} else {
616-
Def = getLiveInValue(PhiBlocks, BB);
617-
}
618-
614+
Def = getLiveInValue(PhiBlocks, BB);
615+
619616
DEBUG(llvm::dbgs() << "*** Replacing " << *LI << " with Def " << *Def);
620-
617+
621618
// Replace the load with the definition that we found.
622619
replaceLoad(LI, Def, ASI);
623620
removedUser = true;

test/SILOptimizer/mem2reg_unreachable.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,22 @@ bb3: // Preds: bb1 bb2
3737
return %20 : $Int32 // id: %22
3838
}
3939

40+
struct S {}
41+
42+
sil hidden @handle_unreachable : $@convention(thin) () -> () {
43+
bb0:
44+
%0 = alloc_stack $S, var, name "x"
45+
%1 = struct $S ()
46+
store %1 to %0 : $*S
47+
unreachable
48+
49+
bb1:
50+
debug_value_addr %0 : $*S, let, name "newvalue", argno 1
51+
br bb2
52+
53+
bb2:
54+
%3 = load %0 : $*S
55+
dealloc_stack %0 : $*S
56+
%4 = tuple ()
57+
return %4 : $()
58+
}

0 commit comments

Comments
 (0)