blob: a36d3fb692a7f9b6ce1a56b495d8104479fb8c54 [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 Glushenkov7defa2d2009-06-25 18:20:10 +000014#include "llvm/CompilerDriver/BuiltinOptions.h"
Mikhail Glushenkov4a1a77c2008-09-22 20:50:40 +000015#include "llvm/CompilerDriver/CompilationGraph.h"
Mikhail Glushenkovf1881782009-03-02 09:01:14 +000016#include "llvm/CompilerDriver/Error.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000017
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000018#include "llvm/ADT/STLExtras.h"
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000019#include "llvm/Support/DOTGraphTraits.h"
20#include "llvm/Support/GraphWriter.h"
Bill Wendling9cdd4f52009-06-30 04:07:12 +000021#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000022
Mikhail Glushenkov02606582008-05-06 18:07:14 +000023#include <algorithm>
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +000024#include <cstring>
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
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000033namespace llvmc {
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000034
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000035 const std::string& LanguageMap::GetLanguage(const sys::Path& File) const {
Mikhail Glushenkov21e099a2010-01-26 14:55:16 +000036 StringRef suf = File.getSuffix();
37 LanguageMap::const_iterator Lang = this->find(suf);
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +000038 if (Lang == this->end())
Mikhail Glushenkov21e099a2010-01-26 14:55:16 +000039 throw std::runtime_error("File '" + File.str() +
40 "' has unknown suffix '" + suf.str() + '\'');
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000041 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 Glushenkov7e6d70a2008-11-26 22:59:45 +000082void Node::AddEdge(Edge* Edg) {
83 // If there already was an edge between two nodes, modify it instead
84 // of adding a new edge.
85 const std::string& ToolName = Edg->ToolName();
86 for (container_type::iterator B = OutEdges.begin(), E = OutEdges.end();
87 B != E; ++B) {
88 if ((*B)->ToolName() == ToolName) {
89 llvm::IntrusiveRefCntPtr<Edge>(Edg).swap(*B);
90 return;
91 }
92 }
93 OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(Edg));
94}
95
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000096CompilationGraph::CompilationGraph() {
97 NodesMap["root"] = Node(this);
98}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000099
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000100Node& CompilationGraph::getNode(const std::string& ToolName) {
101 nodes_map_type::iterator I = NodesMap.find(ToolName);
102 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000103 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000104 return I->second;
105}
106
107const Node& CompilationGraph::getNode(const std::string& ToolName) const {
108 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
109 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000110 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000111 return I->second;
112}
113
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000114// Find the tools list corresponding to the given language name.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000115const CompilationGraph::tools_vector_type&
116CompilationGraph::getToolsVector(const std::string& LangName) const
117{
118 tools_map_type::const_iterator I = ToolsMap.find(LangName);
119 if (I == ToolsMap.end())
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000120 throw std::runtime_error("No tool corresponding to the language "
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000121 + LangName + " found");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000122 return I->second;
123}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000124
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000125void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +0000126 if (NodesMap.count(V->Name()) == 0)
127 NodesMap[V->Name()] = Node(this, V);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000128}
129
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000130void CompilationGraph::insertEdge(const std::string& A, Edge* Edg) {
131 Node& B = getNode(Edg->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000132 if (A == "root") {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000133 const char** InLangs = B.ToolPtr->InputLanguages();
134 for (;*InLangs; ++InLangs)
135 ToolsMap[*InLangs].push_back(IntrusiveRefCntPtr<Edge>(Edg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000136 NodesMap["root"].AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000137 }
138 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000139 Node& N = getNode(A);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000140 N.AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000141 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000142 // Increase the inward edge counter.
143 B.IncrInEdges();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000144}
145
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000146// Pass input file through the chain until we bump into a Join node or
147// a node that says that it is the last.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000148void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
149 const Node* StartNode,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000150 const InputLanguagesSet& InLangs,
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000151 const sys::Path& TempDir,
152 const LanguageMap& LangMap) const {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000153 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000154 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000155
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000156 while(true) {
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000157 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000158
159 if (CurTool->IsJoin()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000160 JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool);
161 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000162 break;
163 }
164
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000165 Action CurAction = CurTool->GenerateAction(In, CurNode->HasChildren(),
166 TempDir, InLangs, LangMap);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000167
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000168 if (int ret = CurAction.Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000169 throw error_code(ret);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000170
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000171 if (CurAction.StopCompilation())
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000172 return;
173
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000174 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000175 InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000176 CurNode->Name())->ToolName());
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000177 In = CurAction.OutFile();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000178 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000179}
180
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000181// Find the head of the toolchain corresponding to the given file.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000182// Also, insert an input language into InLangs.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000183const Node* CompilationGraph::
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000184FindToolChain(const sys::Path& In, const std::string* ForceLanguage,
185 InputLanguagesSet& InLangs, const LanguageMap& LangMap) const {
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000186
187 // Determine the input language.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000188 const std::string& InLanguage =
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000189 ForceLanguage ? *ForceLanguage : LangMap.GetLanguage(In);
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000190
191 // Add the current input language to the input language set.
192 InLangs.insert(InLanguage);
193
194 // Find the toolchain for the input language.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000195 const tools_vector_type& TV = getToolsVector(InLanguage);
196 if (TV.empty())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000197 throw std::runtime_error("No toolchain corresponding to language "
198 + InLanguage + " found");
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000199 return &getNode(ChooseEdge(TV, InLangs)->ToolName());
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000200}
201
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000202// Helper function used by Build().
203// Traverses initial portions of the toolchains (up to the first Join node).
204// This function is also responsible for handling the -x option.
205void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000206 const sys::Path& TempDir,
207 const LanguageMap& LangMap) {
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000208 // This is related to -x option handling.
209 cl::list<std::string>::const_iterator xIter = Languages.begin(),
210 xBegin = xIter, xEnd = Languages.end();
211 bool xEmpty = true;
212 const std::string* xLanguage = 0;
213 unsigned xPos = 0, xPosNext = 0, filePos = 0;
214
215 if (xIter != xEnd) {
216 xEmpty = false;
217 xPos = Languages.getPosition(xIter - xBegin);
218 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
219 xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max()
220 : Languages.getPosition(xNext - xBegin);
221 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
222 }
223
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000224 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000225 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000226 CB = B, E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000227 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000228
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000229 // Code for handling the -x option.
230 // Output: std::string* xLanguage (can be NULL).
231 if (!xEmpty) {
232 filePos = InputFilenames.getPosition(B - CB);
233
234 if (xPos < filePos) {
235 if (filePos < xPosNext) {
236 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
237 }
238 else { // filePos >= xPosNext
239 // Skip xIters while filePos > xPosNext
240 while (filePos > xPosNext) {
241 ++xIter;
242 xPos = xPosNext;
243
244 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
245 if (xNext == xEnd)
246 xPosNext = std::numeric_limits<unsigned>::max();
247 else
248 xPosNext = Languages.getPosition(xNext - xBegin);
249 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
250 }
251 }
252 }
253 }
254
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000255 // Find the toolchain corresponding to this file.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000256 const Node* N = FindToolChain(In, xLanguage, InLangs, LangMap);
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000257 // Pass file through the chain starting at head.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000258 PassThroughGraph(In, N, InLangs, TempDir, LangMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000259 }
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000260}
261
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000262// Sort the nodes in topological order.
263void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
264 std::queue<const Node*> Q;
265 Q.push(&getNode("root"));
266
267 while (!Q.empty()) {
268 const Node* A = Q.front();
269 Q.pop();
270 Out.push_back(A);
271 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
272 EB != EE; ++EB) {
273 Node* B = &getNode((*EB)->ToolName());
274 B->DecrInEdges();
275 if (B->HasNoInEdges())
276 Q.push(B);
277 }
278 }
279}
280
281namespace {
282 bool NotJoinNode(const Node* N) {
283 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
284 }
285}
286
287// Call TopologicalSort and filter the resulting list to include
288// only Join nodes.
289void CompilationGraph::
290TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
291 std::vector<const Node*> TopSorted;
292 TopologicalSort(TopSorted);
293 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
294 std::back_inserter(Out), NotJoinNode);
295}
296
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000297int CompilationGraph::Build (const sys::Path& TempDir,
298 const LanguageMap& LangMap) {
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000299
300 InputLanguagesSet InLangs;
301
302 // Traverse initial parts of the toolchains and fill in InLangs.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000303 BuildInitial(InLangs, TempDir, LangMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000304
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000305 std::vector<const Node*> JTV;
306 TopologicalSortFilterJoinNodes(JTV);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000307
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000308 // For all join nodes in topological order:
309 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
310 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000311
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000312 const Node* CurNode = *B;
313 JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000314
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000315 // Are there any files in the join list?
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000316 if (JT->JoinListEmpty() && !(JT->WorksOnEmpty() && InputFilenames.empty()))
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000317 continue;
318
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000319 Action CurAction = JT->GenerateAction(CurNode->HasChildren(),
320 TempDir, InLangs, LangMap);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000321
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000322 if (int ret = CurAction.Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000323 throw error_code(ret);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000324
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000325 if (CurAction.StopCompilation())
326 return 0;
327
Mikhail Glushenkovb677df82008-12-07 16:45:37 +0000328 const Node* NextNode = &getNode(ChooseEdge(CurNode->OutEdges, InLangs,
329 CurNode->Name())->ToolName());
330 PassThroughGraph(sys::Path(CurAction.OutFile()), NextNode,
331 InLangs, TempDir, LangMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000332 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000333
334 return 0;
335}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000336
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000337int CompilationGraph::CheckLanguageNames() const {
338 int ret = 0;
339 // Check that names for output and input languages on all edges do match.
340 for (const_nodes_iterator B = this->NodesMap.begin(),
341 E = this->NodesMap.end(); B != E; ++B) {
342
343 const Node & N1 = B->second;
344 if (N1.ToolPtr) {
345 for (Node::const_iterator EB = N1.EdgesBegin(), EE = N1.EdgesEnd();
346 EB != EE; ++EB) {
347 const Node& N2 = this->getNode((*EB)->ToolName());
348
349 if (!N2.ToolPtr) {
350 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000351 errs() << "Error: there is an edge from '" << N1.ToolPtr->Name()
352 << "' back to the root!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000353 continue;
354 }
355
356 const char* OutLang = N1.ToolPtr->OutputLanguage();
357 const char** InLangs = N2.ToolPtr->InputLanguages();
358 bool eq = false;
359 for (;*InLangs; ++InLangs) {
360 if (std::strcmp(OutLang, *InLangs) == 0) {
361 eq = true;
362 break;
363 }
364 }
365
366 if (!eq) {
367 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000368 errs() << "Error: Output->input language mismatch in the edge '"
369 << N1.ToolPtr->Name() << "' -> '" << N2.ToolPtr->Name()
370 << "'!\n"
371 << "Expected one of { ";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000372
373 InLangs = N2.ToolPtr->InputLanguages();
374 for (;*InLangs; ++InLangs) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000375 errs() << '\'' << *InLangs << (*(InLangs+1) ? "', " : "'");
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000376 }
377
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000378 errs() << " }, but got '" << OutLang << "'!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000379 }
380
381 }
382 }
383 }
384
385 return ret;
386}
387
388int CompilationGraph::CheckMultipleDefaultEdges() const {
389 int ret = 0;
390 InputLanguagesSet Dummy;
391
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000392 // For all nodes, just iterate over the outgoing edges and check if there is
393 // more than one edge with maximum weight.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000394 for (const_nodes_iterator B = this->NodesMap.begin(),
395 E = this->NodesMap.end(); B != E; ++B) {
396 const Node& N = B->second;
397 unsigned MaxWeight = 0;
398
399 // Ignore the root node.
400 if (!N.ToolPtr)
401 continue;
402
403 for (Node::const_iterator EB = N.EdgesBegin(), EE = N.EdgesEnd();
404 EB != EE; ++EB) {
405 unsigned EdgeWeight = (*EB)->Weight(Dummy);
406 if (EdgeWeight > MaxWeight) {
407 MaxWeight = EdgeWeight;
408 }
409 else if (EdgeWeight == MaxWeight) {
410 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000411 errs() << "Error: there are multiple maximal edges stemming from the '"
412 << N.ToolPtr->Name() << "' node!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000413 break;
414 }
415 }
416 }
417
418 return ret;
419}
420
421int CompilationGraph::CheckCycles() {
422 unsigned deleted = 0;
423 std::queue<Node*> Q;
424 Q.push(&getNode("root"));
425
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000426 // Try to delete all nodes that have no ingoing edges, starting from the
427 // root. If there are any nodes left after this operation, then we have a
428 // cycle. This relies on '--check-graph' not performing the topological sort.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000429 while (!Q.empty()) {
430 Node* A = Q.front();
431 Q.pop();
432 ++deleted;
433
434 for (Node::iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
435 EB != EE; ++EB) {
436 Node* B = &getNode((*EB)->ToolName());
437 B->DecrInEdges();
438 if (B->HasNoInEdges())
439 Q.push(B);
440 }
441 }
442
443 if (deleted != NodesMap.size()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000444 errs() << "Error: there are cycles in the compilation graph!\n"
445 << "Try inspecting the diagram produced by "
446 << "'llvmc --view-graph'.\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000447 return 1;
448 }
449
450 return 0;
451}
452
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000453int CompilationGraph::Check () {
454 // We try to catch as many errors as we can in one go.
455 int ret = 0;
456
457 // Check that output/input language names match.
458 ret += this->CheckLanguageNames();
459
460 // Check for multiple default edges.
461 ret += this->CheckMultipleDefaultEdges();
462
463 // Check for cycles.
464 ret += this->CheckCycles();
465
466 return ret;
467}
468
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000469// Code related to graph visualization.
470
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000471namespace llvm {
472 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000473 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000474 : public DefaultDOTGraphTraits
475 {
Tobias Grosserab010692009-11-30 13:34:51 +0000476 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000477
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000478 template<typename GraphType>
Tobias Grosserb5eedf22009-11-30 13:14:13 +0000479 static std::string getNodeLabel(const Node* N, const GraphType&)
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000480 {
481 if (N->ToolPtr)
482 if (N->ToolPtr->IsJoin())
483 return N->Name() + "\n (join" +
484 (N->HasChildren() ? ")"
485 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
486 else
487 return N->Name();
488 else
489 return "root";
490 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000491
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000492 template<typename EdgeIter>
493 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000494 if (N->ToolPtr) {
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000495 return N->ToolPtr->OutputLanguage();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000496 }
497 else {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000498 const char** InLangs = I->ToolPtr->InputLanguages();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000499 std::string ret;
500
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000501 for (; *InLangs; ++InLangs) {
502 if (*(InLangs + 1)) {
503 ret += *InLangs;
504 ret += ", ";
505 }
506 else {
507 ret += *InLangs;
508 }
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000509 }
510
511 return ret;
512 }
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000513 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000514 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000515
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000516}
517
Mikhail Glushenkovd50d32b2009-03-27 12:57:14 +0000518void CompilationGraph::writeGraph(const std::string& OutputFilename) {
Chris Lattner103289e2009-08-23 07:19:13 +0000519 std::string ErrorInfo;
520 raw_fd_ostream O(OutputFilename.c_str(), ErrorInfo);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000521
Chris Lattner103289e2009-08-23 07:19:13 +0000522 if (ErrorInfo.empty()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000523 errs() << "Writing '"<< OutputFilename << "' file...";
Mikhail Glushenkov3cd3c722009-03-26 21:23:48 +0000524 llvm::WriteGraph(O, this);
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000525 errs() << "done.\n";
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000526 }
527 else {
Mikhail Glushenkovd50d32b2009-03-27 12:57:14 +0000528 throw std::runtime_error("Error opening file '" + OutputFilename
529 + "' for writing!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000530 }
531}
532
533void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000534 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000535}