InsertPhiNodes(G):
let P be a map that maps phi nodes to alloca variables.
for each basic block B in G
for each instruction I in B
if I is a store [alloca A], [source variable V] instruction
Insert phi node for A for each block in IDF(B)
P[phi] = A for all phi nodes
Let H be a map that maps each alloca to the variable that holds its current value
Rename(Basic Block B, H):
for each Phi Instruction P in B:
Find the alloca instruction, I, that P corresponds to
Use H to get the current variable, V, for alloca, I.
Insert V into the operand list of P if V is not already in the operand list
Update H such that H maps I to the target variable of P.
If basic block B has been visited:
Return
Else:
Mark B as visited
For each instruction I in B:
if I is a (store [alloca A], [source variable V]) instruction:
H[A] = V
else if I is a (load [target variable V], [alloca A]) instruction:
Replace all uses of V with H[A]
For each successor, S, of B:
Save state of H, H'
Rename (S, H)
Restore H to H'