1 / 13

Really Basic Stuff

This technical report introduces key code optimization techniques including flow graphs, constant folding, global common subexpressions, and induction variables reduction.

rlawrence
Download Presentation

Really Basic Stuff

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Really Basic Stuff Flow Graphs Constant Folding Global Common Subexpressions Induction Variables/Reduction in Strength

  2. Dawn of Code Optimization • A never-published Stanford technical report by Fran Allen in 1968. • Flow graphs of intermediate code. • Key things worth doing.

  3. Intermediate Code for (i=0; i<n; i++) A[i] = 1; • Intermediate code exposes optimizable constructs we cannot see at source-code level. • Make flow explicit by breaking into basic blocks = sequences of steps with entry at beginning, exit at end.

  4. i = 0 if i>=n goto … t1 = 8*i A[t1] = 1 i = i+1 Basic Blocks

  5. Induction Variables • x is an induction variable in a loop if it takes on a linear sequence of values each time through the loop. • Common case: loop index like i and computed array index like t1. • Eliminate “superfluous” induction variables. • Replace multiplication by addition (reduction in strength ).

  6. i = 0 if i>=n goto … t1 = 8*i A[t1] = 1 i = i+1 Example t1 = 0 n1 = 8*n if t1>=n1 goto … A[t1] = 1 t1 = t1+8

  7. Loop-Invariant Code Motion • Sometimes, a computation is done each time around a loop. • Move it before the loop to save n-1 computations. • Be careful: could n=0? I.e., the loop is typically executed 0 times.

  8. Example i = 0 i = 0 t1 = y+z if i>=n goto … if i>=n goto … t1 = y+z x = x+t1 i = i+1 x = x+t1 i = i+1

  9. Constant Folding • Sometimes a variable has a known constant value at a point. • If so, replacing the variable by the constant simplifies and speeds-up the code. • Easy within a basic block; harder across blocks.

  10. i = 0 n = 100 if i>=n goto … t1 = 8*i A[t1] = 1 i = i+1 Example t1 = 0 if t1>=800 goto … A[t1] = 1 t1 = t1+8

  11. Global Common Subexpressions • Suppose block B has a computation of x+y. • Suppose we are sure that when we reach this computation, we are sure to have: • Computed x+y, and • Not subsequently reassigned x or y. • Then we can hold the value of x+y and use it in B.

  12. a = x+y t = x+y a = t b = x+y t = x+y b = t c = x+y c = t Example

  13. t = x+y a = t t = x+y b = t c = t Example --- Even Better t = x+y a = t b = t t = x+y b = t c = t

More Related