GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Partition branch code into basic blocks

2 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Mark first instruction, branch targets and post-branch instructions as leaders; form maximal blocks and count blocks/largest size. CFG node-edge convention task excluded.

2 marks · NAT
Report issue

Finding leaders and basic blocks

Code Optimization & Data-Flow Analysis

Question 52, GATE 2025: Partition this three-address code into maximal basic blocks. 1001 i=1; 1002 j=1; 1003 t1=10*i; 1004 t2=t1+j; 1005 t3=8*t2; 1006 t4=t3−88; 1007 a[t4]=0.0; 1008 j=j+1; 1009 if j≤10 goto 1003; 1010 i=i+1; 1011 if i≤10 goto 1002; 1012 i=1; 1013 t5=i−1; 1014 t6=88*t5; 1015 a[t6]=1.0; 1016 i=i+1; 1017 if i≤10 goto 1013. How many blocks?
Tap to enlarge
Answer & solution

Official answer: 6

Approach: Mark every leader: the first instruction, every jump target, and every instruction immediately following a jump when present in the supplied code.
The first instruction makes 1001 a leader. Branch targets make 1003,1002,1013 leaders. The instructions following branches at 1009 and 1011 are 1010 and 1012, so they are leaders too. The branch at 1017 is the last supplied instruction; there is no following instruction to create an extra block.
Sorting the distinct leaders gives 1001,1002,1003,1010,1012,1013. Each block extends to just before the next leader, or to the end: [1001], [1002], [1003–1009], [1010–1011], [1012], [1013–1017]. There are six blocks.
A basic block has one entry and no internal branch out before its end. This explains why even a single assignment at 1002 or 1012 can form its own block: the next instruction is independently a branch target. Counting loops or conditionals alone would not identify the correct partition.

Original paper, page 52 · Answer key

2 marks · MCQ
Report issue

Partition code into basic blocks

Code Optimization & Data-Flow Analysis

Question 39, GATE 2024: The code is L1 t1=−1; L2 t2=0; L3 t3=0; L4 t4=4*t3; L5 t5=4*t2; L6 t6=t5*M; L7 t7=t4+t6; L8 t8=a[t7]; L9 if t8<=max goto L11; L10 t1=t8; L11 t3=t3+1; L12 if t3<M goto L4; L13 t2=t2+1; L14 if t2<N goto L3; L15 max=t1. Find (number of basic blocks, instructions in largest block). A (6,6); B (6,7); C (7,7); D (7,6).
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Approach: A basic block is a maximal straight-line instruction sequence with entry only at its first instruction and transfer out only at its last. Identify every leader before counting.
1. The first instruction L1 is a leader. Branch targets L11, L4 and L3 are leaders because execution can enter directly at them.
2. Instructions immediately after conditional branches are also leaders: L10 follows L9, L13 follows L12, and L15 follows L14. These are the fall-through paths.
3. Sort the distinct leaders: L1, L3, L4, L10, L11, L13, L15. Each block runs from one leader through the instruction just before the next leader.
4. The blocks are [L1–L2], [L3], [L4–L9], [L10], [L11–L12], [L13–L14], [L15]. Their lengths are 2,1,6,1,2,2,1, totaling all 15 instructions.
Answer: D, seven blocks with largest size six. The terminating branch L9 belongs to its block; a branch target such as L3 begins a new block even when the preceding instruction falls through to it.

Original paper, page 25 · Answer key