Compiler Optimizations: Classical, Machine Specific, and ILP Enhancing - Prof. Nathan Clar, Assignments of Computer Science

Various optimization techniques used in compiler design, including classical optimization methods such as constant folding and strength reduction, machine-specific optimizations, and ilp enhancing optimizations. These techniques aim to reduce operation count, simplify operations, increase parallelism, and take advantage of specialized hardware features.

Typology: Assignments

Pre 2010

Uploaded on 08/05/2009

koofers-user-gdy-1
koofers-user-gdy-1 🇺🇸

10 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 6241 – Class 9
Classic Optimization
Georgia Tech.
February 5, 2008
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Compiler Optimizations: Classical, Machine Specific, and ILP Enhancing - Prof. Nathan Clar and more Assignments Computer Science in PDF only on Docsity!

CS 6241 – Class 9Classic Optimization

Georgia Tech.February 5, 2008

  • 1 -

Class stuff

HW1 grading appt

HW2 no yacc, code fixes

HW

HW

  • 3 -

Classical Optimizations



Operation-level – 1 operation in isolation

Constant folding, strength reduction



Dead code elimination (global, but 1 op at a time)



Local/Global – Pairs of operations

Constant propagation

Forward copy propagation

Backward copy propagation

CSE

Constant combining

Operation folding



Loop – Body of a loop

Invariant code removal

Global variable migration

Induction variable strength reduction

Induction variable elimination

  • 4 -

Caveat

Traditional compiler class

Fancy implementations of optimizations, efficient algorithms

Entire papers written on how to do 1 optimization

Spend entire class on 1 optimization

For this class – Go over concepts of each optimization

What it is, why its useful

When can it be applied (set of conditions that must be satisfied)

Challenges

How do predicates affect things?

Register pressure?

ILP verses operation count

  • 6 -

Strength Reduction

Replace expensive ops with cheaper ones

Constant propagation creates opportunities for this

Power of 2 constants

Multiply by power of 2, replace with left shift

r1 = r2 * 8

r1 = r2 << 3

Divide by power of 2, replace with right shift

r1 = r2 / 4

r1 = r2 >> 2

Remainder by power of 2, replace with logical and

r1 = r2 REM 16

r1 = r2 & 15

More exotic

Replace multiply by constant by sequence of shift and adds/subs

r1 = r2 * 6



r100 = r2 << 2; r101 = r2 << 1; r1 = r100 + r

r1 = r2 * 7



r100 = r2 << 3; r1 = r100 – r

  • 7 -

Way more exotic

// ntclark: need to flip the low order bit.

if(tmp_sym & 1) {

tmp_sym = ((tmp_sym >> 1) << 1);

} else {

tmp_sym = tmp_sym | 1;

  • 9 -

Class Problem

r1 = 0

r4 = r1 | -1r7 = r1 * 4

r6 = r

r3 = 8 / r

r3 = 8 * r r3 = r3 + r

r2 = r2 + r1r6 = r7 * r

r1 = r1 + 1 store (r1, r3)

Optimize this applying 1. constant folding2. strength reduction3. dead code elimination

  • 10 -

Constant Propagation



Forward propagation of movesof the form

rx = L (where L is a literal)

Maximally propagate

Assume no instructionencoding restrictions



When is it legal?

SRC: Literal is a hard codedconstant, so never a problem

DEST: Must be available



Guaranteed to reach



May reach not good enough

r1 = 5r2 = r1 + r

r1 = r1 + r

r7 = r1 + r

r8 = r1 + 3

r9 = r1 + r

  • 12 -

Global Constant Propagation



Consider 2 ops, X and Y indifferent BBs

  1. X is a move
  1. src1(X) is a literal
  1. Y consumes dest(X)
  1. X is in a_in(BB(Y))
  1. Dest(x) is not modified betweenthe top of BB(Y) and Y
  1. No danger betw X and Y



When dest(X) is a Macro reg,BRL destroys the value

r1 = 5r2 = ‘_x’

r1 = r1 + r

r7 = r1 – r

r8 = r1 * r

r9 = r1 + r

  • 13 -

Class Problem

r1 = 0r2 = 10 r4 = 1r7 = r1 * 4r6 = 8p1,p2 = cmppUNUC r3 > 0r2 = 0 if p1r3 = r4 * r6 if p2r3 = r3 + r2 if p2r6 = r6 * r7 if Tr3 = r2 / r6 if p1r2 = r2 + 1 if Tr1 = r1 + 1 if T store (r1, r3)

Optimize this applying 1. constant propagation2. constant folding

  • 15 -

Backward Copy Propagation



Backward propagation of the LHSof moves

»

r1 = r2 + r



r4 = r2 + r

»

»

r5 = r1 + r



r5 = r4 + r

»

»

r4 = r



noop



Rules (ops X and Y in same BB)

»

dest(X) is a register

»

dest(X) not live out of BB(X)

»

Y is a move

»

dest(Y) is a register

»

Y consumes dest(X)

»

dest(Y) not consumed in (X…Y)

»

dest(Y) not defined in (X…Y)

»

There are no uses of dest(X) afterthe first redefinition of dest(Y)

r1 = r8 + r9r2 = r9 + r1r4 = r2r6 = r2 + 1r9 = r1r10 = r6r5 = r6 + 1r4 = 0r8 = r2 + r

  • 16 -

CSE – Common Subexpression Elimination



Eliminate recomputation of anexpression by reusing the previousresult

»

r1 = r2 * r

»



r100 = r

»

»

r4 = r2 * r



r4 = r



Benefits

»

Reduce work

»

Moves can get copy propagated



Rules (ops X and Y)

»

X and Y have the same opcode

»

src(X) = src(Y), for all srcs

»

expr(X) is available at Y

»

if X is a load, then there is no storethat may write to address(X) alongany path between X and Y

r1 = r2 * r r3 = r4 / r

r2 = r2 + 1

r6 = r3 * 7

r5 = r2 * r r8 = r4 / r7r9 = r3 * 7

if op is a load, call it redundantload elimination rather than CSE

  • 18 -

Loop Optimizations

The most important set of optimizations

Because programs spend so much time in loops

Optimize given that you know a sequence of code will berepeatedly executed

Optis

Invariant code removal

Global variable migration

Induction variable strength reduction

Induction variable elimination

  • 19 -

Recall Loop Terminology

r1 = 3 r2 = 10

r4 = r4 + 1r7 = r4 * 3

r2 = 0

r3 = r2 + 1

r1 = r1 + 2 store (r1, r3)

loop preheader loop header

backedge BB

exit BB

  • r1, r4 are basicinduction variables- r7 is a derivedinduction variable