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