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