blob: 4e953cbe1103a0371bbe26df2c98cccdd6ef8f7e [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
29using namespace llvm;
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +000030using namespace llvmc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000032namespace llvmc {
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000033
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000034 const std::string* LanguageMap::GetLanguage(const sys::Path& File) const {
Mikhail Glushenkov21e099a2010-01-26 14:55:16 +000035 StringRef suf = File.getSuffix();
Mikhail Glushenkov9660c5d2010-02-23 09:05:21 +000036 LanguageMap::const_iterator Lang =
37 this->find(suf.empty() ? "*empty*" : suf);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000038 if (Lang == this->end()) {
39 PrintError("File '" + File.str() + "' has unknown suffix '"
40 + suf.str() + '\'');
41 return 0;
42 }
43 return &Lang->second;
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000044 }
45}
46
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000047namespace {
48
Mikhail Glushenkov4561ab52008-05-07 21:50:19 +000049 /// ChooseEdge - Return the edge with the maximum weight.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000050 template <class C>
51 const Edge* ChooseEdge(const C& EdgesContainer,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000052 const InputLanguagesSet& InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000053 const std::string& NodeName = "root") {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000054 const Edge* MaxEdge = 0;
55 unsigned MaxWeight = 0;
56 bool SingleMax = true;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000057
58 for (typename C::const_iterator B = EdgesContainer.begin(),
59 E = EdgesContainer.end(); B != E; ++B) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000060 const Edge* e = B->getPtr();
61 unsigned EW = e->Weight(InLangs);
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000062 if (EW > MaxWeight) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000063 MaxEdge = e;
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000064 MaxWeight = EW;
65 SingleMax = true;
Mikhail Glushenkove0ff9ae2008-05-06 18:18:58 +000066 } else if (EW == MaxWeight) {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000067 SingleMax = false;
68 }
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000069 }
70
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000071 if (!SingleMax) {
72 PrintError("Node " + NodeName + ": multiple maximal outward edges found!"
73 " Most probably a specification error.");
74 return 0;
75 }
76 if (!MaxEdge) {
77 PrintError("Node " + NodeName + ": no maximal outward edge found!"
78 " Most probably a specification error.");
79 return 0;
80 }
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000081 return MaxEdge;
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000082 }
83
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000084}
85
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +000086void Node::AddEdge(Edge* Edg) {
87 // If there already was an edge between two nodes, modify it instead
88 // of adding a new edge.
89 const std::string& ToolName = Edg->ToolName();
90 for (container_type::iterator B = OutEdges.begin(), E = OutEdges.end();
91 B != E; ++B) {
92 if ((*B)->ToolName() == ToolName) {
93 llvm::IntrusiveRefCntPtr<Edge>(Edg).swap(*B);
94 return;
95 }
96 }
97 OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(Edg));
98}
99
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000100CompilationGraph::CompilationGraph() {
101 NodesMap["root"] = Node(this);
102}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000103
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000104Node* CompilationGraph::getNode(const std::string& ToolName) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000105 nodes_map_type::iterator I = NodesMap.find(ToolName);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000106 if (I == NodesMap.end()) {
107 PrintError("Node " + ToolName + " is not in the graph");
108 return 0;
109 }
110 return &I->second;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000111}
112
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000113const Node* CompilationGraph::getNode(const std::string& ToolName) const {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000114 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000115 if (I == NodesMap.end()) {
116 PrintError("Node " + ToolName + " is not in the graph!");
117 return 0;
118 }
119 return &I->second;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000120}
121
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000122// Find the tools list corresponding to the given language name.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000123const CompilationGraph::tools_vector_type*
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000124CompilationGraph::getToolsVector(const std::string& LangName) const
125{
126 tools_map_type::const_iterator I = ToolsMap.find(LangName);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000127 if (I == ToolsMap.end()) {
128 PrintError("No tool corresponding to the language " + LangName + " found");
129 return 0;
130 }
131 return &I->second;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000132}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000133
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000134void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +0000135 if (NodesMap.count(V->Name()) == 0)
136 NodesMap[V->Name()] = Node(this, V);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000137}
138
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000139int CompilationGraph::insertEdge(const std::string& A, Edge* Edg) {
140 Node* B = getNode(Edg->ToolName());
141 if (B == 0)
142 return -1;
143
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000144 if (A == "root") {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000145 const char** InLangs = B->ToolPtr->InputLanguages();
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000146 for (;*InLangs; ++InLangs)
147 ToolsMap[*InLangs].push_back(IntrusiveRefCntPtr<Edge>(Edg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000148 NodesMap["root"].AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000149 }
150 else {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000151 Node* N = getNode(A);
152 if (N == 0)
153 return -1;
154
155 N->AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000156 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000157 // Increase the inward edge counter.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000158 B->IncrInEdges();
159
160 return 0;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000161}
162
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000163// Pass input file through the chain until we bump into a Join node or
164// a node that says that it is the last.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000165int CompilationGraph::PassThroughGraph (const sys::Path& InFile,
166 const Node* StartNode,
167 const InputLanguagesSet& InLangs,
168 const sys::Path& TempDir,
169 const LanguageMap& LangMap) const {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000170 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000171 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000172
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000173 while(true) {
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000174 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000175
176 if (CurTool->IsJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000177 JoinTool& JT = static_cast<JoinTool&>(*CurTool);
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000178 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000179 break;
180 }
181
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000182 Action CurAction;
183 if (int ret = CurTool->GenerateAction(CurAction, In, CurNode->HasChildren(),
184 TempDir, InLangs, LangMap)) {
185 return ret;
186 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000187
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000188 if (int ret = CurAction.Execute())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000189 return ret;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000190
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000191 if (CurAction.StopCompilation())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000192 return 0;
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000193
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000194 const Edge* Edg = ChooseEdge(CurNode->OutEdges, InLangs, CurNode->Name());
195 if (Edg == 0)
196 return -1;
197
198 CurNode = getNode(Edg->ToolName());
199 if (CurNode == 0)
200 return -1;
201
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000202 In = CurAction.OutFile();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000203 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000204
205 return 0;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000206}
207
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000208// Find the head of the toolchain corresponding to the given file.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000209// Also, insert an input language into InLangs.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000210const Node* CompilationGraph::
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000211FindToolChain(const sys::Path& In, const std::string* ForceLanguage,
212 InputLanguagesSet& InLangs, const LanguageMap& LangMap) const {
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000213
214 // Determine the input language.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000215 const std::string* InLang = LangMap.GetLanguage(In);
216 if (InLang == 0)
217 return 0;
218 const std::string& InLanguage = (ForceLanguage ? *ForceLanguage : *InLang);
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 Glushenkovb374d4f2010-07-23 03:42:55 +0000224 const tools_vector_type* pTV = getToolsVector(InLanguage);
225 if (pTV == 0)
226 return 0;
227
228 const tools_vector_type& TV = *pTV;
229 if (TV.empty()) {
230 PrintError("No toolchain corresponding to language "
231 + InLanguage + " found");
232 return 0;
233 }
234
235 const Edge* Edg = ChooseEdge(TV, InLangs);
236 if (Edg == 0)
237 return 0;
238
239 return getNode(Edg->ToolName());
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000240}
241
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000242// Helper function used by Build().
243// Traverses initial portions of the toolchains (up to the first Join node).
244// This function is also responsible for handling the -x option.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000245int CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
246 const sys::Path& TempDir,
247 const LanguageMap& LangMap) {
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000248 // This is related to -x option handling.
249 cl::list<std::string>::const_iterator xIter = Languages.begin(),
250 xBegin = xIter, xEnd = Languages.end();
251 bool xEmpty = true;
252 const std::string* xLanguage = 0;
253 unsigned xPos = 0, xPosNext = 0, filePos = 0;
254
255 if (xIter != xEnd) {
256 xEmpty = false;
257 xPos = Languages.getPosition(xIter - xBegin);
258 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
259 xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max()
260 : Languages.getPosition(xNext - xBegin);
261 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
262 }
263
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000264 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000265 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000266 CB = B, E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000267 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000268
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000269 // Code for handling the -x option.
270 // Output: std::string* xLanguage (can be NULL).
271 if (!xEmpty) {
272 filePos = InputFilenames.getPosition(B - CB);
273
274 if (xPos < filePos) {
275 if (filePos < xPosNext) {
276 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
277 }
278 else { // filePos >= xPosNext
279 // Skip xIters while filePos > xPosNext
280 while (filePos > xPosNext) {
281 ++xIter;
282 xPos = xPosNext;
283
284 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
285 if (xNext == xEnd)
286 xPosNext = std::numeric_limits<unsigned>::max();
287 else
288 xPosNext = Languages.getPosition(xNext - xBegin);
289 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
290 }
291 }
292 }
293 }
294
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000295 // Find the toolchain corresponding to this file.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000296 const Node* N = FindToolChain(In, xLanguage, InLangs, LangMap);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000297 if (N == 0)
298 return -1;
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000299 // Pass file through the chain starting at head.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000300 if (int ret = PassThroughGraph(In, N, InLangs, TempDir, LangMap))
301 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000302 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000303
304 return 0;
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000305}
306
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000307// Sort the nodes in topological order.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000308int CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000309 std::queue<const Node*> Q;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000310
311 Node* Root = getNode("root");
312 if (Root == 0)
313 return -1;
314
315 Q.push(Root);
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000316
317 while (!Q.empty()) {
318 const Node* A = Q.front();
319 Q.pop();
320 Out.push_back(A);
321 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
322 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000323 Node* B = getNode((*EB)->ToolName());
324 if (B == 0)
325 return -1;
326
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000327 B->DecrInEdges();
328 if (B->HasNoInEdges())
329 Q.push(B);
330 }
331 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000332
333 return 0;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000334}
335
336namespace {
337 bool NotJoinNode(const Node* N) {
338 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
339 }
340}
341
342// Call TopologicalSort and filter the resulting list to include
343// only Join nodes.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000344int CompilationGraph::
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000345TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
346 std::vector<const Node*> TopSorted;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000347 if (int ret = TopologicalSort(TopSorted))
348 return ret;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000349 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
350 std::back_inserter(Out), NotJoinNode);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000351
352 return 0;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000353}
354
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000355int CompilationGraph::Build (const sys::Path& TempDir,
356 const LanguageMap& LangMap) {
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000357 InputLanguagesSet InLangs;
358
359 // Traverse initial parts of the toolchains and fill in InLangs.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000360 if (int ret = BuildInitial(InLangs, TempDir, LangMap))
361 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000362
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000363 std::vector<const Node*> JTV;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000364 if (int ret = TopologicalSortFilterJoinNodes(JTV))
365 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000366
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000367 // For all join nodes in topological order:
368 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
369 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000370
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000371 const Node* CurNode = *B;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000372 JoinTool* JT = &static_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000373
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000374 // Are there any files in the join list?
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000375 if (JT->JoinListEmpty() && !(JT->WorksOnEmpty() && InputFilenames.empty()))
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000376 continue;
377
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000378 Action CurAction;
379 if (int ret = JT->GenerateAction(CurAction, CurNode->HasChildren(),
380 TempDir, InLangs, LangMap)) {
381 return ret;
382 }
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000383
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000384 if (int ret = CurAction.Execute())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000385 return ret;
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000386
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000387 if (CurAction.StopCompilation())
388 return 0;
389
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000390 const Edge* Edg = ChooseEdge(CurNode->OutEdges, InLangs, CurNode->Name());
391 if (Edg == 0)
392 return -1;
393
394 const Node* NextNode = getNode(Edg->ToolName());
395 if (NextNode == 0)
396 return -1;
397
398 if (int ret = PassThroughGraph(sys::Path(CurAction.OutFile()), NextNode,
399 InLangs, TempDir, LangMap)) {
400 return ret;
401 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000402 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000403
404 return 0;
405}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000406
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000407int CompilationGraph::CheckLanguageNames() const {
408 int ret = 0;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000409
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000410 // Check that names for output and input languages on all edges do match.
411 for (const_nodes_iterator B = this->NodesMap.begin(),
412 E = this->NodesMap.end(); B != E; ++B) {
413
414 const Node & N1 = B->second;
415 if (N1.ToolPtr) {
416 for (Node::const_iterator EB = N1.EdgesBegin(), EE = N1.EdgesEnd();
417 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000418 const Node* N2 = this->getNode((*EB)->ToolName());
419 if (N2 == 0)
420 return -1;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000421
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000422 if (!N2->ToolPtr) {
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000423 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000424 errs() << "Error: there is an edge from '" << N1.ToolPtr->Name()
425 << "' back to the root!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000426 continue;
427 }
428
429 const char* OutLang = N1.ToolPtr->OutputLanguage();
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000430 const char** InLangs = N2->ToolPtr->InputLanguages();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000431 bool eq = false;
432 for (;*InLangs; ++InLangs) {
433 if (std::strcmp(OutLang, *InLangs) == 0) {
434 eq = true;
435 break;
436 }
437 }
438
439 if (!eq) {
440 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000441 errs() << "Error: Output->input language mismatch in the edge '"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000442 << N1.ToolPtr->Name() << "' -> '" << N2->ToolPtr->Name()
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000443 << "'!\n"
444 << "Expected one of { ";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000445
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000446 InLangs = N2->ToolPtr->InputLanguages();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000447 for (;*InLangs; ++InLangs) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000448 errs() << '\'' << *InLangs << (*(InLangs+1) ? "', " : "'");
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000449 }
450
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000451 errs() << " }, but got '" << OutLang << "'!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000452 }
453
454 }
455 }
456 }
457
458 return ret;
459}
460
461int CompilationGraph::CheckMultipleDefaultEdges() const {
462 int ret = 0;
463 InputLanguagesSet Dummy;
464
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000465 // For all nodes, just iterate over the outgoing edges and check if there is
466 // more than one edge with maximum weight.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000467 for (const_nodes_iterator B = this->NodesMap.begin(),
468 E = this->NodesMap.end(); B != E; ++B) {
469 const Node& N = B->second;
470 unsigned MaxWeight = 0;
471
472 // Ignore the root node.
473 if (!N.ToolPtr)
474 continue;
475
476 for (Node::const_iterator EB = N.EdgesBegin(), EE = N.EdgesEnd();
477 EB != EE; ++EB) {
478 unsigned EdgeWeight = (*EB)->Weight(Dummy);
479 if (EdgeWeight > MaxWeight) {
480 MaxWeight = EdgeWeight;
481 }
482 else if (EdgeWeight == MaxWeight) {
483 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000484 errs() << "Error: there are multiple maximal edges stemming from the '"
485 << N.ToolPtr->Name() << "' node!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000486 break;
487 }
488 }
489 }
490
491 return ret;
492}
493
494int CompilationGraph::CheckCycles() {
495 unsigned deleted = 0;
496 std::queue<Node*> Q;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000497
498 Node* Root = getNode("root");
499 if (Root == 0)
500 return -1;
501
502 Q.push(Root);
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000503
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000504 // Try to delete all nodes that have no ingoing edges, starting from the
505 // root. If there are any nodes left after this operation, then we have a
506 // cycle. This relies on '--check-graph' not performing the topological sort.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000507 while (!Q.empty()) {
508 Node* A = Q.front();
509 Q.pop();
510 ++deleted;
511
512 for (Node::iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
513 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000514 Node* B = getNode((*EB)->ToolName());
515 if (B == 0)
516 return -1;
517
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000518 B->DecrInEdges();
519 if (B->HasNoInEdges())
520 Q.push(B);
521 }
522 }
523
524 if (deleted != NodesMap.size()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000525 errs() << "Error: there are cycles in the compilation graph!\n"
526 << "Try inspecting the diagram produced by "
527 << "'llvmc --view-graph'.\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000528 return 1;
529 }
530
531 return 0;
532}
533
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000534int CompilationGraph::Check () {
535 // We try to catch as many errors as we can in one go.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000536 int errs = 0;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000537 int ret = 0;
538
539 // Check that output/input language names match.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000540 ret = this->CheckLanguageNames();
541 if (ret < 0)
542 return -1;
543 errs += ret;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000544
545 // Check for multiple default edges.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000546 ret = this->CheckMultipleDefaultEdges();
547 if (ret < 0)
548 return -1;
549 errs += ret;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000550
551 // Check for cycles.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000552 ret = this->CheckCycles();
553 if (ret < 0)
554 return -1;
555 errs += ret;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000556
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000557 return errs;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000558}
559
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000560// Code related to graph visualization.
561
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000562namespace llvm {
563 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000564 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000565 : public DefaultDOTGraphTraits
566 {
Tobias Grosserab010692009-11-30 13:34:51 +0000567 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000568
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000569 template<typename GraphType>
Tobias Grosserb5eedf22009-11-30 13:14:13 +0000570 static std::string getNodeLabel(const Node* N, const GraphType&)
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000571 {
572 if (N->ToolPtr)
573 if (N->ToolPtr->IsJoin())
574 return N->Name() + "\n (join" +
575 (N->HasChildren() ? ")"
576 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
577 else
578 return N->Name();
579 else
580 return "root";
581 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000582
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000583 template<typename EdgeIter>
584 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000585 if (N->ToolPtr) {
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000586 return N->ToolPtr->OutputLanguage();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000587 }
588 else {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000589 const char** InLangs = I->ToolPtr->InputLanguages();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000590 std::string ret;
591
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000592 for (; *InLangs; ++InLangs) {
593 if (*(InLangs + 1)) {
594 ret += *InLangs;
595 ret += ", ";
596 }
597 else {
598 ret += *InLangs;
599 }
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000600 }
601
602 return ret;
603 }
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000604 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000605 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000606
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000607}
608
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000609int CompilationGraph::writeGraph(const std::string& OutputFilename) {
Chris Lattner103289e2009-08-23 07:19:13 +0000610 std::string ErrorInfo;
611 raw_fd_ostream O(OutputFilename.c_str(), ErrorInfo);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000612
Chris Lattner103289e2009-08-23 07:19:13 +0000613 if (ErrorInfo.empty()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000614 errs() << "Writing '"<< OutputFilename << "' file...";
Mikhail Glushenkov3cd3c722009-03-26 21:23:48 +0000615 llvm::WriteGraph(O, this);
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000616 errs() << "done.\n";
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000617 }
618 else {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000619 PrintError("Error opening file '" + OutputFilename + "' for writing!");
620 return -1;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000621 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000622
623 return 0;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000624}
625
626void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000627 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000628}