blob: 2169cd39ce735bdd6f8924a09d2eb819d138a8d2 [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 Glushenkove0ff9ae2008-05-06 18:18:58 +000027#include <cassert>
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 Glushenkov4561ab52008-05-07 21:50:19 +000032 /// StringSet - A wrapper for StringMap that provides set-like
33 /// functionality. Only insert() and count() methods are used by my
34 /// code.
Mikhail Glushenkove0ff9ae2008-05-06 18:18:58 +000035 template <class AllocatorTy = llvm::MallocAllocator>
36 class StringSet : public llvm::StringMap<char, AllocatorTy> {
37 typedef llvm::StringMap<char, AllocatorTy> base;
38 public:
39 void insert (const std::string& InLang) {
40 assert(!InLang.empty());
41 const char* KeyStart = &InLang[0];
42 const char* KeyEnd = KeyStart + InLang.size();
43 base::insert(llvm::StringMapEntry<char>::
44 Create(KeyStart, KeyEnd, base::getAllocator(), '+'));
45 }
46 };
47 typedef StringSet<> InputLanguagesSet;
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000048
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000049 /// Edge - Represents an edge of the compilation graph.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000050 class Edge : public llvm::RefCountedBaseVPTR<Edge> {
51 public:
52 Edge(const std::string& T) : ToolName_(T) {}
53 virtual ~Edge() {};
54
55 const std::string& ToolName() const { return ToolName_; }
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000056 virtual unsigned Weight(const InputLanguagesSet& InLangs) const = 0;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000057 private:
58 std::string ToolName_;
59 };
60
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000061 /// SimpleEdge - An edge that has no properties.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000062 class SimpleEdge : public Edge {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000063 public:
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000064 SimpleEdge(const std::string& T) : Edge(T) {}
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000065 unsigned Weight(const InputLanguagesSet&) const { return 1; }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000066 };
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000067
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000068 /// Node - A node (vertex) of the compilation graph.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000069 struct Node {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000070 // A Node holds a list of the outward edges.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000071 typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
72 typedef container_type::iterator iterator;
73 typedef container_type::const_iterator const_iterator;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000074
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000075 Node() : OwningGraph(0), InEdges(0) {}
76 Node(CompilationGraph* G) : OwningGraph(G), InEdges(0) {}
77 Node(CompilationGraph* G, Tool* T) :
78 OwningGraph(G), ToolPtr(T), InEdges(0) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000079
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000080 bool HasChildren() const { return !OutEdges.empty(); }
Mikhail Glushenkov02606582008-05-06 18:07:14 +000081 const std::string Name() const
82 { return ToolPtr ? ToolPtr->Name() : "root"; }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000083
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000084 // Iteration.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000085 iterator EdgesBegin() { return OutEdges.begin(); }
86 const_iterator EdgesBegin() const { return OutEdges.begin(); }
87 iterator EdgesEnd() { return OutEdges.end(); }
88 const_iterator EdgesEnd() const { return OutEdges.end(); }
89
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000090 /// AddEdge - Add an outward edge. Takes ownership of the provided
91 /// Edge object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000092 void AddEdge(Edge* E)
93 { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
94
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000095 // Inward edge counter. Used to implement topological sort.
96 // TOTHINK: Move the mutable counter back into Tool classes? Makes
97 // us more const-correct.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000098 void IncrInEdges() { ++InEdges; }
99 void DecrInEdges() { --InEdges; }
100 bool HasNoInEdges() const { return InEdges == 0; }
101
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000102 // Needed to implement NodeChildIterator/GraphTraits
103 CompilationGraph* OwningGraph;
104 // The corresponding Tool.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000105 // WARNING: ToolPtr can be NULL (for the root node).
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000106 llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
107 // Links to children.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000108 container_type OutEdges;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000109 // Inward edge counter. Updated in
110 // CompilationGraph::insertEdge(). Used for topological sorting.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000111 unsigned InEdges;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000112 };
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000113
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000114 class NodesIterator;
115
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000116 /// CompilationGraph - The compilation graph itself.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000117 class CompilationGraph {
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000118 /// nodes_map_type - The main data structure.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000119 typedef llvm::StringMap<Node> nodes_map_type;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000120 /// tools_vector_type, tools_map_type - Data structures used to
121 /// map from language names to tools. (We can have several tools
122 /// associated with each language name, hence the need for a
123 /// vector.)
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000124 typedef
125 llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type;
126 typedef llvm::StringMap<tools_vector_type> tools_map_type;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000127
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000128 /// ExtsToLangs - Map from file extensions to language names.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000129 LanguageMap ExtsToLangs;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000130 /// ToolsMap - Map from language names to lists of tool names.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000131 tools_map_type ToolsMap;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000132 /// NodesMap - Map from tool names to Tool objects.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000133 nodes_map_type NodesMap;
134
135 public:
136
137 CompilationGraph();
138
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000139 /// insertNode - Insert a new node into the graph. Takes
140 /// ownership of the object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000141 void insertNode(Tool* T);
142
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000143 /// insertEdge - Insert a new edge into the graph. Takes ownership
144 /// of the Edge object.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000145 void insertEdge(const std::string& A, Edge* E);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000146
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000147 /// Build - Build target(s) from the input file set. Command-line
148 /// options are passed implicitly as global variables.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000149 int Build(llvm::sys::Path const& tempDir);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000150
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000151 /// getNode -Return a reference to the node correponding to the
152 /// given tool name. Throws std::runtime_error.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000153 Node& getNode(const std::string& ToolName);
154 const Node& getNode(const std::string& ToolName) const;
155
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000156 /// viewGraph - This function is meant for use from the debugger.
157 /// You can just say 'call G->viewGraph()' and a ghostview window
158 /// should pop up from the program, displaying the compilation
159 /// graph. This depends on there being a 'dot' and 'gv' program
160 /// in your path.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000161 void viewGraph();
162
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000163 /// writeGraph - Write a compilation-graph.dot file.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000164 void writeGraph();
165
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000166 // GraphTraits support.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000167 friend NodesIterator GraphBegin(CompilationGraph*);
168 friend NodesIterator GraphEnd(CompilationGraph*);
169 friend void PopulateCompilationGraph(CompilationGraph&);
170
171 private:
172 // Helper functions.
173
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000174 /// getLanguage - Find out which language corresponds to the
175 /// suffix of this file.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000176 const std::string& getLanguage(const llvm::sys::Path& File) const;
177
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000178 /// getToolsVector - Return a reference to the list of tool names
179 /// corresponding to the given language name. Throws
180 /// std::runtime_error.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000181 const tools_vector_type& getToolsVector(const std::string& LangName) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000182
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000183 /// PassThroughGraph - Pass the input file through the toolchain
184 /// starting at StartNode.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000185 void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000186 const InputLanguagesSet& InLangs,
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000187 const llvm::sys::Path& TempDir) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000188
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000189 /// FindToolChain - Find head of the toolchain corresponding to the given file.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000190 const Node* FindToolChain(const llvm::sys::Path& In,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000191 const std::string* forceLanguage,
192 InputLanguagesSet& InLangs) const;
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000193
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000194 /// BuildInitial - Traverse the initial parts of the toolchains.
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000195 void BuildInitial(InputLanguagesSet& InLangs,
196 const llvm::sys::Path& TempDir);
197
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000198 /// TopologicalSort - Sort the nodes in topological order.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000199 void TopologicalSort(std::vector<const Node*>& Out);
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000200 /// TopologicalSortFilterJoinNodes - Call TopologicalSort and
201 /// filter the resulting list to include only Join nodes.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000202 void TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000203 };
204
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000205 // GraphTraits support code.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000206
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000207 /// NodesIterator - Auxiliary class needed to implement GraphTraits
208 /// support. Can be generalised to something like value_iterator
209 /// for map-like containers.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000210 class NodesIterator : public llvm::StringMap<Node>::iterator {
211 typedef llvm::StringMap<Node>::iterator super;
212 typedef NodesIterator ThisType;
213 typedef Node* pointer;
214 typedef Node& reference;
215
216 public:
217 NodesIterator(super I) : super(I) {}
218
219 inline reference operator*() const {
220 return super::operator->()->second;
221 }
222 inline pointer operator->() const {
223 return &super::operator->()->second;
224 }
225 };
226
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000227 inline NodesIterator GraphBegin(CompilationGraph* G) {
228 return NodesIterator(G->NodesMap.begin());
229 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000230
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000231 inline NodesIterator GraphEnd(CompilationGraph* G) {
232 return NodesIterator(G->NodesMap.end());
233 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000234
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000235
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000236 /// NodeChildIterator - Another auxiliary class needed by GraphTraits.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000237 class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
238 typedef NodeChildIterator ThisType;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000239 typedef Node::container_type::iterator iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000240
241 CompilationGraph* OwningGraph;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000242 iterator EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000243 public:
244 typedef Node* pointer;
245 typedef Node& reference;
246
247 NodeChildIterator(Node* N, iterator I) :
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000248 OwningGraph(N->OwningGraph), EdgeIter(I) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000249
250 const ThisType& operator=(const ThisType& I) {
251 assert(OwningGraph == I.OwningGraph);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000252 EdgeIter = I.EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000253 return *this;
254 }
255
256 inline bool operator==(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000257 { return EdgeIter == I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000258 inline bool operator!=(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000259 { return EdgeIter != I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000260
261 inline pointer operator*() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000262 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000263 }
264 inline pointer operator->() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000265 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000266 }
267
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000268 ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000269 ThisType operator++(int) { // Postincrement
270 ThisType tmp = *this;
271 ++*this;
272 return tmp;
273 }
274
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000275 inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000276 inline ThisType operator--(int) { // Postdecrement
277 ThisType tmp = *this;
278 --*this;
279 return tmp;
280 }
281
282 };
283}
284
285namespace llvm {
286 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000287 struct GraphTraits<llvmc::CompilationGraph*> {
288 typedef llvmc::CompilationGraph GraphType;
289 typedef llvmc::Node NodeType;
290 typedef llvmc::NodeChildIterator ChildIteratorType;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000291
292 static NodeType* getEntryNode(GraphType* G) {
293 return &G->getNode("root");
294 }
295
296 static ChildIteratorType child_begin(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000297 return ChildIteratorType(N, N->OutEdges.begin());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000298 }
299 static ChildIteratorType child_end(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000300 return ChildIteratorType(N, N->OutEdges.end());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000301 }
302
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000303 typedef llvmc::NodesIterator nodes_iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000304 static nodes_iterator nodes_begin(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000305 return GraphBegin(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000306 }
307 static nodes_iterator nodes_end(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000308 return GraphEnd(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000309 }
310 };
311
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000312}
313
314#endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H