14
SSA

SSA - PHI-functions Placements

Embed Size (px)

Citation preview

Page 1: SSA - PHI-functions Placements

SSA

Page 2: SSA - PHI-functions Placements

SSA Properties● Each definition in the procedure creates a unique name.● Each use refers to a single definition.

Page 3: SSA - PHI-functions Placements

Construct SSA Form● Insert phi-functions.● Rename variables to conform the properties.

Page 4: SSA - PHI-functions Placements

Where to Insert phi-functions● A definition in node n forces a phi-function at join points that lie just outside

the region of the CFG that n dominates.○ n dominate a predecessor of m○ n does not strictly dominate m

■ m belongs DF(n)

● DF(n) contains the first node reachable from n that n does not dominate.● DF: Dominance Frontier

Page 5: SSA - PHI-functions Placements

Dominance● In a flow graph with entry node e, node n dominates node m if and only if n

lies on every path from e to m.● Annotate each node m in the CFG with a set DOM(m) that contains the

names of all nodes that dominate m

Page 6: SSA - PHI-functions Placements

e

a

b c

d f

g

h

i

DOM(d) = {e, a, c, d}

Page 7: SSA - PHI-functions Placements

Dominance Frontier e

a

b c

d f

g

h

i

DF(c) = {h}

Page 8: SSA - PHI-functions Placements

Immediate Dominator● The node in strictly dominator

that is closest to n is called n’s immediate dominator IDOM(n)

e

a

b c

d f

g

h

i

IDOM(g) = {c}

Page 9: SSA - PHI-functions Placements

Dominator Tree● Node

○ Every node of the flow graph

● Edge○ If n is in IDOM(m), then the

dominator tree has an edge from n to m.

e

a

b c

d f

g

h

i

Page 10: SSA - PHI-functions Placements

Dominator Tree Properties● Given a node n in the dominator tree

○ IDOM(n) is just its parent in the tree○ DOM(n): the nodes that lie on the path from the root of the dominator tree to n.

Page 11: SSA - PHI-functions Placements

Placing phi-functions● A definition of x in block b forces a phi-function at every node in DF(b)● Global names

○ The set of names that are live across multiple blocks

● Insert phi-functions for global names and ignore any name that is not in the set

○ semipruned SSA form

Page 12: SSA - PHI-functions Placements

Global Names● In each block, it looks for names with upward exposed uses, UEVAR.● Union of all the UEVAR sets gives global names.

Globals = {}Initialize all the Blocks sets to {}for each block b varkill = {} for each operation i in b in order assume operation i is x = y op z if y is not in varkill Globals = Globals + {y} if z is not in varkill Globals = Globals + {z} varkill = varkill + {x} block(x) = block(x) + {b}

Page 13: SSA - PHI-functions Placements

Computing Dominance Frontiers

for all nodes n in the CFG DF(n) = {}

for all nodes n in the CFG if n has multiple predecessors for each predecessor p of n runner = p while runner != IDOM(n) DF(runner) = DF(runner) + {n} runner = IDOM(runner)

Page 14: SSA - PHI-functions Placements

Insert phi-functions

for each name x in Globals worklist = blocks(x) for each block b in worklist for each block d in DF(b) if d has no phi-function for x insert phi-function for x in d worklist = worklist + {d}