23 #ifndef LLVM_SUPPORT_GRAPHWRITER_H 24 #define LLVM_SUPPORT_GRAPHWRITER_H 35 #include <type_traits> 50 namespace GraphProgram {
65 template<
typename GraphType>
72 using NodeRef =
typename GTraits::NodeRef;
73 using node_iterator =
typename GTraits::nodes_iterator;
74 using child_iterator =
typename GTraits::ChildIteratorType;
77 static_assert(std::is_pointer<NodeRef>::value,
78 "FIXME: Currently GraphWriter requires the NodeRef type to be " 79 "a pointer.\nThe pointer usage should be moved to " 80 "DOTGraphTraits, and removed from GraphWriter itself.");
84 bool getEdgeSourceLabels(
raw_ostream &O, NodeRef Node) {
85 child_iterator EI = GTraits::child_begin(Node);
86 child_iterator EE = GTraits::child_end(Node);
87 bool hasEdgeSourceLabels =
false;
89 for (
unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
90 std::string label = DTraits.getEdgeSourceLabel(Node, EI);
95 hasEdgeSourceLabels =
true;
103 if (EI != EE && hasEdgeSourceLabels)
104 O <<
"|<s64>truncated...";
106 return hasEdgeSourceLabels;
129 std::string GraphName = DTraits.getGraphName(G);
133 else if (!GraphName.empty())
136 O <<
"digraph unnamed {\n";
138 if (DTraits.renderGraphFromBottomUp())
139 O <<
"\trankdir=\"BT\";\n";
143 else if (!GraphName.empty())
145 O << DTraits.getGraphProperties(G);
156 for (
const auto Node : nodes<GraphType>(G))
157 if (!isNodeHidden(Node))
162 return DTraits.isNodeHidden(Node);
166 std::string NodeAttributes = DTraits.getNodeAttributes(Node, G);
168 O <<
"\tNode" <<
static_cast<const void*
>(Node) <<
" [shape=record,";
169 if (!NodeAttributes.empty()) O << NodeAttributes <<
",";
172 if (!DTraits.renderGraphFromBottomUp()) {
176 std::string
Id = DTraits.getNodeIdentifierLabel(Node, G);
180 std::string NodeDesc = DTraits.getNodeDescription(Node, G);
181 if (!NodeDesc.empty())
185 std::string edgeSourceLabels;
187 bool hasEdgeSourceLabels = getEdgeSourceLabels(EdgeSourceLabels, Node);
189 if (hasEdgeSourceLabels) {
190 if (!DTraits.renderGraphFromBottomUp()) O <<
"|";
192 O <<
"{" << EdgeSourceLabels.
str() <<
"}";
194 if (DTraits.renderGraphFromBottomUp()) O <<
"|";
197 if (DTraits.renderGraphFromBottomUp()) {
201 std::string
Id = DTraits.getNodeIdentifierLabel(Node, G);
205 std::string NodeDesc = DTraits.getNodeDescription(Node, G);
206 if (!NodeDesc.empty())
210 if (DTraits.hasEdgeDestLabels()) {
213 unsigned i = 0, e = DTraits.numEdgeDestLabels(Node);
214 for (; i != e && i != 64; ++i) {
216 O <<
"<d" << i <<
">" 221 O <<
"|<d64>truncated...";
228 child_iterator EI = GTraits::child_begin(Node);
229 child_iterator EE = GTraits::child_end(Node);
230 for (
unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
231 if (!DTraits.isNodeHidden(*EI))
232 writeEdge(Node, i, EI);
233 for (; EI != EE; ++EI)
234 if (!DTraits.isNodeHidden(*EI))
235 writeEdge(Node, 64, EI);
238 void writeEdge(NodeRef Node,
unsigned edgeidx, child_iterator EI) {
239 if (NodeRef TargetNode = *EI) {
241 if (DTraits.edgeTargetsEdgeSource(Node, EI)) {
242 child_iterator TargetIt = DTraits.getEdgeTarget(Node, EI);
246 (
unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt);
247 DestPort =
static_cast<int>(
Offset);
250 if (DTraits.getEdgeSourceLabel(Node, EI).empty())
253 emitEdge(static_cast<const void*>(Node), edgeidx,
254 static_cast<const void*>(TargetNode), DestPort,
255 DTraits.getEdgeAttributes(Node, EI, G));
261 const std::string &Label,
unsigned NumEdgeSources = 0,
262 const std::vector<std::string> *EdgeSourceLabels =
nullptr) {
263 O <<
"\tNode" << ID <<
"[ ";
267 if (NumEdgeSources) O <<
"{";
269 if (NumEdgeSources) {
272 for (
unsigned i = 0; i != NumEdgeSources; ++i) {
274 O <<
"<s" << i <<
">";
283 void emitEdge(
const void *SrcNodeID,
int SrcNodePort,
284 const void *DestNodeID,
int DestNodePort,
285 const std::string &
Attrs) {
286 if (SrcNodePort > 64)
return;
287 if (DestNodePort > 64) DestNodePort = 64;
289 O <<
"\tNode" << SrcNodeID;
290 if (SrcNodePort >= 0)
291 O <<
":s" << SrcNodePort;
292 O <<
" -> Node" << DestNodeID;
293 if (DestNodePort >= 0 && DTraits.hasEdgeDestLabels())
294 O <<
":d" << DestNodePort;
297 O <<
"[" << Attrs <<
"]";
308 template<
typename GraphType>
310 bool ShortNames =
false,
311 const Twine &Title =
"") {
323 template <
typename GraphType>
325 bool ShortNames =
false,
const Twine &Title =
"") {
328 std::string
N = Name.
str();
329 N = N.substr(0, std::min<std::size_t>(N.size(), 140));
334 errs() <<
"error opening file '" << Filename <<
"' for writing!\n";
339 errs() <<
" done. \n";
347 template<
typename GraphType>
349 bool ShortNames =
false,
const Twine &Title =
"",
353 if (Filename.empty())
361 #endif // LLVM_SUPPORT_GRAPHWRITER_H void emitSimpleNode(const void *ID, const std::string &Attr, const std::string &Label, unsigned NumEdgeSources=0, const std::vector< std::string > *EdgeSourceLabels=nullptr)
emitSimpleNode - Outputs a simple (non-record) node
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Compute iterated dominance frontiers using a linear time algorithm.
static void addCustomGraphFeatures(const GraphType &, GraphWriter &)
addCustomGraphFeatures - If a graph is made up of more than just straight-forward nodes and edges...
bool isNodeHidden(NodeRef Node)
void writeEdge(NodeRef Node, unsigned edgeidx, child_iterator EI)
void writeGraph(const std::string &Title="")
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
raw_ostream & WriteGraph(raw_ostream &O, const GraphType &G, bool ShortNames=false, const Twine &Title="")
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
std::string EscapeString(const std::string &Label)
void ViewGraph(const GraphType &G, const Twine &Name, bool ShortNames=false, const Twine &Title="", GraphProgram::Name Program=GraphProgram::DOT)
ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file, then cleanup.
std::string & str()
Flushes the stream contents to the target string and returns the string's reference.
std::string createGraphFilename(const Twine &Name, int &FD)
StringRef getColorString(unsigned NodeNumber)
Get a color string for this node number.
raw_ostream & getOStream()
getOStream - Get the raw output stream into the graph file.
A raw_ostream that writes to a file descriptor.
bool DisplayGraph(StringRef Filename, bool wait=true, GraphProgram::Name program=GraphProgram::DOT)
void emitEdge(const void *SrcNodeID, int SrcNodePort, const void *DestNodeID, int DestNodePort, const std::string &Attrs)
emitEdge - Output an edge from a simple node into the graph...
std::string str() const
Return the twine contents as a std::string.
A raw_ostream that writes to an std::string.
void writeHeader(const std::string &Title)
This class implements an extremely fast bulk output stream that can only output to a stream...
void writeNode(NodeRef Node)
StringRef - Represent a constant reference to a string, i.e.
GraphWriter(raw_ostream &o, const GraphType &g, bool SN)