blob: 41731a3f8902537c5dc0d100eb69e055739e12ab [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"
Anton Korobeynikov43d1fd42008-05-29 17:41:17 +000022#include "llvm/ADT/iterator.h"
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000023#include "llvm/ADT/SmallVector.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000024#include "llvm/ADT/StringMap.h"
Mikhail Glushenkov706aecf2008-05-30 06:11:18 +000025#include "llvm/ADT/StringSet.h"
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000026#include "llvm/System/Path.h"
27
Mikhail Glushenkove0ff9ae2008-05-06 18:18:58 +000028#include <cassert>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000029#include <string>
30
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000031namespace llvmc {
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000032
Mikhail Glushenkov706aecf2008-05-30 06:11:18 +000033 typedef llvm::StringSet<> InputLanguagesSet;
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000034
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000035 /// Edge - Represents an edge of the compilation graph.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000036 class Edge : public llvm::RefCountedBaseVPTR<Edge> {
37 public:
38 Edge(const std::string& T) : ToolName_(T) {}
39 virtual ~Edge() {};
40
41 const std::string& ToolName() const { return ToolName_; }
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000042 virtual unsigned Weight(const InputLanguagesSet& InLangs) const = 0;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000043 private:
44 std::string ToolName_;
45 };
46
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000047 /// SimpleEdge - An edge that has no properties.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000048 class SimpleEdge : public Edge {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000049 public:
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000050 SimpleEdge(const std::string& T) : Edge(T) {}
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000051 unsigned Weight(const InputLanguagesSet&) const { return 1; }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000052 };
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000053
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000054 /// Node - A node (vertex) of the compilation graph.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000055 struct Node {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000056 // A Node holds a list of the outward edges.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000057 typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
58 typedef container_type::iterator iterator;
59 typedef container_type::const_iterator const_iterator;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000060
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000061 Node() : OwningGraph(0), InEdges(0) {}
62 Node(CompilationGraph* G) : OwningGraph(G), InEdges(0) {}
63 Node(CompilationGraph* G, Tool* T) :
64 OwningGraph(G), ToolPtr(T), InEdges(0) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000065
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000066 bool HasChildren() const { return !OutEdges.empty(); }
Mikhail Glushenkov02606582008-05-06 18:07:14 +000067 const std::string Name() const
68 { return ToolPtr ? ToolPtr->Name() : "root"; }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000069
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000070 // Iteration.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000071 iterator EdgesBegin() { return OutEdges.begin(); }
72 const_iterator EdgesBegin() const { return OutEdges.begin(); }
73 iterator EdgesEnd() { return OutEdges.end(); }
74 const_iterator EdgesEnd() const { return OutEdges.end(); }
75
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000076 /// AddEdge - Add an outward edge. Takes ownership of the provided
77 /// Edge object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000078 void AddEdge(Edge* E)
79 { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
80
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000081 // Inward edge counter. Used to implement topological sort.
82 // TOTHINK: Move the mutable counter back into Tool classes? Makes
83 // us more const-correct.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000084 void IncrInEdges() { ++InEdges; }
85 void DecrInEdges() { --InEdges; }
86 bool HasNoInEdges() const { return InEdges == 0; }
87
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000088 // Needed to implement NodeChildIterator/GraphTraits
89 CompilationGraph* OwningGraph;
90 // The corresponding Tool.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000091 // WARNING: ToolPtr can be NULL (for the root node).
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000092 llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
93 // Links to children.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000094 container_type OutEdges;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000095 // Inward edge counter. Updated in
96 // CompilationGraph::insertEdge(). Used for topological sorting.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000097 unsigned InEdges;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000098 };
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000099
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000100 class NodesIterator;
101
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000102 /// CompilationGraph - The compilation graph itself.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000103 class CompilationGraph {
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000104 /// nodes_map_type - The main data structure.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000105 typedef llvm::StringMap<Node> nodes_map_type;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000106 /// tools_vector_type, tools_map_type - Data structures used to
107 /// map from language names to tools. (We can have several tools
108 /// associated with each language name, hence the need for a
109 /// vector.)
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000110 typedef
111 llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type;
112 typedef llvm::StringMap<tools_vector_type> tools_map_type;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000113
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000114 /// ExtsToLangs - Map from file extensions to language names.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000115 LanguageMap ExtsToLangs;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000116 /// ToolsMap - Map from language names to lists of tool names.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000117 tools_map_type ToolsMap;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000118 /// NodesMap - Map from tool names to Tool objects.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000119 nodes_map_type NodesMap;
120
121 public:
122
123 CompilationGraph();
124
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000125 /// insertNode - Insert a new node into the graph. Takes
126 /// ownership of the object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000127 void insertNode(Tool* T);
128
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000129 /// insertEdge - Insert a new edge into the graph. Takes ownership
130 /// of the Edge object.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000131 void insertEdge(const std::string& A, Edge* E);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000132
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000133 /// Build - Build target(s) from the input file set. Command-line
134 /// options are passed implicitly as global variables.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000135 int Build(llvm::sys::Path const& tempDir);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000136
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000137 /// getNode -Return a reference to the node correponding to the
138 /// given tool name. Throws std::runtime_error.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000139 Node& getNode(const std::string& ToolName);
140 const Node& getNode(const std::string& ToolName) const;
141
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000142 /// viewGraph - This function is meant for use from the debugger.
143 /// You can just say 'call G->viewGraph()' and a ghostview window
144 /// should pop up from the program, displaying the compilation
145 /// graph. This depends on there being a 'dot' and 'gv' program
146 /// in your path.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000147 void viewGraph();
148
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000149 /// writeGraph - Write a compilation-graph.dot file.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000150 void writeGraph();
151
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000152 // GraphTraits support.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000153 friend NodesIterator GraphBegin(CompilationGraph*);
154 friend NodesIterator GraphEnd(CompilationGraph*);
155 friend void PopulateCompilationGraph(CompilationGraph&);
156
157 private:
158 // Helper functions.
159
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000160 /// getLanguage - Find out which language corresponds to the
161 /// suffix of this file.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000162 const std::string& getLanguage(const llvm::sys::Path& File) const;
163
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000164 /// getToolsVector - Return a reference to the list of tool names
165 /// corresponding to the given language name. Throws
166 /// std::runtime_error.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000167 const tools_vector_type& getToolsVector(const std::string& LangName) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000168
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000169 /// PassThroughGraph - Pass the input file through the toolchain
170 /// starting at StartNode.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000171 void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000172 const InputLanguagesSet& InLangs,
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000173 const llvm::sys::Path& TempDir) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000174
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000175 /// FindToolChain - Find head of the toolchain corresponding to the given file.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000176 const Node* FindToolChain(const llvm::sys::Path& In,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000177 const std::string* forceLanguage,
178 InputLanguagesSet& InLangs) const;
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000179
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000180 /// BuildInitial - Traverse the initial parts of the toolchains.
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000181 void BuildInitial(InputLanguagesSet& InLangs,
182 const llvm::sys::Path& TempDir);
183
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000184 /// TopologicalSort - Sort the nodes in topological order.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000185 void TopologicalSort(std::vector<const Node*>& Out);
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000186 /// TopologicalSortFilterJoinNodes - Call TopologicalSort and
187 /// filter the resulting list to include only Join nodes.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000188 void TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000189 };
190
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000191 // GraphTraits support code.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000192
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000193 /// NodesIterator - Auxiliary class needed to implement GraphTraits
194 /// support. Can be generalised to something like value_iterator
195 /// for map-like containers.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000196 class NodesIterator : public llvm::StringMap<Node>::iterator {
197 typedef llvm::StringMap<Node>::iterator super;
198 typedef NodesIterator ThisType;
199 typedef Node* pointer;
200 typedef Node& reference;
201
202 public:
203 NodesIterator(super I) : super(I) {}
204
205 inline reference operator*() const {
206 return super::operator->()->second;
207 }
208 inline pointer operator->() const {
209 return &super::operator->()->second;
210 }
211 };
212
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000213 inline NodesIterator GraphBegin(CompilationGraph* G) {
214 return NodesIterator(G->NodesMap.begin());
215 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000216
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000217 inline NodesIterator GraphEnd(CompilationGraph* G) {
218 return NodesIterator(G->NodesMap.end());
219 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000220
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000221
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000222 /// NodeChildIterator - Another auxiliary class needed by GraphTraits.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000223 class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
224 typedef NodeChildIterator ThisType;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000225 typedef Node::container_type::iterator iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000226
227 CompilationGraph* OwningGraph;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000228 iterator EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000229 public:
230 typedef Node* pointer;
231 typedef Node& reference;
232
233 NodeChildIterator(Node* N, iterator I) :
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000234 OwningGraph(N->OwningGraph), EdgeIter(I) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000235
236 const ThisType& operator=(const ThisType& I) {
237 assert(OwningGraph == I.OwningGraph);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000238 EdgeIter = I.EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000239 return *this;
240 }
241
242 inline bool operator==(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000243 { return EdgeIter == I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000244 inline bool operator!=(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000245 { return EdgeIter != I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000246
247 inline pointer operator*() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000248 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000249 }
250 inline pointer operator->() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000251 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000252 }
253
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000254 ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000255 ThisType operator++(int) { // Postincrement
256 ThisType tmp = *this;
257 ++*this;
258 return tmp;
259 }
260
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000261 inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000262 inline ThisType operator--(int) { // Postdecrement
263 ThisType tmp = *this;
264 --*this;
265 return tmp;
266 }
267
268 };
269}
270
271namespace llvm {
272 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000273 struct GraphTraits<llvmc::CompilationGraph*> {
274 typedef llvmc::CompilationGraph GraphType;
275 typedef llvmc::Node NodeType;
276 typedef llvmc::NodeChildIterator ChildIteratorType;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000277
278 static NodeType* getEntryNode(GraphType* G) {
279 return &G->getNode("root");
280 }
281
282 static ChildIteratorType child_begin(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000283 return ChildIteratorType(N, N->OutEdges.begin());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000284 }
285 static ChildIteratorType child_end(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000286 return ChildIteratorType(N, N->OutEdges.end());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000287 }
288
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000289 typedef llvmc::NodesIterator nodes_iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000290 static nodes_iterator nodes_begin(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000291 return GraphBegin(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000292 }
293 static nodes_iterator nodes_end(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000294 return GraphEnd(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000295 }
296 };
297
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000298}
299
300#endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H