All mock tests are FREE till 31 December — register to save your progress

Directions (Q91–95): Consider the following Three Address Code sample for solving the questions:

100: t1 = y + 2
101: initial = x / t1
102: limit = 10
103: if i > j goto 105
104: goto 111
105: if num > b goto 107
106: goto 111
107: num = limit - 1
108: b = y + 2
109: i = i - num
110: goto 103
111: j = i - num
112: k = j + b

Which of the following is the optimized version of the TAC given in the passage?

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: C.

100: t1 = y + 2
101: limit = 10
102: num = 9
103: b = t1
104: if i > j goto 106
105: goto 110
106: if num > b goto 108
107: goto 110
108: i = i - num
109: goto 104
110: j = i - num
111: k = j + b

Explanation

Option C applies the expected optimisations: constant folding (num = limit − 1 = 9), common-subexpression/copy propagation (b = y + 2 becomes b = t1), loop-invariant code motion (num and b moved out of the loop), and dead-code elimination ('initial' is never used). It also keeps j = i − num and k = j + b. The other options change the program's logic (wrong jump targets, missing j = i − num, or i = i − limit − 1). (Strictly, hoisting num and b out of the loop assumes the loop body executes; C is the intended answer.)

More Theory of Computation and Compilers questions