Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 1 | //===- Steensgaard.cpp - Context Insensitive Alias Analysis ---------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 9 | // |
| 10 | // This pass uses the data structure graphs to implement a simple context |
| 11 | // insensitive alias analysis. It does this by computing the local analysis |
| 12 | // graphs for all of the functions, then merging them together into a single big |
| 13 | // graph without cloning. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "llvm/Analysis/DataStructure.h" |
Chris Lattner | c5f21de | 2002-10-02 22:14:38 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/DSGraph.h" |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AliasAnalysis.h" |
| 20 | #include "llvm/Module.h" |
Chris Lattner | 6806f56 | 2003-08-01 22:15:03 +0000 | [diff] [blame] | 21 | #include "Support/Debug.h" |
Misha Brukman | f4445df | 2002-12-12 05:34:10 +0000 | [diff] [blame] | 22 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | namespace llvm { |
| 24 | |
Misha Brukman | f4445df | 2002-12-12 05:34:10 +0000 | [diff] [blame] | 25 | namespace { |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 26 | class Steens : public Pass, public AliasAnalysis { |
| 27 | DSGraph *ResultGraph; |
Chris Lattner | e6c0b5d | 2003-02-04 00:03:37 +0000 | [diff] [blame] | 28 | DSGraph *GlobalsGraph; // FIXME: Eliminate globals graph stuff from DNE |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 29 | public: |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 30 | Steens() : ResultGraph(0), GlobalsGraph(0) {} |
Chris Lattner | be1094e | 2003-02-13 21:44:18 +0000 | [diff] [blame] | 31 | ~Steens() { |
| 32 | releaseMyMemory(); |
| 33 | assert(ResultGraph == 0 && "releaseMemory not called?"); |
| 34 | } |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 35 | |
| 36 | //------------------------------------------------ |
| 37 | // Implement the Pass API |
| 38 | // |
| 39 | |
| 40 | // run - Build up the result graph, representing the pointer graph for the |
| 41 | // program. |
| 42 | // |
| 43 | bool run(Module &M); |
| 44 | |
Chris Lattner | be1094e | 2003-02-13 21:44:18 +0000 | [diff] [blame] | 45 | virtual void releaseMyMemory() { delete ResultGraph; ResultGraph = 0; } |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 46 | |
| 47 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 48 | AliasAnalysis::getAnalysisUsage(AU); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 49 | AU.setPreservesAll(); // Does not transform code... |
| 50 | AU.addRequired<LocalDataStructures>(); // Uses local dsgraph |
| 51 | AU.addRequired<AliasAnalysis>(); // Chains to another AA impl... |
| 52 | } |
| 53 | |
| 54 | // print - Implement the Pass::print method... |
| 55 | void print(std::ostream &O, const Module *M) const { |
| 56 | assert(ResultGraph && "Result graph has not yet been computed!"); |
| 57 | ResultGraph->writeGraphToFile(O, "steensgaards"); |
| 58 | } |
| 59 | |
| 60 | //------------------------------------------------ |
| 61 | // Implement the AliasAnalysis API |
| 62 | // |
| 63 | |
| 64 | // alias - This is the only method here that does anything interesting... |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 65 | AliasResult alias(const Value *V1, unsigned V1Size, |
| 66 | const Value *V2, unsigned V2Size); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 68 | private: |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 69 | void ResolveFunctionCall(Function *F, const DSCallSite &Call, |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 70 | DSNodeHandle &RetVal); |
| 71 | }; |
| 72 | |
| 73 | // Register the pass... |
| 74 | RegisterOpt<Steens> X("steens-aa", |
Chris Lattner | 6022ef4 | 2003-02-08 23:03:17 +0000 | [diff] [blame] | 75 | "Steensgaard's alias analysis (DSGraph based)"); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 76 | |
| 77 | // Register as an implementation of AliasAnalysis |
| 78 | RegisterAnalysisGroup<AliasAnalysis, Steens> Y; |
| 79 | } |
| 80 | |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 81 | /// ResolveFunctionCall - Resolve the actual arguments of a call to function F |
| 82 | /// with the specified call site descriptor. This function links the arguments |
| 83 | /// and the return value for the call site context-insensitively. |
| 84 | /// |
Chris Lattner | e6c0b5d | 2003-02-04 00:03:37 +0000 | [diff] [blame] | 85 | void Steens::ResolveFunctionCall(Function *F, const DSCallSite &Call, |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 86 | DSNodeHandle &RetVal) { |
| 87 | assert(ResultGraph != 0 && "Result graph not allocated!"); |
Chris Lattner | 8d32767 | 2003-06-30 03:36:09 +0000 | [diff] [blame] | 88 | DSGraph::ScalarMapTy &ValMap = ResultGraph->getScalarMap(); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 89 | |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 90 | // Handle the return value of the function... |
Chris Lattner | 0969c50 | 2002-10-21 02:08:03 +0000 | [diff] [blame] | 91 | if (Call.getRetVal().getNode() && RetVal.getNode()) |
| 92 | RetVal.mergeWith(Call.getRetVal()); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 93 | |
| 94 | // Loop over all pointer arguments, resolving them to their provided pointers |
Vikram S. Adve | 26b9826 | 2002-10-20 21:41:02 +0000 | [diff] [blame] | 95 | unsigned PtrArgIdx = 0; |
Chris Lattner | 88c7294 | 2003-02-10 18:16:19 +0000 | [diff] [blame] | 96 | for (Function::aiterator AI = F->abegin(), AE = F->aend(); |
| 97 | AI != AE && PtrArgIdx < Call.getNumPtrArgs(); ++AI) { |
Chris Lattner | 8d32767 | 2003-06-30 03:36:09 +0000 | [diff] [blame] | 98 | DSGraph::ScalarMapTy::iterator I = ValMap.find(AI); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 99 | if (I != ValMap.end()) // If its a pointer argument... |
Chris Lattner | e6c0b5d | 2003-02-04 00:03:37 +0000 | [diff] [blame] | 100 | I->second.mergeWith(Call.getPtrArg(PtrArgIdx++)); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 101 | } |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
| 105 | /// run - Build up the result graph, representing the pointer graph for the |
| 106 | /// program. |
| 107 | /// |
| 108 | bool Steens::run(Module &M) { |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 109 | InitializeAliasAnalysis(this); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 110 | assert(ResultGraph == 0 && "Result graph already allocated!"); |
| 111 | LocalDataStructures &LDS = getAnalysis<LocalDataStructures>(); |
| 112 | |
| 113 | // Create a new, empty, graph... |
Chris Lattner | 15869aa | 2003-11-02 22:27:28 +0000 | [diff] [blame] | 114 | ResultGraph = new DSGraph(getTargetData()); |
| 115 | GlobalsGraph = new DSGraph(getTargetData()); |
Chris Lattner | e6c0b5d | 2003-02-04 00:03:37 +0000 | [diff] [blame] | 116 | ResultGraph->setGlobalsGraph(GlobalsGraph); |
Chris Lattner | af28351 | 2003-02-09 19:26:47 +0000 | [diff] [blame] | 117 | ResultGraph->setPrintAuxCalls(); |
| 118 | |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 119 | // RetValMap - Keep track of the return values for all functions that return |
| 120 | // valid pointers. |
| 121 | // |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 122 | DSGraph::ReturnNodesTy RetValMap; |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 123 | |
| 124 | // Loop over the rest of the module, merging graphs for non-external functions |
| 125 | // into this graph. |
| 126 | // |
Chris Lattner | 0f777ab | 2003-02-10 00:14:57 +0000 | [diff] [blame] | 127 | unsigned Count = 0; |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 128 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 129 | if (!I->isExternal()) { |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 130 | DSGraph::ScalarMapTy ValMap; |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 131 | { // Scope to free NodeMap memory ASAP |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 132 | DSGraph::NodeMapTy NodeMap; |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 133 | const DSGraph &FDSG = LDS.getDSGraph(*I); |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 134 | ResultGraph->cloneInto(FDSG, ValMap, RetValMap, NodeMap); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chris Lattner | c875f02 | 2002-11-03 21:27:48 +0000 | [diff] [blame] | 137 | // Incorporate the inlined Function's ScalarMap into the global |
| 138 | // ScalarMap... |
Chris Lattner | 5a54063 | 2003-06-30 03:15:25 +0000 | [diff] [blame] | 139 | DSGraph::ScalarMapTy &GVM = ResultGraph->getScalarMap(); |
Chris Lattner | 8d32767 | 2003-06-30 03:36:09 +0000 | [diff] [blame] | 140 | for (DSGraph::ScalarMapTy::iterator I = ValMap.begin(), |
Chris Lattner | e6c0b5d | 2003-02-04 00:03:37 +0000 | [diff] [blame] | 141 | E = ValMap.end(); I != E; ++I) |
| 142 | GVM[I->first].mergeWith(I->second); |
Chris Lattner | 0f777ab | 2003-02-10 00:14:57 +0000 | [diff] [blame] | 143 | |
| 144 | if ((++Count & 1) == 0) // Prune nodes out every other time... |
| 145 | ResultGraph->removeTriviallyDeadNodes(); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // FIXME: Must recalculate and use the Incomplete markers!! |
| 149 | |
| 150 | // Now that we have all of the graphs inlined, we can go about eliminating |
| 151 | // call nodes... |
| 152 | // |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 153 | std::vector<DSCallSite> &Calls = |
Chris Lattner | 01fb0c7 | 2002-11-08 21:27:37 +0000 | [diff] [blame] | 154 | ResultGraph->getAuxFunctionCalls(); |
| 155 | assert(Calls.empty() && "Aux call list is already in use??"); |
| 156 | |
| 157 | // Start with a copy of the original call sites... |
| 158 | Calls = ResultGraph->getFunctionCalls(); |
| 159 | |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 160 | for (unsigned i = 0; i != Calls.size(); ) { |
Vikram S. Adve | 42fd169 | 2002-10-20 18:07:37 +0000 | [diff] [blame] | 161 | DSCallSite &CurCall = Calls[i]; |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 162 | |
| 163 | // Loop over the called functions, eliminating as many as possible... |
Chris Lattner | 923fc05 | 2003-02-05 21:59:58 +0000 | [diff] [blame] | 164 | std::vector<GlobalValue*> CallTargets; |
| 165 | if (CurCall.isDirectCall()) |
| 166 | CallTargets.push_back(CurCall.getCalleeFunc()); |
| 167 | else |
| 168 | CallTargets = CurCall.getCalleeNode()->getGlobals(); |
| 169 | |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 170 | for (unsigned c = 0; c != CallTargets.size(); ) { |
| 171 | // If we can eliminate this function call, do so! |
| 172 | bool Eliminated = false; |
| 173 | if (Function *F = dyn_cast<Function>(CallTargets[c])) |
| 174 | if (!F->isExternal()) { |
| 175 | ResolveFunctionCall(F, CurCall, RetValMap[F]); |
| 176 | Eliminated = true; |
| 177 | } |
Chris Lattner | 0f777ab | 2003-02-10 00:14:57 +0000 | [diff] [blame] | 178 | if (Eliminated) { |
| 179 | CallTargets[c] = CallTargets.back(); |
| 180 | CallTargets.pop_back(); |
| 181 | } else |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 182 | ++c; // Cannot eliminate this call, skip over it... |
| 183 | } |
| 184 | |
Chris Lattner | 0f777ab | 2003-02-10 00:14:57 +0000 | [diff] [blame] | 185 | if (CallTargets.empty()) { // Eliminated all calls? |
| 186 | CurCall = Calls.back(); // Remove entry |
| 187 | Calls.pop_back(); |
| 188 | } else |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 189 | ++i; // Skip this call site... |
| 190 | } |
| 191 | |
Chris Lattner | 72d29a4 | 2003-02-11 23:11:51 +0000 | [diff] [blame] | 192 | RetValMap.clear(); |
| 193 | |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 194 | // Update the "incomplete" markers on the nodes, ignoring unknownness due to |
| 195 | // incoming arguments... |
| 196 | ResultGraph->maskIncompleteMarkers(); |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 197 | ResultGraph->markIncompleteNodes(DSGraph::IgnoreFormalArgs); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 198 | |
| 199 | // Remove any nodes that are dead after all of the merging we have done... |
Chris Lattner | e6c0b5d | 2003-02-04 00:03:37 +0000 | [diff] [blame] | 200 | // FIXME: We should be able to disable the globals graph for steens! |
Chris Lattner | 394471f | 2003-01-23 22:05:33 +0000 | [diff] [blame] | 201 | ResultGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 202 | |
Chris Lattner | 47a307a | 2003-02-09 18:42:16 +0000 | [diff] [blame] | 203 | DEBUG(print(std::cerr, &M)); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | |
| 207 | // alias - This is the only method here that does anything interesting... |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 208 | AliasAnalysis::AliasResult Steens::alias(const Value *V1, unsigned V1Size, |
| 209 | const Value *V2, unsigned V2Size) { |
| 210 | // FIXME: HANDLE Size argument! |
Misha Brukman | f4445df | 2002-12-12 05:34:10 +0000 | [diff] [blame] | 211 | assert(ResultGraph && "Result graph has not been computed yet!"); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 8d32767 | 2003-06-30 03:36:09 +0000 | [diff] [blame] | 213 | DSGraph::ScalarMapTy &GSM = ResultGraph->getScalarMap(); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 214 | |
Chris Lattner | 8d32767 | 2003-06-30 03:36:09 +0000 | [diff] [blame] | 215 | DSGraph::ScalarMapTy::iterator I = GSM.find(const_cast<Value*>(V1)); |
Chris Lattner | 0a91e63 | 2003-02-03 22:51:53 +0000 | [diff] [blame] | 216 | if (I != GSM.end() && I->second.getNode()) { |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 217 | DSNodeHandle &V1H = I->second; |
Chris Lattner | 8d32767 | 2003-06-30 03:36:09 +0000 | [diff] [blame] | 218 | DSGraph::ScalarMapTy::iterator J=GSM.find(const_cast<Value*>(V2)); |
Chris Lattner | 0a91e63 | 2003-02-03 22:51:53 +0000 | [diff] [blame] | 219 | if (J != GSM.end() && J->second.getNode()) { |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 220 | DSNodeHandle &V2H = J->second; |
| 221 | // If the two pointers point to different data structure graph nodes, they |
| 222 | // cannot alias! |
Chris Lattner | 6022ef4 | 2003-02-08 23:03:17 +0000 | [diff] [blame] | 223 | if (V1H.getNode() != V2H.getNode()) // FIXME: Handle incompleteness! |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 224 | return NoAlias; |
Chris Lattner | 6022ef4 | 2003-02-08 23:03:17 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 226 | // FIXME: If the two pointers point to the same node, and the offsets are |
| 227 | // different, and the LinkIndex vector doesn't alias the section, then the |
| 228 | // two pointers do not alias. We need access size information for the two |
| 229 | // accesses though! |
| 230 | // |
| 231 | } |
| 232 | } |
| 233 | |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 234 | // If we cannot determine alias properties based on our graph, fall back on |
| 235 | // some other AA implementation. |
| 236 | // |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 237 | return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size); |
Chris Lattner | 1c7ce2c | 2002-10-01 22:34:12 +0000 | [diff] [blame] | 238 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 239 | |
| 240 | } // End llvm namespace |