1 //===- DomTreeUpdater.cpp - DomTree/Post DomTree Updater --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the DomTreeUpdater class, which provides a uniform way
10 // to update dominator tree related data structures.
12 //===----------------------------------------------------------------------===//
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"
24 template class GenericDomTreeUpdater
<DomTreeUpdater
, DominatorTree
,
28 GenericDomTreeUpdater
<DomTreeUpdater
, DominatorTree
,
29 PostDominatorTree
>::recalculate(Function
&F
);
32 GenericDomTreeUpdater
<DomTreeUpdater
, DominatorTree
, PostDominatorTree
>::
33 applyUpdatesImpl
</*IsForward=*/true>();
35 GenericDomTreeUpdater
<DomTreeUpdater
, DominatorTree
, PostDominatorTree
>::
36 applyUpdatesImpl
</*IsForward=*/false>();
38 bool DomTreeUpdater::forceFlushDeletedBB() {
39 if (DeletedBBs
.empty())
42 for (auto *BB
: DeletedBBs
) {
43 // After calling deleteBB or callbackDeleteBB under Lazy UpdateStrategy,
44 // validateDeleteBB() removes all instructions of DelBB and adds an
45 // UnreachableInst as its terminator. So we check whether the BasicBlock to
46 // delete only has an UnreachableInst inside.
47 assert(BB
->size() == 1 && isa
<UnreachableInst
>(BB
->getTerminator()) &&
48 "DelBB has been modified while awaiting deletion.");
50 BB
->eraseFromParent();
57 // The DT and PDT require the nodes related to updates
58 // are not deleted when update functions are called.
59 // So BasicBlock deletions must be pended when the
60 // UpdateStrategy is Lazy. When the UpdateStrategy is
61 // Eager, the BasicBlock will be deleted immediately.
62 void DomTreeUpdater::deleteBB(BasicBlock
*DelBB
) {
63 validateDeleteBB(DelBB
);
64 if (Strategy
== UpdateStrategy::Lazy
) {
65 DeletedBBs
.insert(DelBB
);
69 eraseDelBBNode(DelBB
);
70 DelBB
->eraseFromParent();
73 void DomTreeUpdater::callbackDeleteBB(
74 BasicBlock
*DelBB
, std::function
<void(BasicBlock
*)> Callback
) {
75 validateDeleteBB(DelBB
);
76 if (Strategy
== UpdateStrategy::Lazy
) {
77 Callbacks
.push_back(CallBackOnDeletion(DelBB
, Callback
));
78 DeletedBBs
.insert(DelBB
);
82 eraseDelBBNode(DelBB
);
83 DelBB
->removeFromParent();
88 void DomTreeUpdater::validateDeleteBB(BasicBlock
*DelBB
) {
89 assert(DelBB
&& "Invalid push_back of nullptr DelBB.");
90 assert(pred_empty(DelBB
) && "DelBB has one or more predecessors.");
91 // DelBB is unreachable and all its instructions are dead.
92 while (!DelBB
->empty()) {
93 Instruction
&I
= DelBB
->back();
94 // Replace used instructions with an arbitrary value (poison).
96 I
.replaceAllUsesWith(PoisonValue::get(I
.getType()));
97 DelBB
->back().eraseFromParent();
99 // Make sure DelBB has a valid terminator instruction. As long as DelBB is a
100 // Child of Function F it must contain valid IR.
101 new UnreachableInst(DelBB
->getContext(), DelBB
);
105 void DomTreeUpdater::dump() const {
107 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
108 raw_ostream
&OS
= dbgs();
109 OS
<< "Pending Callbacks:\n";
111 for (const auto &BB
: Callbacks
) {
112 OS
<< " " << Index
<< " : ";
115 OS
<< BB
->getName() << "(";