blob: 90e88bb8ddfb2155f6f0fee149e34b19cb522721 [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 Glushenkov6591c892008-05-06 17:23:50 +000028// Choose one of the edges based on command-line options.
29const Edge* Node::ChooseEdge() const {
30 const Edge* DefaultEdge = 0;
31 for (const_iterator B = EdgesBegin(), E = EdgesEnd();
32 B != E; ++B) {
33 const Edge* E = (*B).getPtr();
34 if (E->isDefault())
35 if (!DefaultEdge)
36 DefaultEdge = E;
37 else
38 throw std::runtime_error("Node " + Name() +
39 ": multiple default edges found!"
40 "Most probably a specification error.");
41 if (E->isEnabled())
42 return E;
43 }
44
45 if (DefaultEdge)
46 return DefaultEdge;
47 else
48 throw std::runtime_error("Node " + Name() +
49 ": no suitable edge found! "
50 "Most probably a specification error.");
51}
52
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000053CompilationGraph::CompilationGraph() {
54 NodesMap["root"] = Node(this);
55}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000056
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000057Node& CompilationGraph::getNode(const std::string& ToolName) {
58 nodes_map_type::iterator I = NodesMap.find(ToolName);
59 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000060 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000061 return I->second;
62}
63
64const Node& CompilationGraph::getNode(const std::string& ToolName) const {
65 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
66 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000067 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000068 return I->second;
69}
70
71const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
72 LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000073 if (Lang == ExtsToLangs.end())
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000074 throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!');
75 return Lang->second;
76}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000077
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000078const CompilationGraph::tools_vector_type&
79CompilationGraph::getToolsVector(const std::string& LangName) const
80{
81 tools_map_type::const_iterator I = ToolsMap.find(LangName);
82 if (I == ToolsMap.end())
83 throw std::runtime_error("No tools corresponding to " + LangName
84 + " found!");
85 return I->second;
86}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000087
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000088void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000089 if (NodesMap.count(V->Name()) == 0) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000090 Node N;
91 N.OwningGraph = this;
92 N.ToolPtr = V;
93 NodesMap[V->Name()] = N;
94 }
95}
96
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000097void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +000098 Node& B = getNode(E->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000099 if (A == "root") {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000100 const std::string& InputLanguage = B.ToolPtr->InputLanguage();
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000101 ToolsMap[InputLanguage].push_back(E->ToolName());
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000102 NodesMap["root"].AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000103 }
104 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000105 Node& N = getNode(A);
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000106 N.AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000107 }
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000108 // Increase the inward edge counter.
109 B.IncrInEdges();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000110}
111
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000112// Pass input file through the chain until we bump into a Join node or
113// a node that says that it is the last.
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000114const JoinTool*
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000115CompilationGraph::PassThroughGraph (sys::Path& In,
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000116 const sys::Path& TempDir) const {
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000117 bool Last = false;
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000118 JoinTool* ret = 0;
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000119
120 // Get to the head of the toolchain.
121 const tools_vector_type& TV = getToolsVector(getLanguage(In));
122 if (TV.empty())
123 throw std::runtime_error("Tool names vector is empty!");
124 const Node* N = &getNode(*TV.begin());
125
126 while(!Last) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000127 sys::Path Out;
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000128 Tool* CurTool = N->ToolPtr.getPtr();
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000129
130 if (CurTool->IsJoin()) {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000131 ret = &dynamic_cast<JoinTool&>(*CurTool);
132 ret->AddToJoinList(In);
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000133 break;
134 }
135
136 // Is this the last tool?
137 if (!N->HasChildren() || CurTool->IsLast()) {
138 // Check if the first tool is also the last
139 if (Out.empty())
140 Out.set(In.getBasename());
141 else
142 Out.appendComponent(In.getBasename());
143 Out.appendSuffix(CurTool->OutputSuffix());
144 Last = true;
145 }
146 else {
147 Out = TempDir;
148 Out.appendComponent(In.getBasename());
149 Out.appendSuffix(CurTool->OutputSuffix());
150 Out.makeUnique(true, NULL);
151 Out.eraseFromDisk();
152 }
153
154 if (CurTool->GenerateAction(In, Out).Execute() != 0)
155 throw std::runtime_error("Tool returned error code!");
156
157 N = &getNode(N->ChooseEdge()->ToolName());
158 In = Out; Out.clear();
159 }
160
161 return ret;
162}
163
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000164int CompilationGraph::Build (const sys::Path& TempDir) const {
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000165 const JoinTool* JT = 0;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000166
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000167 // For each input file
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000168 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
169 E = InputFilenames.end(); B != E; ++B) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000170 sys::Path In = sys::Path(*B);
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000171
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000172 const JoinTool* NewJoin = PassThroughGraph(In, TempDir);
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000173 if (JT && NewJoin && JT != NewJoin)
Mikhail Glushenkov2ba4c5a2008-05-06 17:25:51 +0000174 throw std::runtime_error("Graphs with multiple Join nodes"
175 "are not yet supported!");
176 else if (NewJoin)
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000177 JT = NewJoin;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000178 }
179
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000180 if (JT) {
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000181 sys::Path Out;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000182 // If the final output name is empty, set it to "a.out"
183 if (!OutputFilename.empty()) {
184 Out = sys::Path(OutputFilename);
185 }
186 else {
187 Out = sys::Path("a");
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000188 Out.appendSuffix(JT->OutputSuffix());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000189 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000190
Mikhail Glushenkovc74bfc92008-05-06 17:26:53 +0000191 if (JT->GenerateAction(Out).Execute() != 0)
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000192 throw std::runtime_error("Tool returned error code!");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000193 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000194
195 return 0;
196}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000197
Mikhail Glushenkovbbbc9d42008-05-06 17:27:37 +0000198// Code related to graph visualization.
199
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000200namespace llvm {
201 template <>
202 struct DOTGraphTraits<llvmcc::CompilationGraph*>
203 : public DefaultDOTGraphTraits
204 {
205
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000206 template<typename GraphType>
207 static std::string getNodeLabel(const Node* N, const GraphType&)
208 {
209 if (N->ToolPtr)
210 if (N->ToolPtr->IsJoin())
211 return N->Name() + "\n (join" +
212 (N->HasChildren() ? ")"
213 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
214 else
215 return N->Name();
216 else
217 return "root";
218 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000219
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000220 template<typename EdgeIter>
221 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
222 if (N->ToolPtr)
223 return N->ToolPtr->OutputLanguage();
224 else
225 return I->ToolPtr->InputLanguage();
226 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000227 };
Mikhail Glushenkov97fda6d2008-05-06 17:26:14 +0000228
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000229}
230
231void CompilationGraph::writeGraph() {
232 std::ofstream O("CompilationGraph.dot");
233
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000234 if (O.good()) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000235 llvm::WriteGraph(this, "CompilationGraph");
236 O.close();
237 }
238 else {
239 throw std::runtime_error("");
240 }
241}
242
243void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000244 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000245}