Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Code Optimization

CSC 3205: Compiler Design

Marriette Katarahweire

CSC 3205: Compiler Design 1/10


Code Optimization

Optimization is a program transformation technique, which


tries to improve the code. Objectives:
Reduce execution speed
Reduce code size
Code optimization is any method of code modification to
improve code quality and efficiency while preserving semantics.
A program may be optimized so that it becomes a smaller
size, consumes less memory, executes more rapidly, or
performs fewer input/output operations.
High-level general programming constructs are replaced by
very efficient low-level programming codes.

CSC 3205: Compiler Design 2/10


Code Optimization Rules

A code optimizing process must follow the three rules given


The output code must not, in any way, change the meaning of
the program.
Optimization should increase the speed of the program and if
possible, the program should demand less number of
resources.
Optimization should itself be fast and should not delay the
overall compiling process.

CSC 3205: Compiler Design 3/10


Code Optimization

Efficient code starts with intelligent decisions by the programmer


and efforts for an optimized code can be made at various levels of
compiling the process.
At the beginning, users can change/rearrange the code or use
better algorithms to write the code.
After generating intermediate code, the compiler can modify
the intermediate code by address calculations and improving
loops.
While producing the target machine code, the compiler can
make use of memory hierarchy and CPU registers.

CSC 3205: Compiler Design 4/10


Where and When to Optimize

Some techniques are applied to the intermediate code, to


streamline, rearrange, compress, etc. in an effort to reduce the
size of the abstract syntax tree or shrink the number of TAC
instructions
Others are applied as part of final code generation - choosing
which instructions to emit, how to allocate registers and
when/what to spill, and the like.
Other optimizations may occur after final code generation,
attempting to re-work the assembly code itself into something
more efficient.

CSC 3205: Compiler Design 5/10


Types and Levels of Optimization

Optimization can be performed by automatic optimizers, or


programmers.
An optimizer is either a specialized software tool or a built-in
unit of a compiler (the optimizing compiler). Modern
processors can also optimize the execution order of code
instructions.
Optimizations are classified into high-level and low-level
optimizations.
High-level optimizations are usually performed by the
programmer, who handles abstract entities (functions,
procedures, classes, etc.) and keeps in mind the general
framework of the task to optimize the design of a system.

CSC 3205: Compiler Design 6/10


Types and Levels of Optimization

Optimizations performed at the level of elementary structural


blocks of source code - loops, branches, etc. - are usually
referred to as high-level optimizations too, while some authors
classify them into a separate (”middle”) level.
Low-level optimizations are performed at the stage when
source code is compiled into a set of machine instructions,
and it is at this stage that automated optimization is usually
employed.

CSC 3205: Compiler Design 7/10


Machine (in)dependent Optimization

Machine independent: the compiler takes in the intermediate


code and transforms a part of the code that does not involve
any CPU registers and/or absolute memory locations
Machine-dependent optimization is done after the target code
has been generated and when the code is transformed
according to the target machine architecture

CSC 3205: Compiler Design 8/10


Classification of optimizations

Time of application: During which phase of compilation


process is an optimization applied?
Scope of application: What is the area over which
optimizations applied? (Local, global, inter-procedural?)
Time of application: An ideal optimizing compiler structure:

CSC 3205: Compiler Design 9/10


Time of application

Source language optimizations: target independent.


Intermediate code generation: majority of machine
independent optimizations performed here.
Final code generation: (Machine dependent optimizations)
Interaction between various phases of optimization: one phase
may facilitate other phase. So order of application important.

CSC 3205: Compiler Design 10/10

You might also like