Code Optimizations Recap Peephole Optimization

Code Optimizations Recap Peephole Optimization

7/23/2016 Program Analysis Recap https://www.cse.iitb.ac.in/~karkare/cs618/ • Optimizations Code Optimizations – To improve efficiency of generated executable (time, space, resources …) Amey Karkare – Maintain semantic equivalence Dept of Computer Science and Engg • Two levels IIT Kanpur – Machine Independent Visiting IIT Bombay [email protected] – Machine Dependent [email protected] 2 Peephole Optimization Peephole optimization examples… • target code often contains redundant Redundant loads and stores instructions and suboptimal constructs • Consider the code sequence • examine a short sequence of target instruction (peephole) and replace by a Move R , a shorter or faster sequence 0 Move a, R0 • peephole is a small moving window on the target systems • Instruction 2 can always be removed if it does not have a label. 3 4 1 7/23/2016 Peephole optimization examples… Unreachable code example … Unreachable code constant propagation • Consider following code sequence if 0 <> 1 goto L2 int debug = 0 print debugging information if (debug) { L2: print debugging info } Evaluate boolean expression. Since if condition is always true the code becomes this may be translated as goto L2 if debug == 1 goto L1 goto L2 print debugging information L1: print debugging info L2: L2: The print statement is now unreachable. Therefore, the code Eliminate jumps becomes if debug != 1 goto L2 print debugging information L2: L2: 5 6 Peephole optimization examples… Peephole optimization examples… • flow of control: replace jump over jumps • Strength reduction – Replace X^2 by X*X goto L2 goto L1 – Replace multiplication by left shift by … … … – Replace division by right shift … L1: goto L2 L1 : goto L2 • Use faster machine instructions replace Add #1,R • Simplify algebraic expressions remove x := x+0 or x:=x*1 by Inc R 7 8 2 7/23/2016 Proposed Evaluation Assignments 5%‐10% Course Project 30%‐40% Course Logistics ( Proposal 5% ) ( Report 15% ) ( Implementation & Presentation 15% ) Mid semester exam 10%‐20% End semester exam 25%‐35% Quizzes/Class Participation 5% Audit: Stuff in BLACK 9 10 Machine Independent Optimizations • Scope of optimizations – Local Machine Independent Intraprocedural Code Optimizations – Global – Interprocedural 12 3 7/23/2016 Local Optimizations Global Optimizations • Restricted to a basic block • Typically restricted within a procedure/function • Simplifies the analysis – Could be restricted to a smaller scope, e.g. a loop • Not all optimizations can be applied locally • Most compiler implement up to global – E.g. Loop optimizations optimizations • Gains are also limited – Well founded theory • Simplify global/interprocedural – Practical gains optimizations 13 14 Interprocedural Optimizations • Spans multiple procedures, files – In some cases multiple languages! A Catalogue of Code • Not as popular as global optimizations Optimizations – No single theory applicable to multiple scenarios – Time consuming 15 16 4 7/23/2016 Compile‐time Evaluation Compile‐time Evaluation • Move run‐time actions to compile‐time • Constant Propagation • Constant Folding: – Replace a variable by its “constant” value Volume = 4/3*PI*r*r*r; – Compute 4/3*PI at compile time i = 5; i = 5; … Replaced by … – Applied very frequently for linearizing indices of j = i*4; j = 5*4; multidimensional arrays … … – How/When can we apply it? – May result in application of constant folding – How/When can we apply it? 17 18 Common Subexpression Elimination Copy Propagation • Reuse a computation if already “available” • Replace a variable by another – If they are guaranteed to have same value x = u+v; t0 = u+v; i = k; i= k; … Replaced by x = t0; y = u+v+w; … … Replaced by … … y = t0+w; j = i*4; j = k*4; … … … • How to do it? – May result in dead code, common subexpr, … • When can we do it? – How to apply it? – When can we apply it? 19 20 5 7/23/2016 Code Movement Code Movement • Move the code in a program • Code size reduction • Benefits: Suppose op generates a large number of machine – Code size reduction instructions – Reduction in the frequency of execution t1 = x op y; if (a < b) if (a < b) • Allowed only if the meaning of the program u = x op y; Replaced by u = t1; else does not change. else v = x op y; – May result in dead code, common subexpr, … v = t1; – How/When can we apply it? 21 22 Code Movement Loop Invariant Code Movement • Execution frequency reduction • Execution frequency reduction if (a < b) if (a < b) { u = …; u= …; Replaced by for (…) { t3 = a+b; else t2 = x*y; for (…) { … Replaced by v = x*y; } else { u = a+b; … w = x*y; t2 = x*y; … u = t3; v = t2; } … } } w = t2; • How/When can we do it? • How/When can we do it? 23 24 6 7/23/2016 Code Movement Other optimizations • Safety of code motion • Dead code elimination – Remove unreachable, unused code. • Profitability of code motion – Can we always do it? • Strength reduction – Use of low strength operators in place of high strength operators. • i*i instead of i^2, pow(i,2) • i<<1 instead of i*2 – Typically performed for integers only (Why?) 25 26 Data Flow Analysis Data Flow Abstraction • Class of techniques to derive information • Flow graph about flow of data – Graph representation of paths that – along program execution paths program may exercise during execution • Used to answer questions such as: – Typically one graph per procedure – whether two identical expressions evaluate to – Graphs for separate procedure have to same value be combined/connected for • used in common subexpression elimination interprocedural analysis – whether the result of an assignment is used later • Later! • used by dead code elimination • Single procedure, single flow graph for now. 27 28 7 7/23/2016 Data Flow Abstraction • Basic Blocks (bb) • Input state/Output state for Stmt –Program point before/after a stmt –Denoted IN[s] and OUT[s] –Within a basic block: • Program point after a stmt is same as the program point before the next stmt 29 8.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    8 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