• retagged by
17,159 views
29 29 votes

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 

Related Questions :

3 Answers

Best answer
52 52 votes

4*j

is used at two places- so common subexpression elimination is possible

$\text{i%2}$ is loop invariant for the inner loop

$5*i$ is also loop invariant for inner loop

$\text{x += 5 * i}$     can be replaced by  $\text{x += p;}$
$\text{p += 5;}$ ($p$ must be initialized to $0$ before the loop).

Thus replacing $*$ with $+$  gives strength reduction. 

The code does not contain unreachable or dead code. because all statements inside the loops are reachable during the execution of the loops.

So, only $(D)$ is false here. 

• edited by
14 14 votes

Answer=

For Option A : The code contains loop invariant computation  = TRUE 

for (i = 0, i < n; i++)                              
{           

if (i%2)

  for (j = 0; j < n; j++)
    {
           if (i%2)

Now the i%2 will be computer for only 1 for loop

For Option B : There is scope of common sub-expression elimination in this code : TRUE

     {

       m = 4*j 

            x += (4*j + 5*i); // it becomes  x += (  m + 5*i); 

            y += (7 + 4*j); //it becomes y += (7 +  m ); 

        } 
  • Now We only have to compute only 1 time 4*j (later it will be strength reduced by the way )     rather computing it two times 

For Option C : There is scope of strength reduction in this code : TRUE

1. 4*j = j<<4.
  • we can use left shift operator as compared to the multiply operator.

  • Because  modern compilers often optimize x * 2 to x << 1,because left shift operation is more   efficienct to multiply operators in terms of   (* 2^n) 

  For Option D: There is scope of dead code elimination in this code : FALSE

  •   Because All statements inside the loops are reachable during the execution of the loops.
 
 
• edited by
1 flag:
✌ Edit necessary (Mr-TAGORE “Option c, not 4, it's 2”)
3 3 votes

There is scope for dead code elimination in this code.

Explanation:

  1. The code contains loop invariant computation:

    • True. The expressions (4*j + 5*i) and (7 + 4*j) are computed inside the inner loop but do not depend on the inner loop variable j. They can be moved outside the inner loop to improve efficiency.
  2. There is scope for common sub-expression elimination in this code:

    • True. The expression (4*j + 5*i) is computed twice with the same values of i and j inside the inner loop. Common sub-expression elimination can be applied to avoid redundant computations.
  3. There is scope for strength reduction in this code:

    • True. The expression (4*j + 5*i) involves multiplication and addition. Strength reduction could be applied by replacing the multiplication with a series of cheaper operations.
  4. There is scope for dead code elimination in this code:

    • False. The code does not contain unreachable or dead code. All statements inside the loops are reachable during the execution of the loops.
Answer:
Position:
Show:

Related questions

37 37 votes
5 answers 5 answers
19.0k
19.0k views
Rucha Shelke asked Sep 26, 2014
18,983 views
Consider these two functions and two statements S1 and S2 about them. int work1(int *a, int i, int j) { int x = a[i+2]; a[j] = x+1; return a[i+2] - 3; }int work2(int *a, ...
37 37 votes
5 answers 5 answers
13.1k
13.1k views
go_editor asked Nov 7, 2016
13,072 views
The grammar$S\rightarrow AC\mid CB$$C\rightarrow aCb\mid \epsilon$$A\rightarrow aA\mid a$$B\rightarrow Bb\mid b$generates the language $ L=\left \{ a^{i}b^{j}\mid i\neq j...
53 53 votes
4 answers 4 answers
18.3k
18.3k views
Rucha Shelke asked Sep 26, 2014
18,268 views
Which one of the following grammars generates the language $ L=\left \{ a^{i}b^{j}\mid i\neq j \right \}$?$S\rightarrow AC\mid CB$$C\rightarrow aCb\mid a\mid b$$A\rightar...
41 41 votes
5 answers 5 answers
18.2k
18.2k views
Rucha Shelke asked Sep 26, 2014
18,187 views
Consider the following translation scheme. $ S\rightarrow ER$$ R\rightarrow *E\left \{ \text{print}(\text{‘}*\text{’}); \right \} R\mid \varepsilon $$ E\rightarrow F+E\le...