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