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