blob: 838b94050131c1546248fa7deeb00f4f0bc32092 [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 Glushenkova6730372008-05-12 16:31:42 +000014#include "Error.h"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +000015#include "llvm/CompilerDriver/CompilationGraph.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000016
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000017#include "llvm/ADT/STLExtras.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000018#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000019#include "llvm/Support/DOTGraphTraits.h"
20#include "llvm/Support/GraphWriter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000021
Mikhail Glushenkov02606582008-05-06 18:07:14 +000022#include <algorithm>
Mikhail Glushenkov02606582008-05-06 18:07:14 +000023#include <iterator>
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000024#include <limits>
Mikhail Glushenkov02606582008-05-06 18:07:14 +000025#include <queue>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000026#include <stdexcept>
27
28using namespace llvm;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000029using namespace llvmc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000030
31extern cl::list<std::string> InputFilenames;
32extern cl::opt<std::string> OutputFilename;
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000033extern cl::list<std::string> Languages;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000034
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000035namespace llvmc {
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000036
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000037 const std::string& LanguageMap::GetLanguage(const sys::Path& File) const {
38 LanguageMap::const_iterator Lang = this->find(File.getSuffix());
39 if (Lang == this->end())
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000040 throw std::runtime_error("Unknown suffix: " + File.getSuffix());
41 return Lang->second;
42 }
43}
44
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000045namespace {
46
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000047 /// ChooseEdge - Return the edge with the maximum weight.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000048 template <class C>
49 const Edge* ChooseEdge(const C& EdgesContainer,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000050 const InputLanguagesSet& InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000051 const std::string& NodeName = "root") {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000052 const Edge* MaxEdge = 0;
53 unsigned MaxWeight = 0;
54 bool SingleMax = true;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000055
56 for (typename C::const_iterator B = EdgesContainer.begin(),
57 E = EdgesContainer.end(); B != E; ++B) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000058 const Edge* e = B->getPtr();
59 unsigned EW = e->Weight(InLangs);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000060 if (EW > MaxWeight) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000061 MaxEdge = e;
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000062 MaxWeight = EW;
63 SingleMax = true;
Mikhail Glushenkove0ff9ae2008-05-06 18:18:58 +000064 } else if (EW == MaxWeight) {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000065 SingleMax = false;
66 }
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000067 }
68
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000069 if (!SingleMax)
70 throw std::runtime_error("Node " + NodeName +
71 ": multiple maximal outward edges found!"
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000072 " Most probably a specification error.");
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000073 if (!MaxEdge)
74 throw std::runtime_error("Node " + NodeName +
75 ": no maximal outward edge found!"
76 " Most probably a specification error.");
77 return MaxEdge;
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000078 }
79
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000080}
81
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000082CompilationGraph::CompilationGraph() {
83 NodesMap["root"] = Node(this);
84}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000085
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000086Node& CompilationGraph::getNode(const std::string& ToolName) {
87 nodes_map_type::iterator I = NodesMap.find(ToolName);
88 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000089 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000090 return I->second;
91}
92
93const Node& CompilationGraph::getNode(const std::string& ToolName) const {
94 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
95 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000096 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000097 return I->second;
98}
99
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000100// Find the tools list corresponding to the given language name.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000101const CompilationGraph::tools_vector_type&
102CompilationGraph::getToolsVector(const std::string& LangName) const
103{
104 tools_map_type::const_iterator I = ToolsMap.find(LangName);
105 if (I == ToolsMap.end())
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000106 throw std::runtime_error("No tool corresponding to the language "
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000107 + LangName + " found");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000108 return I->second;
109}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000110
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000111void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000112 if (NodesMap.count(V->Name()) == 0) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000113 Node N;
114 N.OwningGraph = this;
115 N.ToolPtr = V;
116 NodesMap[V->Name()] = N;
117 }
118}
119
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000120void CompilationGraph::insertEdge(const std::string& A, Edge* Edg) {
121 Node& B = getNode(Edg->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000122 if (A == "root") {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000123 const char** InLangs = B.ToolPtr->InputLanguages();
124 for (;*InLangs; ++InLangs)
125 ToolsMap[*InLangs].push_back(IntrusiveRefCntPtr<Edge>(Edg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000126 NodesMap["root"].AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000127 }
128 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000129 Node& N = getNode(A);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000130 N.AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000131 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000132 // Increase the inward edge counter.
133 B.IncrInEdges();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000134}
135
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000136namespace {
137 sys::Path MakeTempFile(const sys::Path& TempDir, const std::string& BaseName,
138 const std::string& Suffix) {
Mikhail Glushenkov73296102008-05-30 06:29:17 +0000139 sys::Path Out;
140
141 // Make sure we don't end up with path names like '/file.o' if the
142 // TempDir is empty.
143 if (TempDir.empty()) {
144 Out.set(BaseName);
145 }
146 else {
147 Out = TempDir;
148 Out.appendComponent(BaseName);
149 }
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000150 Out.appendSuffix(Suffix);
Mikhail Glushenkov74a0c282008-05-30 19:56:27 +0000151 // NOTE: makeUnique always *creates* a unique temporary file,
152 // which is good, since there will be no races. However, some
153 // tools do not like it when the output file already exists, so
154 // they have to be placated with -f or something like that.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000155 Out.makeUnique(true, NULL);
156 return Out;
157 }
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000158}
159
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000160// Pass input file through the chain until we bump into a Join node or
161// a node that says that it is the last.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000162void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
163 const Node* StartNode,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000164 const InputLanguagesSet& InLangs,
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000165 const sys::Path& TempDir,
166 const LanguageMap& LangMap) const {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000167 bool Last = false;
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000168 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000169 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000170
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000171 while(!Last) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000172 sys::Path Out;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000173 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000174
175 if (CurTool->IsJoin()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000176 JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool);
177 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000178 break;
179 }
180
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000181 // Since toolchains do not have to end with a Join node, we should
182 // check if this Node is the last.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000183 if (!CurNode->HasChildren() || CurTool->IsLast()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000184 if (!OutputFilename.empty()) {
185 Out.set(OutputFilename);
186 }
187 else {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000188 Out.set(In.getBasename());
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000189 Out.appendSuffix(CurTool->OutputSuffix());
190 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000191 Last = true;
192 }
193 else {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000194 Out = MakeTempFile(TempDir, In.getBasename(), CurTool->OutputSuffix());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000195 }
196
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000197 if (int ret = CurTool->GenerateAction(In, Out, InLangs, LangMap).Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000198 throw error_code(ret);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000199
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000200 if (Last)
201 return;
202
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000203 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000204 InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000205 CurNode->Name())->ToolName());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000206 In = Out; Out.clear();
207 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000208}
209
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000210// Find the head of the toolchain corresponding to the given file.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000211// Also, insert an input language into InLangs.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000212const Node* CompilationGraph::
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000213FindToolChain(const sys::Path& In, const std::string* ForceLanguage,
214 InputLanguagesSet& InLangs, const LanguageMap& LangMap) const {
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000215
216 // Determine the input language.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000217 const std::string& InLanguage =
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000218 ForceLanguage ? *ForceLanguage : LangMap.GetLanguage(In);
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000219
220 // Add the current input language to the input language set.
221 InLangs.insert(InLanguage);
222
223 // Find the toolchain for the input language.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000224 const tools_vector_type& TV = getToolsVector(InLanguage);
225 if (TV.empty())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000226 throw std::runtime_error("No toolchain corresponding to language "
227 + InLanguage + " found");
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000228 return &getNode(ChooseEdge(TV, InLangs)->ToolName());
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000229}
230
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000231// Helper function used by Build().
232// Traverses initial portions of the toolchains (up to the first Join node).
233// This function is also responsible for handling the -x option.
234void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000235 const sys::Path& TempDir,
236 const LanguageMap& LangMap) {
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 Glushenkov11a353a2008-09-22 20:47:46 +0000285 const Node* N = FindToolChain(In, xLanguage, InLangs, LangMap);
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000286 // Pass file through the chain starting at head.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000287 PassThroughGraph(In, N, InLangs, TempDir, LangMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000288 }
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000289}
290
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000291// Sort the nodes in topological order.
292void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
293 std::queue<const Node*> Q;
294 Q.push(&getNode("root"));
295
296 while (!Q.empty()) {
297 const Node* A = Q.front();
298 Q.pop();
299 Out.push_back(A);
300 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
301 EB != EE; ++EB) {
302 Node* B = &getNode((*EB)->ToolName());
303 B->DecrInEdges();
304 if (B->HasNoInEdges())
305 Q.push(B);
306 }
307 }
308}
309
310namespace {
311 bool NotJoinNode(const Node* N) {
312 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
313 }
314}
315
316// Call TopologicalSort and filter the resulting list to include
317// only Join nodes.
318void CompilationGraph::
319TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
320 std::vector<const Node*> TopSorted;
321 TopologicalSort(TopSorted);
322 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
323 std::back_inserter(Out), NotJoinNode);
324}
325
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000326int CompilationGraph::Build (const sys::Path& TempDir,
327 const LanguageMap& LangMap) {
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000328
329 InputLanguagesSet InLangs;
330
331 // Traverse initial parts of the toolchains and fill in InLangs.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000332 BuildInitial(InLangs, TempDir, LangMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000333
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000334 std::vector<const Node*> JTV;
335 TopologicalSortFilterJoinNodes(JTV);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000336
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000337 // For all join nodes in topological order:
338 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
339 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000340
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000341 sys::Path Out;
342 const Node* CurNode = *B;
343 JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
344 bool IsLast = false;
345
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000346 // Are there any files in the join list?
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000347 if (JT->JoinListEmpty())
348 continue;
349
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000350 // Is this the last tool in the toolchain?
351 // NOTE: we can process several toolchains in parallel.
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000352 if (!CurNode->HasChildren() || JT->IsLast()) {
353 if (OutputFilename.empty()) {
354 Out.set("a");
355 Out.appendSuffix(JT->OutputSuffix());
356 }
357 else
358 Out.set(OutputFilename);
359 IsLast = true;
360 }
361 else {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000362 Out = MakeTempFile(TempDir, "tmp", JT->OutputSuffix());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000363 }
364
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000365 if (int ret = JT->GenerateAction(Out, InLangs, LangMap).Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000366 throw error_code(ret);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000367
368 if (!IsLast) {
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000369 const Node* NextNode =
370 &getNode(ChooseEdge(CurNode->OutEdges, InLangs,
371 CurNode->Name())->ToolName());
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000372 PassThroughGraph(Out, NextNode, InLangs, TempDir, LangMap);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000373 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000374 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000375
376 return 0;
377}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000378
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000379// Code related to graph visualization.
380
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000381namespace llvm {
382 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000383 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000384 : public DefaultDOTGraphTraits
385 {
386
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000387 template<typename GraphType>
388 static std::string getNodeLabel(const Node* N, const GraphType&)
389 {
390 if (N->ToolPtr)
391 if (N->ToolPtr->IsJoin())
392 return N->Name() + "\n (join" +
393 (N->HasChildren() ? ")"
394 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
395 else
396 return N->Name();
397 else
398 return "root";
399 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000400
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000401 template<typename EdgeIter>
402 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000403 if (N->ToolPtr) {
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000404 return N->ToolPtr->OutputLanguage();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000405 }
406 else {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000407 const char** InLangs = I->ToolPtr->InputLanguages();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000408 std::string ret;
409
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000410 for (; *InLangs; ++InLangs) {
411 if (*(InLangs + 1)) {
412 ret += *InLangs;
413 ret += ", ";
414 }
415 else {
416 ret += *InLangs;
417 }
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000418 }
419
420 return ret;
421 }
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000422 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000423 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000424
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000425}
426
427void CompilationGraph::writeGraph() {
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000428 std::ofstream O("compilation-graph.dot");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000429
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000430 if (O.good()) {
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000431 llvm::WriteGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000432 O.close();
433 }
434 else {
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000435 throw std::runtime_error("Error opening file 'compilation-graph.dot'"
436 " for writing!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000437 }
438}
439
440void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000441 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000442}