computer science 313 advanced programming topics
Download
Skip this Video
Download Presentation
Lecture 35: Loop Optimizations

Loading in 2 Seconds...

play fullscreen
1 / 24

Lecture 35: Loop Optimizations - PowerPoint PPT Presentation


  • 76 Views
  • Uploaded on

Computer Science 313 – Advanced Programming Topics. Lecture 35: Loop Optimizations. Loops Matter. Computers often used for repetitive analyses Machines speed & memory advantageous for this Not hurt by its lack of common sense “Repetitive analyses” means loops

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Lecture 35: Loop Optimizations' - emmy


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
loops matter
Loops Matter
  • Computers often used for repetitive analyses
    • Machines speed & memory advantageous for this
    • Not hurt by its lack of common sense
  • “Repetitive analyses” means loops
  • Loops occur everywhere in computer science
    • Performing statistical analyses on data
    • Database processing for result analyses
    • Evaluating results of large system simulation
    • Redrawing detailed pictures from WoW
programs without loops
Programs without Loops
  • What do we write that does NOT use loops?
optimizing loops useful
Optimizing Loops Useful
  • Big differences from small change to loop
    • Size unimportant; time spent in execution important
    • Repeated execute the code in the loop
    • Even small changes become greatly magnified
  • Compiler limited in how it optimizes loops
    • Often lacks precise knowledge of how things work
    • Languages prevent optimizations across iterations
    • Non-inlinedmethod calls cannot be optimize
    • Cannot optimize uses of an object or field
simple example
Simple Example
  • Consider following loop
    • Calls size()at start of each iteration
    • Calls get()within body of the loop

inti;intretVal = 0;for (i = 0; i < list.size(); i++){retVal += list.get(i);}

minor change
Minor Change
  • Make following changes which have little impact
    • Calls size()at start of loop, but not within iteration
    • Calls get()within body of the loop

inti = list.size() - 1;intretVal = 0;for (; i >= 0; i--){retVal += list.get(i);}

another small change
Another Small Change
  • Loop counts up, not down, in this version
    • Calls size()at start of loop, but not within iteration
    • Calls get()within body of the loop

int end = list.size();intretVal = 0;for (inti = 0; i < end; i++){retVal += list.get(i);}

little odder change
Little Odder Change
  • Limit iterations needed to complete loop
    • Calls size()at start of loop, but not within iteration
    • Calls get()within body of the loop

int end = list.size();intretVal, tmp1 = 0, tmp2 = 0;for (inti = 0; i < end; i+=2){ tmp1 += list.get(i); tmp2 += list.get(i + 1);}

retVal = tmp1 + tmp2;

reason for the 2 big drops
Reason for the 2 Big Drops
  • Biggest change when code moved out of loop
    • Called loop hoistingor loop invariant code-motion
  • Loop hoisting done automatically by compiler
    • But only when it can determine optimization is safe
  • Need to understand how this works
    • Try to write code to enable optimization
    • Don’t write hard-to-read code duplicating optimization
another use of ssa form
Another Use of SSA Form
  • Method must be converted into SSA form
    • Find definition for each use of a variable
  • Instruction is loop-invariant when:

t = xnyn

    • xn& ynare both constant –or–
    • Definitions of xn & ynare outside the loop –or–
    • Loop-invariant instructions define xn & yn
loop hoisting actions
Loop Hoisting Actions
  • Need location to place hoisted instructions
    • Where instructions moved when they get hoisted
  • Could try and remember where loop starts
    • Prepend instructions just before where loop start
    • Need to make sure is not included in loop
  • Much easier to add pre-loop header
    • Blank header included with all loops
    • Purpose is only to hold hoisted instructions
loop invariant example
Loop-Invariant Example
  • Create loop header for loop
  • Any instruction should be mark as loop-invariant if:
    • Constant operands used
    • Operands def’d outside loop
    • Operands from other loop-invariant instructions

a1= …

b1= …

x1= a1 + 22

y1 = b1 + x1

z1 = foo()

if (z1 < 45)

loop invariant example1
Loop-Invariant Example
  • Create loop header for loop
  • Any instruction should be mark as loop-invariant if:
    • Constant operands used
    • Operands def’d outside loop
    • Operands from other loop-invariant instructions

a1= …

b1= …

x1= a1 + 22

y1 = b1 + x1

z1 = foo()

if (z1 < 45)

loop invariant example2
Loop-Invariant Example
  • Create loop header for loop
  • Any instruction should be mark as loop-invariant if:
    • Constant operands used
    • Operands def’d outside loop
    • Operands from other loop-invariant instructions

a1= …

b1= …

x1 = a1 + 22

y1 = b1 + x1

z1 = foo()

if (z1 < 45)

loop invariant example3
Loop-Invariant Example
  • Create loop header for loop
  • Any instruction should be mark as loop-invariant if:
    • Constant operands used
    • Operands def’d outside loop
    • Operands from other loop-invariant instructions

a1= …

b1= …

x1 = a1 + 22

y1 = b1 + x1

z1 = foo()

if (z1 < 45)

loop invariant example4
Loop-Invariant Example
  • Create pre-header for loop
  • Any instruction should be mark as loop-invariant if:
    • Constant operands used
    • Operands def’d outside loop
    • Operands from other loop-invariant instructions
  • Watch for fields & methods
    • Cannot be moved!

a1= …

b1= …

x1 = a1 + 22

y1 = b1 + x1

z1 = foo()

c1 = field

if (z1 < 45)

must also meet conditions
Must Also Meet Conditions
  • Must meet conditions
    • Pre-header dominates hoisted instruction
    • Loop contains exactly one variable definition

do {i = i + 1t = a * b M[i] = t } while (i < t); x = t

must also meet conditions1
Must Also Meet Conditions
  • Must meet conditions
    • Pre-header dominates hoisted instruction
    • Loop contains exactly one variable definition

do {

if (i >= 45)t = a * bi = i + 1M[i] = t } while (i < t); x = t

must also meet conditions2
Must Also Meet Conditions
  • Must meet conditions
    • Pre-header dominates hoisted instruction
    • Loop contains exactly one variable definition

do { if (i >= 45)t = a * belset = a + bM[i] = t } while (i < t);

great for nested loops
Great For Nested Loops
  • Fairly common to work with sets of loop
    • Databases & scientific data especially so
  • Can hoist instructions:
    • From outer loop to outside both loops
    • From inner loop to outside both loops
    • From inner loop to only in the outer loop
  • Normally nested loops use arrays or objects
    • Use scalar replacement to solve this
nested loop example
Nested Loop Example
  • Exposes reuse of a value
    • Array & object uses cannot be optimized
    • Use of local variable can be optimized
  • For really impressive sounding name:
    • Use dependence analysis to help rewrite loops
loop unrolling
Loop Unrolling
  • Unroll to reduce overhead of the loop
  • Advantages:
    • Fewer instructions executed
    • More optimizations possible
    • Great for consecutive accesses
  • Disadvantages:
    • Code gets bloated
    • Still using objects
for next class
For Next Class
  • Lab available on the web
    • Lab will be due1 week from Friday
  • Read pages 385 – 399 for this Friday
    • Begin looking at the State pattern
    • Closely related to what 2 patterns already discussed?
    • When and where would we want to use State pattern?
ad