Compiler Design Question Bank
Unit – 6 Code Generation, Code Optimization
1. Discuss design issues of code generation. (6 Marks)
2. Explain the following issues in design of code generator: (6 Marks)
i. Input to the code generator
ii. Target programs
iii. Register allocation
3. Explain the following issues in design of code generator: (6 Marks)
i. Evaluation order
ii. Instruction selection
iii. Memory management
4. Create basic blocks and control flow graph for the following code: (6 Marks)
for(i=0; i <n; i++)
{
for(j=i+1; j<n; j++)
{
sum=sum+a[i]*b[j];
}
}
5. Create basic blocks and control flow graph for the following code: (6 Marks)
fact (x)
{
int f = 1;
for (i=2; i<=x; i++)
f = f * i;
return (f);
}
6. Create basic blocks and control flow graph for the following code: (6 Marks)
for(i=0; i <n; i++) {
for(j=i+1; j<n; j++) {
if(a[i]>a[j]) {
temp=a[i];
a[i]=a[j];
a[j]=temp; } } }
7. Create basic blocks and control flow graph for the following code: (6 Marks)
n = 153;
t = 0;
while (n > 0) {
n = n % 10;
t = t + (n * n * n);
n = n / 10;
}
Prepared by – Mr. Viral H. Panchal 1
Compiler Design Question Bank
8. Create basic blocks and control flow graph for the following code: (6 Marks)
int fib(int m)
{
int f1=0, f2=1, f3, i ;
if (m<=1)
{
return m;
}
else
{
for(i=2; i<=m; i++)
{
f3=f1+f2;
f1=f2;
f2=f1;
}
return f3;
}
}
9. Find the liveness and next use information for following three-address code: (6
Marks)
(1) t1 = 4 * i
(2) t2 = a[t1]
(3) t3 = 4 * i
(4) t4 = b[t3]
(5) t5 = t2 * t4
(6) t6 = prod + t5
(7) prod = t6
(8) t7 = i + 1
(9) i = t7
10. Construct a DAG for the following basic block and also generate the code for
the same using two registers. (6 Marks)
t1 = a + b
t2 = c + d
t3 = e - t2
t4 = t1 - t3
11. Construct a DAG for the given expression and also generate the code for the
same using two registers. (6 Marks)
( a - b ) + c * ( d / e)
12. What is loop in flow graph? Discuss flow graph and its use in code optimization
with appropriate example. (6 Marks)
Prepared by – Mr. Viral H. Panchal 2
Compiler Design Question Bank
13. Explain different loop optimization techniques. (6 Marks)
14. Explain loop unrolling and loop fusion. (6 Marks)
15. Generate the code for the expression (a – b) – (c – (d + e)) using (i) two
registers (ii) one register. (6 Marks)
16. What is back edge? Find out natural loops and dominators of each node from
following flow graph: (6 Marks)
17. Explain any three machine independent optimization techniques. (6 Marks)
18. Explain the following code optimization techniques: (6 Marks)
i. Dead code elimination
ii. Code motion
iii. Strength reduction
19. Explain different loop optimization techniques. (6 Marks)
20. Explain peephole optimization. (6 Marks)
Prepared by – Mr. Viral H. Panchal 3