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 | |
| 15 | #include "llvm/Analysis/DataStructure.h" |
| 16 | #include "llvm/Analysis/DSGraph.h" |
| 17 | #include "llvm/Analysis/AliasAnalysis.h" |
| 18 | #include "llvm/Module.h" |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 19 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 20 | namespace llvm { |
| 21 | |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | class DSAA : public Pass, public AliasAnalysis { |
| 24 | TDDataStructures *TD; |
| 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 | // |
| 35 | bool run(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>(); |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 42 | AliasAnalysis::getAnalysisUsage(AU); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 43 | AU.setPreservesAll(); // Does not transform code... |
| 44 | AU.addRequired<TDDataStructures>(); // Uses TD Datastructures |
| 45 | AU.addRequired<AliasAnalysis>(); // Chains to another AA impl... |
| 46 | } |
| 47 | |
| 48 | //------------------------------------------------ |
| 49 | // Implement the AliasAnalysis API |
| 50 | // |
| 51 | |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 52 | AliasResult alias(const Value *V1, unsigned V1Size, |
| 53 | const Value *V2, unsigned V2Size); |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 54 | |
| 55 | void getMustAliases(Value *P, std::vector<Value*> &RetVals); |
| 56 | |
| 57 | private: |
| 58 | DSGraph *getGraphForValue(const Value *V); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | // Register the pass... |
| 62 | RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis"); |
| 63 | |
| 64 | // Register as an implementation of AliasAnalysis |
| 65 | RegisterAnalysisGroup<AliasAnalysis, DSAA> Y; |
| 66 | } |
| 67 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 68 | // getGraphForValue - Return the DSGraph to use for queries about the specified |
| 69 | // value... |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 70 | // |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 71 | DSGraph *DSAA::getGraphForValue(const Value *V) { |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 72 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 73 | return &TD->getDSGraph(*I->getParent()->getParent()); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 74 | else if (const Argument *A = dyn_cast<Argument>(V)) |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 75 | return &TD->getDSGraph(*A->getParent()); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 76 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 77 | return &TD->getDSGraph(*BB->getParent()); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 81 | // isSinglePhysicalObject - For now, the only case that we know that there is |
| 82 | // only one memory object in the node is when there is a single global in the |
| 83 | // node, and the only composition bit set is Global. |
| 84 | // |
| 85 | static bool isSinglePhysicalObject(DSNode *N) { |
| 86 | assert(N->isComplete() && "Can only tell if this is a complete object!"); |
| 87 | return N->isGlobalNode() && N->getGlobals().size() == 1 && |
| 88 | !N->isHeapNode() && !N->isAllocaNode() && !N->isUnknownNode(); |
| 89 | } |
| 90 | |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 91 | // alias - This is the only method here that does anything interesting... |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 92 | AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size, |
| 93 | const Value *V2, unsigned V2Size) { |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 94 | if (V1 == V2) return MustAlias; |
| 95 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 96 | DSGraph *G1 = getGraphForValue(V1); |
| 97 | DSGraph *G2 = getGraphForValue(V2); |
| 98 | assert((!G1 || !G2 || G1 == G2) && "Alias query for 2 different functions?"); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 100 | // Get the graph to use... |
| 101 | DSGraph &G = *(G1 ? G1 : (G2 ? G2 : &TD->getGlobalsGraph())); |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 103 | const DSGraph::ScalarMapTy &GSM = G.getScalarMap(); |
| 104 | DSGraph::ScalarMapTy::const_iterator I = GSM.find((Value*)V1); |
| 105 | if (I != GSM.end()) { |
| 106 | assert(I->second.getNode() && "Scalar map points to null node?"); |
| 107 | DSGraph::ScalarMapTy::const_iterator J = GSM.find((Value*)V2); |
| 108 | if (J != GSM.end()) { |
| 109 | assert(J->second.getNode() && "Scalar map points to null node?"); |
| 110 | |
| 111 | DSNode *N1 = I->second.getNode(), *N2 = J->second.getNode(); |
| 112 | unsigned O1 = I->second.getOffset(), O2 = J->second.getOffset(); |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 113 | |
Misha Brukman | 2f2d065 | 2003-09-11 18:14:24 +0000 | [diff] [blame] | 114 | // We can only make a judgment of one of the nodes is complete... |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 115 | if (N1->isComplete() || N2->isComplete()) { |
| 116 | if (N1 != N2) |
| 117 | return NoAlias; // Completely different nodes. |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 10c45d6 | 2003-07-02 23:56:51 +0000 | [diff] [blame] | 119 | #if 0 // This does not correctly handle arrays! |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 120 | // Both point to the same node and same offset, and there is only one |
| 121 | // physical memory object represented in the node, return must alias. |
| 122 | // |
| 123 | // FIXME: This isn't correct because we do not handle array indexing |
| 124 | // correctly. |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 126 | if (O1 == O2 && isSinglePhysicalObject(N1)) |
| 127 | return MustAlias; // Exactly the same object & offset |
Chris Lattner | 10c45d6 | 2003-07-02 23:56:51 +0000 | [diff] [blame] | 128 | #endif |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 130 | // See if they point to different offsets... if so, we may be able to |
| 131 | // determine that they do not alias... |
| 132 | if (O1 != O2) { |
| 133 | if (O2 < O1) { // Ensure that O1 <= O2 |
| 134 | std::swap(V1, V2); |
| 135 | std::swap(O1, O2); |
| 136 | std::swap(V1Size, V2Size); |
Chris Lattner | bd92b73 | 2003-06-19 21:15:11 +0000 | [diff] [blame] | 137 | } |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 138 | |
| 139 | // FIXME: This is not correct because we do not handle array |
| 140 | // indexing correctly with this check! |
| 141 | //if (O1+V1Size <= O2) return NoAlias; |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // FIXME: we could improve on this by checking the globals graph for aliased |
| 148 | // global queries... |
Chris Lattner | a612afc | 2003-02-26 19:29:36 +0000 | [diff] [blame] | 149 | return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size); |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 150 | } |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 151 | |
| 152 | |
| 153 | /// getMustAliases - If there are any pointers known that must alias this |
| 154 | /// pointer, return them now. This allows alias-set based alias analyses to |
| 155 | /// perform a form a value numbering (which is exposed by load-vn). If an alias |
| 156 | /// analysis supports this, it should ADD any must aliased pointers to the |
| 157 | /// specified vector. |
| 158 | /// |
| 159 | void DSAA::getMustAliases(Value *P, std::vector<Value*> &RetVals) { |
Chris Lattner | 10c45d6 | 2003-07-02 23:56:51 +0000 | [diff] [blame] | 160 | #if 0 // This does not correctly handle arrays! |
Chris Lattner | 9cd0484 | 2003-07-02 04:39:13 +0000 | [diff] [blame] | 161 | // Currently the only must alias information we can provide is to say that |
| 162 | // something is equal to a global value. If we already have a global value, |
| 163 | // don't get worked up about it. |
| 164 | if (!isa<GlobalValue>(P)) { |
| 165 | DSGraph *G = getGraphForValue(P); |
| 166 | if (!G) G = &TD->getGlobalsGraph(); |
| 167 | |
| 168 | // The only must alias information we can currently determine occurs when |
| 169 | // the node for P is a global node with only one entry. |
| 170 | const DSGraph::ScalarMapTy &GSM = G->getScalarMap(); |
| 171 | DSGraph::ScalarMapTy::const_iterator I = GSM.find(P); |
| 172 | if (I != GSM.end()) { |
| 173 | DSNode *N = I->second.getNode(); |
| 174 | if (N->isComplete() && isSinglePhysicalObject(N)) |
| 175 | RetVals.push_back(N->getGlobals()[0]); |
| 176 | } |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 177 | } |
Chris Lattner | 10c45d6 | 2003-07-02 23:56:51 +0000 | [diff] [blame] | 178 | #endif |
Chris Lattner | 5c70dc0 | 2003-06-29 00:54:08 +0000 | [diff] [blame] | 179 | return getAnalysis<AliasAnalysis>().getMustAliases(P, RetVals); |
| 180 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 181 | |
| 182 | } // End llvm namespace |