blob: 259911f713705981a6fdfaa4ab82f6db87c8a299 [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)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000142 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000143
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)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000153 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000154
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)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000196 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000197
198 CurNode = getNode(Edg->ToolName());
199 if (CurNode == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000200 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000201
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)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000298 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)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000313 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000314
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)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000325 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000326
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;
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000358 bool WasSomeActionGenerated = !InputFilenames.empty();
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000359
360 // Traverse initial parts of the toolchains and fill in InLangs.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000361 if (int ret = BuildInitial(InLangs, TempDir, LangMap))
362 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000363
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000364 std::vector<const Node*> JTV;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000365 if (int ret = TopologicalSortFilterJoinNodes(JTV))
366 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000367
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000368 // For all join nodes in topological order:
369 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
370 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000371
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000372 const Node* CurNode = *B;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000373 JoinTool* JT = &static_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000374
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000375 // Are there any files in the join list?
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000376 if (JT->JoinListEmpty() && !(JT->WorksOnEmpty() && InputFilenames.empty()))
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000377 continue;
378
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000379 WasSomeActionGenerated = true;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000380 Action CurAction;
381 if (int ret = JT->GenerateAction(CurAction, CurNode->HasChildren(),
382 TempDir, InLangs, LangMap)) {
383 return ret;
384 }
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000385
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000386 if (int ret = CurAction.Execute())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000387 return ret;
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000388
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000389 if (CurAction.StopCompilation())
390 return 0;
391
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000392 const Edge* Edg = ChooseEdge(CurNode->OutEdges, InLangs, CurNode->Name());
393 if (Edg == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000394 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000395
396 const Node* NextNode = getNode(Edg->ToolName());
397 if (NextNode == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000398 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000399
400 if (int ret = PassThroughGraph(sys::Path(CurAction.OutFile()), NextNode,
401 InLangs, TempDir, LangMap)) {
402 return ret;
403 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000404 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000405
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000406 if (!WasSomeActionGenerated) {
407 PrintError("no input files");
408 return 1;
409 }
410
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000411 return 0;
412}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000413
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000414int CompilationGraph::CheckLanguageNames() const {
415 int ret = 0;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000416
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000417 // Check that names for output and input languages on all edges do match.
418 for (const_nodes_iterator B = this->NodesMap.begin(),
419 E = this->NodesMap.end(); B != E; ++B) {
420
421 const Node & N1 = B->second;
422 if (N1.ToolPtr) {
423 for (Node::const_iterator EB = N1.EdgesBegin(), EE = N1.EdgesEnd();
424 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000425 const Node* N2 = this->getNode((*EB)->ToolName());
426 if (N2 == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000427 return 1;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000428
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000429 if (!N2->ToolPtr) {
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000430 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000431 errs() << "Error: there is an edge from '" << N1.ToolPtr->Name()
432 << "' back to the root!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000433 continue;
434 }
435
436 const char* OutLang = N1.ToolPtr->OutputLanguage();
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000437 const char** InLangs = N2->ToolPtr->InputLanguages();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000438 bool eq = false;
439 for (;*InLangs; ++InLangs) {
440 if (std::strcmp(OutLang, *InLangs) == 0) {
441 eq = true;
442 break;
443 }
444 }
445
446 if (!eq) {
447 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000448 errs() << "Error: Output->input language mismatch in the edge '"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000449 << N1.ToolPtr->Name() << "' -> '" << N2->ToolPtr->Name()
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000450 << "'!\n"
451 << "Expected one of { ";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000452
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000453 InLangs = N2->ToolPtr->InputLanguages();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000454 for (;*InLangs; ++InLangs) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000455 errs() << '\'' << *InLangs << (*(InLangs+1) ? "', " : "'");
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000456 }
457
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000458 errs() << " }, but got '" << OutLang << "'!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000459 }
460
461 }
462 }
463 }
464
465 return ret;
466}
467
468int CompilationGraph::CheckMultipleDefaultEdges() const {
469 int ret = 0;
470 InputLanguagesSet Dummy;
471
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000472 // For all nodes, just iterate over the outgoing edges and check if there is
473 // more than one edge with maximum weight.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000474 for (const_nodes_iterator B = this->NodesMap.begin(),
475 E = this->NodesMap.end(); B != E; ++B) {
476 const Node& N = B->second;
477 unsigned MaxWeight = 0;
478
479 // Ignore the root node.
480 if (!N.ToolPtr)
481 continue;
482
483 for (Node::const_iterator EB = N.EdgesBegin(), EE = N.EdgesEnd();
484 EB != EE; ++EB) {
485 unsigned EdgeWeight = (*EB)->Weight(Dummy);
486 if (EdgeWeight > MaxWeight) {
487 MaxWeight = EdgeWeight;
488 }
489 else if (EdgeWeight == MaxWeight) {
490 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000491 errs() << "Error: there are multiple maximal edges stemming from the '"
492 << N.ToolPtr->Name() << "' node!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000493 break;
494 }
495 }
496 }
497
498 return ret;
499}
500
501int CompilationGraph::CheckCycles() {
502 unsigned deleted = 0;
503 std::queue<Node*> Q;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000504
505 Node* Root = getNode("root");
506 if (Root == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000507 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000508
509 Q.push(Root);
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000510
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000511 // Try to delete all nodes that have no ingoing edges, starting from the
512 // root. If there are any nodes left after this operation, then we have a
513 // cycle. This relies on '--check-graph' not performing the topological sort.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000514 while (!Q.empty()) {
515 Node* A = Q.front();
516 Q.pop();
517 ++deleted;
518
519 for (Node::iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
520 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000521 Node* B = getNode((*EB)->ToolName());
522 if (B == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000523 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000524
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000525 B->DecrInEdges();
526 if (B->HasNoInEdges())
527 Q.push(B);
528 }
529 }
530
531 if (deleted != NodesMap.size()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000532 errs() << "Error: there are cycles in the compilation graph!\n"
533 << "Try inspecting the diagram produced by "
534 << "'llvmc --view-graph'.\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000535 return 1;
536 }
537
538 return 0;
539}
540
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000541int CompilationGraph::Check () {
542 // We try to catch as many errors as we can in one go.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000543 int errs = 0;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000544 int ret = 0;
545
546 // Check that output/input language names match.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000547 ret = this->CheckLanguageNames();
548 if (ret < 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000549 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000550 errs += ret;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000551
552 // Check for multiple default edges.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000553 ret = this->CheckMultipleDefaultEdges();
554 if (ret < 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000555 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000556 errs += ret;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000557
558 // Check for cycles.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000559 ret = this->CheckCycles();
560 if (ret < 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000561 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000562 errs += ret;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000563
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000564 return errs;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000565}
566
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000567// Code related to graph visualization.
568
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000569namespace llvm {
570 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000571 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000572 : public DefaultDOTGraphTraits
573 {
Tobias Grosserab010692009-11-30 13:34:51 +0000574 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000575
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000576 template<typename GraphType>
Tobias Grosserb5eedf22009-11-30 13:14:13 +0000577 static std::string getNodeLabel(const Node* N, const GraphType&)
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000578 {
579 if (N->ToolPtr)
580 if (N->ToolPtr->IsJoin())
581 return N->Name() + "\n (join" +
582 (N->HasChildren() ? ")"
583 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
584 else
585 return N->Name();
586 else
587 return "root";
588 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000589
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000590 template<typename EdgeIter>
591 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000592 if (N->ToolPtr) {
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000593 return N->ToolPtr->OutputLanguage();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000594 }
595 else {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000596 const char** InLangs = I->ToolPtr->InputLanguages();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000597 std::string ret;
598
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000599 for (; *InLangs; ++InLangs) {
600 if (*(InLangs + 1)) {
601 ret += *InLangs;
602 ret += ", ";
603 }
604 else {
605 ret += *InLangs;
606 }
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000607 }
608
609 return ret;
610 }
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000611 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000612 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000613
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000614}
615
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000616int CompilationGraph::writeGraph(const std::string& OutputFilename) {
Chris Lattner103289e2009-08-23 07:19:13 +0000617 std::string ErrorInfo;
618 raw_fd_ostream O(OutputFilename.c_str(), ErrorInfo);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000619
Chris Lattner103289e2009-08-23 07:19:13 +0000620 if (ErrorInfo.empty()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000621 errs() << "Writing '"<< OutputFilename << "' file...";
Mikhail Glushenkov3cd3c722009-03-26 21:23:48 +0000622 llvm::WriteGraph(O, this);
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000623 errs() << "done.\n";
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000624 }
625 else {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000626 PrintError("Error opening file '" + OutputFilename + "' for writing!");
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000627 return 1;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000628 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000629
630 return 0;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000631}
632
633void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000634 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000635}