0% found this document useful (0 votes)
13 views19 pages

CD Unit 5

Uploaded by

baburishabh368
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views19 pages

CD Unit 5

Uploaded by

baburishabh368
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur

Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

UNIT-V

1. Directed Acyclic Graph:

The Directed Acyclic Graph (DAG) is used to represent the structure of basic blocks, to
visualize the flow of values between basic blocks, and to provide optimization techniques
in the basic block. To apply an optimization technique to a basic block, a DAG is a three-
address code that is generated as the result of an intermediate code generation.

 Directed acyclic graphs are a type of data structure and they are used to apply
transformations to basic blocks.
 The Directed Acyclic Graph (DAG) facilitates the transformation of basic blocks.
 DAG is an efficient method for identifying common sub-expressions.
 It demonstrates how the statement’s computed value is used in subsequent
statements.

Examples of directed acyclic graph:

1
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

1.1 Directed Acyclic Graph Characteristics:

A Directed Acyclic Graph for Basic Block is a directed acyclic graph with the following
labels on nodes.

 The graph’s leaves each have a unique identifier, which can be variable names or
constants.

 The interior nodes of the graph are labelled with an operator symbol.

 In addition, nodes are given a string of identifiers to use as labels for storing the
computed value.

 Directed Acyclic Graphs have their own definitions for transitive closure and
transitive reduction.

 Directed Acyclic Graphs have topological orderings defined.

1.2 Algorithm for construction of Directed Acyclic Graph:

There are three possible scenarios for building a DAG on three address codes:

Case 1 – x = y op z
Case 2 – x = op y
Case 3 – x = y

Directed Acyclic Graph for the above cases can be built as follows :
Step 1 –
 If the y operand is not defined, then create a node (y).
 If the z operand is not defined, create a node for case(1) as node(z).

Step 2 –
 Create node(OP) for case(1), with node(z) as its right child and node(OP) as its left
child (y).
 For the case (2), see if there is a node operator (OP) with one child node (y).
 Node n will be node(y) in case (3).
Step 3 –

Remove x from the list of node identifiers. Step 2: Add x to the list of attached identifiers
for node n.

2
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

Example :
T0 = a + b —Expression 1
T1 = T 0 + c —-Expression 2
d = T 0 + T1 —–Expression 3

Expression 1 : T0 = a + b

Expression 2: T1 = T0 + c

Expression 3 : d = T0 + T1

3
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

Example :

T1 = a + b
T2 = T1 + c
T3 = T1 x T2

Example :

T1 = a + b
T2 = a – b
T3 = T 1 * T2
T4 = T 1 – T3
T5 = T 4 + T 3

4
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

Example:

a=bxc
d=b
e=dxc
b=e
f=b+c
g=f+d

Example :

T1:= 4*I0
T2:= a[T1]
T3:= 4*I0
T4:= b[T3]
T5:= T2 * T4
T6:= prod + T5
prod:= T 6
T7:= I0 + 1
I0:= T7
if I0 <= 20 goto 1

5
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

1.3 Application of Directed Acyclic Graph:

 Directed acyclic graph determines the sub expressions that are commonly used.

 Directed acyclic graph determines the names used within the block as well as the
names computed outside the block.

 Determines which statements in the block may have their computed value outside the
block.

 Code can be represented by a Directed acyclic graph that describes the inputs and
outputs of each of the arithmetic operations performed within the code; this
representation allows the compiler to perform common sub expression elimination
efficiently.

 Several programming languages describe value systems that are linked together by a
directed acyclic graph. When one value changes, its successors are recalculated; each
value in the DAG is evaluated as a function of its predecessors.

6
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

1.4 Advantages of DAG:

 Speed, perhaps its greatest advantage, unlike blockchain the more transactions it has
to process its response speed will be faster.

 Higher level of scalability, by not being subject to limitations on block creation times,
a greater number of transactions can be processed than those processed by blockchain
networks. This is particularly attractive in the application of the Internet of Things.

 It does not require mining; its carbon footprint is a tiny fraction of that left by crypto
currencies that require mining to generate their block chain. This is thanks to the fact
that DAGs do not need PoW consensus algorithms.

2. Source Optimization:

Optimization is a program transformation technique, which tries to improve the code


by making it consume less resources (i.e. CPU, Memory) and deliver high speed.
In optimization, high-level general programming constructs are replaced by very
efficient low-level programming codes. A code optimizing process must follow the
three rules given below:
 The output code must not, in any way, change the meaning of the program.

 Optimization should increase the speed of the program and if possible, the program
should demand less number of resources.

 Optimization should itself be fast and should not delay the overall compiling process.

 Efforts for an optimized code can be made at various levels of compiling the process.

 At the beginning, users can change/rearrange the code or use better algorithms to
write the code.

 After generating intermediate code, the compiler can modify the intermediate code by
address calculations and improving loops.

 While producing the target machine code, the compiler can make use of memory
hierarchy and CPU registers.

Optimization can be categorized broadly into two types : machine independent and
machine dependent.

7
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

3. Loop Optimization:

Loop Optimization is the process of increasing execution speed and reducing the
overheads associated with loops. It plays an important role in improving cache
performance and making effective use of parallel processing capabilities. Most
execution time of a scientific program is spent on loops.

Decreasing the number of instructions in an inner loop improves the running time of
a program even if the amount of code outside that loop is increased.

3.1 Loop Optimization Techniques:

 Frequency Reduction (Code Motion):

In frequency reduction, the amount of code in loop is decreased. A statement or


expression, which can be moved outside the loop body without affecting the
semantics of the program, is moved outside the loop.

Example:
Initial code:

while(i<100)
{
a = Sin(x)/Cos(x) + i;
i++;
}

Optimized code:

t = Sin(x)/Cos(x);
while(i<100)
{
a = t + i;
i++;
}

8
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

 Loop Unrolling:
Loop unrolling is a loop transformation technique that helps to optimize the execution
time of a program. We basically remove or reduce iterations. Loop unrolling increases
the program’s speed by eliminating loop control instruction and loop test instructions.

Example:

Initial code: for (int i=0; i<5; i++)


printf("Pankaj\n");
Optimized code:

printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");
printf("Pankaj\n");

 Loop Jamming:
Loop jamming is the combining the two or more loops in a single loop. It reduces the
time taken to compile the many number of loops.

Example:
Initial Code:

for(int i=0; i<5; i++)


a = i + 5;
for(int i=0; i<5; i++)
b = i + 10;

Optimized code:
for(int i=0; i<5; i++)
{
a = i + 5;
b = i + 10;
}

9
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

4. Data Flow Analysis

It is the analysis of flow of data in control flow graph, i.e., the analysis that determines
the information regarding the definition and use of data in program. With the help of
this analysis, optimization can be done. In general, its process in which values are
computed using data flow analysis. The data flow property represents information that
can be used for optimization.
4.1 Basic Terminologies –

 Definition Point: a point in a program containing some definition.

 Reference Point: a point in a program containing a reference to a data item.

 Evaluation Point: a point in a program containing evaluation of expression.

10
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

4.2 Data Flow Properties –

A. Available Expression – A expression is said to be available at a program point x iff


along paths its reaching to x. A Expression is available at its evaluation point.
An expression a+b is said to be available if none of the operands gets modified
before their use.

Example –

Advantage –
It is used to eliminate common sub expressions.

B. Reaching Definition – A definition D is reaches a point x if there is path from D to


x in which D is not killed, i.e., not redefined.

Example –

Advantage –
It is used in constant and variable propagation.

11
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

C. Live variable – A variable is said to be live at some point p if from p to end the
variable is used before it is redefined else it becomes dead.

Example –

Advantage –
1. It is useful for register allocation.
2. It is used in dead code elimination.

D. Busy Expression – An expression is busy along a path if its evaluation exists along
that path and none of its operand definition exists before its evaluation along the
path.

Advantage –
It is used for performing code movement optimization.

12
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

5. Loop Invariant Condition

Definition:

 A loop invariant is a condition [among program variables] that is necessarily


true immediately before and immediately after each iteration of a loop.
 A loop invariant is some predicate (condition) that holds for every iteration
of the loop.

For example, let’s look at a simple for loop that looks like this:
int j = 9;
for(int i=0; i<10; i++)
j--;
In this example it is true (for every iteration) that i + j == 9.
A weaker invariant that is also true is that i >= 0 && i <= 10.
One may get confused between the loop invariant, and the loop conditional ( the
condition which controls termination of the loop ).
The loop invariant must be true:
 before the loop starts

 before each iteration of the loop

 after the loop terminates

( although it can temporarily be false during the body of the loop ).


On the other hand the loop conditional must be false after the loop terminates,
otherwise, the loop would never terminate.

5.1 Usage:

 Loop invariants capture key facts that explain why code works. This means that if
you write code in which the loop invariant is not obvious, you should add a
comment that gives the loop invariant. This helps other programmers understand the
code, and helps keep them from accidentally breaking the invariant with future
changes.

13
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

 A loop Invariant can help in the design of iterative algorithms when considered an
assertion that expresses important relationships among the variables that must be
true at the start of every iteration and when the loop terminates. If this holds, the
computation is on the road to effectiveness. If false, then the algorithm has failed.
 Loop invariants are used to reason about the correctness of computer programs.
Intuition or trial and error can be used to write easy algorithms however when the
complexity of the problem increases, it is better to use formal methods such as loop
invariants.
 Loop invariants can be used to prove the correctness of an algorithm, debug an
existing algorithm without even tracing the code or develop an algorithm directly
from specification.

A good loop invariant should satisfy three properties:


Initialization: The loop invariant must be true before the first execution of the loop.
Maintenance: If the invariant is true before an iteration of the loop, it should be true
also after the iteration.
Termination: When the loop is terminated the invariant should tell us something
useful, something that helps us understand the algorithm.

5.2 Loop Invariant Condition:

Loop invariant condition is a condition about the relationship between the variables of our
program which is definitely true immediately before and immediately after each iteration of
the loop.

6 Peephole Optimization

 Peephole optimization is a type of Code Optimization performed on a small part of


the code. It is performed on the very small set of instructions in a segment of code.
 It basically works on the theory of replacement in which a part of code is replaced
by shorter and faster code without change in output.
 Peephole is the machine dependent optimization.
6.1 Objectives of Peephole Optimization:

The objective of peephole optimization is:


1. To improve performance
2. To reduce memory footprint
3. To reduce code size

14
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

6.2 Peephole Optimization Techniques:

A. Redundant load and store elimination:

In this technique the redundancy is eliminated.


Initial code:
y = x + 5;
i = y;
z = i;
w = z * 3;

Optimized code:
y = x + 5;
i = y;
w = y * 3;
B. Constant folding:
The code that can be simplified by user itself, is simplified.

Initial code:
x = 2 * 3;

Optimized code:
x = 6;

C. Strength Reduction:
The operators that consume higher execution time are replaced by the
operators consuming less execution time.
Initial code:
y = x * 2;

Optimized code:
y = x + x; or y = x << 1;

Initial code:
y = x / 2;

Optimized code:
y = x >> 1;

D. Null sequences:

Useless operations are deleted.

15
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

E. Combine operations:

Several operations are replaced by a single equivalent operation.

6. Issues in the design of a code generator

Code generator converts the intermediate representation of source code into a form that
can be readily executed by the machine. A code generator is expected to generate the
correct code. Designing of code generator should be done in such a way so that it can be
easily implemented, tested and maintained.

The following issue arises during the code generation phase:

A. Input to code generator –


The input to code generator is the intermediate code generated by the
front end, along with information in the symbol table that determines
the run-time addresses of the data-objects denoted by the names in
the intermediate representation. Intermediate codes may be
represented mostly in quadruples, triples, indirect triples, Postfix
notation, syntax trees, DAG’s, etc. The code generation phase just
proceeds on an assumption that the input are free from all of syntactic
and state semantic errors, the necessary type checking has taken place
and the type-conversion operators have been inserted wherever
necessary.

B. Target program –

The target program is the output of the code generator. The output
may be absolute machine language, relocatable machine language,
assembly language.

 Absolute machine language as output has advantages that it can


be placed in a fixed memory location and can be immediately
executed.

 Relocatable machine language as an output allows subprograms


and subroutines to be compiled separately. Relocatable object
modules can be linked together and loaded by linking loader. But
there is added expense of linking and loading.

16
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

 Assembly language as output makes the code generation easier.


We can generate symbolic instructions and use macro-facilities of
assembler in generating code. And we need an additional
assembly step after code generation.

C. Memory Management –

Mapping the names in the source program to the addresses of data


objects is done by the front end and the code generator. A name in the
three address statements refers to the symbol table entry for name. Then
from the symbol table entry, a relative address can be determined for the
name.

D. Instruction selection –

Selecting the best instructions will improve the efficiency of the


program. It includes the instructions that should be complete and
uniform. Instruction speeds and machine idioms also plays a major
role when efficiency is considered.
.
E. Register allocation issues –
Use of registers make the computations faster in comparison to that
of memory, so efficient utilization of registers is important. The use
of registers are subdivided into two sub problems:

1. During Register allocation – we select only those set of variables


that will reside in the registers at each point in the program.
2. During a subsequent Register assignment phase, the specific register
is picked to access the variable.

F. Evaluation order –

The code generator decides the order in which the instruction will be
executed. The order of computations affects the efficiency of the
target code. Among many computational orders, some will require
only fewer registers to hold the intermediate results. However,
picking the best order in the general case is a difficult NP-complete
problem.

17
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

G. Approaches to code generation issues: Code generator must always


generate the correct code. It is essential because of the number of
special cases that a code generator might face. Some of the design
goals of code generator are:

1. Correct
2. Easily maintainable
3. Testable
4. Efficient

7. A Simple Code Generator :

A code generator generates target code for a sequence of three- address statements
and effectively uses registers to store operands of the statements.

For example: consider the three-address statement a := b+c It can have the
following sequence of codes:

ADD Rj, Ri Cost = 1


(or)
ADD c, Ri Cost = 2
(or)
MOV c, Rj Cost = 3
ADD Rj, Ri

Register and Address Descriptors:

o A register descriptor is used to keep track of what is currently in each registers. The
register

o descriptors show that initially all the registers are empty.

o An address descriptor stores the location where the current value of the name can
be found at run time.

18
ARYA INSTITUTE OF ENGINEERING TECHNOLOGY & MANAGEMENT, Omaxe City, Jaipur
Lecture Notes
Branch: Computer Science Engineering Sem. : Vth Subject :CD (5CS04-02)
Topic: Definition of Basic Block Control Flow Graphs
Unit: 5th Lecture No. :

A code-generation algorithm:

The algorithm takes as input a sequence of three-address statements constituting a


basic block. For each three-address statement of the form x : = y op z, perform the following
actions:

1. Invoke a function getreg to determine the location L where the result of the computation y
op z should be stored.

2. Consult the address descriptor for y to determine y’, the current location of y. Prefer the
register for y’ if the value of y is currently both in memory and a register. If the value of y is
not already in L, generate the instruction MOV y’ , L to place a copy of y in L.

3. Generate the instruction OP z’ , L where z’ is a current location of z. Prefer a register to a


memory location if z is in both. Update the address descriptor of x to indicate that x is in
location L. If x is in L, update its descriptor and remove x from all other descriptors.

4. If the current values of y or z have no next uses, are not live on exit from the block, and are
in registers, alter the register descriptor to indicate that, after execution of x : = y op z , those
registers will no longer contain y or z.

8. Code Generation: Using DAG

o This algorithm is also called as Heuristic Ordering Algorithm for code


generation.

o In nodes of DAG are ordered and listed to generate the code.

o The root node represents operator and leaf nodes represents operands in the
DAG.

19

You might also like