23
Loop invariant code removal CS 480

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]

Embed Size (px)

Citation preview

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] * b[k, j]

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

Graph representation

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

Graph rep of our programNode 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) <- @(c+t7) + @(a + (t4 * p + t8)) * @(b + (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)

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

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

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

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) <- @(c+t7) + @(a + (t4 * p + t8)) * @(b + (t8 * m + t5)) k <- k + 1 goto node 6

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) <- @(c+t7) + @(a + (t4 * p + t8)) * @(b + (t8 * m + t5)) k <- k + 1 goto node 6

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) <- @(c+t7) + @(a + (t4 * p + t8)) * @(b + (t8 * m + t5)) k <- k + 1 goto node 6

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) <- @(c+t7) + @(a + (t4 * p + t8)) * @(b + (t8 * m + t5)) k <- k + 1 goto node 6

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) <- @(c+t7) + @(a + (t4 * p + t8)) * @(b + (8 * m + t5)) k <- k + 1 goto node 6

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) <- @(c+t7) + @(a + (t4 * p + t8)) * @(b + (t8 * m + t5)) k <- k + 1 goto node 6

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) + @(a + (t4 * p + t8)) * @(b + (8 * m + 5)) k <- k + 1 goto node 6

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) <- @(c+t7) + @(a + (t6 + t8)) * @(b + (8 * m + t5)) k <- k + 1 goto node 6

Do it again

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

more values

Next innermost Loop, changingNode 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) <- @(c+t7) + @(a + (t6 + t8)) * @(b + (8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4

Invariant expressionsNode 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) <- @(c+t7) + @(a + (t6 + t8)) * @(b + (t8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4

Move them outNode 3 t2 <- m j <- 1 t4 <- i * 4 - 4Node 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) <- @(c+t7) + @(a + (t6 + t8)) * @(b + (t8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4

Do it again, find a new oneNode 3 t2 <- m j <- 1 t4 <- i * 4 – 4 t6 <- t4 * p t9 <- t4 * mNode 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) <- @(c+t7) + @(a + (t6 + t8)) * @(b + (t8 * m + t5)) k <- k + 1 goto node 6 Node 8 j <- j + 1 goto Node 4

Finally go to outermost loop

• Go to outermost loop• But basically nothing changes here

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