blob: 18c1e6b3b5fb33cce93852548ee629f161370bd1 [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 Glushenkov6591c892008-05-06 17:23:50 +000048 bool isEnabled() const { return false;}
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000049 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 Glushenkov6591c892008-05-06 17:23:50 +000063 const std::string Name() const { return ToolPtr->Name(); }
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000064
65 iterator EdgesBegin() { return OutEdges.begin(); }
66 const_iterator EdgesBegin() const { return OutEdges.begin(); }
67 iterator EdgesEnd() { return OutEdges.end(); }
68 const_iterator EdgesEnd() const { return OutEdges.end(); }
69
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000070 // Choose one of the edges based on command-line options.
71 const Edge* ChooseEdge() const;
72
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000073 // Takes ownership of the object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000074 void AddEdge(Edge* E)
75 { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); }
76
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000077 // Needed to implement NodeChildIterator/GraphTraits
78 CompilationGraph* OwningGraph;
79 // The corresponding Tool.
80 llvm::IntrusiveRefCntPtr<Tool> ToolPtr;
81 // Links to children.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000082 container_type OutEdges;
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000083 };
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000084
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000085 class NodesIterator;
86
87 class CompilationGraph {
88 typedef llvm::SmallVector<std::string, 3> tools_vector_type;
89 typedef llvm::StringMap<tools_vector_type> tools_map_type;
90 typedef llvm::StringMap<Node> nodes_map_type;
91
92 // Map from file extensions to language names.
93 LanguageMap ExtsToLangs;
94 // Map from language names to lists of tool names.
95 tools_map_type ToolsMap;
96 // Map from tool names to Tool objects.
97 nodes_map_type NodesMap;
98
99 public:
100
101 CompilationGraph();
102
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000103 // insertVertex - insert a new node into the graph. Takes
104 // ownership of the object.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000105 void insertNode(Tool* T);
106
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000107 // insertEdge - Insert a new edge into the graph. Takes ownership
108 // of the object.
109 void insertEdge(const std::string& A, Edge* E);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000110
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000111 // Build - Build target(s) from the input file set. Command-line
112 // options are passed implicitly as global variables.
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000113 int Build(llvm::sys::Path const& tempDir) const;
114
115 // Return a reference to the node correponding to the given tool
116 // name. Throws std::runtime_error.
117 Node& getNode(const std::string& ToolName);
118 const Node& getNode(const std::string& ToolName) const;
119
120 // viewGraph - This function is meant for use from the debugger.
121 // You can just say 'call G->viewGraph()' and a ghostview window
122 // should pop up from the program, displaying the compilation
123 // graph. This depends on there being a 'dot' and 'gv' program
124 // in your path.
125 void viewGraph();
126
127 // Write a CompilationGraph.dot file.
128 void writeGraph();
129
130 // GraphTraits support
131 friend NodesIterator GraphBegin(CompilationGraph*);
132 friend NodesIterator GraphEnd(CompilationGraph*);
133 friend void PopulateCompilationGraph(CompilationGraph&);
134
135 private:
136 // Helper functions.
137
138 // Find out which language corresponds to the suffix of this file.
139 const std::string& getLanguage(const llvm::sys::Path& File) const;
140
141 // Return a reference to the list of tool names corresponding to
142 // the given language name. Throws std::runtime_error.
143 const tools_vector_type& getToolsVector(const std::string& LangName) const;
144 };
145
146 /// GraphTraits support code.
147
148 // Auxiliary class needed to implement GraphTraits support. Can be
149 // generalised to something like value_iterator for map-like
150 // containers.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000151 class NodesIterator : public llvm::StringMap<Node>::iterator {
152 typedef llvm::StringMap<Node>::iterator super;
153 typedef NodesIterator ThisType;
154 typedef Node* pointer;
155 typedef Node& reference;
156
157 public:
158 NodesIterator(super I) : super(I) {}
159
160 inline reference operator*() const {
161 return super::operator->()->second;
162 }
163 inline pointer operator->() const {
164 return &super::operator->()->second;
165 }
166 };
167
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000168 inline NodesIterator GraphBegin(CompilationGraph* G) {
169 return NodesIterator(G->NodesMap.begin());
170 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000171
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000172 inline NodesIterator GraphEnd(CompilationGraph* G) {
173 return NodesIterator(G->NodesMap.end());
174 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000175
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000176
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000177 // Another auxiliary class needed by GraphTraits.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000178 class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> {
179 typedef NodeChildIterator ThisType;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000180 typedef Node::container_type::iterator iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000181
182 CompilationGraph* OwningGraph;
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000183 iterator EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000184 public:
185 typedef Node* pointer;
186 typedef Node& reference;
187
188 NodeChildIterator(Node* N, iterator I) :
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000189 OwningGraph(N->OwningGraph), EdgeIter(I) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000190
191 const ThisType& operator=(const ThisType& I) {
192 assert(OwningGraph == I.OwningGraph);
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000193 EdgeIter = I.EdgeIter;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000194 return *this;
195 }
196
197 inline bool operator==(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000198 { return EdgeIter == I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000199 inline bool operator!=(const ThisType& I) const
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000200 { return EdgeIter != I.EdgeIter; }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000201
202 inline pointer operator*() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000203 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000204 }
205 inline pointer operator->() const {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000206 return &OwningGraph->getNode((*EdgeIter)->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000207 }
208
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000209 ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000210 ThisType operator++(int) { // Postincrement
211 ThisType tmp = *this;
212 ++*this;
213 return tmp;
214 }
215
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000216 inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000217 inline ThisType operator--(int) { // Postdecrement
218 ThisType tmp = *this;
219 --*this;
220 return tmp;
221 }
222
223 };
224}
225
226namespace llvm {
227 template <>
228 struct GraphTraits<llvmcc::CompilationGraph*> {
229 typedef llvmcc::CompilationGraph GraphType;
230 typedef llvmcc::Node NodeType;
231 typedef llvmcc::NodeChildIterator ChildIteratorType;
232
233 static NodeType* getEntryNode(GraphType* G) {
234 return &G->getNode("root");
235 }
236
237 static ChildIteratorType child_begin(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000238 return ChildIteratorType(N, N->OutEdges.begin());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000239 }
240 static ChildIteratorType child_end(NodeType* N) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000241 return ChildIteratorType(N, N->OutEdges.end());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000242 }
243
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000244 typedef llvmcc::NodesIterator nodes_iterator;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000245 static nodes_iterator nodes_begin(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000246 return GraphBegin(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000247 }
248 static nodes_iterator nodes_end(GraphType *G) {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000249 return GraphEnd(G);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000250 }
251 };
252
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +0000253}
254
255#endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H