Skip to content

8.4 Find the blocks that need a phi-node for a variable

You find the blocks where SSA form (static single assignment) needs a phi-node for a variable: the iterated dominance frontier of the blocks that assign it.

You want to find the blocks where paths with different values of a variable join. Compilers need them to convert code to SSA form (static single assignment). In that form, every variable is assigned exactly once, and each assignment creates a new version of the variable. Where paths with different versions of a variable join, such as after an if or at the header of a loop, a phi-node picks the version of the path that was taken. The phi-nodes of a variable go into the iterated dominance frontier of the blocks that assign it. You need this for:

  • a compiler or a just-in-time compiler that converts each function to SSA form, for each of its variables;
  • a decompiler or a static analyzer that builds SSA form to follow the values of variables.

The method loadloadloadLoad of a small program reads the notes of a notebook from a file. Here it also keeps the note it added last:

List<Note> load(Path file) {
List<String> lines = read(file); // entry
List<Note> notes = new ArrayList<>();
Note last = null;
for (int i = 0; i < lines.size(); i++) { // header: i < lines.size()
String line = lines.get(i); // body
if (!line.isBlank()) {
last = parse(line); // then
notes.add(last);
}
} // next: i++
lastLoaded = last; // exit
return notes;
}
fn load(&mut self, file: &Path) -> Vec<Note> {
let lines = read(file); // entry
let mut notes = Vec::new();
let mut last = None;
let mut i = 0;
while i < lines.len() { // header: i < lines.len()
let line = &lines[i]; // body
if !line.trim().is_empty() {
let note = parse(line); // then
last = Some(note.clone());
notes.push(note);
}
i += 1; // next
}
self.last_loaded = last; // exit
notes
}
function load(file: string): Note[] {
const lines = read(file); // entry
const notes: Note[] = [];
let last: Note | null = null;
for (let i = 0; i < lines.length; i++) { // header: i < lines.length
const line = lines[i]; // body
if (line.trim() !== "") {
last = parse(line); // then
notes.push(last);
}
} // next: i++
lastLoaded = last; // exit
return notes;
}
List<Note> Load(string file)
{
string[] lines = Read(file); // entry
var notes = new List<Note>();
Note? last = null;
for (int i = 0; i < lines.Length; i++) // header: i < lines.Length
{
string line = lines[i]; // body
if (!string.IsNullOrWhiteSpace(line))
{
last = Parse(line); // then
notes.Add(last);
}
} // next: i++
_lastLoaded = last; // exit
return notes;
}

The graph cfg holds its control flow. This control-flow graph has a node for each basic block and an edge from each block to each block that can run right after it. The variable last is assigned in entry and in then, as the picture before shows. The loop counter i is assigned in entry and in next. Which blocks need a phi-node for last, and which for i?

The blocks that assign the variable last
The blocks that need a phi-node for the variable last
0: entry1: header2: body3: then4: next5: exit
0: entry1: header2: body3: then4: next5: exit
Input
  • Block that assigns the variable last
Output
  • Block that needs a phi-node

The picture after shows the two blocks that need a phi-node for last. At header, the value from entry and the value from the pass before join. At next, the path through then and the path that skips it join.

The phi-nodes follow from the dominators. See Find the nodes that every path from the entry passes through.

// The loop counter i is assigned in entry (i = 0) and in next (i++), the variable
// last in entry (last = null) and in then (last = parse(line)).
Dominators dominators = Dominators.of(BiAdjacentGraph.of(cfg), entry);
IteratedDominanceFrontier frontier = IteratedDominanceFrontier.of(dominators);
SortedIntView forI = frontier.frontierOf(IntList.of(entry, next)); // [1] - header
SortedIntView forLast = frontier.frontierOf(IntList.of(entry, then));
// forLast: [1, 4] - header and next
Full example
// The loop counter i is assigned in entry (let mut i = 0) and in next (i += 1), the
// variable last in entry (let mut last = None) and in then
// (last = Some(note.clone())).
let dominators = Dominators::of(&BiAdjacentGraph::of(&cfg), entry);
let frontier = IteratedDominanceFrontier::of(&dominators);
let for_i = frontier.frontier_of(&[entry, next]); // [1] - header
let for_last = frontier.frontier_of(&[entry, then]);
// for_last: [1, 4] - header and next
Full example
// The loop counter i is assigned in entry (i = 0) and in next (i++), the variable
// last in entry (last = null) and in then (last = parse(line)).
const dominators = Dominators.of(BiAdjacentGraph.of(cfg), entry);
const frontier = IteratedDominanceFrontier.of(dominators);
const forI = frontier.frontierOf([entry, next]); // [1] - header
const forLast = frontier.frontierOf([entry, then]);
// forLast: [1, 4] - header and next
Full example
// The loop counter i is assigned in entry (i = 0) and in next (i++), the variable
// last in entry (last = null) and in then (last = Parse(line)).
Dominators dominators = Dominators.Of(BiAdjacentGraph.Of(cfg), entry);
IteratedDominanceFrontier frontier = IteratedDominanceFrontier.Of(dominators);
SortedIntView forI = frontier.FrontierOf([entry, next]); // [1] - header
SortedIntView forLast = frontier.FrontierOf([entry, then]);
// forLast: [1, 4] - header and next
Full example

i needs a phi-node at header only, where its value from entry and its value from next join. last needs one at next as well, where the path through then and the path that skips it join.

The iterated dominance frontier of the blocks that assign a variable starts with their dominance frontier. Then comes the dominance frontier of the blocks found, and so on until no block is added. These steps repeat because a phi-node assigns the variable as well. Here, the dominance frontier of then holds next, and the dominance frontier of next holds header. See Find the blocks where the dominance of a block ends.