Constructing SSA Graph Where should we put phi assignments, and for which variables? Simple answer: in every join node, for every variable in the program. Too expensive! Suffices to put a phi assignment for x in join nodes that aren't dominated by a single definition of x. Dominance Properties 1. If x is the i'th argument of a phi-function in CFG block n, then the definition of x dominates the i'th predecessor of n. 2. If x is used in a non-phi statement in block n, then the definition of x dominates n. -------- Say x *strictly dominates* w if x dominates w and x is not w. The *dominance frontier* of a definition x, DF(x), is the set of nodes {w | x dominates an (immediate) predecessor of w, but x does not strictly dominate w}. Intuition: Any node in DF(x) is the join point of 2 disjoint paths from x and from the ENTRY node. Note: can compute DF(x) easily given dominator tree. Dominance Frontier Criterion for placing phi-nodes: If node x defines a, then any node x in DF(x) requires a phi-function for a. Since such a phi-function is itself a definition of a, must (in general) iterate until there are no more phi-functions to place. Example: CFG: |-------|0 | ENTER | |-------| | V |--------|1 | k <- 0 | | i <- 1 | | j <- 2 | |--------| | -----------------| | | V V | |--------|2 | | i <= N?| | |--------| | / \ | / \ | / \ | V V | |---------|3 |-----------|4 | | k <- 1 | | k > 0 ? | | | i <- i+1| |-----------| | |---------| / \ | | / \ | | V V |------| |---------|5 |-----------|6 | i <- 0 | | i<- i+1 | |---------| |-----------| \ / \ / V V |-----------|7 | EXIT | |-----------| Dominator tree: 0 | 1 | 2 / \ 3 4 /|\ / | \ 5 6 7 n DF(n) 0 {} 1 {} 2 {2} 3 {2} 4 {} 5 {7} 6 {7} 7 {} By dominance frontier criterion, must place phi-nodes for i and k in node 2, and for i in node 7, and then rename variables: Resulting CFG: |-------|0 | ENTER | |-------| | V |---------|1 | k1 <- 0 | | i1 <- 1 | | j1 <- 2 | |---------| | -----------------| | | V V | |------------------|2 | | k2 <- phi(k3,k1) | | | i2 <- phi(i3,i1) | | | i2 <= N? | | |------------------| | / \ | / \ | / \ | V V | |-----------|3 |-----------|4 | | k3 <- 1 | | k2 > 0 ? | | | i3 <- i2+1| |-----------| | |-----------| / \ | | / \ | | V V |------| |----------|5 |-------------|6 | i4 <- 0 | | i5<- i2+1 | |----------| |-------------| \ / \ / V V |------------------|7 | i6 <- phi(i4,i5) | | EXIT | |------------------| --------------------------------------- Sparse Conditional Constant Propagation (SCCP) For Algorithm, see Textbook Section 10.4.1. Example: --------- B1 a1 <- 3 --------- | V --------- B2 f1 <- 2 --------- ______________ | | | | | V V | ------------------ B3 | a3 <- phi(a1,a2) | ------------------ | | | V | ------------------ B4 | f3 <- phi(f1,f2) | ------------------ | | | V | ----------------- B5 | c1 <- a3 * 2 | ----------------- | | | V | ----------------- B6 | d1 <- c1 -2 | ----------------- | | | V | ----------------- B7 | f3 <= 2 ? | ----------------- | | | | Y | | N | V V | --------------- B8 ------------- B9 | d2 <- a3 + 1 a3 >= 3 ? | --------------- ------------- | | | | | | Y | | N | | V V | | ---------- B10 --------- B11 | | PRINT a3 d3 = 99 | | ---------- --------- | | | | | V V V | ---------------------------- B12 | d4 <- phi(d2,d1,d3) | ---------------------------- | | | V | -------------- B13 | a2 <- d4 - 1 | -------------- | | | V | --------------- B14 | f2 <- f3 + 1 | --------------- | | --------------------- Steps in algorithm Process a1 a2 a3 f1 f2 f3 c1 d1 d2 d3 d4 CFG SSA Block Worklist Worklist Entry T T T T T T T T T T T B1 - B1 3 B2 - B2 2 B3 - B3 3 B4 - B4 2 B5 - B5 6 B6 - B6 4 B7 - B7 B8 - B8 4 B12 - B12 4 B13 - B13 3 B14 B3 B14 3 - B3,B4 B3 - B4 B4 Bot - B9 B9 B10 - B10 - -