Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 1 | //===- DataStructure.cpp - Implement the core data structure analysis -----===// |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 3 | // This file implements the core data structure functionality. |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 7 | #include "llvm/Module.h" |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 8 | #include "llvm/DerivedTypes.h" |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 9 | #include "Support/STLExtras.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 10 | #include "Support/StatisticReporter.h" |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 11 | #include "Support/STLExtras.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 12 | #include <algorithm> |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 13 | #include <set> |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/DataStructure.h" |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 15 | |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 16 | using std::vector; |
| 17 | |
Chris Lattner | 1e43516 | 2002-07-26 21:12:44 +0000 | [diff] [blame] | 18 | static RegisterAnalysis<LocalDataStructures> |
| 19 | X("datastructure", "Local Data Structure Analysis"); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 20 | AnalysisID LocalDataStructures::ID(AnalysisID::create<LocalDataStructures>()); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 21 | |
| 22 | //===----------------------------------------------------------------------===// |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 23 | // DSNode Implementation |
| 24 | //===----------------------------------------------------------------------===// |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 25 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 26 | DSNode::DSNode(enum NodeTy NT, const Type *T) : Ty(T), NodeType(NT) { |
| 27 | // If this node has any fields, allocate them now, but leave them null. |
| 28 | switch (T->getPrimitiveID()) { |
| 29 | case Type::PointerTyID: Links.resize(1); break; |
| 30 | case Type::ArrayTyID: Links.resize(1); break; |
| 31 | case Type::StructTyID: |
| 32 | Links.resize(cast<StructType>(T)->getNumContainedTypes()); |
| 33 | break; |
| 34 | default: break; |
| 35 | } |
| 36 | } |
| 37 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 38 | // DSNode copy constructor... do not copy over the referrers list! |
| 39 | DSNode::DSNode(const DSNode &N) |
| 40 | : Ty(N.Ty), Links(N.Links), Globals(N.Globals), NodeType(N.NodeType) { |
| 41 | } |
| 42 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 43 | void DSNode::removeReferrer(DSNodeHandle *H) { |
| 44 | // Search backwards, because we depopulate the list from the back for |
| 45 | // efficiency (because it's a vector). |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 46 | vector<DSNodeHandle*>::reverse_iterator I = |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 47 | std::find(Referrers.rbegin(), Referrers.rend(), H); |
| 48 | assert(I != Referrers.rend() && "Referrer not pointing to node!"); |
| 49 | Referrers.erase(I.base()-1); |
| 50 | } |
| 51 | |
Chris Lattner | f9ae4c5 | 2002-07-11 20:32:22 +0000 | [diff] [blame] | 52 | // addGlobal - Add an entry for a global value to the Globals list. This also |
| 53 | // marks the node with the 'G' flag if it does not already have it. |
| 54 | // |
| 55 | void DSNode::addGlobal(GlobalValue *GV) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 56 | // Keep the list sorted. |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 57 | vector<GlobalValue*>::iterator I = |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 58 | std::lower_bound(Globals.begin(), Globals.end(), GV); |
| 59 | |
| 60 | if (I == Globals.end() || *I != GV) { |
| 61 | assert(GV->getType()->getElementType() == Ty); |
| 62 | Globals.insert(I, GV); |
| 63 | NodeType |= GlobalNode; |
| 64 | } |
Chris Lattner | f9ae4c5 | 2002-07-11 20:32:22 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 68 | // addEdgeTo - Add an edge from the current node to the specified node. This |
| 69 | // can cause merging of nodes in the graph. |
| 70 | // |
| 71 | void DSNode::addEdgeTo(unsigned LinkNo, DSNode *N) { |
| 72 | assert(LinkNo < Links.size() && "LinkNo out of range!"); |
| 73 | if (N == 0 || Links[LinkNo] == N) return; // Nothing to do |
| 74 | if (Links[LinkNo] == 0) { // No merging to perform |
| 75 | Links[LinkNo] = N; |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // Merge the two nodes... |
| 80 | Links[LinkNo]->mergeWith(N); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | // mergeWith - Merge this node into the specified node, moving all links to and |
| 85 | // from the argument node into the current node. The specified node may be a |
| 86 | // null pointer (in which case, nothing happens). |
| 87 | // |
| 88 | void DSNode::mergeWith(DSNode *N) { |
| 89 | if (N == 0 || N == this) return; // Noop |
| 90 | assert(N->Ty == Ty && N->Links.size() == Links.size() && |
| 91 | "Cannot merge nodes of two different types!"); |
| 92 | |
| 93 | // Remove all edges pointing at N, causing them to point to 'this' instead. |
| 94 | while (!N->Referrers.empty()) |
| 95 | *N->Referrers.back() = this; |
| 96 | |
| 97 | // Make all of the outgoing links of N now be outgoing links of this. This |
| 98 | // can cause recursive merging! |
| 99 | // |
| 100 | for (unsigned i = 0, e = Links.size(); i != e; ++i) { |
| 101 | addEdgeTo(i, N->Links[i]); |
| 102 | N->Links[i] = 0; // Reduce unneccesary edges in graph. N is dead |
| 103 | } |
| 104 | |
Chris Lattner | f9ae4c5 | 2002-07-11 20:32:22 +0000 | [diff] [blame] | 105 | // Merge the node types |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 106 | NodeType |= N->NodeType; |
| 107 | N->NodeType = 0; // N is now a dead node. |
Chris Lattner | f9ae4c5 | 2002-07-11 20:32:22 +0000 | [diff] [blame] | 108 | |
| 109 | // Merge the globals list... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 110 | if (!N->Globals.empty()) { |
| 111 | // Save the current globals off to the side... |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 112 | vector<GlobalValue*> OldGlobals(Globals); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 113 | |
| 114 | // Resize the globals vector to be big enough to hold both of them... |
| 115 | Globals.resize(Globals.size()+N->Globals.size()); |
| 116 | |
| 117 | // Merge the two sorted globals lists together... |
| 118 | std::merge(OldGlobals.begin(), OldGlobals.end(), |
| 119 | N->Globals.begin(), N->Globals.end(), Globals.begin()); |
| 120 | |
| 121 | // Erase duplicate entries from the globals list... |
| 122 | Globals.erase(std::unique(Globals.begin(), Globals.end()), Globals.end()); |
| 123 | |
| 124 | // Delete the globals from the old node... |
| 125 | N->Globals.clear(); |
| 126 | } |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | //===----------------------------------------------------------------------===// |
| 130 | // DSGraph Implementation |
| 131 | //===----------------------------------------------------------------------===// |
| 132 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 133 | DSGraph::DSGraph(const DSGraph &G) : Func(G.Func) { |
Vikram S. Adve | 6aa0d62 | 2002-07-18 16:12:08 +0000 | [diff] [blame] | 134 | std::map<const DSNode*, DSNode*> NodeMap; // ignored |
| 135 | RetNode = cloneInto(G, ValueMap, NodeMap, false); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 138 | DSGraph::~DSGraph() { |
| 139 | FunctionCalls.clear(); |
Vikram S. Adve | 6aa0d62 | 2002-07-18 16:12:08 +0000 | [diff] [blame] | 140 | OrigFunctionCalls.clear(); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 141 | ValueMap.clear(); |
| 142 | RetNode = 0; |
| 143 | |
| 144 | #ifndef NDEBUG |
| 145 | // Drop all intra-node references, so that assertions don't fail... |
| 146 | std::for_each(Nodes.begin(), Nodes.end(), |
| 147 | std::mem_fun(&DSNode::dropAllReferences)); |
| 148 | #endif |
| 149 | |
| 150 | // Delete all of the nodes themselves... |
| 151 | std::for_each(Nodes.begin(), Nodes.end(), deleter<DSNode>); |
| 152 | } |
| 153 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 154 | // dump - Allow inspection of graph in a debugger. |
| 155 | void DSGraph::dump() const { print(std::cerr); } |
| 156 | |
Vikram S. Adve | 6aa0d62 | 2002-07-18 16:12:08 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 158 | // cloneInto - Clone the specified DSGraph into the current graph, returning the |
| 159 | // Return node of the graph. The translated ValueMap for the old function is |
| 160 | // filled into the OldValMap member. If StripLocals is set to true, Scalar and |
| 161 | // Alloca markers are removed from the graph, as the graph is being cloned into |
| 162 | // a calling function's graph. |
| 163 | // |
| 164 | DSNode *DSGraph::cloneInto(const DSGraph &G, |
| 165 | std::map<Value*, DSNodeHandle> &OldValMap, |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 166 | std::map<const DSNode*, DSNode*> &OldNodeMap, |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 167 | bool StripLocals) { |
Vikram S. Adve | 6aa0d62 | 2002-07-18 16:12:08 +0000 | [diff] [blame] | 168 | |
| 169 | assert(OldNodeMap.size()==0 && "Return argument OldNodeMap should be empty"); |
| 170 | |
| 171 | OldNodeMap[0] = 0; // Null pointer maps to null |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 172 | |
| 173 | unsigned FN = Nodes.size(); // FirstNode... |
| 174 | |
| 175 | // Duplicate all of the nodes, populating the node map... |
| 176 | Nodes.reserve(FN+G.Nodes.size()); |
| 177 | for (unsigned i = 0, e = G.Nodes.size(); i != e; ++i) { |
| 178 | DSNode *Old = G.Nodes[i], *New = new DSNode(*Old); |
| 179 | Nodes.push_back(New); |
Vikram S. Adve | 6aa0d62 | 2002-07-18 16:12:08 +0000 | [diff] [blame] | 180 | OldNodeMap[Old] = New; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // Rewrite the links in the nodes to point into the current graph now. |
| 184 | for (unsigned i = FN, e = Nodes.size(); i != e; ++i) |
| 185 | for (unsigned j = 0, e = Nodes[i]->getNumLinks(); j != e; ++j) |
Vikram S. Adve | 6aa0d62 | 2002-07-18 16:12:08 +0000 | [diff] [blame] | 186 | Nodes[i]->setLink(j, OldNodeMap[Nodes[i]->getLink(j)]); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 187 | |
| 188 | // If we are inlining this graph into the called function graph, remove local |
| 189 | // markers. |
| 190 | if (StripLocals) |
| 191 | for (unsigned i = FN, e = Nodes.size(); i != e; ++i) |
| 192 | Nodes[i]->NodeType &= ~(DSNode::AllocaNode | DSNode::ScalarNode); |
| 193 | |
| 194 | // Copy the value map... |
| 195 | for (std::map<Value*, DSNodeHandle>::const_iterator I = G.ValueMap.begin(), |
| 196 | E = G.ValueMap.end(); I != E; ++I) |
Vikram S. Adve | 6aa0d62 | 2002-07-18 16:12:08 +0000 | [diff] [blame] | 197 | OldValMap[I->first] = OldNodeMap[I->second]; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 198 | |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 199 | // Copy the function calls list... |
| 200 | unsigned FC = FunctionCalls.size(); // FirstCall |
| 201 | FunctionCalls.reserve(FC+G.FunctionCalls.size()); |
| 202 | for (unsigned i = 0, e = G.FunctionCalls.size(); i != e; ++i) { |
| 203 | FunctionCalls.push_back(std::vector<DSNodeHandle>()); |
| 204 | FunctionCalls[FC+i].reserve(G.FunctionCalls[i].size()); |
| 205 | for (unsigned j = 0, e = G.FunctionCalls[i].size(); j != e; ++j) |
| 206 | FunctionCalls[FC+i].push_back(OldNodeMap[G.FunctionCalls[i][j]]); |
| 207 | } |
Vikram S. Adve | 6aa0d62 | 2002-07-18 16:12:08 +0000 | [diff] [blame] | 208 | |
| 209 | // Copy the list of unresolved callers |
| 210 | PendingCallers.insert(PendingCallers.end(), |
| 211 | G.PendingCallers.begin(), G.PendingCallers.end()); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 212 | |
| 213 | // Return the returned node pointer... |
Vikram S. Adve | 6aa0d62 | 2002-07-18 16:12:08 +0000 | [diff] [blame] | 214 | return OldNodeMap[G.RetNode]; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
| 218 | // markIncompleteNodes - Mark the specified node as having contents that are not |
| 219 | // known with the current analysis we have performed. Because a node makes all |
| 220 | // of the nodes it can reach imcomplete if the node itself is incomplete, we |
| 221 | // must recursively traverse the data structure graph, marking all reachable |
| 222 | // nodes as incomplete. |
| 223 | // |
| 224 | static void markIncompleteNode(DSNode *N) { |
| 225 | // Stop recursion if no node, or if node already marked... |
| 226 | if (N == 0 || (N->NodeType & DSNode::Incomplete)) return; |
| 227 | |
| 228 | // Actually mark the node |
| 229 | N->NodeType |= DSNode::Incomplete; |
| 230 | |
| 231 | // Recusively process children... |
| 232 | for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i) |
| 233 | markIncompleteNode(N->getLink(i)); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | // markIncompleteNodes - Traverse the graph, identifying nodes that may be |
| 238 | // modified by other functions that have not been resolved yet. This marks |
| 239 | // nodes that are reachable through three sources of "unknownness": |
| 240 | // |
| 241 | // Global Variables, Function Calls, and Incoming Arguments |
| 242 | // |
| 243 | // For any node that may have unknown components (because something outside the |
| 244 | // scope of current analysis may have modified it), the 'Incomplete' flag is |
| 245 | // added to the NodeType. |
| 246 | // |
| 247 | void DSGraph::markIncompleteNodes() { |
| 248 | // Mark any incoming arguments as incomplete... |
| 249 | for (Function::aiterator I = Func.abegin(), E = Func.aend(); I != E; ++I) |
| 250 | if (isa<PointerType>(I->getType())) |
| 251 | markIncompleteNode(ValueMap[I]->getLink(0)); |
| 252 | |
| 253 | // Mark stuff passed into functions calls as being incomplete... |
| 254 | for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) { |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 255 | vector<DSNodeHandle> &Args = FunctionCalls[i]; |
| 256 | // Then the return value is certainly incomplete! |
| 257 | markIncompleteNode(Args[0]); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 258 | |
| 259 | // The call does not make the function argument incomplete... |
| 260 | |
| 261 | // All arguments to the function call are incomplete though! |
| 262 | for (unsigned i = 2, e = Args.size(); i != e; ++i) |
| 263 | markIncompleteNode(Args[i]); |
| 264 | } |
| 265 | |
Chris Lattner | 055dc2c | 2002-07-18 15:54:42 +0000 | [diff] [blame] | 266 | // Mark all of the nodes pointed to by global or cast nodes as incomplete... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 267 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
Chris Lattner | 055dc2c | 2002-07-18 15:54:42 +0000 | [diff] [blame] | 268 | if (Nodes[i]->NodeType & (DSNode::GlobalNode | DSNode::CastNode)) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 269 | DSNode *N = Nodes[i]; |
| 270 | for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i) |
| 271 | markIncompleteNode(N->getLink(i)); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // isNodeDead - This method checks to see if a node is dead, and if it isn't, it |
| 276 | // checks to see if there are simple transformations that it can do to make it |
| 277 | // dead. |
| 278 | // |
| 279 | bool DSGraph::isNodeDead(DSNode *N) { |
| 280 | // Is it a trivially dead shadow node... |
| 281 | if (N->getReferrers().empty() && N->NodeType == 0) |
| 282 | return true; |
| 283 | |
| 284 | // Is it a function node or some other trivially unused global? |
| 285 | if ((N->NodeType & ~DSNode::GlobalNode) == 0 && |
| 286 | N->getNumLinks() == 0 && |
| 287 | N->getReferrers().size() == N->getGlobals().size()) { |
| 288 | |
| 289 | // Remove the globals from the valuemap, so that the referrer count will go |
| 290 | // down to zero. |
| 291 | while (!N->getGlobals().empty()) { |
| 292 | GlobalValue *GV = N->getGlobals().back(); |
| 293 | N->getGlobals().pop_back(); |
| 294 | ValueMap.erase(GV); |
| 295 | } |
| 296 | assert(N->getReferrers().empty() && "Referrers should all be gone now!"); |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 304 | // removeTriviallyDeadNodes - After the graph has been constructed, this method |
| 305 | // removes all unreachable nodes that are created because they got merged with |
| 306 | // other nodes in the graph. These nodes will all be trivially unreachable, so |
| 307 | // we don't have to perform any non-trivial analysis here. |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 308 | // |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 309 | void DSGraph::removeTriviallyDeadNodes() { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 310 | for (unsigned i = 0; i != Nodes.size(); ++i) |
| 311 | if (isNodeDead(Nodes[i])) { // This node is dead! |
| 312 | delete Nodes[i]; // Free memory... |
| 313 | Nodes.erase(Nodes.begin()+i--); // Remove from node list... |
| 314 | } |
| 315 | |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 316 | // Remove trivially identical function calls |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 317 | unsigned NumFns = FunctionCalls.size(); |
| 318 | std::sort(FunctionCalls.begin(), FunctionCalls.end()); |
| 319 | FunctionCalls.erase(std::unique(FunctionCalls.begin(), FunctionCalls.end()), |
| 320 | FunctionCalls.end()); |
| 321 | |
| 322 | DEBUG(if (NumFns != FunctionCalls.size()) |
| 323 | std::cerr << "Merged " << (NumFns-FunctionCalls.size()) |
| 324 | << " call nodes in " << Func.getName() << "\n";); |
| 325 | } |
| 326 | |
| 327 | |
Chris Lattner | e221976 | 2002-07-18 18:22:40 +0000 | [diff] [blame] | 328 | // markAlive - Simple graph traverser that recursively walks the graph marking |
| 329 | // stuff to be alive. |
| 330 | // |
| 331 | static void markAlive(DSNode *N, std::set<DSNode*> &Alive) { |
| 332 | if (N == 0 || Alive.count(N)) return; |
| 333 | |
| 334 | Alive.insert(N); |
| 335 | for (unsigned i = 0, e = N->getNumLinks(); i != e; ++i) |
| 336 | markAlive(N->getLink(i), Alive); |
| 337 | } |
| 338 | |
| 339 | |
| 340 | // removeDeadNodes - Use a more powerful reachability analysis to eliminate |
| 341 | // subgraphs that are unreachable. This often occurs because the data |
| 342 | // structure doesn't "escape" into it's caller, and thus should be eliminated |
| 343 | // from the caller's graph entirely. This is only appropriate to use when |
| 344 | // inlining graphs. |
| 345 | // |
| 346 | void DSGraph::removeDeadNodes() { |
| 347 | // Reduce the amount of work we have to do... |
| 348 | removeTriviallyDeadNodes(); |
| 349 | |
| 350 | // FIXME: Merge nontrivially identical call nodes... |
| 351 | |
| 352 | // Alive - a set that holds all nodes found to be reachable/alive. |
| 353 | std::set<DSNode*> Alive; |
| 354 | |
| 355 | // Mark all nodes reachable by call nodes as alive... |
| 356 | for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) |
| 357 | for (unsigned j = 0, e = FunctionCalls[i].size(); j != e; ++j) |
| 358 | markAlive(FunctionCalls[i][j], Alive); |
| 359 | |
| 360 | for (unsigned i = 0, e = OrigFunctionCalls.size(); i != e; ++i) |
| 361 | for (unsigned j = 0, e = OrigFunctionCalls[i].size(); j != e; ++j) |
| 362 | markAlive(OrigFunctionCalls[i][j], Alive); |
| 363 | |
| 364 | // Mark all nodes reachable by scalar, global, or incomplete nodes as |
| 365 | // reachable... |
| 366 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
| 367 | if (Nodes[i]->NodeType & (DSNode::ScalarNode | DSNode::GlobalNode)) |
| 368 | markAlive(Nodes[i], Alive); |
| 369 | |
| 370 | // Loop over all unreachable nodes, dropping their references... |
| 371 | std::vector<DSNode*> DeadNodes; |
| 372 | DeadNodes.reserve(Nodes.size()); // Only one allocation is allowed. |
| 373 | for (unsigned i = 0; i != Nodes.size(); ++i) |
| 374 | if (!Alive.count(Nodes[i])) { |
| 375 | DSNode *N = Nodes[i]; |
| 376 | Nodes.erase(Nodes.begin()+i--); // Erase node from alive list. |
| 377 | DeadNodes.push_back(N); // Add node to our list of dead nodes |
| 378 | N->dropAllReferences(); // Drop all outgoing edges |
| 379 | } |
| 380 | |
| 381 | // The return value is alive as well... |
| 382 | markAlive(RetNode, Alive); |
| 383 | |
| 384 | // Delete all dead nodes... |
| 385 | std::for_each(DeadNodes.begin(), DeadNodes.end(), deleter<DSNode>); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 390 | // maskNodeTypes - Apply a mask to all of the node types in the graph. This |
| 391 | // is useful for clearing out markers like Scalar or Incomplete. |
| 392 | // |
| 393 | void DSGraph::maskNodeTypes(unsigned char Mask) { |
| 394 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
| 395 | Nodes[i]->NodeType &= Mask; |
| 396 | } |
| 397 | |
| 398 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 399 | //===----------------------------------------------------------------------===// |
| 400 | // LocalDataStructures Implementation |
| 401 | //===----------------------------------------------------------------------===// |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 402 | |
| 403 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 404 | // our memory... here... |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 405 | // |
| 406 | void LocalDataStructures::releaseMemory() { |
| 407 | for (std::map<Function*, DSGraph*>::iterator I = DSInfo.begin(), |
| 408 | E = DSInfo.end(); I != E; ++I) |
| 409 | delete I->second; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 410 | |
| 411 | // Empty map so next time memory is released, data structures are not |
| 412 | // re-deleted. |
| 413 | DSInfo.clear(); |
| 414 | } |
| 415 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 416 | bool LocalDataStructures::run(Module &M) { |
| 417 | // Calculate all of the graphs... |
| 418 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 419 | if (!I->isExternal()) |
| 420 | DSInfo.insert(std::make_pair(&*I, new DSGraph(*I))); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 421 | |
| 422 | return false; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 423 | } |