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

Code Optimization

The document outlines key concepts in Compiler Design, focusing on runtime environments and code optimization techniques. It discusses various storage allocation strategies such as static, stack, and heap, along with their pros and cons. Additionally, it covers objectives and types of code optimization, including machine-independent and machine-dependent methods aimed at improving program performance and reducing memory usage.

Uploaded by

Khushbu Jain
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)
2 views

Code Optimization

The document outlines key concepts in Compiler Design, focusing on runtime environments and code optimization techniques. It discusses various storage allocation strategies such as static, stack, and heap, along with their pros and cons. Additionally, it covers objectives and types of code optimization, including machine-independent and machine-dependent methods aimed at improving program performance and reducing memory usage.

Uploaded by

Khushbu Jain
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/ 63

Compiler Design

• Course Code: CS105101CS


• Unit 4: Runtime environment & Code
optimization
Outcome
§ Different Storage Allocation Strategies.
§ Understanding the purpose of code optimization.
§ Objectives of code optimization.
§ Different code optimization Techniques.
Memory Layout:

Stack

Heap

Static /variable

Machine Code

Process
Any element (variable, data structure, etc.) that is allocated
statically retains its memory allocation throughout the entire

Storage Allocation Strategies:


lifetime of the process.

This means the allocated memory does not get deallocated


until the process terminates.

Static: Note: Global arrays are static by


default
§ Allocation is done at compile time
§ Bindings do not change at runtime Pros: The element which is
§ One activation record per procedure provided with the static allocation
gets the life time as same as the
F1() process itself.
F1() F2()
F2() Cons:
F3() F3() § Recursion is not supported
F4() F4() § Size of the data objects must be
F5() known at compile time
F5() § Data structures can not be created
dynamically
Program Static Allocation
Storage Allocation Strategies:
Stack: Whenever a new activation begins, the activation record pushed
onto the stack whenever activation ends, the activation record is popped
off.
F1()
{

F2() Pros: Recursion is supported.


{
F3()
F3()
Cons:
F2() § Local variables can not be
{
F1() retrieved once activation ends.
}
Stack
}
}
Storage Allocation Strategies:
Heap: Allocation and deallocation done in any order.
Cons:
§ Heap management is overhead.

Machine Code

Process
Storage Allocation Strategies:
Heap: Allocation and deallocation done in any order.
Cons:
§ Heap management is overhead.

Hole

Machine Code
In C programming First fit is used

Process
Storage Allocation Strategies:
§ Permanent life time in case of static
allocation. Stack
§ Nested life time in case of stack
allocation.
§ Arbitrary life time in case of heap
Heap
allocation.
Static /variable

Machine Code

Process
Compiler Internal Architecture:
Lexical Analysis

Syntax Analysis Analysis Phase


Semantic Analysis
Intermediate Code
Generation
Code Optimization Synthesis Phase
Target Code
Generation
Objective of Code Optimization:
§ Optimization must be correct and must not change the meaning of program.

§ The compilation time must be kept reasonable.

§ The optimization process should not delay the overall compilation process.

§ Optimization should increase the speed and performance of the program.


Purpose of Code Optimization:
§ It is used to reduce the consumed memory space.

§ It is used to increase the compilation speed.

§ An optimized code facilitate re-usability.

Types of Code Optimization:


§ Machine independent:
§ Improves the intermediate code.
§ Machine Dependent:
§ It involves CPU registers and may have absolute memory references
rather than relative references.
§ It is performed after the target code has been generated.
Types of Code Optimization:
Machine Independent Optimization Machine dependent Optimization
1. Loop Optimizations
Loop Optimization:

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


for (int i = 0; i < 10; i++)
{
{
a = i * 2;
a = i * 2;
}
b=i+3
}
for (int j = 0;p j < 10; j++)
{
b = j + 3;
}
Types of Code Optimization:
Machine Independent Optimization Machine dependent Optimization

1. Loop Optimizations 1. Register Allocation


2. Constant Folding 2. Instruction Scheduling
3. Constant Propagation 3. Peephole Optimization
4. Operator Strength Reduction a) Redundant LOAD and STORE
5. Dead code elimination b) Flow Control Optimization
6. Common Subexpression Elimination c) Use of Machine Idioms
7. Algebraic Simplification
Loop Optimization:
§ The loops must be detected.
§ Loops are detected through Control Flow Analysis (CFA) using Program Flow
Graphs (PFG).
§ In order to determine PFG, we first need to detect the Basic Blocks.

Basic
v A Basic Block is a sequence of 3-address
Block 1
statements where control enters at the
beginning and leaves only from the end
Basic
without any jumps or halts. Block 2
Control Flow Analysis (CFA)
Basic
Block 3

Basic
Block 4
How to Find the Basic Blocks:
§ Find the Leader.

Identifying the Leader: Starting statement


Block 5
§ First statement is a Leader.

§ Statement that is target of a conditional or


Starting statement
unconditional GOTO is a Leader. Block 6

§ Statement that immediately follows a


conditional or unconditional GOTO is a
Leader
Illustration- Producing PFG:
§ HLL Three-address Code (TAC):
Fact (a){ 1. f = 1
int f = 1; 2. i = 2
for (i = 2; i < = a; i++) 3. if (i <= a) GOTO ________
f = f * i; 4. t1 = f * I
} 5. f = t1
6. t2 = i + 1
7. i = t2
8. GOTO_________
9. GOTO_________(Calling Program)
Illustration- Producing PFG:
§ HLL Three-address Code (TAC):
Fact (a){ 1. f = 1
int f = 1; 2. i = 2
for (i = 2; i < = a; i++) 3. if (i <= a) GOTO __(9)______
f = f * i; 4. t1 = f * I
} 5. f = t1
6. t2 = i + 1
7. i = t2
8. GOTO___(3)______
9. GOTO_________(Calling Program)
Illustration- Producing PFG:
Identifying the Leader: Three-address Code (TAC):
1. f = 1
§ First statement is a Leader. 2. i = 2
3. if (i <= a) GOTO __(9)______
§ Statement that is target of a conditional or 4. t1 = f * I BB1
unconditional GOTO is a Leader. 5. f = t1
6. t2 = i + 1
§ Statement that immediately follows a 7. i = t2
conditional or unconditional GOTO is a 8. GOTO___(3)______
Leader 9. GOTO_________(Calling Program)
Illustration- Producing PFG:
Identifying the Leader: Three-address Code (TAC):
1. f = 1
§ First statement is a Leader. 2. i = 2
3. if (i <= a) GOTO __(9)______ BB2
§ Statement that is target of a conditional or 4. t1 = f * I BB1
unconditional GOTO is a Leader. 5. f = t1
6. t2 = i + 1
§ Statement that immediately follows a 7. i = t2
conditional or unconditional GOTO is a 8. GOTO___(3)______
Leader 9. GOTO_________(Calling Program)
Illustration- Producing PFG:
Identifying the Leader: Three-address Code (TAC):
1. f = 1 BB3
§ First statement is a Leader. 2. i = 2
3. if (i <= a) GOTO __(9)______ BB2
§ Statement that is target of a conditional or 4. t1 = f * I BB1
unconditional GOTO is a Leader. 5. f = t1
6. t2 = i + 1
§ Statement that immediately follows a 7. i = t2
conditional or unconditional GOTO is a 8. GOTO___(3)______
Leader 9. GOTO_________(Calling Program)
Illustration- Producing PFG:
Identifying the Leader: Three-address Code (TAC):
1. f = 1 BB3
§ First statement is a Leader. 2. i = 2
3. if (i <= a) GOTO __(9)______ BB2
§ Statement that is target of a conditional or 4. t1 = f * I BB1
unconditional GOTO is a Leader. 5. f = t1
6. t2 = i + 1
§ Statement that immediately follows a 7. i = t2
conditional or unconditional GOTO is a 8. GOTO___(3)______
Leader 9. GOTO_________(Calling Program) BB4
Illustration- Producing PFG:
§ HLL
Fact (a){ PFG
int f = 1;
for (i = 2; i < = a; i++) BB1
f = f * i;
}
Three-address Code (TAC):
1. f = 1 BB1 BB2
2. i = 2
3. if (i <= a) GOTO __(9)______ BB2
4. t1 = f * I BB3 BB3
5. f = t1
6. t2 = i + 1
7. i = t2
8. GOTO___(3)______ BB4
9. GOTO_________(Calling Program) BB4
Loop Optimization Technique: Code Motion:
§ The number of statement within loop is reduced.
§ Also known as Frequency reduction.

While(i < 1000) t = sin(x) / cos(x);


{ While(i < 1000)
a = (sin(x) / cos(x)) * i; {
i++; a = t * i;
} i++;
}

Note: Moving the expression with computation outside, a.k.a Loop invariant method.
Loop Optimization Technique: Loop Unrolling:
§ If possible, eliminate the entire loop.

for ( int i = 0; i<5; i++) printf(“Hello”);


{ printf(“Hello”);
printf(“Hello”); printf(“Hello”);
} printf(“Hello”);
printf(“Hello”);
Loop Optimization Technique: Loop Jamming:
§ Combine the loop bodies.
§ Also known as Loop Fusion.

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


for (int i = 0; i < 10; i++)
{
{
a = i * 2;
a = i * 2;
}
b=i+3
}
for (int j = 0;p j < 10; j++)
{
b = j + 3;
}

Note: The opposite transformation is called Loop fission or loop distribution .


Loop Optimization Technique: Loop
Unswitching:
§ It lifts conditions out of loops, creating two loops .
for (int i = 0; i < 100; ++i) if(c)
{ {
if(c) for (int i = 0; i < 100; ++i)
{ f();
f(); }
{ else
else {
{ for (int i = 0; i < 100; ++i)
g(); g();
} }
}

Note: Also known as Loop splitting.


Loop Optimization Technique: Loop Peeling:
§ Here, problematic iteration is resolved separately before entering the loop.
for (int i = 0; i < 10; i++) a[0] = …..
{ for (int i = 1; i < 10; i++)
if(i == 0) {
a[i] = …… b[i] = ……
else }
b[i] = ……
}

Note: Also known as Loop splitting.


Types of Code Optimization:
Machine Independent Optimization Machine dependent Optimization

1. Loop Optimizations 1. Register Allocation


2. Constant Folding 2. Instruction Scheduling
3. Constant Propagation 3. Peephole Optimization
4. Operator Strength Reduction a) Redundant LOAD and STORE
5. Dead code elimination b) Flow Control Optimization
6. Common Subexpression Elimination c) Use of Machine Idioms
7. Algebraic Simplification
Constant Folding:
§ Evaluation of expression at compile time.
§ Applicable to the operands which are known to be constant.

A = 10 * 5 + 6 – b; A = 56 – b;
Types of Code Optimization:
Machine Independent Optimization Machine dependent Optimization

1. Loop Optimizations 1. Register Allocation


2. Constant Folding 2. Instruction Scheduling
3. Constant Propagation 3. Peephole Optimization
4. Operator Strength Reduction a) Redundant LOAD and STORE
5. Dead code elimination b) Flow Control Optimization
6. Common Subexpression Elimination c) Use of Machine Idioms
7. Algebraic Simplification
Constant Propagation:
§ If a variable is assigned a constant value, then subsequent uses of that variable
can be replaced by the constant as long as no intervening assignment has
changed the value of the variable.

a = 13.7; a/4.5 as 13.7/4.5


b = a/4.5;

j = 1;
GOTO L
if(j) GOTO L
Types of Code Optimization:
Machine Independent Optimization Machine dependent Optimization

1. Loop Optimizations 1. Register Allocation


2. Constant Folding 2. Instruction Scheduling
3. Constant Propagation 3. Peephole Optimization
4. Operator Strength Reduction a) Redundant LOAD and STORE
5. Dead code elimination b) Flow Control Optimization
6. Common Subexpression Elimination c) Use of Machine Idioms
7. Algebraic Simplification
Operator Strength Reduction:
§ It replaces an operator by less expensive one.

b=a*2 b = a << 1
Types of Code Optimization:
Machine Independent Optimization Machine dependent Optimization

1. Loop Optimizations 1. Register Allocation


2. Constant Folding 2. Instruction Scheduling
3. Constant Propagation 3. Peephole Optimization
4. Operator Strength Reduction a) Redundant LOAD and STORE
5. Dead code elimination b) Flow Control Optimization
6. Common Subexpression Elimination c) Use of Machine Idioms
7. Algebraic Simplification
Dead Code Elimination:
§ If an instruction's results is never used, the instruction is considered dead and
can be removed from the instruction stream.

………………..
………………..
t1 = t2 * t3;
……………......
………………..
Dead Code Elimination:
§ If an instruction's results is never used, the instruction is considered dead and
can be removed from the instruction stream.

………………..
………………..
t1 = t2 * t3;
……………......
………………..

§ If t1 holds results of function call, we can not eliminate the instruction.


Types of Code Optimization:
Machine Independent Optimization Machine dependent Optimization

1. Loop Optimizations 1. Register Allocation


2. Constant Folding 2. Instruction Scheduling
3. Constant Propagation 3. Peephole Optimization
4. Operator Strength Reduction a) Redundant LOAD and STORE
5. Dead code elimination b) Flow Control Optimization
6. Common Subexpression Elimination c) Use of Machine Idioms
7. Algebraic Simplification
Common Subexpression Elimination:
§ Two operations are common if they produce the same result. In such a case, it
is likely more efficient to compute the result once and reference it the second
time rather than re-evaluate it.

A=B+C A=B+C
D=2+B+3+C D =2+3+A
Types of Code Optimization:
Machine Independent Optimization Machine dependent Optimization

1. Loop Optimizations 1. Register Allocation


2. Constant Folding 2. Instruction Scheduling
3. Constant Propagation 3. Peephole Optimization
4. Operator Strength Reduction a) Redundant LOAD and STORE
5. Dead code elimination b) Flow Control Optimization
6. Common Subexpression Elimination c) Use of Machine Idioms
7. Algebraic Simplification
Algebraic Simplification:
§ Simplifications use algebraic properties or particular operator-operand
combinations to simplify expression.
§ These optimizations can remove useless instructions entirely via algebraic
identities.

A=A+0
B=B*1
Algebraic Simplification:
§ Simplifications use algebraic properties or particular operator-operand
combinations to simplify expression.
§ These optimizations can remove useless instructions entirely via algebraic
identities.

A=A+0
B=B*1
Types of Code Optimization:
Machine Independent Optimization Machine dependent Optimization

1. Loop Optimizations 1. Register Allocation


2. Constant Folding 2. Instruction Scheduling
3. Constant Propagation 3. Peephole Optimization
4. Operator Strength Reduction a) Redundant LOAD and STORE
5. Dead code elimination b) Flow Control Optimization
6. Common Subexpression Elimination c) Use of Machine Idioms
7. Algebraic Simplification
Register Allocation:
§ The most effective optimization for all architectures.

§ Solely depends on the number of available registers.

§ Types:
Register allocation is the process of assigning
a. Local Allocation variables to CPU registers during compilation.
z = (a + b) * (c - d);

b. Global Allocation MOV a, R1


MOV b, R2
ADD R1, R2 # T1 = a + b
MOV c, R1
MOV d, R2
SUB R1, R2 # T2 = c - d
MOV T1, R3
MOV T2, R1
MUL R3, R1 # z = T1 * T2
Types of Code Optimization:
Machine Independent Optimization Machine dependent Optimization

1. Loop Optimizations 1. Register Allocation


2. Constant Folding 2. Instruction Scheduling
3. Constant Propagation 3. Peephole Optimization
4. Operator Strength Reduction a) Redundant LOAD and STORE
5. Dead code elimination b) Flow Control Optimization
6. Common Subexpression Elimination c) Use of Machine Idioms
7. Algebraic Simplification
Instruction Scheduling:
§ Using this the pipelining capability of the architecture can be used effectively.

§ Instructions can be placed in the delay slots (like NOOP).


Instruction scheduling is a compiler optimization that reorders instructions in a program to improve
performance, particularly on machines with pipelines.
Peephole Optimization: Redundant LOAD &
STORE
x=y+z a=b+c MOV c, R0
d=a+e ADD b, R0
MOV R0, a
MOV y, R0
ADD z, R0
MOV R0, x MOV a, R0
ADD e, R0
MOV R0, d
Peephole Optimization: Redundant LOAD &
STORE
x=y+z a=b+c MOV c, R0
d=a+e ADD b, R0
MOV R0, a
MOV y, R0
Redundant
ADD z, R0
MOV R0, x MOV a, R0
ADD e, R0
MOV R0, d
Peephole Optimization: Flow Control
L1: GOTO L2 L1: GOTO L4
§ Avoid jumps on jumps:

L2: GOTO L3
L4:

L3: GOTO L4

#define x 0
L4: If(x)
{
§ Eliminate Dead Code:

}
Peephole Optimization: Flow Control
L1: GOTO L2 L1: GOTO L4
§ Avoid jumps on jumps:

L2: GOTO L3
L4:

L3: GOTO L4

#define x 0
L4: If(x)
{
§ Eliminate Dead Code:

}
Peephole Optimization: Use of Machine Idioms

i=i+1 MOV i, R0
ADD 1, R0
MOV R0, i

INC i
Liveness Analysis
What is Liveness?
§ A variable is live if its value will be used before it gets overwritten.
Why is it important?
§ Register Allocation: It helps in deciding which variables to keep in registers
and which to store in memory to optimize performance.
§ Dead code Elimination: It can be used to identify and remove code that
compute values that are never used.
§ Code Scheduling: It is used to reorder instructions to minimize stalls and
improve instruction-level parallelism.
Illustration- Producing CFA:
§ HLL
Fact (a){ PFG
int f = 1;
for (i = 2; i < = a; i++) BB1
f = f * i;
}
Three-address Code (TAC):
1. f = 1 BB1 BB2
2. i = 2
3. if (i <= a) GOTO __(9)______ BB2
4. t1 = f * I BB3 BB3
5. f = t1
6. t2 = i + 1
7. i = t2
8. GOTO___(3)______ BB4
9. GOTO_________(Calling Program) BB4
Liveness Analysis: Algorithm
Input: Program Flow Graph (PFG)
Output: Liveness Information

-- IN[B] (Set of variables that are live at the beginning of the block)

-- OUT[B] (Set of variables that are live after the block)

-- DEF/KILL[B] (Set of variables that are defined/killed in the block)

-- USE/GEN[B] (Set of variables that are used / generated in the block)


Liveness Analysis: Algorithm
1. Initialization: In and OUT sets are initially empty.
2. Worklist Initialization: Create a Worklist containing all the Basic blocks of the
CFG.
3. Iterative Data Flow Analysis: While the Worklist is not empty, perform the
following steps for each basic block.
§ Calculate IN[B] = USE[B] U (OUT[B] – DEF[B])
§ Calculate OUT[B] = U IN[S]
4. Final Liveness Information: After the analysis has converged, the IN sets
represent the live variables at the entry points of each block.
Q) A variable x is said to be live at a statement Si in a program if the following three
conditions hold simultaneously.
1. There exist a statement Si that uses x.
2. There is a path from Si to Si in the flow graph corresponding to the program.
3. The path has no intervening assignment to x including at Si and Si.

p=q+r
The variables which are live both at the statement
1 s=p+q
in basic block 2 and at the statement in basic
u=s*v
block 3 of the above control flow graph are:
2 3
a. p, s, u
v=r+u q=s*u b. r, s, u
c. r, u
d. q, v
q=v+r
4
Basic Block USE DEF IN OUT IN OUT IN OUT

1 q, r, v p, s, u q, r, v r, u, s r, q, v r, u, s, v r, v, q r, u, s, v
2 r, u v r, u v, r r, u r, v r, u r, v
3 s, u q s, u v, r v, r, s, u r, v r, v, s, u r, v
4 v, r q v, r q, r, v r, v r, q, v r, v r, v, q
The variables which are live both at the statement
in basic block 2 and at the statement in basic
block 3 of the above control flow graph are:

a. p, s, u
b. r, s, u
c. r, u
d. q, v

Basic Blocks p q r s u v

3
Directed Acyclic Graph
§ DAG represents the structure of basic block.
1. In DAG, internal node represent operators.
2. Leaf node represents identifiers and constants.
3. Internal node also represents result of expression.

§ Applications of DAG
1. Determine the common sub expression.
2. Determine which names are used inside the block and computed outside the
block.
3. Determine which statement of the block could have their computed value
outside the block.
4. Simplifying the list of quadruples by eliminating the common subexpressions.
Construction of DAG for the Basic Block
1. Construct DAG for the expression: a + a * (b – c) + (b - c) * d

+
* *
a
- d

b c
Construction of DAG for the Basic Block
2. Construct DAG for the expression:
T0 = a + b
T1 = T0 + c
d = T0 + T1
Construction of DAG for the Basic Block
3. Construct DAG for the expression:
T1 = a + b
T2 = a - b
T3 = T1 * T2
T4 = T1– T3
T5 = T4 + T3

You might also like