Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 sequence (peephole) of code at a time. Does not require “expensive” dataflow analysis. Does not require “expensive” dataflow analysis. Pattern match for improvements Pattern match for improvements

2 Possible Patterns Store r0, a Load r0, a r5 = r5 r7 = r5 r6 = r7 r7 = whatever Others?

3 Example – C Code X = A + B * C – (D * A)

4 Example – Intermediate Code t1 = D * A t2 = B * C t3 = A + t2 t4 = t3 – t1 X = t4

5 Example – “Assembly” Code r5 = *(fp + D  offset) r6 = *(fp + A  offset) r7 = r5 * r6 *(fp + t1  offset) = r7 r5 = *(fp + B  offset) r6 = *(fp + C  offset) r7 = r5 * r6 *(fp + t2  offset) = r7

6 “Assembly” Code (cont.) “Assembly” Code (cont.) r5 = *(fp + A  offset) r6 = *(fp + t2  offset) r7 = r5 + r6 *(fp + t3  offset) = r7 r5 = *(fp + t3  offset) r6 = *(fp + t1  offset) r7 = r6 – r5 *(fp + t4  offset) = r7 r5 = *(fp + t4  offset) *(fp + X  offset) = r5

7 Assembler After Peephole r5 = *(fp + D  offset) r6 = *(fp + A  offset) r7 = r5 * r6 *(fp + t1  offset) = r7 r5 = *(fp + B  offset) r6 = *(fp + C  offset) r7 = r5 * r6 *(fp + t2  offset) = r7

8 After Peephole (cont.) After Peephole (cont.) r5 = *(fp + A  offset) r6 = *(fp + t2  offset) r7 = r5 + r7 *(fp + t3  offset) = r7 r5 = *(fp + t3  offset) r6 = *(fp + t1  offset) r7 = r6 – r7 *(fp + t4  offset) = r7 r5 = *(fp + t4  offset) *(fp + X  offset) = r7

9 So How big a peephole do we need? How big a peephole do we need? How could you represent code to ease How could you represent code to ease Re-write your code to generate intermediate Re-write your code to generate intermediate Do peephole optimization Do peephole optimization Generate CMachine code from intermediate Generate CMachine code from intermediate

10 Instruction Selection by Peephole Optimization Represent each instruction by RTL Represent each instruction by RTL Look for pairs of instructions whose combined RTL == that of a single instruction Look for pairs of instructions whose combined RTL == that of a single instruction PO, Hop, Chop (late 1970s – early 1980s) PO, Hop, Chop (late 1970s – early 1980s) Used for code selection by GNU compilers Used for code selection by GNU compilers


Download ppt "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."

Similar presentations


Ads by Google