Home » Compiler Design » Compiler design miscellaneous » Question

Compiler design miscellaneous

  1. 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?
    1. The code contains loop invariant computation.
    2. There is scope of common sub-expression elimination in this code
    3. There is scope of strength reduction in this code
    4. There is scope of dead code elimination in this code
Correct Option: D

The expression (4*j) is used at two places- so common sub-expression elimination is possible i% 2 is loop invariant for the inner loop
5*i is also loop invariant for inner loop x + = 5*i can be replaced by x + = p;
p + = 5; (p must be initialized to 0 before the loop).
Thus replacing * with + gives strength reduction. So, only (d) is false here.



Your comments will be displayed only after manual approval.