Code Generation and Optimization
Question 1 |
they enhance the portability of the compiler to other target processors | |
program analysis is more accurate on intermediate code than on machine code | |
the information from dataflow analysis cannot otherwise be used for optimization | |
the information from the front end cannot otherwise be used for optimization |
Discuss it
Question 2 |


2 | |
3 | |
5 | |
6 |
Discuss it
Question 3 |
A basic block is a sequence of instructions where control enters the sequence at the beginning and exits at the end. | |
Available expression analysis can be used for common subexpression elimination. | |
Live variable analysis can be used for dead code elimination. | |
x = 4 ∗ 5 => x = 20 is an example of common subexpression elimination. |
Discuss it
In the following code: a = b * c + g; d = b * c * e; it may be worth transforming the code to: tmp = b * c; a = tmp + g; d = tmp * e;Sources: https://en.wikipedia.org/wiki/Common_subexpression_elimination https://en.wikipedia.org/wiki/Available_expression
Question 4 |
make parsing and semantic analysis simpler. | |
improve error recovery and error reporting. | |
increase the chances of reusing the machine-independent code optimizer in other compilers. | |
improve the register allocation. |
Discuss it
Question 5 |
Consider the following C code segment.
for (i = 0, i<n; i++) { for (j=0; j<n; j++) { if (i%2) { x += (4*j + 5*i); y += (7 + 4*j); } } }
Which one of the following is false?
The code contains loop invariant computation | |
There is scope of common sub-expression elimination in this code | |
There is scope of strength reduction in this code | |
There is scope of dead code elimination in this code |
Discuss it
Question asks about false statement
4*j is common subexpression elimination so B is true. 5*i can be moved out of inner loop so can be i%2. Means, A is true as we have loop invariant computation. Next, 4*j as well as 5*i can be replaced with a = - 4; before j loop then a = a + 4; where 4*j is computed, likewise for 5*i. C is true as there is scope of strength reduction. By choice elimination, we have D.
Question 6 |
Consider the grammar rule E → E1 - E2 for arithmetic expressions. The code generated is targeted to a CPU having a single user register. The subtraction operation requires the first operand to be in the register. If E1 and E2 do not have any common sub expression, in order to get the shortest possible code
E1 should be evaluated first | |
E2 should be evaluated first | |
Evaluation of E1 and E2 should necessarily be interleaved | |
Order of evaluation of E1 and E2 is of no consequence |
Discuss it
E -> E1 - E2 Given that E1 and E2 don't share any sub expression, most optimized usage of single user register for evaluation of this production rule would come only when E2 is evaluated before E1. This is because when we will have E1 evaluated in the register, E2 would have been already computed and stored at some memory location. Hence we could just use subtraction operation to take the user register as first operand, i.e. E1 and E2 value from its memory location referenced using some index register or some other form according to the instruction. Hence correct answer should be (B) E2 should be evaluated first.
Question 7 |
1. i = 1 2. j = 1 3. t1 = 5 * i 4. t2 = t1 + j 5. t3 = 4 * t2 6. t4 = t3 7. a[t4] = –1 8. j = j + 1 9. if j <= 5 goto(3) 10. i = i + 1 11. if i < 5 goto(2)The number of nodes and edges in the control-flow-graph constructed for the above code, respectively, are
5 and 7 | |
6 and 7 | |
5 and 5 | |
7 and 8 |
Discuss it

Question 8 |
x = u - t; y = x * v; x = y + w; y = t - z; y = x * y;The minimum number of total variables required to convert the above code segment to static single assignment form is Note : This question was asked as Numerical Answer Type.
6 | |
8 | |
9 | |
10 |
Discuss it
x = u - t; x = y + w;and three assignments of the variable y.
y = x * v; y = t - z; y = x * ySo we use two variables x1, x2 for specifying distinct assignments of x and y1, y2 and y3 each assignment of y. So, total number of variables is 10 (x1, x2, y1, y2, y3, t, u, v, w, z). Static Single Assignment form(SSA) of the given code segment is:
x1 = u - t; y1 = x1 * v; x2 = y1 + w; y2 = t - z; y3 = x2 * y2;Please refer below link for details https://www.cs.cmu.edu/~fp/courses/15411-f08/lectures/09-ssa.pdf
Question 9 |
Ambiguous | |
Unambiguous | |
Information is not sufficient to decide whether it is ambiguous or Unambiguous. | |
None of the above |
Discuss it
S → n B → BbBHere we see that grammar is left as well as right recursive but still it is unambiguous grammar because A is useless production but it is still part of grammar. So we can say that a grammar having both left as well as right recursion may or may not be ambiguous . Lets understand with another example as we have grammar like this A→AA using this grammar we can not produce any string in finite steps as language of this grammar is empty set {}. Hence, we finally get conclusion as if grammar is having both left as well as right recursion, then grammar may or may not be ambiguous. Option (C) is correct.
Question 10 |
The least number of temporary variables required to create a three-address code in static single assignment form for the expression a=b * d - c + b * e - c is ______
3 | |
4 | |
5 | |
6 |
Discuss it
a=b * d - c + b * e - c t1 = b * d t2 = b * e t3 = t1 + t2 t4 = t3 -c a= t4 -c
Total temp variables = 4
So, option (B) is correct.
Refer - Static Single Assignment