blob: a217078ffabe70e8e2ac170eabec011c2ce4591b [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 Glushenkov7555f0a2010-08-23 19:24:08 +000049 /// ChooseEdge - Return the edge with the maximum weight. Returns 0 on error.
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;
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +000055 int MaxWeight = 0;
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000056 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();
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +000061 int EW = e->Weight(InLangs);
62 if (EW < 0) {
63 // (error) invocation in TableGen -> we don't need to print an error
64 // message.
65 return 0;
66 }
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000067 if (EW > MaxWeight) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000068 MaxEdge = e;
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000069 MaxWeight = EW;
70 SingleMax = true;
Mikhail Glushenkove0ff9ae2008-05-06 18:18:58 +000071 } else if (EW == MaxWeight) {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000072 SingleMax = false;
73 }
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000074 }
75
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000076 if (!SingleMax) {
77 PrintError("Node " + NodeName + ": multiple maximal outward edges found!"
78 " Most probably a specification error.");
79 return 0;
80 }
81 if (!MaxEdge) {
82 PrintError("Node " + NodeName + ": no maximal outward edge found!"
83 " Most probably a specification error.");
84 return 0;
85 }
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000086 return MaxEdge;
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000087 }
88
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000089}
90
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +000091void Node::AddEdge(Edge* Edg) {
92 // If there already was an edge between two nodes, modify it instead
93 // of adding a new edge.
94 const std::string& ToolName = Edg->ToolName();
95 for (container_type::iterator B = OutEdges.begin(), E = OutEdges.end();
96 B != E; ++B) {
97 if ((*B)->ToolName() == ToolName) {
98 llvm::IntrusiveRefCntPtr<Edge>(Edg).swap(*B);
99 return;
100 }
101 }
102 OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(Edg));
103}
104
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000105CompilationGraph::CompilationGraph() {
106 NodesMap["root"] = Node(this);
107}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000108
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000109Node* CompilationGraph::getNode(const std::string& ToolName) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000110 nodes_map_type::iterator I = NodesMap.find(ToolName);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000111 if (I == NodesMap.end()) {
112 PrintError("Node " + ToolName + " is not in the graph");
113 return 0;
114 }
115 return &I->second;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000116}
117
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000118const Node* CompilationGraph::getNode(const std::string& ToolName) const {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000119 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000120 if (I == NodesMap.end()) {
121 PrintError("Node " + ToolName + " is not in the graph!");
122 return 0;
123 }
124 return &I->second;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000125}
126
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000127// Find the tools list corresponding to the given language name.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000128const CompilationGraph::tools_vector_type*
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000129CompilationGraph::getToolsVector(const std::string& LangName) const
130{
131 tools_map_type::const_iterator I = ToolsMap.find(LangName);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000132 if (I == ToolsMap.end()) {
133 PrintError("No tool corresponding to the language " + LangName + " found");
134 return 0;
135 }
136 return &I->second;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000137}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000138
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000139void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +0000140 if (NodesMap.count(V->Name()) == 0)
141 NodesMap[V->Name()] = Node(this, V);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000142}
143
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000144int CompilationGraph::insertEdge(const std::string& A, Edge* Edg) {
145 Node* B = getNode(Edg->ToolName());
146 if (B == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000147 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000148
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000149 if (A == "root") {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000150 const char** InLangs = B->ToolPtr->InputLanguages();
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000151 for (;*InLangs; ++InLangs)
152 ToolsMap[*InLangs].push_back(IntrusiveRefCntPtr<Edge>(Edg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000153 NodesMap["root"].AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000154 }
155 else {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000156 Node* N = getNode(A);
157 if (N == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000158 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000159
160 N->AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000161 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000162 // Increase the inward edge counter.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000163 B->IncrInEdges();
164
165 return 0;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000166}
167
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000168// Pass input file through the chain until we bump into a Join node or
169// a node that says that it is the last.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000170int CompilationGraph::PassThroughGraph (const sys::Path& InFile,
171 const Node* StartNode,
172 const InputLanguagesSet& InLangs,
173 const sys::Path& TempDir,
174 const LanguageMap& LangMap) const {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000175 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000176 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000177
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000178 while(true) {
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000179 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000180
181 if (CurTool->IsJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000182 JoinTool& JT = static_cast<JoinTool&>(*CurTool);
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000183 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000184 break;
185 }
186
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000187 Action CurAction;
188 if (int ret = CurTool->GenerateAction(CurAction, In, CurNode->HasChildren(),
189 TempDir, InLangs, LangMap)) {
190 return ret;
191 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000192
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000193 if (int ret = CurAction.Execute())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000194 return ret;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000195
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000196 if (CurAction.StopCompilation())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000197 return 0;
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000198
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000199 const Edge* Edg = ChooseEdge(CurNode->OutEdges, InLangs, CurNode->Name());
200 if (Edg == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000201 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000202
203 CurNode = getNode(Edg->ToolName());
204 if (CurNode == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000205 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000206
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000207 In = CurAction.OutFile();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000208 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000209
210 return 0;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000211}
212
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000213// Find the head of the toolchain corresponding to the given file.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000214// Also, insert an input language into InLangs.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000215const Node* CompilationGraph::
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000216FindToolChain(const sys::Path& In, const std::string* ForceLanguage,
217 InputLanguagesSet& InLangs, const LanguageMap& LangMap) const {
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000218
219 // Determine the input language.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000220 const std::string* InLang = LangMap.GetLanguage(In);
221 if (InLang == 0)
222 return 0;
223 const std::string& InLanguage = (ForceLanguage ? *ForceLanguage : *InLang);
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000224
225 // Add the current input language to the input language set.
226 InLangs.insert(InLanguage);
227
228 // Find the toolchain for the input language.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000229 const tools_vector_type* pTV = getToolsVector(InLanguage);
230 if (pTV == 0)
231 return 0;
232
233 const tools_vector_type& TV = *pTV;
234 if (TV.empty()) {
235 PrintError("No toolchain corresponding to language "
236 + InLanguage + " found");
237 return 0;
238 }
239
240 const Edge* Edg = ChooseEdge(TV, InLangs);
241 if (Edg == 0)
242 return 0;
243
244 return getNode(Edg->ToolName());
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000245}
246
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000247// Helper function used by Build().
248// Traverses initial portions of the toolchains (up to the first Join node).
249// This function is also responsible for handling the -x option.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000250int CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
251 const sys::Path& TempDir,
252 const LanguageMap& LangMap) {
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000253 // This is related to -x option handling.
254 cl::list<std::string>::const_iterator xIter = Languages.begin(),
255 xBegin = xIter, xEnd = Languages.end();
256 bool xEmpty = true;
257 const std::string* xLanguage = 0;
258 unsigned xPos = 0, xPosNext = 0, filePos = 0;
259
260 if (xIter != xEnd) {
261 xEmpty = false;
262 xPos = Languages.getPosition(xIter - xBegin);
263 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
264 xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max()
265 : Languages.getPosition(xNext - xBegin);
266 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
267 }
268
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000269 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000270 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000271 CB = B, E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000272 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000273
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000274 // Code for handling the -x option.
275 // Output: std::string* xLanguage (can be NULL).
276 if (!xEmpty) {
277 filePos = InputFilenames.getPosition(B - CB);
278
279 if (xPos < filePos) {
280 if (filePos < xPosNext) {
281 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
282 }
283 else { // filePos >= xPosNext
284 // Skip xIters while filePos > xPosNext
285 while (filePos > xPosNext) {
286 ++xIter;
287 xPos = xPosNext;
288
289 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
290 if (xNext == xEnd)
291 xPosNext = std::numeric_limits<unsigned>::max();
292 else
293 xPosNext = Languages.getPosition(xNext - xBegin);
294 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
295 }
296 }
297 }
298 }
299
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000300 // Find the toolchain corresponding to this file.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000301 const Node* N = FindToolChain(In, xLanguage, InLangs, LangMap);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000302 if (N == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000303 return 1;
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000304 // Pass file through the chain starting at head.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000305 if (int ret = PassThroughGraph(In, N, InLangs, TempDir, LangMap))
306 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000307 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000308
309 return 0;
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000310}
311
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000312// Sort the nodes in topological order.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000313int CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000314 std::queue<const Node*> Q;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000315
316 Node* Root = getNode("root");
317 if (Root == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000318 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000319
320 Q.push(Root);
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000321
322 while (!Q.empty()) {
323 const Node* A = Q.front();
324 Q.pop();
325 Out.push_back(A);
326 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
327 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000328 Node* B = getNode((*EB)->ToolName());
329 if (B == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000330 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000331
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000332 B->DecrInEdges();
333 if (B->HasNoInEdges())
334 Q.push(B);
335 }
336 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000337
338 return 0;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000339}
340
341namespace {
342 bool NotJoinNode(const Node* N) {
343 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
344 }
345}
346
347// Call TopologicalSort and filter the resulting list to include
348// only Join nodes.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000349int CompilationGraph::
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000350TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
351 std::vector<const Node*> TopSorted;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000352 if (int ret = TopologicalSort(TopSorted))
353 return ret;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000354 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
355 std::back_inserter(Out), NotJoinNode);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000356
357 return 0;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000358}
359
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000360int CompilationGraph::Build (const sys::Path& TempDir,
361 const LanguageMap& LangMap) {
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000362 InputLanguagesSet InLangs;
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000363 bool WasSomeActionGenerated = !InputFilenames.empty();
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000364
365 // Traverse initial parts of the toolchains and fill in InLangs.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000366 if (int ret = BuildInitial(InLangs, TempDir, LangMap))
367 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000368
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000369 std::vector<const Node*> JTV;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000370 if (int ret = TopologicalSortFilterJoinNodes(JTV))
371 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000372
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000373 // For all join nodes in topological order:
374 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
375 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000376
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000377 const Node* CurNode = *B;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000378 JoinTool* JT = &static_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000379
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000380 // Are there any files in the join list?
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000381 if (JT->JoinListEmpty() && !(JT->WorksOnEmpty() && InputFilenames.empty()))
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000382 continue;
383
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000384 WasSomeActionGenerated = true;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000385 Action CurAction;
386 if (int ret = JT->GenerateAction(CurAction, CurNode->HasChildren(),
387 TempDir, InLangs, LangMap)) {
388 return ret;
389 }
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000390
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000391 if (int ret = CurAction.Execute())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000392 return ret;
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000393
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000394 if (CurAction.StopCompilation())
395 return 0;
396
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000397 const Edge* Edg = ChooseEdge(CurNode->OutEdges, InLangs, CurNode->Name());
398 if (Edg == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000399 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000400
401 const Node* NextNode = getNode(Edg->ToolName());
402 if (NextNode == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000403 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000404
405 if (int ret = PassThroughGraph(sys::Path(CurAction.OutFile()), NextNode,
406 InLangs, TempDir, LangMap)) {
407 return ret;
408 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000409 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000410
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000411 if (!WasSomeActionGenerated) {
412 PrintError("no input files");
413 return 1;
414 }
415
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000416 return 0;
417}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000418
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000419int CompilationGraph::CheckLanguageNames() const {
420 int ret = 0;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000421
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000422 // Check that names for output and input languages on all edges do match.
423 for (const_nodes_iterator B = this->NodesMap.begin(),
424 E = this->NodesMap.end(); B != E; ++B) {
425
426 const Node & N1 = B->second;
427 if (N1.ToolPtr) {
428 for (Node::const_iterator EB = N1.EdgesBegin(), EE = N1.EdgesEnd();
429 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000430 const Node* N2 = this->getNode((*EB)->ToolName());
431 if (N2 == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000432 return 1;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000433
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000434 if (!N2->ToolPtr) {
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000435 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000436 errs() << "Error: there is an edge from '" << N1.ToolPtr->Name()
437 << "' back to the root!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000438 continue;
439 }
440
441 const char* OutLang = N1.ToolPtr->OutputLanguage();
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000442 const char** InLangs = N2->ToolPtr->InputLanguages();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000443 bool eq = false;
444 for (;*InLangs; ++InLangs) {
445 if (std::strcmp(OutLang, *InLangs) == 0) {
446 eq = true;
447 break;
448 }
449 }
450
451 if (!eq) {
452 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000453 errs() << "Error: Output->input language mismatch in the edge '"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000454 << N1.ToolPtr->Name() << "' -> '" << N2->ToolPtr->Name()
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000455 << "'!\n"
456 << "Expected one of { ";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000457
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000458 InLangs = N2->ToolPtr->InputLanguages();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000459 for (;*InLangs; ++InLangs) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000460 errs() << '\'' << *InLangs << (*(InLangs+1) ? "', " : "'");
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000461 }
462
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000463 errs() << " }, but got '" << OutLang << "'!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000464 }
465
466 }
467 }
468 }
469
470 return ret;
471}
472
473int CompilationGraph::CheckMultipleDefaultEdges() const {
474 int ret = 0;
475 InputLanguagesSet Dummy;
476
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000477 // For all nodes, just iterate over the outgoing edges and check if there is
478 // more than one edge with maximum weight.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000479 for (const_nodes_iterator B = this->NodesMap.begin(),
480 E = this->NodesMap.end(); B != E; ++B) {
481 const Node& N = B->second;
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +0000482 int MaxWeight = 0;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000483
484 // Ignore the root node.
485 if (!N.ToolPtr)
486 continue;
487
488 for (Node::const_iterator EB = N.EdgesBegin(), EE = N.EdgesEnd();
489 EB != EE; ++EB) {
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +0000490 int EdgeWeight = (*EB)->Weight(Dummy);
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000491 if (EdgeWeight > MaxWeight) {
492 MaxWeight = EdgeWeight;
493 }
494 else if (EdgeWeight == MaxWeight) {
495 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000496 errs() << "Error: there are multiple maximal edges stemming from the '"
497 << N.ToolPtr->Name() << "' node!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000498 break;
499 }
500 }
501 }
502
503 return ret;
504}
505
506int CompilationGraph::CheckCycles() {
507 unsigned deleted = 0;
508 std::queue<Node*> Q;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000509
510 Node* Root = getNode("root");
511 if (Root == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000512 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000513
514 Q.push(Root);
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000515
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000516 // Try to delete all nodes that have no ingoing edges, starting from the
517 // root. If there are any nodes left after this operation, then we have a
518 // cycle. This relies on '--check-graph' not performing the topological sort.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000519 while (!Q.empty()) {
520 Node* A = Q.front();
521 Q.pop();
522 ++deleted;
523
524 for (Node::iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
525 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000526 Node* B = getNode((*EB)->ToolName());
527 if (B == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000528 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000529
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000530 B->DecrInEdges();
531 if (B->HasNoInEdges())
532 Q.push(B);
533 }
534 }
535
536 if (deleted != NodesMap.size()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000537 errs() << "Error: there are cycles in the compilation graph!\n"
538 << "Try inspecting the diagram produced by "
539 << "'llvmc --view-graph'.\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000540 return 1;
541 }
542
543 return 0;
544}
545
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000546int CompilationGraph::Check () {
547 // We try to catch as many errors as we can in one go.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000548 int errs = 0;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000549 int ret = 0;
550
551 // Check that output/input language names match.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000552 ret = this->CheckLanguageNames();
553 if (ret < 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000554 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000555 errs += ret;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000556
557 // Check for multiple default edges.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000558 ret = this->CheckMultipleDefaultEdges();
559 if (ret < 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000560 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000561 errs += ret;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000562
563 // Check for cycles.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000564 ret = this->CheckCycles();
565 if (ret < 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000566 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000567 errs += ret;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000568
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000569 return errs;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000570}
571
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000572// Code related to graph visualization.
573
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000574namespace llvm {
575 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000576 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000577 : public DefaultDOTGraphTraits
578 {
Tobias Grosserab010692009-11-30 13:34:51 +0000579 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000580
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000581 template<typename GraphType>
Tobias Grosserb5eedf22009-11-30 13:14:13 +0000582 static std::string getNodeLabel(const Node* N, const GraphType&)
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000583 {
584 if (N->ToolPtr)
585 if (N->ToolPtr->IsJoin())
586 return N->Name() + "\n (join" +
587 (N->HasChildren() ? ")"
588 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
589 else
590 return N->Name();
591 else
592 return "root";
593 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000594
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000595 template<typename EdgeIter>
596 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000597 if (N->ToolPtr) {
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000598 return N->ToolPtr->OutputLanguage();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000599 }
600 else {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000601 const char** InLangs = I->ToolPtr->InputLanguages();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000602 std::string ret;
603
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000604 for (; *InLangs; ++InLangs) {
605 if (*(InLangs + 1)) {
606 ret += *InLangs;
607 ret += ", ";
608 }
609 else {
610 ret += *InLangs;
611 }
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000612 }
613
614 return ret;
615 }
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000616 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000617 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000618
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000619}
620
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000621int CompilationGraph::writeGraph(const std::string& OutputFilename) {
Chris Lattner103289e2009-08-23 07:19:13 +0000622 std::string ErrorInfo;
623 raw_fd_ostream O(OutputFilename.c_str(), ErrorInfo);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000624
Chris Lattner103289e2009-08-23 07:19:13 +0000625 if (ErrorInfo.empty()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000626 errs() << "Writing '"<< OutputFilename << "' file...";
Mikhail Glushenkov3cd3c722009-03-26 21:23:48 +0000627 llvm::WriteGraph(O, this);
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000628 errs() << "done.\n";
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000629 }
630 else {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000631 PrintError("Error opening file '" + OutputFilename + "' for writing!");
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000632 return 1;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000633 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000634
635 return 0;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000636}
637
638void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000639 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000640}