1 //===- DataStructureAA.cpp - Data Structure Based Alias Analysis ----------===//
3 // This pass uses the top-down data structure graphs to implement a simple
4 // context sensitive alias analysis.
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Analysis/DataStructure.h"
9 #include "llvm/Analysis/DSGraph.h"
10 #include "llvm/Analysis/AliasAnalysis.h"
11 #include "llvm/Module.h"
14 class DSAA : public Pass, public AliasAnalysis {
19 //------------------------------------------------
20 // Implement the Pass API
23 // run - Build up the result graph, representing the pointer graph for the
27 TD = &getAnalysis<TDDataStructures>();
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...
37 //------------------------------------------------
38 // Implement the AliasAnalysis API
41 // alias - This is the only method here that does anything interesting...
42 Result alias(const Value *V1, const Value *V2);
44 /// canCallModify - Not implemented yet: FIXME
46 Result canCallModify(const CallInst &CI, const Value *Ptr) {
50 /// canInvokeModify - Not implemented yet: FIXME
52 Result canInvokeModify(const InvokeInst &I, const Value *Ptr) {
57 // Register the pass...
58 RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
60 // Register as an implementation of AliasAnalysis
61 RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
64 // getValueFunction - return the function containing the specified value if
65 // available, or null otherwise.
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();
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?");
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...
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);
94 assert(I->second.getNode() && "Scalar map points to null node?");
95 hash_map<Value*, DSNodeHandle>::iterator J = GSM.find((Value*)V2);
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)
101 & DSNode::Incomplete)
103 // both are incomplete, they may alias...
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
107 if (I->second.getOffset() != J->second.getOffset())
114 // FIXME: we could improve on this by checking the globals graph for aliased
116 return getAnalysis<AliasAnalysis>().alias(V1, V2);