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