blob: 4692e5155d79f1361945d07cf57b33243959c357 [file] [log] [blame]
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +00001//===--- CompilationGraph.h - The LLVM Compiler Driver ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open
6// Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Compilation graph - definition.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
15#define LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H
16
17#include "AutoGenerated.h"
18#include "Tool.h"
19
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000020#include "llvm/ADT/GraphTraits.h"
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000021#include "llvm/ADT/IntrusiveRefCntPtr.h"
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000022#include "llvm/ADT/iterator"
23#include "llvm/ADT/SmallVector.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000024#include "llvm/ADT/StringMap.h"
25#include "llvm/System/Path.h"
26
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000027#include <string>
28
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000029namespace llvmc {
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000030
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000031 // An edge of the compilation graph.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000032 class Edge : public llvm::RefCountedBaseVPTR<Edge> {
33 public:
34 Edge(const std::string& T) : ToolName_(T) {}
35 virtual ~Edge() {};
36
37 const std::string& ToolName() const { return ToolName_; }
38 virtual bool isEnabled() const = 0;
39 virtual bool isDefault() const = 0;
40 private:
41 std::string ToolName_;
42 };
43
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000044 // Edges that have no properties are instances of this class.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000045 class SimpleEdge : public Edge {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000046 public:
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000047 SimpleEdge(const std::string& T) : Edge(T) {}
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000048 bool isEnabled() const { return false;}
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000049 bool isDefault() const { return true;}
50 };
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000051
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000052 // A node of the compilation graph.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000053 struct Node {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000054 // A Node holds a list of the outward edges.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000055 typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
56 typedef container_type::iterator iterator;
57 typedef container_type::const_iterator const_iterator;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000058
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000059 Node() : OwningGraph(0), InEdges(0) {}
60 Node(CompilationGraph* G) : OwningGraph(G), InEdges(0) {}
61 Node(CompilationGraph* G, Tool* T) :
62 OwningGraph(G), ToolPtr(T), InEdges(0) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000063
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000064 bool HasChildren() const { return !OutEdges.empty(); }
Mikhail Glushenkov02606582008-05-06 18:07:14 +000065 const std::string Name() const
66 { return ToolPtr ? ToolPtr->Name() : "root"; }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000067
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000068 // Iteration.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000069 iterator EdgesBegin() { return OutEdges.begin(); }
70 const_iterator EdgesBegin() const { return OutEdges.begin(); }
71 iterator EdgesEnd() { return OutEdges.end(); }
72 const_iterator EdgesEnd() const { return OutEdges.end(); }
73
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000074 // Add an outward edge. Takes ownership of the Edge object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000075 void AddEdge(Edge* E)
76 { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
77
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000078 // Inward edge counter. Used to implement topological sort.
79 // TOTHINK: Move the mutable counter back into Tool classes? Makes
80 // us more const-correct.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000081 void IncrInEdges() { ++InEdges; }
82 void DecrInEdges() { --InEdges; }
83 bool HasNoInEdges() const { return InEdges == 0; }
84
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000085 // Needed to implement NodeChildIterator/GraphTraits
86 CompilationGraph* OwningGraph;
87 // The corresponding Tool.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000088 // WARNING: ToolPtr can be NULL (for the root node).
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000089 llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
90 // Links to children.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000091 container_type OutEdges;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000092 // Inward edge counter. Updated in
93 // CompilationGraph::insertEdge(). Used for topological sorting.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000094 unsigned InEdges;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000095 };
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000096
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000097 class NodesIterator;
98
Mikhail Glushenkov02606582008-05-06 18:07:14 +000099 // The compilation graph itself.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000100 class CompilationGraph {
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000101 // Main data structure.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000102 typedef llvm::StringMap<Node> nodes_map_type;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000103 // These are used to map from language names to tools. (We can
104 // have several tools associated with each language name, hence
105 // the need for a vector of Edges.)
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000106 typedef
107 llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type;
108 typedef llvm::StringMap<tools_vector_type> tools_map_type;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000109
110 // Map from file extensions to language names.
111 LanguageMap ExtsToLangs;
112 // Map from language names to lists of tool names.
113 tools_map_type ToolsMap;
114 // Map from tool names to Tool objects.
115 nodes_map_type NodesMap;
116
117 public:
118
119 CompilationGraph();
120
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000121 // insertVertex - insert a new node into the graph. Takes
122 // ownership of the object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000123 void insertNode(Tool* T);
124
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000125 // insertEdge - Insert a new edge into the graph. Takes ownership
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000126 // of the Edge object.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000127 void insertEdge(const std::string& A, Edge* E);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000128
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000129 // Build - Build target(s) from the input file set. Command-line
130 // options are passed implicitly as global variables.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000131 int Build(llvm::sys::Path const& tempDir);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000132
133 // Return a reference to the node correponding to the given tool
134 // name. Throws std::runtime_error.
135 Node& getNode(const std::string& ToolName);
136 const Node& getNode(const std::string& ToolName) const;
137
138 // viewGraph - This function is meant for use from the debugger.
139 // You can just say 'call G->viewGraph()' and a ghostview window
140 // should pop up from the program, displaying the compilation
141 // graph. This depends on there being a 'dot' and 'gv' program
142 // in your path.
143 void viewGraph();
144
145 // Write a CompilationGraph.dot file.
146 void writeGraph();
147
148 // GraphTraits support
149 friend NodesIterator GraphBegin(CompilationGraph*);
150 friend NodesIterator GraphEnd(CompilationGraph*);
151 friend void PopulateCompilationGraph(CompilationGraph&);
152
153 private:
154 // Helper functions.
155
156 // Find out which language corresponds to the suffix of this file.
157 const std::string& getLanguage(const llvm::sys::Path& File) const;
158
159 // Return a reference to the list of tool names corresponding to
160 // the given language name. Throws std::runtime_error.
161 const tools_vector_type& getToolsVector(const std::string& LangName) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000162
163 // Pass the input file through the toolchain.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000164 void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
165 const llvm::sys::Path& TempDir) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000166
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000167 // Find head of the toolchain corresponding to the given file.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000168 const Node* FindToolChain(const llvm::sys::Path& In,
169 const std::string* forceLanguage) const;
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000170
171 // Sort the nodes in topological order.
172 void TopologicalSort(std::vector<const Node*>& Out);
173 // Call TopologicalSort and filter the resulting list to include
174 // only Join nodes.
175 void TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000176 };
177
178 /// GraphTraits support code.
179
180 // Auxiliary class needed to implement GraphTraits support. Can be
181 // generalised to something like value_iterator for map-like
182 // containers.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000183 class NodesIterator : public llvm::StringMap<Node>::iterator {
184 typedef llvm::StringMap<Node>::iterator super;
185 typedef NodesIterator ThisType;
186 typedef Node* pointer;
187 typedef Node& reference;
188
189 public:
190 NodesIterator(super I) : super(I) {}
191
192 inline reference operator*() const {
193 return super::operator->()->second;
194 }
195 inline pointer operator->() const {
196 return &super::operator->()->second;
197 }
198 };
199
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000200 inline NodesIterator GraphBegin(CompilationGraph* G) {
201 return NodesIterator(G->NodesMap.begin());
202 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000203
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000204 inline NodesIterator GraphEnd(CompilationGraph* G) {
205 return NodesIterator(G->NodesMap.end());
206 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000207
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000208
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000209 // Another auxiliary class needed by GraphTraits.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000210 class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
211 typedef NodeChildIterator ThisType;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000212 typedef Node::container_type::iterator iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000213
214 CompilationGraph* OwningGraph;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000215 iterator EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000216 public:
217 typedef Node* pointer;
218 typedef Node& reference;
219
220 NodeChildIterator(Node* N, iterator I) :
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000221 OwningGraph(N->OwningGraph), EdgeIter(I) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000222
223 const ThisType& operator=(const ThisType& I) {
224 assert(OwningGraph == I.OwningGraph);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000225 EdgeIter = I.EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000226 return *this;
227 }
228
229 inline bool operator==(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000230 { return EdgeIter == I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000231 inline bool operator!=(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000232 { return EdgeIter != I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000233
234 inline pointer operator*() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000235 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000236 }
237 inline pointer operator->() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000238 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000239 }
240
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000241 ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000242 ThisType operator++(int) { // Postincrement
243 ThisType tmp = *this;
244 ++*this;
245 return tmp;
246 }
247
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000248 inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000249 inline ThisType operator--(int) { // Postdecrement
250 ThisType tmp = *this;
251 --*this;
252 return tmp;
253 }
254
255 };
256}
257
258namespace llvm {
259 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000260 struct GraphTraits<llvmc::CompilationGraph*> {
261 typedef llvmc::CompilationGraph GraphType;
262 typedef llvmc::Node NodeType;
263 typedef llvmc::NodeChildIterator ChildIteratorType;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000264
265 static NodeType* getEntryNode(GraphType* G) {
266 return &G->getNode("root");
267 }
268
269 static ChildIteratorType child_begin(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000270 return ChildIteratorType(N, N->OutEdges.begin());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000271 }
272 static ChildIteratorType child_end(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000273 return ChildIteratorType(N, N->OutEdges.end());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000274 }
275
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000276 typedef llvmc::NodesIterator nodes_iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000277 static nodes_iterator nodes_begin(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000278 return GraphBegin(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000279 }
280 static nodes_iterator nodes_end(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000281 return GraphEnd(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000282 }
283 };
284
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000285}
286
287#endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H