Optimisation inside one basic block: finding leaders and basic blocks, building the flow graph, DAG-based common-subexpression and dead-code elimination, constant folding, copy propagation, algebraic simplification, strength reduction and peephole optimisation.
A basic block is a maximal sequence of consecutive three-address instructions that control enters only at the first instruction and leaves only at the last. Inside a block there are no jumps in and no jumps out except at the ends, so a block always runs start to finish.
To partition code, first find the leaders:
The first instruction of the program is a leader.
Any instruction that is the target of a jump (conditional or unconditional) is a leader.
Any instruction that immediately follows a jump (conditional or unconditional) is a leader.
Each block runs from a leader up to, but not including, the next leader.
The control flow graph (CFG) has one node per block. There is an edge if can run right after : either ends with a jump to the leader of , or follows in program order and does not end in an unconditional jump. A conditional jump therefore gives two out-edges; goto and return give one and zero.
Local optimisation works inside one block, global optimisation across the CFG, and loop optimisation on cycles of the CFG.
Flow graph of: 1 i = 1, 2 j = 1, 3 t1 = 10 * i, 4 t2 = t1 + j, 5 j = j + 1, 6 if j <= 10 goto 3, 7 i = i + 1, 8 if i <= 10 goto 2, 9 k = 0
2. The local transformations
Transformation
Before
After
Common subexpression elimination
a = b + c ... d = b + c
d = a (if b, c, a unchanged in between)
Constant folding
x = 4 * 2
x = 8
Constant propagation
x = 8 ... y = x + w
y = 8 + w
Copy propagation
u = z ... v = u * k
v = z * k
Dead code elimination
t = a + b with t never used
removed
Algebraic simplification
x + 0, x * 1, x - 0
x
Strength reduction
x * 2, x * 8, x ** 2
x + x, x << 3, x * x
These feed each other. Constant propagation creates folding chances; copy propagation leaves copies like u = z with no remaining uses, which dead code elimination then removes.
A variable is dead at a point if its value is not used before being redefined or before the program ends. Inside a block, dead code elimination needs to know which variables are live on exit from the block; if a question does not say, all named variables are usually assumed live on exit and only temporaries may be removed. Read this carefully: it changes the answer to "minimum number of instructions" questions.
3. The DAG of a basic block
Build the DAG instruction by instruction:
Each variable's initial value is a leaf, labelled , and so on.
For x = y op z, find the current nodes for y and z. If a node with the same op and the same children exists, reuse it; otherwise create one. Then attach the label x to that node (and remove x from any node that carried it before).
For a copy x = y, just attach x to y's node; no new node.
What the DAG gives you:
Common subexpressions: a node reached by more than one instruction is computed once.
Dead code: a node with no parents and no live-on-exit label can be deleted (repeat until none is left).
Minimum code: one instruction per remaining interior node, plus copies only for extra live labels.
Arrays and pointers. After a[i] = x, any node that reads a[j] must be killed (not reused), because may equal . An assignment through a pointer *p = x kills every node that might be aliased by p. A procedure call kills nodes that the callee may change.
The number of DAG nodes is leaves (distinct initial values) plus distinct interior nodes; GATE often asks for this count.
4. Peephole optimisation
A peephole optimiser slides a small window (a few instructions) over target or intermediate code and replaces patterns with shorter or faster ones. It is cheap and is often run repeatedly, since one change can expose another.
Typical peephole patterns:
Redundant load/store: in MOV R0, a followed by MOV a, R0, the store is redundant (unless the second instruction is a jump target).
Unreachable code: instructions right after an unconditional jump that no label points to.
Flow-of-control (jump chaining): goto L1 where L1: goto L2 becomes goto L2. A conditional jump to an unconditional jump is shortened the same way.
Algebraic simplification: delete x = x + 0 and x = x * 1.
Strength reduction and machine idioms: x * 2 to x + x or a shift; x = x + 1 to an increment instruction if the machine has one.
Peephole optimisation is local in the sense of a fixed window. It is not the same as register allocation or loop optimisation, which need the whole flow graph.
5. Local versus global and loop optimisation
Scope
Unit
Examples
Needs
Local
one basic block
DAG-based CSE, folding, copy propagation inside the block
only the block, plus live-on-exit set
Peephole
a sliding window
redundant loads, jump chaining
a few instructions
Global
whole CFG of a procedure
global CSE, constant propagation across blocks, dead code
data-flow analysis
Loop
cycles in the CFG
code motion of loop-invariant computations, induction-variable strength reduction
loop detection
Loop-invariant code motion moves a computation whose operands do not change inside a loop to just before the loop. Induction-variable strength reduction replaces t = 4 * i (with i stepping by 1) by t = t + 4 each iteration. These are not local, but GATE often lists them with local optimisations in "which of these" questions, so know which is which.
A local optimiser sees one block only, so it must be conservative at block boundaries: it cannot assume a variable is dead at the end of a block unless told so.
Formulas to remember
Leaders
Basic blocks
DAG node countCopies add no nodes
Common mistakes
Forgetting rule 3: the instruction after a jump is a leader even when nothing jumps to it.
Adding a fall-through edge after an unconditional goto or a return.
Counting copies such as d = a as new DAG nodes; a copy only adds a label.
Deleting assignments to named variables when the question does not say they are dead on exit.
Reusing an array-element node after an assignment to the same array.
Calling loop-invariant code motion a local optimisation; it needs the loop structure of the flow graph.
Worked examples
Try each step yourself, then reveal the working.
Example 1: Leaders, basic blocks and flow-graph edges
numerical
Find the leaders, basic blocks and flow-graph edges:
1: i = 1
2: j = 1
3: t1 = 10 * i
4: t2 = t1 + j
5: j = j + 1
6: if j <= 10 goto 3
7: i = i + 1
8: if i <= 10 goto 2
9: k = 0
Step 1: Apply the three leader rules.
Try it on paper first.
Example 2: DAG of a block and the shortest code
numerical
Build the DAG of the block below. How many nodes does it have? If only f is live on exit, what is the shortest equivalent code?
a = b + c
d = b + c
e = d * a
f = a * a
Step 1: Process the first two instructions.
Try it on paper first.
Example 3: Chaining the transformations
numerical
Only v is live on exit. Optimise:
x = 4
y = x * 2
z = y + w
u = z
v = u * 1
Step 1: Propagate the constant x and fold.
Try it on paper first.
Example 4: Spotting peephole opportunities
conceptual
Which peephole changes apply?
MOV R0, a
MOV a, R0
goto L1
ADD R1, 1
L1: goto L2
(No label points to ADD R1, 1.)
Step 1: Look at the load followed by the store.
Try it on paper first.
Previous year questions
4 official GATE CSE questions on this topic, newest first.
10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.
Mark as mastered
Loading your progress…
My revision notes
Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.