Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operator Strength Reduction C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students.

Similar presentations


Presentation on theme: "Operator Strength Reduction C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students."— Presentation transcript:

1 Operator Strength Reduction C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University have explicit permission to make copies of these materials for their personal use.

2 COMP 512, Fall 20032 Operator Strength Reduction Consider the following loop What’s wrong with this picture Takes 3 operations to compute the address of a(i) On most machines, the integer multiply (t2) is slow sum = 0 do i = 1 to 100 sum = sum + a(i) dnd do loadI 0  r sum loadI 1  r i loadI 100  r 100 loop:subIr i, 1  r 1 multIr 1, 4  r 2 addIr 2, @ a  r 3 loadr 3  r 4 add r 4,r sum  r sum addIr i, 1  r i cmp _LT r i,r 100  r 5 cbr r 5  loop,exit exit:... } address of a(i)

3 COMP 512, Fall 20033 Operator Strength Reduction Consider the value sequences for the temporaries The only one we care about is r 3 We can compute it directly & cheaply loadI 0  r sum loadI 1  r i loadI 100  r 100 loop:subIr i, 1  r 1 multIr 1, 4  r 2 addIr 2, @ a  r 3 loadr 3  r 4 add r 4,r sum  r sum addIr i, 1  r i cmp _LT r i,r 100  r 5 cbr r 5  loop,exit exit:... r i = { 1, 2, 3, 4, … } r 1 = { 0, 1, 2, 3, … } r 2 = { 0, 4, 8, 12, … } r 3 = { @ a, @ a+4, @ a+8, @ a+12, … }

4 COMP 512, Fall 20034 Operator Strength Reduction Computing r 3 directly yields From 8 operations in the loop to 6 operations No expensive multiply, just cheap adds loadI 0  r sum loadI 1  r i loadI 100  r 100 loadI @ a  r 3 loop:loadr 3  r 4 addIr 3, 4  r 3 add r 4,r sum  r sum addIr i, 1  r i cmp _LT r i,r 100  r 5 cbr r 5  loop,exit exit:... r 3 = { @ a, @ a+4, @ a+8, @ a+12, … } Still, we can do better... * address of a(i)

5 COMP 512, Fall 20035 Operator Strength Reduction Changing the loop’s exit test to use r 3 yields Address computation went from -,+,* to + Exit test went from +, cmp to cmp Loop body went from 8 operations to 5 operations Got rid of that expensive multiply, too loadI 0  r sum loadI @ a  r 3 addIr 3, 396  r lim loop:loadr 3  r 4 addIr 3, 4  r 3 add r 4,r sum  r sum cmp _LT r 3,r lim  r 5 cbr r 5  loop,exit exit:... r 3 = { @ a, @ a+4, @ a+8, @ a+12, … } } Pretty good speed up for most machines 37.5% of ops in the loop

6 COMP 512, Fall 20036 Operator Strength Reduction Definition Strong form Replace series of multiplies with adds Weak form Replace single multiply with shifts and adds The Problem Its easy to see the transformation Its somewhat harder to automate the process Operator Strength Reduction is a transformation that replaces a strong (expensive) operator with a weaker (cheaper) operator

7 COMP 512, Fall 20037 Operator Strength Reduction Assumptions Low-level IR, such as ILOC, converted into SSA form Interpret SSA -form as a graph Terminology A strongly connected component ( SCC ) of a directed graph is a region where a path exists from each node to every other node A region constant ( RC ) of an SCC is an SCC -invariant value An induction variable ( IV ) of an SCC is one whose value only changes in the SCC when operations increment it by an RC or an IV, or when it is the destination of a COPY from another IV A candidate for reduction is an operation “x  y * z” where y, z  IV  RC and either y  IV or z  IV * Intuitively, we are interested in induction variables that are updated in a cyclic fashion. This creates the repetition from which OSR derives its benefits. The classic papers, however, define IVs this way. As you will see, our algorithm only finds IVs that form a cycle in the SSA graph

8 COMP 512, Fall 20038 Operator Strength Reduction loadI 0  r s0 loadI 1  r i0 loadI 100  r 100 loop:phir s0,r s2  r s1 phir i0,r i2  r i1 subIr i1, 1  r 1 multIr 1, 4  r 2 addIr 2, @ a  r 3 loadr 3  r 4 add r 4,r s1  r s2 addIr i1, 1  r i2 cmp _LT r i2,r 100  r 5 cbr r 5  loop,exit exit:... Code in semi-pruned S SA FormS SA Form as a Graph { Short-lived temporary values { 0 Ø load 1 - 1 + Ø 1 4 * @a + + r4r4 r s0 r s1 r s2 r i0 r i1 r i2 r3r3 r2r2 r1r1 cmp _LT cbr r5r5 pc 100

9 COMP 512, Fall 20039 Operator Strength Reduction S SA form as a graph Each IV is an SCC Not every SCC is an IV x  RC if x is a constant or its definition dominates the SCC S SA simplifies O SR Find IV s with SCC finder Test operations in SCC Constant time test for RC > Constant or D OM Without SSA, need several passes 0 Ø load 1 - 1 + Ø 1 4 * @a + + r4r4 r s0 r s1 r s2 r i0 r i1 r i2 r3r3 r2r2 r1r1 cmp _LT cbr r5r5 pc 100

10 COMP 512, Fall 200310 Operator Strength Reduction Finding SCC s Use Tarjan’s algorithm Well-understood method Takes O(N+E ) time Useful property SCC popped only after all its external operands have been popped Reduce the SCC s as popped  | SCC | > 1  if its an IV, mark it  | SCC | = 1  try to reduce it DFS(n) n.DFSnum  nextDFSnum++ n.visited  true n.low  n.DFSnum push(n) for each o  { operands of n} if o.visited = false then DFS(o) n.low  min(n.low, o.low) if o.DFSnum < n.DFSnum and o  stack then n.low  min(n.low, o.DFSnum) if n.low = n.DFSnum then SCC  { } until x = n do x  pop() SCC  SCC  { x } We only need to add one line Process( SCC ) *

11 COMP 512, Fall 200311 Operator Strength Reduction What should Process(r) do? If r is one node, try to reduce it If r is a collection of nodes  Check to see if it is an IV  If so, reduce it & any ops that use it  If not, try to reduce the ops in r Process(r) if r has only one member, n then if n has the form x  IV x RC, x  RC x IV, x  IV ± RC, or x  RC + IV then Replace(n, IV, RC ) else n.header  NULL else ClassifyIV(r) Let’s tackle the easier problem first – ClassifyIV()

12 COMP 512, Fall 200312 Operator Strength Reduction ClassifyIV(r) header  first(r) for each node n  r if header  RPOnum > n.block  RPOnum then header  n.block for each node n  r if n.op is not one of { Ø, +, -, COPY } then r is not an induction variable else for each o  { operands of n } if o  r and not RCon(o,header) then r is not an induction variable if r is an induction variable then for each node n  r n.header  header else for each node n  r if n has the form x  IV x RC, x  RC x IV, x  IV ± RC, or x  RC + IV then Replace(n, IV, RC ) else n.header  NULL RCon(n,header) if n.op is loadI or n.block >> header then return true else return false { Find outer- most def { Reduce these ops >> means “strictly dominates”

13 COMP 512, Fall 200313 Operator Strength Reduction /* replace n with a COPY */ Replace(n,iv,rc) result  Reduce(n.op,iv,rc) Replace n with COPY from result n.header  iv.header /* create new IV & return its name */ Reduce(op,iv,rc) result  search(op,iv,rc) if result is not found then result  a new name add(op,iv,rc,result) newDef  copyDef(iv,result) for each operand o of newDef if o.header = iv.header then replace o with Reduce(op,o,rc) else if (opcode = x or newDef.op = Ø) then replace o with Apply(op,o,rc) return result * Returns name of op applied to iv and rc Clones the definition The Big Picture Reduce() creates a new IV, with appropriate range & increment For t3, in our example, would be range of @ a to @ a+396, with an increment of 4 Replace takes a candidate operation and rewrites it with a COPY from the new IV. It uses Reduce to create the IV. Net effect: replace (i-1)*4+ @ a with a COPY from some new IV that runs from @ a to @ a+396 & increments by 4 on each iteration

14 COMP 512, Fall 200314 Operator Strength Reduction /* insert a new operation */ Apply(op,arg1,arg2) result  search(op,arg1,arg2) if result is not found then if (arg1.header ≠ NULL /*  IV */ & RCon(arg2,arg1.header) then result  Reduce(op,arg1,arg2) else if arg2.header ≠ NULL /*  IV */ & RCon(arg1,arg2.header) then result  Reduce(op,arg2,arg1) else result  a new name add(op,arg1,arg2,result) Choose a location to insert op Try constant folding Create newOp at the location newOper.header  NULL return result The Big Picture Apply takes an op & 2 args and inserts the corresponding operation into the code (if it isn’t already there). Uses >> on arg1 & arg2 to find a location Tries to reduce the operation Tries to simplify the operation Net effect: replace (i-1)*4+a with a COPY from some new IV that runs from @a to @a+396 & increments by 4 on each iteration *

15 COMP 512, Fall 200315 Example And, most of this is dead... 0 Ø load + r4r4 r s0 r s1 r s2 1 + Ø 1 r i0 r i1 r i2 cmp _LT cbr r5r5 pc 1 + Ø 0 r a0 r a1 r a2 COPY r1r1 4 + Ø 0 r a3 r a4 r a5 COPY r2r2 4 + Ø @a@a r a6 r a7 r a8 COPY r3r3 100 *

16 COMP 512, Fall 200316 Example 0.0 Ø load + r4r4 r s0 r s1 r s2 1 + Ø 1 r i0 r i1 r i2 cmp _LT cbr r5r5 pc 4 + Ø @a@a r a6 r a7 r a8 COPY r3r3 This is dead, except for the comparison & branch. Need to reformulate them on r a8 100 The transformation to perform this simplification is called linear function test replacement *

17 COMP 512, Fall 200317 Linear Function Test Replacement Each time a new, reduced IV is created Add an LFTR edge from old IV to new IV Label edge with the opcode and RC of the reduction Walk the LFTR edges to accumulate the transformation Use transformation to rewrite the test

18 COMP 512, Fall 200318 Example pc 0 Ø load + r4r4 r s0 r s1 r s2 1 + Ø 1 r i0 r i1 r i2 cmp _LT cbr r5r5 1 + Ø 0 r a0 r a1 r a2 COPY r1r1 4 + Ø 0 r a3 r a4 r a5 COPY r2r2 4 + Ø @a@a r a6 r a7 r a8 COPY r3r3 100 x 4 + @ a Follow the edges to find the right IV and to accumulate the transformation

19 COMP 512, Fall 200319 Example 1 + Ø 1 r i0 r i1 r i2 Now, this is dead, too Not dead ! r a8 0.0 Ø load + r4r4 r s0 r s1 r s2 4 + Ø @a@a r a6 r a7 COPY r3r3 cbr r5r5 pc 396+ @ a cmp _LT

20 COMP 512, Fall 200320 Example loadI 0  r sum loadI @ a  r 3 addIr3, 396  r lim loop:loadr 3  r 4 addIr 3, 4  r 3 add r 4,r sum  r sum cmp _LT r 3,r lim  r 5 cbr r 5  loop,exit exit:... And, we’re done.. r a8 0.0 Ø load + r4r4 r s0 r s1 r s2 4 + Ø @a@a r a6 r a7 COPY r3r3 cbr r5r5 pc 396+ @ a cmp _LT


Download ppt "Operator Strength Reduction C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students."

Similar presentations


Ads by Google