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 | |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 15 | #include "llvm/Module.h" |
| 16 | #include "llvm/Analysis/AliasAnalysis.h" |
Jeff Cohen | 1d7b5de | 2005-01-09 20:42:52 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/Passes.h" |
Chris Lattner | 4dabb2c | 2004-07-07 06:32:21 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/DataStructure/DataStructure.h" |
| 19 | #include "llvm/Analysis/DataStructure/DSGraph.h" |
Chris Lattner | 9a92729 | 2003-11-12 23:11:14 +0000 | [diff] [blame] | 20 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 22 | namespace { |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 23 | class DSAA : public ModulePass, public AliasAnalysis { |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 24 | TDDataStructures *TD; |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 25 | BUDataStructures *BU; |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 26 | public: |
| 27 | DSAA() : TD(0) {} |
| 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 | // |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 36 | bool runOnModule(Module &M) { |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 37 | InitializeAliasAnalysis(this); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 38 | TD = &getAnalysis<TDDataStructures>(); |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 39 | BU = &getAnalysis<BUDataStructures>(); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 40 | return false; |
| 41 | } |
| 42 | |
| 43 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 44 | AliasAnalysis::getAnalysisUsage(AU); |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 45 | AU.setPreservesAll(); // Does not transform code |
| 46 | AU.addRequiredTransitive<TDDataStructures>(); // Uses TD Datastructures |
| 47 | AU.addRequiredTransitive<BUDataStructures>(); // Uses BU Datastructures |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | //------------------------------------------------ |
| 51 | // Implement the AliasAnalysis API |
| 52 | // |
| 53 | |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 54 | AliasResult alias(const Value *V1, unsigned V1Size, |
| 55 | const Value *V2, unsigned V2Size); |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 56 | |
| 57 | void getMustAliases(Value *P, std::vector<Value*> &RetVals); |
| 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 | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 101 | // isSinglePhysicalObject - For now, the only case that we know that there is |
| 102 | // only one memory object in the node is when there is a single global in the |
| 103 | // node, and the only composition bit set is Global. |
| 104 | // |
| 105 | static bool isSinglePhysicalObject(DSNode *N) { |
| 106 | assert(N->isComplete() && "Can only tell if this is a complete object!"); |
| 107 | return N->isGlobalNode() && N->getGlobals().size() == 1 && |
| 108 | !N->isHeapNode() && !N->isAllocaNode() && !N->isUnknownNode(); |
| 109 | } |
| 110 | |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 111 | // alias - This is the only method here that does anything interesting... |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 112 | AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size, |
| 113 | const Value *V2, unsigned V2Size) { |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 114 | if (V1 == V2) return MustAlias; |
| 115 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 116 | DSGraph *G1 = getGraphForValue(V1); |
| 117 | DSGraph *G2 = getGraphForValue(V2); |
| 118 | assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?"); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 120 | // Get the graph to use... |
| 121 | DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph())); |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 123 | const DSGraph::ScalarMapTy &GSM = G.getScalarMap(); |
| 124 | DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1); |
Chris Lattner | bac7da8 | 2004-04-26 14:44:08 +0000 | [diff] [blame] | 125 | if (I == GSM.end()) return NoAlias; |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 126 | |
Chris Lattner | bac7da8 | 2004-04-26 14:44:08 +0000 | [diff] [blame] | 127 | assert(I->second.getNode() && "Scalar map points to null node?"); |
| 128 | DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2); |
| 129 | if (J == GSM.end()) return NoAlias; |
| 130 | |
| 131 | assert(J->second.getNode() && "Scalar map points to null node?"); |
| 132 | |
| 133 | DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode(); |
| 134 | unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset(); |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 135 | |
Chris Lattner | bac7da8 | 2004-04-26 14:44:08 +0000 | [diff] [blame] | 136 | // We can only make a judgment of one of the nodes is complete... |
| 137 | if (N1->isComplete() || N2->isComplete()) { |
| 138 | if (N1 != N2) |
| 139 | return NoAlias; // Completely different nodes. |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 10c45d6 | 2003-07-02 23:56:51 +0000 | [diff] [blame] | 141 | #if 0 // This does not correctly handle arrays! |
Chris Lattner | bac7da8 | 2004-04-26 14:44:08 +0000 | [diff] [blame] | 142 | // Both point to the same node and same offset, and there is only one |
| 143 | // physical memory object represented in the node, return must alias. |
| 144 | // |
| 145 | // FIXME: This isn't correct because we do not handle array indexing |
| 146 | // correctly. |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 147 | |
Chris Lattner | bac7da8 | 2004-04-26 14:44:08 +0000 | [diff] [blame] | 148 | if (O1 == O2 && isSinglePhysicalObject(N1)) |
| 149 | return MustAlias; // Exactly the same object & offset |
Chris Lattner | 10c45d6 | 2003-07-02 23:56:51 +0000 | [diff] [blame] | 150 | #endif |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 151 | |
Chris Lattner | bac7da8 | 2004-04-26 14:44:08 +0000 | [diff] [blame] | 152 | // See if they point to different offsets... if so, we may be able to |
| 153 | // determine that they do not alias... |
| 154 | if (O1 != O2) { |
| 155 | if (O2 < O1) { // Ensure that O1 <= O2 |
| 156 | std::swap(V1, V2); |
| 157 | std::swap(O1, O2); |
| 158 | std::swap(V1Size, V2Size); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 159 | } |
Chris Lattner | bac7da8 | 2004-04-26 14:44:08 +0000 | [diff] [blame] | 160 | |
| 161 | // FIXME: This is not correct because we do not handle array |
| 162 | // indexing correctly with this check! |
| 163 | //if (O1+V1Size <= O2) return NoAlias; |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | // FIXME: we could improve on this by checking the globals graph for aliased |
| 168 | // global queries... |
Chris Lattner | 484e302 | 2004-05-23 21:14:27 +0000 | [diff] [blame] | 169 | return AliasAnalysis::alias(V1, V1Size, V2, V2Size); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 170 | } |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 171 | |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 172 | /// getModRefInfo - does a callsite modify or reference a value? |
| 173 | /// |
| 174 | AliasAnalysis::ModRefResult |
| 175 | DSAA::getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
| 176 | Function *F = CS.getCalledFunction(); |
Chris Lattner | 2e2cce6 | 2005-03-17 19:56:18 +0000 | [diff] [blame] | 177 | if (!F || F->isExternal()) |
| 178 | return AliasAnalysis::getModRefInfo(CS, P, Size); |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 179 | |
| 180 | // Clone the function TD graph, clearing off Mod/Ref flags |
| 181 | const Function *csParent = CS.getInstruction()->getParent()->getParent(); |
| 182 | DSGraph TDGraph(TD->getDSGraph(*csParent)); |
Chris Lattner | 2e2cce6 | 2005-03-17 19:56:18 +0000 | [diff] [blame] | 183 | TDGraph.maskNodeTypes(~(DSNode::Modified|DSNode::Read)); |
Misha Brukman | a975a9a | 2004-03-12 06:14:22 +0000 | [diff] [blame] | 184 | |
| 185 | // Insert the callee's BU graph into the TD graph |
| 186 | const DSGraph &BUGraph = BU->getDSGraph(*F); |
| 187 | TDGraph.mergeInGraph(TDGraph.getDSCallSiteForCallSite(CS), |
| 188 | *F, BUGraph, 0); |
| 189 | |
| 190 | // Report the flags that have been added |
| 191 | const DSNodeHandle &DSH = TDGraph.getNodeForValue(P); |
| 192 | if (const DSNode *N = DSH.getNode()) |
| 193 | if (N->isModified()) |
| 194 | return N->isRead() ? ModRef : Mod; |
| 195 | else |
| 196 | return N->isRead() ? Ref : NoModRef; |
| 197 | return NoModRef; |
| 198 | } |
| 199 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 200 | |
| 201 | /// getMustAliases - If there are any pointers known that must alias this |
| 202 | /// pointer, return them now. This allows alias-set based alias analyses to |
| 203 | /// perform a form a value numbering (which is exposed by load-vn). If an alias |
| 204 | /// analysis supports this, it should ADD any must aliased pointers to the |
| 205 | /// specified vector. |
| 206 | /// |
| 207 | void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) { |
Chris Lattner | 10c45d6 | 2003-07-02 23:56:51 +0000 | [diff] [blame] | 208 | #if 0 // This does not correctly handle arrays! |
Chris Lattner | 9cd0484 | 2003-07-02 04:39:13 +0000 | [diff] [blame] | 209 | // Currently the only must alias information we can provide is to say that |
| 210 | // something is equal to a global value. If we already have a global value, |
| 211 | // don't get worked up about it. |
| 212 | if (!isa<GlobalValue>(P)) { |
| 213 | DSGraph *G = getGraphForValue(P); |
| 214 | if (!G) G = &TD->getGlobalsGraph(); |
| 215 | |
| 216 | // The only must alias information we can currently determine occurs when |
| 217 | // the node for P is a global node with only one entry. |
Chris Lattner | 02da032 | 2004-01-27 21:51:19 +0000 | [diff] [blame] | 218 | DSGraph::ScalarMapTy::const_iterator I = G->getScalarMap().find(P); |
| 219 | if (I != G->getScalarMap().end()) { |
Chris Lattner | 9cd0484 | 2003-07-02 04:39:13 +0000 | [diff] [blame] | 220 | DSNode *N = I->second.getNode(); |
| 221 | if (N->isComplete() && isSinglePhysicalObject(N)) |
| 222 | RetVals.push_back(N->getGlobals()[0]); |
| 223 | } |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 224 | } |
Chris Lattner | 10c45d6 | 2003-07-02 23:56:51 +0000 | [diff] [blame] | 225 | #endif |
Chris Lattner | 484e302 | 2004-05-23 21:14:27 +0000 | [diff] [blame] | 226 | return AliasAnalysis::getMustAliases(P, RetVals); |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 227 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 228 | |