Lecture 014

Program Optimization

Optimization Goal:

Intermediate Code

Intermediate Language (Representation): exposes details like register for better code optimization. We often use high-level assembly for traditional programming language. We can have multiple steps of IL.

Where should we optimize code:

Example of semantics of IR

Example of semantics of IR

To simplify, we define Basic Block so that we can make sure within a Basic Block, executions are guarantee executed therefore predictable.

Basic Block: a maximal sequence with no label (except for the first instruction) and no jumps (except for the last instruction)

Control-Flow-Graph: a graph where Basic Blocks are nodes and possible jump instructions are edges.

Optimization Levels

Most compiler do Local Optimization, many do Global, only few do Inter-procedural.

Also, in practice, often a conscious decision is made not to implement the fanciest optimization known. Reasons: - hard to implement - costly in compilation time - low payoff

Local Optimization

Possible Optimization:

Constant Folding: combine two constant (say addition) into one constant.

Common Subexpression Elimination

Single assignment form: when a variable x is assigned only once in lifetime, then the variable x is in single assignment form.

If basic block is in single assignment form and x := ... is the first assignment, we can do the following optimization:

x := y + z
...
w := y + z

optimize to

x := y + z
w := x

Copy Propagation

Assuming single assignment form, then we can do

b := z + y
a := b
x := 2 * a

optimize to

b := z + y
a := b     // and so if a is never used, can be deleted (dead statement)
x := 2 * b

optimize to

b := z + y
x := 2 * b

Often, even when an optimization can't speed up things by itself, it enables more optimizations opportunities down the line. So optimization need to be performed in repetition.

Peephole Optimization

Example:

move $a $b
move $b $a

optimize to

move $a $b

Example:

addiu $a $a i
addiu $a $a j

optimize to

addiu $a $a i+j

Example:

addiu $a $b 0

optimize to

move $a $b

In general, it is impossible for compiler to give best optimized code for a given program.

Table of Content