LLVM: include/llvm/Analysis/CFGPrinter.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#ifndef LLVM_ANALYSIS_CFGPRINTER_H
19#define LLVM_ANALYSIS_CFGPRINTER_H
20
32
33namespace llvm {
34template struct GraphTraits;
36public:
39};
40
42public:
45};
46
48public:
51};
52
54public:
57};
58
60private:
65 bool ShowHeat;
66 bool EdgeWeights;
67 bool RawWeights;
68
69public:
71
74 : F(F), BFI(BFI), BPI(BPI), MaxFreq(MaxFreq) {
75 ShowHeat = false;
76 EdgeWeights = !!BPI;
77 RawWeights = !!BFI;
78 }
79
81
83
85
87
89 return BFI->getBlockFreq(BB).getFrequency();
90 }
91
92 void setHeatColors(bool ShowHeat) { this->ShowHeat = ShowHeat; }
93
95
96 void setRawEdgeWeights(bool RawWeights) { this->RawWeights = RawWeights; }
97
99
100 void setEdgeWeights(bool EdgeWeights) { this->EdgeWeights = EdgeWeights; }
101
103};
104
105template <>
109 }
110
111
113
116 }
117
120 }
121
124 }
125};
126
127template
129 if (->getName().empty())
130 return Node->getName().str();
131
132 std::string Str;
134
136 return Str;
137}
138
139template
141 const BasicBlockT *Node,
143 HandleBasicBlock,
144 function_ref<void(std::string &, unsigned &, unsigned)>
145 HandleComment) {
146
147 enum { MaxColumns = 80 };
148 std::string OutStr;
150 HandleBasicBlock(OS, *Node);
151
152 if (OutStr[0] == '%') {
153 OutStr.erase(OutStr.begin());
154 }
155
156 OutStr.insert(OutStr.find_first_of('\n') + 1, "\\|");
157
158 unsigned ColNum = 0;
159 unsigned LastSpace = 0;
160 for (unsigned i = 0; i != OutStr.length(); ++i) {
161 if (OutStr[i] == '\n') {
162 OutStr[i] = '\\';
163 OutStr.insert(OutStr.begin() + i + 1, 'l');
164 ColNum = 0;
165 LastSpace = 0;
166 } else if (OutStr[i] == ';') {
167 unsigned Idx = OutStr.find('\n', i + 1);
168 HandleComment(OutStr, i, Idx);
169 } else if (ColNum == MaxColumns) {
170
171 if (!LastSpace)
172 LastSpace = i;
173 OutStr.insert(LastSpace, "\\l...");
174 ColNum = i - LastSpace;
175 LastSpace = 0;
176 i += 3;
177 } else
178 ++ColNum;
179 if (OutStr[i] == ' ')
180 LastSpace = i;
181 }
182 return OutStr;
183}
184
185template <>
187
188
190
192
193 static void eraseComment(std::string &OutStr, unsigned &I, unsigned Idx) {
194 OutStr.erase(OutStr.begin() + I, OutStr.begin() + Idx);
195 --I;
196 }
197
200 }
201
204 }
205
207
209 OS << ":\n";
211 OS << Inst << "\n";
212 }
213
217 HandleBasicBlock = printBasicBlock,
218 function_ref<void(std::string &, unsigned &, unsigned)>
219 HandleComment = eraseComment) {
221 }
222
224
226 return getSimpleNodeLabel(Node, CFGInfo);
227 else
228 return getCompleteNodeLabel(Node, CFGInfo);
229 }
230
233
234 if (const BranchInst *BI = dyn_cast(Node->getTerminator()))
235 if (BI->isConditional())
237
238
239 if (const SwitchInst *SI = dyn_cast(Node->getTerminator())) {
240 unsigned SuccNo = I.getSuccessorIndex();
241
242 if (SuccNo == 0)
243 return "def";
244
245 std::string Str;
248 OS << Case.getCaseValue()->getValue();
249 return Str;
250 }
251 return "";
252 }
253
255 std::string NodeName = Node->getName().str();
256 if (NodeName.empty()) {
259
260 NodeName.erase(NodeName.begin());
261 }
262 return NodeName;
263 }
264
265
268
270 return "";
271
272 unsigned OpNo = I.getSuccessorIndex();
276 double WeightPercent = ((double)BranchProb.getNumerator()) /
277 ((double)BranchProb.getDenominator());
278 std::string TTAttr =
279 formatv("tooltip=\"{0} -> {1}\\nProbability {2:P}\" ", getBBName(Node),
280 getBBName(SuccBB), WeightPercent);
281
283 return TTAttr + "penwidth=2";
284
286 return TTAttr;
287
288 double Width = 1 + WeightPercent;
289
291 return TTAttr +
292 formatv("label=\"{0:P}\" penwidth={1}", WeightPercent, Width)
293 .str();
294
295
296
297
299 std::string Attrs =
300 TTAttr + formatv("label=\"W:{0}\" penwidth={1}",
301 (uint64_t)(Freq * WeightPercent), Width)
302 .str();
303 if (Attrs.size())
304 return Attrs;
305
307 if (!WeightsNode)
308 return TTAttr;
309
310 OpNo = I.getSuccessorIndex() + 1;
312 return TTAttr;
314 mdconst::dyn_extract(WeightsNode->getOperand(OpNo));
315 if (!Weight)
316 return TTAttr;
317 return (TTAttr + "label=\"W:" + std::to_string(Weight->getZExtValue()) +
318 "\" penwidth=" + std::to_string(Width));
319 }
320
322
324 return "";
325
328 std::string EdgeColor = (Freq <= (CFGInfo->getMaxFreq() / 2))
331
332 std::string Attrs = "color=\"" + EdgeColor + "ff\", style=filled," +
333 " fillcolor=\"" + Color + "70\"" +
334 " fontname=\"Courier\"";
335 return Attrs;
336 }
339};
340}
341
342#endif
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This header defines various interfaces for pass management in LLVM.
StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach)
This file contains the declarations for profiling metadata utility functions.
static bool isSimple(Instruction *I)
void printAsOperand(OutputBuffer &OB, Prec P=Prec::Default, bool StrictlyWorse=false) const
A container for analyses that lazily runs them and caches their results.
LLVM Basic Block Representation.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Conditional or Unconditional Branch instruction.
Analysis providing branch probability information.
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This is the shared class of boolean and integer constants.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
void setRawEdgeWeights(bool RawWeights)
DOTFuncInfo(const Function *F, const BlockFrequencyInfo *BFI, const BranchProbabilityInfo *BPI, uint64_t MaxFreq)
uint64_t getMaxFreq() const
void setEdgeWeights(bool EdgeWeights)
DOTFuncInfo(const Function *F)
uint64_t getFreq(const BasicBlock *BB) const
const BranchProbabilityInfo * getBPI() const
const Function * getFunction() const
const BlockFrequencyInfo * getBFI() const
void setHeatColors(bool ShowHeat)
const BasicBlock & getEntryBlock() const
unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
const MDOperand & getOperand(unsigned I) const
unsigned getNumOperands() const
Return number of MDNode operands.
A set of analyses that are preserved following a run of a transformation pass.
std::string str() const
str - Get the contents as an std::string.
static CaseIteratorImpl fromSuccessorIndex(SwitchInstT *SI, unsigned SuccessorIndex)
Initializes case iterator for given SwitchInst and for given successor index.
StringRef getName() const
Return a constant reference to the value's name.
An efficient, type-erasing, non-owning reference to a callable.
A raw_ostream that writes to an std::string.
This is an optimization pass for GlobalISel generic memory operations.
MDNode * getBranchWeightMDNode(const Instruction &I)
Get the branch weights metadata node.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
std::string SimpleNodeLabelString(const BasicBlockT *Node)
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
std::string getHeatColor(uint64_t freq, uint64_t maxFreq)
std::string CompleteNodeLabelString(const BasicBlockT *Node, function_ref< void(raw_string_ostream &, const BasicBlockT &)> HandleBasicBlock, function_ref< void(std::string &, unsigned &, unsigned)> HandleComment)
std::string getEdgeAttributes(const BasicBlock *Node, const_succ_iterator I, DOTFuncInfo *CFGInfo)
Display the raw branch weights from PGO.
void computeDeoptOrUnreachablePaths(const Function *F)
static std::string getSimpleNodeLabel(const BasicBlock *Node, DOTFuncInfo *)
static std::string getGraphName(DOTFuncInfo *CFGInfo)
DenseMap< const BasicBlock *, bool > isOnDeoptOrUnreachablePath
bool isNodeHidden(const BasicBlock *Node, const DOTFuncInfo *CFGInfo)
DOTGraphTraits(bool isSimple=false)
std::string getNodeAttributes(const BasicBlock *Node, DOTFuncInfo *CFGInfo)
static std::string getEdgeSourceLabel(const BasicBlock *Node, const_succ_iterator I)
static void eraseComment(std::string &OutStr, unsigned &I, unsigned Idx)
std::string getNodeLabel(const BasicBlock *Node, DOTFuncInfo *CFGInfo)
static std::string getBBName(const BasicBlock *Node)
static void printBasicBlock(raw_string_ostream &OS, const BasicBlock &Node)
static std::string getCompleteNodeLabel(const BasicBlock *Node, DOTFuncInfo *, function_ref< void(raw_string_ostream &, const BasicBlock &)> HandleBasicBlock=printBasicBlock, function_ref< void(std::string &, unsigned &, unsigned)> HandleComment=eraseComment)
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to 'dot...
DefaultDOTGraphTraits - This class provides the default implementations of all of the DOTGraphTraits ...
static nodes_iterator nodes_begin(DOTFuncInfo *CFGInfo)
static size_t size(DOTFuncInfo *CFGInfo)
static NodeRef getEntryNode(DOTFuncInfo *CFGInfo)
static nodes_iterator nodes_end(DOTFuncInfo *CFGInfo)
A CRTP mix-in to automatically provide informational APIs needed for passes.