Intermediate Code Generation · Compiler Design
32 questions · asked in 16 of 27 years (2000–2026)
MCQ 26 · MSQ 1 · NAT 51-mark 13 · 2-mark 19
2000Questions per year2026
Questions 1–5 of 32
Consider the control flow graph shown in the figure.
Which one of the following options correctly lists the set of redundant expressions (common subexpressions) in the basic blocks B and B?
Note: All the variables are integers.
Which one of the following options correctly lists the set of redundant expressions (common subexpressions) in the basic blocks B and B?
Note: All the variables are integers.
Answer: (D)
An expression is redundant at a statement if it is computed on every path that reaches the statement, and none of its operands is assigned after that computation. B4 can be reached by B1, B2, B4 and by B1, B3, B4. B5 is reached only through B4.
B4, g*k: it is computed in B2 (a = g * k) and in B3 (t = g * k). Neither path assigns g or k afterwards (B2 assigns a and f, B3 assigns t and b). So g * k is available on both paths, and x = g * k in B4 is redundant.
B4, b+i: it is computed in B1, and B2 does not change b or i, so it is still available on the B2 path. But B3 assigns b = c + m, which kills b + i on the B3 path. So y = b + i is not redundant.
B5, c+m: it is computed only in B3, as b = c + m. The path through B2 never computes it, and B4 does not compute it either. So c + m is not available at B5, and z = c + m is not redundant.
So the redundant expressions are B4: {g * k} and B5: { }. Options A and C wrongly list b + i, and option B wrongly lists c + m.
Answer: (D)
B4, g*k: it is computed in B2 (a = g * k) and in B3 (t = g * k). Neither path assigns g or k afterwards (B2 assigns a and f, B3 assigns t and b). So g * k is available on both paths, and x = g * k in B4 is redundant.
B4, b+i: it is computed in B1, and B2 does not change b or i, so it is still available on the B2 path. But B3 assigns b = c + m, which kills b + i on the B3 path. So y = b + i is not redundant.
B5, c+m: it is computed only in B3, as b = c + m. The path through B2 never computes it, and B4 does not compute it either. So c + m is not available at B5, and z = c + m is not redundant.
So the redundant expressions are B4: {g * k} and B5: { }. Options A and C wrongly list b + i, and option B wrongly lists c + m.
Answer: (D)
Consider the control flow graph given below.
Which one of the following options is the set of live variables at the exit point of each basic block?
Which one of the following options is the set of live variables at the exit point of each basic block?
Answer: (A)
Liveness is a backward analysis. and . Here is the variables read before being written in the block, and is the variables written.
Blocks (edges: B1 to B2 and B3, B2 to B4, B3 to B1, B4 to exit):
B1: use , def
B2: use , def
B3: use , def
B4: use , def
B4: out , so in .
B2: out , so in .
B1 and B3 depend on each other (loop), so iterate. Start with in(B3) = and in(B1) = . Then out(B1) = in(B2) in(B3) , so in(B1) . Then out(B3) = in(B1) , so in(B3) (e is removed because B3 defines it). Then out(B1) , so in(B1) again. Nothing changes, so this is the fixpoint.
Live at the exit of each block: B1 , B2 , B3 , B4 . This matches option (A). Option (C) misses b in B3, but b is read again in B1 after the loop edge and B3 never writes it.
Answer: (A)
Blocks (edges: B1 to B2 and B3, B2 to B4, B3 to B1, B4 to exit):
B1: use , def
B2: use , def
B3: use , def
B4: use , def
B4: out , so in .
B2: out , so in .
B1 and B3 depend on each other (loop), so iterate. Start with in(B3) = and in(B1) = . Then out(B1) = in(B2) in(B3) , so in(B1) . Then out(B3) = in(B1) , so in(B3) (e is removed because B3 defines it). Then out(B1) , so in(B1) again. Nothing changes, so this is the fixpoint.
Live at the exit of each block: B1 , B2 , B3 , B4 . This matches option (A). Option (C) misses b in B3, but b is read again in B1 after the loop edge and B3 never writes it.
Answer: (A)
Which ONE of the following techniques used in compiler code optimization uses live variable analysis?
Answer: (B)
Live variable analysis finds, at every point in the program, the variables whose current values may be used later. These variables are called "live".
This is the very information needed to choose which variables should stay in fast registers and which can safely be removed from them. That is the main idea of register allocation/assignment.
Run-time function call management, strength reduction and constant folding are all real optimizations. But none of them depends on liveness information as much as register allocation does.
Answer: Register assignment to variables
This is the very information needed to choose which variables should stay in fast registers and which can safely be removed from them. That is the main idea of register allocation/assignment.
Run-time function call management, strength reduction and constant folding are all real optimizations. But none of them depends on liveness information as much as register allocation does.
Answer: Register assignment to variables
Refer to the given 3-address code sequence. This code sequence is split into basic blocks. The number of basic blocks is _________. (Answer in integer)
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
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
Answer: 6
A new basic block starts at every leader. A leader is the first statement, any statement that is the target of a jump or goto, and any statement that comes right after a conditional or unconditional jump.
Now look for the leaders in the code: (the first statement); (the target of at ); (it comes right after the conditional jump at ); (the target of at ); (it comes right after the conditional jump at ); (the target of at ).
So there are leaders. This gives basic blocks: .
Answer: 6
Now look for the leaders in the code: (the first statement); (the target of at ); (it comes right after the conditional jump at ); (the target of at ); (it comes right after the conditional jump at ); (the target of at ).
So there are leaders. This gives basic blocks: .
Answer: 6
Consider the following statements about the use of backpatching in a compiler for intermediate code generation:
(I) Backpatching can be used to generate code for Boolean expression in one pass.
(II) Backpatching can be used to generate code for flow-of-control statements in one pass.
Which ONE of the following options is CORRECT?
(I) Backpatching can be used to generate code for Boolean expression in one pass.
(II) Backpatching can be used to generate code for flow-of-control statements in one pass.
Which ONE of the following options is CORRECT?
Answer: (C)
(I) Backpatching for Boolean expressions in one pass: TRUE — this is the main use of backpatching. At first, the jump targets in the and lists are left empty. They are filled in later, when the real target address is known. So no second pass is needed.
(II) Backpatching for flow-of-control statements (if/while) in one pass: also TRUE — the same method gives single-pass code for conditional and loop statements. The jump instructions are patched once the labels they need are decided.
Answer: Both (I) and (II) are correct.
(II) Backpatching for flow-of-control statements (if/while) in one pass: also TRUE — the same method gives single-pass code for conditional and loop statements. The jump instructions are patched once the labels they need are decided.
Answer: Both (I) and (II) are correct.
Keep practising
Topics in Compiler Design
Lexical AnalysisParsingSyntax-directed TranslationRuntime EnvironmentAssemblers, Linkers and Loaders
Intermediate Code GenerationYou are here
Matching