blob: f6cbe0875435d550468fdf30589ed3fb40f3826f [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 Glushenkov0d08db02008-05-06 16:35:25 +000089 if (!NodesMap.count(V->Name())) {
90 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 Glushenkov0d08db02008-05-06 16:35:25 +000098 if (A == "root") {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000099 const Node& N = getNode(E->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000100 const std::string& InputLanguage = N.ToolPtr->InputLanguage();
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000101 ToolsMap[InputLanguage].push_back(E->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000102
103 // Needed to support iteration via GraphTraits.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000104 NodesMap["root"].AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000105 }
106 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000107 Node& N = getNode(A);
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000108 N.AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000109 }
110}
111
Mikhail Glushenkov6591c892008-05-06 17:23:50 +0000112// TOFIX: support more interesting graph topologies. We will need to
113// do topological sorting to process multiple Join nodes correctly.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000114int CompilationGraph::Build (const sys::Path& tempDir) const {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000115 PathVector JoinList;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000116 const Tool* JoinTool = 0;
117 sys::Path In, Out;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000118
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000119 // For each input file
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000120 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
121 E = InputFilenames.end(); B != E; ++B) {
122 In = sys::Path(*B);
123
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000124 // Get to the head of the toolchain.
125 const tools_vector_type& TV = getToolsVector(getLanguage(In));
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000126 if (TV.empty())
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000127 throw std::runtime_error("Tool names vector is empty!");
128 const Node* N = &getNode(*TV.begin());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000129
Mikhail Glushenkov6591c892008-05-06 17:23:50 +0000130 // Pass file through the chain until we bump into a Join node or a
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000131 // node that says that it is the last.
132 bool Last = false;
133 while(!Last) {
134 const Tool* CurTool = N->ToolPtr.getPtr();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000135
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000136 if (CurTool->IsJoin()) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000137 JoinList.push_back(In);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000138 JoinTool = CurTool;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000139 break;
140 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000141
142 // Is this the last tool?
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000143 if (!N->HasChildren() || CurTool->IsLast()) {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000144 // Check if the first tool is also the last
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000145 if (Out.empty())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000146 Out.set(In.getBasename());
147 else
148 Out.appendComponent(In.getBasename());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000149 Out.appendSuffix(CurTool->OutputSuffix());
150 Last = true;
151 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000152 else {
153 Out = tempDir;
154 Out.appendComponent(In.getBasename());
155 Out.appendSuffix(CurTool->OutputSuffix());
156 Out.makeUnique(true, NULL);
157 Out.eraseFromDisk();
158 }
159
160 if (CurTool->GenerateAction(In, Out).Execute() != 0)
161 throw std::runtime_error("Tool returned error code!");
162
Mikhail Glushenkov6591c892008-05-06 17:23:50 +0000163 N = &getNode(N->ChooseEdge()->ToolName());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000164 In = Out; Out.clear();
165 }
166 }
167
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000168 if (JoinTool) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000169 // If the final output name is empty, set it to "a.out"
170 if (!OutputFilename.empty()) {
171 Out = sys::Path(OutputFilename);
172 }
173 else {
174 Out = sys::Path("a");
175 Out.appendSuffix(JoinTool->OutputSuffix());
176 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000177
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000178 if (JoinTool->GenerateAction(JoinList, Out).Execute() != 0)
179 throw std::runtime_error("Tool returned error code!");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000180 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000181
182 return 0;
183}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000184
185namespace llvm {
186 template <>
187 struct DOTGraphTraits<llvmcc::CompilationGraph*>
188 : public DefaultDOTGraphTraits
189 {
190
191 template<typename GraphType>
192 static std::string getNodeLabel(const Node* N, const GraphType&) {
193 if (N->ToolPtr)
Mikhail Glushenkov6591c892008-05-06 17:23:50 +0000194 return N->Name();
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000195 else
196 return "root";
197 }
198
199 };
200}
201
202void CompilationGraph::writeGraph() {
203 std::ofstream O("CompilationGraph.dot");
204
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000205 if (O.good()) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000206 llvm::WriteGraph(this, "CompilationGraph");
207 O.close();
208 }
209 else {
210 throw std::runtime_error("");
211 }
212}
213
214void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000215 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000216}