blob: 4b04d6f60b36451a6bce2ff51c7d0469a6c48325 [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 Glushenkov87416b42008-05-06 18:10:53 +000036 // Go through the list C and find the edge that isEnabled(); if
37 // there is no such edge, return the default edge; if there is no
38 // default edge, throw an exception.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000039 template <class C>
40 const Edge* ChooseEdge(const C& EdgesContainer,
41 const std::string& NodeName = "root") {
42 const Edge* DefaultEdge = 0;
43
44 for (typename C::const_iterator B = EdgesContainer.begin(),
45 E = EdgesContainer.end(); B != E; ++B) {
46 const Edge* E = B->getPtr();
47
48 if (E->isDefault())
49 if (!DefaultEdge)
50 DefaultEdge = E;
51 else
52 throw std::runtime_error("Node " + NodeName
53 + ": multiple default outward edges found!"
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000054 " Most probably a specification error.");
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000055 if (E->isEnabled())
56 return E;
57 }
58
59 if (DefaultEdge)
60 return DefaultEdge;
61 else
62 throw std::runtime_error("Node " + NodeName
63 + ": no default outward edge found!"
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000064 " Most probably a specification error.");
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000065 }
66
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000067}
68
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000069CompilationGraph::CompilationGraph() {
70 NodesMap["root"] = Node(this);
71}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000072
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000073Node& CompilationGraph::getNode(const std::string& ToolName) {
74 nodes_map_type::iterator I = NodesMap.find(ToolName);
75 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000076 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000077 return I->second;
78}
79
80const Node& CompilationGraph::getNode(const std::string& ToolName) const {
81 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
82 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000083 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000084 return I->second;
85}
86
87const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
88 LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000089 if (Lang == ExtsToLangs.end())
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000090 throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!');
91 return Lang->second;
92}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000093
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000094const CompilationGraph::tools_vector_type&
95CompilationGraph::getToolsVector(const std::string& LangName) const
96{
97 tools_map_type::const_iterator I = ToolsMap.find(LangName);
98 if (I == ToolsMap.end())
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000099 throw std::runtime_error("No tool corresponding to the language "
100 + LangName + "found!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000101 return I->second;
102}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000103
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000104void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000105 if (NodesMap.count(V->Name()) == 0) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000106 Node N;
107 N.OwningGraph = this;
108 N.ToolPtr = V;
109 NodesMap[V->Name()] = N;
110 }
111}
112
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000113void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000114 Node& B = getNode(E->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000115 if (A == "root") {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000116 const std::string& InputLanguage = B.ToolPtr->InputLanguage();
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000117 ToolsMap[InputLanguage].push_back(IntrusiveRefCntPtr<Edge>(E));
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000118 NodesMap["root"].AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000119 }
120 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000121 Node& N = getNode(A);
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000122 N.AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000123 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000124 // Increase the inward edge counter.
125 B.IncrInEdges();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000126}
127
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000128namespace {
129 sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName,
130 const std::string& Suffix) {
131 sys::Path Out = TempDir;
132 Out.appendComponent(BaseName);
133 Out.appendSuffix(Suffix);
134 Out.makeUnique(true, NULL);
135 return Out;
136 }
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000137}
138
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000139// Pass input file through the chain until we bump into a Join node or
140// a node that says that it is the last.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000141void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
142 const Node* StartNode,
143 const sys::Path& TempDir) const {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000144 bool Last = false;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000145 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000146 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000147
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000148 while(!Last) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000149 sys::Path Out;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000150 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000151
152 if (CurTool->IsJoin()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000153 JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool);
154 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000155 break;
156 }
157
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000158 // Since toolchains do not have to end with a Join node, we should
159 // check if this Node is the last.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000160 if (!CurNode->HasChildren() || CurTool->IsLast()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000161 if (!OutputFilename.empty()) {
162 Out.set(OutputFilename);
163 }
164 else {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000165 Out.set(In.getBasename());
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000166 Out.appendSuffix(CurTool->OutputSuffix());
167 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000168 Last = true;
169 }
170 else {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000171 Out = MakeTempFile(TempDir, In.getBasename(), CurTool->OutputSuffix());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000172 }
173
174 if (CurTool->GenerateAction(In, Out).Execute() != 0)
175 throw std::runtime_error("Tool returned error code!");
176
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000177 if (Last)
178 return;
179
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000180 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
181 CurNode->Name())->ToolName());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000182 In = Out; Out.clear();
183 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000184}
185
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000186// Sort the nodes in topological order.
187void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
188 std::queue<const Node*> Q;
189 Q.push(&getNode("root"));
190
191 while (!Q.empty()) {
192 const Node* A = Q.front();
193 Q.pop();
194 Out.push_back(A);
195 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
196 EB != EE; ++EB) {
197 Node* B = &getNode((*EB)->ToolName());
198 B->DecrInEdges();
199 if (B->HasNoInEdges())
200 Q.push(B);
201 }
202 }
203}
204
205namespace {
206 bool NotJoinNode(const Node* N) {
207 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
208 }
209}
210
211// Call TopologicalSort and filter the resulting list to include
212// only Join nodes.
213void CompilationGraph::
214TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
215 std::vector<const Node*> TopSorted;
216 TopologicalSort(TopSorted);
217 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
218 std::back_inserter(Out), NotJoinNode);
219}
220
221// Find head of the toolchain corresponding to the given file.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000222const Node* CompilationGraph::
223FindToolChain(const sys::Path& In, const std::string* forceLanguage) const {
224 const std::string& InLanguage =
225 forceLanguage ? *forceLanguage : getLanguage(In);
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000226 const tools_vector_type& TV = getToolsVector(InLanguage);
227 if (TV.empty())
228 throw std::runtime_error("No toolchain corresponding to language"
229 + InLanguage + " found!");
230 return &getNode(ChooseEdge(TV)->ToolName());
231}
232
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000233// Build the targets. Command-line options are passed through
234// temporary variables.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000235int CompilationGraph::Build (const sys::Path& TempDir) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000236
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000237 // This is related to -x option handling.
238 cl::list<std::string>::const_iterator xIter = Languages.begin(),
239 xBegin = xIter, xEnd = Languages.end();
240 bool xEmpty = true;
241 const std::string* xLanguage = 0;
242 unsigned xPos = 0, xPosNext = 0, filePos = 0;
243
244 if (xIter != xEnd) {
245 xEmpty = false;
246 xPos = Languages.getPosition(xIter - xBegin);
247 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
248 xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max()
249 : Languages.getPosition(xNext - xBegin);
250 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
251 }
252
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000253 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000254 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000255 CB = B, E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000256 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000257
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000258 // Code for handling the -x option.
259 // Output: std::string* xLanguage (can be NULL).
260 if (!xEmpty) {
261 filePos = InputFilenames.getPosition(B - CB);
262
263 if (xPos < filePos) {
264 if (filePos < xPosNext) {
265 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
266 }
267 else { // filePos >= xPosNext
268 // Skip xIters while filePos > xPosNext
269 while (filePos > xPosNext) {
270 ++xIter;
271 xPos = xPosNext;
272
273 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
274 if (xNext == xEnd)
275 xPosNext = std::numeric_limits<unsigned>::max();
276 else
277 xPosNext = Languages.getPosition(xNext - xBegin);
278 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
279 }
280 }
281 }
282 }
283
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000284 // Find the toolchain corresponding to this file.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000285 const Node* N = FindToolChain(In, xLanguage);
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000286 // Pass file through the chain starting at head.
287 PassThroughGraph(In, N, TempDir);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000288 }
289
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000290 std::vector<const Node*> JTV;
291 TopologicalSortFilterJoinNodes(JTV);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000292
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000293 // For all join nodes in topological order:
294 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
295 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000296
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000297 sys::Path Out;
298 const Node* CurNode = *B;
299 JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
300 bool IsLast = false;
301
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000302 // Are there any files to be joined?
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000303 if (JT->JoinListEmpty())
304 continue;
305
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000306 // Is this the last tool in the chain?
307 // NOTE: we can process several chains in parallel.
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000308 if (!CurNode->HasChildren() || JT->IsLast()) {
309 if (OutputFilename.empty()) {
310 Out.set("a");
311 Out.appendSuffix(JT->OutputSuffix());
312 }
313 else
314 Out.set(OutputFilename);
315 IsLast = true;
316 }
317 else {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000318 Out = MakeTempFile(TempDir, "tmp", JT->OutputSuffix());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000319 }
320
321 if (JT->GenerateAction(Out).Execute() != 0)
322 throw std::runtime_error("Tool returned error code!");
323
324 if (!IsLast) {
325 const Node* NextNode = &getNode(ChooseEdge(CurNode->OutEdges,
326 CurNode->Name())->ToolName());
327 PassThroughGraph(Out, NextNode, TempDir);
328 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000329 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000330
331 return 0;
332}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000333
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000334// Code related to graph visualization.
335
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000336namespace llvm {
337 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000338 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000339 : public DefaultDOTGraphTraits
340 {
341
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000342 template<typename GraphType>
343 static std::string getNodeLabel(const Node* N, const GraphType&)
344 {
345 if (N->ToolPtr)
346 if (N->ToolPtr->IsJoin())
347 return N->Name() + "\n (join" +
348 (N->HasChildren() ? ")"
349 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
350 else
351 return N->Name();
352 else
353 return "root";
354 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000355
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000356 template<typename EdgeIter>
357 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
358 if (N->ToolPtr)
359 return N->ToolPtr->OutputLanguage();
360 else
361 return I->ToolPtr->InputLanguage();
362 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000363 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000364
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000365}
366
367void CompilationGraph::writeGraph() {
368 std::ofstream O("CompilationGraph.dot");
369
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000370 if (O.good()) {
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000371 llvm::WriteGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000372 O.close();
373 }
374 else {
375 throw std::runtime_error("");
376 }
377}
378
379void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000380 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000381}