peephole optimization improve code by examining and changing a small sequence (peephole) of code at...
TRANSCRIPT
Peephole OptimizationPeephole Optimization
Improve code by examining and Improve code by examining and changing a small sequence changing a small sequence (peephole) of code at a time.(peephole) of code at a time.
Does not require “expensive” Does not require “expensive” dataflow analysis.dataflow analysis.
Pattern match for improvementsPattern match for improvements
Possible PatternsPossible Patterns
Store r0, aStore r0, a
Load r0, aLoad r0, a
r5 = r5r5 = r5
r7 = r5r7 = r5
r6 = r7r6 = r7
r7 = whateverr7 = whatever
Others?Others?
Example – C CodeExample – C CodeX = A + B * C – (D * A)X = A + B * C – (D * A)
Example – Intermediate Example – Intermediate CodeCode
t1 = D * At1 = D * A
t2 = B * Ct2 = B * C
t3 = A + t2t3 = A + t2
t4 = t3 – t1t4 = t3 – t1
X = t4 X = t4
Example – “Assembly” Example – “Assembly” CodeCode
r5 = *(fp + Dr5 = *(fp + Doffset)offset)
r6 = *(fp + Ar6 = *(fp + Aoffset)offset)
r7 = r5 * r6r7 = r5 * r6
*(fp + t1*(fp + t1offset) = r7offset) = r7
r5 = *(fp + Br5 = *(fp + Boffset)offset)
r6 = *(fp + Cr6 = *(fp + Coffset)offset)
r7 = r5 * r6r7 = r5 * r6
*(fp + t2*(fp + t2offset) = r7offset) = r7
“ “Assembly” Code (cont.)Assembly” Code (cont.)
r5 = *(fp + Ar5 = *(fp + Aoffset)offset)r6 = *(fp + t2r6 = *(fp + t2offset)offset)r7 = r5 + r6r7 = r5 + r6*(fp + t3*(fp + t3offset) = r7offset) = r7r5 = *(fp + t3r5 = *(fp + t3offset)offset)r6 = *(fp + t1r6 = *(fp + t1offset)offset)r7 = r6 – r5r7 = r6 – r5*(fp + t4*(fp + t4offset) = r7offset) = r7r5 = *(fp + t4r5 = *(fp + t4offset) offset) *(fp + X*(fp + Xoffset) = r5offset) = r5
Assembler After PeepholeAssembler After Peephole
r5 = *(fp + Dr5 = *(fp + Doffset)offset)
r6 = *(fp + Ar6 = *(fp + Aoffset)offset)
r7 = r5 * r6r7 = r5 * r6
*(fp + t1*(fp + t1offset) = r7offset) = r7
r5 = *(fp + Br5 = *(fp + Boffset)offset)
r6 = *(fp + Cr6 = *(fp + Coffset)offset)
r7 = r5 * r6r7 = r5 * r6
*(fp + t2*(fp + t2offset) = r7offset) = r7
After Peephole (cont.)After Peephole (cont.)
r5 = *(fp + Ar5 = *(fp + Aoffset)offset)r6 = *(fp + t2r6 = *(fp + t2offset)offset)r7 = r5 + rr7 = r5 + r77*(fp + t3*(fp + t3offset) = r7offset) = r7r5 = *(fp + t3r5 = *(fp + t3offset)offset)r6 = *(fp + t1r6 = *(fp + t1offset)offset)r7 = r6 – rr7 = r6 – r77*(fp + t4*(fp + t4offset) = r7offset) = r7r5 = *(fp + t4r5 = *(fp + t4offset)offset) *(fp + X*(fp + Xoffset) = roffset) = r77
SoSo
How big a peephole do we need?How big a peephole do we need? How could you represent code to How could you represent code to
easeease Re-write your code to generate Re-write your code to generate
intermediateintermediate Do peephole optimizationDo peephole optimization Generate CMachine code from Generate CMachine code from
intermediateintermediate
Instruction Selection by Instruction Selection by Peephole OptimizationPeephole Optimization
Represent each instruction by RTLRepresent each instruction by RTL Look for pairs of instructions whose Look for pairs of instructions whose
combined RTL == that of a single combined RTL == that of a single instructioninstruction
PO, Hop, Chop (late 1970s – early PO, Hop, Chop (late 1970s – early 1980s)1980s)
Used for code selection by GNU Used for code selection by GNU compilerscompilers