blob: 132deb8822f8fb1f2ace87df37c72984479fc4c1 [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 Glushenkove0ff9ae2008-05-06 18:18:58 +000032 // A wrapper for StringMap that provides set-like functionality.
33 // Only insert() and count() methods are used by my code.
34 template <class AllocatorTy = llvm::MallocAllocator>
35 class StringSet : public llvm::StringMap<char, AllocatorTy> {
36 typedef llvm::StringMap<char, AllocatorTy> base;
37 public:
38 void insert (const std::string& InLang) {
39 assert(!InLang.empty());
40 const char* KeyStart = &InLang[0];
41 const char* KeyEnd = KeyStart + InLang.size();
42 base::insert(llvm::StringMapEntry<char>::
43 Create(KeyStart, KeyEnd, base::getAllocator(), '+'));
44 }
45 };
46 typedef StringSet<> InputLanguagesSet;
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000047
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000048 // An edge of the compilation graph.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000049 class Edge : public llvm::RefCountedBaseVPTR<Edge> {
50 public:
51 Edge(const std::string& T) : ToolName_(T) {}
52 virtual ~Edge() {};
53
54 const std::string& ToolName() const { return ToolName_; }
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000055 virtual unsigned Weight(const InputLanguagesSet& InLangs) const = 0;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000056 private:
57 std::string ToolName_;
58 };
59
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000060 // Edges that have no properties are instances of this class.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000061 class SimpleEdge : public Edge {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000062 public:
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000063 SimpleEdge(const std::string& T) : Edge(T) {}
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000064 unsigned Weight(const InputLanguagesSet&) const { return 1; }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000065 };
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000066
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000067 // A node of the compilation graph.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000068 struct Node {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000069 // A Node holds a list of the outward edges.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000070 typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
71 typedef container_type::iterator iterator;
72 typedef container_type::const_iterator const_iterator;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000073
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000074 Node() : OwningGraph(0), InEdges(0) {}
75 Node(CompilationGraph* G) : OwningGraph(G), InEdges(0) {}
76 Node(CompilationGraph* G, Tool* T) :
77 OwningGraph(G), ToolPtr(T), InEdges(0) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000078
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000079 bool HasChildren() const { return !OutEdges.empty(); }
Mikhail Glushenkov02606582008-05-06 18:07:14 +000080 const std::string Name() const
81 { return ToolPtr ? ToolPtr->Name() : "root"; }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000082
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000083 // Iteration.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000084 iterator EdgesBegin() { return OutEdges.begin(); }
85 const_iterator EdgesBegin() const { return OutEdges.begin(); }
86 iterator EdgesEnd() { return OutEdges.end(); }
87 const_iterator EdgesEnd() const { return OutEdges.end(); }
88
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000089 // Add an outward edge. Takes ownership of the Edge object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000090 void AddEdge(Edge* E)
91 { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
92
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +000093 // Inward edge counter. Used to implement topological sort.
94 // TOTHINK: Move the mutable counter back into Tool classes? Makes
95 // us more const-correct.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000096 void IncrInEdges() { ++InEdges; }
97 void DecrInEdges() { --InEdges; }
98 bool HasNoInEdges() const { return InEdges == 0; }
99
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000100 // Needed to implement NodeChildIterator/GraphTraits
101 CompilationGraph* OwningGraph;
102 // The corresponding Tool.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000103 // WARNING: ToolPtr can be NULL (for the root node).
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000104 llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
105 // Links to children.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000106 container_type OutEdges;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000107 // Inward edge counter. Updated in
108 // CompilationGraph::insertEdge(). Used for topological sorting.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000109 unsigned InEdges;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000110 };
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000111
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000112 class NodesIterator;
113
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000114 // The compilation graph itself.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000115 class CompilationGraph {
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000116 // Main data structure.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000117 typedef llvm::StringMap<Node> nodes_map_type;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000118 // These are used to map from language names to tools. (We can
119 // have several tools associated with each language name, hence
120 // the need for a vector of Edges.)
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000121 typedef
122 llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type;
123 typedef llvm::StringMap<tools_vector_type> tools_map_type;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000124
125 // Map from file extensions to language names.
126 LanguageMap ExtsToLangs;
127 // Map from language names to lists of tool names.
128 tools_map_type ToolsMap;
129 // Map from tool names to Tool objects.
130 nodes_map_type NodesMap;
131
132 public:
133
134 CompilationGraph();
135
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000136 // insertVertex - insert a new node into the graph. Takes
137 // ownership of the object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000138 void insertNode(Tool* T);
139
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000140 // insertEdge - Insert a new edge into the graph. Takes ownership
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000141 // of the Edge object.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000142 void insertEdge(const std::string& A, Edge* E);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000143
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000144 // Build - Build target(s) from the input file set. Command-line
145 // options are passed implicitly as global variables.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000146 int Build(llvm::sys::Path const& tempDir);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000147
148 // Return a reference to the node correponding to the given tool
149 // name. Throws std::runtime_error.
150 Node& getNode(const std::string& ToolName);
151 const Node& getNode(const std::string& ToolName) const;
152
153 // viewGraph - This function is meant for use from the debugger.
154 // You can just say 'call G->viewGraph()' and a ghostview window
155 // should pop up from the program, displaying the compilation
156 // graph. This depends on there being a 'dot' and 'gv' program
157 // in your path.
158 void viewGraph();
159
160 // Write a CompilationGraph.dot file.
161 void writeGraph();
162
163 // GraphTraits support
164 friend NodesIterator GraphBegin(CompilationGraph*);
165 friend NodesIterator GraphEnd(CompilationGraph*);
166 friend void PopulateCompilationGraph(CompilationGraph&);
167
168 private:
169 // Helper functions.
170
171 // Find out which language corresponds to the suffix of this file.
172 const std::string& getLanguage(const llvm::sys::Path& File) const;
173
174 // Return a reference to the list of tool names corresponding to
175 // the given language name. Throws std::runtime_error.
176 const tools_vector_type& getToolsVector(const std::string& LangName) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000177
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000178 // Pass the input file through the toolchain starting at StartNode.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000179 void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000180 const InputLanguagesSet& InLangs,
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000181 const llvm::sys::Path& TempDir) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000182
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000183 // Find head of the toolchain corresponding to the given file.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000184 const Node* FindToolChain(const llvm::sys::Path& In,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000185 const std::string* forceLanguage,
186 InputLanguagesSet& InLangs) const;
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000187
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000188 // Traverse the initial parts of the toolchains.
189 void BuildInitial(InputLanguagesSet& InLangs,
190 const llvm::sys::Path& TempDir);
191
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000192 // Sort the nodes in topological order.
193 void TopologicalSort(std::vector<const Node*>& Out);
194 // Call TopologicalSort and filter the resulting list to include
195 // only Join nodes.
196 void TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000197 };
198
199 /// GraphTraits support code.
200
201 // Auxiliary class needed to implement GraphTraits support. Can be
202 // generalised to something like value_iterator for map-like
203 // containers.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000204 class NodesIterator : public llvm::StringMap<Node>::iterator {
205 typedef llvm::StringMap<Node>::iterator super;
206 typedef NodesIterator ThisType;
207 typedef Node* pointer;
208 typedef Node& reference;
209
210 public:
211 NodesIterator(super I) : super(I) {}
212
213 inline reference operator*() const {
214 return super::operator->()->second;
215 }
216 inline pointer operator->() const {
217 return &super::operator->()->second;
218 }
219 };
220
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000221 inline NodesIterator GraphBegin(CompilationGraph* G) {
222 return NodesIterator(G->NodesMap.begin());
223 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000224
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000225 inline NodesIterator GraphEnd(CompilationGraph* G) {
226 return NodesIterator(G->NodesMap.end());
227 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000228
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000229
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000230 // Another auxiliary class needed by GraphTraits.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000231 class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
232 typedef NodeChildIterator ThisType;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000233 typedef Node::container_type::iterator iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000234
235 CompilationGraph* OwningGraph;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000236 iterator EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000237 public:
238 typedef Node* pointer;
239 typedef Node& reference;
240
241 NodeChildIterator(Node* N, iterator I) :
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000242 OwningGraph(N->OwningGraph), EdgeIter(I) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000243
244 const ThisType& operator=(const ThisType& I) {
245 assert(OwningGraph == I.OwningGraph);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000246 EdgeIter = I.EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000247 return *this;
248 }
249
250 inline bool operator==(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000251 { return EdgeIter == I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000252 inline bool operator!=(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000253 { return EdgeIter != I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000254
255 inline pointer operator*() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000256 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000257 }
258 inline pointer operator->() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000259 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000260 }
261
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000262 ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000263 ThisType operator++(int) { // Postincrement
264 ThisType tmp = *this;
265 ++*this;
266 return tmp;
267 }
268
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000269 inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000270 inline ThisType operator--(int) { // Postdecrement
271 ThisType tmp = *this;
272 --*this;
273 return tmp;
274 }
275
276 };
277}
278
279namespace llvm {
280 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000281 struct GraphTraits<llvmc::CompilationGraph*> {
282 typedef llvmc::CompilationGraph GraphType;
283 typedef llvmc::Node NodeType;
284 typedef llvmc::NodeChildIterator ChildIteratorType;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000285
286 static NodeType* getEntryNode(GraphType* G) {
287 return &G->getNode("root");
288 }
289
290 static ChildIteratorType child_begin(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000291 return ChildIteratorType(N, N->OutEdges.begin());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000292 }
293 static ChildIteratorType child_end(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000294 return ChildIteratorType(N, N->OutEdges.end());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000295 }
296
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000297 typedef llvmc::NodesIterator nodes_iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000298 static nodes_iterator nodes_begin(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000299 return GraphBegin(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000300 }
301 static nodes_iterator nodes_end(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000302 return GraphEnd(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000303 }
304 };
305
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000306}
307
308#endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H