blob: ed03f5856ec544439f2feaf39aad72d62df19abc [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
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000016#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000017#include "llvm/Support/DOTGraphTraits.h"
18#include "llvm/Support/GraphWriter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000019
Mikhail Glushenkov02606582008-05-06 18:07:14 +000020#include <algorithm>
21#include <iostream>
22#include <iterator>
23#include <queue>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000024#include <stdexcept>
25
26using namespace llvm;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000027using namespace llvmc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000028
29extern cl::list<std::string> InputFilenames;
30extern cl::opt<std::string> OutputFilename;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000032namespace {
33
34 // Choose edge that returns
35 template <class C>
36 const Edge* ChooseEdge(const C& EdgesContainer,
37 const std::string& NodeName = "root") {
38 const Edge* DefaultEdge = 0;
39
40 for (typename C::const_iterator B = EdgesContainer.begin(),
41 E = EdgesContainer.end(); B != E; ++B) {
42 const Edge* E = B->getPtr();
43
44 if (E->isDefault())
45 if (!DefaultEdge)
46 DefaultEdge = E;
47 else
48 throw std::runtime_error("Node " + NodeName
49 + ": multiple default outward edges found!"
50 "Most probably a specification error.");
51 if (E->isEnabled())
52 return E;
53 }
54
55 if (DefaultEdge)
56 return DefaultEdge;
57 else
58 throw std::runtime_error("Node " + NodeName
59 + ": no default outward edge found!"
60 "Most probably a specification error.");
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000061 }
62
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000063}
64
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000065CompilationGraph::CompilationGraph() {
66 NodesMap["root"] = Node(this);
67}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000068
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000069Node& CompilationGraph::getNode(const std::string& ToolName) {
70 nodes_map_type::iterator I = NodesMap.find(ToolName);
71 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000072 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000073 return I->second;
74}
75
76const Node& CompilationGraph::getNode(const std::string& ToolName) const {
77 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
78 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000079 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000080 return I->second;
81}
82
83const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
84 LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000085 if (Lang == ExtsToLangs.end())
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000086 throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!');
87 return Lang->second;
88}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000089
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000090const CompilationGraph::tools_vector_type&
91CompilationGraph::getToolsVector(const std::string& LangName) const
92{
93 tools_map_type::const_iterator I = ToolsMap.find(LangName);
94 if (I == ToolsMap.end())
95 throw std::runtime_error("No tools corresponding to " + LangName
96 + " found!");
97 return I->second;
98}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000099
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000100void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000101 if (NodesMap.count(V->Name()) == 0) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000102 Node N;
103 N.OwningGraph = this;
104 N.ToolPtr = V;
105 NodesMap[V->Name()] = N;
106 }
107}
108
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000109void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000110 Node& B = getNode(E->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000111 if (A == "root") {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000112 const std::string& InputLanguage = B.ToolPtr->InputLanguage();
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000113 ToolsMap[InputLanguage].push_back(IntrusiveRefCntPtr<Edge>(E));
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000114 NodesMap["root"].AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000115 }
116 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000117 Node& N = getNode(A);
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000118 N.AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000119 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000120 // Increase the inward edge counter.
121 B.IncrInEdges();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000122}
123
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000124// Make a temporary file named like BaseName-RandomDigits.Suffix
125sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName,
126 const std::string& Suffix) {
127 sys::Path Out = TempDir;
128 Out.appendComponent(BaseName);
129 Out.appendSuffix(Suffix);
130 Out.makeUnique(true, NULL);
131 Out.eraseFromDisk();
132 return Out;
133}
134
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000135// Pass input file through the chain until we bump into a Join node or
136// a node that says that it is the last.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000137void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
138 const Node* StartNode,
139 const sys::Path& TempDir) const {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000140 bool Last = false;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000141 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000142 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000143
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000144 while(!Last) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000145 sys::Path Out;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000146 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000147
148 if (CurTool->IsJoin()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000149 JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool);
150 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000151 break;
152 }
153
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000154 // Since toolchains do not have to end with a Join node, we should
155 // check if this Node is the last.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000156 if (!CurNode->HasChildren() || CurTool->IsLast()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000157 if (!OutputFilename.empty()) {
158 Out.set(OutputFilename);
159 }
160 else {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000161 Out.set(In.getBasename());
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000162 Out.appendSuffix(CurTool->OutputSuffix());
163 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000164 Last = true;
165 }
166 else {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000167 Out = MakeTempFile(TempDir, In.getBasename(), CurTool->OutputSuffix());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000168 }
169
170 if (CurTool->GenerateAction(In, Out).Execute() != 0)
171 throw std::runtime_error("Tool returned error code!");
172
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000173 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
174 CurNode->Name())->ToolName());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000175 In = Out; Out.clear();
176 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000177}
178
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000179// Sort the nodes in topological order.
180void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
181 std::queue<const Node*> Q;
182 Q.push(&getNode("root"));
183
184 while (!Q.empty()) {
185 const Node* A = Q.front();
186 Q.pop();
187 Out.push_back(A);
188 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
189 EB != EE; ++EB) {
190 Node* B = &getNode((*EB)->ToolName());
191 B->DecrInEdges();
192 if (B->HasNoInEdges())
193 Q.push(B);
194 }
195 }
196}
197
198namespace {
199 bool NotJoinNode(const Node* N) {
200 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
201 }
202}
203
204// Call TopologicalSort and filter the resulting list to include
205// only Join nodes.
206void CompilationGraph::
207TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
208 std::vector<const Node*> TopSorted;
209 TopologicalSort(TopSorted);
210 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
211 std::back_inserter(Out), NotJoinNode);
212}
213
214// Find head of the toolchain corresponding to the given file.
215const Node* CompilationGraph::FindToolChain(const sys::Path& In) const {
216 const std::string& InLanguage = getLanguage(In);
217 const tools_vector_type& TV = getToolsVector(InLanguage);
218 if (TV.empty())
219 throw std::runtime_error("No toolchain corresponding to language"
220 + InLanguage + " found!");
221 return &getNode(ChooseEdge(TV)->ToolName());
222}
223
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000224// Build the targets. Command-line options are passed through
225// temporary variables.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000226int CompilationGraph::Build (const sys::Path& TempDir) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000227
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000228 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000229 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
230 E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000231 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000232
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000233 // Find the toolchain corresponding to this file.
234 const Node* N = FindToolChain(In);
235 // Pass file through the chain starting at head.
236 PassThroughGraph(In, N, TempDir);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000237 }
238
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000239 std::vector<const Node*> JTV;
240 TopologicalSortFilterJoinNodes(JTV);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000241
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000242 // For all join nodes in topological order:
243 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
244 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000245
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000246 sys::Path Out;
247 const Node* CurNode = *B;
248 JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
249 bool IsLast = false;
250
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000251 // Are there any files to be joined?
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000252 if (JT->JoinListEmpty())
253 continue;
254
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000255 // Is this the last tool in the chain?
256 // NOTE: we can process several chains in parallel.
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000257 if (!CurNode->HasChildren() || JT->IsLast()) {
258 if (OutputFilename.empty()) {
259 Out.set("a");
260 Out.appendSuffix(JT->OutputSuffix());
261 }
262 else
263 Out.set(OutputFilename);
264 IsLast = true;
265 }
266 else {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000267 Out = MakeTempFile(TempDir, "tmp", JT->OutputSuffix());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000268 }
269
270 if (JT->GenerateAction(Out).Execute() != 0)
271 throw std::runtime_error("Tool returned error code!");
272
273 if (!IsLast) {
274 const Node* NextNode = &getNode(ChooseEdge(CurNode->OutEdges,
275 CurNode->Name())->ToolName());
276 PassThroughGraph(Out, NextNode, TempDir);
277 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000278 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000279
280 return 0;
281}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000282
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000283// Code related to graph visualization.
284
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000285namespace llvm {
286 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000287 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000288 : public DefaultDOTGraphTraits
289 {
290
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000291 template<typename GraphType>
292 static std::string getNodeLabel(const Node* N, const GraphType&)
293 {
294 if (N->ToolPtr)
295 if (N->ToolPtr->IsJoin())
296 return N->Name() + "\n (join" +
297 (N->HasChildren() ? ")"
298 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
299 else
300 return N->Name();
301 else
302 return "root";
303 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000304
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000305 template<typename EdgeIter>
306 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
307 if (N->ToolPtr)
308 return N->ToolPtr->OutputLanguage();
309 else
310 return I->ToolPtr->InputLanguage();
311 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000312 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000313
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000314}
315
316void CompilationGraph::writeGraph() {
317 std::ofstream O("CompilationGraph.dot");
318
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000319 if (O.good()) {
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000320 llvm::WriteGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000321 O.close();
322 }
323 else {
324 throw std::runtime_error("");
325 }
326}
327
328void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000329 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000330}