Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 1 | //===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===// |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 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 | // |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 10 | // Compilation graph - implementation. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 14 | #include "CompilationGraph.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 15 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CommandLine.h" |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 17 | #include "llvm/Support/DOTGraphTraits.h" |
| 18 | #include "llvm/Support/GraphWriter.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 19 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame^] | 20 | #include <algorithm> |
| 21 | #include <iostream> |
| 22 | #include <iterator> |
| 23 | #include <queue> |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 24 | #include <stdexcept> |
| 25 | |
| 26 | using namespace llvm; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 27 | using namespace llvmcc; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 28 | |
| 29 | extern cl::list<std::string> InputFilenames; |
| 30 | extern cl::opt<std::string> OutputFilename; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 31 | |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | // Choose edge that returns |
| 35 | template <class C> |
| 36 | const Edge* ChooseEdge(const C& EdgesContainer, |
| 37 | const std::string& NodeName = "root") { |
| 38 | const Edge* DefaultEdge = 0; |
| 39 | |
| 40 | for (typename C::const_iterator B = EdgesContainer.begin(), |
| 41 | E = EdgesContainer.end(); B != E; ++B) { |
| 42 | const Edge* E = B->getPtr(); |
| 43 | |
| 44 | if (E->isDefault()) |
| 45 | if (!DefaultEdge) |
| 46 | DefaultEdge = E; |
| 47 | else |
| 48 | throw std::runtime_error("Node " + NodeName |
| 49 | + ": multiple default outward edges found!" |
| 50 | "Most probably a specification error."); |
| 51 | if (E->isEnabled()) |
| 52 | return E; |
| 53 | } |
| 54 | |
| 55 | if (DefaultEdge) |
| 56 | return DefaultEdge; |
| 57 | else |
| 58 | throw std::runtime_error("Node " + NodeName |
| 59 | + ": no default outward edge found!" |
| 60 | "Most probably a specification error."); |
Mikhail Glushenkov | 6591c89 | 2008-05-06 17:23:50 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Mikhail Glushenkov | 6591c89 | 2008-05-06 17:23:50 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 65 | CompilationGraph::CompilationGraph() { |
| 66 | NodesMap["root"] = Node(this); |
| 67 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 68 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 69 | Node& CompilationGraph::getNode(const std::string& ToolName) { |
| 70 | nodes_map_type::iterator I = NodesMap.find(ToolName); |
| 71 | if (I == NodesMap.end()) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 72 | throw std::runtime_error("Node " + ToolName + " is not in the graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 73 | return I->second; |
| 74 | } |
| 75 | |
| 76 | const Node& CompilationGraph::getNode(const std::string& ToolName) const { |
| 77 | nodes_map_type::const_iterator I = NodesMap.find(ToolName); |
| 78 | if (I == NodesMap.end()) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 79 | throw std::runtime_error("Node " + ToolName + " is not in the graph!"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 80 | return I->second; |
| 81 | } |
| 82 | |
| 83 | const std::string& CompilationGraph::getLanguage(const sys::Path& File) const { |
| 84 | LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix()); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 85 | if (Lang == ExtsToLangs.end()) |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 86 | throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!'); |
| 87 | return Lang->second; |
| 88 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 89 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 90 | const CompilationGraph::tools_vector_type& |
| 91 | CompilationGraph::getToolsVector(const std::string& LangName) const |
| 92 | { |
| 93 | tools_map_type::const_iterator I = ToolsMap.find(LangName); |
| 94 | if (I == ToolsMap.end()) |
| 95 | throw std::runtime_error("No tools corresponding to " + LangName |
| 96 | + " found!"); |
| 97 | return I->second; |
| 98 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 99 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 100 | void CompilationGraph::insertNode(Tool* V) { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 101 | if (NodesMap.count(V->Name()) == 0) { |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 102 | Node N; |
| 103 | N.OwningGraph = this; |
| 104 | N.ToolPtr = V; |
| 105 | NodesMap[V->Name()] = N; |
| 106 | } |
| 107 | } |
| 108 | |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 109 | void CompilationGraph::insertEdge(const std::string& A, Edge* E) { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 110 | Node& B = getNode(E->ToolName()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 111 | if (A == "root") { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 112 | const std::string& InputLanguage = B.ToolPtr->InputLanguage(); |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 113 | ToolsMap[InputLanguage].push_back(IntrusiveRefCntPtr<Edge>(E)); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 114 | NodesMap["root"].AddEdge(E); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 115 | } |
| 116 | else { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 117 | Node& N = getNode(A); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 118 | N.AddEdge(E); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 119 | } |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 120 | // Increase the inward edge counter. |
| 121 | B.IncrInEdges(); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 124 | // Pass input file through the chain until we bump into a Join node or |
| 125 | // a node that says that it is the last. |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 126 | const JoinTool* |
Mikhail Glushenkov | bbbc9d4 | 2008-05-06 17:27:37 +0000 | [diff] [blame] | 127 | CompilationGraph::PassThroughGraph (sys::Path& In, |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 128 | const Node* StartNode, |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 129 | const sys::Path& TempDir) const { |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 130 | bool Last = false; |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 131 | const Node* CurNode = StartNode; |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 132 | JoinTool* ret = 0; |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 133 | |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 134 | while(!Last) { |
Mikhail Glushenkov | bbbc9d4 | 2008-05-06 17:27:37 +0000 | [diff] [blame] | 135 | sys::Path Out; |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 136 | Tool* CurTool = CurNode->ToolPtr.getPtr(); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 137 | |
| 138 | if (CurTool->IsJoin()) { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 139 | ret = &dynamic_cast<JoinTool&>(*CurTool); |
| 140 | ret->AddToJoinList(In); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 141 | break; |
| 142 | } |
| 143 | |
| 144 | // Is this the last tool? |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 145 | if (!CurNode->HasChildren() || CurTool->IsLast()) { |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 146 | // Check if the first tool is also the last |
| 147 | if (Out.empty()) |
| 148 | Out.set(In.getBasename()); |
| 149 | else |
| 150 | Out.appendComponent(In.getBasename()); |
| 151 | Out.appendSuffix(CurTool->OutputSuffix()); |
| 152 | Last = true; |
| 153 | } |
| 154 | else { |
| 155 | Out = TempDir; |
| 156 | Out.appendComponent(In.getBasename()); |
| 157 | Out.appendSuffix(CurTool->OutputSuffix()); |
| 158 | Out.makeUnique(true, NULL); |
| 159 | Out.eraseFromDisk(); |
| 160 | } |
| 161 | |
| 162 | if (CurTool->GenerateAction(In, Out).Execute() != 0) |
| 163 | throw std::runtime_error("Tool returned error code!"); |
| 164 | |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 165 | CurNode = &getNode(ChooseEdge(CurNode->OutEdges, |
| 166 | CurNode->Name())->ToolName()); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 167 | In = Out; Out.clear(); |
| 168 | } |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame^] | 173 | // Sort the nodes in topological order. |
| 174 | void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) { |
| 175 | std::queue<const Node*> Q; |
| 176 | Q.push(&getNode("root")); |
| 177 | |
| 178 | while (!Q.empty()) { |
| 179 | const Node* A = Q.front(); |
| 180 | Q.pop(); |
| 181 | Out.push_back(A); |
| 182 | for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd(); |
| 183 | EB != EE; ++EB) { |
| 184 | Node* B = &getNode((*EB)->ToolName()); |
| 185 | B->DecrInEdges(); |
| 186 | if (B->HasNoInEdges()) |
| 187 | Q.push(B); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | namespace { |
| 193 | bool NotJoinNode(const Node* N) { |
| 194 | return N->ToolPtr ? !N->ToolPtr->IsJoin() : true; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Call TopologicalSort and filter the resulting list to include |
| 199 | // only Join nodes. |
| 200 | void CompilationGraph:: |
| 201 | TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) { |
| 202 | std::vector<const Node*> TopSorted; |
| 203 | TopologicalSort(TopSorted); |
| 204 | std::remove_copy_if(TopSorted.begin(), TopSorted.end(), |
| 205 | std::back_inserter(Out), NotJoinNode); |
| 206 | } |
| 207 | |
| 208 | // Find head of the toolchain corresponding to the given file. |
| 209 | const Node* CompilationGraph::FindToolChain(const sys::Path& In) const { |
| 210 | const std::string& InLanguage = getLanguage(In); |
| 211 | const tools_vector_type& TV = getToolsVector(InLanguage); |
| 212 | if (TV.empty()) |
| 213 | throw std::runtime_error("No toolchain corresponding to language" |
| 214 | + InLanguage + " found!"); |
| 215 | return &getNode(ChooseEdge(TV)->ToolName()); |
| 216 | } |
| 217 | |
| 218 | int CompilationGraph::Build (const sys::Path& TempDir) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 219 | |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 220 | // For each input file: |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 221 | for (cl::list<std::string>::const_iterator B = InputFilenames.begin(), |
| 222 | E = InputFilenames.end(); B != E; ++B) { |
Mikhail Glushenkov | bbbc9d4 | 2008-05-06 17:27:37 +0000 | [diff] [blame] | 223 | sys::Path In = sys::Path(*B); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 224 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame^] | 225 | // Find the toolchain corresponding to this file. |
| 226 | const Node* N = FindToolChain(In); |
| 227 | // Pass file through the chain starting at head. |
| 228 | PassThroughGraph(In, N, TempDir); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame^] | 231 | std::vector<const Node*> JTV; |
| 232 | TopologicalSortFilterJoinNodes(JTV); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 233 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame^] | 234 | // For all join nodes in topological order: |
| 235 | for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end(); |
| 236 | B != E; ++B) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 237 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 238 | |
| 239 | return 0; |
| 240 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 241 | |
Mikhail Glushenkov | bbbc9d4 | 2008-05-06 17:27:37 +0000 | [diff] [blame] | 242 | // Code related to graph visualization. |
| 243 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 244 | namespace llvm { |
| 245 | template <> |
| 246 | struct DOTGraphTraits<llvmcc::CompilationGraph*> |
| 247 | : public DefaultDOTGraphTraits |
| 248 | { |
| 249 | |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 250 | template<typename GraphType> |
| 251 | static std::string getNodeLabel(const Node* N, const GraphType&) |
| 252 | { |
| 253 | if (N->ToolPtr) |
| 254 | if (N->ToolPtr->IsJoin()) |
| 255 | return N->Name() + "\n (join" + |
| 256 | (N->HasChildren() ? ")" |
| 257 | : std::string(": ") + N->ToolPtr->OutputLanguage() + ')'); |
| 258 | else |
| 259 | return N->Name(); |
| 260 | else |
| 261 | return "root"; |
| 262 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 263 | |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 264 | template<typename EdgeIter> |
| 265 | static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) { |
| 266 | if (N->ToolPtr) |
| 267 | return N->ToolPtr->OutputLanguage(); |
| 268 | else |
| 269 | return I->ToolPtr->InputLanguage(); |
| 270 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 271 | }; |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 272 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void CompilationGraph::writeGraph() { |
| 276 | std::ofstream O("CompilationGraph.dot"); |
| 277 | |
Mikhail Glushenkov | a4db8c0 | 2008-05-06 16:37:33 +0000 | [diff] [blame] | 278 | if (O.good()) { |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 279 | llvm::WriteGraph(this, "CompilationGraph"); |
| 280 | O.close(); |
| 281 | } |
| 282 | else { |
| 283 | throw std::runtime_error(""); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void CompilationGraph::viewGraph() { |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 288 | llvm::ViewGraph(this, "compilation-graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 289 | } |