blob: 33c6566499b8777bd7084293115be9df8525a0f5 [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 {
Michael J. Spencer73855092010-12-18 22:23:07 +000035 // Remove the '.'.
36 StringRef suf = sys::path::extension(File.str()).substr(1);
Mikhail Glushenkov9660c5d2010-02-23 09:05:21 +000037 LanguageMap::const_iterator Lang =
38 this->find(suf.empty() ? "*empty*" : suf);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000039 if (Lang == this->end()) {
40 PrintError("File '" + File.str() + "' has unknown suffix '"
41 + suf.str() + '\'');
42 return 0;
43 }
44 return &Lang->second;
Mikhail Glushenkov2e73e852008-05-30 06:19:52 +000045 }
46}
47
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000048namespace {
49
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +000050 /// ChooseEdge - Return the edge with the maximum weight. Returns 0 on error.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000051 template <class C>
52 const Edge* ChooseEdge(const C& EdgesContainer,
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +000053 const InputLanguagesSet& InLangs,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000054 const std::string& NodeName = "root") {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000055 const Edge* MaxEdge = 0;
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +000056 int MaxWeight = 0;
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000057 bool SingleMax = true;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000058
Mikhail Glushenkov316abba2010-08-23 19:24:12 +000059 // TODO: fix calculation of SingleMax.
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000060 for (typename C::const_iterator B = EdgesContainer.begin(),
61 E = EdgesContainer.end(); B != E; ++B) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000062 const Edge* e = B->getPtr();
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +000063 int EW = e->Weight(InLangs);
64 if (EW < 0) {
65 // (error) invocation in TableGen -> we don't need to print an error
66 // message.
67 return 0;
68 }
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000069 if (EW > MaxWeight) {
Bill Wendling46f7a5e2008-10-02 18:39:11 +000070 MaxEdge = e;
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000071 MaxWeight = EW;
72 SingleMax = true;
Mikhail Glushenkove0ff9ae2008-05-06 18:18:58 +000073 } else if (EW == MaxWeight) {
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000074 SingleMax = false;
75 }
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000076 }
77
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +000078 if (!SingleMax) {
79 PrintError("Node " + NodeName + ": multiple maximal outward edges found!"
80 " Most probably a specification error.");
81 return 0;
82 }
83 if (!MaxEdge) {
84 PrintError("Node " + NodeName + ": no maximal outward edge found!"
85 " Most probably a specification error.");
86 return 0;
87 }
Mikhail Glushenkovbb8b58d2008-05-06 18:14:24 +000088 return MaxEdge;
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000089 }
90
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000091}
92
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +000093void Node::AddEdge(Edge* Edg) {
94 // If there already was an edge between two nodes, modify it instead
95 // of adding a new edge.
96 const std::string& ToolName = Edg->ToolName();
97 for (container_type::iterator B = OutEdges.begin(), E = OutEdges.end();
98 B != E; ++B) {
99 if ((*B)->ToolName() == ToolName) {
100 llvm::IntrusiveRefCntPtr<Edge>(Edg).swap(*B);
101 return;
102 }
103 }
104 OutEdges.push_back(llvm::IntrusiveRefCntPtr<Edge>(Edg));
105}
106
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000107CompilationGraph::CompilationGraph() {
108 NodesMap["root"] = Node(this);
109}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000110
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000111Node* CompilationGraph::getNode(const std::string& ToolName) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000112 nodes_map_type::iterator I = NodesMap.find(ToolName);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000113 if (I == NodesMap.end()) {
114 PrintError("Node " + ToolName + " is not in the graph");
115 return 0;
116 }
117 return &I->second;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000118}
119
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000120const Node* CompilationGraph::getNode(const std::string& ToolName) const {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000121 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000122 if (I == NodesMap.end()) {
123 PrintError("Node " + ToolName + " is not in the graph!");
124 return 0;
125 }
126 return &I->second;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000127}
128
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000129// Find the tools list corresponding to the given language name.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000130const CompilationGraph::tools_vector_type*
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000131CompilationGraph::getToolsVector(const std::string& LangName) const
132{
133 tools_map_type::const_iterator I = ToolsMap.find(LangName);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000134 if (I == ToolsMap.end()) {
135 PrintError("No tool corresponding to the language " + LangName + " found");
136 return 0;
137 }
138 return &I->second;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000139}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000140
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000141void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovaa4774c2008-11-12 00:04:46 +0000142 if (NodesMap.count(V->Name()) == 0)
143 NodesMap[V->Name()] = Node(this, V);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000144}
145
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000146int CompilationGraph::insertEdge(const std::string& A, Edge* Edg) {
147 Node* B = getNode(Edg->ToolName());
148 if (B == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000149 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000150
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000151 if (A == "root") {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000152 const char** InLangs = B->ToolPtr->InputLanguages();
Mikhail Glushenkov5fe84752008-05-30 06:24:49 +0000153 for (;*InLangs; ++InLangs)
154 ToolsMap[*InLangs].push_back(IntrusiveRefCntPtr<Edge>(Edg));
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000155 NodesMap["root"].AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000156 }
157 else {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000158 Node* N = getNode(A);
159 if (N == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000160 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000161
162 N->AddEdge(Edg);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000163 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000164 // Increase the inward edge counter.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000165 B->IncrInEdges();
166
167 return 0;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000168}
169
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000170// Pass input file through the chain until we bump into a Join node or
171// a node that says that it is the last.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000172int CompilationGraph::PassThroughGraph (const sys::Path& InFile,
173 const Node* StartNode,
174 const InputLanguagesSet& InLangs,
175 const sys::Path& TempDir,
176 const LanguageMap& LangMap) const {
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000177 sys::Path In = InFile;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000178 const Node* CurNode = StartNode;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000179
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000180 while(true) {
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000181 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000182
183 if (CurTool->IsJoin()) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000184 JoinTool& JT = static_cast<JoinTool&>(*CurTool);
Mikhail Glushenkov35a85e82008-05-06 18:10:20 +0000185 JT.AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000186 break;
187 }
188
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000189 Action CurAction;
190 if (int ret = CurTool->GenerateAction(CurAction, In, CurNode->HasChildren(),
191 TempDir, InLangs, LangMap)) {
192 return ret;
193 }
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000194
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000195 if (int ret = CurAction.Execute())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000196 return ret;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000197
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000198 if (CurAction.StopCompilation())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000199 return 0;
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000200
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000201 const Edge* Edg = ChooseEdge(CurNode->OutEdges, InLangs, CurNode->Name());
202 if (Edg == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000203 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000204
205 CurNode = getNode(Edg->ToolName());
206 if (CurNode == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000207 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000208
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000209 In = CurAction.OutFile();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000210 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000211
212 return 0;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000213}
214
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000215// Find the head of the toolchain corresponding to the given file.
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000216// Also, insert an input language into InLangs.
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000217const Node* CompilationGraph::
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000218FindToolChain(const sys::Path& In, const std::string* ForceLanguage,
219 InputLanguagesSet& InLangs, const LanguageMap& LangMap) const {
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000220
221 // Determine the input language.
Mikhail Glushenkove6334d82010-09-15 15:20:41 +0000222 const std::string* InLang = (ForceLanguage ? ForceLanguage
223 : LangMap.GetLanguage(In));
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000224 if (InLang == 0)
225 return 0;
Mikhail Glushenkove6334d82010-09-15 15:20:41 +0000226 const std::string& InLanguage = *InLang;
Mikhail Glushenkov76b1b242008-05-06 18:15:12 +0000227
228 // Add the current input language to the input language set.
229 InLangs.insert(InLanguage);
230
231 // Find the toolchain for the input language.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000232 const tools_vector_type* pTV = getToolsVector(InLanguage);
233 if (pTV == 0)
234 return 0;
235
236 const tools_vector_type& TV = *pTV;
237 if (TV.empty()) {
238 PrintError("No toolchain corresponding to language "
239 + InLanguage + " found");
240 return 0;
241 }
242
243 const Edge* Edg = ChooseEdge(TV, InLangs);
244 if (Edg == 0)
245 return 0;
246
247 return getNode(Edg->ToolName());
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000248}
249
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000250// Helper function used by Build().
251// Traverses initial portions of the toolchains (up to the first Join node).
252// This function is also responsible for handling the -x option.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000253int CompilationGraph::BuildInitial (InputLanguagesSet& InLangs,
254 const sys::Path& TempDir,
255 const LanguageMap& LangMap) {
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000256 // This is related to -x option handling.
257 cl::list<std::string>::const_iterator xIter = Languages.begin(),
258 xBegin = xIter, xEnd = Languages.end();
259 bool xEmpty = true;
260 const std::string* xLanguage = 0;
261 unsigned xPos = 0, xPosNext = 0, filePos = 0;
262
263 if (xIter != xEnd) {
264 xEmpty = false;
265 xPos = Languages.getPosition(xIter - xBegin);
266 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
267 xPosNext = (xNext == xEnd) ? std::numeric_limits<unsigned>::max()
268 : Languages.getPosition(xNext - xBegin);
269 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
270 }
271
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000272 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000273 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000274 CB = B, E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000275 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000276
Mikhail Glushenkov87416b42008-05-06 18:10:53 +0000277 // Code for handling the -x option.
278 // Output: std::string* xLanguage (can be NULL).
279 if (!xEmpty) {
280 filePos = InputFilenames.getPosition(B - CB);
281
282 if (xPos < filePos) {
283 if (filePos < xPosNext) {
284 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
285 }
286 else { // filePos >= xPosNext
287 // Skip xIters while filePos > xPosNext
288 while (filePos > xPosNext) {
289 ++xIter;
290 xPos = xPosNext;
291
292 cl::list<std::string>::const_iterator xNext = llvm::next(xIter);
293 if (xNext == xEnd)
294 xPosNext = std::numeric_limits<unsigned>::max();
295 else
296 xPosNext = Languages.getPosition(xNext - xBegin);
297 xLanguage = (*xIter == "none") ? 0 : &(*xIter);
298 }
299 }
300 }
301 }
302
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000303 // Find the toolchain corresponding to this file.
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000304 const Node* N = FindToolChain(In, xLanguage, InLangs, LangMap);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000305 if (N == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000306 return 1;
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000307 // Pass file through the chain starting at head.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000308 if (int ret = PassThroughGraph(In, N, InLangs, TempDir, LangMap))
309 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000310 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000311
312 return 0;
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000313}
314
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000315// Sort the nodes in topological order.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000316int CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000317 std::queue<const Node*> Q;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000318
319 Node* Root = getNode("root");
320 if (Root == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000321 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000322
323 Q.push(Root);
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000324
325 while (!Q.empty()) {
326 const Node* A = Q.front();
327 Q.pop();
328 Out.push_back(A);
329 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
330 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000331 Node* B = getNode((*EB)->ToolName());
332 if (B == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000333 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000334
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000335 B->DecrInEdges();
336 if (B->HasNoInEdges())
337 Q.push(B);
338 }
339 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000340
341 return 0;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000342}
343
344namespace {
345 bool NotJoinNode(const Node* N) {
346 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
347 }
348}
349
350// Call TopologicalSort and filter the resulting list to include
351// only Join nodes.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000352int CompilationGraph::
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000353TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
354 std::vector<const Node*> TopSorted;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000355 if (int ret = TopologicalSort(TopSorted))
356 return ret;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000357 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
358 std::back_inserter(Out), NotJoinNode);
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000359
360 return 0;
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000361}
362
Mikhail Glushenkov11a353a2008-09-22 20:47:46 +0000363int CompilationGraph::Build (const sys::Path& TempDir,
364 const LanguageMap& LangMap) {
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000365 InputLanguagesSet InLangs;
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000366 bool WasSomeActionGenerated = !InputFilenames.empty();
Mikhail Glushenkov4c11a622008-05-06 18:15:35 +0000367
368 // Traverse initial parts of the toolchains and fill in InLangs.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000369 if (int ret = BuildInitial(InLangs, TempDir, LangMap))
370 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000371
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000372 std::vector<const Node*> JTV;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000373 if (int ret = TopologicalSortFilterJoinNodes(JTV))
374 return ret;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000375
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000376 // For all join nodes in topological order:
377 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
378 B != E; ++B) {
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000379
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000380 const Node* CurNode = *B;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000381 JoinTool* JT = &static_cast<JoinTool&>(*CurNode->ToolPtr.getPtr());
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000382
Mikhail Glushenkovbe867122008-05-06 18:16:52 +0000383 // Are there any files in the join list?
Mikhail Glushenkovbe6ee7c2010-02-23 09:04:28 +0000384 if (JT->JoinListEmpty() && !(JT->WorksOnEmpty() && InputFilenames.empty()))
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000385 continue;
386
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000387 WasSomeActionGenerated = true;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000388 Action CurAction;
389 if (int ret = JT->GenerateAction(CurAction, CurNode->HasChildren(),
390 TempDir, InLangs, LangMap)) {
391 return ret;
392 }
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000393
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000394 if (int ret = CurAction.Execute())
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000395 return ret;
Mikhail Glushenkov3d688222008-05-06 18:07:48 +0000396
Mikhail Glushenkovf9152532008-12-07 16:41:11 +0000397 if (CurAction.StopCompilation())
398 return 0;
399
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000400 const Edge* Edg = ChooseEdge(CurNode->OutEdges, InLangs, CurNode->Name());
401 if (Edg == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000402 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000403
404 const Node* NextNode = getNode(Edg->ToolName());
405 if (NextNode == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000406 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000407
408 if (int ret = PassThroughGraph(sys::Path(CurAction.OutFile()), NextNode,
409 InLangs, TempDir, LangMap)) {
410 return ret;
411 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000412 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000413
Mikhail Glushenkovb482f232010-07-27 11:19:40 +0000414 if (!WasSomeActionGenerated) {
415 PrintError("no input files");
416 return 1;
417 }
418
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000419 return 0;
420}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000421
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000422int CompilationGraph::CheckLanguageNames() const {
423 int ret = 0;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000424
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000425 // Check that names for output and input languages on all edges do match.
426 for (const_nodes_iterator B = this->NodesMap.begin(),
427 E = this->NodesMap.end(); B != E; ++B) {
428
429 const Node & N1 = B->second;
430 if (N1.ToolPtr) {
431 for (Node::const_iterator EB = N1.EdgesBegin(), EE = N1.EdgesEnd();
432 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000433 const Node* N2 = this->getNode((*EB)->ToolName());
434 if (N2 == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000435 return 1;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000436
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000437 if (!N2->ToolPtr) {
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000438 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000439 errs() << "Error: there is an edge from '" << N1.ToolPtr->Name()
440 << "' back to the root!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000441 continue;
442 }
443
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000444 const char** OutLangs = N1.ToolPtr->OutputLanguages();
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000445 const char** InLangs = N2->ToolPtr->InputLanguages();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000446 bool eq = false;
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000447 const char* OutLang = 0;
448 for (;*OutLangs; ++OutLangs) {
449 OutLang = *OutLangs;
450 for (;*InLangs; ++InLangs) {
451 if (std::strcmp(OutLang, *InLangs) == 0) {
452 eq = true;
453 break;
454 }
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000455 }
456 }
457
458 if (!eq) {
459 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000460 errs() << "Error: Output->input language mismatch in the edge '"
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000461 << N1.ToolPtr->Name() << "' -> '" << N2->ToolPtr->Name()
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000462 << "'!\n"
463 << "Expected one of { ";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000464
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000465 InLangs = N2->ToolPtr->InputLanguages();
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000466 for (;*InLangs; ++InLangs) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000467 errs() << '\'' << *InLangs << (*(InLangs+1) ? "', " : "'");
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000468 }
469
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000470 errs() << " }, but got '" << OutLang << "'!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000471 }
472
473 }
474 }
475 }
476
477 return ret;
478}
479
480int CompilationGraph::CheckMultipleDefaultEdges() const {
481 int ret = 0;
482 InputLanguagesSet Dummy;
483
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000484 // For all nodes, just iterate over the outgoing edges and check if there is
485 // more than one edge with maximum weight.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000486 for (const_nodes_iterator B = this->NodesMap.begin(),
487 E = this->NodesMap.end(); B != E; ++B) {
488 const Node& N = B->second;
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000489 int MaxWeight = -1024;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000490
491 // Ignore the root node.
492 if (!N.ToolPtr)
493 continue;
494
495 for (Node::const_iterator EB = N.EdgesBegin(), EE = N.EdgesEnd();
496 EB != EE; ++EB) {
Mikhail Glushenkov7555f0a2010-08-23 19:24:08 +0000497 int EdgeWeight = (*EB)->Weight(Dummy);
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000498 if (EdgeWeight > MaxWeight) {
499 MaxWeight = EdgeWeight;
500 }
501 else if (EdgeWeight == MaxWeight) {
502 ++ret;
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000503 errs() << "Error: there are multiple maximal edges stemming from the '"
504 << N.ToolPtr->Name() << "' node!\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000505 break;
506 }
507 }
508 }
509
510 return ret;
511}
512
513int CompilationGraph::CheckCycles() {
514 unsigned deleted = 0;
515 std::queue<Node*> Q;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000516
517 Node* Root = getNode("root");
518 if (Root == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000519 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000520
521 Q.push(Root);
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000522
Mikhail Glushenkov7ba60522009-01-30 02:12:57 +0000523 // Try to delete all nodes that have no ingoing edges, starting from the
524 // root. If there are any nodes left after this operation, then we have a
525 // cycle. This relies on '--check-graph' not performing the topological sort.
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000526 while (!Q.empty()) {
527 Node* A = Q.front();
528 Q.pop();
529 ++deleted;
530
531 for (Node::iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
532 EB != EE; ++EB) {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000533 Node* B = getNode((*EB)->ToolName());
534 if (B == 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000535 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000536
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000537 B->DecrInEdges();
538 if (B->HasNoInEdges())
539 Q.push(B);
540 }
541 }
542
543 if (deleted != NodesMap.size()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000544 errs() << "Error: there are cycles in the compilation graph!\n"
545 << "Try inspecting the diagram produced by "
546 << "'llvmc --view-graph'.\n\n";
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000547 return 1;
548 }
549
550 return 0;
551}
552
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000553int CompilationGraph::Check () {
554 // We try to catch as many errors as we can in one go.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000555 int errs = 0;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000556 int ret = 0;
557
558 // Check that output/input language names match.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000559 ret = this->CheckLanguageNames();
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 multiple default edges.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000565 ret = this->CheckMultipleDefaultEdges();
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
570 // Check for cycles.
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000571 ret = this->CheckCycles();
572 if (ret < 0)
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000573 return 1;
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000574 errs += ret;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000575
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000576 return errs;
Mikhail Glushenkovf8c430b2009-01-09 16:16:27 +0000577}
578
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000579// Code related to graph visualization.
580
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000581namespace {
582
583std::string SquashStrArray (const char** StrArr) {
584 std::string ret;
585
586 for (; *StrArr; ++StrArr) {
587 if (*(StrArr + 1)) {
588 ret += *StrArr;
589 ret += ", ";
590 }
591 else {
592 ret += *StrArr;
593 }
594 }
595
596 return ret;
597}
598
599} // End anonymous namespace.
600
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000601namespace llvm {
602 template <>
Mikhail Glushenkovbe9d9a12008-05-06 18:08:59 +0000603 struct DOTGraphTraits<llvmc::CompilationGraph*>
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000604 : public DefaultDOTGraphTraits
605 {
Tobias Grosserab010692009-11-30 13:34:51 +0000606 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000607
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000608 template<typename GraphType>
Tobias Grosserb5eedf22009-11-30 13:14:13 +0000609 static std::string getNodeLabel(const Node* N, const GraphType&)
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000610 {
611 if (N->ToolPtr)
612 if (N->ToolPtr->IsJoin())
613 return N->Name() + "\n (join" +
614 (N->HasChildren() ? ")"
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000615 : std::string(": ") +
616 SquashStrArray(N->ToolPtr->OutputLanguages()) + ')');
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000617 else
618 return N->Name();
619 else
620 return "root";
621 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000622
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000623 template<typename EdgeIter>
624 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000625 if (N->ToolPtr) {
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000626 return SquashStrArray(N->ToolPtr->OutputLanguages());
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000627 }
628 else {
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000629 return SquashStrArray(I->ToolPtr->InputLanguages());
Mikhail Glushenkovffcf3a12008-05-30 06:18:16 +0000630 }
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000631 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000632 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000633
Mikhail Glushenkov46aa5242010-09-21 14:59:42 +0000634} // End namespace llvm
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000635
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000636int CompilationGraph::writeGraph(const std::string& OutputFilename) {
Chris Lattner103289e2009-08-23 07:19:13 +0000637 std::string ErrorInfo;
638 raw_fd_ostream O(OutputFilename.c_str(), ErrorInfo);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000639
Chris Lattner103289e2009-08-23 07:19:13 +0000640 if (ErrorInfo.empty()) {
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000641 errs() << "Writing '"<< OutputFilename << "' file...";
Mikhail Glushenkov3cd3c722009-03-26 21:23:48 +0000642 llvm::WriteGraph(O, this);
Bill Wendling9cdd4f52009-06-30 04:07:12 +0000643 errs() << "done.\n";
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000644 }
645 else {
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000646 PrintError("Error opening file '" + OutputFilename + "' for writing!");
Mikhail Glushenkov67d985f2010-07-27 11:19:36 +0000647 return 1;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000648 }
Mikhail Glushenkovb374d4f2010-07-23 03:42:55 +0000649
650 return 0;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000651}
652
653void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000654 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000655}