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