CpS 450 Lecture Notes

Optimization


Introduction

Optimization

Sources of Code Optimization

In decreasing order of "payoff":

 

 

Implementing Optimizations

Intermediate Representation

An intermediate representation is a data structure that represents the program being translated during the translation process.

Optimization Classifications

Optimizations can be classified as local or global:

Basic Block

A basic block is a maximal straight-line segment of code: code sequences that contain no labels (targets for jumps) or jump/call instructions.

 

Example:

The compiler translates this into 3-address code:

Now, compute basic blocks



Why Basic Blocks?

After a compiler carves your code up into basic blocks, it can use data flow analysis to determine whether it safe to perform optimizations such as constant propagation and common subexpression elimination.



Data Flow Analysis

To perform data flow analysis, construct a control flow graph as follows:

Example: Construct control flow graph for example above

Reaching Definitions

A definition is an instruction that alters the value of a variable. Examples:

For each basic block, we compute the reaching definitions for each variable. A reaching definition is a definition which can affect the variable's value at the beginning of the block.

Example: Compute reaching definitions for fact and x in the graph



Using DAGs to Eliminate Common Subexpressions

See example pp. 233-5

 

Sample Optimizations

Constant Folding