Optimizations an Example Kinds of Optimizations Peephole Optimization

Optimizations an Example Kinds of Optimizations Peephole Optimization

Optimizations An example Identify inefficiencies in intermediate or target code Source code: Replace with equivalent but better sequences x = a[i] + b[2]; ¥ equivalent = "has the same externally visible behavior" c[i] = x - 5; Target-independent optimizations best done on IL code Intermediate code (if array indexing calculations explicit): Target-dependent optimizations best done on target code t1 = *(fp + ioffset); // i t2 = t1 * 4; t3 = fp + t2; “Optimize” overly optimistic t4 = *(t3 + aoffset); // a[i] ¥ “usually improve” better t5 = 2; t6 = t5 * 4; t7 = fp + t6; t8 = *(t7 + boffset); // b[2] t9 = t4 + t8; *(fp + xoffset) = t9; // x = ... t10 = *(fp + xoffset); // x t11 = 5; t12 = t10 - t11; t13 = *(fp + ioffset); // i t14 = t13 * 4; t15 = fp + t14; *(t15 + coffset) = t15; // c[i] := ... Craig Chambers 233 CSE 401 Craig Chambers 234 CSE 401 Kinds of optimizations Peephole optimization Scope of study for optimizations: After code generation, look at adjacent instructions ¥ peephole: (a “peephole” on the code stream) look at adjacent instructions ¥ try to replace adjacent instructions with something faster ¥ local: look at straight-line sequence of statements Example: ¥ global (intraprocedural): sw $8, 12($fp) look at whole procedure lw $12, 12($fp) ¥ interprocedural: ⇒ look across procedures sw $8, 12($fp) mv $12, $8 Larger scope ⇒ better optimization, but more cost & complexity Craig Chambers 235 CSE 401 Craig Chambers 236 CSE 401 More examples Peephole optimization of jumps On 68k: Eliminate jumps to jumps sub sp, 4, sp Eliminate jumps after conditional branches mov r1, 0(sp) ⇒ “Adjacent” instructions = “adjacent in control flow” mov r1, -(sp) Source code: mov 12(fp), r1 if (a < b) { add r1, 1, r1 if (c < d) { mov r1, 12(fp) // do nothing ⇒ } else { inc 12(fp) stmt1; } } else { stmt2; Do complex instruction selection through peephole optimization } IL code: Craig Chambers 237 CSE 401 Craig Chambers 238 CSE 401 Algebraic simplifications Local optimization “constant folding”, “strength reduction” Analysis and optimizations within a basic block z = 3 + 4; Basic block: straight-line sequence of statements ¥ no control flow into or out of middle of sequence z = x + 0; z = x * 1; Better than peephole z = x * 2; Not too hard to implement z = x * 8; Machine-independent, if done on intermediate code z = x / 8; double x, y, z; z = (x + y) - y; Can be done by peephole optimizer, or by code generator Craig Chambers 239 CSE 401 Craig Chambers 240 CSE 401 Local constant propagation Local dead assignment elimination If variable assigned a constant, If l.h.s. of assignment never referenced again before being replace downstream uses of the variable with constant overwritten, then can delete assignment Can enable more constant folding E.g. clean-up after previous optimizations Example: Example: final int count = 10; final int count = 10; ... ... x = count * 5; x = count * 5; y = x ^ 3; y = x ^ 3; x = 7; Unoptimized intermediate code: t1 = 10; Intermediate code after constant propagation: t2 = 5; t1 = 10; t3 = t1 * t2; t2 = 5; x = t3; t3 = 50; x = 50; t4 = x; t4 = 50; t5 = 3; t5 = 3; t6 = exp(t4, t5); t6 = 125000; y = t6; y = 125000; x = 7; Craig Chambers 241 CSE 401 Craig Chambers 242 CSE 401 Local common subexpression elimination Avoid repeating the same calculation ¥ CSE of repeated loads: redundant load elimination Keep track of available expressions Source: ... a[i] + b[i] ... Unoptimized intermediate code: t1 = *(fp + ioffset); t2 = t1 * 4; t3 = fp + t2; t4 = *(t3 + aoffset); t5 = *(fp + ioffset); t6 = t5 * 4; t7 = fp + t6; t8 = *(t7 + boffset); t9 = t4 + t8; Craig Chambers 243 CSE 401.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    3 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us