Optimization


Optimization

Poor Term

The word "optimization" is not really a good term for the process of making the translated program run faster.  It is theoretically impossible to optimize programs with a program.  A better term for the optimization phase of a compiler would be the "code improvement" phase.

Where Optimizations Can Be Done

Source Language:  The semantic routines can, when source code is being translated, do some optimizations:

Intermediate Code:  Two different kinds of optimization strategies are identified--local and global.  For local optimizations, the notion of a basic block is used.  A basic block is a block of intermediated code that is always entered at the top and exited at the bottom.  In between there are no branches into or out of the code.  A program can be viewed as a graph of basic blocks with arrows to a basic block indicating from which points the basic block can be reached and arrows leaving a basic block indicating which next basic blocks can follow this one.

Optimizations within a basic block are called "local."  Optimizations that consider the whole graph are called "global."

local optimizations:  These are fairly easy to do, because they do not required flow analysis among different blocks.  Coupled with code generation, good use of registers,  machine language instructions, etc. during translation from IR to actual code can be done.

global optimizations:  This requires flow analysis.  A major optimization here is common subexpression elimination (e.g., if the compiler can determine that the same code for a subexpression is executed in different blocks with no change to the operands in between, the expression can be evaluated just once, its result saved, and the saved value used wherever that subexpression is to be re-evaluated).  Another big one is moving invariant code out of loops.

Code generation:

Peephole Optimization

Global Optimizations

Global affect a much broader series of statements than peephole optimization.

Optimization in Perspective

Computer Engineering -- Where Hardware and Software Meet

A good computer engineer needs about half hardware and half software. The study of compilers is a good example of where the two disciplines meet.  A computer designer should understand programming languages and compilers well.  That way, the instructions that are included in hardware can be ones that make it possible to generate efficient translated code from source programs.