LLVM 19.0.0git
DomTreeUpdater.cpp
Go to the documentation of this file.
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
15#include "llvm/ADT/SmallSet.h"
18#include "llvm/IR/Constants.h"
21#include <algorithm>
22#include <functional>
23#include <utility>
24
25namespace llvm {
26
27template class GenericDomTreeUpdater<DomTreeUpdater, DominatorTree,
28 PostDominatorTree>;
29
30template void
31GenericDomTreeUpdater<DomTreeUpdater, DominatorTree,
32 PostDominatorTree>::recalculate(Function &F);
33
34bool 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 BB->removeFromParent();
47 delete BB;
48 }
50 Callbacks.clear();
51 return true;
52}
53
54// The DT and PDT require the nodes related to updates
55// are not deleted when update functions are called.
56// So BasicBlock deletions must be pended when the
57// UpdateStrategy is Lazy. When the UpdateStrategy is
58// Eager, the BasicBlock will be deleted immediately.
60 validateDeleteBB(DelBB);
61 if (Strategy == UpdateStrategy::Lazy) {
62 DeletedBBs.insert(DelBB);
63 return;
64 }
65
66 DelBB->removeFromParent();
67 eraseDelBBNode(DelBB);
68 delete DelBB;
69}
70
72 BasicBlock *DelBB, std::function<void(BasicBlock *)> Callback) {
73 validateDeleteBB(DelBB);
74 if (Strategy == UpdateStrategy::Lazy) {
75 Callbacks.push_back(CallBackOnDeletion(DelBB, Callback));
76 DeletedBBs.insert(DelBB);
77 return;
78 }
79
80 DelBB->removeFromParent();
81 eraseDelBBNode(DelBB);
82 Callback(DelBB);
83 delete DelBB;
84}
85
86void DomTreeUpdater::validateDeleteBB(BasicBlock *DelBB) {
87 assert(DelBB && "Invalid push_back of nullptr DelBB.");
88 assert(pred_empty(DelBB) && "DelBB has one or more predecessors.");
89 // DelBB is unreachable and all its instructions are dead.
90 while (!DelBB->empty()) {
91 Instruction &I = DelBB->back();
92 // Replace used instructions with an arbitrary value (poison).
93 if (!I.use_empty())
94 I.replaceAllUsesWith(PoisonValue::get(I.getType()));
95 DelBB->back().eraseFromParent();
96 }
97 // Make sure DelBB has a valid terminator instruction. As long as DelBB is a
98 // Child of Function F it must contain valid IR.
99 new UnreachableInst(DelBB->getContext(), DelBB);
100}
101
103void DomTreeUpdater::dump() const {
104 Base::dump();
105#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
106 raw_ostream &OS = dbgs();
107 OS << "Pending Callbacks:\n";
108 int Index = 0;
109 for (const auto &BB : Callbacks) {
110 OS << " " << Index << " : ";
111 ++Index;
112 if (BB->hasName())
113 OS << BB->getName() << "(";
114 else
115 OS << "(no_name)(";
116 OS << BB << ")\n";
117 }
118#endif
119}
120
121} // namespace llvm
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:537
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines a set of templates that efficiently compute a dominator tree over a generic graph.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallSet class.
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
bool empty() const
Definition: BasicBlock.h:460
void removeFromParent()
Unlink 'this' from the containing function, but do not delete it.
Definition: BasicBlock.cpp:273
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:168
const Instruction & back() const
Definition: BasicBlock.h:463
void callbackDeleteBB(BasicBlock *DelBB, std::function< void(BasicBlock *)> Callback)
Delete DelBB.
void deleteBB(BasicBlock *DelBB)
Delete DelBB.
void eraseDelBBNode(BasicBlockT *DelBB)
Erase Basic Block node that has been unlinked from Function in the DomTree and PostDomTree.
LLVM_DUMP_METHOD void dump() const
Debug method to help view the internal state of this class.
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:92
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1814
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:344
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
bool pred_empty(const BasicBlock *BB)
Definition: CFG.h:118