blob: 310413b9f0a5a4b41d0b2740c0897d831278e850 [file] [log] [blame]
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +00001//===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===//
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00002//
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 Glushenkovb90cd832008-05-06 16:34:12 +000010// Compilation graph - implementation.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000011//
12//===----------------------------------------------------------------------===//
13
Mikhail Glushenkovb90cd832008-05-06 16:34:12 +000014#include "CompilationGraph.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000015
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000016#include "llvm/ADT/STLExtras.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000017#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000018#include "llvm/Support/DOTGraphTraits.h"
19#include "llvm/Support/GraphWriter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000020
Mikhail Glushenkov02606582008-05-06 18:07:14 +000021#include <algorithm>
Mikhail Glushenkov02606582008-05-06 18:07:14 +000022#include <iterator>
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000023#include <limits>
Mikhail Glushenkov02606582008-05-06 18:07:14 +000024#include <queue>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000025#include <stdexcept>
26
27using namespace llvm;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000028using namespace llvmc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000029
30extern cl::list<std::string> InputFilenames;
31extern cl::opt<std::string> OutputFilename;
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000032extern cl::list<std::string> Languages;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000033
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000034namespace {
35
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000036 // Return the edge with the maximum weight.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000037 template <class C>
38 const Edge* ChooseEdge(const C& EdgesContainer,
39 const std::string& NodeName = "root") {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000040 const Edge* MaxEdge = 0;
41 unsigned MaxWeight = 0;
42 bool SingleMax = true;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000043
44 for (typename C::const_iterator B = EdgesContainer.begin(),
45 E = EdgesContainer.end(); B != E; ++B) {
46 const Edge* E = B->getPtr();
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000047 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 Glushenkov4f6e3a42008-05-06 17:28:03 +000056 }
57
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000058 if (!SingleMax)
59 throw std::runtime_error("Node " + NodeName +
60 ": multiple maximal outward edges found!"
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000061 " Most probably a specification error.");
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000062 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 Glushenkov6591c892008-05-06 17:23:50 +000067 }
68
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000069}
70
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000071CompilationGraph::CompilationGraph() {
72 NodesMap["root"] = Node(this);
73}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000074
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000075Node& CompilationGraph::getNode(const std::string& ToolName) {
76 nodes_map_type::iterator I = NodesMap.find(ToolName);
77 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000078 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000079 return I->second;
80}
81
82const Node& CompilationGraph::getNode(const std::string& ToolName) const {
83 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
84 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000085 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000086 return I->second;
87}
88
89const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
90 LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000091 if (Lang == ExtsToLangs.end())
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000092 throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!');
93 return Lang->second;
94}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000095
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000096const CompilationGraph::tools_vector_type&
97CompilationGraph::getToolsVector(const std::string& LangName) const
98{
99 tools_map_type::const_iterator I = ToolsMap.find(LangName);
100 if (I == ToolsMap.end())
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000101 throw std::runtime_error("No tool corresponding to the language "
102 + LangName + "found!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000103 return I->second;
104}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000105
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000106void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000107 if (NodesMap.count(V->Name()) == 0) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000108 Node N;
109 N.OwningGraph = this;
110 N.ToolPtr = V;
111 NodesMap[V->Name()] = N;
112 }
113}
114
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000115void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000116 Node& B = getNode(E->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000117 if (A == "root") {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000118 const std::string& InputLanguage = B.ToolPtr->InputLanguage();
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000119 ToolsMap[InputLanguage].push_back(IntrusiveRefCntPtr<Edge>(E));
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000120 NodesMap["root"].AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000121 }
122 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000123 Node& N = getNode(A);
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000124 N.AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000125 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000126 // Increase the inward edge counter.
127 B.IncrInEdges();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000128}
129
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000130namespace {
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 Glushenkov35a85e82008-05-06 18:10:20 +0000139}
140
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000141// 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 Glushenkov35a85e82008-05-06 18:10:20 +0000143void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
144 const Node* StartNode,
145 const sys::Path& TempDir) const {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000146 bool Last = false;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000147 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000148 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000149
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000150 while(!Last) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000151 sys::Path Out;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000152 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000153
154 if (CurTool->IsJoin()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000155 JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool);
156 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000157 break;
158 }
159
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000160 // Since toolchains do not have to end with a Join node, we should
161 // check if this Node is the last.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000162 if (!CurNode->HasChildren() || CurTool->IsLast()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000163 if (!OutputFilename.empty()) {
164 Out.set(OutputFilename);
165 }
166 else {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000167 Out.set(In.getBasename());
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000168 Out.appendSuffix(CurTool->OutputSuffix());
169 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000170 Last = true;
171 }
172 else {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000173 Out = MakeTempFile(TempDir, In.getBasename(), CurTool->OutputSuffix());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000174 }
175
176 if (CurTool->GenerateAction(In, Out).Execute() != 0)
177 throw std::runtime_error("Tool returned error code!");
178
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000179 if (Last)
180 return;
181
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000182 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
183 CurNode->Name())->ToolName());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000184 In = Out; Out.clear();
185 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000186}
187
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000188// Sort the nodes in topological order.
189void 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
207namespace {
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.
215void CompilationGraph::
216TopologicalSortFilterJoinNodes(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 Glushenkov87416b42008-05-06 18:10:53 +0000224const Node* CompilationGraph::
225FindToolChain(const sys::Path& In, const std::string* forceLanguage) const {
226 const std::string& InLanguage =
227 forceLanguage ? *forceLanguage : getLanguage(In);
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000228 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 Glushenkovbe9d9a12008-05-06 18:08:59 +0000235// Build the targets. Command-line options are passed through
236// temporary variables.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000237int CompilationGraph::Build (const sys::Path& TempDir) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000238
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000239 // 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 Glushenkov4f6e3a42008-05-06 17:28:03 +0000255 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000256 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000257 CB = B, E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000258 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000259
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000260 // 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 Glushenkov02606582008-05-06 18:07:14 +0000286 // Find the toolchain corresponding to this file.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000287 const Node* N = FindToolChain(In, xLanguage);
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000288 // Pass file through the chain starting at head.
289 PassThroughGraph(In, N, TempDir);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000290 }
291
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000292 std::vector<const Node*> JTV;
293 TopologicalSortFilterJoinNodes(JTV);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000294
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000295 // 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 Glushenkovbe9d9a12008-05-06 18:08:59 +0000298
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000299 sys::Path Out;
300 const Node* CurNode = *B;
301 JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
302 bool IsLast = false;
303
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000304 // Are there any files to be joined?
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000305 if (JT->JoinListEmpty())
306 continue;
307
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000308 // Is this the last tool in the chain?
309 // NOTE: we can process several chains in parallel.
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000310 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 Glushenkov35a85e82008-05-06 18:10:20 +0000320 Out = MakeTempFile(TempDir, "tmp", JT->OutputSuffix());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000321 }
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 Korobeynikovac67b7e2008-03-23 08:57:20 +0000331 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000332
333 return 0;
334}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000335
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000336// Code related to graph visualization.
337
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000338namespace llvm {
339 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000340 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000341 : public DefaultDOTGraphTraits
342 {
343
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000344 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 Glushenkov0d08db02008-05-06 16:35:25 +0000357
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000358 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 Glushenkov0d08db02008-05-06 16:35:25 +0000365 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000366
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000367}
368
369void CompilationGraph::writeGraph() {
370 std::ofstream O("CompilationGraph.dot");
371
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000372 if (O.good()) {
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000373 llvm::WriteGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000374 O.close();
375 }
376 else {
377 throw std::runtime_error("");
378 }
379}
380
381void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000382 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000383}