LLVM 20.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
17#include "llvm/IR/Constants.h"
20#include <functional>
21
22namespace llvm {
23
24template class GenericDomTreeUpdater<DomTreeUpdater, DominatorTree,
25 PostDominatorTree>;
26
27template void
28GenericDomTreeUpdater<DomTreeUpdater, DominatorTree,
29 PostDominatorTree>::recalculate(Function &F);
30
31template void
32GenericDomTreeUpdater<DomTreeUpdater, DominatorTree, PostDominatorTree>::
33 applyUpdatesImpl</*IsForward=*/true>();
34template void
35GenericDomTreeUpdater<DomTreeUpdater, DominatorTree, PostDominatorTree>::
36 applyUpdatesImpl</*IsForward=*/false>();
37
38bool DomTreeUpdater::forceFlushDeletedBB() {
39 if (DeletedBBs.empty())
40 return false;
41
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();
51 }
53 Callbacks.clear();
54 return true;
55}
56
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.
63 validateDeleteBB(DelBB);
64 if (Strategy == UpdateStrategy::Lazy) {
65 DeletedBBs.insert(DelBB);
66 return;
67 }
68
69 eraseDelBBNode(DelBB);
70 DelBB->eraseFromParent();
71}
72
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);
79 return;
80 }
81
82 eraseDelBBNode(DelBB);
83 DelBB->removeFromParent();
84 Callback(DelBB);
85 delete DelBB;
86}
87
88void 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).
95 if (!I.use_empty())
96 I.replaceAllUsesWith(PoisonValue::get(I.getType()));
97 DelBB->back().eraseFromParent();
98 }
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);
102}
103
106 Base::dump();
107#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
108 raw_ostream &OS = dbgs();
109 OS << "Pending Callbacks:\n";
110 int Index = 0;
111 for (const auto &BB : Callbacks) {
112 OS << " " << Index << " : ";
113 ++Index;
114 if (BB->hasName())
115 OS << BB->getName() << "(";
116 else
117 OS << "(no_name)(";
118 OS << BB << ")\n";
119 }
120#endif
121}
122
123} // namespace llvm
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:622
This file contains the declarations for the subclasses of Constant, which represent the different fla...
uint32_t Index
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
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
bool empty() const
Definition: BasicBlock.h:470
void removeFromParent()
Unlink 'this' from the containing function, but do not delete it.
Definition: BasicBlock.cpp:275
SymbolTableList< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
Definition: BasicBlock.cpp:279
LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:168
const Instruction & back() const
Definition: BasicBlock.h:473
LLVM_DUMP_METHOD void dump() const
Debug method to help view the internal state of this class.
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 before it is 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:1878
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:384
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
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