272 views
2 2 votes

Consider the following C code segment:
 

int a = 10, b = 20;
for (int i = 0; i < n; i++)
{
    int constant_val = a * b + 5;
    for (int j = 0; j < n; j++)
    {
        int temp = i * 8;
        arr[i][j] = temp + j;
        
        if (0) {
            printf("Debugging: %d", arr[i][j]);
        }
    }
}


Which one of the following is FALSE?

  1. THE CODE CONTAINS LOOP INVARIANT COMPUTATION
     
  2. THERE IS SCOPE FOR STRENGTH REDUCTION IN THIS CODE
     
  3. THERE IS SCOPE FOR DEAD CODE ELIMINATION IN THIS CODE
     
  4. THERE IS SCOPE FOR COMMON SUB-EXPRESSION ELIMINATION IN THIS CODE

2 Answers

0 0 votes

Loop Invariant Computation (Option A is TRUE): The expression $\verb|int constant_val = a * b + 5;|$ inside the outer loop does not depend on the loop variable $\verb|i|$. It can be moved outside the entire loop structure.

Strength Reduction (Option B is TRUE): The expression $\verb|temp = i * 8|$ involves a multiplication. A compiler can replace this with a bitwise shift $(i \ll 3)$ or by using addition in each iteration of the loop, which are "cheaper" operations.

Dead Code Elimination (Option C is TRUE): The block $\verb|if (0)| ~\{ ~\ldots ~\}$ will never execute because the condition is always false. The compiler can remove this entire block of code during the optimization phase.

Common Sub-expression Elimination (Option D is FALSE): In this specific snippet, there are no identical expressions calculated multiple times that could be replaced by a single variable. Each calculation $\verb|(a * b + 5|$, $\verb|i * 8|$, and $\verb|temp + j|)$ is unique within its scope.

Answer:
Position:
Show:

Related questions

1 1 vote
3 3 answers
344
344 views
GO Classes asked Jan 16
344 views
$\text { Consider the following C code segment: }$inline int square(int s) { return s * s; } void process(int n, int a[]) { int x = 10; int y = x; // Copy of x for (int i...
4 4 votes
3 3 answers
355
355 views
GO Classes asked Jan 16
355 views
Consider a language that allows identifiers to start with a digit if they contain at least one letter. How would this impact the design of the Lexical Analyzer?IT WOULD S...
3 3 votes
2 2 answers
306
306 views
GO Classes asked Jan 16
306 views
Consider the following context-free grammar:$$\begin{aligned}& E \rightarrow T R \\& R \rightarrow+T R \mid \epsilon \\& T \rightarrow F Y \\& Y \rightarrow * F Y \mid \e...
2 2 votes
2 2 answers
311
311 views
GO Classes asked Jan 16
311 views
Consider the following grammar $G$ with non-terminals $\{S, A, B, C\}$ and terminals $\{a, b, c, d, g\}$ :$$\begin{aligned}& S \rightarrow A B \\& A \rightarrow a A \mid ...