Docsity

Inter Procedural Control Flow Analysis - Lecture Slides | CS 6463, Study notes of Computer Science

Partial preview of the text

Download Inter Procedural Control Flow Analysis - Lecture Slides | CS 6463 and more Study notes Computer Science in PDF only on Docsity!

Inter-procedural

Control Flow Analysis

Using Constraint-based

Approach

The Dynamic Dispatch Problem

 Which function is called by p(x)?

int myFunc ( int (*p)(int), …) { …… return p(x); }  P is a function pointer. What function could p point to (what is the value of p)?  P is a function parameter, so the value of p is unknown unless inter-procedural dataflow analysis is performed  But inter-procedural data-flow requires an inter-procedural control flow graph (or a call graph)

 The problem is relevant for

 Imperative languages that allow functions as parameters  Object oriented languages and functional languages

Defining the Analysis

 What is the domain of analysis

 What is the solution space?

 What could be the values for each function pointer expression?  Specification of the analysis

 How to compute the solution?

 how to accommodate the information flow from function definitions to function invocations  Well-definedness of the analysis

 What are the properties of the solution space?

 Does it compute a solution?

 Does the algorithm terminate?

 Is the solution precise?

Specification of Domain

 What is the solution?

 For each expression in the program, could it have a

function pointer value? If yes, what functions may it

point to? (if no, the solution is ∅)

 Must keep track of the values of variables (especially

function parameters)

 To represent the solution, label each expression within the program, compute

 An abstract cache (C) so that for each expression e,

 C(e) contains the set of function values e may have

 An abstract environment (P) so that for each variable x,

 P(x) contains the set of function values x may have

cs6463 7 Example Code and Control-flow Analysis Solution

 Example code

((fun f x => x) (fun g y => y))  Labels: 1: x; 2: (fun f x => x) 3: y; 4: (fun g y => y) 5: ((fun f x => x) (fun g y => y))

 Example CFA solution (guesses of the (C,P) mappings)

g { fun g y => y} y^ ∅ f {fun f x => x} x { fun g y => y} 5 {fun g y => y} 4 {fun g y => y} (^3) ∅ (^2) {fun f x => x} 1 {fun g y => y}

Solution Space of CFA

 Formally  Abstract values: Val = Power(Term)

 Each term is a function definition in the form (fun f x => e0)

 Abstract environment: Env = Var -> Val

 Var: the set of all variables (including function parameters)

 Abstract cache: Cache = Label -> Val

 Label: the set of labels (expressions)

 Each solution: a pair of (P,C) ⊆(Env, Cache)

Specification of CFA (2)

 Function invocation (application)

 (C,P) |= ((e1)l1 (e2)l2)l3 iff (C,P) |= (e1)l1, (C,P) |= (e2)l2, and ∀ (fun f x => (e0)l0) ∈ C(l1 ): (C,P)|=(e0)l0, C(l2) ⊆ P(x) and C(l0 ) ⊆ C(l2)  The solution for function parameter (x) must contain that of the invocation argument (e2);  The solution of the function invocation must contain that of the function body

 Local variables (nested scopes)

 (C,P) |= (let x = (e1)l1 in (e2)l2)l3 iff (C,P) |= (e1)l1, (C,P) |= (e2)l2, C(l1) ⊆ P(x) and C(l2 ) ⊆ C(l3)  The solution for the local variable (x) must contain that of its defined value  The solution of the outer scope must contain that of the inner scope

 Conditionals

 (C,P) |= (if (e0)l0 then (e1)l1 else (e2)l2)l3 iff (C,P) |= (e0)l0, (C,P) |= (e1)l1, (C,P) |= (e2)l2, and C(l2) ⊆ C(l3) and C(l2 ) ⊆ C(l3)  The solution of the outer scope must contain that of the inner scopes (both branches)

Example Code and Control-flow Analysis Solution  Example code ((fun f x => x) (fun g y => y))  Labels: 1: x; 2: (fun f x => x) 3: y; 4: (fun g y => y) 5: ((fun f x => x) (fun g y => y))  Example CFA solution (guesses of the (C,P) mappings). Are the valid? (C,P) (C’,P’) y ∅^ ∅ f {fun f x => x} {fun f x => x} g {fun g y => y} {fun g y => y} { fun g y => y} {fun g y => y} {fun g y => y} ∅ {fun f x => x} {fun g y => y} x ∅ 5 {fun g y => y} 4 {fun g y => y} 3 ∅ (^2) {fun f x => x} 1 {fun g y => y} (C,P) |= ((fun f x => x) (fun g y => y)) (C’,P’) |= ((fun f x => x) (fun g y => y))

Correctness of Specification

 If there is a possible evaluation of the program such that the function at a call point evaluates to some function definition  then this definition has to be in the set of possible definitions computed by the analysis.  Existence of solutions  Every expression accepts a least CFA solution

Constraint based Analysis

 Syntax-directed analysis  Reformulate the analysis specification

 Construct a finite set of constraints based on structural

induction

 Compute the least solution of the set of constraints  Each constraint has the form

(sol1 ⊆ sol2) or ({t} ⊆ sol) or ({t} ⊆ sol1 => sol2 ⊆ sol3)

 where

 Each sol is either C(l) or P(x)

 l is label, x is a variable

 Each t is either (fn x => e0) or (fun f x => e0)

Example: Constraint Construction Cond[((fun f x => (x) 1 ) 2 (fun g y => (y) 3 ) 4 ) 5 ] = { {fun f x => (x)} ⊆ C(2), {fun f x => (x)} ⊆ P(f), P(x) ⊆ C(1), {fun g y => (y)}⊆C(4), {fun g y => (y)}⊆P(g), P(y) ⊆ C(3), {fun f x => (x)} ⊆ C(2) => C(4) ⊆ P(x), {fun f x => (x)} ⊆ C(2) => C(1) ⊆ C(5), {fun g y => (y)} ⊆ C(2) => C(4) ⊆ P(y), {fun g y => (y)} ⊆ C(2) => C(3) ⊆ C(5) }

Solving the constraints

 Input: a set of constraints for the entire program

 Output: the least solution (C,P) to the constraints

 Idea: equivalent to finding the least fixed point of a

monotone function defined by the constraints

 Straight-forward iterative algorithm has n^5 cost, where n is the size of the program (expression)  A more sophisticated algorithm takes n^3 complexity

 The graph-based algorithm

 Build a graph where

 Each node n corresponds to a unique C(l) or P(x) =>val(n)  Add an edge from node n1 to n2 if any change to val(n1) may require modifications to val(n2)

 Use a worklist to keep track of nodes to change

Constraint Solving Algorithm(2)

 Step 3 Iteration

 while worklist is not empty do q := Remove-first(W); for each cc in Edge[q] do case cc of p1 ⊆ p2: add(p2, Val[p1]); {t} ⊆ p => p1 ⊆ p2: if t ⊆ Val[p] then add(val(p1), p2); {fun g y => y} {fun f x => x} ∅ {fun g y => y} ∅ {fun g y => y} ∅ {fun f x => x} {fun g y => y} Propoage P(x) {fun g y => y} {fun f x => x} ∅ {fun g y => y} ∅ {fun g y => y} ∅ {fun f x => x} ∅ Val Iteration 0 Propogate C(2)… Propoage C(1) P(y) ∅^ ∅ P(f) {fun f x => x} {fun f x => x} P(g) {fun g y => y} {fun g y => y} ∅ ∅ {fun g y => y} ∅ {fun f x => x} ∅ P(x) {fun g y => y} C(5) {fun g y => y} C(4) {fun g y => y} C(3) ∅ C(2) {fun f x => x} C(1) {fun g y => y}

Summary

 Recording the solution of CFA analysis

 for each label l (or variable x) do

 C(l) = Val[C(l)] (P(x) = Val[P(x)] )  Correctness and Termination

 The worklist algorithm terminates and the result produced by the

algorithm is the least solution to C[[e]].

 Complexity: The algorithm takes at most O(n3) steps if the original

expression e has size n.