0% found this document useful (0 votes)
62 views58 pages

Chapter 8 Code Optimization and Code Generation

Uploaded by

Yohannes Dereje
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
62 views58 pages

Chapter 8 Code Optimization and Code Generation

Uploaded by

Yohannes Dereje
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 58

Principles of Compiler

Design
Chapter 8: Code optimization and code
generation
Contents
• Introduction to code optimization

• The principle sources of optimization:


– Loop optimization: Common Sub expressions, Copy propagation

• The DAG representation of basic blocks, Value numbers


and algebraic laws, Global data-flow analysis
• Code generation:
– The Target Machine , Object programs, Problems in code
generation, A machine model, A simple code generator, Register
allocation and assignment, Code generation from DAG’s,
Peephole optimization
Introduction to code optimization

• The code generated by the compiler can be made


faster or take less space or both. For that, some
transformations can be applied on the code, and
is called optimization or optimization
transformations.
• Compilers that can perform optimizations are
called optimizing compilers.
• Code optimization is an optional phase and it
must not change the meaning of the program.
Need For Optimization Phase in Compiler

• Code produced by a compiler may not be perfect


in terms of execution speed and memory space.
• Optimization by hand takes much more effort and
time.
• Machine level details like instructions and
addresses are not known to the programmer.
• Advanced architecture features like instruction
pipeline requires optimized code.
• Structure reusability and maintainability of the
Criteria for Code Optimization
• It should preserve the meaning of the program
i.e., it should not change the output or produce
error for a given input.
• Eventually it should improve the efficiency of the
program by a reusable amount. Sometimes it
may increase the size of the code or may slow the
program slightly but it should improve the
efficiency.
• It must be worth with the effort, i.e., the effort put
on optimization must be worthy when compared
Introduction to code optimization
Types of Optimization:
• There are two types of optimization:

1. Machine dependent optimization – run only in


particular machine.
2. Machine independent optimization – used for
any machine
Types of Optimization:
• Optimization can be done in 2 phases:

a) Local optimization:
– Transformations are applied over a small
segment of the program called basic block, in
which statements are executed in sequential
order.
b) Global optimization
– Transformations are applied over a large
segment of the program like loop, procedures,
functions etc.
Types of Optimization - Basic Block

• Basic block is a sequence of consecutive 3


address statements which may be entered only at
the beginning and statements are executed in
sequence without halting or branching.
• To identify basic block, we have to find leader
statements.
Flow Graphs
Flow Graphs
Flow Graphs
Flow Graphs
Principle Sources of Optimization
• There are two types of basic block optimizations:
1. Structure preserving transformations

2. Algebraic transformations
Principle Sources of Optimization
Principle Sources of Optimization

– We can avoid re-computing the expression if we can use


the previously computed value.
Principle Sources of Optimization
Principle Sources of Optimization- Copy
Propagation
Principle Sources of Optimization
Principle Sources of Optimization
Principle Sources of Optimization

• Loop optimization techniques are:


– Code Motion (or Code movement)
– Induction Variables
– Strength Reduction
– Loop jamming (merging/combining)
– Loop unrolling
Principle Sources of Optimization
Principle Sources of Optimization
Principle Sources of Optimization
Principle Sources of Optimization
Principle Sources of Optimization
Principle Sources of Optimization
Loop Jamming:
• If the operations performed can be done in a
single loop then, merge or combine the loops.

This can be converted to,


Principle Sources of Optimization - Algebraic
Transformations:
Directed Acyclic Graph
(Many of the structure preserving transformations
can be done with the help of Directed Acyclic Graphs
- DAG)
Directed Acyclic Graph

Example-
Directed Acyclic Graph
Directed Acyclic Graph
Directed Acyclic Graph
CODE GENERATION
Issues in the Design of a Code Generator:
Input to Code Generator

• The input to the code generator consists of the


intermediate representation of the source program
produced by the front end, together with information
in the symbol table.
• There are several choices for the intermediate
language including postfix notation, three address
representation such as quadruple, virtual machine
representations such as stack machine code, and
graphical representations such as syntax trees and
DAGS.
Target Programs

• The output of the code generator is the target program.


This output may take on a variety of forms- absolute
machine language, relocatable machine language or
assembly language.
• Producing an absolute machine language as output has
the advantage that it can be placed in a fixed location
in memory and intermediate executed.
• Producing a relocatable machine language program as
output allows subprograms to be compiled separately.
A set of relocatable object modules can be linked
together and loaded for execution by a linking loader.
Target Programs

• Producing an assembly language program as output


makes the process of code generation somewhat easier.
• The instruction set architecture of the target machine
has a significant impact on the difficulty of constructing
a good code generator that produces high quality
machine code.
• The most common target machine architectures are
– RISC (reduced instruction set computer),

– CISC (complex instruction set computer) and

– stack based.
Target Programs

• The RISC machine has many registers, three-address


instructions, simple addressing modes and a relatively
simple instruction set architecture.
– In contrast, a CISC machine has few registers, two-address
instructions, a variety of addressing modes, several register
classes, variable length instructions.

• In stack based machine, operations are done by pushing


operands onto the stack and then performing the
operations on the operands at the top of the stack.
– To achieve high performance, the top of the stack is typically
kept in registers.
Memory Management

• Mapping of variable names to address is done co-


operatively by the front end and code generator.
Name and width are obtained from symbol table.
– Width is the amount of storage needed for that
variable. Each three-address code is translated to
addresses and instructions during code generation.
A relative addressing is done for each instruction.
Instruction Selection

• The code generator must map the IR (Intermediate


Representation) into a code sequence that can be
executed by the target machine. The complexity of
performing this mapping is determined by factors such
as,
– the level of the IR
– the nature of the instruction-set architecture
– the desired quality of the generated code
Instruction Selection
• The nature of the instruction set of the target machine has
a strong effect on the difficulty of instruction selection.
– Uniformity and completeness of the instruction set are
important factors.
• Instruction speed and machine idioms are other important
factors. If we do not care about the efficiency of the target
program, instruction selection is straightforward.
• For each common three-address statement, a general code
can be designed.
Register allocation:
Choice of Evaluation Order:

• The order of evaluation can affect the efficiency of target


code. Some order requires fewer registers and instructions
than others.
• Picking the best order is an NP-complete problem. This can
be solved up to an extent by code optimization in which the
order of instruction may change.
Target Machine
Target Machine
Target Machine

• The source and destination of an instruction are specified


by combining registers and memory locations with address
modes.
• The address modes together with their assembly-language
forms and associated costs are as follows:
Target Machine
Simple Code Generator

• Code generator is used to produce the target code for


three-address statements.
• It uses registers to store the operands of the three address
statement
Code Generation Algorithm

• The algorithm takes a sequence of three-address


statements as input. For each three address statement of
the form x := y op z perform the various actions. These are
as follows:
Code Generation Algorithm

• Code generator uses getReg function to determine the status of


available registers and the location of name values. getReg
works as follows:
– If variable Y is already in register R, it uses that register.

– Else if some register R is available, it uses that register.

– Else if both the above options are not possible, it chooses a


register that requires minimal number of load and store
instructions. That is, it takes a register which is already
occupied, move its contents into some memory using the
instruction MOV R, M. and then uses the register R.
Exercise

You might also like