# ROSE Compiler Framework/Lattice

## IntroductionEdit

Lattices are mathematical structures. They can be used as a general way to express an order among objects. This data can be exploited in data flow analysis.

Lattices can describe transformations effected by basic blocks on data flow values also known as flow functions.

Lattices can describe data flow frameworks when instantiated as algebraic structures consisting of a set of data flow values, a set of flow functions, and a merge operator.

## PosetEdit

**Partial ordering**:

A **partial ordering** is a binary relation over a set P which is reflexive, antisymmetric and transitive, i.e.

- Reflexive x<=x
- Anti-Symmetric, if then x=y
- Transitive: if then

Partial orders should not be confused with total orders. A total order is a partial order but not vice versa. In a total order any two elements in the set P can be compared. This is not required in a partial order. Two elements that can be compared are said to be ** comparable **

A **partially ordered set**, also known as a **poset**, is a set with a partial order.

Given a poset there may exist an infimum or a supremum. However, not all posets contain these.

Given a poset P with set X and order :

An **infimum** of a subset S of X is an element a of X such that

- for all x in S and
- for all y in X, if for all x in S, then

The dual of this notion is the **supremum** which has the definition of infimum if you switch with

If we simply pick an element of X that satisfies the first condition we have a **lower bound**. The second condition ensures that we have (if it exists) the **unique** greatest lower bound. Similarly for suprema.

A lattice is a particular kind of poset. In particular, a lattice L is a poset P(X, where
For any two elements of the lattice a and b, the set {a, b} has a **join** and a **meet**

The join and meet operations MUST satisfy the following conditions

- 1) The join and meet must commute
- 2) The join and meet are associative
- 3) The join and meet are idempotent, that is, x join itself or x meet itself are both x.

If the lattice contains a meet it is a meet-semilattice, if a lattice contains a join it is a join-semilattice, similarly there exists a meet-semilattice

(Definitions obtained from wikipedia with minimal modification)

## Lattice DefinitionEdit

Definition of a Lattice (L, , )

- L is a poset under such that
- Every pair of elements has a unique greatest lower bound (meet) and least upper bound (join)
- Not every poset is a lattice: greatest lower bounds and least upper bounds need not exist in a poset.

## Infinite vs. Finite latticesEdit

- Infinite: An infinite lattice does not contain an 0 (bottom) or 1 (top) element, even though every pair of elements contains a greatest lower bound and a least upper bound on the entire underlying set. By the definition of unbounded or infinite sets we know that given X an unbounded set given any x in X we can find an x' that is greater than x (under some ordering, in this case the lattice). Similarly for greatest lower bounds.
- a finite/bounded lattice: the underlying set itself has a greatest lower bound and a least upper bound, For now we will call the greatest lower bound 0 and the least upper bound 1.
- if a x, for all x in L, then a is the 0 element of L, , recall that this is a unique element
- if a x for all x from L, then a is the 1 element of L,

Meet is a binary operation such that a b take the greatest lower bound of the set (this is guaranteed by the definition lattice.

Similarly Join returns the least upper bound of the set, guaranteed to exist by the definition of a lattice.

To recap, a lattice L is a triple {X, , } composed of a set, a Meet function, and a Join function

Properties of Meet and .

- We refer to the as and the as J
- Closure: If x and y belong to L, then there exists a unique z and a unique w from L such that x y = z, and x y = w
- Commutativity: for all x, y in L, x y = y meet x, x y = y x:
- Associativity: (x y) z = x (y z), similarly in the operation
- There are two unique elements of L called bottom ( _|_ ), and top (T) , such that for all x, x _|_ = _|_ and x T = T
- Many lattices, with some exceptions, notably the lattice corresponding to constant propagatioin, are also distributive: x y z = (x z) (y z)

Lattices and partial order:

if and only if

A **strictly ascending chain** is a sequence of elements of a set X such that, for x_i in X, has the property . The greatest is the chain with final index n such that n is the greatest such final index among all strictly ascending chains.

The **height** of a lattice is defined as the length of the longest strictly ascending chain it contains.

If a data-flow analysis lattice has a finite height and a monotonic flow function then we know that the associated data flow analysis algorithm will terminate.

- Example: If the greatest strictly ascending chain of a lattice L is finite and it takes finitely many steps to reach the top, we can infer that the associated data flow algorithm terminates.

(wikipedia used for definitions)

## Example: Bit vector LatticesEdit

- The elements of the set are bit vectors
- The bottom is the 0 vector
- The top is a 1 vector
- Meet is a bitwise And
- Join is a bitwise Or

denotes the lattice of bit vectors of length n.

Constructing complex lattices from multiple less complex lattices

- Example: The product operation which combines (concatenates) lattices elementwise
- The product of two lattices L1 and L2 with meet operators M1, M2, respectively: L1 x L2
- The elements in the lattice: {<x1, x2> | x1 from L1, x2 from L2}

- The meet operator: <x1, x2> M <y1, y2> = <x1 M y1, x2 M y2>
- The join operation: <x1, x2> J <y1, y2> = <x1 J y1, x2 J y2>

- Example:
- BV^n is the product of n copies of the trivial bit vector attice BV^1 with bottom 0 and top 1

Graphical Representation BV^3

111 / | \ 110 101 011 | x x \ 100 010 001 \ | / 000

Here meet and join operators induce a partial order on the lattice elements

x is less than or equal to (<=) y if an only if x M y = x

For the BV^3: 000<= 010 <= 101<=111

The partial order on the lattice is:

- Transitive x <= y and y <= z, then x <=z
- Antisymmetric: if x<=y and y<=x, then x = y
- Reflexive: for all x: x<=x:

The height of the lattice is the length of its longest strictly ascending chain:

- The maximal n such that there exists a strictly ascending chain x1, x2, ..., xn such that
- Bottom = x1 < x2 < xn = Top

For BV^3 lattice, height = 4

## Monotonic FunctionsEdit

A monotonic function is a function that preserves an ordering.

## ExamplesEdit

A function f from L to itself, f: L -> L, is monotonic if for all x, y from L, x<=y ==> f(x)<=f(y)

f: BV^3 -> BV^3: f (<x1 x2 x3>) -> <x1 1 x3>

## Lattice TuplesEdit

Simple analyses may require complex lattices:

- Problem:
- Reaching Constants: V 2^(v*c) where v is the number of variables and c is the constants

- Solution:
- Construct a tuple of lattices where each lattice corresponds to a variable

V = constant U {Top, Bottom}

## integer value: ICPEdit

This is used in constant propagation Elements: Top, Bottom, Integers, Booleans

- n M Bottom = Bottom
- n J Top = Top
- n J n = n M n = n
- Integers and booleans m,n, if m != n, then m M n = Bottom, m J n = Top
- The lattice has three levels: the top element, all other elements, the bottom element
- Join operation: Higher level to lower level
- Meet operation: Lower level to higher level

## Relevance to data flow analysisEdit

A lattice provides a set of flow values to a particular data flow analysis.

Lattices are used to argue the existence of a solution obtainable through fixed-point iteration

- At each program point a lattice represents an IN[p] or OUT[p] set (flow value)
- meet: merge flow values, e.g. set union, deal with control flow branches merge
- Top usually represents the best information (initial flow value). Note people can use top to represent worst-base information also!!
- The bottom value represents the worst-base information
- if BOTTOM <= x <= y <= TOP , then x is a conservative approximation of y. e.g. x is a superset

### e.g. liveness analysisEdit

bitvector for all variables x_1, x_2, ..., x_n

First step: design the lattice values

- top value: empty set {}, initial value, knowing nothing
- bottom value: all set {x_1, x_2, ..., x_n}: max possible value, knowing every variable is live

n = 3, 3 variable case: a flow value==> a set of live variable at a point

S = {v1, v2, v3}

value set: 2^3 = { empty, {v1},{v2}, {v3}, {v1, v2}, {v1,v3}, {v2, v3}, {v1, v2, ve} }

Design lattice

- top value, best case: none live { T } // top
- bottom value, worst ase: all live {v1, v2, v3}

Design meet: set Union (Or operation): bring the value down to the bottom, context insensitive

- design partial order <= -->

In between, a partial order: inferior/conservative solutions are lower on the lattice

Top / | \ {v1} {v2} {v3} | x x | {v1, v2} {v1,v3} {v2,v3} \ | / {v1, v2, v3} = Bottom

Flow function F: }

### reaching definitionEdit

Value: 2^n n = number of all definitions

top: empty set: knowing nothing, initial value

bottom: all set: all definitions are reaching definition

Meet operation: set union: bring down the levels of values, from unknowing to knowing