Upload
pomona
View
49
Download
1
Embed Size (px)
DESCRIPTION
Alias Analysis. 2006 년 5 월 1 일 ADVANCED COMPILER 이정옥. p. n. 4. What Is Alias Analysis. Determination of Storage Locations That May Be Accessed in Two or More Ways. main() { int *p; int n; p=&n; n=4 printf(“%d\n”,*p); }. simple pointer aliasing in C. - PowerPoint PPT Presentation
Citation preview
Alias AnalysisAlias Analysis
20062006 년 년 55 월 월 11 일일ADVANCED COMPILER ADVANCED COMPILER
이정옥이정옥
2
What Is Alias AnalysisWhat Is Alias Analysis
Determination of Storage Locations That May Be Accessed in Two or More Ways
main()
{ int *p;
int n;
p=&n;
n=4
printf(“%d\n”,*p);
}
simple pointer aliasing in C
Relationship between the variable at the call to printf()
44
pp
nn
3
ImportantImportant
Important to Doing Most of Optimization
exam1()exam1()
{ int a, k;{ int a, k;
extern int *q;extern int *q;
. . .. . .
k = a + 5;k = a + 5;
f(a,&k);f(a,&k);
*q = 13;*q = 13;
k = a + 5; /*redundant?*/k = a + 5; /*redundant?*/
. . .. . .
}}
if and only if both the call to f() and the assignment *q=13 leave the values of both k and a unmodified
■■ Example of the Importance of Alias Example of the Importance of Alias Computation Computation ■■
Interprocedural AliasInterprocedural Alias
Intraprocedural AliasIntraprocedural Alias
In pracIn practtice, usually
ice, usually
more Important
more Important
To Chapter 19
To Chapter 19
4
Classification for Alias InformationClassification for Alias Information
May Alias InformationMay Alias Information p may point to y or zp may point to y or z
Must Alias InformationMust Alias Information p must points to xp must points to x
Flow-Insensitive Flow-Insensitive InformationInformation
p may points to xp may points to x (there is a path, p assigned the address of x)(there is a path, p assigned the address of x)
Flow-Sensitive Flow-Sensitive InformationInformation
p points to x in block B7p points to x in block B7
p=&yp=&y p=&zp=&zp=&xp=&x
5
Formal Characterization of AliasingFormal Characterization of Aliasing
x alias y if and only if x and y x alias y if and only if x and y may, may,
possibly at different times, possibly at different times, refer refer
to the same storage location.to the same storage location.
SymmetricSymmetric
. x alias y == y alias x. x alias y == y alias x
Intransitive Intransitive
. a alias b, and b alias c but . a alias b, and b alias c but not not
→→ a alias c.a alias c.
Flow-Insensitive Flow-Insensitive May InformationMay Information
x alias y if and only if x and y x alias y if and only if x and y
must, throughout the must, throughout the execution of execution of
a procedure, refer to the a procedure, refer to the same same
storage location.storage location.
SymmetricSymmetric
. x alias y == y alias x. x alias y == y alias x
Transitive Transitive
. a alias b and b alias c . a alias b and b alias c
→→ a alias ca alias c
Flow-Insensitive Flow-Insensitive Must InformationMust Information
6
Formal Characterization of AliasingFormal Characterization of Aliasing
Flow-Sensitive Flow-Sensitive May InformationMay Information
Flow-Sensitive Flow-Sensitive Must InformationMust Information
Alias(p,v) = SL Alias(p,v) = SL . at point p variable v may refer to. at point p variable v may refer to any of the locations in SLany of the locations in SL
Alias(p,a) ∩ Alias(p,b) ≠Alias(p,a) ∩ Alias(p,b) ≠ and and Alias(p,b) ∩ Alias(p,c) ≠Alias(p,b) ∩ Alias(p,c) ≠, then, then Alias(p,a) ∩ Alias(p,c) ≠Alias(p,a) ∩ Alias(p,c) ≠ but not but not necessarilynecessarily
Alias(p1,a) ∩ Alias(p2,a) ≠Alias(p1,a) ∩ Alias(p2,a) ≠ and and Alias(p2,a) ∩ Alias(p3,a) ≠Alias(p2,a) ∩ Alias(p3,a) ≠, then , then Alias(p1,a) ∩ Alias(p3,a) ≠Alias(p1,a) ∩ Alias(p3,a) ≠ but but not necessarilynot necessarily
Alias(p,v) = l Alias(p,v) = l . at point p variable v must refer to . at point p variable v must refer to location llocation l
Alias(p,a) = Alias(p,b) and Alias(p,a) = Alias(p,b) and Alias(p,b) = Alias(p,c) Alias(p,b) = Alias(p,c) → → Alias(p,a) = Alias(p,c)Alias(p,a) = Alias(p,c)
Alias(p1,a) = Alias(p2,a) and Alias(p1,a) = Alias(p2,a) and Alias(p2,a) = Alias(p3,a) Alias(p2,a) = Alias(p3,a) →→ Alias(p1,a) = Alias(p3,a)Alias(p1,a) = Alias(p3,a)
7
Example for Different AliasesExample for Different Aliases
exam2()exam2()
{ int a, b, c[100], d, i;{ int a, b, c[100], d, i;
extern int *q;extern int *q;
. . .. . .
q = &a;q = &a;
a = 2;a = 2;
b = *q + 2;b = *q + 2;
. . .. . .
q = &b;q = &b;
for( i = 0; i < 100; i++ ) {for( i = 0; i < 100; i++ ) {
c[i] = c[i] + a;c[i] = c[i] + a;
*q = i; /* can out loop ? */*q = i; /* can out loop ? */
}}
d = * q + a;d = * q + a;
}}
If we were to do If we were to do flow-insensitive flow-insensitive may alias,may alias, q could point to a q could point to a or b or b so not loop so not loop invariantinvariantIf we were to do If we were to do flow-sensitive must flow-sensitive must alias,alias, q must point to b in q must point to b in thisthisso loop invariant.so loop invariant.
exam2()exam2()
{ int a, b, c[100], d, i;{ int a, b, c[100], d, i;
extern int *q;extern int *q;
. . .. . .
q = &a;q = &a;
a = 2;a = 2;
b = *q + 2;b = *q + 2;
. . .. . .
q = &b;q = &b;
for( i = 0; i < 100; i++ ) {for( i = 0; i < 100; i++ ) {
c[i] = c[i] + a;c[i] = c[i] + a;
}}
*q = 100; /* can ! */*q = 100; /* can ! */
d = * q + a;d = * q + a;
}}
8
Divide to Two PassDivide to Two Pass
A language-specific Component,
called the alias gatherer
Compiler front end to provide
us.
A single component in the
optimizer, called the alias
propagator
Performs a data flow analysis
9
Aliases in Various Real Programming Aliases in Various Real Programming LanguagesLanguages
☼☼ Because C is most popular and C is most difficult, I will Because C is most popular and C is most difficult, I will explain only in C.explain only in C.
Fortran 90Fortran 90
CC
PascalPascal
Fortran 77Fortran 77
10
Aliases in C (ANSI-standard C)Aliases in C (ANSI-standard C)
[union]Union type may have several fields declared, all of which overlap in storage.
[Array]Array index : a[100], we have a = &a[0]
[Pointer]Int *p, a[100]; p = a;
[Parameter passing and returning a pointer]
f(i,j)f(i,j)
int *i, *j;int *i, *j;
{ *i = *j + 1;{ *i = *j + 1;
}}
11
The Alias GathererThe Alias Gatherer
[P][P]denote a program point,
[entry+][entry+] immediately follow the entry node
[exit-][exit-]immediately precede the exit node
[stmt(P)][stmt(P)]the statement immediately preceding P
12
The Alias Gatherer_The Alias Gatherer_EXAMPLE EXAMPLE
entryentry
q = pq = p
q = NILq = NIL
q -> elt == mq -> elt == m
return qreturn q
q = q -> npq = q -> np return NILreturn NIL
exitexit
entry+
1
2
3
4
5
exit-
6
Y
Y
N
N
stmt(2) = stmt(6) = node(q =NIL)stmt(2) = stmt(6) = node(q =NIL)
13
The Alias GathererThe Alias Gatherer
[x] a scalar variable, an array, a structure, the value of a pointer,etc.
[memp(x)] an abstarct memory location associated with object x.(at point P)
[star(o)]a static or automatically allocated memory area occupied bylinguistic object o.
14
The Alias GathererThe Alias Gatherer
[anon(ty)][anon(ty)] If x is dynamically allocated (ty is the type of x).
[anon][anon] If x is dynamically allocated and its type is not known.
∀ty1,ty2, if ty1 ≠ ty2, then anon(ty1) ≠ anon(ty2)
...........................
.............
..................
knownnotistypeitsandallocatedydynamicallisxanon
allocteddynmicallyisxanon(ty)
allocatedllyautomaticastaticallyisxstar(x)
memp(x)
15
The Alias Gatherer_The Alias Gatherer_EXAMPLE EXAMPLE
B4
mem2(i) = star(i)
mem5(q) = anon(ptr)
2
entry+
1
entry
receive p(val)
i = 1
i <= 5
return q
p = q q = q.next
print (q.value)
3 4
5
exit-
6
Y
Y
N
N
p = q
exit
7
8
9
B1
B2
B3
B5
B6
B7
B8
16
The Alias GathererThe Alias Gatherer
[any(ty)][any(ty)] the set of all possible storage location of the type
[any][any]the set of all possible storage location.
[globals][globals]all location in common storage (in c: includes all variablesdeclared outside and declared with the extern).
17
The Alias GathererThe Alias Gatherer
ovrp(x) = the set of abstract memory locations x may overlap with at point p
ptr(x) = the set of abstract memory locations x may point to at point p
Define a Series of FunctionDefine a Series of Function
18
The Alias GathererThe Alias Gatherer
refp(x) = the set of abstract memory locations reachable through arbitrarily many dereferences from x at point p.
We define refp1(x) = ptrp(x) , for i > 1,
refpi(x) = ptrp (felds(refp
i-1(x))) Where fields(x) = x (if x is a point)
the set of pointer-valued fields (if x is a structure)
refp(x) =
ref(x) = the set of abstract memory locations reachable through arbitrarily many dereferences from x in dependent of the program point.
1
)(i
ip xref
p1 p2 p3 p4
19
Rules for CRules for C
p, q are pointers.
s is a structure
st is a structure type
ut is union type
a [] is array
i is integer-valued
if stmt(P) is as following…
Now assume thatNow assume that
20
Rules for CRules for C
p=null
P
P’
ptrP(p) =
1
p=malloc()
P
P’
ptrp(p)=anon
2
p = &a
P
P’
ptrp(p)={memp(a)}={memp’(a)}
3
21
Rules for CRules for C
ptrp(p1)=ptrp(p2)= ptrp’(p2)
4
ptrp(p1)=ptrp(p2)= mementry+(*p2)
p1=p2
P
P’
p1=p2
P
entry+
p1=p2->p3
P
P’
ptrp(p1)=ptrp’(p2->p3)
5
22
Rules for CRules for C
p=&a[expr]
P
P’
prtp(p)=ovrp(a)=ovrp’(a)={memp’(a)}
6
p = p + i
P
P’
ptrp(p)=ptrp’(p)
7
*p=a
P
P’ptrp(p)=ptrp’(p) and if *p is a pointer
then ptrp(*p) = ptrp(a) = ptrp’(a)
8
23
Rules for CRules for C
ptrp(p)=ptrp(q)=ptrp’(p) ∩ ptrp’(q)if(p==q)
P
P’9
YN
ovrp(s)={memp(s)}= {memp(s.si)} and for each i,{memp(s.si)}= ovrp(s.si) ⊂ ovrp(s) and for each j≠i,ovrp(s.si) ∩ ovrp(s.sj)=
10 struct st {
type s1;
…
type sn;
} s;
n
i 1
24
Rules for CRules for C
p=&s
P
P’ptrp(p)={memp(*p)} = {memp(p->si)}
and for each i{memp(p->si)}=ptrp(p->si)⊂ptrp(s) and for all i≠j,ptrp(p->si) ∩ ptrp(p->sj) = and for all other objects x,ptrp(p) ∩ {memp(x)}=
11 n
i 1
ovrp(u)={memp(u)}= {memp(u.ui)} for i=1,…,n
12 union ut {
type u1;
…
type un;
} u;
25
Rules for CRules for C
p=&u
P
P’ ptrp(p)={memp(*p)} = {memp(p->ui)}
and for each i and for all other objects x,ptrp(p) ∩ {memp(x)}=
13
f( )
P
P’ ptrp(p)=refp’(p) for all pointers p
p are arguments to f(), p are global pointer, or p =f().
14
26
Alias PropagatorAlias Propagator
The Data-Flow Function in Alias PropagatorThe Data-Flow Function in Alias Propagator
P
P’ Ovr(P,x)=1 ovrp(x) if stmt(P) affects p
Ovr(P’,x)= otherwise
Ptr(P,p)= ptrp(p) if stmt(P) affects p
Ptr(P’,p)= otherwise
Ovr() , Ptr()Ovr() , Ptr()
27
alias propagatoralias propagator
n
i 1
empty
P
P1…Pn
Ovr(P,x)=
2
Ovr(Pi,x)
Ptr(P,p)= Ptr(Pi,p)n
i 1
test
P
P1…Pn
3 Ovr(Pi,x) = Ovr(P,x)
Ptr(Pi,p) = Ptr(P,p)
28
Initial Values for Ovr() and Ptr()Initial Values for Ovr() and Ptr()
Ovr(P,x) Ovr(P,x) =={star(x)} if P = entry+ otherwise
Ptr(P,p) Ptr(P,p) ==
if P = entry+ and p is localany if P = entry+ and p is global{mementry+(*p)} if P = entry+ and p is a parameter
otherwise
29
First ExampleFirst Example
typedef struct {int i; char c;} struct_type;
struct_type s, *ps, **pps1, **pps2, arr[100];
pps1 = &ps;
pps2 = pps1;
*pps2 = &s;
ps->i = 13;
func(ps);
arr[1].i = 10;
entry
pps1 = &ps
pps2 = pps1
*pps2 = &s
ps -> i = 13
func(ps)
arr[1].i = 10
exit
entry+
1
2
3
4
5
exit-
ptr1(pps1)={mementry+(ps)} ={star(ps)}
ptr2(pps2)= ptr2(pps1)=ptr1(pps1)
ptr3(pps2)=ptr2(pps2)
ptr3(*pps2)=ptr3(&s)=ptr2(&s)=ovr2(s)
ptr5(ps)=ref4(ps)
Ovr(entry+,s) = {star(s)}
Ovr(entry+,ps) = {star(ps)}
Ovr(entry+,pps1) = {star(pps1)}
Ovr(entry+,pps2) = {star(pps2)}
Ovr(entry+,arr) = {star(arr)}
30
First ExampleFirst Example
entry
pps1 = &ps
pps2 = pps1
*pps2 = &s
ps -> i = 13
func(ps)
arr[1].i = 10
exit
entry+
1
2
3
4
5
exit-
ptr1(pps1) ={star(ps)}
ptr2(pps2)=ptr1(pps1)
ptr3(pps2)=ptr2(pps2)
ptr3(*pps2)=ovr2(s)
ptr5(ps)=ref4(ps)
Ovr(entry+,s) = {star(s)}
Ovr(entry+,ps) = {star(ps)}
Ovr(entry+,pps1) = {star(pps1)}
Ovr(entry+,pps2) = {star(pps2)}
Ovr(entry+,arr) = {star(arr)}
Ptr(1,pps1)={star(ps)}
Ptr(2,pps2)={star(ps)}
Ptr(3,ps) = {star(s)}
Ptr(5,ps) = ref5(ps) ∪ ∪ ref(p) = {star(s)}p∈globals
globalsp
Ovr() is identical
31
Second ExampleSecond Example
int arith(n)
int n;
{ int i, j, k, *p, *q;
p =&i:
i = n +1;
q = &j;
j = n * 2;
k = *p + *q;
return k;
}
entry
p = &i
i = n +1
q = &j
j = n * 2
k = *p + *q
return k
exit
entry+
1
2
3
4
5
exit-
ptr1(p) ={mem1(i)}={star(i)} ptr3(q) ={mem3(j)}={star(j)}
the pointers are only fetched and never stored,
we can optimize
k = *p + *q; k = i + j;
32
Third ExampleThird Example
typedef struct {node * np; int elt;} node;
node *find(p,m)
node *p;
int m;
{ node * q;
for (q=p; q==NIL; q = q->np)
if (q->elt ==m)
return q;
return NIL;
}N
2
3
1
entry+
entry
q = p
q == NIL
q -> elt == m
return q
q = q → np return NIL
exit
4
5
exit
6
Y
Y
N
33
Third ExampleThird Example
ptr1(q)=ptr(p)=mementry+(*p)
ptr6(q)={nil}
ptr5(q)=ptr5(q → np)=ptr4(q → np)
Ptr(entry+,p)= {mementry+(*p)}
Ptr(1,q)=ptr1(q) Ptr(2,q)=Ptr(1,q)∪Ptr(5,q) Ptr(3,q)=Ptr2(q) Ptr(4,q)=Ptr2(q) Ptr(5,q)=ptr5(q) Ptr(6,q)=ptr6(q)
Ptr(exit-,q)=Ptr(3,q )∪Ptr(6,q)
4
N
entry+
1
2
3Y
N
entry
q = p
q == NIL
q → elt == m
return q
q = q → np return NIL
exit
5
exit-
6
Y
34
Third ExampleThird Example
Ptr(entry+,p)={mementry+(*p)}
Ptr(1,q)= {mementry+(*p)}
Ptr(2,q)= {mementry+(*p)} ∪Ptr(5,q)
Ptr(3,q)= {mementry+(*p)} ∪ Ptr(5,q)
Ptr(4,q)= {mementry+(*p)} ∪Ptr(5,q)
Ptr(5,q)=ptr4(q->np) =ref4(q)
Ptr(6,q)={nil}
Ptr(exit-,q)= {nil,mementry+(*p)} ∪Ptr(5,q)
35
Third ExampleThird Example
Ptr(entry+,p)={mementry+(*p)}
Ptr(1,q) = {mementry+(*p)}
Ptr(2,q) = {mementry+(*p)} ∪ ref4(q)
Ptr(3,q) = {mementry+(*p)} ∪ ref4(q)
Ptr(4,q) = {mementry+(*p)} ∪ ref4(q)
Ptr(5,q) = ref4(q)
Ptr(6,q)={nil}
Ptr(exit-,q)= {nil,mementry+(*p)} ∪ ref4(q)