Add weights to graph edges. Choose between edges based on their weight.
[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"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/System/Path.h"
26
27 #include <string>
28
29 namespace llvmc {
30
31   // An edge of the compilation graph.
32   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 unsigned Weight() const = 0;
39   private:
40     std::string ToolName_;
41   };
42
43   // Edges that have no properties are instances of this class.
44   class SimpleEdge : public Edge {
45   public:
46     SimpleEdge(const std::string& T) : Edge(T) {}
47     unsigned Weight() const { return 1; }
48   };
49
50   // A node of the compilation graph.
51   struct Node {
52     // A Node holds a list of the outward edges.
53     typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type;
54     typedef container_type::iterator iterator;
55     typedef container_type::const_iterator const_iterator;
56
57     Node() : OwningGraph(0), InEdges(0) {}
58     Node(CompilationGraph* G) : OwningGraph(G), InEdges(0) {}
59     Node(CompilationGraph* G, Tool* T) :
60       OwningGraph(G), ToolPtr(T), InEdges(0) {}
61
62     bool HasChildren() const { return !OutEdges.empty(); }
63     const std::string Name() const
64     { return ToolPtr ? ToolPtr->Name() : "root"; }
65
66     // Iteration.
67     iterator EdgesBegin() { return OutEdges.begin(); }
68     const_iterator EdgesBegin() const { return OutEdges.begin(); }
69     iterator EdgesEnd() { return OutEdges.end(); }
70     const_iterator EdgesEnd() const { return OutEdges.end(); }
71
72     // Add an outward edge. Takes ownership of the Edge object.
73     void AddEdge(Edge* E)
74     { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
75
76     // Inward edge counter. Used to implement topological sort.
77     // TOTHINK: Move the mutable counter back into Tool classes? Makes
78     // us more const-correct.
79     void IncrInEdges() { ++InEdges; }
80     void DecrInEdges() { --InEdges; }
81     bool HasNoInEdges() const { return InEdges == 0; }
82
83     // Needed to implement NodeChildIterator/GraphTraits
84     CompilationGraph* OwningGraph;
85     // The corresponding Tool.
86     // WARNING: ToolPtr can be NULL (for the root node).
87     llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
88     // Links to children.
89     container_type OutEdges;
90     // Inward edge counter. Updated in
91     // CompilationGraph::insertEdge(). Used for topological sorting.
92     unsigned InEdges;
93   };
94
95   class NodesIterator;
96
97   // The compilation graph itself.
98   class CompilationGraph {
99     // Main data structure.
100     typedef llvm::StringMap<Node> nodes_map_type;
101     // These are used to map from language names to tools. (We can
102     // have several tools associated with each language name, hence
103     // the need for a vector of Edges.)
104     typedef
105     llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type;
106     typedef llvm::StringMap<tools_vector_type> tools_map_type;
107
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
119     // insertVertex - insert a new node into the graph. Takes
120     // ownership of the object.
121     void insertNode(Tool* T);
122
123     // insertEdge - Insert a new edge into the graph. Takes ownership
124     // of the Edge object.
125     void insertEdge(const std::string& A, Edge* E);
126
127     // Build - Build target(s) from the input file set. Command-line
128     // options are passed implicitly as global variables.
129     int Build(llvm::sys::Path const& tempDir);
130
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;
160
161     // Pass the input file through the toolchain.
162     void PassThroughGraph (const llvm::sys::Path& In, const Node* StartNode,
163                            const llvm::sys::Path& TempDir) const;
164
165     // Find head of the toolchain corresponding to the given file.
166     const Node* FindToolChain(const llvm::sys::Path& In,
167                               const std::string* forceLanguage) 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);
174   };
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.
181   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
198   inline NodesIterator GraphBegin(CompilationGraph* G) {
199     return NodesIterator(G->NodesMap.begin());
200   }
201
202   inline NodesIterator GraphEnd(CompilationGraph* G) {
203     return NodesIterator(G->NodesMap.end());
204   }
205
206
207   // Another auxiliary class needed by GraphTraits.
208   class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
209     typedef NodeChildIterator ThisType;
210     typedef Node::container_type::iterator iterator;
211
212     CompilationGraph* OwningGraph;
213     iterator EdgeIter;
214   public:
215     typedef Node* pointer;
216     typedef Node& reference;
217
218     NodeChildIterator(Node* N, iterator I) :
219       OwningGraph(N->OwningGraph), EdgeIter(I) {}
220
221     const ThisType& operator=(const ThisType& I) {
222       assert(OwningGraph == I.OwningGraph);
223       EdgeIter = I.EdgeIter;
224       return *this;
225     }
226
227     inline bool operator==(const ThisType& I) const
228     { return EdgeIter == I.EdgeIter; }
229     inline bool operator!=(const ThisType& I) const
230     { return EdgeIter != I.EdgeIter; }
231
232     inline pointer operator*() const {
233       return &OwningGraph->getNode((*EdgeIter)->ToolName());
234     }
235     inline pointer operator->() const {
236       return &OwningGraph->getNode((*EdgeIter)->ToolName());
237     }
238
239     ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
240     ThisType operator++(int) { // Postincrement
241       ThisType tmp = *this;
242       ++*this;
243       return tmp;
244     }
245
246     inline ThisType& operator--() { --EdgeIter; return *this; }  // Predecrement
247     inline ThisType operator--(int) { // Postdecrement
248       ThisType tmp = *this;
249       --*this;
250       return tmp;
251     }
252
253   };
254 }
255
256 namespace llvm {
257   template <>
258   struct GraphTraits<llvmc::CompilationGraph*> {
259     typedef llvmc::CompilationGraph GraphType;
260     typedef llvmc::Node NodeType;
261     typedef llvmc::NodeChildIterator ChildIteratorType;
262
263     static NodeType* getEntryNode(GraphType* G) {
264       return &G->getNode("root");
265     }
266
267     static ChildIteratorType child_begin(NodeType* N) {
268       return ChildIteratorType(N, N->OutEdges.begin());
269     }
270     static ChildIteratorType child_end(NodeType* N) {
271       return ChildIteratorType(N, N->OutEdges.end());
272     }
273
274     typedef llvmc::NodesIterator nodes_iterator;
275     static nodes_iterator nodes_begin(GraphType *G) {
276       return GraphBegin(G);
277     }
278     static nodes_iterator nodes_end(GraphType *G) {
279       return GraphEnd(G);
280     }
281   };
282
283 }
284
285 #endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H