Jaideep M. More

software engineer


Intermediate Representation

Control Flow Graphs:

Each instruction in a function has a set of possible successor instruction in that function

CFG\text{CFG} is a Directed Graph (V,E,r)(V, E, r)

  • VV: instructions of the function
  • EE: (v,w)∈E(v, w) \in E iff vv is a possible successor of vv
  • rr: is the root node with no predecessor

IR’s typically are intra-functional and control flow between functions is not modelled by CFGs

Basic Blocks:

Is a sequence of instructions with the following properties.

  1. the first node has multiple predecessors
  2. the last node has multiple successors
  3. the nodes in the between have exactly one predecessor and one successor

Critical Edges:

A edge in the control graph that goes from a basic block with multiple successor to a node with multiple predecessor

Dominance:

For a connected CFGCFG, a node vv dominates node ww if for every path from the root node rr to the node ww passes through node vv

That is to say if we want to reach ww from the root rr, we have to pass through vv.

Theorem1:

Dominance is a Partial Ordered Relation.

Reflexive: Every basic block/node dominates itself

AntiSymmetric: If v⪰wv \succeq w and w⪰vw \succeq v then v=wv = w

Transitive: if u⪰vu \succeq v and v⪰wv \succeq w then u⪰wu \succeq w

Theorem2:

If u⪰wu \succeq w and v⪰wv \succeq w ⟹\Longrightarrow either u⪰vu \succeq v or v⪰uv \succeq u

If there are two different nodes that are dominating a third node, then one has to dominate the other.

Corollary:

  1. All dominators of a particular node have a total ordering among them
  2. Except for the root node rr that has no dominators, for every node vv there is an immediate dominator idom(v)\text{idom}(v).
  3. T=(V,{idom(v) ∣ v∈V},r)T = (V, \{\text{idom}(v) \ |\ v \in V\}, r) is a tree.

Post Dominance:

Dual of the dominance relation.

Consider a CFG with a unique exit node ee.

A node ww post dominates a node vv if all paths from vv to ee have to go through ww

SESE - Single Entry Single Exit:

A set of nodes that are dominated by a node uu and are post dominated by a node vv form a set of single entry and single exit nodes.

The notion of dominance is important as we want to identify set of nodes that are executed under same control flow conditions.

Control Flow Dependence:

A node ww is control dependent on an edge (u,v)(u, v) if

  1. node ww post dominates vv: Thus if vv is executed then ww has to be executed.
  2. w≠uw \neq u: Thus, uu is not post dominated by ww: Thus the control condition at node uu determines the execution of the node ww

Using DFS we can classify each edge (u,v)∈E(u, v) \in E as:

  1. forward edge: uu is an ancestor of vv
  2. retreat edge: vv is an ancestor of uu
  3. cross edges: otherwise

A DAG form a CFG is obtained by removing all the retreat edges.

Reducibility:

A class of practically important CFGs are Reducible CFGs

A graph G is called reducible if for every retreat edge (v,h)(v, h), h⪰vh \succeq v

such an edge is called a back edge and the node hh is called header.

Presence of a retreat edge in CFG means that there is a loop. Now if retreat edge is a back edge then the only entry in the loop is through the header node hh.

Theorem:

The following three statements are equivalent.

  1. G is reducible
  2. G has a unique DAG: To not have a unique DAG there has to be multiple entry in the loop. but here since the header dominates all the nodes in a cycle there is a single entry in the loop.
  3. for all the cycles in G, all the nodes in the cycle are dominated by a single node. i.e. header hh

Loop Nesting:

The natural loop of a back edge (v,h)(v, h) is the set of all the nodes that reach vv without going through hh

Natural loop is a set of the nodes that follow the above condition

Theorem:

For two loops L1L_{1} and L2L_{2} one of the following conditions hold:

  1. L1=L2L_{1} = L_{2}
  2. L1⊆L2L_{1} \subseteq L_{2} or L2⊆L1L_{2} \subseteq L_{1}
  3. L1∩L2=ϕL_{1} \cap L_{2} = \phi