Similar questions 1 more →
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).](../../assets/questions/CS-2024-S1-Q39-1.webp)
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.