Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loop invariant code removal CS 480. Our sample calculation for i := 1 to n for j := 1 to m c [i, j] := 0 for k := 1 to p c[i, j] := c[i, j] + a[i, k]

Similar presentations


Presentation on theme: "Loop invariant code removal CS 480. Our sample calculation for i := 1 to n for j := 1 to m c [i, j] := 0 for k := 1 to p c[i, j] := c[i, j] + a[i, k]"— Presentation transcript:

1 Loop invariant code removal CS 480

2 Our sample calculation for i := 1 to n for j := 1 to m c [i, j] := 0 for k := 1 to p c[i, j] := c[i, j] + a[i, k] * b[k, j]

3 Translating for statement for i := 1 to n is translated as: temp := n i := 1 L1: if (i > temp) goto L2... i := i + 1 goto L1

4 Graph representation Each node has a DAG (series of assignments or function calls) and ends with either 1)unconditional transfer (goto a new node) 2)conditional transfer or a unconditional goto 3) function or procedure exit (basically a fancy type of branch).

5 Graph rep of our program Node 1 t1 <- n i <- 1 (implicit goto node 2) Node 2 If i > t1 goto node 10 Node 3 t2 <- m j <- 1 Node 4 if j > t2 goto node 9 Node 5 (c + ((i*4-4) * m + (j*4-4))) <- 0 t3 <- p k <- i Node 6 If k > t3 goto node 8 Node 7 t4 <- i*4-4 t5 <- j*4-4 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4 Node 9 i <- i + 1 goto Node 2 Node 10 (procedure exit)

6 Multi step process Make a list of all variables that are potentially modified in the body of the loop Look for expressions that do NOT involve any of these variables Move any such expressions out of the body of the loop (may need to make new temps) Repeat until nothing new is found

7 What variables are changing? Find the list of all the variables that can potentially change Assignments Pass-by-reference parameters Global variables Targets of pointers

8 What is a loop? Can define a loop in terms of graph representation Loop header (can’t get into the loop without going through header) Loop test (way to get out of loop) Loop body (one or more nodes that go back to loop test) Can not get into loop body from anyplace else

9 Innermost loop Node 5 (c + ((i*4-4) * m + (j*4-4))) <- 0 t3 <- p k <- i Node 6 If k > t3 goto node 8 Node 7 t4 <- i*4-4 t5 <- j*4-4 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6

10 Values that are changing Node 5 (c + ((i*4-4) * m + (j*4-4))) <- 0 t3 <- p k <- i Node 6 If k > t3 goto node 8 Node 7 t4 <- i*4-4 t5 <- j*4-4 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6

11 Expressions that are not changing Node 5 (c + ((i*4-4) * m + (j*4-4))) <- 0 t3 <- p k <- i Node 6 If k > t3 goto node 8 Node 7 t4 <- i*4-4 t5 <- j*4-4 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6

12 Lets move them out of loop Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 (c + ((i*4-4) * m + (j*4-4))) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6

13 Now have new common subexps Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 (c + (t4 * m + t5)) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (8 * m + t5)) k <- k + 1 goto node 6

14 Things have changed, so repeat Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 (c + (t4 * m + t5)) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) + (t4 * p + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6

15 More new invariant expressions Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 (c + (t4 * m + t5)) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t7 <- t4 * m + t5 t8 <- k*4-4 (c + t7) <- deref(c+t7) + (t4 * p + t8)) + (8 * m + 5)) k <- k + 1 goto node 6

16 Move it out, find more common exps Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 t7 <- t4 * m + t5 t6 <- t4 * p (c + t7) <- 0 t3 <- p k <- 0 Node 6 If k > te3 goto node 8 Node 7 t8 <- k*4-4 (c + t7) + (t6 + t8)) + (8 * m + t5)) k <- k + 1 goto node 6

17 Do it again Do it once more This time nothing changes So then you back out to the next loop, find more values

18 Next innermost Loop, changing Node 3 t2 <- m j <- 1 Node 4 if j > t2 goto node 9 Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 t7 <- t4 * m + t5 t6 <- t4 * p (c + t7) <- 0 t3 <- p k <- 0 Node 6 If k > te3 goto node 8 Node 7 t8 <- k*4-4 (c + t7) + (t6 + t8)) + (8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4

19 Invariant expressions Node 3 t2 <- m j <- 1 Node 4 if j > t2 goto node 9 Node 5 t4 <- i * 4 – 4 t5 <- j * 4 – 4 t7 <- t4 * m + t5 t6 <- t4 * p (c + t7) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t8 <- k*4-4 (c + t7) + (t6 + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4

20 Move them out Node 3 t2 <- m j <- 1 t4 <- i * Node 4 if j > t2 goto node 9 Node t5 <- j * 4 – 4 t7 <- t4 * m + t5 t6 <- t4 * p (c + t7) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t8 <- k*4-4 (c + t7) + (t6 + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4

21 Do it again, find a new one Node 3 t2 <- m j <- 1 t4 <- i * 4 – 4 t6 <- t4 * p t9 <- t4 * m Node 4 if j > t2 goto node 9 Node t5 <- j * 4 – 4 t7 <- t9 + t5 (c + t7) <- 0 t3 <- p k <- 0 Node 6 If k > t3 goto node 8 Node 7 t8 <- k*4-4 (c + t7) + (t6 + t8)) + (t8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4

22 Finally go to outermost loop Go to outermost loop But basically nothing changes here

23 So, are we done? Originally had +/-: 12 and */%: 7 in innermost loop Now have +/-: 8 and */%: 3 Are we done? Can we do better? Sure we can.. Next lecture


Download ppt "Loop invariant code removal CS 480. Our sample calculation for i := 1 to n for j := 1 to m c [i, j] := 0 for k := 1 to p c[i, j] := c[i, j] + a[i, k]"

Similar presentations


Ads by Google