DOC PREVIEW
CSU CS 553 - Lecture 14-Code Motion

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CS553 Lecture Loop Invariant Code Motion 1Loop Invariant Code Motion Last Time− Control flow analysis Today− Loop invariant code motion Next Time− Induction variable identification and eliminationCS553 Lecture Loop Invariant Code Motion 2Loop Invariant Code MotionBackground: ud- and du-chains ud-Chains− A ud-chain connects a use of a variable to all defs of a variable that mightreach it (a sparse representation of Reaching Definitions) du-Chains− A du-chain connects a def to all uses that it might reach (a sparserepresentation of Upward Exposed Uses) How do ud- and du-chains differ?x = …… = xx = …CS553 Lecture Loop Invariant Code Motion 3Upward Exposed Uses Definition− An upward exposed use at program point p is a use that may be reachedby a definition at p (i.e, no intervening definitions). How do upward exposed uses differ from live variables?2y := 2 * cb := . . .3a := . . .x := a + bp1. . .CS553 Lecture Loop Invariant Code Motion 4Identifying Loop Invariant Code Motivation− Avoid redundant computations Example w = . . . y = . . . z = . . . L1: x = y + z v = w + x . . . if . . . goto L1Everything that x depends upon iscomputed outside the loop, i.e., alldefs of y and z are outside of theloop, so we can move x = y + zoutside the loopWhat happens once we move thatstatement outside the loop?2CS553 Lecture Loop Invariant Code Motion 5Algorithm for Identifying Loop Invariant CodeInput: A loop L consisting of basic blocks. Each basic block contains a sequence of3-address instructions. We assume ud-chains have been computed.Output: The set of instructions that compute the same value each time through the loopInformal Algorithm:1. Mark “invariant” those statements whose operands are either– Constant– Have all reaching definitions outside of L2. Repeat until a fixed point is reached: mark “invariant” those unmarked statementswhose operands are either– Constant– Have all reaching definitions outside of L– Have exactly one reaching definition and that definition is in the set marked“invariant”Is this last condition too strict?CS553 Lecture Loop Invariant Code Motion 6Algorithm for Identifying Loop Invariant Code (cont) Is the Last Condition Too Strict?− No− If there is more than one reaching definition for an operand, then neitherone dominates the operand− If neither one dominates the operand, then the value can vary dependingon the control path taken, so the value is not loop invariantx = c1… = xx = c2InvariantstatementsCS553 Lecture Loop Invariant Code Motion 7Code MotionWhat’s the Next Step?− Do we simply move the “invariant” statements outside the loop?− No, there are three requirements that ensure that code motion does notchange program semantics. For some statements: x = y + z1. The block containing s dominates all loop exits2. No other statement in the loop assigns to x3. No use of x in the loop is reached by any def of x other than sCS553 Lecture Loop Invariant Code Motion 8i = 2u = u+1if u<v goto B3i = 1j = iv = v – 1if v<9 goto B5B1B5B4B2B3i=2 is loop invariant, but B3 doesnot dominate B4, the exit node, somoving i=2 would change themeaning of the loop for those caseswhere B3 is never executedExample 1 Condition 1 is Needed− If the block containing s does not dominate all exits, after the loop mightget a different valueCan we move i=2 outside the loop?3CS553 Lecture Loop Invariant Code Motion 9i = 2u = u+1if u<v goto B3i = 1j = iv = v – 1if v<9 goto B5B1B5B4B2B3i = 3B2 dominates the exit so condition 1is satisfied, but code motion will setthe value of i to 2 if B3 is everexecuted, rather than letting it varybetween 2 and 3.Example 2 Condition 2 is Needed− If some other statement in the loop assigns i, the movement of thestatement may cause some statement to see the wrong valueCan we move i=3 outside the loop?CS553 Lecture Loop Invariant Code Motion 10u = u+1i = 1j = iv = v – 1B1B5B4B2B3if v<9 goto B5Conditions 1 and 2 are met, but theuse of i in block B2, can be reachedfrom a different def, namely i=1from B1.If we were to move i=4 outside theloop, the first iteration through theloop would print 4 instead of 1Example 3 Condition 3 is Needed− If a use in L can be reached by some other def, then we cannot move thedef outside the loopCan we move i=4 outside the loop?i = 4if u<v goto B3print iCS553 Lecture Loop Invariant Code Motion 11Loop Invariant Code Motion AlgorithmInput: A loop L with ud-chains and dominator informationOutput: A modified loop with a preheader and 0 or more statements moved to the preheaderAlgorithm:1. Find loop-invariant statements2. Insert preheader3. For each statement s defining x found in step 1, move s to preheader if:a. s is in a block that dominates all exits of L,b. x is not defined elsewhere in L, andc. x is not live-out of the loop preheaderCorrectnessConditions 2a and 2b ensure that the value of x computed at s is the value ofx after any exit block of L. When we move s to the preheader, s will still bethe def that reaches any of the exit blocks of L. Condition 2c ensures that any use of x inside of L used (and continues touse) the value of x computed by sCS553 Lecture Loop Invariant Code Motion 12Loop Invariant Code Motion Algorithm (cont)Profitability− Can loop invariant code motion ever increase the running time of theprogram?− Can loop invariant code motion ever increase the number of instructionsexecuted?− Before transformation, s is executed at least once (condition 2a)− After transformation, s is executed exactly onceRelaxing Condition 1− If we’re willing to sometimes do more work: Change the condition toa. The block containing s either dominates all loop exits, or x is dead after the loop4CS553 Lecture Loop Invariant Code Motion 13Alternate Approach to Loop Invariant Code MotionDivision of labor− Move all invariant computations to the preheader and assign them totemporaries− Use the temporaries inside the loop− Insert copies where necessary− Rely on Copy Propagation to remove unnecessary assignments Benefits− Much simpler: Fewer cases to handleCS553 Lecture Loop Invariant Code Motion 14i = 2u = u+1if u<v goto B3i = 1j = iv = v – 1if v<9 goto B5B1B5B4B2B3Example 1 Revisited Using the alternate approach− Move the invariant code outside the loop− Use a temporary inside the loopi = tu = u+1if u<v goto B3i = 1t = 2j = iv = v – 1if v<9 goto B5B5B4B2B3CS553 Lecture Loop Invariant Code Motion 15i = 2u = u+1if u<v goto B3i = 1j =


View Full Document

CSU CS 553 - Lecture 14-Code Motion

Download Lecture 14-Code Motion
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 14-Code Motion and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 14-Code Motion 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?