Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 1 | //===- DataStructureAA.cpp - Data Structure Based 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 | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 9 | // |
| 10 | // This pass uses the top-down data structure graphs to implement a simple |
| 11 | // context sensitive alias analysis. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 1c8327b | 2005-03-17 20:33:27 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
| 16 | #include "llvm/DerivedTypes.h" |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/Analysis/AliasAnalysis.h" |
Jeff Cohen | 1d7b5de | 2005-01-09 20:42:52 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/Passes.h" |
Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/DataStructure/DataStructure.h" |
| 21 | #include "llvm/Analysis/DataStructure/DSGraph.h" |
Chris Lattner | 9a92729 | 2003-11-12 23:11:14 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 24 | namespace { |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 25 | class DSAA : public ModulePass, public AliasAnalysis { |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 26 | TDDataStructures *TD; |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 27 | BUDataStructures *BU; |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 28 | public: |
| 29 | DSAA() : TD(0) {} |
| 30 | |
| 31 | //------------------------------------------------ |
| 32 | // Implement the Pass API |
| 33 | // |
| 34 | |
| 35 | // run - Build up the result graph, representing the pointer graph for the |
| 36 | // program. |
| 37 | // |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 38 | bool runOnModule(Module &M) { |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 39 | InitializeAliasAnalysis(this); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 40 | TD = &getAnalysis<TDDataStructures>(); |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 41 | BU = &getAnalysis<BUDataStructures>(); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 42 | return false; |
| 43 | } |
| 44 | |
| 45 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 46 | AliasAnalysis::getAnalysisUsage(AU); |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 47 | AU.setPreservesAll(); // Does not transform code |
| 48 | AU.addRequiredTransitive<TDDataStructures>(); // Uses TD Datastructures |
| 49 | AU.addRequiredTransitive<BUDataStructures>(); // Uses BU Datastructures |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | //------------------------------------------------ |
| 53 | // Implement the AliasAnalysis API |
| 54 | // |
| 55 | |
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 | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 484e302 | 2004-05-23 21:14:27 +0000 | [diff] [blame] | 59 | ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size); |
Reid Spencer | 4a7ebfa | 2004-12-07 08:11:24 +0000 | [diff] [blame] | 60 | ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) { |
| 61 | return AliasAnalysis::getModRefInfo(CS1,CS2); |
| 62 | } |
Chris Lattner | 92e41d5 | 2004-01-30 22:20:55 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 851b534 | 2005-01-24 20:00:14 +0000 | [diff] [blame] | 64 | virtual void deleteValue(Value *V) { |
| 65 | BU->deleteValue(V); |
| 66 | TD->deleteValue(V); |
| 67 | } |
| 68 | |
| 69 | virtual void copyValue(Value *From, Value *To) { |
| 70 | if (From == To) return; |
| 71 | BU->copyValue(From, To); |
| 72 | TD->copyValue(From, To); |
| 73 | } |
| 74 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 75 | private: |
| 76 | DSGraph *getGraphForValue(const Value *V); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | // Register the pass... |
| 80 | RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis"); |
| 81 | |
| 82 | // Register as an implementation of AliasAnalysis |
| 83 | RegisterAnalysisGroup<AliasAnalysis, DSAA> Y; |
| 84 | } |
| 85 | |
Jeff Cohen | 1d7b5de | 2005-01-09 20:42:52 +0000 | [diff] [blame] | 86 | ModulePass *llvm::createDSAAPass() { return new DSAA(); } |
| 87 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 88 | // getGraphForValue - Return the DSGraph to use for queries about the specified |
| 89 | // value... |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 90 | // |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 91 | DSGraph *DSAA::getGraphForValue(const Value *V) { |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 92 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 93 | return &TD->getDSGraph(*I->getParent()->getParent()); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 94 | else if (const Argument *A = dyn_cast<Argument>(V)) |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 95 | return &TD->getDSGraph(*A->getParent()); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 96 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 97 | return &TD->getDSGraph(*BB->getParent()); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 101 | AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size, |
| 102 | const Value *V2, unsigned V2Size) { |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 103 | if (V1 == V2) return MustAlias; |
| 104 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 105 | DSGraph *G1 = getGraphForValue(V1); |
| 106 | DSGraph *G2 = getGraphForValue(V2); |
| 107 | assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?"); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 109 | // Get the graph to use... |
| 110 | DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph())); |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 112 | const DSGraph::ScalarMapTy &GSM = G.getScalarMap(); |
| 113 | DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1); |
Chris Lattner | bac7da8 | 2004-04-26 14:44:08 +0000 | [diff] [blame] | 114 | if (I == GSM.end()) return NoAlias; |
Chris Lattner | 511f60c | 2005-03-18 00:21:03 +0000 | [diff] [blame] | 115 | |
Chris Lattner | bac7da8 | 2004-04-26 14:44:08 +0000 | [diff] [blame] | 116 | DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2); |
| 117 | if (J == GSM.end()) return NoAlias; |
| 118 | |
Chris Lattner | bac7da8 | 2004-04-26 14:44:08 +0000 | [diff] [blame] | 119 | DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode(); |
| 120 | unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset(); |
Chris Lattner | 511f60c | 2005-03-18 00:21:03 +0000 | [diff] [blame] | 121 | if (N1 == 0 || N2 == 0) |
| 122 | return MayAlias; // Can't tell whether anything aliases null. |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 123 | |
Chris Lattner | bac7da8 | 2004-04-26 14:44:08 +0000 | [diff] [blame] | 124 | // We can only make a judgment of one of the nodes is complete... |
| 125 | if (N1->isComplete() || N2->isComplete()) { |
| 126 | if (N1 != N2) |
| 127 | return NoAlias; // Completely different nodes. |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 128 | |
Chris Lattner | bac7da8 | 2004-04-26 14:44:08 +0000 | [diff] [blame] | 129 | // See if they point to different offsets... if so, we may be able to |
| 130 | // determine that they do not alias... |
| 131 | if (O1 != O2) { |
| 132 | if (O2 < O1) { // Ensure that O1 <= O2 |
| 133 | std::swap(V1, V2); |
| 134 | std::swap(O1, O2); |
| 135 | std::swap(V1Size, V2Size); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 136 | } |
Chris Lattner | bac7da8 | 2004-04-26 14:44:08 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 2f72f94 | 2005-03-23 01:47:19 +0000 | [diff] [blame] | 138 | if (O1+V1Size <= O2) |
| 139 | return NoAlias; |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | // FIXME: we could improve on this by checking the globals graph for aliased |
| 144 | // global queries... |
Chris Lattner | 484e302 | 2004-05-23 21:14:27 +0000 | [diff] [blame] | 145 | return AliasAnalysis::alias(V1, V1Size, V2, V2Size); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 147 | |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 148 | /// getModRefInfo - does a callsite modify or reference a value? |
| 149 | /// |
| 150 | AliasAnalysis::ModRefResult |
| 151 | DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
Chris Lattner | 50cb9b4 | 2005-03-17 20:16:58 +0000 | [diff] [blame] | 152 | AliasAnalysis::ModRefResult Result =AliasAnalysis::getModRefInfo(CS, P, Size); |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 153 | Function *F = CS.getCalledFunction(); |
Chris Lattner | 50cb9b4 | 2005-03-17 20:16:58 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 7d671b8 | 2005-03-24 03:04:50 +0000 | [diff] [blame] | 155 | if (!F || Result == NoModRef) |
Chris Lattner | 50cb9b4 | 2005-03-17 20:16:58 +0000 | [diff] [blame] | 156 | return Result; |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 7d671b8 | 2005-03-24 03:04:50 +0000 | [diff] [blame] | 158 | if (F->isExternal()) { |
| 159 | // If we are calling an external function, and if this global doesn't escape |
| 160 | // the portion of the program we have analyzed, we can draw conclusions |
| 161 | // based on whether the global escapes the program. |
| 162 | Function *Caller = CS.getInstruction()->getParent()->getParent(); |
| 163 | DSGraph *G = &TD->getDSGraph(*Caller); |
| 164 | DSScalarMap::iterator NI = G->getScalarMap().find(P); |
| 165 | if (NI == G->getScalarMap().end()) { |
| 166 | // If it wasn't in the local function graph, check the global graph. This |
| 167 | // can occur for globals who are locally reference but hoisted out to the |
| 168 | // globals graph despite that. |
| 169 | G = G->getGlobalsGraph(); |
| 170 | NI = G->getScalarMap().find(P); |
| 171 | if (NI == G->getScalarMap().end()) |
| 172 | return Result; |
| 173 | } |
| 174 | |
| 175 | // If we found a node and it's complete, it cannot be passed out to the |
| 176 | // called function. |
| 177 | if (NI->second.getNode()->isComplete()) |
| 178 | return NoModRef; |
| 179 | return Result; |
| 180 | } |
| 181 | |
Chris Lattner | 511f60c | 2005-03-18 00:21:03 +0000 | [diff] [blame] | 182 | // Get the graphs for the callee and caller. Note that we want the BU graph |
| 183 | // for the callee because we don't want all caller's effects incorporated! |
| 184 | const Function *Caller = CS.getInstruction()->getParent()->getParent(); |
| 185 | DSGraph &CallerTDGraph = TD->getDSGraph(*Caller); |
| 186 | DSGraph &CalleeBUGraph = BU->getDSGraph(*F); |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 511f60c | 2005-03-18 00:21:03 +0000 | [diff] [blame] | 188 | // Figure out which node in the TD graph this pointer corresponds to. |
| 189 | DSScalarMap &CallerSM = CallerTDGraph.getScalarMap(); |
| 190 | DSScalarMap::iterator NI = CallerSM.find(P); |
| 191 | if (NI == CallerSM.end()) { |
Chris Lattner | db7436a | 2005-03-18 23:18:30 +0000 | [diff] [blame] | 192 | if (isa<ConstantPointerNull>(P) || isa<UndefValue>(P)) |
Chris Lattner | 511f60c | 2005-03-18 00:21:03 +0000 | [diff] [blame] | 193 | Result = NoModRef; // null is never modified :) |
| 194 | else { |
Chris Lattner | 1c8327b | 2005-03-17 20:33:27 +0000 | [diff] [blame] | 195 | assert(isa<GlobalVariable>(P) && |
| 196 | cast<GlobalVariable>(P)->getType()->getElementType()->isFirstClassType() && |
| 197 | "This isn't a global that DSA inconsiderately dropped " |
| 198 | "from the graph?"); |
Chris Lattner | bc499de | 2005-03-26 22:47:03 +0000 | [diff] [blame] | 199 | |
| 200 | DSGraph &GG = *CallerTDGraph.getGlobalsGraph(); |
| 201 | DSScalarMap::iterator NI = GG.getScalarMap().find(P); |
| 202 | if (NI != GG.getScalarMap().end() && !NI->second.isNull()) { |
| 203 | // Otherwise, if the node is only M or R, return this. This can be |
| 204 | // useful for globals that should be marked const but are not. |
| 205 | DSNode *N = NI->second.getNode(); |
| 206 | if (!N->isModified()) |
| 207 | Result = (ModRefResult)(Result & ~Mod); |
| 208 | if (!N->isRead()) |
| 209 | Result = (ModRefResult)(Result & ~Ref); |
| 210 | } |
Chris Lattner | 511f60c | 2005-03-18 00:21:03 +0000 | [diff] [blame] | 211 | } |
| 212 | return Result; |
Chris Lattner | 50cb9b4 | 2005-03-17 20:16:58 +0000 | [diff] [blame] | 213 | } |
Chris Lattner | 511f60c | 2005-03-18 00:21:03 +0000 | [diff] [blame] | 214 | |
| 215 | const DSNode *N = NI->second.getNode(); |
| 216 | assert(N && "Null pointer in scalar map??"); |
| 217 | |
| 218 | // Compute the mapping from nodes in the callee graph to the nodes in the |
| 219 | // caller graph for this call site. |
| 220 | DSGraph::NodeMapTy CalleeCallerMap; |
| 221 | DSCallSite DSCS = CallerTDGraph.getDSCallSiteForCallSite(CS); |
| 222 | CallerTDGraph.computeCalleeCallerMapping(DSCS, *F, CalleeBUGraph, |
| 223 | CalleeCallerMap); |
| 224 | |
| 225 | // Loop over all of the nodes in the callee that correspond to "N", keeping |
| 226 | // track of aggregate mod/ref info. |
| 227 | bool NeverReads = true, NeverWrites = true; |
| 228 | for (DSGraph::NodeMapTy::iterator I = CalleeCallerMap.begin(), |
| 229 | E = CalleeCallerMap.end(); I != E; ++I) |
| 230 | if (I->second.getNode() == N) { |
| 231 | if (I->first->isModified()) |
| 232 | NeverWrites = false; |
| 233 | if (I->first->isRead()) |
| 234 | NeverReads = false; |
| 235 | if (NeverReads == false && NeverWrites == false) |
| 236 | return Result; |
| 237 | } |
| 238 | |
| 239 | if (NeverWrites) // We proved it was not modified. |
| 240 | Result = ModRefResult(Result & ~Mod); |
| 241 | if (NeverReads) // We proved it was not read. |
| 242 | Result = ModRefResult(Result & ~Ref); |
| 243 | |
Chris Lattner | 50cb9b4 | 2005-03-17 20:16:58 +0000 | [diff] [blame] | 244 | return Result; |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 245 | } |