LLVM 22.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"
21#include <functional>
22
23namespace llvm {
24
25template class LLVM_EXPORT_TEMPLATE
27
28template LLVM_EXPORT_TEMPLATE void
30 PostDominatorTree>::recalculate(Function &F);
31
32template LLVM_EXPORT_TEMPLATE void
34 applyUpdatesImpl</*IsForward=*/true>();
35template LLVM_EXPORT_TEMPLATE void
37 applyUpdatesImpl</*IsForward=*/false>();
38
39bool DomTreeUpdater::forceFlushDeletedBB() {
40 if (DeletedBBs.empty())
41 return false;
42
43 for (auto *BB : DeletedBBs) {
44 // After calling deleteBB or callbackDeleteBB under Lazy UpdateStrategy,
45 // validateDeleteBB() removes all instructions of DelBB and adds an
46 // UnreachableInst as its terminator. So we check whether the BasicBlock to
47 // delete only has an UnreachableInst inside.
48 assert(BB->size() == 1 && isa<UnreachableInst>(BB->getTerminator()) &&
49 "DelBB has been modified while awaiting deletion.");
51 BB->eraseFromParent();
52 }
54 Callbacks.clear();
55 return true;
56}
57
58// The DT and PDT require the nodes related to updates
59// are not deleted when update functions are called.
60// So BasicBlock deletions must be pended when the
61// UpdateStrategy is Lazy. When the UpdateStrategy is
62// Eager, the BasicBlock will be deleted immediately.
64 validateDeleteBB(DelBB);
65 if (Strategy == UpdateStrategy::Lazy) {
66 DeletedBBs.insert(DelBB);
67 return;
68 }
69
70 eraseDelBBNode(DelBB);
71 DelBB->eraseFromParent();
72}
73
75 BasicBlock *DelBB, std::function<void(BasicBlock *)> Callback) {
76 validateDeleteBB(DelBB);
77 if (Strategy == UpdateStrategy::Lazy) {
78 Callbacks.push_back(CallBackOnDeletion(DelBB, Callback));
79 DeletedBBs.insert(DelBB);
80 return;
81 }
82
83 eraseDelBBNode(DelBB);
84 DelBB->removeFromParent();
85 Callback(DelBB);
86 delete DelBB;
87}
88
89void DomTreeUpdater::validateDeleteBB(BasicBlock *DelBB) {
90 assert(DelBB && "Invalid push_back of nullptr DelBB.");
91 assert(pred_empty(DelBB) && "DelBB has one or more predecessors.");
92 // DelBB is unreachable and all its instructions are dead.
93 while (!DelBB->empty()) {
94 Instruction &I = DelBB->back();
95 // Replace used instructions with an arbitrary value (poison).
96 if (!I.use_empty())
97 I.replaceAllUsesWith(PoisonValue::get(I.getType()));
98 DelBB->back().eraseFromParent();
99 }
100 // Make sure DelBB has a valid terminator instruction. As long as DelBB is a
101 // Child of Function F it must contain valid IR.
102 new UnreachableInst(DelBB->getContext(), DelBB);
103}
104
107 Base::dump();
108#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
109 raw_ostream &OS = dbgs();
110 OS << "Pending Callbacks:\n";
111 int Index = 0;
112 for (const auto &BB : Callbacks) {
113 OS << " " << Index << " : ";
114 ++Index;
115 if (BB->hasName())
116 OS << BB->getName() << "(";
117 else
118 OS << "(no_name)(";
119 OS << BB << ")\n";
120 }
121#endif
122}
123
124} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:638
#define LLVM_EXPORT_TEMPLATE
Definition: Compiler.h:215
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
raw_pwrite_stream & OS
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
bool empty() const
Definition: BasicBlock.h:481
LLVM_ABI void removeFromParent()
Unlink 'this' from the containing function, but do not delete it.
Definition: BasicBlock.cpp:231
LLVM_ABI SymbolTableList< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
Definition: BasicBlock.cpp:235
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
Definition: BasicBlock.cpp:131
const Instruction & back() const
Definition: BasicBlock.h:484
LLVM_ABI LLVM_DUMP_METHOD void dump() const
Debug method to help view the internal state of this class.
LLVM_ABI void callbackDeleteBB(BasicBlock *DelBB, std::function< void(BasicBlock *)> Callback)
Delete DelBB.
LLVM_ABI void deleteBB(BasicBlock *DelBB)
Delete DelBB.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:165
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.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1885
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
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:401
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
bool pred_empty(const BasicBlock *BB)
Definition: CFG.h:119