principles of program analysis a sampler of approaches
play

Principles of Program Analysis: A Sampler of Approaches - PowerPoint PPT Presentation

Principles of Program Analysis: A Sampler of Approaches Transparencies based on Chapter 1 of the book: Flemming Nielson, Hanne Riis Nielson and Chris Hankin: Principles of Program Analysis. Springer Verlag 2005. c Flemming Nielson &


  1. Principles of Program Analysis: A Sampler of Approaches Transparencies based on Chapter 1 of the book: Flemming Nielson, Hanne Riis Nielson and Chris Hankin: Principles of Program Analysis. Springer Verlag 2005. c � Flemming Nielson & Hanne Riis Nielson & Chris Hankin. PPA Chapter 1 1 � F.Nielson & H.Riis Nielson & C.Hankin (May 2005) c

  2. Compiler Optimisation The classical use of program analysis is to facilitate the construction of compilers generating “optimal” code. We begin by outlining the structure of optimising compilers. We then prepare the setting for a worked example where we “optimise” a naive implementation of Algol-like arrays in a C-like language by per- forming a series of analyses and transformations. PPA Chapter 1 2 � F.Nielson & H.Riis Nielson & C.Hankin (May 2005) c

  3. The structure of a simple compiler static syntactic lexical code ✲ ✲ ✲ ✲ ✲ semantic analysis generation analysis checking ✻ ✻ ✻ ✻ ✻ symbol table string of string of syntax machine & code characters tokens tree syntax tree Characteristics of a simple compiler: • many phases – one or more passes • the compiler is fast – but the code is not very efficient PPA Chapter 1 3 � F.Nielson & H.Riis Nielson & C.Hankin (May 2005) c

  4. The structure of an optimising compiler high-level or intermediate-level static representation syntactic lexical ✲ ✲ ✲ semantic analysis analysis checking machine machine code independent dependent ✲ ✲ ✲ ✲ generation optimisations optimisations low-level representation Characteristics of the optimising compiler: • high-level optimisations: easy to adapt to new architectures • low-level optimisations: less likely to port to new architectures PPA Chapter 1 4 � F.Nielson & H.Riis Nielson & C.Hankin (May 2005) c

  5. The structure of the optimisation phase program ✲ ✲ ✲ ✲ front end back end optimiser � ❅ � ❅ � ❅ � ❅ � ❅ � ❅ � ❅ � ❅ � ❅ � ❅ � ❅ � ❅ program � ❅ transfor- � ✲ ✲ ✲ ❅ analysis mation Avoid redundant computations: reuse available results, move loop in- variant computations out of loops, ... Avoid superfluous computations: results known not to be needed, results known already at compile time, ... PPA Chapter 1 5 � F.Nielson & H.Riis Nielson & C.Hankin (May 2005) c

  6. Example: Array Optimisation program with program with ✲ Algol-like arrays C-like arrays sequence of analysis and transformation steps optimised . . . ✲ ✲ program with C-like arrays PPA Chapter 1 6 � F.Nielson & H.Riis Nielson & C.Hankin (May 2005) c

  7. Array representation: Algol vs. C A: array [0:n, 0:m] of integer Accessing the (i,j)’th element of A : Base(A) ❆ 0 1 · · · m ❆ ❆ in Algol: 0 ❆ ❯ 1 A[i,j] ✛ i . . . n in C: ✻ j Cont(Base(A) + i * (m+1) + j) PPA Chapter 1 7 � F.Nielson & H.Riis Nielson & C.Hankin (May 2005) c

  8. An example program and its naive realisation Algol-like arrays: C-like arrays: i := 0; i := 0; while i <= n do while i <= n do j := 0; j := 0; while j <= m do while j <= m do A[i,j] := B[i,j] + C[i,j]; temp := Base(A) + i * (m+1) + j; j := j+1 Cont(temp) := Cont(Base(B) + i * (m+1) + j) od; + Cont(Base(C) + i * (m+1) + j); i := i+1 j := j+1 od od; i := i+1 od PPA Chapter 1 8 c � F.Nielson & H.Riis Nielson & C.Hankin (May 2005)

  9. Available Expressions analysis and Common Subexpression Elimination i := 0; while i <= n do first computation j := 0; t1 := i * (m+1) + j; while j <= m do ❄ temp := Base(A) + t1 ; temp := Base(A) + i*(m+1) + j; Cont(temp) := Cont(Base(B)+t1) Cont(temp) := Cont(Base(B) + i*(m+1) + j) + Cont(Base(C)+t1) ; ✁ ✕ ✁ + Cont(Base(C) + i*(m+1) + j); ✁ � ✒ ✁ � j := j+1 ✁ � ✁ � od; re-computations i := i+1 od PPA Chapter 1 9 � F.Nielson & H.Riis Nielson & C.Hankin (May 2005) c

  10. Detection of Loop Invariants and Invariant Code Motion i := 0; while i <= n do loop invariant t2 := i * (m+1); j := 0; ✟ ✟ while j <= m do ✟ while j <= m do ✟ ✟ ✙ t1 := t2 + j; t1 := i * (m+1) + j; temp := ... temp := Base(A) + t1; Cont(temp) := ... Cont(temp) := Cont(Base(B) + t1) j := ... + Cont(Base(C) + t1); od j := j+1 od; i := i+1 od PPA Chapter 1 10 � F.Nielson & H.Riis Nielson & C.Hankin (May 2005) c

  11. Detection of Induction Variables and Reduction of Strength i := 0; ❳ ② ❳ ❳ i := 0; ❳ ❳ ❳ while i <= n do ❳ ❳ ❳ t3 := 0; induction variable ✘ ✘ j := 0; ✘ ✘ ✘ ✘ while i <= n do ✘ ✾ ✁ t2 := i * (m+1); ✁ j := 0 ; ✁ while j <= m do ✁ t2 := t3 ; ✁ t1 := t2 + j; ✁ while j <= m do ... od ✁ temp := Base(A) + t1; ✁ i := i + 1; ✁ Cont(temp) := Cont(Base(B) + t1) ✁ t3 := t3 + (m+1) ✁ + Cont(Base(C) + t1); ✁ od j := j+1 ✁ ✁ od; ✁ ☛ ✁ i := i+1 od PPA Chapter 1 11 � F.Nielson & H.Riis Nielson & C.Hankin (May 2005) c

  12. Equivalent Expressions analysis and Copy Propagation i := 0; t3 := 0 ; while j <= m do while i <= n do t2 = t3 j := 0; ✘✘✘✘✘ ✿ t1 := t3 + j; � t2 := t3; � temp := ...; � while j <= m do � � ✠ t1 := t2 + j; Cont(temp) := ...; temp := Base(A) + t1; j := ... Cont(temp) := Cont(Base(B) + t1) + Cont(Base(C) + t1); od j := j+1 od; i := i+1 ; t3 := t3 + (m+1) od PPA Chapter 1 12 � F.Nielson & H.Riis Nielson & C.Hankin (May 2005) c

  13. Live Variables analysis and Dead Code Elimination i := 0; i := 0; t3 := 0 ; dead variable t3 := 0 ; while i <= n do ✘ ✘ ✘ ✘ ✘ while i <= n do ✘ j := 0; ✘ ✘ ✘ ✘ ✾ ✘ j := 0; t2 := t3; while j <= m do while j <= m do t1 := t3 + j; t1 := t3 + j; temp := Base(A) + t1; temp := Base(A) + t1; Cont(temp) := Cont(Base(B) + t1) Cont(temp) := Cont(Base(B) + t1) + Cont(Base(C) + t1); + Cont(Base(C) + t1); j := j+1 j := j+1 od; od; i := i+1 ; i := i+1 ; t3 := t3 + (m+1) t3 := t3 + (m+1) od od PPA Chapter 1 13 � F.Nielson & H.Riis Nielson & C.Hankin (May 2005) c

  14. Summary of analyses and transformations Analysis Transformation Available expressions analysis Common subexpression elimination Detection of loop invariants Invariant code motion Detection of induction variables Strength reduction Equivalent expression analysis Copy propagation Live variables analysis Dead code elimination PPA Chapter 1 14 � F.Nielson & H.Riis Nielson & C.Hankin (May 2005) c

  15. The Essence of Program Analysis Program analysis offers techniques for predicting we cannot expect statically at compile-time exact answers! safe & efficient approximations to the set of configurations or behaviours arising dynamically at run-time Safe: faithful to the semantics Efficient: implementation with – good time performance and – low space consumption PPA Section 1.2 15 c � F.Nielson & H.Riis Nielson & C.Hankin (May 2005)

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend