blob: bb0eb7bcf197fe2a48f7a96acb285799921c17dc [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 {
36 LanguageMap::const_iterator Lang = this->find(File.getSuffix());
37 if (Lang == this->end())
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000038 throw std::runtime_error("Unknown suffix: " + File.getSuffix());
39 return Lang->second;
40 }
41}
42
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000043namespace {
44
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000045 /// ChooseEdge - Return the edge with the maximum weight.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000046 template <class C>
47 const Edge* ChooseEdge(const C& EdgesContainer,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000048 const InputLanguagesSet& InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000049 const std::string& NodeName = "root") {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000050 const Edge* MaxEdge = 0;
51 unsigned MaxWeight = 0;
52 bool SingleMax = true;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000053
54 for (typename C::const_iterator B = EdgesContainer.begin(),
55 E = EdgesContainer.end(); B != E; ++B) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000056 const Edge* e = B->getPtr();
57 unsigned EW = e->Weight(InLangs);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000058 if (EW > MaxWeight) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000059 MaxEdge = e;
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000060 MaxWeight = EW;
61 SingleMax = true;
Mikhail Glushenkove0ff9ae2008-05-06 18:18:58 +000062 } else if (EW == MaxWeight) {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000063 SingleMax = false;
64 }
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000065 }
66
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000067 if (!SingleMax)
68 throw std::runtime_error("Node " + NodeName +
69 ": multiple maximal outward edges found!"
Mikhail Glushenkov87416b42008-05-06 18:10:53 +000070 " Most probably a specification error.");
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000071 if (!MaxEdge)
72 throw std::runtime_error("Node " + NodeName +
73 ": no maximal outward edge found!"
74 " Most probably a specification error.");
75 return MaxEdge;
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000076 }
77
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000078}
79
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +000080void Node::AddEdge(Edge* Edg) {
81 // If there already was an edge between two nodes, modify it instead
82 // of adding a new edge.
83 const std::string& ToolName = Edg->ToolName();
84 for (container_type::iterator B = OutEdges.begin(), E = OutEdges.end();
85 B != E; ++B) {
86 if ((*B)->ToolName() == ToolName) {
87 llvm::IntrusiveRefCntPtr<Edge>(Edg).swap(*B);
88 return;
89 }
90 }
91 OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(Edg));
92}
93
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000094CompilationGraph::CompilationGraph() {
95 NodesMap["root"] = Node(this);
96}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000097
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000098Node& CompilationGraph::getNode(const std::string& ToolName) {
99 nodes_map_type::iterator I = NodesMap.find(ToolName);
100 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000101 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000102 return I->second;
103}
104
105const Node& CompilationGraph::getNode(const std::string& ToolName) const {
106 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
107 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000108 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000109 return I->second;
110}
111
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000112// Find the tools list corresponding to the given language name.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000113const CompilationGraph::tools_vector_type&
114CompilationGraph::getToolsVector(const std::string& LangName) const
115{
116 tools_map_type::const_iterator I = ToolsMap.find(LangName);
117 if (I == ToolsMap.end())
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000118 throw std::runtime_error("No tool corresponding to the language "
Mikhail Glushenkov35bca412008-05-30 06:16:59 +0000119 + LangName + " found");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000120 return I->second;
121}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000122
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000123void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +0000124 if (NodesMap.count(V->Name()) == 0)
125 NodesMap[V->Name()] = Node(this, V);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000126}
127
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000128void CompilationGraph::insertEdge(const std::string& A, Edge* Edg) {
129 Node& B = getNode(Edg->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000130 if (A == "root") {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000131 const char** InLangs = B.ToolPtr->InputLanguages();
132 for (;*InLangs; ++InLangs)
133 ToolsMap[*InLangs].push_back(IntrusiveRefCntPtr<Edge>(Edg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000134 NodesMap["root"].AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000135 }
136 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000137 Node& N = getNode(A);
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000138 N.AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000139 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000140 // Increase the inward edge counter.
141 B.IncrInEdges();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000142}
143
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000144// Pass input file through the chain until we bump into a Join node or
145// a node that says that it is the last.
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000146void CompilationGraph::PassThroughGraph (const sys::Path& InFile,
147 const Node* StartNode,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000148 const InputLanguagesSet& InLangs,
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000149 const sys::Path& TempDir,
150 const LanguageMap& LangMap) const {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000151 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000152 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000153
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000154 while(true) {
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000155 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000156
157 if (CurTool->IsJoin()) {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000158 JoinTool& JT = dynamic_cast<JoinTool&>(*CurTool);
159 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000160 break;
161 }
162
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000163 Action CurAction = CurTool->GenerateAction(In, CurNode->HasChildren(),
164 TempDir, InLangs, LangMap);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000165
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000166 if (int ret = CurAction.Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000167 throw error_code(ret);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000168
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000169 if (CurAction.StopCompilation())
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000170 return;
171
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000172 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000173 InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000174 CurNode->Name())->ToolName());
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000175 In = CurAction.OutFile();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000176 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000177}
178
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000179// Find the head of the toolchain corresponding to the given file.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000180// Also, insert an input language into InLangs.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000181const Node* CompilationGraph::
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000182FindToolChain(const sys::Path& In, const std::string* ForceLanguage,
183 InputLanguagesSet& InLangs, const LanguageMap& LangMap) const {
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000184
185 // Determine the input language.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000186 const std::string& InLanguage =
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000187 ForceLanguage ? *ForceLanguage : LangMap.GetLanguage(In);
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000188
189 // Add the current input language to the input language set.
190 InLangs.insert(InLanguage);
191
192 // Find the toolchain for the input language.
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000193 const tools_vector_type& TV = getToolsVector(InLanguage);
194 if (TV.empty())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000195 throw std::runtime_error("No toolchain corresponding to language "
196 + InLanguage + " found");
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000197 return &getNode(ChooseEdge(TV, InLangs)->ToolName());
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000198}
199
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000200// Helper function used by Build().
201// Traverses initial portions of the toolchains (up to the first Join node).
202// This function is also responsible for handling the -x option.
203void CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000204 const sys::Path& TempDir,
205 const LanguageMap& LangMap) {
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000206 // This is related to -x option handling.
207 cl::list<std::string>::const_iterator xIter = Languages.begin(),
208 xBegin = xIter, xEnd = Languages.end();
209 bool xEmpty = true;
210 const std::string* xLanguage = 0;
211 unsigned xPos = 0, xPosNext = 0, filePos = 0;
212
213 if (xIter != xEnd) {
214 xEmpty = false;
215 xPos = Languages.getPosition(xIter - xBegin);
216 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
217 xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max()
218 : Languages.getPosition(xNext - xBegin);
219 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
220 }
221
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000222 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000223 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000224 CB = B, E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000225 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000226
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000227 // Code for handling the -x option.
228 // Output: std::string* xLanguage (can be NULL).
229 if (!xEmpty) {
230 filePos = InputFilenames.getPosition(B - CB);
231
232 if (xPos < filePos) {
233 if (filePos < xPosNext) {
234 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
235 }
236 else { // filePos >= xPosNext
237 // Skip xIters while filePos > xPosNext
238 while (filePos > xPosNext) {
239 ++xIter;
240 xPos = xPosNext;
241
242 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
243 if (xNext == xEnd)
244 xPosNext = std::numeric_limits<unsigned>::max();
245 else
246 xPosNext = Languages.getPosition(xNext - xBegin);
247 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
248 }
249 }
250 }
251 }
252
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000253 // Find the toolchain corresponding to this file.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000254 const Node* N = FindToolChain(In, xLanguage, InLangs, LangMap);
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000255 // Pass file through the chain starting at head.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000256 PassThroughGraph(In, N, InLangs, TempDir, LangMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000257 }
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000258}
259
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000260// Sort the nodes in topological order.
261void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
262 std::queue<const Node*> Q;
263 Q.push(&getNode("root"));
264
265 while (!Q.empty()) {
266 const Node* A = Q.front();
267 Q.pop();
268 Out.push_back(A);
269 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
270 EB != EE; ++EB) {
271 Node* B = &getNode((*EB)->ToolName());
272 B->DecrInEdges();
273 if (B->HasNoInEdges())
274 Q.push(B);
275 }
276 }
277}
278
279namespace {
280 bool NotJoinNode(const Node* N) {
281 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
282 }
283}
284
285// Call TopologicalSort and filter the resulting list to include
286// only Join nodes.
287void CompilationGraph::
288TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
289 std::vector<const Node*> TopSorted;
290 TopologicalSort(TopSorted);
291 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
292 std::back_inserter(Out), NotJoinNode);
293}
294
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000295int CompilationGraph::Build (const sys::Path& TempDir,
296 const LanguageMap& LangMap) {
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000297
298 InputLanguagesSet InLangs;
299
300 // Traverse initial parts of the toolchains and fill in InLangs.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000301 BuildInitial(InLangs, TempDir, LangMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000302
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000303 std::vector<const Node*> JTV;
304 TopologicalSortFilterJoinNodes(JTV);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000305
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000306 // For all join nodes in topological order:
307 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
308 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000309
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000310 const Node* CurNode = *B;
311 JoinTool* JT = &dynamic_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000312
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000313 // Are there any files in the join list?
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000314 if (JT->JoinListEmpty())
315 continue;
316
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000317 Action CurAction = JT->GenerateAction(CurNode->HasChildren(),
318 TempDir, InLangs, LangMap);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000319
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000320 if (int ret = CurAction.Execute())
Mikhail Glushenkova6730372008-05-12 16:31:42 +0000321 throw error_code(ret);
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000322
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000323 if (CurAction.StopCompilation())
324 return 0;
325
Mikhail Glushenkovb677df82008-12-07 16:45:37 +0000326 const Node* NextNode = &getNode(ChooseEdge(CurNode->OutEdges, InLangs,
327 CurNode->Name())->ToolName());
328 PassThroughGraph(sys::Path(CurAction.OutFile()), NextNode,
329 InLangs, TempDir, LangMap);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000330 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000331
332 return 0;
333}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000334
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000335int CompilationGraph::CheckLanguageNames() const {
336 int ret = 0;
337 // Check that names for output and input languages on all edges do match.
338 for (const_nodes_iterator B = this->NodesMap.begin(),
339 E = this->NodesMap.end(); B != E; ++B) {
340
341 const Node & N1 = B->second;
342 if (N1.ToolPtr) {
343 for (Node::const_iterator EB = N1.EdgesBegin(), EE = N1.EdgesEnd();
344 EB != EE; ++EB) {
345 const Node& N2 = this->getNode((*EB)->ToolName());
346
347 if (!N2.ToolPtr) {
348 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000349 errs() << "Error: there is an edge from '" << N1.ToolPtr->Name()
350 << "' back to the root!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000351 continue;
352 }
353
354 const char* OutLang = N1.ToolPtr->OutputLanguage();
355 const char** InLangs = N2.ToolPtr->InputLanguages();
356 bool eq = false;
357 for (;*InLangs; ++InLangs) {
358 if (std::strcmp(OutLang, *InLangs) == 0) {
359 eq = true;
360 break;
361 }
362 }
363
364 if (!eq) {
365 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000366 errs() << "Error: Output->input language mismatch in the edge '"
367 << N1.ToolPtr->Name() << "' -> '" << N2.ToolPtr->Name()
368 << "'!\n"
369 << "Expected one of { ";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000370
371 InLangs = N2.ToolPtr->InputLanguages();
372 for (;*InLangs; ++InLangs) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000373 errs() << '\'' << *InLangs << (*(InLangs+1) ? "', " : "'");
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000374 }
375
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000376 errs() << " }, but got '" << OutLang << "'!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000377 }
378
379 }
380 }
381 }
382
383 return ret;
384}
385
386int CompilationGraph::CheckMultipleDefaultEdges() const {
387 int ret = 0;
388 InputLanguagesSet Dummy;
389
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000390 // For all nodes, just iterate over the outgoing edges and check if there is
391 // more than one edge with maximum weight.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000392 for (const_nodes_iterator B = this->NodesMap.begin(),
393 E = this->NodesMap.end(); B != E; ++B) {
394 const Node& N = B->second;
395 unsigned MaxWeight = 0;
396
397 // Ignore the root node.
398 if (!N.ToolPtr)
399 continue;
400
401 for (Node::const_iterator EB = N.EdgesBegin(), EE = N.EdgesEnd();
402 EB != EE; ++EB) {
403 unsigned EdgeWeight = (*EB)->Weight(Dummy);
404 if (EdgeWeight > MaxWeight) {
405 MaxWeight = EdgeWeight;
406 }
407 else if (EdgeWeight == MaxWeight) {
408 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000409 errs() << "Error: there are multiple maximal edges stemming from the '"
410 << N.ToolPtr->Name() << "' node!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000411 break;
412 }
413 }
414 }
415
416 return ret;
417}
418
419int CompilationGraph::CheckCycles() {
420 unsigned deleted = 0;
421 std::queue<Node*> Q;
422 Q.push(&getNode("root"));
423
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000424 // Try to delete all nodes that have no ingoing edges, starting from the
425 // root. If there are any nodes left after this operation, then we have a
426 // cycle. This relies on '--check-graph' not performing the topological sort.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000427 while (!Q.empty()) {
428 Node* A = Q.front();
429 Q.pop();
430 ++deleted;
431
432 for (Node::iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
433 EB != EE; ++EB) {
434 Node* B = &getNode((*EB)->ToolName());
435 B->DecrInEdges();
436 if (B->HasNoInEdges())
437 Q.push(B);
438 }
439 }
440
441 if (deleted != NodesMap.size()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000442 errs() << "Error: there are cycles in the compilation graph!\n"
443 << "Try inspecting the diagram produced by "
444 << "'llvmc --view-graph'.\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000445 return 1;
446 }
447
448 return 0;
449}
450
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000451int CompilationGraph::Check () {
452 // We try to catch as many errors as we can in one go.
453 int ret = 0;
454
455 // Check that output/input language names match.
456 ret += this->CheckLanguageNames();
457
458 // Check for multiple default edges.
459 ret += this->CheckMultipleDefaultEdges();
460
461 // Check for cycles.
462 ret += this->CheckCycles();
463
464 return ret;
465}
466
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000467// Code related to graph visualization.
468
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000469namespace llvm {
470 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000471 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000472 : public DefaultDOTGraphTraits
473 {
474
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000475 template<typename GraphType>
Mikhail Glushenkov7defa2d2009-06-25 18:20:10 +0000476 static std::string getNodeLabel(const Node* N, const GraphType&,
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000477 bool ShortNames)
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000478 {
479 if (N->ToolPtr)
480 if (N->ToolPtr->IsJoin())
481 return N->Name() + "\n (join" +
482 (N->HasChildren() ? ")"
483 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
484 else
485 return N->Name();
486 else
487 return "root";
488 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000489
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000490 template<typename EdgeIter>
491 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000492 if (N->ToolPtr) {
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000493 return N->ToolPtr->OutputLanguage();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000494 }
495 else {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000496 const char** InLangs = I->ToolPtr->InputLanguages();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000497 std::string ret;
498
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000499 for (; *InLangs; ++InLangs) {
500 if (*(InLangs + 1)) {
501 ret += *InLangs;
502 ret += ", ";
503 }
504 else {
505 ret += *InLangs;
506 }
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000507 }
508
509 return ret;
510 }
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000511 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000512 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000513
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000514}
515
Mikhail Glushenkovd50d32b2009-03-27 12:57:14 +0000516void CompilationGraph::writeGraph(const std::string& OutputFilename) {
Chris Lattner103289e2009-08-23 07:19:13 +0000517 std::string ErrorInfo;
518 raw_fd_ostream O(OutputFilename.c_str(), ErrorInfo);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000519
Chris Lattner103289e2009-08-23 07:19:13 +0000520 if (ErrorInfo.empty()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000521 errs() << "Writing '"<< OutputFilename << "' file...";
Mikhail Glushenkov3cd3c722009-03-26 21:23:48 +0000522 llvm::WriteGraph(O, this);
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000523 errs() << "done.\n";
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000524 }
525 else {
Mikhail Glushenkovd50d32b2009-03-27 12:57:14 +0000526 throw std::runtime_error("Error opening file '" + OutputFilename
527 + "' for writing!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000528 }
529}
530
531void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000532 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000533}