constant propagation and interval analysis
play

Constant Propagation and Interval Analysis Daniela Moldovan 29. May - PowerPoint PPT Presentation

Seminar Static Program Analysis Constant Propagation and Interval Analysis Daniela Moldovan 29. May 2010 Daniela Moldovan Constant Propagation and Interval Analysis 1 / 70 Outline Motivation 1 Constant Propagation 2 Interval Analysis 3


  1. Seminar Static Program Analysis Constant Propagation and Interval Analysis Daniela Moldovan 29. May 2010 Daniela Moldovan Constant Propagation and Interval Analysis 1 / 70

  2. Outline Motivation 1 Constant Propagation 2 Interval Analysis 3 Daniela Moldovan Constant Propagation and Interval Analysis 2 / 70

  3. Motivation Compiler translation from a program language to machine understandable code must be correct Program Analysis use static analysis (at compile time), considering global properties of programs recognize optimization opportunities transform the produced code using optimization techniques, without changing the semantics of the program time and space efficiency Daniela Moldovan Constant Propagation and Interval Analysis 3 / 70

  4. A Programming Language x Variable: e Arithmetical expression: x ← e Assignment: x ← M [ e ] Load: M [ e 1 ] ← e 2 Store: if (e) s 1 else s 2 If statement: goto L Jump: Daniela Moldovan Constant Propagation and Interval Analysis 4 / 70

  5. Control Flow Graph Programs are represented by control flow graphs (CFG), where Nodes: program points Edges labeled with program statements ◮ NonZero(e) or Zero(e) 0 x ← 7 ◮ x ← e 1 ◮ x ← M [ e ] NonZero ( x > 0 ) ◮ M [ e 1 ] ← e 2 2 Zero ( x > 0 ) ◮ ; M [ A ] ← B 3 ; 4 Daniela Moldovan Constant Propagation and Interval Analysis 5 / 70

  6. Data Flow Analysis Data flow analysis winning information about the possible set of values calculated at various points in a program the information gathered is used by compilers when optimizing a program How to perform it set up dataflow equations for each node of the CFG solve the equations by applying some iteration algorithm until the system stabilizes Daniela Moldovan Constant Propagation and Interval Analysis 6 / 70

  7. Static Analysis theory of the static program analysis goes back to Gary Kildall (1972) and to Patrick Cousot(1978) Kildall: lattice-based theory of the control flow analysis Cousot: abstract interpretation based on program semantics Daniela Moldovan Constant Propagation and Interval Analysis 7 / 70

  8. Definitions computations: along paths program state at a program point: s = ( ρ, µ ) ρ : Vars → int µ : N → int each edge k = ( u , lab , v ) , u-start node, v-end node, lab represents a program statement define transformation on program states as the edge effect: � k � = � lab � Daniela Moldovan Constant Propagation and Interval Analysis 8 / 70

  9. Definition of � lab � � ; � ( ρ, µ ) = ( ρ, µ ) � 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 � ρ } ) Example � x + y � { x �→ 7 , y �→ − 1 } = 6 � ¬ ( x = 4 ) � { x �→ 5 } = ¬ 0 = 1 Daniela Moldovan Constant Propagation and Interval Analysis 9 / 70

  10. Definitions � if y ≡ x d ( ρ ⊕ { x �→ d } )( y ) = ρ ( y ) otherwise Example � x ← x + 1 � ( { x �→ 5 } , µ ) = ( ρ, µ ) , where ρ = { x �→ 5 } ⊕ { x �→ � x + 1 � { x �→ 5 }} = { x �→ 5 } ⊕ { x �→ 6 } = { x �→ 6 } A computation π of the program is a path in CFG, from a start node to an end node. π = k 1 . . . k n , where k i = ( u i , lab i , u i + 1 ) is an edge, u 1 = u , u n = v The state transformation � π � is the composition: � π � = � k n � ◦ . . . ◦ � k 1 � Daniela Moldovan Constant Propagation and Interval Analysis 10 / 70

  11. Definitions A set D on a relation ⊑ ⊆ D × D is a partial ordering if the following properties hold: a ⊑ a (reflexive) a ⊑ b ∧ b ⊑ a ⇒ a = b (anti-symmetric) a ⊑ b ∧ b ⊑ c ⇒ a ⊑ c (transitiv) An element d ∈ D is an upper bound of a subset X ⊆ D if: x ⊑ d ∀ x ∈ X An element d is the least upper bound, when it holds: d is an upper bound d ⊑ y for every upper bound y of X A partial ordering is a complete lattice, if every subset X ⊆ D has a least upper bound, � X. Daniela Moldovan Constant Propagation and Interval Analysis 11 / 70

  12. Complete Lattice - Example Z ⊤ ⊥ = Z ∪ {⊥ , ⊤} Daniela Moldovan Constant Propagation and Interval Analysis 12 / 70

  13. Complete Lattice In a complete lattice D , each subset X ⊆ D has a least upper bound � X a greatest lower bound � X. Daniela Moldovan Constant Propagation and Interval Analysis 13 / 70

  14. Constant Propagation - the Problem CP : Which variables have which constant value at a program point l , every time program execution reaches l ? Constant propagation process of substituting values of known constants in expressions at compile time Constant Folding evaluate expressions with constant operands at compile time improves run time Daniela Moldovan Constant Propagation and Interval Analysis 14 / 70

  15. Example 0 0 x ← 7 ; ; x ← 7 if (x > 0) 1 1 M [ A ] ← B ; NonZero ( x > 0 ) ; = ⇒ 2 2 Zero ( x > 0 ) M [ A ] ← B M [ A ] ← B 3 3 ; ; 4 4 Daniela Moldovan Constant Propagation and Interval Analysis 15 / 70

  16. Constant Propagation for every program point v find out: ◮ is v reachable from the start node? ◮ which values have the program variables when reaching v ? Daniela Moldovan Constant Propagation and Interval Analysis 16 / 70

  17. Constant Propagation - Framework Complete lattice partial order Z ⊤ = Z ∪ {⊤} with x ⊑ y ⇔ y = ⊤ or x = y ⊤ is the unknown value ⊤ . . . − 2 − 1 0 1 2 . . . Daniela Moldovan Constant Propagation and Interval Analysis 17 / 70

  18. Constant Propagation - Complete Lattice complete lattice for the abstract variable assignment D = ( Vars → Z ⊤ ) ⊥ = ( Vars → Z ⊤ ) ∪ {⊥} ⊥ marks a program point where there is no variable assignment yet or a program point that is unreachable. ordering relation on the set of abstract states: D 1 ⊑ D 2 ⇔ ⊥ = D 1 or D 1 x ⊑ D 2 x, ∀ x ∈ Vars D 1 knows the exact value for at least as many variables as D 2 . Daniela Moldovan Constant Propagation and Interval Analysis 18 / 70

  19. Contant Propagation - Complete Lattice For each X ⊆ D ( ⊥ / ∈ X): If X = ∅ , � X = ⊥ ∈ D ⇒ X has a least upper bound If X � = ∅ , we define the least upper bound � X = D, where � z if f x = z, ∀ f ∈ X D x = � { f x | f ∈ X } = ⊤ otherwise We construct for each edge k = ( u , lab , v ) an abstract edge effect � k � ♯ = � lab � ♯ : D → D , which simulates the concrete computation. D = ⊥ � lab � ♯ ⊥ = ⊥ for all edge labels lab Daniela Moldovan Constant Propagation and Interval Analysis 19 / 70

  20. Variable Assignment D � = ⊥ - variable assignment set for the construction of the edge effects, we need an abstract evaluation function: � ⊤ if a = ⊤ or b = ⊤ a � ♯ b = a � b otherwise Daniela Moldovan Constant Propagation and Interval Analysis 20 / 70

  21. Abstract Expression Evaluation � e � ♯ The abstract expression evaluation defined as: � e � ♯ : ( Vars → Z ⊤ ) → Z ⊤ � c � ♯ D = c D = � ♯ � e � ♯ D �� e � ♯ for unary operators � D = � e 1 � ♯ � ♯ � e 2 � ♯ D � e 1 � e 2 � ♯ for binary operators � Example For the variable assignment set: D = { x �→ 2 , y �→ ⊤} � x + 7 � ♯ D = � x � ♯ + ♯ � 7 � ♯ D = 2 + ♯ 7 = 9 � x − y � ♯ D = � x � ♯ − ♯ � y � ♯ D = 2 − ♯ ⊤ = ⊤ Daniela Moldovan Constant Propagation and Interval Analysis 21 / 70

  22. Abstract Edge Effect � k � ♯ Abstract edge effect: � k � ♯ = � lab � ♯ � lab � ♯ ⊥ D = ⊥ : = ⊥ D � = ⊥ : � ; � ♯ D = D if 0 = � e � ♯ D � ⊥ � NonZero ( e ) � ♯ D = D otherwise � if 0 �⊑ � e � ♯ D ⊥ � Zero ( e ) � ♯ D = if 0 ⊑ � e � ♯ D D � x ← e � ♯ D D ⊕ { x �→ � e � ♯ D } = � x ← M [ e ] � ♯ D = D ⊕ { x �→ ⊤} � M [ e 1 ] ← e 2 � ♯ D = D For the start node: D ⊤ = { x �→ ⊤ | x ∈ Vars } ⊕ alters the function value for a given argument. Daniela Moldovan Constant Propagation and Interval Analysis 22 / 70

  23. Abstract Path Effect � π � ♯ For a path π = k 1 ◦ . . . ◦ k n : � π � ♯ = � k r � ♯ ◦ . . . ◦ � k 1 � ♯ : D → D Daniela Moldovan Constant Propagation and Interval Analysis 23 / 70

  24. Example - Fixpoint Iteration The smallest solution: 0 x ← 7 0 { x �→ ⊤} 1 NonZero ( x > 0 ) 1 { x �→ 7 } 2 { x �→ 7 } 2 Zero ( x > 0 ) 3 { x �→ 7 } M [ A ] ← B ⊥ ⊔ { x �→ 7 } = { x �→ 7 } 4 3 ; 4 Daniela Moldovan Constant Propagation and Interval Analysis 24 / 70

  25. Correctness Proof use Abstract Interpretation (Cousot & Cousot 1977 ) characterize concrete values with help of abstract values choose abstract values from D relation between concrete and abstract values given by the representation relation ∆ : x ∆ a 1 ∧ a 1 ⊑ a 2 = ⇒ x ∆ a 2 define for the representation relation the concretisation function γ : γ a = { x | x ∆ a } a 1 ⊑ a 2 = ⇒ γ ( a 1 ) ⊆ γ ( a 2 ) Daniela Moldovan Constant Propagation and Interval Analysis 25 / 70

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