Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 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 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/GraphTraits.h" |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/iterator" |
| 23 | #include "llvm/ADT/SmallVector.h" |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringMap.h" |
| 25 | #include "llvm/System/Path.h" |
| 26 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 27 | #include <string> |
| 28 | |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 29 | namespace llvmcc { |
| 30 | |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 31 | // An edge in the graph. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 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 bool isEnabled() const = 0; |
| 39 | virtual bool isDefault() const = 0; |
| 40 | private: |
| 41 | std::string ToolName_; |
| 42 | }; |
| 43 | |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 44 | // Edges with no properties are instances of this class. |
| 45 | class SimpleEdge : public Edge { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 46 | public: |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 47 | SimpleEdge(const std::string& T) : Edge(T) {} |
Mikhail Glushenkov | 6591c89 | 2008-05-06 17:23:50 +0000 | [diff] [blame] | 48 | bool isEnabled() const { return false;} |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 49 | bool isDefault() const { return true;} |
| 50 | }; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 51 | |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 52 | // A node in the graph. |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 53 | struct Node { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 54 | typedef llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> container_type; |
| 55 | typedef container_type::iterator iterator; |
| 56 | typedef container_type::const_iterator const_iterator; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 57 | |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 58 | Node() : OwningGraph(0), InEdges(0) {} |
| 59 | Node(CompilationGraph* G) : OwningGraph(G), InEdges(0) {} |
| 60 | Node(CompilationGraph* G, Tool* T) : |
| 61 | OwningGraph(G), ToolPtr(T), InEdges(0) {} |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 62 | |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 63 | bool HasChildren() const { return !OutEdges.empty(); } |
Mikhail Glushenkov | 6591c89 | 2008-05-06 17:23:50 +0000 | [diff] [blame] | 64 | const std::string Name() const { return ToolPtr->Name(); } |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 65 | |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 66 | // Iteration. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 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 | |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 72 | // Add an outward edge. Takes ownership of the Edge object. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 73 | void AddEdge(Edge* E) |
| 74 | { OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(E)); } |
| 75 | |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 76 | // Inward edge counter. Used by Build() to implement topological |
| 77 | // sort. |
| 78 | void IncrInEdges() { ++InEdges; } |
| 79 | void DecrInEdges() { --InEdges; } |
| 80 | bool HasNoInEdges() const { return InEdges == 0; } |
| 81 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 82 | // Needed to implement NodeChildIterator/GraphTraits |
| 83 | CompilationGraph* OwningGraph; |
| 84 | // The corresponding Tool. |
| 85 | llvm::IntrusiveRefCntPtr<Tool> ToolPtr; |
| 86 | // Links to children. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 87 | container_type OutEdges; |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 88 | // Number of parents. |
| 89 | unsigned InEdges; |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 90 | }; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 91 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 92 | class NodesIterator; |
| 93 | |
| 94 | class CompilationGraph { |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame^] | 95 | // Main data structure. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 96 | typedef llvm::StringMap<Node> nodes_map_type; |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame^] | 97 | // These are used to map from language names-> tools. (We can have |
| 98 | // several tools associated with each language name.) |
| 99 | typedef |
| 100 | llvm::SmallVector<llvm::IntrusiveRefCntPtr<Edge>, 3> tools_vector_type; |
| 101 | typedef llvm::StringMap<tools_vector_type> tools_map_type; |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 102 | |
| 103 | // Map from file extensions to language names. |
| 104 | LanguageMap ExtsToLangs; |
| 105 | // Map from language names to lists of tool names. |
| 106 | tools_map_type ToolsMap; |
| 107 | // Map from tool names to Tool objects. |
| 108 | nodes_map_type NodesMap; |
| 109 | |
| 110 | public: |
| 111 | |
| 112 | CompilationGraph(); |
| 113 | |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 114 | // insertVertex - insert a new node into the graph. Takes |
| 115 | // ownership of the object. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 116 | void insertNode(Tool* T); |
| 117 | |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 118 | // insertEdge - Insert a new edge into the graph. Takes ownership |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 119 | // of the Edge object. |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 120 | void insertEdge(const std::string& A, Edge* E); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 121 | |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 122 | // Build - Build target(s) from the input file set. Command-line |
| 123 | // options are passed implicitly as global variables. |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 124 | int Build(llvm::sys::Path const& tempDir) const; |
| 125 | |
| 126 | // Return a reference to the node correponding to the given tool |
| 127 | // name. Throws std::runtime_error. |
| 128 | Node& getNode(const std::string& ToolName); |
| 129 | const Node& getNode(const std::string& ToolName) const; |
| 130 | |
| 131 | // viewGraph - This function is meant for use from the debugger. |
| 132 | // You can just say 'call G->viewGraph()' and a ghostview window |
| 133 | // should pop up from the program, displaying the compilation |
| 134 | // graph. This depends on there being a 'dot' and 'gv' program |
| 135 | // in your path. |
| 136 | void viewGraph(); |
| 137 | |
| 138 | // Write a CompilationGraph.dot file. |
| 139 | void writeGraph(); |
| 140 | |
| 141 | // GraphTraits support |
| 142 | friend NodesIterator GraphBegin(CompilationGraph*); |
| 143 | friend NodesIterator GraphEnd(CompilationGraph*); |
| 144 | friend void PopulateCompilationGraph(CompilationGraph&); |
| 145 | |
| 146 | private: |
| 147 | // Helper functions. |
| 148 | |
| 149 | // Find out which language corresponds to the suffix of this file. |
| 150 | const std::string& getLanguage(const llvm::sys::Path& File) const; |
| 151 | |
| 152 | // Return a reference to the list of tool names corresponding to |
| 153 | // the given language name. Throws std::runtime_error. |
| 154 | const tools_vector_type& getToolsVector(const std::string& LangName) const; |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 155 | |
| 156 | // Pass the input file through the toolchain. |
Mikhail Glushenkov | bbbc9d4 | 2008-05-06 17:27:37 +0000 | [diff] [blame] | 157 | const JoinTool* PassThroughGraph (llvm::sys::Path& In, |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame^] | 158 | const Node* StartNode, |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 159 | const llvm::sys::Path& TempDir) const; |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 160 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | /// GraphTraits support code. |
| 164 | |
| 165 | // Auxiliary class needed to implement GraphTraits support. Can be |
| 166 | // generalised to something like value_iterator for map-like |
| 167 | // containers. |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 168 | class NodesIterator : public llvm::StringMap<Node>::iterator { |
| 169 | typedef llvm::StringMap<Node>::iterator super; |
| 170 | typedef NodesIterator ThisType; |
| 171 | typedef Node* pointer; |
| 172 | typedef Node& reference; |
| 173 | |
| 174 | public: |
| 175 | NodesIterator(super I) : super(I) {} |
| 176 | |
| 177 | inline reference operator*() const { |
| 178 | return super::operator->()->second; |
| 179 | } |
| 180 | inline pointer operator->() const { |
| 181 | return &super::operator->()->second; |
| 182 | } |
| 183 | }; |
| 184 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 185 | inline NodesIterator GraphBegin(CompilationGraph* G) { |
| 186 | return NodesIterator(G->NodesMap.begin()); |
| 187 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 188 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 189 | inline NodesIterator GraphEnd(CompilationGraph* G) { |
| 190 | return NodesIterator(G->NodesMap.end()); |
| 191 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 192 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 193 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 194 | // Another auxiliary class needed by GraphTraits. |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 195 | class NodeChildIterator : public bidirectional_iterator<Node, ptrdiff_t> { |
| 196 | typedef NodeChildIterator ThisType; |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 197 | typedef Node::container_type::iterator iterator; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 198 | |
| 199 | CompilationGraph* OwningGraph; |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 200 | iterator EdgeIter; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 201 | public: |
| 202 | typedef Node* pointer; |
| 203 | typedef Node& reference; |
| 204 | |
| 205 | NodeChildIterator(Node* N, iterator I) : |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 206 | OwningGraph(N->OwningGraph), EdgeIter(I) {} |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 207 | |
| 208 | const ThisType& operator=(const ThisType& I) { |
| 209 | assert(OwningGraph == I.OwningGraph); |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 210 | EdgeIter = I.EdgeIter; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 211 | return *this; |
| 212 | } |
| 213 | |
| 214 | inline bool operator==(const ThisType& I) const |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 215 | { return EdgeIter == I.EdgeIter; } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 216 | inline bool operator!=(const ThisType& I) const |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 217 | { return EdgeIter != I.EdgeIter; } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 218 | |
| 219 | inline pointer operator*() const { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 220 | return &OwningGraph->getNode((*EdgeIter)->ToolName()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 221 | } |
| 222 | inline pointer operator->() const { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 223 | return &OwningGraph->getNode((*EdgeIter)->ToolName()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 226 | ThisType& operator++() { ++EdgeIter; return *this; } // Preincrement |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 227 | ThisType operator++(int) { // Postincrement |
| 228 | ThisType tmp = *this; |
| 229 | ++*this; |
| 230 | return tmp; |
| 231 | } |
| 232 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 233 | inline ThisType& operator--() { --EdgeIter; return *this; } // Predecrement |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 234 | inline ThisType operator--(int) { // Postdecrement |
| 235 | ThisType tmp = *this; |
| 236 | --*this; |
| 237 | return tmp; |
| 238 | } |
| 239 | |
| 240 | }; |
| 241 | } |
| 242 | |
| 243 | namespace llvm { |
| 244 | template <> |
| 245 | struct GraphTraits<llvmcc::CompilationGraph*> { |
| 246 | typedef llvmcc::CompilationGraph GraphType; |
| 247 | typedef llvmcc::Node NodeType; |
| 248 | typedef llvmcc::NodeChildIterator ChildIteratorType; |
| 249 | |
| 250 | static NodeType* getEntryNode(GraphType* G) { |
| 251 | return &G->getNode("root"); |
| 252 | } |
| 253 | |
| 254 | static ChildIteratorType child_begin(NodeType* N) { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 255 | return ChildIteratorType(N, N->OutEdges.begin()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 256 | } |
| 257 | static ChildIteratorType child_end(NodeType* N) { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 258 | return ChildIteratorType(N, N->OutEdges.end()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 261 | typedef llvmcc::NodesIterator nodes_iterator; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 262 | static nodes_iterator nodes_begin(GraphType *G) { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 263 | return GraphBegin(G); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 264 | } |
| 265 | static nodes_iterator nodes_end(GraphType *G) { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 266 | return GraphEnd(G); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 267 | } |
| 268 | }; |
| 269 | |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | #endif // LLVM_TOOLS_LLVMC2_COMPILATION_GRAPH_H |