1 / 43

CSE 231 : Advanced Compilers

CSE 231 : Advanced Compilers. Building Program Analyzers. Foundations. Foundations : Relations. Relation R over set S is just a set of pairs from S: R ⊆ S x S a R b means (a, b) ∈ R Example: < = { (a, b) | b – a is positive } a < b means (a, b) ∈ <.

swinford
Télécharger la présentation

CSE 231 : Advanced Compilers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 231 : Advanced Compilers Building Program Analyzers

  2. Foundations

  3. Foundations : Relations Relation R over set S is just a set of pairs from S: R ⊆S xS a R b means (a, b)∈R Example: < = { (a, b) | b – a is positive } a < b means (a, b)∈<

  4. Foundations : Types of Relations • reflexive • ∀a ∈S . a R a • transitive • ∀a, b, c ∈S. a R b /\b R c a R c • symmetric • ∀a, b ∈S . a R b b R a • anti-symmetric • ∀a, b ∈S . a R b /\ b R a a = b

  5. Foundations : Anti-Symmetry • anti-symmetric • ∀a, b ∈S . a R b /\ b R a a = b • Anti-symmetry is slightly weird. • Essentially: • “If you start at X and only follow R to new elements, you will never get back to X.”

  6. Foundations : Types of Relations equivalence reflexive, transitive, symmetric defines equivalence classes partitions domain partial order reflexive, transitive, anti-symmetric defines a partial order on domain

  7. Foundations : Posets partially ordered set (poset) defined by set S and partial order ·

  8. Foundations : Posets partially ordered set (poset) defined by set S and partial order · Example: (2{x, y, z},⊆)

  9. Foundations : Posets partially ordered set (poset) defined by set S and partial order · Examples: (2S, ⊆) (Z, <) (Z, divides)

  10. Foundations : Least Upper Bounds Assume poset (S, ⊑) least upper bound (lub) of a and b is c where a ⊑c b ⊑c ∀d . (a ⊑d /\b ⊑d) c ⊑d Upper Least

  11. Foundations : Greatest Lower Bounds Assume poset (S, ⊑) greatest lower bound (glb) of a and b is c where c ⊑a c ⊑b ∀d . (d ⊑a /\d ⊑b) d ⊑c Lower Greatest

  12. Foundations : Bounds Assume poset (S, ⊑) Essentially: lub(a, b) = smallest thing bigger than a and b glb(a, b) = biggest thing smaller than a and b Question: Do lub and glb always exist?

  13. Foundations : Bounds Assume poset (S, ⊑) Essentially: lub(a, b) = smallest thing bigger than a and b glb(a, b) = biggest thing smaller than a and b Question: Do lub and glb always exist? Answer: No!

  14. Foundations : Bounds Question: Do lub and glb always exist? Answer: No! glb( , ) = ???

  15. Foundations : Lattices A lattice is (S, ⊑, ⊥, ⊤, ⊔, ⊓) where: (S, ⊑) is a poset ⊥ is the smallest thing in S ⊤ is the biggest thing in S lub(a, b) and glb(a, b) always exist a⊔ b = lub(a, b) a⊓ b = glb(a, b)

  16. Foundations : Lattices (Formally) A lattice is (S, ⊑, ⊥, ⊤, ⊔, ⊓) where: (S, ⊑) is a poset ∀ a ∈ S . ⊥ ⊑ a ∀ a ∈ S . a ⊑ ⊤ ∀ a, b ∈ S . ∃ c . c = lub(a, b) /\ a⊔ b = c ∀ a, b ∈ S . ∃ c . c = glb(a, b) /\ a⊓ b = c

  17. Foundations :Fancy Lattice Names ⊥ is “botom” ⊤ is “top” ⊔ is “join” ⊓ is “meet”

  18. Examples of lattices • Powerset lattice

  19. Examples of lattices • Powerset lattice

  20. Examples of lattices • Booleans expressions

  21. Examples of lattices • Booleans expressions

  22. Examples of lattices • Booleans expressions

  23. Examples of lattices • Booleans expressions

  24. End Foundations, Now Build

  25. Analysis on Sets let m: map from edge to computed value at edge let worklist: work list of nodes for each edge e in CFG do m(e) := ∅ for each node n do worklist.add(n) while (worklist.empty.not) do let n := worklist.remove_any; let info_in := m(n.incoming_edges); let info_out := F(n, info_in); for i := 0 .. info_out.length do let new_info := m(n.outgoing_edges[i]) ∪ info_out[i]; if (m(n.outgoing_edges[i])  new_info]) m(n.outgoing_edges[i]) := new_info; worklist.add(n.outgoing_edges[i].dst);

  26. Port Analysis To Run On Lattices Formalize domain with a powerset lattice. What should top and bottom be?

  27. Port Analysis To Run On Lattices Formalize domain with a powerset lattice. What should top and bottom be? Does it matter? Should we even care?

  28. Port Analysis To Run On Lattices Formalize domain with a powerset lattice. What should top and bottom be? Does it matter? Should we even care? Yes. A notion of approximation shows up in the lattice.

  29. Lattice Direction Unfortunate name clashes: dataflow analysis picked one direction abstract interpretation picked the other We work in the abstract interpretation direction: ⊥(bottom) is most precise (optimistic) ⊤(top) is most imprecise (conservative)

  30. Lattice Direction Always safe to go up in the lattice. can always set the result to ⊤ (top) Hard to go down in the lattice. So: ⊥(bottom) will be empty set in reaching defns

  31. Building an Analysis: worklist + lattice let m: map from edge to computed value at edge let worklist: work list of nodes for each edge e in CFG do m(e) := ⊥ for each node n do worklist.add(n) while (worklist.empty.not) do let n := worklist.remove_any; let info_in := m(n.incoming_edges); let info_out := F(n, info_in); for i := 0 .. info_out.length do let new_info := m(n.outgoing_edges[i]) ⊔ info_out[i]; if (m(n.outgoing_edges[i])  new_info]) m(n.outgoing_edges[i]) := new_info; worklist.add(n.outgoing_edges[i].dst);

  32. Termination? For reaching definitions, it terminates. Why?

  33. Termination? For reaching definitions, it terminates. Why? Because lattice is finite. Can we loosen this requirement? Yes, only require the lattice to have a finite height. Height of a lattice: length of longest ascending or descending chain

  34. Termination? Height of lattice (2S, ⊆) = ???

  35. Termination? Height of lattice (2S, ⊆) = | S |

  36. Termination. But can we do better? Still annoying to perform join in the worklist algo: It would be nice to get rid of it. Is there a property of the flow functions that can help? while (worklist.empty.not) do let n := worklist.remove_any; let info_in := m(n.incoming_edges); let info_out := F(n, info_in); for i := 0 .. info_out.length do let new_info := m(n.outgoing_edges[i]) ⊔ info_out[i]; if (m(n.outgoing_edges[i])  new_info]) m(n.outgoing_edges[i]) := new_info; worklist.add(n.outgoing_edges[i].dst);

  37. Crank Up the Formality… To reason even more precisely about termination, we port our worklist algorithm to math. Fixed points underlie our new algorithm.

  38. Back to Foundations

  39. Fixpoints What is a fixpoint?

  40. Fixpoints What is a fixpoint? An input to a function that equals its output. So a fixpoint for f is any input x such that: f(x) = x Easy!

  41. Fixpoints Goal: compute map m from CFG edges to dataflow information Strategy: define a global flow function F as follows: F takes a map m as a parameter and returns a new map m’, in which individual local flow functions have been applied

  42. Fixpoints • Recall, we are computing m, a map from edges to dataflow information • Define a global flow function F as follows: F takes a map m as a parameter and returns a new map m’, in which individual local flow functions have been applied

  43. Fixpoints • We want to find a fixed point of F, that is, a map m such that m = F(m) • Approach to doing this? • Define ⊥, which is ⊥ lifted to be a map: ⊥ =  e. ⊥ • Compute F(⊥), then F(F(⊥)), then F(F(F(⊥))), ... until the result doesn’t change

More Related