Академический Документы
Профессиональный Документы
Культура Документы
• Optimize loops
– Loop invariant code motion [last time]
Loop Optimizations – Strength reduction of induction variables
and Pointer Analysis – Induction variable elimination
CS 412/413 Spring 2008 Introduction to Compilers 1 CS 412/413 Spring 2008 Introduction to Compilers 2
CS 412/413 Spring 2008 Introduction to Compilers 3 CS 412/413 Spring 2008 Introduction to Compilers 4
1
Families of Induction Variables Representation
• Basic induction variable: a variable whose only definition in the • Representation of induction variables in family i by triples:
loop body is of the form
– Denote basic induction variable i by <i, 1, 0>
i=i+c
– Denote induction variable k=i*a+b by triple <i, a, b>
where c is a loop-invariant value
CS 412/413 Spring 2008 Introduction to Compilers 5 CS 412/413 Spring 2008 Introduction to Compilers 6
CS 412/413 Spring 2008 Introduction to Compilers 7 CS 412/413 Spring 2008 Introduction to Compilers 8
2
General Algorithm Strength Reduction
• Algorithm: • Gives opportunities for copy propagation, dead code
elimination
For each induction variable j with triple <i,a,b>
whose definition involves multiplication: s = 3*i+1; s = 3*i+1;
1. create a new variable s while (i<10) { while (i<10) {
2. replace definition of j with j=s j = s;
3. immediately after i=i+c, insert s = s+a*c a[j] = a[j] –2; a[s] = a[s] –2;
(here a*c is constant) i = i+2; i = i+2;
s= s+6; s= s+6;
4. insert s = a*i+b into preheader
} }
• Correctness: transformation maintains invariant s = a*i+b
CS 412/413 Spring 2008 Introduction to Compilers 9 CS 412/413 Spring 2008 Introduction to Compilers 10
CS 412/413 Spring 2008 Introduction to Compilers 11 CS 412/413 Spring 2008 Introduction to Compilers 12
3
Induction Variable Elimination Where We Are
For each basic induction variable i whose only uses are • Defined dataflow analysis framework
– The test condition i < u
• Used it for several analyses
– The definition of i: i = i + c – Live variables
– Available expressions
• Take a derived induction variable k in family i, with – Reaching definitions
triple <i,c,d> – Constant folding
• Replace test condition i < u with k < c*u+d
• Remove definition i = i+c if i is not live on loop exit • Loop transformations
– Loop invariant code motion
– Induction variables
• Next:
– Pointer alias analysis
CS 412/413 Spring 2008 Introduction to Compilers 13 CS 412/413 Spring 2008 Introduction to Compilers 14
4
Alias Analysis Problem Simple Alias Analyses
• Address-taken analysis:
• Goal: for each variable v that may hold an address, – Consider AT = set of variables whose addresses are taken
compute the set Ptr(v) of possible targets of v – Then, Ptr(v) = AT, for each pointer variable v
– Ptr(v) is a set of variables (or objects) – Addresses of heap variables are always taken at allocation
– Ptr(v) includes stack- and heap-allocated variables (objects) sites (e.g., x = new int[2]; x=malloc(8); )
– Hence AT includes all heap variables
• Is a “may” analysis: if x Ptr(v), then v may hold the
• Type-based alias analysis:
address of x in some execution of the program
– If v is a pointer (or reference) to type T, then Ptr(v) is the
set of all variables of type T
• No alias information: for each variable v, Ptr(v) = V, – Example: p->f and q->f can be aliases only if p and q are
where V is the set of all variables in the program references to objects of the same type
– Works only for strongly-typed languages
CS 412/413 Spring 2008 Introduction to Compilers 17 CS 412/413 Spring 2008 Introduction to Compilers 18
CS 412/413 Spring 2008 Introduction to Compilers 19 CS 412/413 Spring 2008 Introduction to Compilers 20
5
Alias Analysis Example Alias Analysis Uses
• Once alias information is available, use it in other
Program Points-to dataflow analyses
CFG
Graph
x=&a (at the end of program) • Example: Live variable analysis
x=&a; y=&b Use alias information to compute use[I] and def[I] for
y=&b; c=&i x a
load and store statements:
if(i) i
c=&i; y b
if(i) x=y; x = *y use[I] = {y} ⋃ Ptr(y) def[I]={x}
x=y
*x=c; c
*x = y use[I] = {x,y} def[I]=Ptr(x)
*x=c
CS 412/413 Spring 2008 Introduction to Compilers 21 CS 412/413 Spring 2008 Introduction to Compilers 22