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