2579b2630b014f798b1d91558cef5c369e644bf2
[oota-llvm.git] / include / llvm / CompilerDriver / CompilationGraph.h
1 //===--- 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_INCLUDE_COMPILER_DRIVER_COMPILATION_GRAPH_H
15 #define LLVM_INCLUDE_COMPILER_DRIVER_COMPILATION_GRAPH_H
16
17 #include "llvm/CompilerDriver/Tool.h"
18
19 #include "llvm/ADT/GraphTraits.h"
20 #include "llvm/ADT/IntrusiveRefCntPtr.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringSet.h"
24 #include "llvm/System/Path.h"
25
26 #include <cassert>
27 #include <string>
28
29 namespace llvmc {
30
31   class CompilationGraph;
32   typedef llvm::StringSet<> InputLanguagesSet;
33
34   /// LanguageMap - Maps from extensions to language names.
35   class LanguageMap : public llvm::StringMap<std::string> {
36   public:
37
38     /// GetLanguage -  Find the language name corresponding to a given file.
39     const std::string& GetLanguage(const llvm::sys::Path&) const;
40   };
41
42   /// Edge - Represents an edge of the compilation graph.
43   class Edge : public llvm::RefCountedBaseVPTR<Edge> {
44   public:
45     Edge(const std::string& T) : ToolName_(T) {}
46     virtual ~Edge() {};
47
48     const std::string& ToolName() const { return ToolName_; }
49     virtual unsigned Weight(const InputLanguagesSet& InLangs) const = 0;
50   private:
51     std::string ToolName_;
52   };
53
54   /// SimpleEdge - An edge that has no properties.
55   class SimpleEdge : public Edge {
56   public:
57     SimpleEdge(const std::string& T) : Edge(T) {}
58     unsigned Weight(const InputLanguagesSet&) const { return 1; }
59   };
60
61   /// Node - A node (vertex) of the compilation graph.
62   struct Node {
63     // A Node holds a list of the outward edges.
64     typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
65     typedef container_type::iterator iterator;
66     typedef container_type::const_iterator const_iterator;
67
68     Node() : OwningGraph(0), InEdges(0) {}
69     Node(CompilationGraph* G) : OwningGraph(G), InEdges(0) {}
70     Node(CompilationGraph* G, Tool* T) :
71       OwningGraph(G), ToolPtr(T), InEdges(0) {}
72
73     bool HasChildren() const { return !OutEdges.empty(); }
74     const std::string Name() const
75     { return ToolPtr ? ToolPtr->Name() : "root"; }
76
77     // Iteration.
78     iterator EdgesBegin() { return OutEdges.begin(); }
79     const_iterator EdgesBegin() const { return OutEdges.begin(); }
80     iterator EdgesEnd() { return OutEdges.end(); }
81     const_iterator EdgesEnd() const { return OutEdges.end(); }
82
83     /// AddEdge - Add an outward edge. Takes ownership of the provided
84     /// Edge object.
85     void AddEdge(Edge* E);
86
87     // Inward edge counter. Used to implement topological sort.
88     void IncrInEdges() { ++InEdges; }
89     void DecrInEdges() { --InEdges; }
90     bool HasNoInEdges() const { return InEdges == 0; }
91
92     // Needed to implement NodeChildIterator/GraphTraits
93     CompilationGraph* OwningGraph;
94     // The corresponding Tool.
95     // WARNING: ToolPtr can be NULL (for the root node).
96     llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
97     // Links to children.
98     container_type OutEdges;
99     // Inward edge counter. Updated in
100     // CompilationGraph::insertEdge(). Used for topological sorting.
101     unsigned InEdges;
102   };
103
104   class NodesIterator;
105
106   /// CompilationGraph - The compilation graph itself.
107   class CompilationGraph {
108     /// nodes_map_type - The main data structure.
109     typedef llvm::StringMap<Node> nodes_map_type;
110     /// tools_vector_type, tools_map_type - Data structures used to
111     /// map from language names to tools. (We can have several tools
112     /// associated with each language name, hence the need for a
113     /// vector.)
114     typedef
115     llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type;
116     typedef llvm::StringMap<tools_vector_type> tools_map_type;
117
118     /// ToolsMap - Map from language names to lists of tool names.
119     tools_map_type ToolsMap;
120     /// NodesMap - Map from tool names to Tool objects.
121     nodes_map_type NodesMap;
122
123   public:
124
125     typedef nodes_map_type::iterator nodes_iterator;
126     typedef nodes_map_type::const_iterator const_nodes_iterator;
127
128     CompilationGraph();
129
130     /// insertNode - Insert a new node into the graph. Takes
131     /// ownership of the object.
132     void insertNode(Tool* T);
133
134     /// insertEdge - Insert a new edge into the graph. Takes ownership
135     /// of the Edge object.
136     void insertEdge(const std::string& A, Edge* E);
137
138     /// Build - Build target(s) from the input file set. Command-line
139     /// options are passed implicitly as global variables.
140     int Build(llvm::sys::Path const& TempDir, const LanguageMap& LangMap);
141
142     /// Check - Check the compilation graph for common errors like
143     /// cycles, input/output language mismatch and multiple default
144     /// edges. Prints error messages and in case it finds any errors.
145     int Check();
146
147     /// getNode - Return a reference to the node correponding to the
148     /// given tool name. Throws std::runtime_error.
149     Node& getNode(const std::string& ToolName);
150     const Node& getNode(const std::string& ToolName) const;
151
152     /// viewGraph - This function is meant for use from the debugger.
153     /// You can just say 'call G->viewGraph()' and a ghostview window
154     /// should pop up from the program, displaying the compilation
155     /// graph. This depends on there being a 'dot' and 'gv' program
156     /// in your path.
157     void viewGraph();
158
159     /// writeGraph - Write Graphviz .dot source file to the current direcotry.
160     void writeGraph(const std::string& OutputFilename);
161
162     // GraphTraits support.
163     friend NodesIterator GraphBegin(CompilationGraph*);
164     friend NodesIterator GraphEnd(CompilationGraph*);
165
166   private:
167     // Helper functions.
168
169     /// getToolsVector - Return a reference to the list of tool names
170     /// corresponding to the given language name. Throws
171     /// std::runtime_error.
172     const tools_vector_type& getToolsVector(const std::string& LangName) const;
173
174     /// PassThroughGraph - Pass the input file through the toolchain
175     /// starting at StartNode.
176     void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
177                            const InputLanguagesSet& InLangs,
178                            const llvm::sys::Path& TempDir,
179                            const LanguageMap& LangMap) const;
180
181     /// FindToolChain - Find head of the toolchain corresponding to
182     /// the given file.
183     const Node* FindToolChain(const llvm::sys::Path& In,
184                               const std::string* ForceLanguage,
185                               InputLanguagesSet& InLangs,
186                               const LanguageMap& LangMap) const;
187
188     /// BuildInitial - Traverse the initial parts of the toolchains.
189     void BuildInitial(InputLanguagesSet& InLangs,
190                       const llvm::sys::Path& TempDir,
191                       const LanguageMap& LangMap);
192
193     /// TopologicalSort - Sort the nodes in topological order.
194     void TopologicalSort(std::vector<const Node*>& Out);
195     /// TopologicalSortFilterJoinNodes - Call TopologicalSort and
196     /// filter the resulting list to include only Join nodes.
197     void TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out);
198
199     // Functions used to implement Check().
200
201     /// CheckLanguageNames - Check that output/input language names
202     /// match for all nodes.
203     int CheckLanguageNames() const;
204     /// CheckMultipleDefaultEdges - check that there are no multiple
205     /// default default edges.
206     int CheckMultipleDefaultEdges() const;
207     /// CheckCycles - Check that there are no cycles in the graph.
208     int CheckCycles();
209
210   };
211
212   // GraphTraits support code.
213
214   /// NodesIterator - Auxiliary class needed to implement GraphTraits
215   /// support. Can be generalised to something like value_iterator
216   /// for map-like containers.
217   class NodesIterator : public CompilationGraph::nodes_iterator {
218     typedef CompilationGraph::nodes_iterator super;
219     typedef NodesIterator ThisType;
220     typedef Node* pointer;
221     typedef Node& reference;
222
223   public:
224     NodesIterator(super I) : super(I) {}
225
226     inline reference operator*() const {
227       return super::operator->()->second;
228     }
229     inline pointer operator->() const {
230       return &super::operator->()->second;
231     }
232   };
233
234   inline NodesIterator GraphBegin(CompilationGraph* G) {
235     return NodesIterator(G->NodesMap.begin());
236   }
237
238   inline NodesIterator GraphEnd(CompilationGraph* G) {
239     return NodesIterator(G->NodesMap.end());
240   }
241
242
243   /// NodeChildIterator - Another auxiliary class needed by GraphTraits.
244   class NodeChildIterator : public std::iterator<std::bidirectional_iterator_tag, Node, ptrdiff_t> {
245     typedef NodeChildIterator ThisType;
246     typedef Node::container_type::iterator iterator;
247
248     CompilationGraph* OwningGraph;
249     iterator EdgeIter;
250   public:
251     typedef Node* pointer;
252     typedef Node& reference;
253
254     NodeChildIterator(Node* N, iterator I) :
255       OwningGraph(N->OwningGraph), EdgeIter(I) {}
256
257     const ThisType& operator=(const ThisType& I) {
258       assert(OwningGraph == I.OwningGraph);
259       EdgeIter = I.EdgeIter;
260       return *this;
261     }
262
263     inline bool operator==(const ThisType& I) const {
264       assert(OwningGraph == I.OwningGraph);
265       return EdgeIter == I.EdgeIter;
266     }
267     inline bool operator!=(const ThisType& I) const {
268       return !this->operator==(I);
269     }
270
271     inline pointer operator*() const {
272       return &OwningGraph->getNode((*EdgeIter)->ToolName());
273     }
274     inline pointer operator->() const {
275       return this->operator*();
276     }
277
278     ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
279     ThisType operator++(int) { // Postincrement
280       ThisType tmp = *this;
281       ++*this;
282       return tmp;
283     }
284
285     inline ThisType& operator--() { --EdgeIter; return *this; }  // Predecrement
286     inline ThisType operator--(int) { // Postdecrement
287       ThisType tmp = *this;
288       --*this;
289       return tmp;
290     }
291
292   };
293 }
294
295 namespace llvm {
296   template <>
297   struct GraphTraits<llvmc::CompilationGraph*> {
298     typedef llvmc::CompilationGraph GraphType;
299     typedef llvmc::Node NodeType;
300     typedef llvmc::NodeChildIterator ChildIteratorType;
301
302     static NodeType* getEntryNode(GraphType* G) {
303       return &G->getNode("root");
304     }
305
306     static ChildIteratorType child_begin(NodeType* N) {
307       return ChildIteratorType(N, N->OutEdges.begin());
308     }
309     static ChildIteratorType child_end(NodeType* N) {
310       return ChildIteratorType(N, N->OutEdges.end());
311     }
312
313     typedef llvmc::NodesIterator nodes_iterator;
314     static nodes_iterator nodes_begin(GraphType *G) {
315       return GraphBegin(G);
316     }
317     static nodes_iterator nodes_end(GraphType *G) {
318       return GraphEnd(G);
319     }
320   };
321
322 }
323
324 #endif // LLVM_INCLUDE_COMPILER_DRIVER_COMPILATION_GRAPH_H