Advanced Compilers CMPSCI 710 Spring 2003 Basic Loop
Advanced Compilers CMPSCI 710 Spring 2003 Basic Loop Optimizations Emery Berger University of Massachusetts, Amherst UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE
Topics n Last time n Optimizations using SSA form n n n Constant propagation & dead code elimination Loop invariant code motion This time n Loop optimizations n n Induction variable Linear test replacement Loop unrolling Scalar replacement UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 2
Easy Detection of Loop Induction Variables n Pattern match & check: n n n Search for “i = i + b” in loop i is induction variable if no other assignment to i in loop Pros & Cons: Easy! - Does not catch all loop induction variables e. g. , “i = a * c + 2” + UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 3
Taxonomy of Induction Variables n basic induction variable: n n only definition in loop is assignment j = j § c, where c is loop invariant mutual induction variable: n definition is linear function of other induction variable i ‘: n n n i = c 1 * i‘ § c 2 i = i‘ / c 1 § c 2 family of basic induction variable j: n set of induction variables i such that i is always assigned linear function of j UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 4
Strength Reduction n Replace “expensive” op by “cheaper” one n n E. g. , replace multiply by addition Apply to induction variable families n Especially: array indexing UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 5
Strength Reduction Algorithm n Let i be induction variable in the family of basic induction variable j: n n i = c 1 * j + c 2 Create new variable i’ Initialize in pre-header: i’ = c 1*j + c 2 Track value of j: after j = j + c 3, add i’ = i’ + (c 1 * c 3) UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 6
Strength Reduction Example UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 7
Candidates for Strength Reduction n Induction variable IV multiplied by invariant n Recursively: n IV * IV, IV mod constant, IV + IV UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 8
Strength Reduction Algorithm UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 9
Strength Reduction Examples n basic induction variable: n n only definition in loop is assignment j = j § c, where c is loop invariant mutual induction variable: n definition is linear function of other induction variable i‘: n n n i = c 1 * i‘ § c 2 i = i‘ / c 1 § c 2 family of basic induction variable j: n set of induction variables i such that i is always assigned linear function of j UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 10
Linear Test Replacement n Eliminates induction variable! n n After strength reduction, loop test is often last use of induction variable Algorithm: n If only use of IV is loop test and its own increment, and test is always computed n n i. e. , only one exit from loop Replace test with equivalent one: n E. g. , “i comp k” ) “i_50 comp k*50” UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 11
Linear Test Replacement Example UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 12
Loop Unrolling n n To reduce loop overhead, we can unroll loops Advantages: n Disadvantages: + Execute fewer total instructions Code bloat + More fodder for common Still updating subexpression elimination, strength through memory reduction, etc. 13 NIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE + U Move consecutive access closer
Scalar Replacement n n Problem: register allocators never keep a[i] in register Idea: trick allocator n Locate patterns of consistent reuse n Replace load with copy into temporary n Replace store with copy from temporary n May need copies at end of loop n n E. g. , when reuse spans > 1 iteration Advantages: n Decreases number of loads and stores n Keeps reused values in registers n Big performance impact (2 x, 3 x!) UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 14
Scalar Replacement Example n Scalar replacement exposes the reuse of a[i] n n Traditional scalar analysis – inadequate Use dependence analysis to understand array references (later) UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 15
Next Time n n Common Subexpression Elimination Read ACDI: n n Ch. 12, pp. 343 -355 Ch. 13, pp. 378 -396 UNIVERSITY OF MASSACHUSETTS, AMHERST • DEPARTMENT OF COMPUTER SCIENCE 16
- Slides: 16