0% found this document useful (0 votes)
38 views

Compiler Design Unit 5

The basic block is a sequence of consecutive instructions that are always executed in order without branching. Basic blocks do not contain any jump statements. When the first instruction of a basic block executes, all instructions in that basic block will execute sequentially. Basic blocks are identified using rules such as determining leader instructions and partitioning the code at leader instructions. Basic blocks are represented as nodes in a flow graph to show control flow. Code optimization techniques aim to improve performance and efficiency without changing semantics. These include common subexpression elimination, code movement, strength reduction, and loop and peephole optimizations.

Uploaded by

Shubham Dixit
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)
38 views

Compiler Design Unit 5

The basic block is a sequence of consecutive instructions that are always executed in order without branching. Basic blocks do not contain any jump statements. When the first instruction of a basic block executes, all instructions in that basic block will execute sequentially. Basic blocks are identified using rules such as determining leader instructions and partitioning the code at leader instructions. Basic blocks are represented as nodes in a flow graph to show control flow. Code optimization techniques aim to improve performance and efficiency without changing semantics. These include common subexpression elimination, code movement, strength reduction, and loop and peephole optimizations.

Uploaded by

Shubham Dixit
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/ 39

BASIC BLOCKS

•The basic block is a sequence Examples:


of consecutive elements which
are always executed in -> a = b + c + d
Three address code
sequence with out halt or
1. T1=b+c
possibility of branching. 2. T2=T1+d
•The basic block doesn’t have 3. a=T2
any jump statement among
them. -> if A<B then 1 else 0
•When the first statement is Three address code
executed, all the instructions in 1. If (A<B) goto (3)
the same basic block will be 2. goto(4)
executed in their sequence of 3. T1=1
appearance without losing the 4. T1=0
5. ………
flow control of the program.
RULES FOR CREATING BASIC BLOCKS
After an intermediate code is generated for the given
code, we can use the following rules to partitioned into
the basic blocks.
Rule 1: Determine the Leaders
•The first statement is a Leader

•Any Target element of conditional or unconditional “goto” is a


Leader.

•Any statement that immediately follow a “goto” is a Leader.

Rule 2: The basic block is formed starting at the leader statement


and ending just before the next leader statement appearing.
FLOW GRAPH
A flow graph is a directed graph in which the flow control
information is added to the basic blocks.

Rules:
•The basic blocks are the nodes to the flow graph

•The block whose leader is the first statement is called initial block.
•There is a directed edge from block B1 to block B2 if B2
immediately follows B1 in the given sequence, we can say that B1 is
a predecessor of B2
(If-else) case
Q: Translate the expression into Three address code
if a<b then x=y+z else p=q+r
Sol: 3-address code :
L: 1 if a < b goto (3) B1 L1: if a < b goto (B3)
L: 2 goto (6)
L: 3 t1 = y + z B2 L2: goto (B4)
4 x = t1
5 goto (Next)
L: 6 t2 = q + r B3 L3: t1 = y + z
7 p = t2 x = t1
goto (Next)
8 goto (Next)

L4: t2 = q + r
B4
p = t2
goto (Next)
(Do-while) case
Q: Translate the expression into Three address code
do x = y + z while a < b

Sol: 3-address code : L1: t1 = y + z


L: 1 t1 = y + z B1 x = t1
2 x = t1 goto (B2)

3 goto (4)
L: 4 if a < b goto (1)
L: 5 goto (NEXT)
B2 L2: if a < b goto (B1)

B3 L3: goto (NEXT)


(while) case
Q: Translate the expression into Three address code
while a < b do x = y + z

Sol: 3-address code :


L: 1 if a < b goto (3) B1 L1: if a < b goto (B3)
L: 2 goto(6)
L: 3 t1 = y + z
4 x = t1 B2 L2: goto(B4)
5 goto (1)
L: 6 goto NEXT
L3: t1 = y + z
B3 x = t1
goto (B1)

B4 L4: goto(NEXT)
CODE OPTIMIZATION
•It is required to produce an efficient target code.
•The semantic equivalence of the source program must not be
changed.
•It must be achieved without changing the algorithm of the
program.
•Optimized code
 Executes faster.
 Efficient memory usage.
 Yielding better performance.

•Users should only focus on optimizations not provided by the


compiler such as choosing a faster and/or less memory intensive
algorithm.
CODE OPTIMIZATION TECHNIQUE
•Compile time evaluation: Computation of such expression moved
to compile time rather than execution.
•Constant folding
•Constant propagation
•Common sub-expression elimination: A major source of
optimization is the identification of common sub-expression that
are present in the various expression with in program.
•Code movement optimization: It is a technique of moving a block
of code outside a loop if it would not have any difference it is
executed outside or inside the block.
•Strength reduction: It correspond to the replacement of an
operator requiring high executing time by an equivalent operator
requiring less processing time.
•Dead Code elimination: These are the portion of the program that
can never executed.
CODE OPTIMIZATION TECHNIQUE

Compile-Time Evaluation
• Expressions whose values can be pre - computed at
the compilation time
• Two ways:
– Constant folding
– Constant propagation
CODE OPTIMIZATION TECHNIQUE

Compile-Time Evaluation
• Constant folding: Evaluation of an expression
with constant operands to replace the
expression with single value
• Example:
area := (22.0/7.0) * r ** 2

area := 3.14286 * r ** 2
CODE OPTIMIZATION TECHNIQUE

Compile-Time Evaluation
• Constant Propagation: Replace a variable with
constant which has been assigned to it earlier.
Example:
pi :=3.14286 area = 3.14286 * r ** 2
area = pi * r ** 2
CODE OPTIMIZATION TECHNIQUE

Common Sub-expression Evaluation


• Identify common sub-expression present in different
expression, compute once, and use the result in all the places.
– The definition of the variables involved should not change

Example:
a := b * c temp := b * c
… a := temp
… …
x := b * c + 5 x := temp + 5
CODE OPTIMIZATION TECHNIQUE

Code Movement
Example: Unsafe code movement

if (a<b) then temp = x * 2


z=x*2 if (a<b) then
else z = temp
y = 10 else
y = 10
CODE OPTIMIZATION TECHNIQUE
Strength Reduction
• Replacement of an operator with a less costly one.
Example:

for i=1 to 10 do temp=5;


… for i=1 to 10 do
x=i*5 …
… x = temp
end …
temp=temp+5
end

• Typical cases of strength reduction occurs in address


calculation of array references.
• Applies to integer expressions involving induction variables
(loop optimization)
CODE OPTIMIZATION TECHNIQUE

Dead Code Elimination


• Eliminate those code statements which are either never
executes or unreachable or if executed there output is never
used.

If statement is
i=0
dead code because
If(i=1) this condition will
{ never satisfies
a=x+5
}
CODE OPTIMIZATION TECHNIQUE

Loop optimization Technique


In loops, especially in the inner loops, programs tend to spend the bulk of
their time. The running time of a program may be improved if the number
of instructions in an inner loop is decreased, even if we increase the
amount of code outside that loop.
Three techniques are important for loop optimization:
• Code motion/Code Movement
• Loop Unrolling
• Loop Jamming
CODE OPTIMIZATION TECHNIQUE

Loop optimization Technique


• Code Movement (Frequency
reduction ):
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.
CODE OPTIMIZATION TECHNIQUE

Loop optimization Technique


• 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.
CODE OPTIMIZATION TECHNIQUE

Loop optimization Technique


• 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.
CODE OPTIMIZATION TECHNIQUE

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.

Objectives of Peephole Optimization:


• To improve performance
• To reduce memory footprint
• To reduce code size
CODE OPTIMIZATION TECHNIQUE

Peephole optimization techniques:

• Redundant load and store elimination:


In this technique the redundancy is eliminated.
int add(int x) int add(int x)
{ {
int add(int x) int add(int x)
int y, z; int y; { {

y=10; y=10; int y=10; return x+10;

z=x+y y=x+y return x+y }

return z return y }

} }
CODE OPTIMIZATION TECHNIQUE

Peephole optimization techniques:


• Unreachable Code:
It is a part of a program code that is never accessed because of
program constructs. Programmers may have accidently written a
piece of code that can never be reached.
void add (int x)
{
return x+10;
printf(“value of x is %d ”, x);
}

In this statement “printf” statement will never be executed as program control


returns back before it execute hence “printf” statement can be removed.
CODE OPTIMIZATION TECHNIQUE

Peephole optimization techniques:


• Flow of Control Optimization:
These are instances in a code where the program control
jumps back forth without performing any significant task,
these jumps can be removed.

Goto L1 Goto L2
L1: Goto L2 L1: Goto L2 (can be deleted)
L2: a=b L2: a=b
CODE OPTIMIZATION TECHNIQUE

Peephole optimization techniques:


• Algebraic Simplification:
There are occasions where algebraic expression can be
made simple.
a=a+0 (can be replaced by “a” itself)

a=a+1 (can be easily replaced by Increment “a” )

a++
DIRECTED ACYCLIC GRAPH
In compiler design, a directed acyclic graph (DAG)
is an abstract syntax tree (AST) with a unique node
for each value.

Or

A directed acyclic graph (DAG) is a directed graph


that contains no cycle
PROPERTIES OF DAG
•The reachability relation in DAG forms a partial
order and any finite partial order may be
represented by a DAG using reachability.

•The transitive reduction and transitive closure are


both uniquely defined for DAG’s

•Every DAG has a topological ordering.


APPLICATIONS OF DAG
THE DAG IS USED IN:
•Determining the common subexpression
•Determining which names are used in the block
and computed outside the block.
•Determining which statements of the blocks could
have their computed value outside the block.
•Simplifying the list of quadruples by eliminating
the common subexpression and not performing
the assignment of the form x=y until and unless it
is must.
RULES FOR CONSTRUCTION OF DAG
In a DAG
RULE 1:
Leaf Node represents identifiers, names or constants.
Interior nodes represent operators

RULE 2:
While constructing DAG, there is a check made to find if there is an existing
node with the same children. A new node is created only when such a node
does not exist. This action allows us to detect common sub-expressions and
eliminate the re-computation of the same.

RULE 3:
The assignments of the form x=y must not be performed until and unless it is a
must.
RULES FOR CONSTRUCTION OF DAG
Example: 2.
t0 = a + b
t1 = t0 + c
d = t0 + t1
Solution:
1)

3.
CONSTRUCTION OF DAG
Example: Consider the following three address code statements.
a=b*c 2) 5)
d=b
e=d*c
b=e
f=b+c
g=f+d
Solution: 3)
1)
6)

4)
CONSTRUCTION OF DAG
Ex:
DAG
t1 = b - c
t2 = a * t1
t3 = a + t2
t4 = t1 * d
t5 = t3 + t4
CODE GENERATOR
Design Issues in Code generator
In the code generation phase, various issues can arises:
1. Input to the code generator
2. Target program
3. Memory management
4. Instruction selection
5. Register allocation
6. Evaluation order
CODE GENERATOR
Design Issues in Code generator
1. Input to the code generator
•The input to the code generator contains the intermediate
representation of the source program and the information of the
symbol table. The source program is produced by the front end.
•Intermediate representation has the several choices:
a) Postfix notation
b) Syntax tree
c) Three address code
•The code generation phase needs complete error-free
intermediate code as an input requires.
CODE GENERATOR
Design Issues in Code generator
2. Target program:
The target program is the output of the code generator. The
output can be:
a) Assembly language: It allows subprogram to be separately
compiled.
b) Relocatable machine language: It makes the process of
code generation easier (for linker or obj).
c) Absolute machine language: It can be placed in a fixed
location in memory and can be executed immediately
(executable file).
CODE GENERATOR
Design Issues in Code generator
3. Memory management
• During code generation process the symbol table entries have
to be mapped to actual p addresses and levels have to be
mapped to instruction address.
• Mapping name in the source program to address of data is co-
operating done by the front end and code generator.
• Local variables are stack allocation in the activation record
while global variables are in static area.
CODE GENERATOR
Design Issues in Code generator
4. Instruction selection:
• Nature of instruction set of the target machine should be
complete and uniform.
• When you consider the efficiency of target machine then the
instruction speed and machine idioms are important factors.
• The quality of the generated code can be determined by its
speed and size.
CODE GENERATOR
Design Issues in Code generator
5. Register allocation
• Register can be accessed faster than memory. The instructions
involving operands in register are shorter and faster than
those involving in memory operand.
• The following sub problems arise when we use registers:
• Register allocation: In register allocation, we select the set of
variables that will reside in register.
• Register assignment: In Register assignment, we pick the register that
contains variable.
• Certain machine requires even-odd pairs of registers for some
operands and result.
CODE GENERATOR
Design Issues in Code generator
6. Evaluation order
The efficiency of the target code can be affected by the order in which the
computations are performed.
Some computation orders need fewer registers to hold results of
intermediate than others.
CODE GENERATOR
Target Machine
Target Machine
• The target computer is a type of byte-addressable machine. It
has 4 bytes to a word.
• The target machine has n general purpose registers, R0, R1,....,
Rn-1. It also has two-address instructions of the form:
op source, destination
Where, op is used as an op-code and source and destination are
used as a data field.
• It has the following op-codes:
• ADD (add source to destination)
• SUB (subtract source from destination)
• MOV (move source to destination)

You might also like