loop invariant code motion example
play

Loop-invariant code motion Example Two steps: analysis & - PDF document

Loop-invariant code motion Example Two steps: analysis & transformation x := 3 Step 1: find invariant computations in loop y := 4 y := 5 invariant: computes same result each time evaluated z := x * y Step 2: move them outside loop


  1. Loop-invariant code motion Example Two steps: analysis & transformation x := 3 Step 1: find invariant computations in loop y := 4 y := 5 • invariant: computes same result each time evaluated z := x * y Step 2: move them outside loop q := y * y • to top: code hoisting w := y + 2 • if used within loop • to bottom: code sinking • if only used after loop w := w + 5 p := w + y x := x + 1 q := q + 1 Craig Chambers 89 CSE 501 Craig Chambers 90 CSE 501 Detecting loop-invariant expressions Computing loop-invariant expressions An expression is invariant w.r.t. a loop L iff: Option 1: • repeat iterative dfa until no more invariant expressions found base cases: • to start, optimistically assume all expressions loop-invariant • it’s a constant • it’s a variable use, all of whose defs are outside L Option 2: • build def/use chains, inductive cases: follow chains to identify & propagate • it’s an idempotent computation invariant expressions all of whose args are loop-invariant • it’s a variable use with only one reaching def , Option 3: and the rhs of that def is loop-invariant • convert to SSA form, then similar to def/use form Craig Chambers 91 CSE 501 Craig Chambers 92 CSE 501

  2. Example using def/use chains Loop-invariant expression detection for SSA form SSA form simplifies detection of loop invariants, x := 3 since each use has only one reaching definition An expression is invariant w.r.t. a loop L iff: y := 4 y := 5 base cases: • it’s a constant z := x * y • it’s a variable use whose single def is outside L q := y * y w := y + 2 inductive cases: • it’s an idempotent computation all of whose args are loop-invariant w := w + 5 • it’s a variable use whose single def’s rhs is loop-invariant p := w + y x := x + 1 φ functions are not idempotent q := q + 1 Craig Chambers 93 CSE 501 Craig Chambers 94 CSE 501 Example using SSA form Example using SSA form & preheader x 1 := 3 x 1 := 3 y 1 := 4 y 2 := 5 y 1 := 4 y 2 := 5 x 2 = φ (x 1 , x 3 ) y 3 = φ (y 1 , y 2 ) y 3 = φ (y 1 , y 2 , y 3 ) z 1 := x 2 * y 3 x 2 = φ (x 1 , x 3 ) q 1 := y 3 * y 3 z 1 := x 2 * y 3 w 1 := y 3 + 2 q 1 := y 3 * y 3 w 1 := y 3 + 2 w 2 := w 1 + 5 w 2 := w 1 + 5 w 3 = φ (w 1 , w 2 ) w 3 = φ (w 1 , w 2 ) p 1 := w 3 + y 3 p 1 := w 3 + y 3 x 3 := x 2 + 1 q 2 := q 1 + 1 x 3 := x 2 + 1 q 2 := q 1 + 1 Craig Chambers 95 CSE 501 Craig Chambers 96 CSE 501

  3. Code motion Example of need for domination requirement When find invariant computation S: z := x op y , want to move it out of loop (to loop preheader) x := 0 y := 1 When is this legal? Sufficient conditions: z != 0? • S dominates all loop exits [ A dominates B when all paths to B must first pass through A ] x := a * b q := x + y • otherwise may execute S when never executed otherwise y := x / z • can relax this condition, if S has no side-effects or traps, at cost of possibly slowing down program • S is only assignment to z in loop, & no use of z in loop is reached by any def other than S • otherwise may reorder defs/uses and change outcome • unnecessary in SSA form! If met, then can move S to loop preheader • but preserve relative order of invariant computations, to preserve data flow among moved statements Craig Chambers 97 CSE 501 Craig Chambers 98 CSE 501 Avoiding domination restriction Example of data dependence restrictions Requirement that invariant computation dominates exit is strict “ S is only assignment to z in loop, & no use of z in loop is reached by any def other than S ” • nothing in conditional branch can be moved • nothing after loop exit test can be moved z := 5 Can be circumvented through other transformations such as loop normalization • move loop exit test to bottom of loop (while-do ⇒ do-while) z := z + 1 Before After S: z := 0 i := 0 i := 0 i < N? i < N? ... z ... x := a / b x := a / b i := i + 1 i := i + 1 i < N? Craig Chambers 99 CSE 501 Craig Chambers 100 CSE 501

  4. Example in SSA form Loop-invariant code copying Restrictions unnecessary if in SSA form Alternative to code motion: copy instruction to loop header, assigning to new temp, • if reorder defs/uses, generate code along merging arcs to implement φ functions then do CSE & copy propagation to simplify in-loop version • more modular design, leverage off of existing optimizations z 1 := 5 Can always copy, unless instruction has side-effects CSE & copy propagation will eliminate in-loop instruction z 2 := φ (z 1 ,z 4 ) exactly when (non-SSA) loop-invariant code motion would have, PLUS can replace invariant but unmovable z 3 := z 2 + 1 instructions with copies S: z 4 := 0 SSA-based code motion gets same effect • copies correspond to reified φ functions ... z 4 ... Craig Chambers 101 CSE 501 Craig Chambers 102 CSE 501 Example q := 0 y := 1 ... y ... ... q ... q := z * w x := a * b y := q * x Craig Chambers 103 CSE 501

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend