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

Compiler Optimization

The document discusses machine-independent code optimization, highlighting inefficiencies in intermediate code generation and various optimization techniques to improve performance in terms of time, space, or power consumption. It lists specific optimization methods such as global common sub-expression elimination, loop unrolling, and function inlining, among others. Additionally, it provides examples and explanations of these techniques in practice.

Uploaded by

Naruto Hinata
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Compiler Optimization

The document discusses machine-independent code optimization, highlighting inefficiencies in intermediate code generation and various optimization techniques to improve performance in terms of time, space, or power consumption. It lists specific optimization methods such as global common sub-expression elimination, loop unrolling, and function inlining, among others. Additionally, it provides examples and explanations of these techniques in practice.

Uploaded by

Naruto Hinata
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Machine-independent Code Optimization

Intermediate code generation process introduces many


inefficiencies
Extra copies of variables, using variables instead of
constants, repeated evaluation of expressions, etc.
Code optimization removes such inefficiencies and
improves code
Improvement may be time, space, or power consumption
It changes the structure of programs, sometimes of beyond
recognition
Inlines functions, unrolls loops, eliminates some
programmer-defined variables, etc.
Code optimization consists of a bunch of heuristics and
percentage of improvement depends on programs (may be
zero also)

Y.N. Srikant Introduction to Optimizations


Examples of Machine-Independant Optimizations

Global common sub-expression elimination


Copy propagation
Constant propagation and constant folding
Loop invariant code motion
Induction variable elimination and strength reduction
Partial redundancy elimination
Loop unrolling
Function inlining
Tail recursion removal
Vectorization and Concurrentization
Loop interchange, and loop blocking

Y.N. Srikant Introduction to Optimizations


Bubble Sort Running Example

Y.N. Srikant Introduction to Optimizations


Control Flow Graph of Bubble Sort

Y.N. Srikant Introduction to Optimizations


GCSE Conceptual Example

Y.N. Srikant Introduction to Optimizations


GCSE on Running Example - 1

Y.N. Srikant Introduction to Optimizations


GCSE on Running Example - 2

Y.N. Srikant Introduction to Optimizations


Copy Propagation on Running Example

Y.N. Srikant Introduction to Optimizations


GCSE and Copy Propagation on Running Example

Y.N. Srikant Introduction to Optimizations


Constant Propagation and Folding Example

Y.N. Srikant Introduction to Optimizations


Loop Invariant Code motion Example

Y.N. Srikant Introduction to Optimizations


Strength Reduction

Y.N. Srikant Introduction to Optimizations


Induction Variable Elimination

Y.N. Srikant Introduction to Optimizations


Partial Redundancy Elimination

Y.N. Srikant Introduction to Optimizations


Unrolling a For-loop

Y.N. Srikant Introduction to Optimizations


Unrolling While and Repeat loops

Y.N. Srikant Introduction to Optimizations


Function Inlining

Y.N. Srikant Introduction to Optimizations


Tail Recursion Removal

Y.N. Srikant Introduction to Optimizations


Vectorization and Concurrentization Example 1

for I = 1 to 100 do {
X(I) = X(I) + Y(I)
}

can be converted to

X(1:100) = X(1:100) + Y(1:100)

or

forall I = 1 to 100 do X(I) = X(I) + Y(I)

Y.N. Srikant Introduction to Optimizations


Vectorization Example 2

for I = 1 to 100 do {
X(I+1) = X(I) + Y(I)
}

cannot be converted to

X(2:101) = X(1:100) + Y(1:100)


or equivalent concurrent code

because of dependence as shown below

X(2) = X(1) + Y(1)


X(3) = X(2) + Y(2)
X(4) = X(3) + Y(3)
...

Y.N. Srikant Introduction to Optimizations


Loop Interchange for parallelizability

Y.N. Srikant Introduction to Optimizations


Loop Blocking

Y.N. Srikant Introduction to Optimizations

You might also like