1 //===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open
6 // Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Compilation graph - implementation.
12 //===----------------------------------------------------------------------===//
14 #include "CompilationGraph.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/DOTGraphTraits.h"
18 #include "llvm/Support/GraphWriter.h"
27 using namespace llvmcc;
29 extern cl::list<std::string> InputFilenames;
30 extern cl::opt<std::string> OutputFilename;
34 // Choose edge that returns
36 const Edge* ChooseEdge(const C& EdgesContainer,
37 const std::string& NodeName = "root") {
38 const Edge* DefaultEdge = 0;
40 for (typename C::const_iterator B = EdgesContainer.begin(),
41 E = EdgesContainer.end(); B != E; ++B) {
42 const Edge* E = B->getPtr();
48 throw std::runtime_error("Node " + NodeName
49 + ": multiple default outward edges found!"
50 "Most probably a specification error.");
58 throw std::runtime_error("Node " + NodeName
59 + ": no default outward edge found!"
60 "Most probably a specification error.");
65 CompilationGraph::CompilationGraph() {
66 NodesMap["root"] = Node(this);
69 Node& CompilationGraph::getNode(const std::string& ToolName) {
70 nodes_map_type::iterator I = NodesMap.find(ToolName);
71 if (I == NodesMap.end())
72 throw std::runtime_error("Node " + ToolName + " is not in the graph");
76 const Node& CompilationGraph::getNode(const std::string& ToolName) const {
77 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
78 if (I == NodesMap.end())
79 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
83 const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
84 LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
85 if (Lang == ExtsToLangs.end())
86 throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!');
90 const CompilationGraph::tools_vector_type&
91 CompilationGraph::getToolsVector(const std::string& LangName) const
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
100 void CompilationGraph::insertNode(Tool* V) {
101 if (NodesMap.count(V->Name()) == 0) {
103 N.OwningGraph = this;
105 NodesMap[V->Name()] = N;
109 void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
110 Node& B = getNode(E->ToolName());
112 const std::string& InputLanguage = B.ToolPtr->InputLanguage();
113 ToolsMap[InputLanguage].push_back(IntrusiveRefCntPtr<Edge>(E));
114 NodesMap["root"].AddEdge(E);
117 Node& N = getNode(A);
120 // Increase the inward edge counter.
124 // Pass input file through the chain until we bump into a Join node or
125 // a node that says that it is the last.
127 CompilationGraph::PassThroughGraph (sys::Path& In,
128 const Node* StartNode,
129 const sys::Path& TempDir) const {
131 const Node* CurNode = StartNode;
136 Tool* CurTool = CurNode->ToolPtr.getPtr();
138 if (CurTool->IsJoin()) {
139 ret = &dynamic_cast<JoinTool&>(*CurTool);
140 ret->AddToJoinList(In);
144 // Is this the last tool?
145 if (!CurNode->HasChildren() || CurTool->IsLast()) {
146 // Check if the first tool is also the last
148 Out.set(In.getBasename());
150 Out.appendComponent(In.getBasename());
151 Out.appendSuffix(CurTool->OutputSuffix());
156 Out.appendComponent(In.getBasename());
157 Out.appendSuffix(CurTool->OutputSuffix());
158 Out.makeUnique(true, NULL);
162 if (CurTool->GenerateAction(In, Out).Execute() != 0)
163 throw std::runtime_error("Tool returned error code!");
165 CurNode = &getNode(ChooseEdge(CurNode->OutEdges,
166 CurNode->Name())->ToolName());
167 In = Out; Out.clear();
173 // Sort the nodes in topological order.
174 void CompilationGraph::TopologicalSort(std::vector<const Node*>& Out) {
175 std::queue<const Node*> Q;
176 Q.push(&getNode("root"));
179 const Node* A = Q.front();
182 for (Node::const_iterator EB = A->EdgesBegin(), EE = A->EdgesEnd();
184 Node* B = &getNode((*EB)->ToolName());
186 if (B->HasNoInEdges())
193 bool NotJoinNode(const Node* N) {
194 return N->ToolPtr ? !N->ToolPtr->IsJoin() : true;
198 // Call TopologicalSort and filter the resulting list to include
200 void CompilationGraph::
201 TopologicalSortFilterJoinNodes(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);
208 // Find head of the toolchain corresponding to the given file.
209 const Node* CompilationGraph::FindToolChain(const sys::Path& In) const {
210 const std::string& InLanguage = getLanguage(In);
211 const tools_vector_type& TV = getToolsVector(InLanguage);
213 throw std::runtime_error("No toolchain corresponding to language"
214 + InLanguage + " found!");
215 return &getNode(ChooseEdge(TV)->ToolName());
218 int CompilationGraph::Build (const sys::Path& TempDir) {
220 // For each input file:
221 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
222 E = InputFilenames.end(); B != E; ++B) {
223 sys::Path In = sys::Path(*B);
225 // 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);
231 std::vector<const Node*> JTV;
232 TopologicalSortFilterJoinNodes(JTV);
234 // For all join nodes in topological order:
235 for (std::vector<const Node*>::iterator B = JTV.begin(), E = JTV.end();
242 // Code related to graph visualization.
246 struct DOTGraphTraits<llvmcc::CompilationGraph*>
247 : public DefaultDOTGraphTraits
250 template<typename GraphType>
251 static std::string getNodeLabel(const Node* N, const GraphType&)
254 if (N->ToolPtr->IsJoin())
255 return N->Name() + "\n (join" +
256 (N->HasChildren() ? ")"
257 : std::string(": ") + N->ToolPtr->OutputLanguage() + ')');
264 template<typename EdgeIter>
265 static std::string getEdgeSourceLabel(const Node* N, EdgeIter I) {
267 return N->ToolPtr->OutputLanguage();
269 return I->ToolPtr->InputLanguage();
275 void CompilationGraph::writeGraph() {
276 std::ofstream O("CompilationGraph.dot");
279 llvm::WriteGraph(this, "CompilationGraph");
283 throw std::runtime_error("");
287 void CompilationGraph::viewGraph() {
288 llvm::ViewGraph(this, "compilation-graph");