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) { |
| 27 | TD = &getAnalysis<TDDataStructures>(); |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 32 | AU.setPreservesAll(); // Does not transform code... |
| 33 | AU.addRequired<TDDataStructures>(); // Uses TD Datastructures |
| 34 | AU.addRequired<AliasAnalysis>(); // Chains to another AA impl... |
| 35 | } |
| 36 | |
| 37 | //------------------------------------------------ |
| 38 | // Implement the AliasAnalysis API |
| 39 | // |
| 40 | |
| 41 | // alias - This is the only method here that does anything interesting... |
| 42 | Result alias(const Value *V1, const Value *V2); |
| 43 | |
| 44 | /// canCallModify - Not implemented yet: FIXME |
| 45 | /// |
| 46 | Result canCallModify(const CallInst &CI, const Value *Ptr) { |
| 47 | return MayAlias; |
| 48 | } |
| 49 | |
| 50 | /// canInvokeModify - Not implemented yet: FIXME |
| 51 | /// |
| 52 | Result canInvokeModify(const InvokeInst &I, const Value *Ptr) { |
| 53 | return MayAlias; |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | // Register the pass... |
| 58 | RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis"); |
| 59 | |
| 60 | // Register as an implementation of AliasAnalysis |
| 61 | RegisterAnalysisGroup<AliasAnalysis, DSAA> Y; |
| 62 | } |
| 63 | |
| 64 | // getValueFunction - return the function containing the specified value if |
| 65 | // available, or null otherwise. |
| 66 | // |
| 67 | static const Function *getValueFunction(const Value *V) { |
| 68 | if (const Instruction *I = dyn_cast<Instruction>(V)) |
| 69 | return I->getParent()->getParent(); |
| 70 | else if (const Argument *A = dyn_cast<Argument>(V)) |
| 71 | return A->getParent(); |
| 72 | else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) |
| 73 | return BB->getParent(); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | // alias - This is the only method here that does anything interesting... |
| 78 | AliasAnalysis::Result DSAA::alias(const Value *V1, const Value *V2) { |
| 79 | const Function *F1 = getValueFunction(V1); |
| 80 | const Function *F2 = getValueFunction(V2); |
| 81 | assert((!F1 || !F2 || F1 == F2) && "Alias query for 2 different functions?"); |
| 82 | |
| 83 | |
| 84 | // FIXME: This can return must alias if querying a DSNode for a global value |
| 85 | // where the node has only the G composition bit set, and only one entry in |
| 86 | // the globals list... |
| 87 | if (F2) F1 = F2; |
| 88 | if (F1) { |
| 89 | // Get the graph for a function... |
| 90 | DSGraph &G = TD->getDSGraph(*F1); |
| 91 | hash_map<Value*, DSNodeHandle> &GSM = G.getScalarMap(); |
| 92 | hash_map<Value*, DSNodeHandle>::iterator I = GSM.find((Value*)V1); |
| 93 | if (I != GSM.end()) { |
| 94 | assert(I->second.getNode() && "Scalar map points to null node?"); |
| 95 | hash_map<Value*, DSNodeHandle>::iterator J = GSM.find((Value*)V2); |
| 96 | if (J != GSM.end()) { |
| 97 | assert(J->second.getNode() && "Scalar map points to null node?"); |
| 98 | if (I->second.getNode() != J->second.getNode()) { |
| 99 | // Return noalias if one of the nodes is complete... |
| 100 | if ((~I->second.getNode()->NodeType | ~J->second.getNode()->NodeType) |
Chris Lattner | 1dbd1b8 | 2003-02-07 20:39:48 +0000 | [diff] [blame] | 101 | & DSNode::Incomplete) |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 102 | return NoAlias; |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 103 | // both are incomplete, they may alias... |
| 104 | } else { |
| 105 | // Both point to the same node, see if they point to different |
| 106 | // offsets... FIXME: This needs to know the size of the alias query |
Chris Lattner | 1dbd1b8 | 2003-02-07 20:39:48 +0000 | [diff] [blame] | 107 | if (I->second.getOffset() != J->second.getOffset()) |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 108 | return NoAlias; |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // FIXME: we could improve on this by checking the globals graph for aliased |
| 115 | // global queries... |
Chris Lattner | 8105cfb | 2003-02-03 22:50:46 +0000 | [diff] [blame] | 116 | return getAnalysis<AliasAnalysis>().alias(V1, V2); |
| 117 | } |