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