in Compiler Design recategorized by
1,548 views
2 votes
2 votes

Consider the code segment

int i,j,x,y,m,n;
n = 20;
for(i=0;i<n;i++)
{
    for(j=0;j<n;j++)
    {
        if(i%2)
        {
            x += ((4*j)+5*i);
            y += (7+4*j)
        }
    }
}
m=x+y;

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
in Compiler Design recategorized by
by
1.5k views

1 comment

Question image has not loaded it seems
0
0

2 Answers

12 votes
12 votes
Best answer
  • loop invariant: A fragment of code that resides in the loop and computes the same value in all iterations. In given loop if(i%2) computes same value within loop of j. This code can be moved out of the loop.
 for(i=0;i<n;i++) {
if(i%2)
   {
    for(j=0;j<n;j++)
    {
       ......

This movement is called code hoisting

A is true 

  • The sub expression  4*j is used at two places which can be eliminated.

B is true

  • Strength reduction: Operations that consume more time and space can be replaced by simple operations that produce the same result.

  4*j can be replaced by j <<2

C is true

  • There is no dead / Unreachable code.

D is false

Answer is D

selected by
by
0 votes
0 votes
option D

There is no scope of dead code elimination.
Answer:

Related questions