blob: 57deef30a5c61af1e71333bf24526664806a94ad [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"
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +000023#include "llvm/ADT/SmallSet.h"
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000024#include "llvm/ADT/SmallVector.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000025#include "llvm/ADT/StringMap.h"
26#include "llvm/System/Path.h"
27
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000028#include <string>
29
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000030namespace llvmc {
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000031
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +000032 typedef llvm::SmallSet<std::string, 5> InputLanguagesSet;
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000033
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000034 // An edge of the compilation graph.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000035 class Edge : public llvm::RefCountedBaseVPTR<Edge> {
36 public:
37 Edge(const std::string& T) : ToolName_(T) {}
38 virtual ~Edge() {};
39
40 const std::string& ToolName() const { return ToolName_; }
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000041 virtual unsigned Weight(const InputLanguagesSet& InLangs) const = 0;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000042 private:
43 std::string ToolName_;
44 };
45
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000046 // Edges that have no properties are instances of this class.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000047 class SimpleEdge : public Edge {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000048 public:
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000049 SimpleEdge(const std::string& T) : Edge(T) {}
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000050 unsigned Weight(const InputLanguagesSet&) const { return 1; }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000051 };
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000052
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000053 // A node of the compilation graph.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000054 struct Node {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000055 // A Node holds a list of the outward edges.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000056 typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
57 typedef container_type::iterator iterator;
58 typedef container_type::const_iterator const_iterator;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000059
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000060 Node() : OwningGraph(0), InEdges(0) {}
61 Node(CompilationGraph* G) : OwningGraph(G), InEdges(0) {}
62 Node(CompilationGraph* G, Tool* T) :
63 OwningGraph(G), ToolPtr(T), InEdges(0) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000064
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000065 bool HasChildren() const { return !OutEdges.empty(); }
Mikhail Glushenkov02606582008-05-06 18:07:14 +000066 const std::string Name() const
67 { return ToolPtr ? ToolPtr->Name() : "root"; }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000068
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000069 // Iteration.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000070 iterator EdgesBegin() { return OutEdges.begin(); }
71 const_iterator EdgesBegin() const { return OutEdges.begin(); }
72 iterator EdgesEnd() { return OutEdges.end(); }
73 const_iterator EdgesEnd() const { return OutEdges.end(); }
74
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000075 // Add an outward edge. Takes ownership of the Edge object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000076 void AddEdge(Edge* E)
77 { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
78
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000079 // Inward edge counter. Used to implement topological sort.
80 // TOTHINK: Move the mutable counter back into Tool classes? Makes
81 // us more const-correct.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000082 void IncrInEdges() { ++InEdges; }
83 void DecrInEdges() { --InEdges; }
84 bool HasNoInEdges() const { return InEdges == 0; }
85
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000086 // Needed to implement NodeChildIterator/GraphTraits
87 CompilationGraph* OwningGraph;
88 // The corresponding Tool.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000089 // WARNING: ToolPtr can be NULL (for the root node).
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000090 llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
91 // Links to children.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000092 container_type OutEdges;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000093 // Inward edge counter. Updated in
94 // CompilationGraph::insertEdge(). Used for topological sorting.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000095 unsigned InEdges;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000096 };
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000097
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000098 class NodesIterator;
99
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000100 // The compilation graph itself.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000101 class CompilationGraph {
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000102 // Main data structure.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000103 typedef llvm::StringMap<Node> nodes_map_type;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000104 // These are used to map from language names to tools. (We can
105 // have several tools associated with each language name, hence
106 // the need for a vector of Edges.)
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000107 typedef
108 llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type;
109 typedef llvm::StringMap<tools_vector_type> tools_map_type;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000110
111 // Map from file extensions to language names.
112 LanguageMap ExtsToLangs;
113 // Map from language names to lists of tool names.
114 tools_map_type ToolsMap;
115 // Map from tool names to Tool objects.
116 nodes_map_type NodesMap;
117
118 public:
119
120 CompilationGraph();
121
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000122 // insertVertex - insert a new node into the graph. Takes
123 // ownership of the object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000124 void insertNode(Tool* T);
125
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000126 // insertEdge - Insert a new edge into the graph. Takes ownership
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000127 // of the Edge object.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000128 void insertEdge(const std::string& A, Edge* E);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000129
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000130 // Build - Build target(s) from the input file set. Command-line
131 // options are passed implicitly as global variables.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000132 int Build(llvm::sys::Path const& tempDir);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000133
134 // Return a reference to the node correponding to the given tool
135 // name. Throws std::runtime_error.
136 Node& getNode(const std::string& ToolName);
137 const Node& getNode(const std::string& ToolName) const;
138
139 // viewGraph - This function is meant for use from the debugger.
140 // You can just say 'call G->viewGraph()' and a ghostview window
141 // should pop up from the program, displaying the compilation
142 // graph. This depends on there being a 'dot' and 'gv' program
143 // in your path.
144 void viewGraph();
145
146 // Write a CompilationGraph.dot file.
147 void writeGraph();
148
149 // GraphTraits support
150 friend NodesIterator GraphBegin(CompilationGraph*);
151 friend NodesIterator GraphEnd(CompilationGraph*);
152 friend void PopulateCompilationGraph(CompilationGraph&);
153
154 private:
155 // Helper functions.
156
157 // Find out which language corresponds to the suffix of this file.
158 const std::string& getLanguage(const llvm::sys::Path& File) const;
159
160 // Return a reference to the list of tool names corresponding to
161 // the given language name. Throws std::runtime_error.
162 const tools_vector_type& getToolsVector(const std::string& LangName) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000163
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000164 // Pass the input file through the toolchain starting at StartNode.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000165 void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000166 const InputLanguagesSet& InLangs,
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000167 const llvm::sys::Path& TempDir) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000168
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000169 // Find head of the toolchain corresponding to the given file.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000170 const Node* FindToolChain(const llvm::sys::Path& In,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000171 const std::string* forceLanguage,
172 InputLanguagesSet& InLangs) const;
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000173
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000174 // Traverse the initial parts of the toolchains.
175 void BuildInitial(InputLanguagesSet& InLangs,
176 const llvm::sys::Path& TempDir);
177
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000178 // Sort the nodes in topological order.
179 void TopologicalSort(std::vector<const Node*>& Out);
180 // Call TopologicalSort and filter the resulting list to include
181 // only Join nodes.
182 void TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000183 };
184
185 /// GraphTraits support code.
186
187 // Auxiliary class needed to implement GraphTraits support. Can be
188 // generalised to something like value_iterator for map-like
189 // containers.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000190 class NodesIterator : public llvm::StringMap<Node>::iterator {
191 typedef llvm::StringMap<Node>::iterator super;
192 typedef NodesIterator ThisType;
193 typedef Node* pointer;
194 typedef Node& reference;
195
196 public:
197 NodesIterator(super I) : super(I) {}
198
199 inline reference operator*() const {
200 return super::operator->()->second;
201 }
202 inline pointer operator->() const {
203 return &super::operator->()->second;
204 }
205 };
206
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000207 inline NodesIterator GraphBegin(CompilationGraph* G) {
208 return NodesIterator(G->NodesMap.begin());
209 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000210
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000211 inline NodesIterator GraphEnd(CompilationGraph* G) {
212 return NodesIterator(G->NodesMap.end());
213 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000214
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000215
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000216 // Another auxiliary class needed by GraphTraits.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000217 class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
218 typedef NodeChildIterator ThisType;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000219 typedef Node::container_type::iterator iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000220
221 CompilationGraph* OwningGraph;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000222 iterator EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000223 public:
224 typedef Node* pointer;
225 typedef Node& reference;
226
227 NodeChildIterator(Node* N, iterator I) :
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000228 OwningGraph(N->OwningGraph), EdgeIter(I) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000229
230 const ThisType& operator=(const ThisType& I) {
231 assert(OwningGraph == I.OwningGraph);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000232 EdgeIter = I.EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000233 return *this;
234 }
235
236 inline bool operator==(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000237 { return EdgeIter == I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000238 inline bool operator!=(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000239 { return EdgeIter != I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000240
241 inline pointer operator*() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000242 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000243 }
244 inline pointer operator->() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000245 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000246 }
247
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000248 ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000249 ThisType operator++(int) { // Postincrement
250 ThisType tmp = *this;
251 ++*this;
252 return tmp;
253 }
254
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000255 inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000256 inline ThisType operator--(int) { // Postdecrement
257 ThisType tmp = *this;
258 --*this;
259 return tmp;
260 }
261
262 };
263}
264
265namespace llvm {
266 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000267 struct GraphTraits<llvmc::CompilationGraph*> {
268 typedef llvmc::CompilationGraph GraphType;
269 typedef llvmc::Node NodeType;
270 typedef llvmc::NodeChildIterator ChildIteratorType;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000271
272 static NodeType* getEntryNode(GraphType* G) {
273 return &G->getNode("root");
274 }
275
276 static ChildIteratorType child_begin(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000277 return ChildIteratorType(N, N->OutEdges.begin());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000278 }
279 static ChildIteratorType child_end(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000280 return ChildIteratorType(N, N->OutEdges.end());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000281 }
282
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000283 typedef llvmc::NodesIterator nodes_iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000284 static nodes_iterator nodes_begin(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000285 return GraphBegin(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000286 }
287 static nodes_iterator nodes_end(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000288 return GraphEnd(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000289 }
290 };
291
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000292}
293
294#endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H