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

constant propagation and interval analysis
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Seminar Static Program Analysis

Constant Propagation and Interval Analysis

Daniela Moldovan

  • 29. May 2010

Daniela Moldovan Constant Propagation and Interval Analysis 1 / 70

slide-2
SLIDE 2

Outline

1

Motivation

2

Constant Propagation

3

Interval Analysis

Daniela Moldovan Constant Propagation and Interval Analysis 2 / 70

slide-3
SLIDE 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

  • f 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

slide-4
SLIDE 4

A Programming Language

Variable: Arithmetical expression: Assignment: Load: Store: If statement: Jump: x e x ← e x ← M[e] M[e1] ← e2 if (e) s1 else s2 goto L

Daniela Moldovan Constant Propagation and Interval Analysis 4 / 70

slide-5
SLIDE 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) ◮ x ← e ◮ x ← M[e] ◮ M[e1] ← e2 ◮ ;

1 2 3 4

x ← 7 NonZero(x > 0) Zero(x > 0) M[A] ← B ;

Daniela Moldovan Constant Propagation and Interval Analysis 5 / 70

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 9

Definition of lab

; (ρ, µ) = (ρ, µ) NonZero(e) (ρ, µ) = (ρ, µ), if eρ = 0 Zero(e) (ρ, µ) = (ρ, µ), if e = 0 x ← e (ρ, µ) = (ρ ⊕ {x → e ρ}, µ) x ← M[e] (ρ, µ) = (ρ ⊕ {x → µ(e ρ)}, µ) M[e1] ← e2 (ρ, µ) = (ρ, µ ⊕ {e1ρ → e2 ρ})

Example

x + y{x → 7, y → −1} = 6 ¬(x = 4){x → 5} = ¬0 = 1

Daniela Moldovan Constant Propagation and Interval Analysis 9 / 70

slide-10
SLIDE 10

Definitions

(ρ ⊕ {x → d})(y) =

  • d

if y ≡ x ρ(y)

  • therwise

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. π = k1 . . . kn, where ki = (ui, labi, ui+1) is an edge, u1 = u, un = v The state transformation π is the composition: π = kn ◦ . . . ◦ k1

Daniela Moldovan Constant Propagation and Interval Analysis 10 / 70

slide-11
SLIDE 11

Definitions

A set D on a relation ⊑ ⊆ D × D is a partial ordering if the following properties hold: a ⊑ a a ⊑ b ∧ b ⊑ a ⇒ a = b a ⊑ b ∧ b ⊑ c ⇒ a ⊑ c (reflexive) (anti-symmetric) (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

slide-12
SLIDE 12

Complete Lattice - Example

Z⊤

⊥ = Z ∪ {⊥, ⊤}

Daniela Moldovan Constant Propagation and Interval Analysis 12 / 70

slide-13
SLIDE 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

slide-14
SLIDE 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

slide-15
SLIDE 15

Example

x ← 7; if (x > 0) M[A] ← B;

1 2 3 4

x ← 7 NonZero(x > 0) Zero(x > 0) M[A] ← B ;

= ⇒

1 2 3 4

; ; M[A] ← B ;

Daniela Moldovan Constant Propagation and Interval Analysis 15 / 70

slide-16
SLIDE 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

slide-17
SLIDE 17

Constant Propagation - Framework

Complete lattice partial order Z⊤ = Z ∪ {⊤} with x ⊑ y ⇔ y = ⊤ or x = y ⊤ is the unknown value

⊤ . . . −2 −1 1 2 . . .

Daniela Moldovan Constant Propagation and Interval Analysis 17 / 70

slide-18
SLIDE 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.

  • rdering relation on the set of abstract states:

D1 ⊑ D2 ⇔ ⊥ = D1 or D1 x ⊑ D2 x, ∀ x ∈ Vars D1 knows the exact value for at least as many variables as D2.

Daniela Moldovan Constant Propagation and Interval Analysis 18 / 70

slide-19
SLIDE 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 D x = {f x | f ∈ X} =

  • z

if f x = z, ∀ f ∈ X ⊤

  • therwise

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

slide-20
SLIDE 20

Variable Assignment

D = ⊥ - variable assignment set for the construction of the edge effects, we need an abstract evaluation function: a ♯ b =

if a = ⊤ or b = ⊤ a b

  • therwise

Daniela Moldovan Constant Propagation and Interval Analysis 20 / 70

slide-21
SLIDE 21

Abstract Expression Evaluation e♯

The abstract expression evaluation defined as: e♯ : (Vars → Z⊤) → Z⊤ c♯ D = c e♯ D = ♯ e♯D for unary operators e1 e2♯ D = e1♯ ♯ e2♯ D 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

slide-22
SLIDE 22

Abstract Edge Effect k♯

Abstract edge effect: k♯ = lab♯ D = ⊥ : lab♯ ⊥ = ⊥ D = ⊥: ; ♯ D = D NonZero(e)♯ D =

if 0 = e♯ D D

  • therwise

Zero(e)♯ D =

if 0 ⊑ e♯ D D if 0 ⊑ e♯ D x ← e♯ D = D ⊕ {x → e♯ D} x ← M[e]♯ D = D ⊕ {x → ⊤} M[e1] ← e2♯ 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

slide-23
SLIDE 23

Abstract Path Effect π♯

For a path π = k1 ◦ . . . ◦ kn: π♯ = kr♯ ◦ . . . ◦ k1♯ : D → D

Daniela Moldovan Constant Propagation and Interval Analysis 23 / 70

slide-24
SLIDE 24

Example - Fixpoint Iteration

The smallest solution:

1 2 3 4

x ← 7 NonZero(x > 0) Zero(x > 0) M[A] ← B ;

{x → ⊤} 1 {x → 7} 2 {x → 7} 3 {x → 7} 4 ⊥ ⊔ {x → 7}= {x → 7}

Daniela Moldovan Constant Propagation and Interval Analysis 24 / 70

slide-25
SLIDE 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∆a1 ∧ a1 ⊑ a2 = ⇒ x∆a2 define for the representation relation the concretisation function γ: γa = {x | x∆a} a1 ⊑ a2 = ⇒ γ(a1) ⊆ γ(a2)

Daniela Moldovan Constant Propagation and Interval Analysis 25 / 70

slide-26
SLIDE 26

Correctness Proof - Representation Relation ∆

For Constant Propagation we construct the representation relation ∆ stepwise. For values of program variables: z ∆ a ⇐ ⇒ z = a ∨ a = ⊤ γ a =

  • {a}

if a ⊏ ⊤ Z if a = ⊤ Representation relation between concrete and abstract variable assignments: ∆ ⊆ (Vars → Z) × (Vars → Z⊤)⊥ ρ ∆ D ⇐ ⇒ D = ⊥ ∧ ρ x ⊑ D x (x ∈ Vars) γ D = {ρ | ∀ x: (ρ x) ∆ (D x)}

Example

{x → 1, y → -7 } ∆ {x → ⊤, y → -7}

Daniela Moldovan Constant Propagation and Interval Analysis 26 / 70

slide-27
SLIDE 27

Correctness Proof

CP analysis ignores the values from the memory program state (ρ, µ) characterized by an abstract variable assignment, which describes ρ. (ρ, µ) ∆ D ⇔ ρ ∆ D γ D =

if D = ⊥ {(ρ, µ) | ρ ∈ γ D}

  • therwise

Daniela Moldovan Constant Propagation and Interval Analysis 27 / 70

slide-28
SLIDE 28

Correctness Proof - ∆ along paths

show, that the representation relation remains preserved along all the paths of the control flow graph: (K): s∆D and πs is defined ⇒ (πs) ∆ (π♯D)

s s1 D D1

π π♯ ∆ ∆

(K)⇒ πs ∈ γ(π♯), when s ∈ γ(D)

Daniela Moldovan Constant Propagation and Interval Analysis 28 / 70

slide-29
SLIDE 29

Correctness Proof - ∆ along paths

prove for a single edge: ks is defined ⇒ (ks) ∆ (k♯D Idea: for every expression e show: (eρ) ∆ (e♯ D), if ρ ∆ D to prove this, show for every operator : (x y) ∆ (x♯ ♯ y♯), if x ∆ x♯ ∧ y ∆ y♯ (structural induction over e)

Daniela Moldovan Constant Propagation and Interval Analysis 29 / 70

slide-30
SLIDE 30

Correctness Proof - ∆ along paths

edge k = (u, lab, v) consider s = (ρ, µ) ∆ D. (D = ⊥) x ← e x ← e s = (ρ, µ) with ρ1 = ρ ⊕ {x → eρ} x ← e♯ D = D1 with D1 = D ⊕ {x → e♯D} x ← M[e] x ← M[e] s = (ρ1, µ) with ρ1 = ρ ⊕ {x → µ(eρ)} x ← M[e]♯ D = D1 with D1 = D ⊕ {x → ⊤}

Daniela Moldovan Constant Propagation and Interval Analysis 30 / 70

slide-31
SLIDE 31

Correctness Proof - ∆ along paths

M[e1] ← e2 for Store, concrete or abstract edge effects do not modify the variable assignment. Zero(e) Zero(e)s is defined. It follows: (eρ) ∆ (e♯D) = 0. It implies: Zero(e)♯D = D = ⊥ NonZero(e) Zero(e)s is defined. It follows: (eρ) ∆ (e♯D) = 0. It implies: Zero(e)♯D = 0 .Zero(e)♯D = D

Daniela Moldovan Constant Propagation and Interval Analysis 31 / 70

slide-32
SLIDE 32

Meet Over All Paths

MOP-Solution for constant propagation is the least upper bound solution. D∗[v] = {π♯ D⊤ | π : start →∗ v}, where D⊤ x = ⊤ ∀x ∈ Vars (π s) ∆ D∗[v] An approximation for the MOP solution can be found by solving the equation system.

Daniela Moldovan Constant Propagation and Interval Analysis 32 / 70

slide-33
SLIDE 33

MOP

1 2 3 4 5 6 7

x ← 10 y ← 1 NonZero(x > 1) Zero(x > 1) y ← x ∗ y x ← x − 1 M[R] ← y

1 2 3 x y x y x y ⊤ ⊤ ⊤ ⊤ ⊤ ⊤ 1 10 ⊤ 10 ⊤ 10 ⊤ 2 10 1 ⊤ ⊤ ⊤ ⊤ 3 10 1 ⊤ ⊤ ⊤ ⊤ 4 10 10 ⊤ ⊤ ⊤ ⊤ 5 9 10 ⊤ ⊤ ⊤ ⊤ 6 ⊥ ⊥ ⊤ ⊤ ⊤ ⊤ 7 ⊥ ⊥ ⊤ ⊤ ⊤ ⊤

Daniela Moldovan Constant Propagation and Interval Analysis 33 / 70

slide-34
SLIDE 34

Example - Nondistributivity

Edge effects for constant propagation are not all distributiv Assignment x ← x + y; Two assignment sets: D1 = {x → 2, y → 3} and D2 = {x → 3, y → 2}

x ← x + y♯D1 ⊔ x ← x + y♯D2 = {x → 5, y → 3} ⊔ {x → 5, y → 2} = {x → 5, y → ⊤} x ← x + y♯(D1 ⊔ D2) = x ← x + y♯{x → ⊤, y → ⊤} = {x → ⊤, y → ⊤}

x ← x + y♯D1 ⊔ x ← x + y♯D2 = x ← x + y♯(D1 ⊔ D2)

Daniela Moldovan Constant Propagation and Interval Analysis 34 / 70

slide-35
SLIDE 35

Constant Propagation - Nondistributivity

Distributivity does not hold. Constant Propagation can determine concrete values just for some variables. Fixpoint iteration for finding the smallest solution of the equation system terminates, but returns just an overapproximation of the MOP solution: (D∗[v] ⊑ D[v]) for every program point v safe approximation

Daniela Moldovan Constant Propagation and Interval Analysis 35 / 70

slide-36
SLIDE 36

Transformation Constant Folding (CF)

dead code elimination remove program points which are for sure unreachable

Daniela Moldovan Constant Propagation and Interval Analysis 36 / 70

slide-37
SLIDE 37

Transformation Constant Folding (CF)

remove conditional edges, whose edge effects return ⊥

Daniela Moldovan Constant Propagation and Interval Analysis 37 / 70

slide-38
SLIDE 38

Transformation Constant Folding (CF)

reduce conditional edges by edges labeled with the empty statement, when the value of the edge effect is definitely known.

Daniela Moldovan Constant Propagation and Interval Analysis 38 / 70

slide-39
SLIDE 39

Transformation Constant Folding (CF)

use information D for evaluation of constant expressions at compile time. For assignments: e′ =

  • c

ife♯ D = c = ⊤ e ife♯ D = ⊤

Daniela Moldovan Constant Propagation and Interval Analysis 39 / 70

slide-40
SLIDE 40

Transformation Constant Folding (CF) - Extension

Partial Evaluation Constant Folding considers the maximal expression of a statement Idea: change it, so it can also compute subexpressions x + (3 · y)

{x→⊤,y→5}

= ⇒ x + 15 y + (x + 3)

{x→⊤,y→5}

= ⇒ 5 · (x + 3)

Daniela Moldovan Constant Propagation and Interval Analysis 40 / 70

slide-41
SLIDE 41

Example

Consider if (x = 7) y ← x + 3; Even if the value of x is unknown before the if statement, in Then part x has value 7.

Daniela Moldovan Constant Propagation and Interval Analysis 41 / 70

slide-42
SLIDE 42

Transformation Constant Folding (CF) - Extension

Use statements like: NonZero(x = e)♯ D =

if x = e♯ D = 0 D1

  • therwise

where D1 = D ⊕ {x → (D x ⊓ e♯D)}

Daniela Moldovan Constant Propagation and Interval Analysis 42 / 70

slide-43
SLIDE 43

Interval Analysis

The set of possible values of a variable at a program point is unknown in the most cases. Sometimes it is sufficient to just know an interval, where the value

  • f that variable is for sure.

Interval analysis extends constant propagation, by exchanging Z⊤ for the variables with a co-domain for intervals. The set of all intervals: I = {[l, u] | l ∈ Z ∪ {−∞}, u ∈ Z ∪ {+∞}, l ≤ u} Every interval represents a non-empty set of integers.

Daniela Moldovan Constant Propagation and Interval Analysis 43 / 70

slide-44
SLIDE 44

Interval Analysis

Define ordering relation ⊑ between intervals: [l1, u1]⊑ [l2, u2] ⇔ l2 ≤ l1 ∧ u1 ≤ u2

Daniela Moldovan Constant Propagation and Interval Analysis 44 / 70

slide-45
SLIDE 45

Interval Analysis

The least upper bound and the greatest lower bound of two intervals are given by: [l1, u1] ⊔ [l2, u2] = [min{l1, l2}, max{u1, u2}] [l1, u1] ⊓ [l2, u2] = [max{l1, l2}, min{u1, u2}], when max{l1, l2} ≤ min{u1, u2} holds.

Daniela Moldovan Constant Propagation and Interval Analysis 45 / 70

slide-46
SLIDE 46

Interval Analysis

I on ⊑ is a partial ordered set, but not a complete lattice (the smallest element ⊥ is missing, ∅ / ∈ I) ⇒ ∃ least upper bound for non-empty sets of intervals in I there are ascending chains, that never stabilize: [0, 0] ⊑ [0, 1] ⊑ [−1, 1] ⊑ [−1, 2] ⊑ . . .

Daniela Moldovan Constant Propagation and Interval Analysis 46 / 70

slide-47
SLIDE 47

Interval Analysis - Representation relation

Representation relation between concrete values and intervals: z ∆ [l, u] ⇔ l ≤ z ≤ u Concretisation γ: γ[l, u] = {z ∈ Z | l ≤ z ≤ u}

Example

γ[0, 7] = {0, . . . , 7} γ[0, +∞] = {0, 1, 2 . . .}

Daniela Moldovan Constant Propagation and Interval Analysis 47 / 70

slide-48
SLIDE 48

Computations with intervals

[l1, u1] +♯ [l2, u2] = [l1 + l2, u1 + u2]

◮ −∞ +

= −∞

◮ +∞ +

= +∞

−[l, u] = [−u, −l] [l1, u1] ·♯ [l2, u2] = [a, b]

◮ a = min{l1l2, l1u2, u1l2, u1u2} ◮ b = max{l1l2, l1u2, u1l2, u1u2}

[l1, u1] /♯ [l2, u2] = [a, b] If l2 ≤ 0 ≤ u2:

◮ a = min{l1/l2, l1/u2, u1/l2, u1/u2} ◮ b = max{l1/l2, l1/u2, u1/l2, u1/u2}

If 0 / ∈ [l2, u2]:

◮ [a,b] = [−∞, +∞]

Example

[ 0, 2] ·♯ [3, 4] = [ 0, 8] [−1, 2] ·♯ [3, 4] = [−4, 8] [−1, 2] ·♯ [−3, 4] = [−6, 8] [−1, 2] ·♯ [−4, −3] = [−8, 4]

Daniela Moldovan Constant Propagation and Interval Analysis 48 / 70

slide-49
SLIDE 49

Computations with intervals

[l1, u1] =♯ [l2, u2] =      [1, 1] if l1 = u1 = l2 = u2 [0, 0] if u1 < l2 ∨ u2 < l1 [0, 1]

  • therwise

[l1, u1] <♯ [l2, u2] =      [1, 1] if u1 < l2 [0, 0] if u2 ≤ l1 [0, 1]

  • therwise

Example

[13, 13] =♯ [13, 13] = [1, 1] [ 5, 13] =♯ [18, 19] = [0, 0] [ 5, 13] =♯ [ 5, 13] = [0, 1] [ 5, 13] <♯ [18, 19] = [1, 1] [ 5, 13] <♯ [ 5, 13] = [0, 0] [ 5, 18] <♯ [13, 19] = [0, 1]

Daniela Moldovan Constant Propagation and Interval Analysis 49 / 70

slide-50
SLIDE 50

Complete Lattice for Interval Analysis

Construct with the partial order (I, ⊑) a complete lattice for abstract variable assignments: DI = (Vars → I)⊥ = Vars → I ∪ {⊥} Representation relation between concrete and abstract variable assignments: ρ ∆ D ⇔ D = ⊥ ∧ ∀x ∈ Vars : (ρx) ∆ (Dx) Representation relation between concrete states and abstract variable assignments: (ρ, µ) ∆ D ⇔ Dρ ∆ D For the abstract expression evaluation: (e ρ) ∆ (e♯D), when ρ ∆ D

Daniela Moldovan Constant Propagation and Interval Analysis 50 / 70

slide-51
SLIDE 51

Interval Analysis - Edge effects

lab♯ ⊥ = ⊥ for D = ⊥: ; ♯ D = D x ← e♯ D = D ⊕ {x → e♯ D} x ← M[e]♯ D = D ⊕ {x → ⊤} M[e1] ← e2♯ D = D NonZero(e)♯ D =

if [0,0] = e♯ D D

  • therwise

Zero(e)♯ D =

if [0,0] ⊑ e♯ D D if [0,0] ⊑ e♯ D ⊤ = [−∞, +∞] At start program point : ⊤ = {x → [−∞, +∞] | x ∈ Vars}

Daniela Moldovan Constant Propagation and Interval Analysis 51 / 70

slide-52
SLIDE 52

Interval Analysis - Edge effects

e = x e1, ∈ {=, <, >}

NonZero(e)♯ D =

if [0,0] = e♯ D D1

  • therwise

D1 =      D ⊕ {x → (Dx) ⊓ (e1♯D)} e ≡ (x = e1) D ⊕ {x → (Dx) ⊓ [−∞, u − 1]} e ≡ (x < e1), e1♯D = [ , u] D ⊕ {x → (Dx) ⊓ [l + 1, +∞]} e ≡ (x ≥ e1), e1♯D = [l, ]

Zero(e)♯ D =

if [0,0] ⊑ e♯D D1

  • therwise

D1 =      D ⊕ {x → (Dx) ⊓ [−∞, u]} e ≡ (x > e1), e1♯D = [ , u] D ⊕ {x → (Dx) ⊓ [l + 1, +∞]} e ≡ (x < e1), e1♯D = [l, ] D e ≡ (x = e1)

Daniela Moldovan Constant Propagation and Interval Analysis 52 / 70

slide-53
SLIDE 53

Example

for (i ← 0; i < 42; i++) a[i] = i; Zwischencode: i ← 0; B: if(i < 42) { if(0 ≤ i ∧ i < 42) { A1 ← A + 1; M[A1] ← i; i ← i + 1; } else goto error; goto B; }

Daniela Moldovan Constant Propagation and Interval Analysis 53 / 70

slide-54
SLIDE 54

The smallest interval solution

After 42 iterations:

Daniela Moldovan Constant Propagation and Interval Analysis 54 / 70

slide-55
SLIDE 55

Interval analysis - Problem

Fixpoint iteration may never terminate I has ascending chains which will not stabilize Solutions: Widening, Narrowing !

Daniela Moldovan Constant Propagation and Interval Analysis 55 / 70

slide-56
SLIDE 56

Widening

Idea: speed-up fixpoint iteration, possibly with loss of precision accelerate the iteration, such that for every abstract value of a variable of the inequation sysyem changes just a finite number of times. For Interval Analysis: admite no arbitrary increasing of intervals, no transformation from finite to finite interval bounds. Example: [3, 17] ⊑ [3, +∞] ⊑ [+∞, +∞]

Daniela Moldovan Constant Propagation and Interval Analysis 56 / 70

slide-57
SLIDE 57

Widening - Formal

xi ⊒ fi(x1, . . . , xn) is an inequation system over the complete lattice D, where fi do not need to be monoton. xi ⊔ fi(x1, . . . , xn) is the accumulative equation system - does not need to terminate. A tupel x = (x1, . . . , xn) ∈ Dn is a solution of the inequation system if and only if it is a solution of the accumulative equation system. introduce widening-operator ⊔ ”: xi = xi ⊔ fi(x1, . . . , xn), i = 1, . . . , n it holds: v1 ⊔ v2 ⊒ v1 ⊔ v2

Daniela Moldovan Constant Propagation and Interval Analysis 57 / 70

slide-58
SLIDE 58

Interval Analysis - Widening

Apply widening operation for interval analysis, on the complete lattice DI = (Vars → I)⊥ define Widening operator ⊔ : ⊥ ⊔ D = D ⊔ ⊥ = D For D1 = ⊥ = D2 (D1 ⊔ D2)x = (D1x) ⊔ (D2x), [l1, u1] ⊔ [l − 2, u2] = [l, u] l =

  • l1 if l1 ≤ l2

−∞, otherwise u =

  • u1, if u1 ≥ u2

+∞, otherwise during the fixpoint iteration, the left operand is the old value. complexity: O(n · #Vars)

Example

[0, 2] ⊔ [1, 2] = [0, 2] [1, 2] ⊔ [0, 2] = [−∞, 2] [1, 5] ⊔ [3, 7] = [1, +∞]

Daniela Moldovan Constant Propagation and Interval Analysis 58 / 70

slide-59
SLIDE 59

Widening

is sufficient, when it is applied once for a cycle in the CFG use loop separator I it terminates

Daniela Moldovan Constant Propagation and Interval Analysis 59 / 70

slide-60
SLIDE 60

Example

For I1: For I2: Daniela Moldovan Constant Propagation and Interval Analysis 60 / 70

slide-61
SLIDE 61

Narrowing

improves unprecise solutions x is an arbitrary solution of the inequation system xi ⊒ fi(x1, . . . , xn), i = 1, . . . , n fi - monoton and F : Dn → Dn. monotony implies: x ⊒ Fx ⊒ F 2x ⊒ . . . ⊒ F k ¯ x ⊒ . . . xi ⊒ fi(x1, . . . , xn), i = 1, . . . , n every tupel F ix that is reached after some iterations is itself a solution of the equation system. the iteration stops when the obtained values are satisfying.

Daniela Moldovan Constant Propagation and Interval Analysis 61 / 70

slide-62
SLIDE 62

Example - Narrowing

We apply narrowing on the result of the widening

Daniela Moldovan Constant Propagation and Interval Analysis 62 / 70

slide-63
SLIDE 63

Narrowing

when the complete lattice has descending chains (d1 ⊒ d2 ⊒ . . .), which do not stabilize, the iteration does not terminate solution: accelerated Narrowing: it terminates there is a solution of the inequation system xi ⊒ fi(x1, . . . , xn), i = 1, . . . , n consider xi = xi ⊓ fi(x1, . . . , xn), i = 1, . . . , n restrict the intervals

Daniela Moldovan Constant Propagation and Interval Analysis 63 / 70

slide-64
SLIDE 64

Narrowing

H : Dn → Dn is a function with H(x1, . . . , xn) = (y1, . . . , yn), i = 1, . . . , n, where yi = xi ⊓ fi(x1, . . . , xn, i = 1, . . . , n) It holds: Hix = F ix, ∀i ≥ 0 Narrowing operator: ⊓ : a1 ⊓ a2 ⊑ a1 ⊓ a2 ⊑ a1 reduces the values, slowlier as the greatest lower bound

Daniela Moldovan Constant Propagation and Interval Analysis 64 / 70

slide-65
SLIDE 65

Narrowing - Interval Analysis

⊥ ⊓ D = D ⊓ ⊥ = ⊥ For D1 = ⊥ = D2 (D1 ⊓ D2)x = (D1x) ⊓ (D2x), [l1, u1] ⊓ [l2, u2] = [l, u] l =

  • l2 if l1 = −∞

l1, else u =

  • u2 if u1 = +∞

u1, else the left operand is the value from the last iteration, the right

  • perand is the new computed value.

Daniela Moldovan Constant Propagation and Interval Analysis 65 / 70

slide-66
SLIDE 66

Example - Accelerated Narrowing

In our example no information is lost.

Daniela Moldovan Constant Propagation and Interval Analysis 66 / 70

slide-67
SLIDE 67

Narrowing

Narrowing: Requirement: left part of the inequation system has to be monoton narrowing operator was defined in the interval analysis in such a way, that every interval can be modified two times. Fixpoint iteration with Narrowing-Operator terminates in at least O(n · #Vars) rounds.

Daniela Moldovan Constant Propagation and Interval Analysis 67 / 70

slide-68
SLIDE 68

Summary

Program analyses at compile time with corresponding transformations Code optimizations Run time improvement

Daniela Moldovan Constant Propagation and Interval Analysis 68 / 70

slide-69
SLIDE 69

References

Reinhard Wilhelm, H. Seidl, S. Hack. ¨ Ubersetzerbau, Band 3: Analyse und Transformation. Springer, 2010

Daniela Moldovan Constant Propagation and Interval Analysis 69 / 70

slide-70
SLIDE 70

Thank you ! Questions ?

Daniela Moldovan Constant Propagation and Interval Analysis 70 / 70