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