blob: dc388d0fa74deb91de2a2c96b8d3cd9dad91cdb6 [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 Glushenkov0d08db02008-05-06 16:35:25 +000027#include <string>
28
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000029namespace llvmcc {
30
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000031 // An edge in the graph.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000032 class Edge : public llvm::RefCountedBaseVPTR<Edge> {
33 public:
34 Edge(const std::string& T) : ToolName_(T) {}
35 virtual ~Edge() {};
36
37 const std::string& ToolName() const { return ToolName_; }
38 virtual bool isEnabled() const = 0;
39 virtual bool isDefault() const = 0;
40 private:
41 std::string ToolName_;
42 };
43
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000044 // Edges with no properties are instances of this class.
45 class SimpleEdge : public Edge {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000046 public:
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000047 SimpleEdge(const std::string& T) : Edge(T) {}
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000048 bool isEnabled() const { return false;}
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000049 bool isDefault() const { return true;}
50 };
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000051
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000052 // A node in the graph.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000053 struct Node {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000054 typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
55 typedef container_type::iterator iterator;
56 typedef container_type::const_iterator const_iterator;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000057
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000058 Node() : OwningGraph(0), InEdges(0) {}
59 Node(CompilationGraph* G) : OwningGraph(G), InEdges(0) {}
60 Node(CompilationGraph* G, Tool* T) :
61 OwningGraph(G), ToolPtr(T), InEdges(0) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000062
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000063 bool HasChildren() const { return !OutEdges.empty(); }
Mikhail Glushenkov02606582008-05-06 18:07:14 +000064 const std::string Name() const
65 { return ToolPtr ? ToolPtr->Name() : "root"; }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000066
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000067 // Iteration.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000068 iterator EdgesBegin() { return OutEdges.begin(); }
69 const_iterator EdgesBegin() const { return OutEdges.begin(); }
70 iterator EdgesEnd() { return OutEdges.end(); }
71 const_iterator EdgesEnd() const { return OutEdges.end(); }
72
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000073 // Add an outward edge. Takes ownership of the Edge object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000074 void AddEdge(Edge* E)
75 { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
76
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000077 // Inward edge counter. Used by Build() to implement topological
78 // sort.
Mikhail Glushenkov02606582008-05-06 18:07:14 +000079 // TOTHINK: Move the counter back into Tool classes? Makes us more
80 // const-correct.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000081 void IncrInEdges() { ++InEdges; }
82 void DecrInEdges() { --InEdges; }
83 bool HasNoInEdges() const { return InEdges == 0; }
84
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000085 // Needed to implement NodeChildIterator/GraphTraits
86 CompilationGraph* OwningGraph;
87 // The corresponding Tool.
Mikhail Glushenkov02606582008-05-06 18:07:14 +000088 // WARNING: For the root node, ToolPtr is NULL.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000089 llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
90 // Links to children.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000091 container_type OutEdges;
Mikhail Glushenkov02606582008-05-06 18:07:14 +000092 // Number of parents. Used for topological sorting.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000093 unsigned InEdges;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000094 };
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000095
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000096 class NodesIterator;
97
Mikhail Glushenkov02606582008-05-06 18:07:14 +000098 // The compilation graph itself.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000099 class CompilationGraph {
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000100 // Main data structure.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000101 typedef llvm::StringMap<Node> nodes_map_type;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000102 // These are used to map from language names-> tools. (We can have
103 // several tools associated with each language name.)
104 typedef
105 llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type;
106 typedef llvm::StringMap<tools_vector_type> tools_map_type;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000107
108 // Map from file extensions to language names.
109 LanguageMap ExtsToLangs;
110 // Map from language names to lists of tool names.
111 tools_map_type ToolsMap;
112 // Map from tool names to Tool objects.
113 nodes_map_type NodesMap;
114
115 public:
116
117 CompilationGraph();
118
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000119 // insertVertex - insert a new node into the graph. Takes
120 // ownership of the object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000121 void insertNode(Tool* T);
122
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000123 // insertEdge - Insert a new edge into the graph. Takes ownership
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000124 // of the Edge object.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000125 void insertEdge(const std::string& A, Edge* E);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000126
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000127 // Build - Build target(s) from the input file set. Command-line
128 // options are passed implicitly as global variables.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000129 int Build(llvm::sys::Path const& tempDir);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000130
131 // Return a reference to the node correponding to the given tool
132 // name. Throws std::runtime_error.
133 Node& getNode(const std::string& ToolName);
134 const Node& getNode(const std::string& ToolName) const;
135
136 // viewGraph - This function is meant for use from the debugger.
137 // You can just say 'call G->viewGraph()' and a ghostview window
138 // should pop up from the program, displaying the compilation
139 // graph. This depends on there being a 'dot' and 'gv' program
140 // in your path.
141 void viewGraph();
142
143 // Write a CompilationGraph.dot file.
144 void writeGraph();
145
146 // GraphTraits support
147 friend NodesIterator GraphBegin(CompilationGraph*);
148 friend NodesIterator GraphEnd(CompilationGraph*);
149 friend void PopulateCompilationGraph(CompilationGraph&);
150
151 private:
152 // Helper functions.
153
154 // Find out which language corresponds to the suffix of this file.
155 const std::string& getLanguage(const llvm::sys::Path& File) const;
156
157 // Return a reference to the list of tool names corresponding to
158 // the given language name. Throws std::runtime_error.
159 const tools_vector_type& getToolsVector(const std::string& LangName) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000160
161 // Pass the input file through the toolchain.
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000162 const JoinTool* PassThroughGraph (llvm::sys::Path& In,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000163 const Node* StartNode,
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000164 const llvm::sys::Path& TempDir) const;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000165
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000166 // Find head of the toolchain corresponding to the given file.
167 const Node* FindToolChain(const llvm::sys::Path& In) const;
168
169 // Sort the nodes in topological order.
170 void TopologicalSort(std::vector<const Node*>& Out);
171 // Call TopologicalSort and filter the resulting list to include
172 // only Join nodes.
173 void TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000174 };
175
176 /// GraphTraits support code.
177
178 // Auxiliary class needed to implement GraphTraits support. Can be
179 // generalised to something like value_iterator for map-like
180 // containers.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000181 class NodesIterator : public llvm::StringMap<Node>::iterator {
182 typedef llvm::StringMap<Node>::iterator super;
183 typedef NodesIterator ThisType;
184 typedef Node* pointer;
185 typedef Node& reference;
186
187 public:
188 NodesIterator(super I) : super(I) {}
189
190 inline reference operator*() const {
191 return super::operator->()->second;
192 }
193 inline pointer operator->() const {
194 return &super::operator->()->second;
195 }
196 };
197
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000198 inline NodesIterator GraphBegin(CompilationGraph* G) {
199 return NodesIterator(G->NodesMap.begin());
200 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000201
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000202 inline NodesIterator GraphEnd(CompilationGraph* G) {
203 return NodesIterator(G->NodesMap.end());
204 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000205
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000206
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000207 // Another auxiliary class needed by GraphTraits.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000208 class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
209 typedef NodeChildIterator ThisType;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000210 typedef Node::container_type::iterator iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000211
212 CompilationGraph* OwningGraph;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000213 iterator EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000214 public:
215 typedef Node* pointer;
216 typedef Node& reference;
217
218 NodeChildIterator(Node* N, iterator I) :
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000219 OwningGraph(N->OwningGraph), EdgeIter(I) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000220
221 const ThisType& operator=(const ThisType& I) {
222 assert(OwningGraph == I.OwningGraph);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000223 EdgeIter = I.EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000224 return *this;
225 }
226
227 inline bool operator==(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000228 { return EdgeIter == I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000229 inline bool operator!=(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000230 { return EdgeIter != I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000231
232 inline pointer operator*() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000233 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000234 }
235 inline pointer operator->() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000236 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000237 }
238
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000239 ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000240 ThisType operator++(int) { // Postincrement
241 ThisType tmp = *this;
242 ++*this;
243 return tmp;
244 }
245
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000246 inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000247 inline ThisType operator--(int) { // Postdecrement
248 ThisType tmp = *this;
249 --*this;
250 return tmp;
251 }
252
253 };
254}
255
256namespace llvm {
257 template <>
258 struct GraphTraits<llvmcc::CompilationGraph*> {
259 typedef llvmcc::CompilationGraph GraphType;
260 typedef llvmcc::Node NodeType;
261 typedef llvmcc::NodeChildIterator ChildIteratorType;
262
263 static NodeType* getEntryNode(GraphType* G) {
264 return &G->getNode("root");
265 }
266
267 static ChildIteratorType child_begin(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000268 return ChildIteratorType(N, N->OutEdges.begin());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000269 }
270 static ChildIteratorType child_end(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000271 return ChildIteratorType(N, N->OutEdges.end());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000272 }
273
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000274 typedef llvmcc::NodesIterator nodes_iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000275 static nodes_iterator nodes_begin(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000276 return GraphBegin(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000277 }
278 static nodes_iterator nodes_end(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000279 return GraphEnd(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000280 }
281 };
282
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000283}
284
285#endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H