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