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 | |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 18 | #include "llvm/Support/DOTGraphTraits.h" |
| 19 | #include "llvm/Support/GraphWriter.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 20 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 22 | #include <iterator> |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 23 | #include <limits> |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 24 | #include <queue> |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 25 | #include <stdexcept> |
| 26 | |
| 27 | using namespace llvm; |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 28 | using namespace llvmc; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 29 | |
| 30 | extern cl::list<std::string> InputFilenames; |
| 31 | extern cl::opt<std::string> OutputFilename; |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 32 | extern cl::list<std::string> Languages; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 33 | |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame^] | 36 | // Return the edge with the maximum weight. |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 37 | template <class C> |
| 38 | const Edge* ChooseEdge(const C& EdgesContainer, |
| 39 | const std::string& NodeName = "root") { |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame^] | 40 | const Edge* MaxEdge = 0; |
| 41 | unsigned MaxWeight = 0; |
| 42 | bool SingleMax = true; |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 43 | |
| 44 | for (typename C::const_iterator B = EdgesContainer.begin(), |
| 45 | E = EdgesContainer.end(); B != E; ++B) { |
| 46 | const Edge* E = B->getPtr(); |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame^] | 47 | unsigned EW = E->Weight(); |
| 48 | if (EW > MaxWeight) { |
| 49 | MaxEdge = E; |
| 50 | MaxWeight = EW; |
| 51 | SingleMax = true; |
| 52 | } |
| 53 | else if (EW == MaxWeight) { |
| 54 | SingleMax = false; |
| 55 | } |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame^] | 58 | if (!SingleMax) |
| 59 | throw std::runtime_error("Node " + NodeName + |
| 60 | ": multiple maximal outward edges found!" |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 61 | " Most probably a specification error."); |
Mikhail Glushenkov | bb8b58d | 2008-05-06 18:14:24 +0000 | [diff] [blame^] | 62 | if (!MaxEdge) |
| 63 | throw std::runtime_error("Node " + NodeName + |
| 64 | ": no maximal outward edge found!" |
| 65 | " Most probably a specification error."); |
| 66 | return MaxEdge; |
Mikhail Glushenkov | 6591c89 | 2008-05-06 17:23:50 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Mikhail Glushenkov | 6591c89 | 2008-05-06 17:23:50 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 71 | CompilationGraph::CompilationGraph() { |
| 72 | NodesMap["root"] = Node(this); |
| 73 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 74 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 75 | Node& CompilationGraph::getNode(const std::string& ToolName) { |
| 76 | nodes_map_type::iterator I = NodesMap.find(ToolName); |
| 77 | if (I == NodesMap.end()) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 78 | throw std::runtime_error("Node " + ToolName + " is not in the graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 79 | return I->second; |
| 80 | } |
| 81 | |
| 82 | const Node& CompilationGraph::getNode(const std::string& ToolName) const { |
| 83 | nodes_map_type::const_iterator I = NodesMap.find(ToolName); |
| 84 | if (I == NodesMap.end()) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 85 | throw std::runtime_error("Node " + ToolName + " is not in the graph!"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 86 | return I->second; |
| 87 | } |
| 88 | |
| 89 | const std::string& CompilationGraph::getLanguage(const sys::Path& File) const { |
| 90 | LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix()); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 91 | if (Lang == ExtsToLangs.end()) |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 92 | throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!'); |
| 93 | return Lang->second; |
| 94 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 95 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 96 | const CompilationGraph::tools_vector_type& |
| 97 | CompilationGraph::getToolsVector(const std::string& LangName) const |
| 98 | { |
| 99 | tools_map_type::const_iterator I = ToolsMap.find(LangName); |
| 100 | if (I == ToolsMap.end()) |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 101 | throw std::runtime_error("No tool corresponding to the language " |
| 102 | + LangName + "found!"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 103 | return I->second; |
| 104 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 105 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 106 | void CompilationGraph::insertNode(Tool* V) { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 107 | if (NodesMap.count(V->Name()) == 0) { |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 108 | Node N; |
| 109 | N.OwningGraph = this; |
| 110 | N.ToolPtr = V; |
| 111 | NodesMap[V->Name()] = N; |
| 112 | } |
| 113 | } |
| 114 | |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 115 | void CompilationGraph::insertEdge(const std::string& A, Edge* E) { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 116 | Node& B = getNode(E->ToolName()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 117 | if (A == "root") { |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 118 | const std::string& InputLanguage = B.ToolPtr->InputLanguage(); |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 119 | ToolsMap[InputLanguage].push_back(IntrusiveRefCntPtr<Edge>(E)); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 120 | NodesMap["root"].AddEdge(E); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 121 | } |
| 122 | else { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 123 | Node& N = getNode(A); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 124 | N.AddEdge(E); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 125 | } |
Mikhail Glushenkov | c74bfc9 | 2008-05-06 17:26:53 +0000 | [diff] [blame] | 126 | // Increase the inward edge counter. |
| 127 | B.IncrInEdges(); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 130 | namespace { |
| 131 | sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName, |
| 132 | const std::string& Suffix) { |
| 133 | sys::Path Out = TempDir; |
| 134 | Out.appendComponent(BaseName); |
| 135 | Out.appendSuffix(Suffix); |
| 136 | Out.makeUnique(true, NULL); |
| 137 | return Out; |
| 138 | } |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 141 | // Pass input file through the chain until we bump into a Join node or |
| 142 | // a node that says that it is the last. |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 143 | void CompilationGraph::PassThroughGraph (const sys::Path& InFile, |
| 144 | const Node* StartNode, |
| 145 | const sys::Path& TempDir) const { |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 146 | bool Last = false; |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 147 | sys::Path In = InFile; |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 148 | const Node* CurNode = StartNode; |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 149 | |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 150 | while(!Last) { |
Mikhail Glushenkov | bbbc9d4 | 2008-05-06 17:27:37 +0000 | [diff] [blame] | 151 | sys::Path Out; |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 152 | Tool* CurTool = CurNode->ToolPtr.getPtr(); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 153 | |
| 154 | if (CurTool->IsJoin()) { |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 155 | JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool); |
| 156 | JT.AddToJoinList(In); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 157 | break; |
| 158 | } |
| 159 | |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 160 | // Since toolchains do not have to end with a Join node, we should |
| 161 | // check if this Node is the last. |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 162 | if (!CurNode->HasChildren() || CurTool->IsLast()) { |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 163 | if (!OutputFilename.empty()) { |
| 164 | Out.set(OutputFilename); |
| 165 | } |
| 166 | else { |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 167 | Out.set(In.getBasename()); |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 168 | Out.appendSuffix(CurTool->OutputSuffix()); |
| 169 | } |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 170 | Last = true; |
| 171 | } |
| 172 | else { |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 173 | Out = MakeTempFile(TempDir, In.getBasename(), CurTool->OutputSuffix()); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | if (CurTool->GenerateAction(In, Out).Execute() != 0) |
| 177 | throw std::runtime_error("Tool returned error code!"); |
| 178 | |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 179 | if (Last) |
| 180 | return; |
| 181 | |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 182 | CurNode = &getNode(ChooseEdge(CurNode->OutEdges, |
| 183 | CurNode->Name())->ToolName()); |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 184 | In = Out; Out.clear(); |
| 185 | } |
Mikhail Glushenkov | 2ba4c5a | 2008-05-06 17:25:51 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 188 | // Sort the nodes in topological order. |
| 189 | void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) { |
| 190 | std::queue<const Node*> Q; |
| 191 | Q.push(&getNode("root")); |
| 192 | |
| 193 | while (!Q.empty()) { |
| 194 | const Node* A = Q.front(); |
| 195 | Q.pop(); |
| 196 | Out.push_back(A); |
| 197 | for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd(); |
| 198 | EB != EE; ++EB) { |
| 199 | Node* B = &getNode((*EB)->ToolName()); |
| 200 | B->DecrInEdges(); |
| 201 | if (B->HasNoInEdges()) |
| 202 | Q.push(B); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | namespace { |
| 208 | bool NotJoinNode(const Node* N) { |
| 209 | return N->ToolPtr ? !N->ToolPtr->IsJoin() : true; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // Call TopologicalSort and filter the resulting list to include |
| 214 | // only Join nodes. |
| 215 | void CompilationGraph:: |
| 216 | TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) { |
| 217 | std::vector<const Node*> TopSorted; |
| 218 | TopologicalSort(TopSorted); |
| 219 | std::remove_copy_if(TopSorted.begin(), TopSorted.end(), |
| 220 | std::back_inserter(Out), NotJoinNode); |
| 221 | } |
| 222 | |
| 223 | // Find head of the toolchain corresponding to the given file. |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 224 | const Node* CompilationGraph:: |
| 225 | FindToolChain(const sys::Path& In, const std::string* forceLanguage) const { |
| 226 | const std::string& InLanguage = |
| 227 | forceLanguage ? *forceLanguage : getLanguage(In); |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 228 | const tools_vector_type& TV = getToolsVector(InLanguage); |
| 229 | if (TV.empty()) |
| 230 | throw std::runtime_error("No toolchain corresponding to language" |
| 231 | + InLanguage + " found!"); |
| 232 | return &getNode(ChooseEdge(TV)->ToolName()); |
| 233 | } |
| 234 | |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 235 | // Build the targets. Command-line options are passed through |
| 236 | // temporary variables. |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 237 | int CompilationGraph::Build (const sys::Path& TempDir) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 238 | |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 239 | // This is related to -x option handling. |
| 240 | cl::list<std::string>::const_iterator xIter = Languages.begin(), |
| 241 | xBegin = xIter, xEnd = Languages.end(); |
| 242 | bool xEmpty = true; |
| 243 | const std::string* xLanguage = 0; |
| 244 | unsigned xPos = 0, xPosNext = 0, filePos = 0; |
| 245 | |
| 246 | if (xIter != xEnd) { |
| 247 | xEmpty = false; |
| 248 | xPos = Languages.getPosition(xIter - xBegin); |
| 249 | cl::list<std::string>::const_iterator xNext = llvm::next(xIter); |
| 250 | xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max() |
| 251 | : Languages.getPosition(xNext - xBegin); |
| 252 | xLanguage = (*xIter == "none") ? 0 : &(*xIter); |
| 253 | } |
| 254 | |
Mikhail Glushenkov | 4f6e3a4 | 2008-05-06 17:28:03 +0000 | [diff] [blame] | 255 | // For each input file: |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 256 | for (cl::list<std::string>::const_iterator B = InputFilenames.begin(), |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 257 | CB = B, E = InputFilenames.end(); B != E; ++B) { |
Mikhail Glushenkov | bbbc9d4 | 2008-05-06 17:27:37 +0000 | [diff] [blame] | 258 | sys::Path In = sys::Path(*B); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 259 | |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 260 | // Code for handling the -x option. |
| 261 | // Output: std::string* xLanguage (can be NULL). |
| 262 | if (!xEmpty) { |
| 263 | filePos = InputFilenames.getPosition(B - CB); |
| 264 | |
| 265 | if (xPos < filePos) { |
| 266 | if (filePos < xPosNext) { |
| 267 | xLanguage = (*xIter == "none") ? 0 : &(*xIter); |
| 268 | } |
| 269 | else { // filePos >= xPosNext |
| 270 | // Skip xIters while filePos > xPosNext |
| 271 | while (filePos > xPosNext) { |
| 272 | ++xIter; |
| 273 | xPos = xPosNext; |
| 274 | |
| 275 | cl::list<std::string>::const_iterator xNext = llvm::next(xIter); |
| 276 | if (xNext == xEnd) |
| 277 | xPosNext = std::numeric_limits<unsigned>::max(); |
| 278 | else |
| 279 | xPosNext = Languages.getPosition(xNext - xBegin); |
| 280 | xLanguage = (*xIter == "none") ? 0 : &(*xIter); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 286 | // Find the toolchain corresponding to this file. |
Mikhail Glushenkov | 87416b4 | 2008-05-06 18:10:53 +0000 | [diff] [blame] | 287 | const Node* N = FindToolChain(In, xLanguage); |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 288 | // Pass file through the chain starting at head. |
| 289 | PassThroughGraph(In, N, TempDir); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 292 | std::vector<const Node*> JTV; |
| 293 | TopologicalSortFilterJoinNodes(JTV); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 294 | |
Mikhail Glushenkov | 0260658 | 2008-05-06 18:07:14 +0000 | [diff] [blame] | 295 | // For all join nodes in topological order: |
| 296 | for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end(); |
| 297 | B != E; ++B) { |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 298 | |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 299 | sys::Path Out; |
| 300 | const Node* CurNode = *B; |
| 301 | JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr()); |
| 302 | bool IsLast = false; |
| 303 | |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 304 | // Are there any files to be joined? |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 305 | if (JT->JoinListEmpty()) |
| 306 | continue; |
| 307 | |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 308 | // Is this the last tool in the chain? |
| 309 | // NOTE: we can process several chains in parallel. |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 310 | if (!CurNode->HasChildren() || JT->IsLast()) { |
| 311 | if (OutputFilename.empty()) { |
| 312 | Out.set("a"); |
| 313 | Out.appendSuffix(JT->OutputSuffix()); |
| 314 | } |
| 315 | else |
| 316 | Out.set(OutputFilename); |
| 317 | IsLast = true; |
| 318 | } |
| 319 | else { |
Mikhail Glushenkov | 35a85e8 | 2008-05-06 18:10:20 +0000 | [diff] [blame] | 320 | Out = MakeTempFile(TempDir, "tmp", JT->OutputSuffix()); |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | if (JT->GenerateAction(Out).Execute() != 0) |
| 324 | throw std::runtime_error("Tool returned error code!"); |
| 325 | |
| 326 | if (!IsLast) { |
| 327 | const Node* NextNode = &getNode(ChooseEdge(CurNode->OutEdges, |
| 328 | CurNode->Name())->ToolName()); |
| 329 | PassThroughGraph(Out, NextNode, TempDir); |
| 330 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 331 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 332 | |
| 333 | return 0; |
| 334 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 335 | |
Mikhail Glushenkov | bbbc9d4 | 2008-05-06 17:27:37 +0000 | [diff] [blame] | 336 | // Code related to graph visualization. |
| 337 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 338 | namespace llvm { |
| 339 | template <> |
Mikhail Glushenkov | be9d9a1 | 2008-05-06 18:08:59 +0000 | [diff] [blame] | 340 | struct DOTGraphTraits<llvmc::CompilationGraph*> |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 341 | : public DefaultDOTGraphTraits |
| 342 | { |
| 343 | |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 344 | template<typename GraphType> |
| 345 | static std::string getNodeLabel(const Node* N, const GraphType&) |
| 346 | { |
| 347 | if (N->ToolPtr) |
| 348 | if (N->ToolPtr->IsJoin()) |
| 349 | return N->Name() + "\n (join" + |
| 350 | (N->HasChildren() ? ")" |
| 351 | : std::string(": ") + N->ToolPtr->OutputLanguage() + ')'); |
| 352 | else |
| 353 | return N->Name(); |
| 354 | else |
| 355 | return "root"; |
| 356 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 357 | |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 358 | template<typename EdgeIter> |
| 359 | static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) { |
| 360 | if (N->ToolPtr) |
| 361 | return N->ToolPtr->OutputLanguage(); |
| 362 | else |
| 363 | return I->ToolPtr->InputLanguage(); |
| 364 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 365 | }; |
Mikhail Glushenkov | 97fda6d | 2008-05-06 17:26:14 +0000 | [diff] [blame] | 366 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void CompilationGraph::writeGraph() { |
| 370 | std::ofstream O("CompilationGraph.dot"); |
| 371 | |
Mikhail Glushenkov | a4db8c0 | 2008-05-06 16:37:33 +0000 | [diff] [blame] | 372 | if (O.good()) { |
Mikhail Glushenkov | 3d68822 | 2008-05-06 18:07:48 +0000 | [diff] [blame] | 373 | llvm::WriteGraph(this, "compilation-graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 374 | O.close(); |
| 375 | } |
| 376 | else { |
| 377 | throw std::runtime_error(""); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | void CompilationGraph::viewGraph() { |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 382 | llvm::ViewGraph(this, "compilation-graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 383 | } |