


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 26
This page cannot be seen from the preview
Don't miss anything!



















Class stuff
Classical Optimizations
Constant folding, strength reduction
Constant propagation
Forward copy propagation
Backward copy propagation
Constant combining
Operation folding
Invariant code removal
Global variable migration
Induction variable strength reduction
Induction variable elimination
Caveat
Strength Reduction
r1 = r2 * 8
r1 = r2 << 3
r1 = r2 / 4
r1 = r2 >> 2
r1 = r2 REM 16
r1 = r2 & 15
r1 = r2 * 6
r100 = r2 << 2; r101 = r2 << 1; r1 = r100 + r
r1 = r2 * 7
r100 = r2 << 3; r1 = r100 – r
Way more exotic
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
Constant Propagation
rx = L (where L is a literal)
Maximally propagate
Assume no instructionencoding restrictions
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
Global Constant Propagation
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
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)
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)
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
Loop Optimizations
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