Tag: Directed Acyclic Graph PPT

Miscellaneous Problems in Compiler Design

Three Address Code | DAGs | Basic Blocks & Flow Graphs-

 

Before you go through this article, make sure that you have gone through the previous articles on-

 

In this article, we will solve Miscellaneous Problems based on these Concepts.

 

PRACTICE PROBLEMS BASED ON ABOVE CONCEPTS-

 

Problem-01:

 

Generate three address code for the following code-

 

c = 0

do

{

if (a < b) then

x++;

else

x–;

c++;

} while (c < 5)

 

Solution-

 

Three address code for the given code is-

  1. c = 0
  2. if (a < b) goto (4)
  3. goto (7)
  4. T1 = x + 1
  5. x = T1
  6. goto (9)
  7. T2 = x – 1
  8. x = T2
  9. T3 = c + 1
  10. c = T3
  11. if (c < 5) goto (2)

 

Problem-02:

 

Generate three address code for the following code-

 

while (A < C and B > D) do

if A = 1 then C = C + 1

else

while A <= D

do A = A + B

 

Solution-

 

Three address code for the given code is-

  1. if (A < C) goto (3)
  2. goto (15)
  3. if (B > D) goto (5)
  4. goto (15)
  5. if (A = 1) goto (7)
  6. goto (10)
  7. T1 = c + 1
  8. c = T1
  9. goto (1)
  10. if (A <= D) goto (12)
  11. goto (1)
  12. T2 = A + B
  13. A = T2
  14. goto (10)

 

Problem-03:

 

Generate three address code for the following code-

 

switch (ch)

{

case 1 : c = a + b;

break;

case 2 : c = a – b;

break;

}

 

Solution-

 

Three address code for the given code is-

 

if ch = 1 goto L1

if ch = 2 goto L2

L1:

T1 = a + b

c = T1

goto Last

L2:

T1 = a – b

c = T2

goto Last

Last:

 

Problem-04:

 

Construct a DAG for the following three address code-

  1. a = b + c
  2. t1 = a x a
  3. b = t1 + a
  4. c = t1 x b
  5. t2 = c + b
  6. a = t2 + t2

 

Solution-

 

Directed acyclic graph for the given three address code is-

 

 

Problem-05:

 

Consider the following code-

 

prod = 0 ;

i = 1 ;

do

{

prod = prod + a[ i ] x b[ i ] ;

i = i + 1 ;

} while (i <= 10) ;

 

  1. Compute the three address code.
  2. Compute the basic blocks and draw the flow graph.

 

Solution-

 

Part-01:

 

Three address code for the given code is-

 

prod = 0

i = 1

T1 = 4 x i

T2 = a[T1]

T3 = 4 x i

T4 = b[T3]

T5 = T2 x T4

T6 = T5 + prod

prod = T6

T7 = i + 1

i = T7

if (i <= 10) goto (3)

 

Part-02:

 

Step-01:

 

We identify the leader statements as-

  • prod = 0 is a leader because first statement is a leader.
  • T1 = 4 x i is a leader because target of conditional or unconditional goto is a leader.

 

Step-02:

 

The above generated three address code can be partitioned into 2 basic blocks as-

 

 

Step-03:

 

The flow graph is-

 

 

To gain better understanding about these Miscellaneous Problems,

Watch this Video Lecture

 

Download Handwritten Notes Here-

 

Next Article- Code Optimization

 

Get more notes and other study material of Compiler Design.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Directed Acyclic Graphs | DAGs | Examples

Directed Acyclic Graph-

 

Directed Acyclic Graph (DAG) is a special kind of Abstract Syntax Tree.

 

  • Each node of it contains a unique value.
  • It does not contain any cycles in it, hence called Acyclic.

 

Optimization Of Basic Blocks-

 

DAG is a very useful data structure for implementing transformations on Basic Blocks.

 

  • A DAG is constructed for optimizing the basic block.
  • A DAG is usually constructed using Three Address Code.
  • Transformations such as dead code elimination and common sub expression elimination are then applied.

 

Properties-

 

  • Reachability relation forms a partial order in DAGs.
  • Both transitive closure & transitive reduction are uniquely defined for DAGs.
  • Topological Orderings are defined for DAGs.

 

Applications-

 

DAGs are used for the following purposes-

  • To determine the expressions which have been computed more than once (called common sub-expressions).
  • To determine the names whose computation has been done outside the block but used inside the block.
  • To determine the statements of the block whose computed value can be made available outside the block.
  • To simplify the list of Quadruples by not executing the assignment instructions x:=y unless they are necessary and eliminating the common sub-expressions.

 

Construction of DAGs-

 

Following rules are used for the construction of DAGs-

 

Rule-01:

 

In a DAG,

  • Interior nodes always represent the operators.
  • Exterior nodes (leaf nodes) always represent the names, identifiers or constants.

 

Rule-02:

 

While constructing a DAG,

  • A check is made to find if there exists any node with the same value.
  • A new node is created only when there does not exist any node with the same value.
  • This action helps in detecting the common sub-expressions and avoiding the re-computation of the same.

 

Rule-03:

 

The assignment instructions of the form x:=y are not performed unless they are necessary.

 

Also Read- Code Optimization

 

PRACTICE PROBLEMS BASED ON DIRECTED ACYCLIC GRAPHS-

 

Problem-01:

 

Consider the following expression and construct a DAG for it-

         ( a + b ) x ( a + b + c )

 

Solution-

 

Three Address Code for the given expression is-

 

T1 = a + b

T2 = T1 + c

T3 = T1 x T2

 

Now, Directed Acyclic Graph is-

 

 

NOTE

 

From the constructed DAG, we observe-

  • The common sub-expression (a+b) has been expressed into a single node in the DAG.
  • The computation is carried out only once and stored in the identifier T1 and reused later.

 

This illustrates how the construction scheme of a DAG identifies the common sub-expression and helps in eliminating its re-computation later.

 

Problem-02:

 

Consider the following expression and construct a DAG for it-

( ( ( a + a ) + ( a + a ) ) + ( ( a + a ) + ( a + a ) ) )

 

Solution-

 

Directed Acyclic Graph for the given expression is-

 

 

Problem-03:

 

Consider the following block and construct a DAG for it-

 

(1) a = b x c

(2) d = b

(3) e = d x c

(4) b = e

(5) f = b + c

(6) g = f + d

 

Solution-

 

Directed Acyclic Graph for the given block is-

 

 

Problem-04:

 

Optimize the block in the Problem-03.

 

Solution-

 

Step-01:

 

Firstly, construct a DAG for the given block (already done above).

 

Step-02:

 

Now, the optimized block can be generated by traversing the DAG.

  • The common sub-expression e = d x c which is actually b x c (since d = b) is eliminated.
  • The dead code b = e is eliminated.

 

The optimized block is-

 

(1) a = b x c

(2) d = b

(3) f = a + c

(4) g = f + d

 

Problem-05:

 

Consider the following basic block-

 

B10:

S1 = 4 x I

S2 = addr(A) – 4

S3 = S2[S1]

S4 = 4 x I

S5 = addr(B) – 4

S6 = S5[S4]

S7 = S3 x S6

S8 = PROD + S7

PROD = S8

S9 = I + 1

I = S9

If I <= 20 goto L10

 

  1. Draw a directed acyclic graph and identify local common sub-expressions.
  2. After eliminating the common sub-expressions, re-write the basic block.

 

Solution-

 

Directed Acyclic Graph for the given basic block is-

 

 

In this code fragment,

  • 4 x I is a common sub-expression. Hence, we can eliminate because S1 = S4.
  • We can optimize S8 = PROD + S7 and PROD = S8 as PROD = PROD + S7.
  • We can optimize S9 = I + 1 and I = S9 as I = I + 1.

 

After eliminating S4, S8 and S9, we get the following basic block-

 

B10:

S1 = 4 x I

S2 = addr(A) – 4

S3 = S2[S1]

S5 = addr(B) – 4

S6 = S5[S1]

S7 = S3 x S6

PROD = PROD + S7

I = I + 1

If I <= 20 goto L10

 

To gain  better understanding about Directed Acyclic Graphs,

Watch this Video Lecture

 

Download Handwritten Notes Here-

 

 

Next Article- Misc Problems On Directed Acyclic Graphs

 

Get more notes and other study material of Compiler Design.

Watch video lectures by visiting our YouTube channel LearnVidFun.