blob: 87a2c4817a842487c8d260d2ac18ba7976c4a632 [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 Glushenkove6334d82010-09-15 15:20:41 +0000221 const std::string* InLang = (ForceLanguage ? ForceLanguage
222 : LangMap.GetLanguage(In));
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000223 if (InLang == 0)
224 return 0;
Mikhail Glushenkove6334d82010-09-15 15:20:41 +0000225 const std::string& InLanguage = *InLang;
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000226
227 // Add the current input language to the input language set.
228 InLangs.insert(InLanguage);
229
230 // Find the toolchain for the input language.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000231 const tools_vector_type* pTV = getToolsVector(InLanguage);
232 if (pTV == 0)
233 return 0;
234
235 const tools_vector_type& TV = *pTV;
236 if (TV.empty()) {
237 PrintError("No toolchain corresponding to language "
238 + InLanguage + " found");
239 return 0;
240 }
241
242 const Edge* Edg = ChooseEdge(TV, InLangs);
243 if (Edg == 0)
244 return 0;
245
246 return getNode(Edg->ToolName());
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000247}
248
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000249// Helper function used by Build().
250// Traverses initial portions of the toolchains (up to the first Join node).
251// This function is also responsible for handling the -x option.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000252int CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
253 const sys::Path& TempDir,
254 const LanguageMap& LangMap) {
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000255 // This is related to -x option handling.
256 cl::list<std::string>::const_iterator xIter = Languages.begin(),
257 xBegin = xIter, xEnd = Languages.end();
258 bool xEmpty = true;
259 const std::string* xLanguage = 0;
260 unsigned xPos = 0, xPosNext = 0, filePos = 0;
261
262 if (xIter != xEnd) {
263 xEmpty = false;
264 xPos = Languages.getPosition(xIter - xBegin);
265 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
266 xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max()
267 : Languages.getPosition(xNext - xBegin);
268 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
269 }
270
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000271 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000272 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000273 CB = B, E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000274 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000275
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000276 // Code for handling the -x option.
277 // Output: std::string* xLanguage (can be NULL).
278 if (!xEmpty) {
279 filePos = InputFilenames.getPosition(B - CB);
280
281 if (xPos < filePos) {
282 if (filePos < xPosNext) {
283 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
284 }
285 else { // filePos >= xPosNext
286 // Skip xIters while filePos > xPosNext
287 while (filePos > xPosNext) {
288 ++xIter;
289 xPos = xPosNext;
290
291 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
292 if (xNext == xEnd)
293 xPosNext = std::numeric_limits<unsigned>::max();
294 else
295 xPosNext = Languages.getPosition(xNext - xBegin);
296 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
297 }
298 }
299 }
300 }
301
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000302 // Find the toolchain corresponding to this file.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000303 const Node* N = FindToolChain(In, xLanguage, InLangs, LangMap);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000304 if (N == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000305 return 1;
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000306 // Pass file through the chain starting at head.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000307 if (int ret = PassThroughGraph(In, N, InLangs, TempDir, LangMap))
308 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000309 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000310
311 return 0;
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000312}
313
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000314// Sort the nodes in topological order.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000315int CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000316 std::queue<const Node*> Q;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000317
318 Node* Root = getNode("root");
319 if (Root == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000320 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000321
322 Q.push(Root);
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000323
324 while (!Q.empty()) {
325 const Node* A = Q.front();
326 Q.pop();
327 Out.push_back(A);
328 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
329 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000330 Node* B = getNode((*EB)->ToolName());
331 if (B == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000332 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000333
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000334 B->DecrInEdges();
335 if (B->HasNoInEdges())
336 Q.push(B);
337 }
338 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000339
340 return 0;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000341}
342
343namespace {
344 bool NotJoinNode(const Node* N) {
345 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
346 }
347}
348
349// Call TopologicalSort and filter the resulting list to include
350// only Join nodes.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000351int CompilationGraph::
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000352TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
353 std::vector<const Node*> TopSorted;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000354 if (int ret = TopologicalSort(TopSorted))
355 return ret;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000356 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
357 std::back_inserter(Out), NotJoinNode);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000358
359 return 0;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000360}
361
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000362int CompilationGraph::Build (const sys::Path& TempDir,
363 const LanguageMap& LangMap) {
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000364 InputLanguagesSet InLangs;
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000365 bool WasSomeActionGenerated = !InputFilenames.empty();
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000366
367 // Traverse initial parts of the toolchains and fill in InLangs.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000368 if (int ret = BuildInitial(InLangs, TempDir, LangMap))
369 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000370
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000371 std::vector<const Node*> JTV;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000372 if (int ret = TopologicalSortFilterJoinNodes(JTV))
373 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000374
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000375 // For all join nodes in topological order:
376 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
377 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000378
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000379 const Node* CurNode = *B;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000380 JoinTool* JT = &static_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000381
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000382 // Are there any files in the join list?
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000383 if (JT->JoinListEmpty() && !(JT->WorksOnEmpty() && InputFilenames.empty()))
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000384 continue;
385
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000386 WasSomeActionGenerated = true;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000387 Action CurAction;
388 if (int ret = JT->GenerateAction(CurAction, CurNode->HasChildren(),
389 TempDir, InLangs, LangMap)) {
390 return ret;
391 }
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000392
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000393 if (int ret = CurAction.Execute())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000394 return ret;
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000395
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000396 if (CurAction.StopCompilation())
397 return 0;
398
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000399 const Edge* Edg = ChooseEdge(CurNode->OutEdges, InLangs, CurNode->Name());
400 if (Edg == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000401 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000402
403 const Node* NextNode = getNode(Edg->ToolName());
404 if (NextNode == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000405 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000406
407 if (int ret = PassThroughGraph(sys::Path(CurAction.OutFile()), NextNode,
408 InLangs, TempDir, LangMap)) {
409 return ret;
410 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000411 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000412
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000413 if (!WasSomeActionGenerated) {
414 PrintError("no input files");
415 return 1;
416 }
417
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000418 return 0;
419}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000420
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000421int CompilationGraph::CheckLanguageNames() const {
422 int ret = 0;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000423
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000424 // Check that names for output and input languages on all edges do match.
425 for (const_nodes_iterator B = this->NodesMap.begin(),
426 E = this->NodesMap.end(); B != E; ++B) {
427
428 const Node & N1 = B->second;
429 if (N1.ToolPtr) {
430 for (Node::const_iterator EB = N1.EdgesBegin(), EE = N1.EdgesEnd();
431 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000432 const Node* N2 = this->getNode((*EB)->ToolName());
433 if (N2 == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000434 return 1;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000435
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000436 if (!N2->ToolPtr) {
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000437 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000438 errs() << "Error: there is an edge from '" << N1.ToolPtr->Name()
439 << "' back to the root!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000440 continue;
441 }
442
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000443 const char** OutLangs = N1.ToolPtr->OutputLanguages();
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000444 const char** InLangs = N2->ToolPtr->InputLanguages();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000445 bool eq = false;
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000446 const char* OutLang = 0;
447 for (;*OutLangs; ++OutLangs) {
448 OutLang = *OutLangs;
449 for (;*InLangs; ++InLangs) {
450 if (std::strcmp(OutLang, *InLangs) == 0) {
451 eq = true;
452 break;
453 }
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000454 }
455 }
456
457 if (!eq) {
458 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000459 errs() << "Error: Output->input language mismatch in the edge '"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000460 << N1.ToolPtr->Name() << "' -> '" << N2->ToolPtr->Name()
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000461 << "'!\n"
462 << "Expected one of { ";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000463
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000464 InLangs = N2->ToolPtr->InputLanguages();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000465 for (;*InLangs; ++InLangs) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000466 errs() << '\'' << *InLangs << (*(InLangs+1) ? "', " : "'");
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000467 }
468
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000469 errs() << " }, but got '" << OutLang << "'!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000470 }
471
472 }
473 }
474 }
475
476 return ret;
477}
478
479int CompilationGraph::CheckMultipleDefaultEdges() const {
480 int ret = 0;
481 InputLanguagesSet Dummy;
482
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000483 // For all nodes, just iterate over the outgoing edges and check if there is
484 // more than one edge with maximum weight.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000485 for (const_nodes_iterator B = this->NodesMap.begin(),
486 E = this->NodesMap.end(); B != E; ++B) {
487 const Node& N = B->second;
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000488 int MaxWeight = -1024;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000489
490 // Ignore the root node.
491 if (!N.ToolPtr)
492 continue;
493
494 for (Node::const_iterator EB = N.EdgesBegin(), EE = N.EdgesEnd();
495 EB != EE; ++EB) {
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +0000496 int EdgeWeight = (*EB)->Weight(Dummy);
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000497 if (EdgeWeight > MaxWeight) {
498 MaxWeight = EdgeWeight;
499 }
500 else if (EdgeWeight == MaxWeight) {
501 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000502 errs() << "Error: there are multiple maximal edges stemming from the '"
503 << N.ToolPtr->Name() << "' node!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000504 break;
505 }
506 }
507 }
508
509 return ret;
510}
511
512int CompilationGraph::CheckCycles() {
513 unsigned deleted = 0;
514 std::queue<Node*> Q;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000515
516 Node* Root = getNode("root");
517 if (Root == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000518 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000519
520 Q.push(Root);
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000521
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000522 // Try to delete all nodes that have no ingoing edges, starting from the
523 // root. If there are any nodes left after this operation, then we have a
524 // cycle. This relies on '--check-graph' not performing the topological sort.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000525 while (!Q.empty()) {
526 Node* A = Q.front();
527 Q.pop();
528 ++deleted;
529
530 for (Node::iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
531 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000532 Node* B = getNode((*EB)->ToolName());
533 if (B == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000534 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000535
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000536 B->DecrInEdges();
537 if (B->HasNoInEdges())
538 Q.push(B);
539 }
540 }
541
542 if (deleted != NodesMap.size()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000543 errs() << "Error: there are cycles in the compilation graph!\n"
544 << "Try inspecting the diagram produced by "
545 << "'llvmc --view-graph'.\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000546 return 1;
547 }
548
549 return 0;
550}
551
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000552int CompilationGraph::Check () {
553 // We try to catch as many errors as we can in one go.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000554 int errs = 0;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000555 int ret = 0;
556
557 // Check that output/input language names match.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000558 ret = this->CheckLanguageNames();
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 multiple default edges.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000564 ret = this->CheckMultipleDefaultEdges();
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
569 // Check for cycles.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000570 ret = this->CheckCycles();
571 if (ret < 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000572 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000573 errs += ret;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000574
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000575 return errs;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000576}
577
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000578// Code related to graph visualization.
579
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000580namespace {
581
582std::string SquashStrArray (const char** StrArr) {
583 std::string ret;
584
585 for (; *StrArr; ++StrArr) {
586 if (*(StrArr + 1)) {
587 ret += *StrArr;
588 ret += ", ";
589 }
590 else {
591 ret += *StrArr;
592 }
593 }
594
595 return ret;
596}
597
598} // End anonymous namespace.
599
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000600namespace llvm {
601 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000602 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000603 : public DefaultDOTGraphTraits
604 {
Tobias Grosserab010692009-11-30 13:34:51 +0000605 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000606
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000607 template<typename GraphType>
Tobias Grosserb5eedf22009-11-30 13:14:13 +0000608 static std::string getNodeLabel(const Node* N, const GraphType&)
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000609 {
610 if (N->ToolPtr)
611 if (N->ToolPtr->IsJoin())
612 return N->Name() + "\n (join" +
613 (N->HasChildren() ? ")"
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000614 : std::string(": ") +
615 SquashStrArray(N->ToolPtr->OutputLanguages()) + ')');
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000616 else
617 return N->Name();
618 else
619 return "root";
620 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000621
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000622 template<typename EdgeIter>
623 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000624 if (N->ToolPtr) {
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000625 return SquashStrArray(N->ToolPtr->OutputLanguages());
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000626 }
627 else {
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000628 return SquashStrArray(I->ToolPtr->InputLanguages());
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000629 }
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000630 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000631 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000632
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000633} // End namespace llvm
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000634
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000635int CompilationGraph::writeGraph(const std::string& OutputFilename) {
Chris Lattner103289e2009-08-23 07:19:13 +0000636 std::string ErrorInfo;
637 raw_fd_ostream O(OutputFilename.c_str(), ErrorInfo);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000638
Chris Lattner103289e2009-08-23 07:19:13 +0000639 if (ErrorInfo.empty()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000640 errs() << "Writing '"<< OutputFilename << "' file...";
Mikhail Glushenkov3cd3c722009-03-26 21:23:48 +0000641 llvm::WriteGraph(O, this);
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000642 errs() << "done.\n";
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000643 }
644 else {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000645 PrintError("Error opening file '" + OutputFilename + "' for writing!");
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000646 return 1;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000647 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000648
649 return 0;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000650}
651
652void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000653 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000654}