blob: 71ae2272eb80e61a4bf2adcfcab0097a02093e0e [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 Glushenkov11a353a2008-09-22 20:47:46 +000035 /// LanguageMap - Maps from extensions to language names.
36 class LanguageMap : public llvm::StringMap<std::string> {
37 public:
38
39 /// GetLanguage - Find the language name corresponding to a given file.
40 const std::string& GetLanguage(const llvm::sys::Path&) const;
41 };
42
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000043 /// Edge - Represents an edge of the compilation graph.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000044 class Edge : public llvm::RefCountedBaseVPTR<Edge> {
45 public:
46 Edge(const std::string& T) : ToolName_(T) {}
47 virtual ~Edge() {};
48
49 const std::string& ToolName() const { return ToolName_; }
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000050 virtual unsigned Weight(const InputLanguagesSet& InLangs) const = 0;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000051 private:
52 std::string ToolName_;
53 };
54
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000055 /// SimpleEdge - An edge that has no properties.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000056 class SimpleEdge : public Edge {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000057 public:
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000058 SimpleEdge(const std::string& T) : Edge(T) {}
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000059 unsigned Weight(const InputLanguagesSet&) const { return 1; }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000060 };
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000061
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000062 /// Node - A node (vertex) of the compilation graph.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000063 struct Node {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000064 // A Node holds a list of the outward edges.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000065 typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
66 typedef container_type::iterator iterator;
67 typedef container_type::const_iterator const_iterator;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000068
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000069 Node() : OwningGraph(0), InEdges(0) {}
70 Node(CompilationGraph* G) : OwningGraph(G), InEdges(0) {}
71 Node(CompilationGraph* G, Tool* T) :
72 OwningGraph(G), ToolPtr(T), InEdges(0) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000073
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000074 bool HasChildren() const { return !OutEdges.empty(); }
Mikhail Glushenkov02606582008-05-06 18:07:14 +000075 const std::string Name() const
76 { return ToolPtr ? ToolPtr->Name() : "root"; }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000077
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000078 // Iteration.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000079 iterator EdgesBegin() { return OutEdges.begin(); }
80 const_iterator EdgesBegin() const { return OutEdges.begin(); }
81 iterator EdgesEnd() { return OutEdges.end(); }
82 const_iterator EdgesEnd() const { return OutEdges.end(); }
83
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000084 /// AddEdge - Add an outward edge. Takes ownership of the provided
85 /// Edge object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000086 void AddEdge(Edge* E)
87 { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
88
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000089 // Inward edge counter. Used to implement topological sort.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000090 void IncrInEdges() { ++InEdges; }
91 void DecrInEdges() { --InEdges; }
92 bool HasNoInEdges() const { return InEdges == 0; }
93
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000094 // Needed to implement NodeChildIterator/GraphTraits
95 CompilationGraph* OwningGraph;
96 // The corresponding Tool.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000097 // WARNING: ToolPtr can be NULL (for the root node).
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000098 llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
99 // Links to children.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000100 container_type OutEdges;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000101 // Inward edge counter. Updated in
102 // CompilationGraph::insertEdge(). Used for topological sorting.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000103 unsigned InEdges;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000104 };
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000105
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000106 class NodesIterator;
107
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000108 /// CompilationGraph - The compilation graph itself.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000109 class CompilationGraph {
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000110 /// nodes_map_type - The main data structure.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000111 typedef llvm::StringMap<Node> nodes_map_type;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000112 /// tools_vector_type, tools_map_type - Data structures used to
113 /// map from language names to tools. (We can have several tools
114 /// associated with each language name, hence the need for a
115 /// vector.)
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000116 typedef
117 llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type;
118 typedef llvm::StringMap<tools_vector_type> tools_map_type;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000119
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000120 /// ToolsMap - Map from language names to lists of tool names.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000121 tools_map_type ToolsMap;
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000122 /// NodesMap - Map from tool names to Tool objects.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000123 nodes_map_type NodesMap;
124
125 public:
126
127 CompilationGraph();
128
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000129 /// insertNode - Insert a new node into the graph. Takes
130 /// ownership of the object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000131 void insertNode(Tool* T);
132
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000133 /// insertEdge - Insert a new edge into the graph. Takes ownership
134 /// of the Edge object.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000135 void insertEdge(const std::string& A, Edge* E);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000136
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000137 /// Build - Build target(s) from the input file set. Command-line
138 /// options are passed implicitly as global variables.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000139 int Build(llvm::sys::Path const& TempDir, const LanguageMap& LangMap);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000140
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +0000141 /// getNode - Return a reference to the node correponding to the
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000142 /// given tool name. Throws std::runtime_error.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000143 Node& getNode(const std::string& ToolName);
144 const Node& getNode(const std::string& ToolName) const;
145
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000146 /// viewGraph - This function is meant for use from the debugger.
147 /// You can just say 'call G->viewGraph()' and a ghostview window
148 /// should pop up from the program, displaying the compilation
149 /// graph. This depends on there being a 'dot' and 'gv' program
150 /// in your path.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000151 void viewGraph();
152
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000153 /// writeGraph - Write a compilation-graph.dot file.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000154 void writeGraph();
155
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000156 // GraphTraits support.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000157 friend NodesIterator GraphBegin(CompilationGraph*);
158 friend NodesIterator GraphEnd(CompilationGraph*);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000159
160 private:
161 // Helper functions.
162
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000163 /// getToolsVector - Return a reference to the list of tool names
164 /// corresponding to the given language name. Throws
165 /// std::runtime_error.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000166 const tools_vector_type& getToolsVector(const std::string& LangName) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000167
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000168 /// PassThroughGraph - Pass the input file through the toolchain
169 /// starting at StartNode.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000170 void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000171 const InputLanguagesSet& InLangs,
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000172 const llvm::sys::Path& TempDir,
173 const LanguageMap& LangMap) 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 Glushenkov11a353a2008-09-22 20:47:46 +0000177 const std::string* ForceLanguage,
178 InputLanguagesSet& InLangs,
179 const LanguageMap& LangMap) const;
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000180
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000181 /// BuildInitial - Traverse the initial parts of the toolchains.
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000182 void BuildInitial(InputLanguagesSet& InLangs,
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000183 const llvm::sys::Path& TempDir,
184 const LanguageMap& LangMap);
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000185
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000186 /// TopologicalSort - Sort the nodes in topological order.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000187 void TopologicalSort(std::vector<const Node*>& Out);
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000188 /// TopologicalSortFilterJoinNodes - Call TopologicalSort and
189 /// filter the resulting list to include only Join nodes.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000190 void TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000191 };
192
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000193 // GraphTraits support code.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000194
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000195 /// NodesIterator - Auxiliary class needed to implement GraphTraits
196 /// support. Can be generalised to something like value_iterator
197 /// for map-like containers.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000198 class NodesIterator : public llvm::StringMap<Node>::iterator {
199 typedef llvm::StringMap<Node>::iterator super;
200 typedef NodesIterator ThisType;
201 typedef Node* pointer;
202 typedef Node& reference;
203
204 public:
205 NodesIterator(super I) : super(I) {}
206
207 inline reference operator*() const {
208 return super::operator->()->second;
209 }
210 inline pointer operator->() const {
211 return &super::operator->()->second;
212 }
213 };
214
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000215 inline NodesIterator GraphBegin(CompilationGraph* G) {
216 return NodesIterator(G->NodesMap.begin());
217 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000218
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000219 inline NodesIterator GraphEnd(CompilationGraph* G) {
220 return NodesIterator(G->NodesMap.end());
221 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000222
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000223
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +0000224 /// NodeChildIterator - Another auxiliary class needed by GraphTraits.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000225 class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
226 typedef NodeChildIterator ThisType;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000227 typedef Node::container_type::iterator iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000228
229 CompilationGraph* OwningGraph;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000230 iterator EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000231 public:
232 typedef Node* pointer;
233 typedef Node& reference;
234
235 NodeChildIterator(Node* N, iterator I) :
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000236 OwningGraph(N->OwningGraph), EdgeIter(I) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000237
238 const ThisType& operator=(const ThisType& I) {
239 assert(OwningGraph == I.OwningGraph);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000240 EdgeIter = I.EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000241 return *this;
242 }
243
244 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 inline bool operator!=(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000247 { return EdgeIter != I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000248
249 inline pointer operator*() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000250 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000251 }
252 inline pointer operator->() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000253 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000254 }
255
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000256 ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000257 ThisType operator++(int) { // Postincrement
258 ThisType tmp = *this;
259 ++*this;
260 return tmp;
261 }
262
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000263 inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000264 inline ThisType operator--(int) { // Postdecrement
265 ThisType tmp = *this;
266 --*this;
267 return tmp;
268 }
269
270 };
271}
272
273namespace llvm {
274 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000275 struct GraphTraits<llvmc::CompilationGraph*> {
276 typedef llvmc::CompilationGraph GraphType;
277 typedef llvmc::Node NodeType;
278 typedef llvmc::NodeChildIterator ChildIteratorType;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000279
280 static NodeType* getEntryNode(GraphType* G) {
281 return &G->getNode("root");
282 }
283
284 static ChildIteratorType child_begin(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000285 return ChildIteratorType(N, N->OutEdges.begin());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000286 }
287 static ChildIteratorType child_end(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000288 return ChildIteratorType(N, N->OutEdges.end());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000289 }
290
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000291 typedef llvmc::NodesIterator nodes_iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000292 static nodes_iterator nodes_begin(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000293 return GraphBegin(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000294 }
295 static nodes_iterator nodes_end(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000296 return GraphEnd(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000297 }
298 };
299
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000300}
301
302#endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H