14 #ifndef LLVM_LIB_TRANSFORMS_INSTRUMENTATION_CFGMST_H 15 #define LLVM_LIB_TRANSFORMS_INSTRUMENTATION_CFGMST_H 29 #define DEBUG_TYPE "cfgmst" 37 template <
class Edge,
class BBInfo>
class CFGMST {
56 return static_cast<BBInfo *
>(G->Group);
69 if (BB1G->Rank < BB2G->Rank)
74 if (BB1G->Rank == BB2G->Rank)
82 auto It = BBInfos.
find(BB);
83 assert(It->second.get() !=
nullptr);
84 return *It->second.get();
89 auto It = BBInfos.
find(BB);
90 if (It == BBInfos.
end())
92 return It->second.get();
103 Edge *EntryIncoming =
nullptr, *EntryOutgoing =
nullptr,
104 *ExitOutgoing =
nullptr, *ExitIncoming =
nullptr;
105 uint64_t MaxEntryOutWeight = 0, MaxExitOutWeight = 0, MaxExitInWeight = 0;
108 EntryIncoming = &
addEdge(
nullptr, Entry, EntryWeight);
109 LLVM_DEBUG(
dbgs() <<
" Edge: from fake node to " << Entry->getName()
110 <<
" w = " << EntryWeight <<
"\n");
114 addEdge(Entry,
nullptr, EntryWeight);
118 static const uint32_t CriticalEdgeMultiplier = 1000;
129 uint64_t scaleFactor = BBWeight;
131 if (scaleFactor <
UINT64_MAX / CriticalEdgeMultiplier)
132 scaleFactor *= CriticalEdgeMultiplier;
138 auto *
E = &
addEdge(&*BB, TargetBB, Weight);
139 E->IsCritical = Critical;
141 << TargetBB->
getName() <<
" w=" << Weight <<
"\n");
145 if (Weight > MaxEntryOutWeight) {
146 MaxEntryOutWeight = Weight;
152 if (TargetTI && !TargetTI->getNumSuccessors()) {
153 if (Weight > MaxExitInWeight) {
154 MaxExitInWeight = Weight;
160 ExitBlockFound =
true;
161 Edge *ExitO = &
addEdge(&*BB,
nullptr, BBWeight);
162 if (BBWeight > MaxExitOutWeight) {
163 MaxExitOutWeight = BBWeight;
164 ExitOutgoing = ExitO;
166 LLVM_DEBUG(
dbgs() <<
" Edge: from " << BB->getName() <<
" to fake exit" 167 <<
" w = " << BBWeight <<
"\n");
181 uint64_t EntryInWeight = EntryWeight;
183 if (EntryInWeight >= MaxExitOutWeight &&
184 EntryInWeight * 2 < MaxExitOutWeight * 3) {
185 EntryIncoming->Weight = MaxExitOutWeight;
186 ExitOutgoing->Weight = EntryInWeight + 1;
189 if (MaxEntryOutWeight >= MaxExitInWeight &&
190 MaxEntryOutWeight * 2 < MaxExitInWeight * 3) {
191 EntryOutgoing->Weight = MaxExitInWeight;
192 ExitIncoming->Weight = MaxEntryOutWeight + 1;
199 const std::unique_ptr<Edge> &Edge2) {
200 return Edge1->Weight > Edge2->Weight;
210 for (
auto &Ei : AllEdges) {
213 if (Ei->IsCritical) {
214 if (Ei->DestBB && Ei->DestBB->isLandingPad()) {
221 for (
auto &Ei : AllEdges) {
226 if (!ExitBlockFound && Ei->SrcBB ==
nullptr)
235 if (!Message.
str().empty())
236 OS << Message <<
"\n";
237 OS <<
" Number of Basic Blocks: " << BBInfos.
size() <<
"\n";
238 for (
auto &BI : BBInfos) {
240 OS <<
" BB: " << (BB ==
nullptr ?
"FakeNode" : BB->
getName()) <<
" " 241 << BI.second->infoString() <<
"\n";
244 OS <<
" Number of Edges: " << AllEdges.size()
245 <<
" (*: Instrument, C: CriticalEdge, -: Removed)\n";
247 for (
auto &EI : AllEdges)
248 OS <<
" Edge " << Count++ <<
": " <<
getBBInfo(EI->SrcBB).Index <<
"-->" 249 <<
getBBInfo(EI->DestBB).Index << EI->infoString() <<
"\n";
255 auto Iter = BBInfos.
end();
257 std::tie(Iter, Inserted) = BBInfos.
insert(std::make_pair(Src,
nullptr));
260 Iter->second = std::move(std::make_unique<BBInfo>(Index));
263 std::tie(Iter, Inserted) = BBInfos.
insert(std::make_pair(Dest,
nullptr));
266 Iter->second = std::move(std::make_unique<BBInfo>(Index));
267 AllEdges.emplace_back(
new Edge(Src, Dest, W));
268 return *AllEdges.back();
277 : F(Func), BPI(BPI_), BFI(BFI_) {
286 #undef DEBUG_TYPE // "cfgmst" 288 #endif // LLVM_LIB_TRANSFORMS_INSTRUMENTATION_CFGMST_H This class represents lattice values for constants.
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
std::vector< std::unique_ptr< Edge > > AllEdges
CFGMST(Function &Func, BranchProbabilityInfo *BPI_=nullptr, BlockFrequencyInfo *BFI_=nullptr)
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
An union-find based Minimum Spanning Tree for CFG.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
bool unionGroups(const BasicBlock *BB1, const BasicBlock *BB2)
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
iterator find(const_arg_type_t< KeyT > Val)
const BasicBlock & getEntryBlock() const
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
LLVM Basic Block Representation.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
bool isCriticalEdge(const Instruction *TI, unsigned SuccNum, bool AllowIdenticalEdges=false)
Return true if the specified edge is a critical edge.
DenseMap< const BasicBlock *, std::unique_ptr< BBInfo > > BBInfos
bool succ_empty(const Instruction *I)
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
BBInfo * findBBInfo(const BasicBlock *BB) const
uint64_t getEntryFreq() const
void computeMinimumSpanningTree()
Iterator for intrusive lists based on ilist_node.
BBInfo * findAndCompressGroup(BBInfo *G)
uint64_t scale(uint64_t Num) const
Scale a large integer.
void dumpEdges(raw_ostream &OS, const Twine &Message) const
BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
BranchProbabilityInfo * BPI
BBInfo & getBBInfo(const BasicBlock *BB) const
Analysis providing branch probability information.
Edge & addEdge(const BasicBlock *Src, const BasicBlock *Dest, uint64_t W)
StringRef getName() const
Return a constant reference to the value's name.
std::string str() const
Return the twine contents as a std::string.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void stable_sort(R &&Range)
succ_range successors(Instruction *I)
This class implements an extremely fast bulk output stream that can only output to a stream...