Unit 5
Unit 5
CD : COMPILER DESIGN
What is Advantages
Optimization Optimized code has faster execution speed
Optimized code utilizes the memory efficiently
Optimized code gives better performance
Optimization
Technique
Function Preserving
Common Sub Expression Elimination
Constant Folding
Copy Propagation
Dead Code Elimination
A = PI
Elimination If (i==1)
{
x=x+i;
}
It is a simple and effective technique for locally improving
target code.
This technique is applied to improve the performance of
the target program by examining the short sequence of
target instruction (called peephole or window) and
replacing these instructions by shorter or faster sequence
Peephole whenever possible.
Optimization Peephole is a small, moving window on the target
program.
Goals :
Improve Performance
Reduce code size
Redundant Load and Store can be eliminated.
Example
MOV R0 , x
MOV x , R0
Redundant
Instruction We can eliminate second instruction.
Elimination
Unnecessary jumps can be eliminated in either the
intermediate code or the target code by the following
types of peephole optimization.
Goto L1 Goto L2
------ ------
L1 : Goto L2 L1 : Goto L2
Flow of
Control
If there are no jump to L1 then we can eliminate L1:Goto
Optimization L2
Algebraic
Specification
Certain machine instructions are cheaper than other.
In order to improve the performance of the intermediate
code we can replace these instructions by equivalent
cheaper instructions.
For example, x^2 can be computed easily by x*x, instead
of using exponential function.
Reduction in
Strength
The target instructions have equivalent machine
instructions for performing some operations.
Hence we can replace these target instructions by
equivalent machine instruction in order to improve the
efficiency.
Example : Some machines have auto-increment and auto-
Machine decrement addressing modes.
Idioms These modes can be used in code for the statements :
i=i+1 or i=i-1.
In order to do code optimization and a good job of
code generation , a compiler needs to collect
information about the program as a whole and to
distribute this information to each block in the flow
graph.
Global Data Data flow equations are the equations representing
Flow Analysis the expressions that are appearing in the flow
graph.
Data flow information can be collected by setting up
and solving systems of equations that relate
information at various point in a program.
A typical equation has the form
Out[S] = gen[S] ꓴ (in[S] – kill[S])
And can be read as
Global Data “the information at the end of a statement is either
Flow Analysis generated within the statement, or enters at the
beginning and is not killed as control flows through
the statement”.
The details of how data-flow analysis are set up and solved
depend on three factors.
1. The notion of generating and killing depend on the
desired information. i.e. for some problem, instead of
proceeding along with the flow of control and defining
Global Data out[S] in terms of in[S], we need to proceed backwards
and defines in[S] in terms of out[S].
Flow Analysis
2. Since data flows along control paths, data-flow analysis
is affected by the control constructs in a program.
3. There are subtleties that go along with such statements
as procedure calls, assignment through pointer and even
assignments to array variables.
A program point containing the definition is called
Definition Point.
A program point at which a reference to a data item
is made is called reference point.
A program point at which some evaluating
expression is given is called evaluation point.
Data Flow
Properties W1 : x=3 Definition
point
W2 : y=x Reference
point
W3 : z = x * y Evaluation
point
1. The expression x+y is said to be available at its evaluation
point
2. Neither of two operands get modified before their use.
Reaching Definition:
Instruction MOV b, R0
Selection ADD c, R0
MOV R0, a
MOV a, R0
ADD e, R0
MOV R0, d
Here the fourth statement is redundant, so we can eliminate that
statement.
Instructions containing register operands are usually shorter
and faster than that of using in memory or involving
operands in memory.
The use of registers is often subdivided into two sub
problems:
1. Register allocation: we select the set of variables that will
reside in registers at a point in the program.
Register
2. Register assignment: select a specific register that a
Allocation variable reside in.
MOV a, R0
T1 = a + b
ADD b, R0
T2 = c + d
MOV R0, T1
T3 = e * t2
Choice of T4 = T1 – T3
MOV c, R1
MOV c, R0
evaluation ADD d, R1
ADD d, R0
(Example) reorder MOV e, R0
MOV e, R1
MUL R1, R0
T2 = c + d MUL R0, R1
MOV T1, R1
T3 = e * T2 MOV a, R0
SUB R0, R1
T1 = a + b ADD b, R0
MOV R1, T4
T4 = T1 – T3 SUB R1, R0
MOV R0, T4
The most important criterion for a code generator is that it
produces correct code.
Approaches Correctness takes on special significance because of the number
to code of special cases that code generator might face.
Leader 1) f = 1;
Block 1
2) i = 2;
Register
Assignment
for Outer Following criteria should be adopted for register assignment for
Loop outer loop,
If a is allocated in loop L2 then it should not be allocated in L1 - L2.
If a is allocated in L1 and it is not allocated in L2 then store a on
entrance to L2 and load a while leaving L2.
If a is allocated in L2 and not in L1 then load a on entrance of L2
and store a on exit from L2.
The graph coloring works in two passes. The working is as given
below,
In the first pass the specific machine instruction is selected for
register allocation. For each variable a symbolic register is
allocated.
In the second pass the register inference graph is prepared.
Register In register inference graph each node is a symbolic registers and
Allocation an edge connects two nodes where one is live at a point where
other is defined.
for Graph Then a graph coloring technique is applied for this register
Coloring inference graph using k- color.
The k-colors can be assumed to be number of assignable registers.
In graph coloring technique no two adjacent nodes can have same
color. Hence in register inference graph using such graph coloring
principle each node (actually a variable) is assigned the symbolic
registers so that no two symbolic registers can interfere with each
other with assigned physical registers.
DAG Representation
of Basic Block
We assume the three address statement could of following types,
Case (i) x:=y op z
Case (ii)x:=op y
Case (iii) x:=y
With the help of following steps the DAG can be constructed.
Step 1: If y is undefined then create node(y). Similarly if z is
Algorithm for undefined create a node(z)
Constructing Step 2:
DAG Case(i) create a node(op) whose left child is node(y) and
node(z) will be the right child. Also check for any common sub
expressions.
Case(ii) determine whether is a node labeled op, such node
will have a child node(y).
Case(iii) node n win be node(y).
Step 3: Delete x from list of identifiers for node(x). Append x to
the list of attached identifiers for node n found in 2.
DAG
Representation
of Basic Block
The DAGs are used in:
Determining the common sub-expressions.
Determining which names are used inside the block and
Applications computed outside the block.