• edited by
19,355 views
38 38 votes

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, int i, int j)
{
    int t1 = i+2;
    int t2 = a[t1];
    a[j] = t2+1;
    return t2 - 3;
}

S1: The transformation form work1 to work2 is valid, i.e., for any program state and input arguments, work2 will compute the same output and have the same effect on program state as work1 

S2: All the transformations applied to work1 to get work2 will always improve the performance (i.e reduce CPU time) of work2 compared to work1

  1. S1 is false and S2 is false
  2. S1 is false and S2 is true
  3. S1 is true and S2 is false
  4. S1 is true and S2 is true

5 Answers

Best answer
44 44 votes

Consider an array a = 1 2 3 4 5 and condition i + 2 =j. Lets take i =0 and j =2 for this example.

Work 1, 

x = a[0+2] = 3

a[2] = 3 + 1 = 4;  which means a = 1 2 4 4 5

return a[0+2] - 3 = 4 -3 = 1

Work 2

t1 = 0 + 2 = 2

t2 = a[2] = 3

a[2] = 3 + 1 = 4, which means a = 1 2 4 4 5 again

return t2 - 3 = 3 -3 =0

Hence S1 is false when i + 2 =j. S2 will also be false, since we cant explicitly say the performance of work2 will always be better than work1.

Hence answer is A

• selected by
9 9 votes
answer - A

when j == i+2 programs will return different results
1 1 vote
CounterExample for statement 1:

a= [1,7,12,3,9,6,5,13]

i=2 and j=4

{you can simply run & check that}

Code 1 will return 7 while Code 2 will return 6 , so the transformation is invalid.

 

Now statement 2: yes we are reducing computation of i+2 by storing it in a temp, but we can't gaurantee that this will always increase efficiency as this is high level optimization, & we have no idea of machine & behaviour during it.
0 0 votes

 

 

The Correct Option

 

S1 is false and S2 is false


 

Analysis of the Code

 

To understand why, let's look at what the compiler (or the programmer) changed between work1 and work2.

  • work1: Reads a[i+2] twice. Once to assign it to x, and again at the end for the return statement.

  • work2: Reads a[i+2] once, stores it in a temporary variable t2, and reuses t2 for the return statement.

This transformation is known as Common Subexpression Elimination (CSE). The compiler assumes that a[i+2] does not change between the first read and the second read.


 

Step-by-Step Approach

 

 

1. Analyzing Statement S1 (Validity)

 

Statement: The transformation from work1 to work2 is valid... work2 will compute the same output... as work1.

Verdict: FALSE

Reasoning:

This statement fails due to Memory Aliasing. Aliasing occurs when two different pointers or indices refer to the same memory location.

Let's assume a specific case where i and j cause an overlap:

  • Let i = 0

  • Let j = 2

  • Therefore, the index i+2 is the same as index j.

  • Let the array a initially be {10, 20, 30, 40}. So a[2] is 30.

Trace for work1 (No Optimization):

  1. x = a[0+2] (which is a[2]). So, x = 30.

  2. a[j] = x + 1. Since j=2, we set a[2] = 31. (Memory is updated)

  3. return a[i+2] - 3. Since i+2 is 2, this reads the new value of a[2].

  4. Calculation: $31 - 3 = 28$.

  5. Result: 28.

Trace for work2 (With Optimization):

  1. t1 = 0+2.

  2. t2 = a[t1]. So, t2 = 30. (The value is cached in a register/variable).

  3. a[j] = t2 + 1. Since j=2, we set a[2] = 31. (Memory is updated)

  4. return t2 - 3. This uses the old cached value of t2.

  5. Calculation: $30 - 3 = 27$.

  6. Result: 27.

Conclusion: Because the outputs differ (28 vs 27) when aliasing occurs, the transformation is not valid for all program states.

 

2. Analyzing Statement S2 (Performance)

 

Statement: All the transformations... will always improve the performance... of work2 compared to work1.

Verdict: FALSE

Reasoning:

While removing a redundant memory load (reading a[i+2] twice) usually improves speed, the word "always" makes this statement false in the context of compiler theory.

  1. Register Pressure: In work2, the variable t2 must be kept "alive" (held in a CPU register) across the execution of line a[j] = t2+1.

  2. Spilling: If the CPU has very few registers available (high register pressure), the compiler might be forced to "spill" t2 to the stack (RAM) to free up a register for the assignment operation, and then reload it later.

  3. Cost: Spilling to the stack and reloading can be just as expensive, or sometimes more expensive, than simply reloading the value from the L1 cache as done in work1.

Therefore, we cannot guarantee that this transformation always reduces CPU time.


 

Summary Table

 

StatementTruth ValueKey Concept
S1FalseAliasing: a[j] might overwrite a[i+2], changing the return value in work1 but not work2.
S2FalseRegister Pressure: Holding the temporary variable t2 might cause register spilling, potentially degrading performance
• edited by
–1 –1 vote
Ans- C

Only an extra variable t1 has been added in work 2 instead of directly computing the subscript as in work 1.The output will be same. S1 is true.

The addition of variables t1 and t2 will not improve the performance in anyway. i.e S2 is false.

Corrcet me I'm wrong
Answer:
Position:
Show:

Related questions

30 30 votes
3 answers 3 answers
17.3k
17.3k views
Rucha Shelke asked Sep 26, 2014
17,330 views
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...
37 37 votes
5 answers 5 answers
13.3k
13.3k views
go_editor asked Nov 7, 2016
13,285 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.5k
18.5k views
Rucha Shelke asked Sep 26, 2014
18,544 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.5k
18.5k views
Rucha Shelke asked Sep 26, 2014
18,519 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...