Code Optimization-I
Code Optimization-I
Date - 05/07/2023
cd_sk@vit.ac.in
What is Code Optimization?
● Transforming the intermediate code so that
○ The memory usage reduces
○ Machine code runs faster
● Code optimization should
○ Preserve the original meaning of code - correctness
○ Enhance the performance and speed of the program
○ Have a check on compile time
● Optimization also reduces the space usage in the memory
cd_sk@vit.ac.in 05/07/2023 2
Types of Code Optimization
cd_sk@vit.ac.in 05/07/2023 3
Loop Optimization
● Reduce overhead associated with loops in a program code
● Reduces execution time and cache performance
● Some of the the loop optimization techniques are
○ Code Motion (Frequency Reduction) - to reduce the amount of code in
the loop
■ Similar to Loop Invariant Computations
○ Loop unrolling - to reduce number of control transfers
○ Loop Jamming - to combine two or more loops in a single loop
○ Elimination of induction variables - change p=i*w+b to p=b+w, when w,b
invariant
cd_sk@vit.ac.in 05/07/2023 4
Loop Optimization - Finding Loops
● To optimize loops, we need to find them!
● Find loops using Control-Flow-Analysis (CFA) by creating Flow-Graph
○ - how control of a program flows
● CFA works upon finding basic blocks
cd_sk@vit.ac.in 05/07/2023 5
Finding Loops - Basic Block
● Basic Block is a Sequences of three-address instructions that are always
executed sequentially
● The flow of control can only enter the basic block through the first instruction
in the block.
○ no jumps into the middle of the block
● Control will leave the block without halting or branching, except possibly at
the last instruction in the block.
● Basic blocks become the nodes of a flow graph
○ where edges indicate which blocks can follow which other blocks
cd_sk@vit.ac.in 05/07/2023 6
Finding Basic Block - Algorithm
● INPUT: A sequence of three-address instructions.
● OUTPUT: A list of the basic blocks for that sequence in which each instruction is
assigned to exactly one basic block.
METHOD:
● Step 1: FInd instructions in the intermediate code that are leaders - first instructions in some
basic block.
○ The instruction just past the end of the intermediate program is not included as a leader.
○ The rules for finding leaders are:
■ First three-address instruction in the intermediate code is a leader.
■ Any instruction that is the target of a conditional or unconditional jump is a leader.
■ Any instruction that immediately follows a conditional or unconditional jump is a
leader.
● Step 2: for each leader, its basic block consists of itself and all instructions up to but not
including the next leader or the end of the intermediate program. cd_sk@vit.ac.in 05/07/2023 7
(1) i=0
Basic Block - Example
Assumptions: (2) j=0
❖ The matrix a is stored in row-major
(3) t1 = 10 * i
form
❖ Array elements take 8 bytes each (4) t2 = t1 + j
(5) t3 = 8 * t2
Source Code
(10) i=i+1
(5) t3 = 8 * t2
cd_sk@vit.ac.in 05/07/2023 10
Flow Graphs (1) i=0 (1) i=0 B1
(2) j=0
(4) t2 = t1 + j
(5) t3 = 8 * t2 (3) t1 = 10 * i
(4) t2 = t1 + j
(6) t4 = addr (a) (5) t3 = 8 * t2
(7) x =t4[t3] (6) t4 = addr (a) B3
If there is a cycle in (7) x = t4[t3]
the flow graph then (8) j=j+1 (9) j=j+1
there is a loop in the (10) if j < 10 goto (3)
code (9) if j < 10 goto (3)
After optimization:
Before optimization:
t = x*y;
while(i<100)
while(i<100)
{
{
a = x*y + i;
a = t + i;
i++;
i++;
}
}
cd_sk@vit.ac.in 05/07/2023 12