blob: dece4e8e0ae33ceb4dfb06028fd1df9661813de5 [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 Glushenkov4a1a77c2008-09-22 20:50:40 +000014#include "llvm/CompilerDriver/CompilationGraph.h"
Mikhail Glushenkovf1881782009-03-02 09:01:14 +000015#include "llvm/CompilerDriver/Error.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 Glushenkovf8c430b2009-01-09 16:16:27 +000023#include <cstring>
24#include <iostream>
Mikhail Glushenkov02606582008-05-06 18:07:14 +000025#include <iterator>
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000026#include <limits>
Mikhail Glushenkov02606582008-05-06 18:07:14 +000027#include <queue>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000028#include <stdexcept>
29
30using namespace llvm;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000031using namespace llvmc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000032
33extern cl::list<std::string> InputFilenames;
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000034extern cl::list<std::string> Languages;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000035
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000036namespace llvmc {
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000037
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000038 const std::string& LanguageMap::GetLanguage(const sys::Path& File) const {
39 LanguageMap::const_iterator Lang = this->find(File.getSuffix());
40 if (Lang == this->end())
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000041 throw std::runtime_error("Unknown suffix: " + File.getSuffix());
42 return Lang->second;
43 }
44}
45
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000046namespace {
47
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000048 /// ChooseEdge - Return the edge with the maximum weight.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000049 template <class C>
50 const Edge* ChooseEdge(const C& EdgesContainer,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000051 const InputLanguagesSet& InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000052 const std::string& NodeName = "root") {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000053 const Edge* MaxEdge = 0;
54 unsigned MaxWeight = 0;
55 bool SingleMax = true;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000056
57 for (typename C::const_iterator B = EdgesContainer.begin(),
58 E = EdgesContainer.end(); B != E; ++B) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000059 const Edge* e = B->getPtr();
60 unsigned EW = e->Weight(InLangs);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000061 if (EW > MaxWeight) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000062 MaxEdge = e;
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000063 MaxWeight = EW;
64 SingleMax = true;
Mikhail Glushenkove0ff9ae2008-05-06 18:18:58 +000065 } else if (EW == MaxWeight) {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000066 SingleMax = false;
67 }
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000068 }
69
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000070 if (!SingleMax)
71 throw std::runtime_error("Node " + NodeName +
72 ": multiple maximal outward edges found!"
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000073 " Most probably a specification error.");
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000074 if (!MaxEdge)
75 throw std::runtime_error("Node " + NodeName +
76 ": no maximal outward edge found!"
77 " Most probably a specification error.");
78 return MaxEdge;
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000079 }
80
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000081}
82
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +000083void Node::AddEdge(Edge* Edg) {
84 // If there already was an edge between two nodes, modify it instead
85 // of adding a new edge.
86 const std::string& ToolName = Edg->ToolName();
87 for (container_type::iterator B = OutEdges.begin(), E = OutEdges.end();
88 B != E; ++B) {
89 if ((*B)->ToolName() == ToolName) {
90 llvm::IntrusiveRefCntPtr<Edge>(Edg).swap(*B);
91 return;
92 }
93 }
94 OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(Edg));
95}
96
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000097CompilationGraph::CompilationGraph() {
98 NodesMap["root"] = Node(this);
99}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000100
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000101Node& CompilationGraph::getNode(const std::string& ToolName) {
102 nodes_map_type::iterator I = NodesMap.find(ToolName);
103 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000104 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000105 return I->second;
106}
107
108const Node& CompilationGraph::getNode(const std::string& ToolName) const {
109 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
110 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000111 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000112 return I->second;
113}
114
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000115// Find the tools list corresponding to the given language name.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000116const CompilationGraph::tools_vector_type&
117CompilationGraph::getToolsVector(const std::string& LangName) const
118{
119 tools_map_type::const_iterator I = ToolsMap.find(LangName);
120 if (I == ToolsMap.end())
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000121 throw std::runtime_error("No tool corresponding to the language "
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000122 + LangName + " found");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000123 return I->second;
124}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000125
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000126void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +0000127 if (NodesMap.count(V->Name()) == 0)
128 NodesMap[V->Name()] = Node(this, V);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000129}
130
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000131void CompilationGraph::insertEdge(const std::string& A, Edge* Edg) {
132 Node& B = getNode(Edg->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000133 if (A == "root") {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000134 const char** InLangs = B.ToolPtr->InputLanguages();
135 for (;*InLangs; ++InLangs)
136 ToolsMap[*InLangs].push_back(IntrusiveRefCntPtr<Edge>(Edg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000137 NodesMap["root"].AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000138 }
139 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000140 Node& N = getNode(A);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000141 N.AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000142 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000143 // Increase the inward edge counter.
144 B.IncrInEdges();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000145}
146
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000147// Pass input file through the chain until we bump into a Join node or
148// a node that says that it is the last.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000149void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
150 const Node* StartNode,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000151 const InputLanguagesSet& InLangs,
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000152 const sys::Path& TempDir,
153 const LanguageMap& LangMap) const {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000154 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000155 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000156
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000157 while(true) {
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000158 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000159
160 if (CurTool->IsJoin()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000161 JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool);
162 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000163 break;
164 }
165
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000166 Action CurAction = CurTool->GenerateAction(In, CurNode->HasChildren(),
167 TempDir, InLangs, LangMap);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000168
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000169 if (int ret = CurAction.Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000170 throw error_code(ret);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000171
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000172 if (CurAction.StopCompilation())
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000173 return;
174
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000175 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000176 InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000177 CurNode->Name())->ToolName());
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000178 In = CurAction.OutFile();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000179 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000180}
181
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000182// Find the head of the toolchain corresponding to the given file.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000183// Also, insert an input language into InLangs.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000184const Node* CompilationGraph::
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000185FindToolChain(const sys::Path& In, const std::string* ForceLanguage,
186 InputLanguagesSet& InLangs, const LanguageMap& LangMap) const {
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000187
188 // Determine the input language.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000189 const std::string& InLanguage =
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000190 ForceLanguage ? *ForceLanguage : LangMap.GetLanguage(In);
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000191
192 // Add the current input language to the input language set.
193 InLangs.insert(InLanguage);
194
195 // Find the toolchain for the input language.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000196 const tools_vector_type& TV = getToolsVector(InLanguage);
197 if (TV.empty())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000198 throw std::runtime_error("No toolchain corresponding to language "
199 + InLanguage + " found");
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000200 return &getNode(ChooseEdge(TV, InLangs)->ToolName());
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000201}
202
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000203// Helper function used by Build().
204// Traverses initial portions of the toolchains (up to the first Join node).
205// This function is also responsible for handling the -x option.
206void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000207 const sys::Path& TempDir,
208 const LanguageMap& LangMap) {
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000209 // This is related to -x option handling.
210 cl::list<std::string>::const_iterator xIter = Languages.begin(),
211 xBegin = xIter, xEnd = Languages.end();
212 bool xEmpty = true;
213 const std::string* xLanguage = 0;
214 unsigned xPos = 0, xPosNext = 0, filePos = 0;
215
216 if (xIter != xEnd) {
217 xEmpty = false;
218 xPos = Languages.getPosition(xIter - xBegin);
219 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
220 xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max()
221 : Languages.getPosition(xNext - xBegin);
222 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
223 }
224
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000225 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000226 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000227 CB = B, E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000228 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000229
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000230 // Code for handling the -x option.
231 // Output: std::string* xLanguage (can be NULL).
232 if (!xEmpty) {
233 filePos = InputFilenames.getPosition(B - CB);
234
235 if (xPos < filePos) {
236 if (filePos < xPosNext) {
237 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
238 }
239 else { // filePos >= xPosNext
240 // Skip xIters while filePos > xPosNext
241 while (filePos > xPosNext) {
242 ++xIter;
243 xPos = xPosNext;
244
245 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
246 if (xNext == xEnd)
247 xPosNext = std::numeric_limits<unsigned>::max();
248 else
249 xPosNext = Languages.getPosition(xNext - xBegin);
250 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
251 }
252 }
253 }
254 }
255
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000256 // Find the toolchain corresponding to this file.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000257 const Node* N = FindToolChain(In, xLanguage, InLangs, LangMap);
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000258 // Pass file through the chain starting at head.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000259 PassThroughGraph(In, N, InLangs, TempDir, LangMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000260 }
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000261}
262
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000263// Sort the nodes in topological order.
264void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
265 std::queue<const Node*> Q;
266 Q.push(&getNode("root"));
267
268 while (!Q.empty()) {
269 const Node* A = Q.front();
270 Q.pop();
271 Out.push_back(A);
272 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
273 EB != EE; ++EB) {
274 Node* B = &getNode((*EB)->ToolName());
275 B->DecrInEdges();
276 if (B->HasNoInEdges())
277 Q.push(B);
278 }
279 }
280}
281
282namespace {
283 bool NotJoinNode(const Node* N) {
284 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
285 }
286}
287
288// Call TopologicalSort and filter the resulting list to include
289// only Join nodes.
290void CompilationGraph::
291TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
292 std::vector<const Node*> TopSorted;
293 TopologicalSort(TopSorted);
294 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
295 std::back_inserter(Out), NotJoinNode);
296}
297
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000298int CompilationGraph::Build (const sys::Path& TempDir,
299 const LanguageMap& LangMap) {
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000300
301 InputLanguagesSet InLangs;
302
303 // Traverse initial parts of the toolchains and fill in InLangs.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000304 BuildInitial(InLangs, TempDir, LangMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000305
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000306 std::vector<const Node*> JTV;
307 TopologicalSortFilterJoinNodes(JTV);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000308
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000309 // For all join nodes in topological order:
310 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
311 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000312
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000313 const Node* CurNode = *B;
314 JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000315
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000316 // Are there any files in the join list?
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000317 if (JT->JoinListEmpty())
318 continue;
319
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000320 Action CurAction = JT->GenerateAction(CurNode->HasChildren(),
321 TempDir, InLangs, LangMap);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000322
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000323 if (int ret = CurAction.Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000324 throw error_code(ret);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000325
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000326 if (CurAction.StopCompilation())
327 return 0;
328
Mikhail Glushenkovb677df82008-12-07 16:45:37 +0000329 const Node* NextNode = &getNode(ChooseEdge(CurNode->OutEdges, InLangs,
330 CurNode->Name())->ToolName());
331 PassThroughGraph(sys::Path(CurAction.OutFile()), NextNode,
332 InLangs, TempDir, LangMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000333 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000334
335 return 0;
336}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000337
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000338int CompilationGraph::CheckLanguageNames() const {
339 int ret = 0;
340 // Check that names for output and input languages on all edges do match.
341 for (const_nodes_iterator B = this->NodesMap.begin(),
342 E = this->NodesMap.end(); B != E; ++B) {
343
344 const Node & N1 = B->second;
345 if (N1.ToolPtr) {
346 for (Node::const_iterator EB = N1.EdgesBegin(), EE = N1.EdgesEnd();
347 EB != EE; ++EB) {
348 const Node& N2 = this->getNode((*EB)->ToolName());
349
350 if (!N2.ToolPtr) {
351 ++ret;
352 std::cerr << "Error: there is an edge from '" << N1.ToolPtr->Name()
353 << "' back to the root!\n\n";
354 continue;
355 }
356
357 const char* OutLang = N1.ToolPtr->OutputLanguage();
358 const char** InLangs = N2.ToolPtr->InputLanguages();
359 bool eq = false;
360 for (;*InLangs; ++InLangs) {
361 if (std::strcmp(OutLang, *InLangs) == 0) {
362 eq = true;
363 break;
364 }
365 }
366
367 if (!eq) {
368 ++ret;
369 std::cerr << "Error: Output->input language mismatch in the edge '" <<
370 N1.ToolPtr->Name() << "' -> '" << N2.ToolPtr->Name() << "'!\n";
371
372 std::cerr << "Expected one of { ";
373
374 InLangs = N2.ToolPtr->InputLanguages();
375 for (;*InLangs; ++InLangs) {
376 std::cerr << '\'' << *InLangs << (*(InLangs+1) ? "', " : "'");
377 }
378
379 std::cerr << " }, but got '" << OutLang << "'!\n\n";
380 }
381
382 }
383 }
384 }
385
386 return ret;
387}
388
389int CompilationGraph::CheckMultipleDefaultEdges() const {
390 int ret = 0;
391 InputLanguagesSet Dummy;
392
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000393 // For all nodes, just iterate over the outgoing edges and check if there is
394 // more than one edge with maximum weight.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000395 for (const_nodes_iterator B = this->NodesMap.begin(),
396 E = this->NodesMap.end(); B != E; ++B) {
397 const Node& N = B->second;
398 unsigned MaxWeight = 0;
399
400 // Ignore the root node.
401 if (!N.ToolPtr)
402 continue;
403
404 for (Node::const_iterator EB = N.EdgesBegin(), EE = N.EdgesEnd();
405 EB != EE; ++EB) {
406 unsigned EdgeWeight = (*EB)->Weight(Dummy);
407 if (EdgeWeight > MaxWeight) {
408 MaxWeight = EdgeWeight;
409 }
410 else if (EdgeWeight == MaxWeight) {
411 ++ret;
412 std::cerr
413 << "Error: there are multiple maximal edges stemming from the '"
414 << N.ToolPtr->Name() << "' node!\n\n";
415 break;
416 }
417 }
418 }
419
420 return ret;
421}
422
423int CompilationGraph::CheckCycles() {
424 unsigned deleted = 0;
425 std::queue<Node*> Q;
426 Q.push(&getNode("root"));
427
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000428 // Try to delete all nodes that have no ingoing edges, starting from the
429 // root. If there are any nodes left after this operation, then we have a
430 // cycle. This relies on '--check-graph' not performing the topological sort.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000431 while (!Q.empty()) {
432 Node* A = Q.front();
433 Q.pop();
434 ++deleted;
435
436 for (Node::iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
437 EB != EE; ++EB) {
438 Node* B = &getNode((*EB)->ToolName());
439 B->DecrInEdges();
440 if (B->HasNoInEdges())
441 Q.push(B);
442 }
443 }
444
445 if (deleted != NodesMap.size()) {
446 std::cerr << "Error: there are cycles in the compilation graph!\n"
447 << "Try inspecting the diagram produced by "
448 "'llvmc --view-graph'.\n\n";
449 return 1;
450 }
451
452 return 0;
453}
454
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000455int CompilationGraph::Check () {
456 // We try to catch as many errors as we can in one go.
457 int ret = 0;
458
459 // Check that output/input language names match.
460 ret += this->CheckLanguageNames();
461
462 // Check for multiple default edges.
463 ret += this->CheckMultipleDefaultEdges();
464
465 // Check for cycles.
466 ret += this->CheckCycles();
467
468 return ret;
469}
470
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000471// Code related to graph visualization.
472
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000473namespace llvm {
474 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000475 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000476 : public DefaultDOTGraphTraits
477 {
478
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000479 template<typename GraphType>
480 static std::string getNodeLabel(const Node* N, const GraphType&)
481 {
482 if (N->ToolPtr)
483 if (N->ToolPtr->IsJoin())
484 return N->Name() + "\n (join" +
485 (N->HasChildren() ? ")"
486 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
487 else
488 return N->Name();
489 else
490 return "root";
491 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000492
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000493 template<typename EdgeIter>
494 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000495 if (N->ToolPtr) {
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000496 return N->ToolPtr->OutputLanguage();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000497 }
498 else {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000499 const char** InLangs = I->ToolPtr->InputLanguages();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000500 std::string ret;
501
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000502 for (; *InLangs; ++InLangs) {
503 if (*(InLangs + 1)) {
504 ret += *InLangs;
505 ret += ", ";
506 }
507 else {
508 ret += *InLangs;
509 }
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000510 }
511
512 return ret;
513 }
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000514 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000515 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000516
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000517}
518
Mikhail Glushenkovd50d32b2009-03-27 12:57:14 +0000519void CompilationGraph::writeGraph(const std::string& OutputFilename) {
520 std::ofstream O(OutputFilename.c_str());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000521
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000522 if (O.good()) {
Mikhail Glushenkovd50d32b2009-03-27 12:57:14 +0000523 std::cerr << "Writing '"<< OutputFilename << "' file...";
Mikhail Glushenkov3cd3c722009-03-26 21:23:48 +0000524 llvm::WriteGraph(O, this);
525 std::cerr << "done.\n";
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000526 O.close();
527 }
528 else {
Mikhail Glushenkovd50d32b2009-03-27 12:57:14 +0000529 throw std::runtime_error("Error opening file '" + OutputFilename
530 + "' for writing!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000531 }
532}
533
534void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000535 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000536}