blob: d953aeea9003cf891eabd865676dab16a92afd45 [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 Glushenkov0a174932008-05-06 16:36:06 +000048 bool isEnabled() const { return true;}
49 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 Glushenkov0d08db02008-05-06 16:35:25 +000058 Node() {}
59 Node(CompilationGraph* G) : OwningGraph(G) {}
60 Node(CompilationGraph* G, Tool* T) : OwningGraph(G), ToolPtr(T) {}
61
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000062 bool HasChildren() const { return !OutEdges.empty(); }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000063
64 iterator EdgesBegin() { return OutEdges.begin(); }
65 const_iterator EdgesBegin() const { return OutEdges.begin(); }
66 iterator EdgesEnd() { return OutEdges.end(); }
67 const_iterator EdgesEnd() const { return OutEdges.end(); }
68
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000069 // Takes ownership of the object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000070 void AddEdge(Edge* E)
71 { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
72
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000073 // Needed to implement NodeChildIterator/GraphTraits
74 CompilationGraph* OwningGraph;
75 // The corresponding Tool.
76 llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
77 // Links to children.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000078 container_type OutEdges;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000079 };
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000080
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000081 class NodesIterator;
82
83 class CompilationGraph {
84 typedef llvm::SmallVector<std::string, 3> tools_vector_type;
85 typedef llvm::StringMap<tools_vector_type> tools_map_type;
86 typedef llvm::StringMap<Node> nodes_map_type;
87
88 // Map from file extensions to language names.
89 LanguageMap ExtsToLangs;
90 // Map from language names to lists of tool names.
91 tools_map_type ToolsMap;
92 // Map from tool names to Tool objects.
93 nodes_map_type NodesMap;
94
95 public:
96
97 CompilationGraph();
98
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000099 // insertVertex - insert a new node into the graph. Takes
100 // ownership of the object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000101 void insertNode(Tool* T);
102
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000103 // insertEdge - Insert a new edge into the graph. Takes ownership
104 // of the object.
105 void insertEdge(const std::string& A, Edge* E);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000106
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000107 // Build - Build target(s) from the input file set. Command-line
108 // options are passed implicitly as global variables.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000109 int Build(llvm::sys::Path const& tempDir) const;
110
111 // Return a reference to the node correponding to the given tool
112 // name. Throws std::runtime_error.
113 Node& getNode(const std::string& ToolName);
114 const Node& getNode(const std::string& ToolName) const;
115
116 // viewGraph - This function is meant for use from the debugger.
117 // You can just say 'call G->viewGraph()' and a ghostview window
118 // should pop up from the program, displaying the compilation
119 // graph. This depends on there being a 'dot' and 'gv' program
120 // in your path.
121 void viewGraph();
122
123 // Write a CompilationGraph.dot file.
124 void writeGraph();
125
126 // GraphTraits support
127 friend NodesIterator GraphBegin(CompilationGraph*);
128 friend NodesIterator GraphEnd(CompilationGraph*);
129 friend void PopulateCompilationGraph(CompilationGraph&);
130
131 private:
132 // Helper functions.
133
134 // Find out which language corresponds to the suffix of this file.
135 const std::string& getLanguage(const llvm::sys::Path& File) const;
136
137 // Return a reference to the list of tool names corresponding to
138 // the given language name. Throws std::runtime_error.
139 const tools_vector_type& getToolsVector(const std::string& LangName) const;
140 };
141
142 /// GraphTraits support code.
143
144 // Auxiliary class needed to implement GraphTraits support. Can be
145 // generalised to something like value_iterator for map-like
146 // containers.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000147 class NodesIterator : public llvm::StringMap<Node>::iterator {
148 typedef llvm::StringMap<Node>::iterator super;
149 typedef NodesIterator ThisType;
150 typedef Node* pointer;
151 typedef Node& reference;
152
153 public:
154 NodesIterator(super I) : super(I) {}
155
156 inline reference operator*() const {
157 return super::operator->()->second;
158 }
159 inline pointer operator->() const {
160 return &super::operator->()->second;
161 }
162 };
163
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000164 inline NodesIterator GraphBegin(CompilationGraph* G) {
165 return NodesIterator(G->NodesMap.begin());
166 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000167
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000168 inline NodesIterator GraphEnd(CompilationGraph* G) {
169 return NodesIterator(G->NodesMap.end());
170 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000171
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000172
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000173 // Another auxiliary class needed by GraphTraits.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000174 class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
175 typedef NodeChildIterator ThisType;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000176 typedef Node::container_type::iterator iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000177
178 CompilationGraph* OwningGraph;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000179 iterator EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000180 public:
181 typedef Node* pointer;
182 typedef Node& reference;
183
184 NodeChildIterator(Node* N, iterator I) :
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000185 OwningGraph(N->OwningGraph), EdgeIter(I) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000186
187 const ThisType& operator=(const ThisType& I) {
188 assert(OwningGraph == I.OwningGraph);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000189 EdgeIter = I.EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000190 return *this;
191 }
192
193 inline bool operator==(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000194 { return EdgeIter == I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000195 inline bool operator!=(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000196 { return EdgeIter != I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000197
198 inline pointer operator*() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000199 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000200 }
201 inline pointer operator->() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000202 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000203 }
204
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000205 ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000206 ThisType operator++(int) { // Postincrement
207 ThisType tmp = *this;
208 ++*this;
209 return tmp;
210 }
211
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000212 inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000213 inline ThisType operator--(int) { // Postdecrement
214 ThisType tmp = *this;
215 --*this;
216 return tmp;
217 }
218
219 };
220}
221
222namespace llvm {
223 template <>
224 struct GraphTraits<llvmcc::CompilationGraph*> {
225 typedef llvmcc::CompilationGraph GraphType;
226 typedef llvmcc::Node NodeType;
227 typedef llvmcc::NodeChildIterator ChildIteratorType;
228
229 static NodeType* getEntryNode(GraphType* G) {
230 return &G->getNode("root");
231 }
232
233 static ChildIteratorType child_begin(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000234 return ChildIteratorType(N, N->OutEdges.begin());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000235 }
236 static ChildIteratorType child_end(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000237 return ChildIteratorType(N, N->OutEdges.end());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000238 }
239
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000240 typedef llvmcc::NodesIterator nodes_iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000241 static nodes_iterator nodes_begin(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000242 return GraphBegin(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000243 }
244 static nodes_iterator nodes_end(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000245 return GraphEnd(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000246 }
247 };
248
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000249}
250
251#endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H