lecture27.1 - Global Optimization
lecture27.1 - Global Optimization
Compiler Design
Lecture 27
Ahmed Ezzat
Source
Code Lexi c al Analysis
Syntax Analysis
S e m a n t i c Analysis
I R Generation
I R Optimization
C o d e Generation
Machine
Optimization Code
3 CMPE 152
Code Optimizations Topics:
High-level Overview
❑ Requirements
❖ Improved code has the same output
❖ Improved code has the same side effect
❖ Improved code is as correct as original
❑ Example tasks
❖ Eliminate redundant computations
❖ Efficient use of registers
❖ Eliminate loops or move computation out of loops; when
appropriate
⚫ Programmer ⚫ Processor
❑ Choice of algorithm
❑ Pipelining
❑ Intelligent coding
❑ Multiple execution units
❑ Memory accesses
⚫ Compiler
❑ Branches
❑ Choice of instructions
❑ Caches
❑ Moving code
❑ Reordering code
❑ Strength reduction*
⚫ Rest of system
❑ Must be faithful to original ❑ Uncontrollable
program
* An optimization where a function is calculated more efficiently using previous values of the
6 function, or to replace expensive operation with less expensive one (e.g., replace
multiplication with shift)
Code Optimizations Topics:
Overview
⚫ Strength reduction
❑ Replace costly operation with simpler one
❑ Shift, add instead of multiply or divide
16*x → x << 4
⚫ Utility is machine-dependent
⚫ Depends on cost of multiply or divide instruction
⚫ On Pentium II or III, integer multiply only requires 4 CPU cycles
❑ Recognize sequence of products int ni = 0;
for (i = 0; i < n; i++) {
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
for (j = 0; j < n; j++)
a[ni + j] = b[j];
a[n*i + j] = b[j];
ni += n;
}
⚫ Limitations
❑ Compiler not always able to determine whether variable can
be held in register
❑ Possibility of aliasing
❑ See example later
⚫ Code hosting:
❑ Moving computations outside loops saves computing time
❑ In the following example (2.0 * PI) is an invariant expression there
is no reason to re-compute it 100 times.
DO i = 1, 100
ARRAY(i) = 2.0 * PI * i
END DO
⚫ Loop unrolling:
❑ The loop exit checks cost CPU time.
❑ Loop unrolling tries to get rid of the checks completely
or to reduce the number of checks.
❑ If you know a loop is only performed a certain number
of times, or if you know the number of times it will be
repeated is a multiple of a constant you can unroll this
loop.
where the loop code doesn't change the limit variable. The
subtraction, limit-2, will be inside the loop. Code motion
would substitute:
t = limit-2;
while (i <= t)
do {loop code}
25 CMPE 152 Ahmed Ezzat
Compiler Code Optimizations:
High-level
⚫ In Summary:
❑ Compilers can provide some code optimization.
❑ Programmers do not have to worry about such optimizations.
❑ Program definition must be preserved.
⚫ Basic Blocks:
❑ A list of instructions
❑ Once the block is entered, each instruction in that
basic block is executed exactly once
❑ Only the first statement can be reached from
outside the block (no jumping in)
❑ Execution continues from first instruction to last in
the block (no jumping out)