Pointer analysis.

Slides:



Advertisements
Similar presentations
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Advertisements

P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Pointer Analysis.
Context-Sensitive Interprocedural Points-to Analysis in the Presence of Function Pointers Presentation by Patrick Kaleem Justin.
Pointer Analysis – Part I Mayur Naik Intel Research, Berkeley CS294 Lecture March 17, 2009.
Flow insensitive pointer analysis: fixed S1: l := new Cons p := l S2: t := new Cons *p := t p := t l p S1 l p tS2 l p S1 t S2 l t S1 p S2 l t S1 p S2 l.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Pointer Analysis.
Worst case complexity of Andersen *x = y x abc y def x abc y def Worst case: N 2 per statement, so at least N 3 for the whole program. Andersen is in fact.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
Semi-Sparse Flow-Sensitive Pointer Analysis Ben Hardekopf Calvin Lin The University of Texas at Austin POPL ’09 Simplified by Eric Villasenor.
Interprocedural analysis © Marcelo d’Amorim 2010.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Feedback: Keep, Quit, Start
Next Section: Pointer Analysis Outline: –What is pointer analysis –Intraprocedural pointer analysis –Interprocedural pointer analysis (Wilson & Lam) –Unification.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
From last time: live variables Set D = 2 Vars Lattice: (D, v, ?, >, t, u ) = (2 Vars, µ, ;,Vars, [, Å ) x := y op z in out F x := y op z (out) = out –
Interprocedural pointer analysis for C We’ll look at Wilson & Lam PLDI 95, and focus on two problems solved by this paper: –how to represent pointer information.
Flow insensitivity and imprecision If you ignore flow, then you lose precision. main() { x := &y;... x := &z; } Flow insensitive analysis tells us that.
Previous finals up on the web page use them as practice problems look at them early.
Another example p := &x; *p := 5 y := x + 1;. Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 y := x + 1; ???
Range Analysis. Intraprocedural Points-to Analysis Want to compute may-points-to information Lattice:
Intraprocedural Points-to Analysis Flow functions:
From last time S1: l := new Cons p := l S2: t := new Cons *p := t p := t l p S1 l p tS2 l p S1 t S2 l t S1 p S2 l t S1 p S2 l t S1 p L2 l t S1 p S2 l t.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Comparison Caller precisionCallee precisionCode bloat Inlining context-insensitive interproc Context sensitive interproc Specialization.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Reps Horwitz and Sagiv 95 (RHS) Another approach to context-sensitive interprocedural analysis Express the problem as a graph reachability query Works.
Schedule Midterm out tomorrow, due by next Monday Final during finals week Project updates next week.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Composing Dataflow Analyses and Transformations Sorin Lerner (University of Washington) David Grove (IBM T.J. Watson) Craig Chambers (University of Washington)
Pointer analysis. Pointer Analysis Outline: –What is pointer analysis –Intraprocedural pointer analysis –Interprocedural pointer analysis Andersen and.
Procedure Optimizations and Interprocedural Analysis Chapter 15, 19 Mooly Sagiv.
Precision Going back to constant prop, in what cases would we lose precision?
Global Redundancy Elimination: Computing Available Expressions Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Static Program Analysis of Embedded Software Ramakrishnan Venkitaraman Graduate Student, Computer Science Advisor: Dr. Gopal Gupta
ESEC/FSE-99 1 Data-Flow Analysis of Program Fragments Atanas Rountev 1 Barbara G. Ryder 1 William Landi 2 1 Department of Computer Science, Rutgers University.
Using Types to Analyze and Optimize Object-Oriented Programs By: Amer Diwan Presented By: Jess Martin, Noah Wallace, and Will von Rosenberg.
Pointer Analysis Survey. Rupesh Nasre. Aug 24, 2007.
Points-To Analysis in Almost Linear Time Josh Bauman Jason Bartkowiak CSCI 3294 OCTOBER 9, 2001.
Pointer Analysis – Part I CS Pointer Analysis Answers which pointers can point to which memory locations at run-time Central to many program optimization.
Pointer Analysis CS Alias Analysis  Aliases: two expressions that denote the same memory location.  Aliases are introduced by:  pointers  call-by-reference.
Inter-procedural analysis
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
Manuel Fahndrich Jakob Rehof Manuvir Das
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
Data Flow Analysis Suman Jana
Chapter - 12 GRAPH MATRICES AND APPLICATIONS.
Spring 2016 Program Analysis and Verification
Pointer Analysis Lecture 2
Structural testing, Path Testing
Interprocedural Analysis Chapter 19
Symbolic Implementation of the Best Transformer
Topic 10: Dataflow Analysis
University Of Virginia
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Another example: constant prop
This Lecture Substitution model
Pointer Analysis Lecture 2
Data Flow Analysis Compiler Design
Basics of Recursion Programming with Recursion
Static Single Assignment
Union-Find.
Ch. 2: Getting Started.
Data Structures & Algorithms
Pointer Analysis Jeff Da Silva Sept 20, 2004 CARG.
This Lecture Substitution model
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Pointer analysis John Rollinson & Kaiyuan Li
Presentation transcript:

Pointer analysis

Pointer Analysis Outline: What is pointer analysis Intraprocedural pointer analysis Interprocedural pointer analysis Andersen and Steensgaard

Pointer and Alias Analysis Aliases: two expressions that denote the same memory location. Aliases are introduced by: pointers call-by-reference array indexing C unions

Useful for what? Improve the precision of analyses that require knowing what is modified or referenced (eg const prop, CSE …) Eliminate redundant loads/stores and dead stores. Parallelization of code can recursive calls to quick_sort be run in parallel? Yes, provided that they reference distinct regions of the array. Identify objects to be tracked in error detection tools x := *p; ... y := *p; // replace with y := x? *x := ...; // is *x dead? x.lock(); ... y.unlock(); // same object as x?

Kinds of alias information Points-to information (must or may versions) at program point, compute a set of pairs of the form p ! x, where p points to x. can represent this information in a points-to graph Alias pairs at each program point, compute the set of of all pairs (e1,e2) where e1 and e2 must/may reference the same memory. Storage shape analysis at each program point, compute an abstract description of the pointer structure. p x y z p

Intraprocedural Points-to Analysis Want to compute may-points-to information Lattice:

Flow functions Fx := k(in) = Fx := a+b(in) = x := k x := a + b in out

Flow functions Fx := y(in) = Fx := &y(in) = x := y x := &y in out in

Flow functions Fx := *y(in) = F*x := y(in) = x := *y *x := y in out in

Intraprocedural Points-to Analysis Flow functions:

Pointers to dynamically-allocated memory Handle statements of the form: x := new T One idea: generate a new variable each time the new statement is analyzed to stand for the new location:

Example l := new Cons p := l t := new Cons *p := t p := t

Example solved l := new Cons p := l t := new Cons *p := t p := t p t l

What went wrong? Lattice infinitely tall! We were essentially running the program Instead, we need to summarize the infinitely many allocated objects in a finite way New Idea: introduce summary nodes, which will stand for a whole class of allocated objects.

What went wrong? Example: For each new statement with label L, introduce a summary node locL , which stands for the memory allocated by statement L. Summary nodes can use other criterion for merging.

Example revisited S1: l := new Cons p := l S2: t := new Cons *p := t

Example revisited & solved S1: l := new Cons Iter 1 Iter 2 Iter 3 p := l p t p t l S1 l S1 S2 p l S1 S2 S2: t := new Cons p t p t l S1 t S2 l S1 S2 l S1 S2 p *p := t p t p t t l S1 S2 l S1 S2 l S1 S2 p p := t p t p t p t l S1 S2 l S1 S2 l S1 S2

Array aliasing, and pointers to arrays Array indexing can cause aliasing: a[i] aliases b[j] if: a aliases b and i = j a and b overlap, and i = j + k, where k is the amount of overlap. Can have pointers to elements of an array p := &a[i]; ...; p++; How can arrays be modeled? Could treat the whole array as one location. Could try to reason about the array index expressions: array dependence analysis.

Fields Can summarize fields using per field summary for each field F, keep a points-to node called F that summarizes all possible values that can ever be stored in F Can also use allocation sites for each field F, and each allocation site S, keep a points-to node called (F, S) that summarizes all possible values that can ever be stored in the field F of objects allocated at site S.

Summary We just saw: intraprocedural points-to analysis handling dynamically allocated memory handling pointers to arrays But, intraprocedural pointer analysis is not enough. Sharing data structures across multiple procedures is one the big benefits of pointers: instead of passing the whole data structures around, just pass pointers to them (eg C pass by reference). So pointers end up pointing to structures shared across procedures. If you don’t do an interproc analysis, you’ll have to make conservative assumptions functions entries and function calls.

Conservative approximation on entry Say we don’t have interprocedural pointer analysis. What should the information be at the input of the following procedure: global g; void p(x,y) { ... } x y g

Conservative approximation on entry Here are a few solutions: x y g global g; void p(x,y) { ... } x,y,g & locations from alloc sites prior to this invocation locations from alloc sites prior to this invocation They are all very conservative! We can try to do better.

Interprocedural pointer analysis Main difficulty in performing interprocedural pointer analysis is scaling One can use a top-down summary based approach (Wilson & Lam 95), but even these are hard to scale

Example revisited Cost: space: store one fact at each prog point time: iteration S1: l := new Cons Iter 1 Iter 2 Iter 3 p := l p t p t l S1 l S1 S2 p l S1 S2 S2: t := new Cons p t p t l S1 t S2 l S1 S2 l S1 S2 p *p := t p t p t t l S1 S2 l S1 S2 l S1 S2 p p := t p t p t p t l S1 S2 l S1 S2 l S1 S2

New idea: store one dataflow fact Store one dataflow fact for the whole program Each statement updates this one dataflow fact use the previous flow functions, but now they take the whole program dataflow fact, and return an updated version of it. Process each statement once, ignoring the order of the statements This is called a flow-insensitive analysis.

Flow insensitive pointer analysis S1: l := new Cons p := l S2: t := new Cons *p := t p := t

Flow insensitive pointer analysis S1: l := new Cons p := l l S1 p S2: t := new Cons l S1 t S2 p *p := t t l S1 S2 p p := t p t l S1 S2

Flow sensitive vs. insensitive S1: l := new Cons Flow-sensitive Soln Flow-insensitive Soln p := l p t l S1 S2 S2: t := new Cons p t p t l S1 S2 *p := t l S1 S2 p t l S1 S2 p := t p t l S1 S2

What went wrong? What happened to the link between p and S1? Can’t do strong updates anymore! Need to remove all the kill sets from the flow functions. What happened to the self loop on S2? We still have to iterate!

Flow insensitive pointer analysis: fixed S1: l := new Cons p := l l S1 p S2: t := new Cons l S1 t S2 p *p := t t l S1 S2 p p := t

Flow insensitive pointer analysis: fixed This is Andersen’s algorithm ’94 Final result S1: l := new Cons Iter 1 Iter 2 Iter 3 p := l p t p t l S1 l S1 S2 p l S1 S2 S2: t := new Cons p t p t l S1 t S2 l S1 L2 l L1 L2 p *p := t p t p t t l S1 S2 l S1 S2 l S1 S2 p p := t p t p t l t S1 p S2 l S1 S2 l S1 S2

Flow sensitive vs. insensitive, again S1: l := new Cons Flow-sensitive Soln Flow-insensitive Soln p := l p t l S1 S2 S2: t := new Cons p t l S1 S2 l t S1 p S2 *p := t p t l S1 S2 p := t p t l S1 S2

Flow insensitive loss of precision Flow insensitive analysis leads to loss of precision! main() { x := &y; ... x := &z; } Flow insensitive analysis tells us that x may point to z here! However: uses less memory (memory can be a big bottleneck to running on large programs) runs faster

In Class Exercise! S1: p := new Cons S2: q := new Cons *p = q r = &q *q = p s = r s = p *r = s

In Class Exercise! solved S1: p := new Cons S2: q := new Cons p S1 *p = q q S2 r = &q r s *q = r *q = p s = r s = p *r = s

Worst case complexity of Andersen *x = y a b c d e f a b c d e f Worst case: N2 per statement, so at least N3 for the whole program. Andersen is in fact O(N3)

New idea: one successor per node Make each node have only one successor. This is an invariant that we want to maintain. x y x y *x = y a,b,c d,e,f a,b,c d,e,f

More general case for *x = y

More general case for *x = y

Handling: x = *y x y x = *y

Handling: x = *y x x = *y y

Handling: x = y (what about y = x?)

Handling: x = y (what about y = x?) get the same for y = x Handling: x = &y x = &y x y y,…

Our favorite example, once more! S1: l := new Cons 1 p := l 2 S2: t := new Cons 3 *p := t 4 p := t 5

Our favorite example, once more! 1 2 S1: l := new Cons 1 S1 S1 3 p := l 2 l p t l p t 4 S2: t := new Cons 3 S1 S2 S1 S2 5 *p := t 4 l p t l p t p := t 5 S1 S2 S1,S2

Flow insensitive loss of precision S1: l := new Cons Flow-insensitive Unification- based Flow-sensitive Subset-based Flow-insensitive Subset-based p := l p t l S1 S2 S2: t := new Cons p t l t S1 p S2 l p t l S1 S2 *p := t p t S1,S2 l S1 S2 p := t p t l S1 S2

Another example bar() { i := &a; j := &b; 1 foo(&i); foo(&j); 2 // i pnts to what? *i := ...; } void foo(int* p) { printf(“%d”,*p); 1 2 3 4

Another example bar() { i := &a; j := &b; foo(&i); foo(&j); // i pnts to what? *i := ...; } void foo(int* p) { printf(“%d”,*p); i i j i j 1 2 3 1 2 a a b a b 3 4 4 p p i j i,j a b a,b

inverse Ackermann function Almost linear time Time complexity: O(Nα(N, N)) So slow-growing, it is basically linear in practice For the curious: node merging implemented using UNION-FIND structure, which allows set union with amortized cost of O(α(N, N)) per op. Take CSE 202 to learn more! inverse Ackermann function

In Class Exercise! S1: p := new Cons S2: q := new Cons *p = q r = &q *q = p s = r s = p *r = s

In Class Exercise! solved S1: p := new Cons q,S1,s2 S2: q := new Cons Steensgaard p r s *p = q r = &q p S1 *q = r *q = p Andersen q S2 s = r s = p r s *r = s

Advanced Pointer Analysis Combine flow-sensitive/flow-insensitive Clever data-structure design Context-sensitivity