# Compilers 2027 Rank-Decider: Basic-Block Leaders, SSA Phi-Functions, Dominators & Graph Coloring
Exam: GATE Computer Science & Information Technology (GATE CS)
Subject: Compiler Design
Canonical URL: https://gateandnet.com/exams/gate-cs/compiler-design/ssa-dominators-basic-blocks-and-register-coloring

> Exact 3-rule Basic-Block Leader algorithm, Control-Flow Dominator trees & Natural Loops, Static Single Assignment (SSA) minimal phi-nodes, backward Liveness fixpoint, and Chaitin k-coloring.

## 1. Three Exact Rules to Identify Basic-Block Leaders in TAC

Given a sequence of Three-Address Code (TAC) instructions `1..N`, an instruction is a **Leader** iff:
1. It is the **very first instruction** (`1`) of the intermediate program; OR
2. It is the **target** of any conditional or unconditional jump (`goto L`); OR
3. It **immediately follows** a conditional or unconditional jump!

Each Leader starts a **Basic Block** consisting of itself and all subsequent instructions up to (not including) the next Leader. **Number of Basic Blocks = Number of Distinct Leaders!**

## 2. Static Single Assignment (SSA) & $\phi$-Functions (Hot GATE CS 2024–2027 Topic!)

In **SSA form**, every variable in the IR is **assigned statically at most ONCE** (`x1, x2, x3`):
- Straight-line reassignments rename `x = x + 1` into `x2 = x1 + 1`.
- Where two control-flow branches merge at a ** Dominance Frontier** join block (e.g., after `if-else` or at a loop header) with different versions `x1` and `x2`, SSA inserts a **$\phi$-function**:
  $$x_3 = \phi(x_1, x_2)$$
- **Why compilers love SSA:** Constant propagation, dead-code elimination, and global value numbering become sparse & ultra-fast because every use has a **single unique reaching definition**!

## 3. Dominators & Natural Loops in CFGs

Node $d$ **dominates** node $n$ ($d \text{ dom } n$) iff **every path** from the entry block to $n$ **must pass through $d$** (reflexive, antisymmetric, transitive $\implies$ forms a **Dominator Tree** rooted at Entry!). A **Natural Loop** is defined by a **Back Edge $n \to d$ where the target $d$ dominates source $n$**!

## Solved Practice Questions & PYQs

### [GATE CS 2027 Expected NAT | NAT]
Consider the Three-Address Code fragment below:
`1: i = 1`
`2: s = 0`
`3: if i > 10 goto 8`
`4: if i % 2 == 0 goto 6`
`5: s = s + i`
`6: i = i + 1`
`7: goto 3`
`8: return s`
How many **Basic Blocks** exist in its Control Flow Graph?
**Correct Answer:** 6
**Solution:** Apply the 3 Leader rules:
- Rule 1 (First stmt): **`1`**
- Rule 2 (Jump targets): **`3`** (`goto 3`), **`6`** (`goto 6`), **`8`** (`goto 8`)
- Rule 3 (Follows a jump): **`4`** (follows `3`), **`5`** (follows `4`), and `8` (already counted, follows `7`).
Leaders $= \{1, 3, 4, 5, 6, 8\} \implies \mathbf{6}$ **Basic Blocks**!

### [GATE CS 2026–27 Expected | MCQ]
In **Static Single Assignment (SSA)** intermediate representation, what is the exact role of a $\phi$-function such as $x_3 = \phi(x_1, x_2)$ placed at the entry of a join basic block?
**Correct Answer:** B
**Solution:** At a Dominance-Frontier control-flow merge point, $\phi(x_1, x_2)$ selects the SSA version corresponding to the predecessor basic block executed at runtime so every variable retains a single static definition!
