blob: 8f3918f9e4d89dc67012d954fc90e944b208a30a [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 Glushenkovb90cd832008-05-06 16:34:12 +000014#include "CompilationGraph.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000015
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000016#include "llvm/Support/CommandLine.h"
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000017#include "llvm/Support/DOTGraphTraits.h"
18#include "llvm/Support/GraphWriter.h"
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000019
Mikhail Glushenkov02606582008-05-06 18:07:14 +000020#include <algorithm>
21#include <iostream>
22#include <iterator>
23#include <queue>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000024#include <stdexcept>
25
26using namespace llvm;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000027using namespace llvmcc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000028
29extern cl::list<std::string> InputFilenames;
30extern cl::opt<std::string> OutputFilename;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000032namespace {
33
34 // Choose edge that returns
35 template <class C>
36 const Edge* ChooseEdge(const C& EdgesContainer,
37 const std::string& NodeName = "root") {
38 const Edge* DefaultEdge = 0;
39
40 for (typename C::const_iterator B = EdgesContainer.begin(),
41 E = EdgesContainer.end(); B != E; ++B) {
42 const Edge* E = B->getPtr();
43
44 if (E->isDefault())
45 if (!DefaultEdge)
46 DefaultEdge = E;
47 else
48 throw std::runtime_error("Node " + NodeName
49 + ": multiple default outward edges found!"
50 "Most probably a specification error.");
51 if (E->isEnabled())
52 return E;
53 }
54
55 if (DefaultEdge)
56 return DefaultEdge;
57 else
58 throw std::runtime_error("Node " + NodeName
59 + ": no default outward edge found!"
60 "Most probably a specification error.");
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000061 }
62
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000063}
64
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000065CompilationGraph::CompilationGraph() {
66 NodesMap["root"] = Node(this);
67}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000068
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000069Node& CompilationGraph::getNode(const std::string& ToolName) {
70 nodes_map_type::iterator I = NodesMap.find(ToolName);
71 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000072 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000073 return I->second;
74}
75
76const Node& CompilationGraph::getNode(const std::string& ToolName) const {
77 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
78 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000079 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000080 return I->second;
81}
82
83const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
84 LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000085 if (Lang == ExtsToLangs.end())
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000086 throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!');
87 return Lang->second;
88}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000089
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000090const CompilationGraph::tools_vector_type&
91CompilationGraph::getToolsVector(const std::string& LangName) const
92{
93 tools_map_type::const_iterator I = ToolsMap.find(LangName);
94 if (I == ToolsMap.end())
95 throw std::runtime_error("No tools corresponding to " + LangName
96 + " found!");
97 return I->second;
98}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000099
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000100void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000101 if (NodesMap.count(V->Name()) == 0) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000102 Node N;
103 N.OwningGraph = this;
104 N.ToolPtr = V;
105 NodesMap[V->Name()] = N;
106 }
107}
108
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000109void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000110 Node& B = getNode(E->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000111 if (A == "root") {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000112 const std::string& InputLanguage = B.ToolPtr->InputLanguage();
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000113 ToolsMap[InputLanguage].push_back(IntrusiveRefCntPtr<Edge>(E));
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000114 NodesMap["root"].AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000115 }
116 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000117 Node& N = getNode(A);
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000118 N.AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000119 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000120 // Increase the inward edge counter.
121 B.IncrInEdges();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000122}
123
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000124// Pass input file through the chain until we bump into a Join node or
125// a node that says that it is the last.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000126const JoinTool*
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000127CompilationGraph::PassThroughGraph (sys::Path& In,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000128 const Node* StartNode,
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000129 const sys::Path& TempDir) const {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000130 bool Last = false;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000131 const Node* CurNode = StartNode;
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000132 JoinTool* ret = 0;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000133
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000134 while(!Last) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000135 sys::Path Out;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000136 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000137
138 if (CurTool->IsJoin()) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000139 ret = &dynamic_cast<JoinTool&>(*CurTool);
140 ret->AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000141 break;
142 }
143
144 // Is this the last tool?
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000145 if (!CurNode->HasChildren() || CurTool->IsLast()) {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000146 // Check if the first tool is also the last
147 if (Out.empty())
148 Out.set(In.getBasename());
149 else
150 Out.appendComponent(In.getBasename());
151 Out.appendSuffix(CurTool->OutputSuffix());
152 Last = true;
153 }
154 else {
155 Out = TempDir;
156 Out.appendComponent(In.getBasename());
157 Out.appendSuffix(CurTool->OutputSuffix());
158 Out.makeUnique(true, NULL);
159 Out.eraseFromDisk();
160 }
161
162 if (CurTool->GenerateAction(In, Out).Execute() != 0)
163 throw std::runtime_error("Tool returned error code!");
164
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000165 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
166 CurNode->Name())->ToolName());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000167 In = Out; Out.clear();
168 }
169
170 return ret;
171}
172
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000173// Sort the nodes in topological order.
174void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
175 std::queue<const Node*> Q;
176 Q.push(&getNode("root"));
177
178 while (!Q.empty()) {
179 const Node* A = Q.front();
180 Q.pop();
181 Out.push_back(A);
182 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
183 EB != EE; ++EB) {
184 Node* B = &getNode((*EB)->ToolName());
185 B->DecrInEdges();
186 if (B->HasNoInEdges())
187 Q.push(B);
188 }
189 }
190}
191
192namespace {
193 bool NotJoinNode(const Node* N) {
194 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
195 }
196}
197
198// Call TopologicalSort and filter the resulting list to include
199// only Join nodes.
200void CompilationGraph::
201TopologicalSortFilterJoinNodes(std::vector<const Node*>& Out) {
202 std::vector<const Node*> TopSorted;
203 TopologicalSort(TopSorted);
204 std::remove_copy_if(TopSorted.begin(), TopSorted.end(),
205 std::back_inserter(Out), NotJoinNode);
206}
207
208// Find head of the toolchain corresponding to the given file.
209const Node* CompilationGraph::FindToolChain(const sys::Path& In) const {
210 const std::string& InLanguage = getLanguage(In);
211 const tools_vector_type& TV = getToolsVector(InLanguage);
212 if (TV.empty())
213 throw std::runtime_error("No toolchain corresponding to language"
214 + InLanguage + " found!");
215 return &getNode(ChooseEdge(TV)->ToolName());
216}
217
218int CompilationGraph::Build (const sys::Path& TempDir) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000219
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000220 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000221 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
222 E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000223 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000224
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000225 // Find the toolchain corresponding to this file.
226 const Node* N = FindToolChain(In);
227 // Pass file through the chain starting at head.
228 PassThroughGraph(In, N, TempDir);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000229 }
230
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000231 std::vector<const Node*> JTV;
232 TopologicalSortFilterJoinNodes(JTV);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000233
Mikhail Glushenkov02606582008-05-06 18:07:14 +0000234 // For all join nodes in topological order:
235 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
236 B != E; ++B) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000237 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000238
239 return 0;
240}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000241
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000242// Code related to graph visualization.
243
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000244namespace llvm {
245 template <>
246 struct DOTGraphTraits<llvmcc::CompilationGraph*>
247 : public DefaultDOTGraphTraits
248 {
249
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000250 template<typename GraphType>
251 static std::string getNodeLabel(const Node* N, const GraphType&)
252 {
253 if (N->ToolPtr)
254 if (N->ToolPtr->IsJoin())
255 return N->Name() + "\n (join" +
256 (N->HasChildren() ? ")"
257 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
258 else
259 return N->Name();
260 else
261 return "root";
262 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000263
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000264 template<typename EdgeIter>
265 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
266 if (N->ToolPtr)
267 return N->ToolPtr->OutputLanguage();
268 else
269 return I->ToolPtr->InputLanguage();
270 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000271 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000272
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000273}
274
275void CompilationGraph::writeGraph() {
276 std::ofstream O("CompilationGraph.dot");
277
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000278 if (O.good()) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000279 llvm::WriteGraph(this, "CompilationGraph");
280 O.close();
281 }
282 else {
283 throw std::runtime_error("");
284 }
285}
286
287void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000288 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000289}