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