blob: 94e323b7f6d001bee916da736c9afa271f5d3e52 [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 Glushenkov0d08db02008-05-06 16:35:25 +000028CompilationGraph::CompilationGraph() {
29 NodesMap["root"] = Node(this);
30}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000032Node& CompilationGraph::getNode(const std::string& ToolName) {
33 nodes_map_type::iterator I = NodesMap.find(ToolName);
34 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000035 throw std::runtime_error("Node " + ToolName + " is not in the graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000036 return I->second;
37}
38
39const Node& CompilationGraph::getNode(const std::string& ToolName) const {
40 nodes_map_type::const_iterator I = NodesMap.find(ToolName);
41 if (I == NodesMap.end())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000042 throw std::runtime_error("Node " + ToolName + " is not in the graph!");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000043 return I->second;
44}
45
46const std::string& CompilationGraph::getLanguage(const sys::Path& File) const {
47 LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000048 if (Lang == ExtsToLangs.end())
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000049 throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!');
50 return Lang->second;
51}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000052
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000053const CompilationGraph::tools_vector_type&
54CompilationGraph::getToolsVector(const std::string& LangName) const
55{
56 tools_map_type::const_iterator I = ToolsMap.find(LangName);
57 if (I == ToolsMap.end())
58 throw std::runtime_error("No tools corresponding to " + LangName
59 + " found!");
60 return I->second;
61}
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000062
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000063void CompilationGraph::insertNode(Tool* V) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000064 if (!NodesMap.count(V->Name())) {
65 Node N;
66 N.OwningGraph = this;
67 N.ToolPtr = V;
68 NodesMap[V->Name()] = N;
69 }
70}
71
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000072void CompilationGraph::insertEdge(const std::string& A, Edge* E) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000073 if (A == "root") {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000074 const Node& N = getNode(E->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000075 const std::string& InputLanguage = N.ToolPtr->InputLanguage();
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000076 ToolsMap[InputLanguage].push_back(E->ToolName());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000077
78 // Needed to support iteration via GraphTraits.
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000079 NodesMap["root"].AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000080 }
81 else {
Mikhail Glushenkov0a174932008-05-06 16:36:06 +000082 Node& N = getNode(A);
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000083 N.AddEdge(E);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000084 }
85}
86
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +000087// TOFIX: support edge properties.
88// TOFIX: support more interesting graph topologies.
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000089int CompilationGraph::Build (const sys::Path& tempDir) const {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000090 PathVector JoinList;
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000091 const Tool* JoinTool = 0;
92 sys::Path In, Out;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000093
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000094 // For each input file
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000095 for (cl::list<std::string>::const_iterator B = InputFilenames.begin(),
96 E = InputFilenames.end(); B != E; ++B) {
97 In = sys::Path(*B);
98
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +000099 // Get to the head of the toolchain.
100 const tools_vector_type& TV = getToolsVector(getLanguage(In));
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000101 if (TV.empty())
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000102 throw std::runtime_error("Tool names vector is empty!");
103 const Node* N = &getNode(*TV.begin());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000104
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000105 // Pass it through the chain until we bump into a Join node or a
106 // node that says that it is the last.
107 bool Last = false;
108 while(!Last) {
109 const Tool* CurTool = N->ToolPtr.getPtr();
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000110
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000111 if (CurTool->IsJoin()) {
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000112 JoinList.push_back(In);
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000113 JoinTool = CurTool;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000114 break;
115 }
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000116
117 // Is this the last tool?
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000118 if (!N->HasChildren() || CurTool->IsLast()) {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000119 // Check if the first tool is also the last
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000120 if (Out.empty())
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000121 Out.set(In.getBasename());
122 else
123 Out.appendComponent(In.getBasename());
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000124 Out.appendSuffix(CurTool->OutputSuffix());
125 Last = true;
126 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000127 else {
128 Out = tempDir;
129 Out.appendComponent(In.getBasename());
130 Out.appendSuffix(CurTool->OutputSuffix());
131 Out.makeUnique(true, NULL);
132 Out.eraseFromDisk();
133 }
134
135 if (CurTool->GenerateAction(In, Out).Execute() != 0)
136 throw std::runtime_error("Tool returned error code!");
137
Mikhail Glushenkov0a174932008-05-06 16:36:06 +0000138 N = &getNode((*N->EdgesBegin())->ToolName());
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000139 In = Out; Out.clear();
140 }
141 }
142
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000143 if (JoinTool) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000144 // If the final output name is empty, set it to "a.out"
145 if (!OutputFilename.empty()) {
146 Out = sys::Path(OutputFilename);
147 }
148 else {
149 Out = sys::Path("a");
150 Out.appendSuffix(JoinTool->OutputSuffix());
151 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000152
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000153 if (JoinTool->GenerateAction(JoinList, Out).Execute() != 0)
154 throw std::runtime_error("Tool returned error code!");
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000155 }
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000156
157 return 0;
158}
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000159
160namespace llvm {
161 template <>
162 struct DOTGraphTraits<llvmcc::CompilationGraph*>
163 : public DefaultDOTGraphTraits
164 {
165
166 template<typename GraphType>
167 static std::string getNodeLabel(const Node* N, const GraphType&) {
168 if (N->ToolPtr)
169 return N->ToolPtr->Name();
170 else
171 return "root";
172 }
173
174 };
175}
176
177void CompilationGraph::writeGraph() {
178 std::ofstream O("CompilationGraph.dot");
179
Mikhail Glushenkova4db8c02008-05-06 16:37:33 +0000180 if (O.good()) {
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000181 llvm::WriteGraph(this, "CompilationGraph");
182 O.close();
183 }
184 else {
185 throw std::runtime_error("");
186 }
187}
188
189void CompilationGraph::viewGraph() {
Mikhail Glushenkovd752c3f2008-05-06 16:36:50 +0000190 llvm::ViewGraph(this, "compilation-graph");
Mikhail Glushenkov0d08db02008-05-06 16:35:25 +0000191}