blob: d0c0e15bcdb7ba626ffa8bc796e9600deeb0e64c [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
Mikhail Glushenkov316abba2010-08-23 19:24:12 +000058 // TODO: fix calculation of SingleMax.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000059 for (typename C::const_iterator B = EdgesContainer.begin(),
60 E = EdgesContainer.end(); B != E; ++B) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000061 const Edge* e = B->getPtr();
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +000062 int EW = e->Weight(InLangs);
63 if (EW < 0) {
64 // (error) invocation in TableGen -> we don't need to print an error
65 // message.
66 return 0;
67 }
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000068 if (EW > MaxWeight) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000069 MaxEdge = e;
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000070 MaxWeight = EW;
71 SingleMax = true;
Mikhail Glushenkove0ff9ae2008-05-06 18:18:58 +000072 } else if (EW == MaxWeight) {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000073 SingleMax = false;
74 }
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000075 }
76
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000077 if (!SingleMax) {
78 PrintError("Node " + NodeName + ": multiple maximal outward edges found!"
79 " Most probably a specification error.");
80 return 0;
81 }
82 if (!MaxEdge) {
83 PrintError("Node " + NodeName + ": no maximal outward edge found!"
84 " Most probably a specification error.");
85 return 0;
86 }
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000087 return MaxEdge;
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000088 }
89
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000090}
91
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +000092void Node::AddEdge(Edge* Edg) {
93 // If there already was an edge between two nodes, modify it instead
94 // of adding a new edge.
95 const std::string& ToolName = Edg->ToolName();
96 for (container_type::iterator B = OutEdges.begin(), E = OutEdges.end();
97 B != E; ++B) {
98 if ((*B)->ToolName() == ToolName) {
99 llvm::IntrusiveRefCntPtr<Edge>(Edg).swap(*B);
100 return;
101 }
102 }
103 OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(Edg));
104}
105
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000106CompilationGraph::CompilationGraph() {
107 NodesMap["root"] = Node(this);
108}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000109
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000110Node* CompilationGraph::getNode(const std::string& ToolName) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000111 nodes_map_type::iterator I = NodesMap.find(ToolName);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000112 if (I == NodesMap.end()) {
113 PrintError("Node " + ToolName + " is not in the graph");
114 return 0;
115 }
116 return &I->second;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000117}
118
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000119const Node* CompilationGraph::getNode(const std::string& ToolName) const {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000120 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000121 if (I == NodesMap.end()) {
122 PrintError("Node " + ToolName + " is not in the graph!");
123 return 0;
124 }
125 return &I->second;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000126}
127
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000128// Find the tools list corresponding to the given language name.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000129const CompilationGraph::tools_vector_type*
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000130CompilationGraph::getToolsVector(const std::string& LangName) const
131{
132 tools_map_type::const_iterator I = ToolsMap.find(LangName);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000133 if (I == ToolsMap.end()) {
134 PrintError("No tool corresponding to the language " + LangName + " found");
135 return 0;
136 }
137 return &I->second;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000138}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000139
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000140void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +0000141 if (NodesMap.count(V->Name()) == 0)
142 NodesMap[V->Name()] = Node(this, V);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000143}
144
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000145int CompilationGraph::insertEdge(const std::string& A, Edge* Edg) {
146 Node* B = getNode(Edg->ToolName());
147 if (B == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000148 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000149
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000150 if (A == "root") {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000151 const char** InLangs = B->ToolPtr->InputLanguages();
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000152 for (;*InLangs; ++InLangs)
153 ToolsMap[*InLangs].push_back(IntrusiveRefCntPtr<Edge>(Edg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000154 NodesMap["root"].AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000155 }
156 else {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000157 Node* N = getNode(A);
158 if (N == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000159 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000160
161 N->AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000162 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000163 // Increase the inward edge counter.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000164 B->IncrInEdges();
165
166 return 0;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000167}
168
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000169// Pass input file through the chain until we bump into a Join node or
170// a node that says that it is the last.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000171int CompilationGraph::PassThroughGraph (const sys::Path& InFile,
172 const Node* StartNode,
173 const InputLanguagesSet& InLangs,
174 const sys::Path& TempDir,
175 const LanguageMap& LangMap) const {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000176 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000177 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000178
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000179 while(true) {
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000180 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000181
182 if (CurTool->IsJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000183 JoinTool& JT = static_cast<JoinTool&>(*CurTool);
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000184 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000185 break;
186 }
187
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000188 Action CurAction;
189 if (int ret = CurTool->GenerateAction(CurAction, In, CurNode->HasChildren(),
190 TempDir, InLangs, LangMap)) {
191 return ret;
192 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000193
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000194 if (int ret = CurAction.Execute())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000195 return ret;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000196
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000197 if (CurAction.StopCompilation())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000198 return 0;
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000199
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000200 const Edge* Edg = ChooseEdge(CurNode->OutEdges, InLangs, CurNode->Name());
201 if (Edg == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000202 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000203
204 CurNode = getNode(Edg->ToolName());
205 if (CurNode == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000206 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000207
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000208 In = CurAction.OutFile();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000209 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000210
211 return 0;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000212}
213
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000214// Find the head of the toolchain corresponding to the given file.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000215// Also, insert an input language into InLangs.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000216const Node* CompilationGraph::
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000217FindToolChain(const sys::Path& In, const std::string* ForceLanguage,
218 InputLanguagesSet& InLangs, const LanguageMap& LangMap) const {
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000219
220 // Determine the input language.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000221 const std::string* InLang = LangMap.GetLanguage(In);
222 if (InLang == 0)
223 return 0;
224 const std::string& InLanguage = (ForceLanguage ? *ForceLanguage : *InLang);
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000225
226 // Add the current input language to the input language set.
227 InLangs.insert(InLanguage);
228
229 // Find the toolchain for the input language.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000230 const tools_vector_type* pTV = getToolsVector(InLanguage);
231 if (pTV == 0)
232 return 0;
233
234 const tools_vector_type& TV = *pTV;
235 if (TV.empty()) {
236 PrintError("No toolchain corresponding to language "
237 + InLanguage + " found");
238 return 0;
239 }
240
241 const Edge* Edg = ChooseEdge(TV, InLangs);
242 if (Edg == 0)
243 return 0;
244
245 return getNode(Edg->ToolName());
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000246}
247
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000248// Helper function used by Build().
249// Traverses initial portions of the toolchains (up to the first Join node).
250// This function is also responsible for handling the -x option.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000251int CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
252 const sys::Path& TempDir,
253 const LanguageMap& LangMap) {
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000254 // This is related to -x option handling.
255 cl::list<std::string>::const_iterator xIter = Languages.begin(),
256 xBegin = xIter, xEnd = Languages.end();
257 bool xEmpty = true;
258 const std::string* xLanguage = 0;
259 unsigned xPos = 0, xPosNext = 0, filePos = 0;
260
261 if (xIter != xEnd) {
262 xEmpty = false;
263 xPos = Languages.getPosition(xIter - xBegin);
264 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
265 xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max()
266 : Languages.getPosition(xNext - xBegin);
267 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
268 }
269
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000270 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000271 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000272 CB = B, E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000273 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000274
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000275 // Code for handling the -x option.
276 // Output: std::string* xLanguage (can be NULL).
277 if (!xEmpty) {
278 filePos = InputFilenames.getPosition(B - CB);
279
280 if (xPos < filePos) {
281 if (filePos < xPosNext) {
282 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
283 }
284 else { // filePos >= xPosNext
285 // Skip xIters while filePos > xPosNext
286 while (filePos > xPosNext) {
287 ++xIter;
288 xPos = xPosNext;
289
290 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
291 if (xNext == xEnd)
292 xPosNext = std::numeric_limits<unsigned>::max();
293 else
294 xPosNext = Languages.getPosition(xNext - xBegin);
295 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
296 }
297 }
298 }
299 }
300
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000301 // Find the toolchain corresponding to this file.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000302 const Node* N = FindToolChain(In, xLanguage, InLangs, LangMap);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000303 if (N == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000304 return 1;
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000305 // Pass file through the chain starting at head.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000306 if (int ret = PassThroughGraph(In, N, InLangs, TempDir, LangMap))
307 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000308 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000309
310 return 0;
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000311}
312
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000313// Sort the nodes in topological order.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000314int CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000315 std::queue<const Node*> Q;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000316
317 Node* Root = getNode("root");
318 if (Root == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000319 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000320
321 Q.push(Root);
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000322
323 while (!Q.empty()) {
324 const Node* A = Q.front();
325 Q.pop();
326 Out.push_back(A);
327 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
328 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000329 Node* B = getNode((*EB)->ToolName());
330 if (B == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000331 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000332
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000333 B->DecrInEdges();
334 if (B->HasNoInEdges())
335 Q.push(B);
336 }
337 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000338
339 return 0;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000340}
341
342namespace {
343 bool NotJoinNode(const Node* N) {
344 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
345 }
346}
347
348// Call TopologicalSort and filter the resulting list to include
349// only Join nodes.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000350int CompilationGraph::
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000351TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
352 std::vector<const Node*> TopSorted;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000353 if (int ret = TopologicalSort(TopSorted))
354 return ret;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000355 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
356 std::back_inserter(Out), NotJoinNode);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000357
358 return 0;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000359}
360
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000361int CompilationGraph::Build (const sys::Path& TempDir,
362 const LanguageMap& LangMap) {
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000363 InputLanguagesSet InLangs;
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000364 bool WasSomeActionGenerated = !InputFilenames.empty();
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000365
366 // Traverse initial parts of the toolchains and fill in InLangs.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000367 if (int ret = BuildInitial(InLangs, TempDir, LangMap))
368 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000369
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000370 std::vector<const Node*> JTV;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000371 if (int ret = TopologicalSortFilterJoinNodes(JTV))
372 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000373
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000374 // For all join nodes in topological order:
375 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
376 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000377
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000378 const Node* CurNode = *B;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000379 JoinTool* JT = &static_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000380
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000381 // Are there any files in the join list?
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000382 if (JT->JoinListEmpty() && !(JT->WorksOnEmpty() && InputFilenames.empty()))
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000383 continue;
384
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000385 WasSomeActionGenerated = true;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000386 Action CurAction;
387 if (int ret = JT->GenerateAction(CurAction, CurNode->HasChildren(),
388 TempDir, InLangs, LangMap)) {
389 return ret;
390 }
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000391
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000392 if (int ret = CurAction.Execute())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000393 return ret;
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000394
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000395 if (CurAction.StopCompilation())
396 return 0;
397
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000398 const Edge* Edg = ChooseEdge(CurNode->OutEdges, InLangs, CurNode->Name());
399 if (Edg == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000400 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000401
402 const Node* NextNode = getNode(Edg->ToolName());
403 if (NextNode == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000404 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000405
406 if (int ret = PassThroughGraph(sys::Path(CurAction.OutFile()), NextNode,
407 InLangs, TempDir, LangMap)) {
408 return ret;
409 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000410 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000411
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000412 if (!WasSomeActionGenerated) {
413 PrintError("no input files");
414 return 1;
415 }
416
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000417 return 0;
418}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000419
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000420int CompilationGraph::CheckLanguageNames() const {
421 int ret = 0;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000422
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000423 // Check that names for output and input languages on all edges do match.
424 for (const_nodes_iterator B = this->NodesMap.begin(),
425 E = this->NodesMap.end(); B != E; ++B) {
426
427 const Node & N1 = B->second;
428 if (N1.ToolPtr) {
429 for (Node::const_iterator EB = N1.EdgesBegin(), EE = N1.EdgesEnd();
430 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000431 const Node* N2 = this->getNode((*EB)->ToolName());
432 if (N2 == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000433 return 1;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000434
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000435 if (!N2->ToolPtr) {
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000436 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000437 errs() << "Error: there is an edge from '" << N1.ToolPtr->Name()
438 << "' back to the root!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000439 continue;
440 }
441
442 const char* OutLang = N1.ToolPtr->OutputLanguage();
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000443 const char** InLangs = N2->ToolPtr->InputLanguages();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000444 bool eq = false;
445 for (;*InLangs; ++InLangs) {
446 if (std::strcmp(OutLang, *InLangs) == 0) {
447 eq = true;
448 break;
449 }
450 }
451
452 if (!eq) {
453 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000454 errs() << "Error: Output->input language mismatch in the edge '"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000455 << N1.ToolPtr->Name() << "' -> '" << N2->ToolPtr->Name()
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000456 << "'!\n"
457 << "Expected one of { ";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000458
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000459 InLangs = N2->ToolPtr->InputLanguages();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000460 for (;*InLangs; ++InLangs) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000461 errs() << '\'' << *InLangs << (*(InLangs+1) ? "', " : "'");
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000462 }
463
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000464 errs() << " }, but got '" << OutLang << "'!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000465 }
466
467 }
468 }
469 }
470
471 return ret;
472}
473
474int CompilationGraph::CheckMultipleDefaultEdges() const {
475 int ret = 0;
476 InputLanguagesSet Dummy;
477
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000478 // For all nodes, just iterate over the outgoing edges and check if there is
479 // more than one edge with maximum weight.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000480 for (const_nodes_iterator B = this->NodesMap.begin(),
481 E = this->NodesMap.end(); B != E; ++B) {
482 const Node& N = B->second;
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +0000483 int MaxWeight = 0;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000484
485 // Ignore the root node.
486 if (!N.ToolPtr)
487 continue;
488
489 for (Node::const_iterator EB = N.EdgesBegin(), EE = N.EdgesEnd();
490 EB != EE; ++EB) {
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +0000491 int EdgeWeight = (*EB)->Weight(Dummy);
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000492 if (EdgeWeight > MaxWeight) {
493 MaxWeight = EdgeWeight;
494 }
495 else if (EdgeWeight == MaxWeight) {
496 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000497 errs() << "Error: there are multiple maximal edges stemming from the '"
498 << N.ToolPtr->Name() << "' node!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000499 break;
500 }
501 }
502 }
503
504 return ret;
505}
506
507int CompilationGraph::CheckCycles() {
508 unsigned deleted = 0;
509 std::queue<Node*> Q;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000510
511 Node* Root = getNode("root");
512 if (Root == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000513 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000514
515 Q.push(Root);
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000516
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000517 // Try to delete all nodes that have no ingoing edges, starting from the
518 // root. If there are any nodes left after this operation, then we have a
519 // cycle. This relies on '--check-graph' not performing the topological sort.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000520 while (!Q.empty()) {
521 Node* A = Q.front();
522 Q.pop();
523 ++deleted;
524
525 for (Node::iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
526 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000527 Node* B = getNode((*EB)->ToolName());
528 if (B == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000529 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000530
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000531 B->DecrInEdges();
532 if (B->HasNoInEdges())
533 Q.push(B);
534 }
535 }
536
537 if (deleted != NodesMap.size()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000538 errs() << "Error: there are cycles in the compilation graph!\n"
539 << "Try inspecting the diagram produced by "
540 << "'llvmc --view-graph'.\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000541 return 1;
542 }
543
544 return 0;
545}
546
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000547int CompilationGraph::Check () {
548 // We try to catch as many errors as we can in one go.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000549 int errs = 0;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000550 int ret = 0;
551
552 // Check that output/input language names match.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000553 ret = this->CheckLanguageNames();
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 multiple default edges.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000559 ret = this->CheckMultipleDefaultEdges();
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
564 // Check for cycles.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000565 ret = this->CheckCycles();
566 if (ret < 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000567 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000568 errs += ret;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000569
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000570 return errs;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000571}
572
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000573// Code related to graph visualization.
574
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000575namespace llvm {
576 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000577 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000578 : public DefaultDOTGraphTraits
579 {
Tobias Grosserab010692009-11-30 13:34:51 +0000580 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000581
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000582 template<typename GraphType>
Tobias Grosserb5eedf22009-11-30 13:14:13 +0000583 static std::string getNodeLabel(const Node* N, const GraphType&)
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000584 {
585 if (N->ToolPtr)
586 if (N->ToolPtr->IsJoin())
587 return N->Name() + "\n (join" +
588 (N->HasChildren() ? ")"
589 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
590 else
591 return N->Name();
592 else
593 return "root";
594 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000595
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000596 template<typename EdgeIter>
597 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000598 if (N->ToolPtr) {
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000599 return N->ToolPtr->OutputLanguage();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000600 }
601 else {
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000602 const char** InLangs = I->ToolPtr->InputLanguages();
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000603 std::string ret;
604
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000605 for (; *InLangs; ++InLangs) {
606 if (*(InLangs + 1)) {
607 ret += *InLangs;
608 ret += ", ";
609 }
610 else {
611 ret += *InLangs;
612 }
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000613 }
614
615 return ret;
616 }
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000617 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000618 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000619
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000620}
621
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000622int CompilationGraph::writeGraph(const std::string& OutputFilename) {
Chris Lattner103289e2009-08-23 07:19:13 +0000623 std::string ErrorInfo;
624 raw_fd_ostream O(OutputFilename.c_str(), ErrorInfo);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000625
Chris Lattner103289e2009-08-23 07:19:13 +0000626 if (ErrorInfo.empty()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000627 errs() << "Writing '"<< OutputFilename << "' file...";
Mikhail Glushenkov3cd3c722009-03-26 21:23:48 +0000628 llvm::WriteGraph(O, this);
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000629 errs() << "done.\n";
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000630 }
631 else {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000632 PrintError("Error opening file '" + OutputFilename + "' for writing!");
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000633 return 1;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000634 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000635
636 return 0;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000637}
638
639void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000640 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000641}