blob: 5c3d946478a474738dd4897dad47b3266fd1f2a5 [file] [log] [blame]
Chris Lattner8105cfb2003-02-03 22:50:46 +00001//===- 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 Lattner8105cfb2003-02-03 22:50:46 +000012
13namespace {
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 Lattnera612afc2003-02-26 19:29:36 +000027 InitializeAliasAnalysis(this);
Chris Lattner8105cfb2003-02-03 22:50:46 +000028 TD = &getAnalysis<TDDataStructures>();
29 return false;
30 }
31
32 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnera612afc2003-02-26 19:29:36 +000033 AliasAnalysis::getAnalysisUsage(AU);
Chris Lattner8105cfb2003-02-03 22:50:46 +000034 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
43 // alias - This is the only method here that does anything interesting...
Chris Lattnera612afc2003-02-26 19:29:36 +000044 AliasResult alias(const Value *V1, unsigned V1Size,
45 const Value *V2, unsigned V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +000046 };
47
48 // Register the pass...
49 RegisterOpt<DSAA> X("ds-aa", "Data Structure Graph Based Alias Analysis");
50
51 // Register as an implementation of AliasAnalysis
52 RegisterAnalysisGroup<AliasAnalysis, DSAA> Y;
53}
54
55// getValueFunction - return the function containing the specified value if
56// available, or null otherwise.
57//
58static const Function *getValueFunction(const Value *V) {
59 if (const Instruction *I = dyn_cast<Instruction>(V))
60 return I->getParent()->getParent();
61 else if (const Argument *A = dyn_cast<Argument>(V))
62 return A->getParent();
63 else if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
64 return BB->getParent();
65 return 0;
66}
67
68// alias - This is the only method here that does anything interesting...
Chris Lattnera612afc2003-02-26 19:29:36 +000069AliasAnalysis::AliasResult DSAA::alias(const Value *V1, unsigned V1Size,
70 const Value *V2, unsigned V2Size) {
71 // FIXME: This should handle the Size argument as well!
Chris Lattner8105cfb2003-02-03 22:50:46 +000072 const Function *F1 = getValueFunction(V1);
73 const Function *F2 = getValueFunction(V2);
74 assert((!F1 || !F2 || F1 == F2) && "Alias query for 2 different functions?");
75
76
77 // FIXME: This can return must alias if querying a DSNode for a global value
78 // where the node has only the G composition bit set, and only one entry in
79 // the globals list...
80 if (F2) F1 = F2;
81 if (F1) {
82 // Get the graph for a function...
83 DSGraph &G = TD->getDSGraph(*F1);
84 hash_map<Value*, DSNodeHandle> &GSM = G.getScalarMap();
85 hash_map<Value*, DSNodeHandle>::iterator I = GSM.find((Value*)V1);
86 if (I != GSM.end()) {
87 assert(I->second.getNode() && "Scalar map points to null node?");
88 hash_map<Value*, DSNodeHandle>::iterator J = GSM.find((Value*)V2);
89 if (J != GSM.end()) {
90 assert(J->second.getNode() && "Scalar map points to null node?");
91 if (I->second.getNode() != J->second.getNode()) {
92 // Return noalias if one of the nodes is complete...
93 if ((~I->second.getNode()->NodeType | ~J->second.getNode()->NodeType)
Chris Lattner1dbd1b82003-02-07 20:39:48 +000094 & DSNode::Incomplete)
Chris Lattner8105cfb2003-02-03 22:50:46 +000095 return NoAlias;
Chris Lattner8105cfb2003-02-03 22:50:46 +000096 // both are incomplete, they may alias...
97 } else {
98 // Both point to the same node, see if they point to different
99 // offsets... FIXME: This needs to know the size of the alias query
Chris Lattner1dbd1b82003-02-07 20:39:48 +0000100 if (I->second.getOffset() != J->second.getOffset())
Chris Lattner8105cfb2003-02-03 22:50:46 +0000101 return NoAlias;
Chris Lattner8105cfb2003-02-03 22:50:46 +0000102 }
103 }
104 }
105 }
106
107 // FIXME: we could improve on this by checking the globals graph for aliased
108 // global queries...
Chris Lattnera612afc2003-02-26 19:29:36 +0000109 return getAnalysis<AliasAnalysis>().alias(V1, V1Size, V2, V2Size);
Chris Lattner8105cfb2003-02-03 22:50:46 +0000110}