18 #ifndef LLVM_ANALYSIS_CFGPRINTER_H 19 #define LLVM_ANALYSIS_CFGPRINTER_H 69 :
F(
F), BFI(BFI), BPI(BPI), MaxFreq(MaxFreq) {
84 return BFI->getBlockFreq(BB).getFrequency();
135 if (!
Node->getName().empty())
136 return Node->getName().str();
141 Node->printAsOperand(OS,
false);
146 OutStr.erase(OutStr.begin() +
I, OutStr.begin() + Idx);
156 HandleComment = eraseComment) {
157 enum { MaxColumns = 80 };
159 raw_string_ostream OS(Str);
161 if (
Node->getName().empty()) {
162 Node->printAsOperand(OS,
false);
166 HandleBasicBlock(OS, *
Node);
167 std::string OutStr = OS.
str();
168 if (OutStr[0] ==
'\n')
169 OutStr.erase(OutStr.begin());
173 unsigned LastSpace = 0;
174 for (
unsigned i = 0; i != OutStr.length(); ++i) {
175 if (OutStr[i] ==
'\n') {
177 OutStr.insert(OutStr.begin() + i + 1,
'l');
180 }
else if (OutStr[i] ==
';') {
181 unsigned Idx = OutStr.find(
'\n', i + 1);
182 HandleComment(OutStr, i, Idx);
183 }
else if (ColNum == MaxColumns) {
187 OutStr.insert(LastSpace,
"\\l...");
188 ColNum = i - LastSpace;
193 if (OutStr[i] ==
' ')
202 return getSimpleNodeLabel(
Node, CFGInfo);
204 return getCompleteNodeLabel(
Node, CFGInfo);
210 if (
const BranchInst *BI = dyn_cast<BranchInst>(
Node->getTerminator()))
211 if (BI->isConditional())
216 unsigned SuccNo =
I.getSuccessorIndex();
224 OS << Case.getCaseValue()->getValue();
240 unsigned OpNo =
I.getSuccessorIndex();
247 double WeightPercent = ((double)BranchProb.getNumerator()) /
248 ((
double)BranchProb.getDenominator());
249 double Width = 1 + WeightPercent;
252 return formatv(
"label=\"{0:P}\" penwidth={1}", WeightPercent,
Width)
259 std::string
Attrs =
formatv(
"label=\"W:{0}\" penwidth={1}",
260 (uint64_t)(Freq * WeightPercent),
Width);
269 if (MDName->
getString() !=
"branch_weights")
272 OpNo =
I.getSuccessorIndex() + 1;
276 mdconst::dyn_extract<ConstantInt>(WeightsNode->
getOperand(OpNo));
290 std::string EdgeColor = (Freq <= (CFGInfo->
getMaxFreq() / 2))
294 std::string
Attrs =
"color=\"" + EdgeColor +
"ff\", style=filled," +
295 " fillcolor=\"" +
Color +
"70\"";
299 void computeHiddenNodes(
const Function *
F);
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
This class represents lattice values for constants.
DOTFuncInfo(const Function *F)
static std::string getSimpleNodeLabel(const BasicBlock *Node, DOTFuncInfo *)
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
static std::string getGraphName(DOTFuncInfo *CFGInfo)
FunctionPass * createCFGPrinterLegacyPassPass()
static std::string getCompleteNodeLabel(const BasicBlock *Node, DOTFuncInfo *, llvm::function_ref< void(raw_string_ostream &, const BasicBlock &)> HandleBasicBlock=[](raw_string_ostream &OS, const BasicBlock &Node) -> void { OS<< Node;}, llvm::function_ref< void(std::string &, unsigned &, unsigned)> HandleComment=eraseComment)
An efficient, type-erasing, non-owning reference to a callable.
DOTFuncInfo(const Function *F, const BlockFrequencyInfo *BFI, const BranchProbabilityInfo *BPI, uint64_t MaxFreq)
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
uint64_t getFreq(const BasicBlock *BB)
const MDOperand & getOperand(unsigned I) const
std::string getNodeAttributes(const BasicBlock *Node, DOTFuncInfo *CFGInfo)
static nodes_iterator nodes_begin(DOTFuncInfo *CFGInfo)
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
static CaseIteratorImpl fromSuccessorIndex(SwitchInstT *SI, unsigned SuccessorIndex)
Initializes case iterator for given SwitchInst and for given successor index.
static bool isSimple(Instruction *I)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A CRTP mix-in to automatically provide informational APIs needed for passes.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
static NodeRef getEntryNode(DOTFuncInfo *CFGInfo)
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
static std::string getEdgeSourceLabel(const BasicBlock *Node, const_succ_iterator I)
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
StringRef getString() const
const BasicBlock & getEntryBlock() const
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
FunctionPass * createCFGOnlyPrinterLegacyPassPass()
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
A set of analyses that are preserved following a run of a transformation pass.
LLVM Basic Block Representation.
Conditional or Unconditional Branch instruction.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static void eraseComment(std::string &OutStr, unsigned &I, unsigned Idx)
void setEdgeWeights(bool EdgeWeights)
const Function * getFunction()
static nodes_iterator nodes_end(DOTFuncInfo *CFGInfo)
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
DOTGraphTraits(bool isSimple=false)
DOTGraphTraits - Template class that can be specialized to customize how graphs are converted to 'dot...
llvm::DenseMap< const BasicBlock *, bool > isHiddenBasicBlock
Color
A "color", which is either even or odd.
This is the shared class of boolean and integer constants.
const BranchProbabilityInfo * getBPI()
const BlockFrequencyInfo * getBFI()
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
std::string getNodeLabel(const BasicBlock *Node, DOTFuncInfo *CFGInfo)
Analysis providing branch probability information.
void setHeatColors(bool ShowHeat)
StringRef getName() const
Return a constant reference to the value's name.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
std::string getHeatColor(uint64_t freq, uint64_t maxFreq)
std::string getEdgeAttributes(const BasicBlock *Node, const_succ_iterator I, DOTFuncInfo *CFGInfo)
Display the raw branch weights from PGO.
const std::string to_string(const T &Value)
A raw_ostream that writes to an std::string.
static size_t size(DOTFuncInfo *CFGInfo)
DefaultDOTGraphTraits - This class provides the default implementations of all of the DOTGraphTraits ...
void setRawEdgeWeights(bool RawWeights)
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
unsigned getNumOperands() const
Return number of MDNode operands.