Add methods to StringMap to erase entries by key.
[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     void IncrInEdges() { ++InEdges; }
83     void DecrInEdges() { --InEdges; }
84     bool HasNoInEdges() const { return InEdges == 0; }
85
86     // Needed to implement NodeChildIterator/GraphTraits
87     CompilationGraph* OwningGraph;
88     // The corresponding Tool.
89     // WARNING: ToolPtr can be NULL (for the root node).
90     llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
91     // Links to children.
92     container_type OutEdges;
93     // Inward edge counter. Updated in
94     // CompilationGraph::insertEdge(). Used for topological sorting.
95     unsigned InEdges;
96   };
97
98   class NodesIterator;
99
100   /// CompilationGraph - The compilation graph itself.
101   class CompilationGraph {
102     /// nodes_map_type - The main data structure.
103     typedef llvm::StringMap<Node> nodes_map_type;
104     /// tools_vector_type, tools_map_type - Data structures used to
105     /// map from language names to tools. (We can have several tools
106     /// associated with each language name, hence the need for a
107     /// vector.)
108     typedef
109     llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type;
110     typedef llvm::StringMap<tools_vector_type> tools_map_type;
111
112     /// ToolsMap - Map from language names to lists of tool names.
113     tools_map_type ToolsMap;
114     /// NodesMap - Map from tool names to Tool objects.
115     nodes_map_type NodesMap;
116
117   public:
118
119     CompilationGraph();
120
121     /// insertNode - Insert a new node into the graph. Takes
122     /// ownership of the object.
123     void insertNode(Tool* T);
124
125     /// insertEdge - Insert a new edge into the graph. Takes ownership
126     /// of the Edge object.
127     void insertEdge(const std::string& A, Edge* E);
128
129     /// Build - Build target(s) from the input file set. Command-line
130     /// options are passed implicitly as global variables.
131     int Build(llvm::sys::Path const& tempDir);
132
133     /// getNode - Return a reference to the node correponding to the
134     /// given tool name. Throws std::runtime_error.
135     Node& getNode(const std::string& ToolName);
136     const Node& getNode(const std::string& ToolName) const;
137
138     /// viewGraph - This function is meant for use from the debugger.
139     /// You can just say 'call G->viewGraph()' and a ghostview window
140     /// should pop up from the program, displaying the compilation
141     /// graph. This depends on there being a 'dot' and 'gv' program
142     /// in your path.
143     void viewGraph();
144
145     /// writeGraph - Write a compilation-graph.dot file.
146     void writeGraph();
147
148     // GraphTraits support.
149     friend NodesIterator GraphBegin(CompilationGraph*);
150     friend NodesIterator GraphEnd(CompilationGraph*);
151
152   private:
153     // Helper functions.
154
155     /// getToolsVector - Return a reference to the list of tool names
156     /// corresponding to the given language name. Throws
157     /// std::runtime_error.
158     const tools_vector_type& getToolsVector(const std::string& LangName) const;
159
160     /// PassThroughGraph - Pass the input file through the toolchain
161     /// starting at StartNode.
162     void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
163                            const InputLanguagesSet& InLangs,
164                            const llvm::sys::Path& TempDir) const;
165
166     /// FindToolChain - Find head of the toolchain corresponding to the given file.
167     const Node* FindToolChain(const llvm::sys::Path& In,
168                               const std::string* forceLanguage,
169                               InputLanguagesSet& InLangs) const;
170
171     /// BuildInitial - Traverse the initial parts of the toolchains.
172     void BuildInitial(InputLanguagesSet& InLangs,
173                       const llvm::sys::Path& TempDir);
174
175     /// TopologicalSort - Sort the nodes in topological order.
176     void TopologicalSort(std::vector<const Node*>& Out);
177     /// TopologicalSortFilterJoinNodes - Call TopologicalSort and
178     /// filter the resulting list to include only Join nodes.
179     void TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out);
180   };
181
182   // GraphTraits support code.
183
184   /// NodesIterator - Auxiliary class needed to implement GraphTraits
185   /// support. Can be generalised to something like value_iterator
186   /// for map-like containers.
187   class NodesIterator : public llvm::StringMap<Node>::iterator {
188     typedef llvm::StringMap<Node>::iterator super;
189     typedef NodesIterator ThisType;
190     typedef Node* pointer;
191     typedef Node& reference;
192
193   public:
194     NodesIterator(super I) : super(I) {}
195
196     inline reference operator*() const {
197       return super::operator->()->second;
198     }
199     inline pointer operator->() const {
200       return &super::operator->()->second;
201     }
202   };
203
204   inline NodesIterator GraphBegin(CompilationGraph* G) {
205     return NodesIterator(G->NodesMap.begin());
206   }
207
208   inline NodesIterator GraphEnd(CompilationGraph* G) {
209     return NodesIterator(G->NodesMap.end());
210   }
211
212
213   /// NodeChildIterator - Another auxiliary class needed by GraphTraits.
214   class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
215     typedef NodeChildIterator ThisType;
216     typedef Node::container_type::iterator iterator;
217
218     CompilationGraph* OwningGraph;
219     iterator EdgeIter;
220   public:
221     typedef Node* pointer;
222     typedef Node& reference;
223
224     NodeChildIterator(Node* N, iterator I) :
225       OwningGraph(N->OwningGraph), EdgeIter(I) {}
226
227     const ThisType& operator=(const ThisType& I) {
228       assert(OwningGraph == I.OwningGraph);
229       EdgeIter = I.EdgeIter;
230       return *this;
231     }
232
233     inline bool operator==(const ThisType& I) const
234     { return EdgeIter == I.EdgeIter; }
235     inline bool operator!=(const ThisType& I) const
236     { return EdgeIter != I.EdgeIter; }
237
238     inline pointer operator*() const {
239       return &OwningGraph->getNode((*EdgeIter)->ToolName());
240     }
241     inline pointer operator->() const {
242       return &OwningGraph->getNode((*EdgeIter)->ToolName());
243     }
244
245     ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
246     ThisType operator++(int) { // Postincrement
247       ThisType tmp = *this;
248       ++*this;
249       return tmp;
250     }
251
252     inline ThisType& operator--() { --EdgeIter; return *this; }  // Predecrement
253     inline ThisType operator--(int) { // Postdecrement
254       ThisType tmp = *this;
255       --*this;
256       return tmp;
257     }
258
259   };
260 }
261
262 namespace llvm {
263   template <>
264   struct GraphTraits<llvmc::CompilationGraph*> {
265     typedef llvmc::CompilationGraph GraphType;
266     typedef llvmc::Node NodeType;
267     typedef llvmc::NodeChildIterator ChildIteratorType;
268
269     static NodeType* getEntryNode(GraphType* G) {
270       return &G->getNode("root");
271     }
272
273     static ChildIteratorType child_begin(NodeType* N) {
274       return ChildIteratorType(N, N->OutEdges.begin());
275     }
276     static ChildIteratorType child_end(NodeType* N) {
277       return ChildIteratorType(N, N->OutEdges.end());
278     }
279
280     typedef llvmc::NodesIterator nodes_iterator;
281     static nodes_iterator nodes_begin(GraphType *G) {
282       return GraphBegin(G);
283     }
284     static nodes_iterator nodes_end(GraphType *G) {
285       return GraphEnd(G);
286     }
287   };
288
289 }
290
291 #endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H