peephole optimization improve code by examining and changing a small sequence (peephole) of code at...
TRANSCRIPT
![Page 1: Peephole Optimization Improve code by examining and changing a small sequence (peephole) of code at a time. Improve code by examining and changing a small](https://reader036.vdocument.in/reader036/viewer/2022072013/56649e4d5503460f94b436a3/html5/thumbnails/1.jpg)
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
![Page 2: Peephole Optimization Improve code by examining and changing a small sequence (peephole) of code at a time. Improve code by examining and changing a small](https://reader036.vdocument.in/reader036/viewer/2022072013/56649e4d5503460f94b436a3/html5/thumbnails/2.jpg)
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?
![Page 3: Peephole Optimization Improve code by examining and changing a small sequence (peephole) of code at a time. Improve code by examining and changing a small](https://reader036.vdocument.in/reader036/viewer/2022072013/56649e4d5503460f94b436a3/html5/thumbnails/3.jpg)
Example – C CodeExample – C CodeX = A + B * C – (D * A)X = A + B * C – (D * A)
![Page 4: Peephole Optimization Improve code by examining and changing a small sequence (peephole) of code at a time. Improve code by examining and changing a small](https://reader036.vdocument.in/reader036/viewer/2022072013/56649e4d5503460f94b436a3/html5/thumbnails/4.jpg)
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
![Page 5: Peephole Optimization Improve code by examining and changing a small sequence (peephole) of code at a time. Improve code by examining and changing a small](https://reader036.vdocument.in/reader036/viewer/2022072013/56649e4d5503460f94b436a3/html5/thumbnails/5.jpg)
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
![Page 6: Peephole Optimization Improve code by examining and changing a small sequence (peephole) of code at a time. Improve code by examining and changing a small](https://reader036.vdocument.in/reader036/viewer/2022072013/56649e4d5503460f94b436a3/html5/thumbnails/6.jpg)
“ “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
![Page 7: Peephole Optimization Improve code by examining and changing a small sequence (peephole) of code at a time. Improve code by examining and changing a small](https://reader036.vdocument.in/reader036/viewer/2022072013/56649e4d5503460f94b436a3/html5/thumbnails/7.jpg)
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
![Page 8: Peephole Optimization Improve code by examining and changing a small sequence (peephole) of code at a time. Improve code by examining and changing a small](https://reader036.vdocument.in/reader036/viewer/2022072013/56649e4d5503460f94b436a3/html5/thumbnails/8.jpg)
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
![Page 9: Peephole Optimization Improve code by examining and changing a small sequence (peephole) of code at a time. Improve code by examining and changing a small](https://reader036.vdocument.in/reader036/viewer/2022072013/56649e4d5503460f94b436a3/html5/thumbnails/9.jpg)
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
![Page 10: Peephole Optimization Improve code by examining and changing a small sequence (peephole) of code at a time. Improve code by examining and changing a small](https://reader036.vdocument.in/reader036/viewer/2022072013/56649e4d5503460f94b436a3/html5/thumbnails/10.jpg)
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