blob: 079007d255e4dac9cbb4f3d73302a7522e8eae4b [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
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000020#include <stdexcept>
21
22using namespace llvm;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000023using namespace llvmcc;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000024
25extern cl::list<std::string> InputFilenames;
26extern cl::opt<std::string> OutputFilename;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000027
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +000028namespace {
29
30 // Choose edge that returns
31 template <class C>
32 const Edge* ChooseEdge(const C& EdgesContainer,
33 const std::string& NodeName = "root") {
34 const Edge* DefaultEdge = 0;
35
36 for (typename C::const_iterator B = EdgesContainer.begin(),
37 E = EdgesContainer.end(); B != E; ++B) {
38 const Edge* E = B->getPtr();
39
40 if (E->isDefault())
41 if (!DefaultEdge)
42 DefaultEdge = E;
43 else
44 throw std::runtime_error("Node " + NodeName
45 + ": multiple default outward edges found!"
46 "Most probably a specification error.");
47 if (E->isEnabled())
48 return E;
49 }
50
51 if (DefaultEdge)
52 return DefaultEdge;
53 else
54 throw std::runtime_error("Node " + NodeName
55 + ": no default outward edge found!"
56 "Most probably a specification error.");
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000057 }
58
Mikhail Glushenkov6591c892008-05-06 17:23:50 +000059}
60
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000061CompilationGraph::CompilationGraph() {
62 NodesMap["root"] = Node(this);
63}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000064
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000065Node& CompilationGraph::getNode(const std::string& ToolName) {
66 nodes_map_type::iterator I = NodesMap.find(ToolName);
67 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000068 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000069 return I->second;
70}
71
72const Node& CompilationGraph::getNode(const std::string& ToolName) const {
73 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
74 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000075 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000076 return I->second;
77}
78
79const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
80 LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000081 if (Lang == ExtsToLangs.end())
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000082 throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!');
83 return Lang->second;
84}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000085
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000086const CompilationGraph::tools_vector_type&
87CompilationGraph::getToolsVector(const std::string& LangName) const
88{
89 tools_map_type::const_iterator I = ToolsMap.find(LangName);
90 if (I == ToolsMap.end())
91 throw std::runtime_error("No tools corresponding to " + LangName
92 + " found!");
93 return I->second;
94}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000095
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000096void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000097 if (NodesMap.count(V->Name()) == 0) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000098 Node N;
99 N.OwningGraph = this;
100 N.ToolPtr = V;
101 NodesMap[V->Name()] = N;
102 }
103}
104
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000105void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000106 Node& B = getNode(E->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000107 if (A == "root") {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000108 const std::string& InputLanguage = B.ToolPtr->InputLanguage();
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000109 ToolsMap[InputLanguage].push_back(IntrusiveRefCntPtr<Edge>(E));
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000110 NodesMap["root"].AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000111 }
112 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000113 Node& N = getNode(A);
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000114 N.AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000115 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000116 // Increase the inward edge counter.
117 B.IncrInEdges();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000118}
119
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000120// Pass input file through the chain until we bump into a Join node or
121// a node that says that it is the last.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000122const JoinTool*
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000123CompilationGraph::PassThroughGraph (sys::Path& In,
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000124 const Node* StartNode,
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000125 const sys::Path& TempDir) const {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000126 bool Last = false;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000127 const Node* CurNode = StartNode;
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000128 JoinTool* ret = 0;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000129
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000130 while(!Last) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000131 sys::Path Out;
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000132 Tool* CurTool = CurNode->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000133
134 if (CurTool->IsJoin()) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000135 ret = &dynamic_cast<JoinTool&>(*CurTool);
136 ret->AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000137 break;
138 }
139
140 // Is this the last tool?
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000141 if (!CurNode->HasChildren() || CurTool->IsLast()) {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000142 // Check if the first tool is also the last
143 if (Out.empty())
144 Out.set(In.getBasename());
145 else
146 Out.appendComponent(In.getBasename());
147 Out.appendSuffix(CurTool->OutputSuffix());
148 Last = true;
149 }
150 else {
151 Out = TempDir;
152 Out.appendComponent(In.getBasename());
153 Out.appendSuffix(CurTool->OutputSuffix());
154 Out.makeUnique(true, NULL);
155 Out.eraseFromDisk();
156 }
157
158 if (CurTool->GenerateAction(In, Out).Execute() != 0)
159 throw std::runtime_error("Tool returned error code!");
160
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000161 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
162 CurNode->Name())->ToolName());
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000163 In = Out; Out.clear();
164 }
165
166 return ret;
167}
168
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000169int CompilationGraph::Build (const sys::Path& TempDir) const {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000170 const JoinTool* JT = 0;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000171
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000172 // For each input file:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000173 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
174 E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000175 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000176
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000177 // Get to the head of the toolchain.
178 const std::string& InLanguage = getLanguage(In);
179 const tools_vector_type& TV = getToolsVector(InLanguage);
180 if (TV.empty())
181 throw std::runtime_error("No toolchain corresponding to language"
182 + InLanguage + " found!");
183 const Node* N = &getNode(ChooseEdge(TV)->ToolName());
184
185 // Pass it through the chain starting at head.
186 const JoinTool* NewJoin = PassThroughGraph(In, N, TempDir);
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000187 if (JT && NewJoin && JT != NewJoin)
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000188 throw std::runtime_error("Graphs with multiple Join nodes"
189 "are not yet supported!");
190 else if (NewJoin)
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000191 JT = NewJoin;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000192 }
193
Mikhail Glushenkov4f6e3a42008-05-06 17:28:03 +0000194 // For all join nodes in topological order:
195 // TOFIX: implement.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000196 if (JT) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000197 sys::Path Out;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000198 // If the final output name is empty, set it to "a.out"
199 if (!OutputFilename.empty()) {
200 Out = sys::Path(OutputFilename);
201 }
202 else {
203 Out = sys::Path("a");
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000204 Out.appendSuffix(JT->OutputSuffix());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000205 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000206
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000207 if (JT->GenerateAction(Out).Execute() != 0)
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000208 throw std::runtime_error("Tool returned error code!");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000209 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000210
211 return 0;
212}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000213
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000214// Code related to graph visualization.
215
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000216namespace llvm {
217 template <>
218 struct DOTGraphTraits<llvmcc::CompilationGraph*>
219 : public DefaultDOTGraphTraits
220 {
221
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000222 template<typename GraphType>
223 static std::string getNodeLabel(const Node* N, const GraphType&)
224 {
225 if (N->ToolPtr)
226 if (N->ToolPtr->IsJoin())
227 return N->Name() + "\n (join" +
228 (N->HasChildren() ? ")"
229 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
230 else
231 return N->Name();
232 else
233 return "root";
234 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000235
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000236 template<typename EdgeIter>
237 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
238 if (N->ToolPtr)
239 return N->ToolPtr->OutputLanguage();
240 else
241 return I->ToolPtr->InputLanguage();
242 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000243 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000244
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000245}
246
247void CompilationGraph::writeGraph() {
248 std::ofstream O("CompilationGraph.dot");
249
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000250 if (O.good()) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000251 llvm::WriteGraph(this, "CompilationGraph");
252 O.close();
253 }
254 else {
255 throw std::runtime_error("");
256 }
257}
258
259void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000260 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000261}