CD Unit 5
CD Unit 5
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
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.
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. :
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.
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. :
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. :
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 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.
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:
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:
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. :
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 –
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. :
Example –
Advantage –
It is used to eliminate common sub expressions.
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. :
Definition:
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
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.
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
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. :
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:
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:
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.
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.
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. :
C. Memory Management –
D. Instruction selection –
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. :
1. Correct
2. Easily maintainable
3. Testable
4. Efficient
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:
o A register descriptor is used to keep track of what is currently in each registers. The
register
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:
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.
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.
o The root node represents operator and leaf nodes represents operands in the
DAG.
19