@@ -58,94 +58,37 @@ static cl::opt<unsigned> MaxDeoptOrUnreachableSuccessorCheckDepth(
5858 " is followed by a block that either has a terminating "
5959 " deoptimizing call or is terminated with an unreachable" ));
6060
61- // / Zap all the instructions in the block and replace them with an unreachable
62- // / instruction and notify the basic block's successors that one of their
63- // / predecessors is going away.
64- static void
65- emptyAndDetachBlock (BasicBlock *BB,
66- SmallVectorImpl<DominatorTree::UpdateType> *Updates,
67- bool KeepOneInputPHIs) {
68- // Loop through all of our successors and make sure they know that one
69- // of their predecessors is going away.
70- SmallPtrSet<BasicBlock *, 4 > UniqueSuccessors;
71- for (BasicBlock *Succ : successors (BB)) {
72- Succ->removePredecessor (BB, KeepOneInputPHIs);
73- if (Updates && UniqueSuccessors.insert (Succ).second )
74- Updates->push_back ({DominatorTree::Delete, BB, Succ});
75- }
76-
77- // Zap all the instructions in the block.
78- while (!BB->empty ()) {
79- Instruction &I = BB->back ();
80- // If this instruction is used, replace uses with an arbitrary value.
81- // Because control flow can't get here, we don't care what we replace the
82- // value with. Note that since this block is unreachable, and all values
83- // contained within it must dominate their uses, that all uses will
84- // eventually be removed (they are themselves dead).
85- if (!I.use_empty ())
86- I.replaceAllUsesWith (PoisonValue::get (I.getType ()));
87- BB->back ().eraseFromParent ();
88- }
89- new UnreachableInst (BB->getContext (), BB);
90- assert (BB->size () == 1 && isa<UnreachableInst>(BB->getTerminator ()) &&
91- " The successor list of BB isn't empty before "
92- " applying corresponding DTU updates." );
93- }
94-
95- void llvm::detachDeadBlocks (ArrayRef<BasicBlock *> BBs,
96- SmallVectorImpl<DominatorTree::UpdateType> *Updates,
97- bool KeepOneInputPHIs) {
61+ void llvm::detachDeadBlocks (
62+ ArrayRef<BasicBlock *> BBs,
63+ SmallVectorImpl<DominatorTree::UpdateType> *Updates,
64+ bool KeepOneInputPHIs) {
9865 for (auto *BB : BBs) {
99- auto NonFirstPhiIt = BB->getFirstNonPHIIt ();
100- if (NonFirstPhiIt != BB->end ()) {
101- Instruction &I = *NonFirstPhiIt;
102- // Exception handling funclets need to be explicitly addressed.
103- // These funclets must begin with cleanuppad or catchpad and end with
104- // cleanupred or catchret. The return instructions can be in different
105- // basic blocks than the pad instruction. If we would only delete the
106- // first block, the we would have possible cleanupret and catchret
107- // instructions with poison arguments, which wouldn't be valid.
108- if (isa<FuncletPadInst>(I)) {
109- for (User *User : make_early_inc_range (I.users ())) {
110- Instruction *ReturnInstr = dyn_cast<Instruction>(User);
111- // If we have a cleanupret or catchret block, replace it with just an
112- // unreachable. The other alternative, that may use a catchpad is a
113- // catchswitch. That does not need special handling for now.
114- if (isa<CatchReturnInst>(ReturnInstr) ||
115- isa<CleanupReturnInst>(ReturnInstr)) {
116- BasicBlock *ReturnInstrBB = ReturnInstr->getParent ();
117- // This catchret or catchpad basic block is detached now. Let the
118- // successors know it.
119- // This basic block also may have some predecessors too. For
120- // example the following LLVM-IR is valid:
121- //
122- // [cleanuppad_block]
123- // |
124- // [regular_block]
125- // |
126- // [cleanupret_block]
127- //
128- // The IR after the cleanup will look like this:
129- //
130- // [cleanuppad_block]
131- // |
132- // [regular_block]
133- // |
134- // [unreachable]
135- //
136- // So regular_block will lead to an unreachable block, which is also
137- // valid. There is no need to replace regular_block with unreachable
138- // in this context now.
139- // On the other hand, the cleanupret/catchret block's successors
140- // need to know about the deletion of their predecessors.
141- emptyAndDetachBlock (ReturnInstrBB, Updates, KeepOneInputPHIs);
142- }
143- }
144- }
66+ // Loop through all of our successors and make sure they know that one
67+ // of their predecessors is going away.
68+ SmallPtrSet<BasicBlock *, 4 > UniqueSuccessors;
69+ for (BasicBlock *Succ : successors (BB)) {
70+ Succ->removePredecessor (BB, KeepOneInputPHIs);
71+ if (Updates && UniqueSuccessors.insert (Succ).second )
72+ Updates->push_back ({DominatorTree::Delete, BB, Succ});
14573 }
14674
147- // Detaching and emptying the current basic block.
148- emptyAndDetachBlock (BB, Updates, KeepOneInputPHIs);
75+ // Zap all the instructions in the block.
76+ while (!BB->empty ()) {
77+ Instruction &I = BB->back ();
78+ // If this instruction is used, replace uses with an arbitrary value.
79+ // Because control flow can't get here, we don't care what we replace the
80+ // value with. Note that since this block is unreachable, and all values
81+ // contained within it must dominate their uses, that all uses will
82+ // eventually be removed (they are themselves dead).
83+ if (!I.use_empty ())
84+ I.replaceAllUsesWith (PoisonValue::get (I.getType ()));
85+ BB->back ().eraseFromParent ();
86+ }
87+ new UnreachableInst (BB->getContext (), BB);
88+ assert (BB->size () == 1 &&
89+ isa<UnreachableInst>(BB->getTerminator ()) &&
90+ " The successor list of BB isn't empty before "
91+ " applying corresponding DTU updates." );
14992 }
15093}
15194
0 commit comments