-
Notifications
You must be signed in to change notification settings - Fork 1
/
RangeBasedAliasAnalysis.cpp.old
184 lines (151 loc) · 4.85 KB
/
RangeBasedAliasAnalysis.cpp.old
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#define DEBUG_TYPE "range-based-aa"
#include "RangeBasedAliasAnalysis.h"
using namespace llvm;
extern SAGEInterface *SI;
// Register this pass...
char RangeBasedAliasAnalysis::ID = 0;
static RegisterPass<RangeBasedAliasAnalysis> X("range-based-aa",
"Range Analysis based Alias Analysis", false, false);
static RegisterAnalysisGroup<AliasAnalysis> E(X);
void RangeBasedAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const
{
AliasAnalysis::getAnalysisUsage(AU);
AU.addRequired<RangeBasedPointerAnalysis>();
AU.setPreservesAll();
}
bool RangeBasedAliasAnalysis::runOnModule(Module &M)
{
InitializeAliasAnalysis(this);
rbpa = &(getAnalysis<RangeBasedPointerAnalysis>());
return false;
}
Expr higher(Expr e1, Expr e2)
{
if(e1.isConstant() && e2.isConstant() )
return (e1 > e2);
else
return Expr::getFalse(*SI);
}
Expr lower(Expr e1, Expr e2)
{
if(e1.isConstant() && e2.isConstant() )
return (e1 < e2);
else
return Expr::getFalse(*SI);
}
bool RangeBasedAliasAnalysis::eval(RangedPointer* rp1, RangedPointer* rp2)
{
for(auto i = rp1->addr_begin(), ie = rp1->addr_end(); i != ie; i++)
{
Address* ai = *i;
for(auto j = rp2->addr_begin(), je = rp2->addr_end(); j != je; j++)
{
Address* aj = *j;
/*errs() << "Compare: ";
ai->print();
errs() << " X ";
aj->print();
errs() << "\n";*/
bool disjoint = false;
if( ai->getBase()->getPointerType() == RangedPointer::Alloc
and aj->getBase()->getPointerType() == RangedPointer::Alloc
and ai->getBase() != aj->getBase())
{
disjoint = true;
}
else if(ai->getBase() == aj->getBase())
{
Expr ex =
//higher(ai->getOffset()->getLower(), aj->getOffset()->getUpper()) ||
//lower(ai->getOffset()->getUpper(), aj->getOffset()->getLower());
ai->getOffset()->getLower() > aj->getOffset()->getUpper() ||
ai->getOffset()->getUpper() < aj->getOffset()->getLower();
if(ex.isEQ(Expr::getTrue(*SI)))
disjoint = true;
}
if(!disjoint)
return false;
}
}
return true;
}
RangeBasedAliasAnalysis::AliasResult
RangeBasedAliasAnalysis::alias(const Location &LocA, const Location &LocB)
{
const Value *p1, *p2;
p1 = LocA.Ptr;
p2 = LocB.Ptr;
RangedPointer* rp1 = rbpa->getRangedPointer(p1);
RangedPointer* rp2 = rbpa->getRangedPointer(p2);
//errs() << *(rp1->getPointer()) << "\n";
//errs() << *(rp2->getPointer()) << "\n";
/// Phase 1 evaluation
if(rp1->phase1_base == rp2->phase1_base)
{
Expr ex =
//higher(rp1->phase1_offset->getLower(), rp2->phase1_offset->getUpper()) ||
//lower(rp1->phase1_offset->getUpper(), rp2->phase1_offset->getLower());
rp1->phase1_offset->getLower() > rp2->phase1_offset->getUpper() ||
rp1->phase1_offset->getUpper() < rp2->phase1_offset->getLower();
if(ex.isEQ(Expr::getTrue(*SI)))
{
//errs() << "-----------NoAlias------------\n";
return NoAlias;
}
}
/// Phase 2 evaluation
if(eval(rp1,rp2))
{
//errs() << "-----------NoAlias------------\n";
return NoAlias;
}
//errs() << "-----------MayAlias-------------\n";
return AliasAnalysis::alias(LocA, LocB);
}
AliasAnalysis::ModRefResult
RangeBasedAliasAnalysis::getModRefInfo(ImmutableCallSite CS, const Location &Loc)
{
return AliasAnalysis::getModRefInfo(CS, Loc);
}
/// pointsToConstantMemory - Chase pointers until we find a (constant
/// global) or not.
bool RangeBasedAliasAnalysis::pointsToConstantMemory(const Location &Loc, bool OrLocal)
{
const Value* ptr = Loc.Ptr;
RangedPointer* rp = rbpa->getRangedPointer(ptr);
for(auto i = rp->addr_begin(), ie = rp->addr_end(); i != ie; i++)
{
Address* ai = *i;
const Value* V = ai->getBase()->getPointer();
if (OrLocal && isa<AllocaInst>(V))
continue;
if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
if (!GV->isConstant()) {
return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
}
continue;
}
return AliasAnalysis::pointsToConstantMemory(Loc, OrLocal);
}
return true;
}
/// Get the location associated with a pointer argument of a callsite.
AliasAnalysis::Location
RangeBasedAliasAnalysis::getArgLocation(ImmutableCallSite CS, unsigned ArgIdx, ModRefResult &Mask)
{
return AliasAnalysis::getArgLocation(CS, ArgIdx, Mask);
}
/// getModRefBehavior - Return the behavior when calling the given
/// call site.
AliasAnalysis::ModRefBehavior
RangeBasedAliasAnalysis::getModRefBehavior(ImmutableCallSite CS)
{
return AliasAnalysis::getModRefBehavior(CS);
}
/// getModRefBehavior - Return the behavior when calling the given function.
/// For use when the call site is not known.
AliasAnalysis::ModRefBehavior
RangeBasedAliasAnalysis::getModRefBehavior(const Function *F)
{
return AliasAnalysis::getModRefBehavior(F);
}