Code Optimization
Code Optimization
Stack
Heap
Static /variable
Machine Code
Process
Any element (variable, data structure, etc.) that is allocated
statically retains its memory allocation throughout the entire
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
§ The optimization process should not delay the overall compilation process.
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.
Note: Moving the expression with computation outside, a.k.a Loop invariant method.
Loop Optimization Technique: Loop Unrolling:
§ If possible, eliminate the entire loop.
A = 10 * 5 + 6 – b; A = 56 – b;
Types of Code Optimization:
Machine Independent Optimization Machine dependent Optimization
j = 1;
GOTO L
if(j) GOTO L
Types of Code Optimization:
Machine Independent Optimization Machine dependent Optimization
b=a*2 b = a << 1
Types of Code Optimization:
Machine Independent Optimization Machine dependent Optimization
………………..
………………..
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;
……………......
………………..
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
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
§ Types:
Register allocation is the process of assigning
a. Local Allocation variables to CPU registers during compilation.
z = (a + b) * (c - d);
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)
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