1 //===- DomTreeUpdater.cpp - DomTree/Post DomTree Updater --------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the DomTreeUpdater class, which provides a uniform way 10 // to update dominator tree related data structures. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Analysis/DomTreeUpdater.h" 15 #include "llvm/Analysis/GenericDomTreeUpdaterImpl.h" 16 #include "llvm/Analysis/PostDominators.h" 17 #include "llvm/IR/Constants.h" 18 #include "llvm/IR/Instructions.h" 19 #include "llvm/Support/GenericDomTree.h" 20 #include <functional> 21 22 namespace llvm { 23 24 template class GenericDomTreeUpdater<DomTreeUpdater, DominatorTree, 25 PostDominatorTree>; 26 27 template void 28 GenericDomTreeUpdater<DomTreeUpdater, DominatorTree, 29 PostDominatorTree>::recalculate(Function &F); 30 31 bool DomTreeUpdater::forceFlushDeletedBB() { 32 if (DeletedBBs.empty()) 33 return false; 34 35 for (auto *BB : DeletedBBs) { 36 // After calling deleteBB or callbackDeleteBB under Lazy UpdateStrategy, 37 // validateDeleteBB() removes all instructions of DelBB and adds an 38 // UnreachableInst as its terminator. So we check whether the BasicBlock to 39 // delete only has an UnreachableInst inside. 40 assert(BB->size() == 1 && isa<UnreachableInst>(BB->getTerminator()) && 41 "DelBB has been modified while awaiting deletion."); 42 eraseDelBBNode(BB); 43 BB->eraseFromParent(); 44 } 45 DeletedBBs.clear(); 46 Callbacks.clear(); 47 return true; 48 } 49 50 // The DT and PDT require the nodes related to updates 51 // are not deleted when update functions are called. 52 // So BasicBlock deletions must be pended when the 53 // UpdateStrategy is Lazy. When the UpdateStrategy is 54 // Eager, the BasicBlock will be deleted immediately. 55 void DomTreeUpdater::deleteBB(BasicBlock *DelBB) { 56 validateDeleteBB(DelBB); 57 if (Strategy == UpdateStrategy::Lazy) { 58 DeletedBBs.insert(DelBB); 59 return; 60 } 61 62 eraseDelBBNode(DelBB); 63 DelBB->eraseFromParent(); 64 } 65 66 void DomTreeUpdater::callbackDeleteBB( 67 BasicBlock *DelBB, std::function<void(BasicBlock *)> Callback) { 68 validateDeleteBB(DelBB); 69 if (Strategy == UpdateStrategy::Lazy) { 70 Callbacks.push_back(CallBackOnDeletion(DelBB, Callback)); 71 DeletedBBs.insert(DelBB); 72 return; 73 } 74 75 eraseDelBBNode(DelBB); 76 DelBB->removeFromParent(); 77 Callback(DelBB); 78 delete DelBB; 79 } 80 81 void DomTreeUpdater::validateDeleteBB(BasicBlock *DelBB) { 82 assert(DelBB && "Invalid push_back of nullptr DelBB."); 83 assert(pred_empty(DelBB) && "DelBB has one or more predecessors."); 84 // DelBB is unreachable and all its instructions are dead. 85 while (!DelBB->empty()) { 86 Instruction &I = DelBB->back(); 87 // Replace used instructions with an arbitrary value (poison). 88 if (!I.use_empty()) 89 I.replaceAllUsesWith(PoisonValue::get(I.getType())); 90 DelBB->back().eraseFromParent(); 91 } 92 // Make sure DelBB has a valid terminator instruction. As long as DelBB is a 93 // Child of Function F it must contain valid IR. 94 new UnreachableInst(DelBB->getContext(), DelBB); 95 } 96 97 LLVM_DUMP_METHOD 98 void DomTreeUpdater::dump() const { 99 Base::dump(); 100 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 101 raw_ostream &OS = dbgs(); 102 OS << "Pending Callbacks:\n"; 103 int Index = 0; 104 for (const auto &BB : Callbacks) { 105 OS << " " << Index << " : "; 106 ++Index; 107 if (BB->hasName()) 108 OS << BB->getName() << "("; 109 else 110 OS << "(no_name)("; 111 OS << BB << ")\n"; 112 } 113 #endif 114 } 115 116 } // namespace llvm 117