0% found this document useful (0 votes)
15 views12 pages

Code Optimization-I

Code optimization transforms intermediate code to reduce memory usage and increase machine code speed while preserving correctness. It includes machine-independent optimizations like loop optimization and machine-dependent optimizations. Loop optimization identifies loops using control flow analysis and basic block identification. It then applies techniques like code motion to reduce loop overhead by moving invariant computations outside loops.

Uploaded by

Aakash D.V
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
15 views12 pages

Code Optimization-I

Code optimization transforms intermediate code to reduce memory usage and increase machine code speed while preserving correctness. It includes machine-independent optimizations like loop optimization and machine-dependent optimizations. Loop optimization identifies loops using control flow analysis and basic block identification. It then applies techniques like code motion to reduce loop overhead by moving invariant computations outside loops.

Uploaded by

Aakash D.V
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

Code Optimization

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

Machine Dependent Optimization


Machine Independent Optimization
● Done after the target code has
● Improve intermediate code to
been generated
generate better target code
● Depends on the target machine
● Does not require CPU register or
architecture
absolute memory locations
● Involves CPU registers and may
● Example:
have absolute memory references
○ Loop optimization
● Example:
○ Folding
○ Peephole Optimization
○ Dead code elimination
○ Register Allocation, etc
○ Strength reduction, etc

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

for (i = 0; i<10; i++) (6) t4 = addr (a)

for j =0; j<10; j++) (7) x =t4[t3] Corresponding


Three-Address Code
x =a[i, j ] ; (8) j=j+1

(9) if j < 10 goto (3)

Source Code
(10) i=i+1

(11) if i < 10 goto (2) cd_sk@vit.ac.in 05/07/2023 8


(1) i=0
Basic Block - Example leader
Assumptions: leader (2) j=0
❖ The matrix a is stored in row-major
leader (3) t1 = 10 * i
form
❖ Array elements take 8 bytes each (4) t2 = t1 + j

(5) t3 = 8 * t2

for (i = 0; i<10; i++) (6) t4 = addr (a)

for j =0; j<10; j++) (7) x = t4[t3]

x =a[i, j ] ; (8) j=j+1

(9) if j < 10 goto (3)

Source Code leader (10) i=i+1

Corresponding (11) if i < 10 goto (2)


Three-Address Code cd_sk@vit.ac.in 05/07/2023 9
Control Flow Analysis - Flow Graphs

● The nodes of the flow graph are the basic blocks.


● There is an edge from block B to block C if and only if it is possible for
the first instruction in block C to immediately follow the last instruction in
block B.
● There are two ways that such an edge could be justified:
○ There is a conditional or unconditional jump from the end of B to
the beginning of C .
○ C immediately follows B in the original order of the three-address
instructions, and B does not end in an unconditional jump.

cd_sk@vit.ac.in 05/07/2023 10
Flow Graphs (1) i=0 (1) i=0 B1

(2) j=0

(3) t1 = 10 * i (2) j=0 B2

(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)

(10) i=i+1 (11) i=i+1 B4


(11) if i < 10 goto (2) (12) if i < 10 goto (2)
11
cd_sk@vit.ac.in 05/07/2023
Loop Optimization - Code Motion
● A statement or expression, which can be moved outside the loop body without
affecting the semantics of the program, is moved outside the loop.

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

You might also like