Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 1 | //===--- CompilationGraph.cpp - The LLVM Compiler Driver --------*- C++ -*-===// |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 2 | // |
| 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 Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 10 | // Compilation graph - implementation. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mikhail Glushenkov | b90cd83 | 2008-05-06 16:34:12 +0000 | [diff] [blame] | 14 | #include "CompilationGraph.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 15 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CommandLine.h" |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 17 | #include "llvm/Support/DOTGraphTraits.h" |
| 18 | #include "llvm/Support/GraphWriter.h" |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 19 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 20 | #include <stdexcept> |
| 21 | |
| 22 | using namespace llvm; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 23 | using namespace llvmcc; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 24 | |
| 25 | extern cl::list<std::string> InputFilenames; |
| 26 | extern cl::opt<std::string> OutputFilename; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 27 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 28 | CompilationGraph::CompilationGraph() { |
| 29 | NodesMap["root"] = Node(this); |
| 30 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 31 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 32 | Node& CompilationGraph::getNode(const std::string& ToolName) { |
| 33 | nodes_map_type::iterator I = NodesMap.find(ToolName); |
| 34 | if (I == NodesMap.end()) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 35 | throw std::runtime_error("Node " + ToolName + " is not in the graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 36 | return I->second; |
| 37 | } |
| 38 | |
| 39 | const Node& CompilationGraph::getNode(const std::string& ToolName) const { |
| 40 | nodes_map_type::const_iterator I = NodesMap.find(ToolName); |
| 41 | if (I == NodesMap.end()) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 42 | throw std::runtime_error("Node " + ToolName + " is not in the graph!"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 43 | return I->second; |
| 44 | } |
| 45 | |
| 46 | const std::string& CompilationGraph::getLanguage(const sys::Path& File) const { |
| 47 | LanguageMap::const_iterator Lang = ExtsToLangs.find(File.getSuffix()); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 48 | if (Lang == ExtsToLangs.end()) |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 49 | throw std::runtime_error("Unknown suffix: " + File.getSuffix() + '!'); |
| 50 | return Lang->second; |
| 51 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 52 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 53 | const CompilationGraph::tools_vector_type& |
| 54 | CompilationGraph::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 Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 62 | |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 63 | void CompilationGraph::insertNode(Tool* V) { |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 64 | 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 Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 72 | void CompilationGraph::insertEdge(const std::string& A, Edge* E) { |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 73 | if (A == "root") { |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 74 | const Node& N = getNode(E->ToolName()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 75 | const std::string& InputLanguage = N.ToolPtr->InputLanguage(); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 76 | ToolsMap[InputLanguage].push_back(E->ToolName()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 77 | |
| 78 | // Needed to support iteration via GraphTraits. |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 79 | NodesMap["root"].AddEdge(E); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 80 | } |
| 81 | else { |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 82 | Node& N = getNode(A); |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 83 | N.AddEdge(E); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 87 | // TOFIX: support edge properties. |
| 88 | // TOFIX: support more interesting graph topologies. |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 89 | int CompilationGraph::Build (const sys::Path& tempDir) const { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 90 | PathVector JoinList; |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 91 | const Tool* JoinTool = 0; |
| 92 | sys::Path In, Out; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 93 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 94 | // For each input file |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 95 | for (cl::list<std::string>::const_iterator B = InputFilenames.begin(), |
| 96 | E = InputFilenames.end(); B != E; ++B) { |
| 97 | In = sys::Path(*B); |
| 98 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 99 | // Get to the head of the toolchain. |
| 100 | const tools_vector_type& TV = getToolsVector(getLanguage(In)); |
Mikhail Glushenkov | a4db8c0 | 2008-05-06 16:37:33 +0000 | [diff] [blame] | 101 | if (TV.empty()) |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 102 | throw std::runtime_error("Tool names vector is empty!"); |
| 103 | const Node* N = &getNode(*TV.begin()); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 104 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 105 | // 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 Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 110 | |
Mikhail Glushenkov | a4db8c0 | 2008-05-06 16:37:33 +0000 | [diff] [blame] | 111 | if (CurTool->IsJoin()) { |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 112 | JoinList.push_back(In); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 113 | JoinTool = CurTool; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 114 | break; |
| 115 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 116 | |
| 117 | // Is this the last tool? |
Mikhail Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 118 | if (!N->HasChildren() || CurTool->IsLast()) { |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 119 | // Check if the first tool is also the last |
Mikhail Glushenkov | a4db8c0 | 2008-05-06 16:37:33 +0000 | [diff] [blame] | 120 | if (Out.empty()) |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 121 | Out.set(In.getBasename()); |
| 122 | else |
| 123 | Out.appendComponent(In.getBasename()); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 124 | Out.appendSuffix(CurTool->OutputSuffix()); |
| 125 | Last = true; |
| 126 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 127 | 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 Glushenkov | 0a17493 | 2008-05-06 16:36:06 +0000 | [diff] [blame] | 138 | N = &getNode((*N->EdgesBegin())->ToolName()); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 139 | In = Out; Out.clear(); |
| 140 | } |
| 141 | } |
| 142 | |
Mikhail Glushenkov | a4db8c0 | 2008-05-06 16:37:33 +0000 | [diff] [blame] | 143 | if (JoinTool) { |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 144 | // 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 Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 152 | |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 153 | if (JoinTool->GenerateAction(JoinList, Out).Execute() != 0) |
| 154 | throw std::runtime_error("Tool returned error code!"); |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 155 | } |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 156 | |
| 157 | return 0; |
| 158 | } |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 159 | |
| 160 | namespace 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 | |
| 177 | void CompilationGraph::writeGraph() { |
| 178 | std::ofstream O("CompilationGraph.dot"); |
| 179 | |
Mikhail Glushenkov | a4db8c0 | 2008-05-06 16:37:33 +0000 | [diff] [blame] | 180 | if (O.good()) { |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 181 | llvm::WriteGraph(this, "CompilationGraph"); |
| 182 | O.close(); |
| 183 | } |
| 184 | else { |
| 185 | throw std::runtime_error(""); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | void CompilationGraph::viewGraph() { |
Mikhail Glushenkov | d752c3f | 2008-05-06 16:36:50 +0000 | [diff] [blame] | 190 | llvm::ViewGraph(this, "compilation-graph"); |
Mikhail Glushenkov | 0d08db0 | 2008-05-06 16:35:25 +0000 | [diff] [blame] | 191 | } |