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

Chapter 6 Code Optimization and Code Generation Compress.pdf (4)

Chapter 6 of the document discusses code optimization and code generation in compiler design. It covers the need for optimization, types of optimization (machine dependent and independent), and various techniques such as loop optimization and algebraic transformations. Additionally, it addresses code generation processes, including instruction selection, memory management, and the impact of target machine architecture on code quality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Chapter 6 Code Optimization and Code Generation Compress.pdf (4)

Chapter 6 of the document discusses code optimization and code generation in compiler design. It covers the need for optimization, types of optimization (machine dependent and independent), and various techniques such as loop optimization and algebraic transformations. Additionally, it addresses code generation processes, including instruction selection, memory management, and the impact of target machine architecture on code quality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Principles of Compiler

Design
Chapter 6: 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
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

You might also like