edited by
4,581 views
2 votes
2 votes
Consider the following code which computes the inner product of 2 vectors:

prod := 0;

i := 1;

repeat

{

prod := prod + a[i] * b[i]

i = i+1;

until i >20

}

Below is possible IR for this program :

1) prod := 0

2) i :=1

3) t1 :=4*i

4) t2 :=a[t1]

5) t3 :=4*i

6) t4 :=b[t3]

7) t5 :=t2*t4

8)t6 :=prod + t5

9) prod :- t6

10)t7 := i+1

11) i :=t7

12) if i<=20 goto (3)

13) ..

 

Create Basic Blocks and the control Flow Graph and also show any Optimizatioions.If you Find.
edited by

1 Answer

4 votes
4 votes

The original code is computes inner product of vectors i.e., a[1]*b[1] + a[2]*b[2] + ...... + a[20]*b[20]. So, it can be re-written as $\Rightarrow$

int prod = 0;
for(i=1;i<=20;i++) prod += a[i]*b[i];

Now, Intermediate representations can be in many ways, but if the IR given in question is considered, then we Find Basic Blocks and CFG by finding leaders first.

  • First statement is a leader.
  • Target of goto is a leader
  • Statement that immediatedly follows goto is a leader.

And a Basic block starts from one leader and continues till starting of next leader without including that leader. So, statement $1,3$ and $13$ are leaders here.

And Basic blocks are $(1-2)$, $(3-12)$ and  $13$ onwards till program exits. Flow graph looks like $\Rightarrow$

And Cycle in program detects the presence of a loop. Further optimization like Loop Unrolling can be done here.

Related questions

429
views
2 answers
0 votes
Ebrahim asked Dec 18, 2023
429 views
Please Answer this question in detail step by step: 3. Translate the arithmetic expression (a + b * c) + d + (a + b * c) − d + e into: a). Syntax tree, (please dr...
289
views
1 answers
0 votes
Ebrahim asked Jan 31
289 views
6. Generate code for the following C program using any code generation algorithm. [3 Marks] main() { int x, a, b, c, d, e; ...
135
views
0 answers
0 votes
Ebrahim asked Jan 11
135 views
Find the FIRST and FOLLOW of the grammar to check whether it is LL (1) parser or not. N → AB | BA A → a | CAC B → b | CBC C → a | b
185
views
0 answers
0 votes
Ebrahim asked Jan 10
185 views
Please provide in detail solution step by step 5. Find the FIRST and FOLLOW of the grammar to check whether it is LL (1) parser or not. N → AB | BA A → a | CAC...