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