code optimization
code optimization
Method:
Step 1:
Step 2:
For case(i), create node(OP) whose right child is node(z) and left
child is node(y).
Output:
1. S1:= 4 * i
2. S2:= a[S1]
3. S3:= 4 * i
4. S4:= b[S3]
5. S5:= s2 * S4
6. S6:= prod + S5
7. Prod:= s6
8. S7:= i+1
9. i := S7
10. if i<= 20 goto (1)
Stages in DAG Construction:
A
Advantages of DAG
(b) x = 5.7
y = x/3.6
Evaluate x/3.6 as 5.7/3.6 at compile time.
1. c = a * b
2. x = a
3. till
4. d = x * b + 4
1. c = a * b
2. x = a
3. till
4. d=a*b+4
1. c = a * b
2. x = b
3. till
4. d = a * b + 4
1. do
2. {
3. item = 10;
4. valuevalue = value + item;
5. } while(value<100);
6.
7.
8. //This code can be further optimized as
9.
10. item = 10;
11. do
12. {
13. valuevalue = value + item;
14. } while(value<100);
1. i = 1
2. t = 4
3. {
4. while( t<40)
5. y = t;
6. t = t + 4;
7. }
Loop Optimization
Loop optimization is most valuable machine-independent
optimization because program's inner loop takes bulk to time of a
programmer.
1. Code motion
2. Induction-variable elimination
3. Strength reduction
1.Code Motion:
Code motion is used to decrease the amount of code in loop. This
transformation takes a statement or expression which can be
moved outside the loop body without affecting the semantics of
the program.
For example
In the while statement, the limit-2 equation is a loop invariant
equation.
2.Induction-Variable Elimination
Induction variable elimination is used to replace variable from
inner loop.
3.Reduction in Strength
o Strength reduction is used to replace the expensive
operation by the cheaper once on the target machine.
o Addition of a constant is cheaper than a multiplication. So
we can replace multiplication with an addition within the
loop.
o Multiplication is cheaper than exponentiation. So we can
replace exponentiation with multiplication within the loop.
Example:
1. while (i<10)
2. {
3. j= 3 * i+1;
4. a[j]=a[j]-2;
5. i=i+2;
6. }
1. s= 3*i+1;
2. while (i<10)
3. {
4. j=s;
5. a[j]= a[j]-2;
6. i=i+2;
7. s=s+6;
8. }
1. x = a + b;
2. x=6*3
o In this code, the first assignment of x is useless. The value
computer for x is never used in the program.
o At compile time the expression 6*3 will be computed,
simplifying the second assignment statement to x = 18;
1. a = 1;
2. b = 2;
3. c = 3;
4. if (....) x = a + 5;
5. else x = b + 4;
6. c = x + 1;
In this code, at line 3 the initial assignment is useless and x +1
expression can be simplified as 7.
But it is less obvious that how a compiler can discover these facts
by looking only at one or two consecutive statements. A more
global analysis is required so that the compiler knows the
following things at each point in the program:
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.
Optimized code:
y = x + 5;
i = y;
w = y * 3;
2. Constant folding:
The code that can be simplified by user itself, is simplified.
Initial code:
x = 2 * 3;
Optimized code:
x = 6;
3. 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;
4. Null sequences:
Useless operations are deleted.
5. Combine operations:
Several operations are replaced by a single equivalent operation.
Design Issues
In the code generation phase, various issues can arises:
2. Target program:
The target program is the output of the code generator. The
output can be:
16.5M
332
History of Java
c) Absolute machine language: It can be placed in a fixed
location in memory and can be executed immediately.
3. Memory management
o 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.
o Mapping name in the source program to address of data is
co-operating done by the front end and code generator.
o Local variables are stack allocation in the activation record
while global variables are in static area.
4. Instruction selection:
o Nature of instruction set of the target machine should be
complete and uniform.
o When you consider the efficiency of target machine then the
instruction speed and machine idioms are important factors.
o The quality of the generated code can be determined by its
speed and size.
Example:
The Three address code is:
1. a:= b + c
2. d:= a + e
1. MOV b, R0 R0→b
2. ADD c, R0 R0 c + R0
3. MOV R0, a a → R0
4. MOV a, R0 R0→ a
5. ADD e, R0 R0 → e + R0
6. MOV R0, d d → R0
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.
For example:
Consider the following division instruction of the form:
1. D x, y
Where,
y is the divisor
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
Code generator is used to produce the target code for three-
address statements. It uses registers to store the operands of the
three address statement.
Example:
Consider the three address statement x:= y + z. It can have the
following sequence of codes:
MOV x, R0
ADD y, R0
A code-generation algorithm:
The algorithm takes a sequence of three-address statements as
input. For each three address statement of the form a:= b op c
perform the various actions. These are as follows:
C++ vs Java
1. t:= a-b
2. u:= a-c
3. v:= t +u
4. d:= v+u
The order in which computations are done can affect the cost of resulting
object code. For example, consider the following basic block:
t1 : = a + b
t2 : = c + d
t3 : = e - t2
t4 : = t1 - t3
Algorithm:
1) while unlisted interior nodes remain do begin
2) select an unlisted node n, all of whose parents have been listed;
3) list n;
4) while the leftmost child m of n has no unlisted parents and is not a leaf
do
begin
5) list m;
6) n:=m
end
end