Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Interprocedural Optimisation Seminar Static Program Analysis Barbara D¨ orr Sources : ¨ Ubersetzerbau - Analyse und Transformation (H. Seidl, R. Wilhelm, S. Hack) Principles of Program Analysis (F. Nielson, H.R. Nielson, C. Hankin) 12. M¨ arz 2010 1 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Forms of Program Optimisation Program Optimisation Intraprocedural Interprocedural Optimisation : Optimisation optimise each function separately explicitly model optimise function calls function calls without explicit mod- elling e.g. ⇒ Interprocedural Opti- ◮ Inlining misation : ◮ Remove Last more demanding, but also Call more precise information 2 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Interprocedural vs. Intraprocedural disadvantage of intraprocedural optimisation : context-insensitive optimisation : cannot distinguish between different calls (information is combined from all call sites) → imprecise information interprocedural optimisation : context-sensitive optimisation : different calls reached with different contexts δ 1 and δ 2 → information obtained clearly related to δ 1 and δ 2 ⇒ more precise, but more costly 3 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Introduction Simple Interprocedural Optimisations Operational Semantic Functional Approach Related Approaches Summary 4 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Program Representation intraprocedural → program represented by a control flow graph : 0 y ← 1 1 y <- 1; Zero ( x > 1) NonZero ( x > 1) while (x>1){ y <- x*y; 5 2 x <- x-1; } y ← x ∗ y 3 x ← x − 1 4 5 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Program Representation interprocedural → program represented by a set of control flow graphs; f () 4 main A ← b main() { b <- 3; 5 0 f(); M[17] <- ret; Zero ( A ≤ 1) NonZero ( A > 1) b ← 3 } 6 9 f(){ 1 A <- b; b ← A − 1 if (A <=1) ret <- 1; f () else { 7 b <- A-1; 2 f(); ret ← 1 f () ret <- A*ret; M [17] < − ret } 8 } 3 ret ← A ∗ ret 10 6 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Edge Annotations ( x ... variable, e ... arithmetic expression) edge effects - intraprocedural : Test: NonZero ( e ) Zero ( e ) Assignment: x ← e Load: x ← M [ e ] M [ e 1 ] ← e 2 Store: Empty Statement: ; additional edge effect - interprocedural : Function Call: f () 7 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Introduction Simple Interprocedural Optimisations Operational Semantic Functional Approach Related Approaches Summary 8 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Inlining inlining : copy function body to calling point problems : ◮ function has to be statically known ◮ local variables of calling function must not be modified → rename local variables ◮ recursive functions → identified from call graph → ◮ inlining only for leave functions (without calls) ◮ inlining only for non-recursive functions 9 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Inlining Call Graph Call Graph : nodes ∼ functions edges ∼ between function f 1 and function f 2 , if f 1 calls f 2 main() { b <- 3; f(); M[17] <- ret; } f(){ A <- b; if (A <=1) ret <- 1; main f else { b <- A-1; f(); ret <- A*ret; } } 10 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Inlining Call Graph abs(){ b_1 <- b; b_2 <- -b; max(); } max abs max(){ if (b_1 < b_2) ret <- b_2; else ret <- b_1; } 11 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Inlining transformation PI : u u f () A f = 0; A ∈ Loc v ; copy of f v 12 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Inlining example abs(){ b_1 <- b; abs(){ b_2 <- -b; b_1 <- b; max(); b_2 <- -b; } if (b_1 < b_2) ret <- b2; max(){ else ret <- b_1; if (b_1 < b_2) ret <- b_2; } else ret <- b_1; } 13 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Remove Last Calls → no own stack frame needed; only replace local variables (unconditional jump to function body) ! only possible if local variables of calling function are not accessible any more transformation LC : A = 0; ( A ∈ Loc ) f (): u u f () v v f (): 14 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Remove Last Calls example f(){ f(){ if (b_2 <= 1) ret <- b_1; _f: if (b_2 <= 1) ret <- b_1; else { else { b_1 <- b_1*b_2; b_1 <- b_1*b_2; b_2 <- b_2 - 1; b_2 <- b_2 - 1; f(); goto _f; } } } } 15 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Introduction Simple Interprocedural Optimisations Operational Semantic Functional Approach Related Approaches Summary 16 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Operational Semantic intraprocedural ◮ computations are described by paths through the control flow graph ◮ computations transform the current program state ◮ program state : s = ( ρ, µ ) with ρ : Vars → int ... value of variables µ : N → int ... content of memory ◮ edge k = ( u , lab , v ) ... entry node u , exit node v , edge annotation label ◮ edge effect : transformation [ [ k ] ] on program states defined by the edge k [ [ k ] ] = [ [ lab ] ] 17 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Operational Semantic Edge Effects - intraprocedural � ; � ( ρ, µ ) = ( ρ, µ ) � NonZero ( e ) � ( ρ, µ ) = ( ρ, µ ) , if � e � ρ � = 0 � Zero ( e ) � ( ρ, µ ) = ( ρ, µ ) , if � e � ρ = 0 � � � x ← e � ( ρ, µ ) = ρ ⊕ { x �→ � e � ρ } , µ � � � x ← M [ e ] � ( ρ, µ ) = ρ ⊕ { x �→ µ ( � e � ρ ) } , µ � � � M [ e 1 ] ← e 2 � ( ρ, µ ) = ρ, µ ⊕ { � e 1 � ρ �→ � e 2 � ρ } 18 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Stack Representation Call Stack : f(){ A <- b; if (A <=1) ret <- 1; else { main() { b <- A-1; b <- 3; f(); f(); ret <- A*ret; M[17] <- ret; } } } 05 A �→ 1 05 A �→ 2 07 A �→ 2 08 A �→ 2 05 A �→ 3 07 A �→ 3 08 A �→ 3 08 A �→ 3 08 A �→ 3 01 02 02 02 02 02 10 A �→ 1 08 08 10 A �→ 2 A �→ 2 A �→ 2 08 A �→ 3 08 A �→ 3 08 A �→ 3 08 A �→ 3 10 A �→ 1 02 02 02 02 02 02 19 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Stack Representation call stack : ◮ describes called and not yet finished functions ◮ basis of operational semantic stack × globals × store config = = Glob → Z globals = N → Z store frame · frame ∗ stack = = point × locals frame locals = Loc → Z ! function body is a scope with own local variables 20 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Modeling of Function Call ◮ call k = ( u , f () , v ): ! ρ f = { x �→ 0 | x ∈ Loc } � � � � � � � � σ · ( u , ρ Loc ) , ρ Glob , µ ⊢ σ · · v , ρ Loc u f , ρ f , ρ Glob , µ � �� � config ◮ effect of function itself ◮ return from call: � � � � � � σ · ( v , ρ Loc ) · ⊢ σ · ( v , ρ Loc ) , ρ Glob , µ r f , , ρ Glob , µ σ ... stack ρ Glob ... global variables µ ... store ( u , ρ Loc ) ... frame ( point × locals ) 21 / 54
Introduction Simple Optimisations Operational Semantic Functional Approach Related Approaches Summary Path Effects π : (( u , ρ Loc ) , ρ Glob , µ ) � (( v , ρ ′ Loc ) , ρ ′ Glob , µ ′ ) path π defines a partial function � π � , that transforms (( u , ρ Loc ) , ρ Glob , µ ) into (( v , ρ ′ Loc ) , ρ ′ Glob , µ ′ ) ⇒ compute transformation inductive over the structure of the path: � k � ◦ � π � � π k � = for a normal edge k ( composition of edge effects ) 22 / 54
Recommend
More recommend