Skip to content

Commit cc6ca09

Browse files
committed
[SimplifyCFG] Hoist common code when succ is unreachable block
1 parent 29e07b8 commit cc6ca09

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,10 +1866,19 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(Instruction *TI,
18661866
// If either of the blocks has it's address taken, then we can't do this fold,
18671867
// because the code we'd hoist would no longer run when we jump into the block
18681868
// by it's address.
1869-
for (auto *Succ : successors(BB))
1870-
if (Succ->hasAddressTaken() || !Succ->getSinglePredecessor())
1869+
for (auto *Succ : successors(BB)) {
1870+
if (Succ->hasAddressTaken())
18711871
return false;
1872-
1872+
if (Succ->getSinglePredecessor())
1873+
continue;
1874+
// If Succ has >1 predecessors, continue to check if the Succ is terminated
1875+
// by an `unreachable` inst. Since executing `unreachable` inst is an UB, we
1876+
// can relax the condition based on the assumptiom that the program would
1877+
// never enter Succ and trigger an UB.
1878+
if (isa<UnreachableInst>(Succ->getTerminator()))
1879+
continue;
1880+
return false;
1881+
}
18731882
// The second of pair is a SkipFlags bitmask.
18741883
using SuccIterPair = std::pair<BasicBlock::iterator, unsigned>;
18751884
SmallVector<SuccIterPair, 8> SuccIterPairs;

0 commit comments

Comments
 (0)