blob: 817e0fb4c6b1ec5add4b616c622a70953ed9e002 [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 Glushenkov2ba4c5a2008-05-06 17:25:51 +0000124// Pass input file through the chain until we bump into a Join node or
125// a node that says that it is the last.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000126const JoinTool*
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000127CompilationGraph::PassThroughGraph (sys::Path& In,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000128 const Node* StartNode,
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000129 const sys::Path& TempDir) const {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000130 bool Last = false;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000131 const Node* CurNode = StartNode;
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000132 JoinTool* ret = 0;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000133
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000134 while(!Last) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000135 sys::Path Out;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000136 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000137
138 if (CurTool->IsJoin()) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000139 ret = &dynamic_cast<JoinTool&>(*CurTool);
140 ret->AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000141 break;
142 }
143
144 // Is this the last tool?
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000145 if (!CurNode->HasChildren() || CurTool->IsLast()) {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000146 // Check if the first tool is also the last
147 if (Out.empty())
148 Out.set(In.getBasename());
149 else
150 Out.appendComponent(In.getBasename());
151 Out.appendSuffix(CurTool->OutputSuffix());
152 Last = true;
153 }
154 else {
155 Out = TempDir;
156 Out.appendComponent(In.getBasename());
157 Out.appendSuffix(CurTool->OutputSuffix());
158 Out.makeUnique(true, NULL);
159 Out.eraseFromDisk();
160 }
161
162 if (CurTool->GenerateAction(In, Out).Execute() != 0)
163 throw std::runtime_error("Tool returned error code!");
164
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000165 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
166 CurNode->Name())->ToolName());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000167 In = Out; Out.clear();
168 }
169
170 return ret;
171}
172
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000173// Sort the nodes in topological order.
174void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
175 std::queue<const Node*> Q;
176 Q.push(&getNode("root"));
177
178 while (!Q.empty()) {
179 const Node* A = Q.front();
180 Q.pop();
181 Out.push_back(A);
182 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
183 EB != EE; ++EB) {
184 Node* B = &getNode((*EB)->ToolName());
185 B->DecrInEdges();
186 if (B->HasNoInEdges())
187 Q.push(B);
188 }
189 }
190}
191
192namespace {
193 bool NotJoinNode(const Node* N) {
194 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
195 }
196}
197
198// Call TopologicalSort and filter the resulting list to include
199// only Join nodes.
200void CompilationGraph::
201TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
202 std::vector<const Node*> TopSorted;
203 TopologicalSort(TopSorted);
204 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
205 std::back_inserter(Out), NotJoinNode);
206}
207
208// Find head of the toolchain corresponding to the given file.
209const Node* CompilationGraph::FindToolChain(const sys::Path& In) const {
210 const std::string& InLanguage = getLanguage(In);
211 const tools_vector_type& TV = getToolsVector(InLanguage);
212 if (TV.empty())
213 throw std::runtime_error("No toolchain corresponding to language"
214 + InLanguage + " found!");
215 return &getNode(ChooseEdge(TV)->ToolName());
216}
217
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000218// TOFIX: merge some parts with PassThroughGraph.
219// Build the targets. Command-line options are passed through
220// temporary variables.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000221int CompilationGraph::Build (const sys::Path& TempDir) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000222
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000223 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000224 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
225 E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000226 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000227
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000228 // Find the toolchain corresponding to this file.
229 const Node* N = FindToolChain(In);
230 // Pass file through the chain starting at head.
231 PassThroughGraph(In, N, TempDir);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000232 }
233
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000234 std::vector<const Node*> JTV;
235 TopologicalSortFilterJoinNodes(JTV);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000236
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000237 // For all join nodes in topological order:
238 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
239 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000240
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000241 sys::Path Out;
242 const Node* CurNode = *B;
243 JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
244 bool IsLast = false;
245
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000246 // Has files pending?
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000247 if (JT->JoinListEmpty())
248 continue;
249
250 if (!CurNode->HasChildren() || JT->IsLast()) {
251 if (OutputFilename.empty()) {
252 Out.set("a");
253 Out.appendSuffix(JT->OutputSuffix());
254 }
255 else
256 Out.set(OutputFilename);
257 IsLast = true;
258 }
259 else {
260 Out = TempDir;
261 Out.appendComponent("tmp");
262 Out.appendSuffix(JT->OutputSuffix());
263 Out.makeUnique(true, NULL);
264 Out.eraseFromDisk();
265 }
266
267 if (JT->GenerateAction(Out).Execute() != 0)
268 throw std::runtime_error("Tool returned error code!");
269
270 if (!IsLast) {
271 const Node* NextNode = &getNode(ChooseEdge(CurNode->OutEdges,
272 CurNode->Name())->ToolName());
273 PassThroughGraph(Out, NextNode, TempDir);
274 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000275 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000276
277 return 0;
278}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000279
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000280// Code related to graph visualization.
281
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000282namespace llvm {
283 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000284 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000285 : public DefaultDOTGraphTraits
286 {
287
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000288 template<typename GraphType>
289 static std::string getNodeLabel(const Node* N, const GraphType&)
290 {
291 if (N->ToolPtr)
292 if (N->ToolPtr->IsJoin())
293 return N->Name() + "\n (join" +
294 (N->HasChildren() ? ")"
295 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
296 else
297 return N->Name();
298 else
299 return "root";
300 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000301
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000302 template<typename EdgeIter>
303 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
304 if (N->ToolPtr)
305 return N->ToolPtr->OutputLanguage();
306 else
307 return I->ToolPtr->InputLanguage();
308 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000309 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000310
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000311}
312
313void CompilationGraph::writeGraph() {
314 std::ofstream O("CompilationGraph.dot");
315
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000316 if (O.good()) {
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000317 llvm::WriteGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000318 O.close();
319 }
320 else {
321 throw std::runtime_error("");
322 }
323}
324
325void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000326 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000327}