







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Various optimization techniques for ir code, including common subexpression elimination (cse), global cse, partial redundancy elimination (pre), copy propagation, value numbering, loop invariant code motion, reduction in strength, and induction-variable elimination. It also discusses the cascading problem and limitations of value numbering.
Typology: Papers
1 / 13
This page cannot be seen from the preview
Don't miss anything!








Jingke Li
Portland State University
Jingke Li (Portland State University) CS322 IR Code Optimization 1 / 26
Optimizations transform a program into a functionally-equivalent program with better performance. They can be performed at various stages and levels.
Advantages of IR-Level Optimization:
Scopes of Optimization:
Concepts and Techniques:
A common IR code optimization theme is removing redundant computations. The following are some concrete examples:
Jingke Li (Portland State University) CS322 IR Code Optimization 3 / 26
An occurrence of an expression E is a common subexpression if E was previously computed, and its value has not changed.
To eliminate a common subexpression, one can introduce a temp to hold the value of the subexpression when it is first evaluated.
Example:
t6 := 4i x := a[t6] t7 := 4i t8 := 4j t9 := a[t8] a[t7] := t t10 := 4j a[t10] := x goto B
t6 := 4i x := a[t6] t7 := t t8 := 4j t9 := a[t8] a[t7] := t t10 := t a[t10] := x goto B
B5 B
Before After The second occurrence of 4i in the above basic block is a common subexpression; so is the second occurrence of 4j.
i := m-1j := n t1 := 4*nv := a[t1]
i := i+1t2 := 4*i t3 := a[t2]if t3<v goto B
j := j-1t4 := 4*j t5 := a[t4]if t5>v goto B
if i>=j goto B x := t3a[t2] := t a[t4] := xgoto B
x := t3t14 := a[t1] a[t2] := t14a[t1] := x
B
B
B
B B5 B
After
Jingke Li (Portland State University) CS322 IR Code Optimization 7 / 26
First, the concept of available expressions — An expression x ⊕ y is available at node n if every path from the entry node to n evaluates the expression, and there are no definitions of x or y after the last evaluation.
Algorithm:
i := j a := 4*i
i := i+ b := 4*i
c := 4i d := 4i
B
B
B
Before
i := j t2 := 4*i t1 := t a := t
i := i+ t2 := 4*i t1 := t b := t
c := t d := t
B
B
B
After
Jingke Li (Portland State University) CS322 IR Code Optimization 9 / 26
The previous CSE algorithm performs the expensive backward search and inserts a new temp for every use of a common subexpression. The following ideas can be be used to improve the algorithm:
Algorithm:
Copy statement has the form f := g.
A large number of copy statements may be generated after performing a CSE optimization. Copy propagation eliminates copy statements by using g for f wherever possible.
Example:
t6 := 4i x := a[t6] t7 := t t8 := 4j t9 := a[t8] a[t7] := t t10 := t a[t10] := x goto B
t6 := 4i x := a[t6] t8 := 4j t9 := a[t8] a[t6] := t a[t8] := x goto B
B B
Before
After
Jingke Li (Portland State University) CS322 IR Code Optimization 13 / 26
CSE transformation may have a cascading effect — more rounds of CSE/Copy-propagation may be needed before reaching the final form:
x := b + c y := a + x u := b + c v := a + u
x := b + c y := a + x u := x v := a + u
x := b + c y := a + x v := a + x
x := b + c y := a + x v := y
The Idea:
Jingke Li (Portland State University) CS322 IR Code Optimization 15 / 26
x := b + c y := a + x u := b + c v := a + u
statement var or expr asgnd# x := b + c b 1 c 2 b + c (“1”+“2”) 3 x 3 y := a + x a 4 a + x (“4”+“3”) 5 y 5 u := b + c u (“1”+“2”) 3 v := a + u v (“4”+“3”) 5
Value numbering uses a single round to calculate the effect of cascaded optimizations.
If a loop contains a statement t ← a ⊕ b such that a and b have the same values each time around the loop, then t will also have the same value each time. We would like to hoist such loop-invariant statement out of the loop.
t <- 0
i <- i+ t <- a⊕b M[i] <- t if a<N
x <- t
B
B
B
t <- 0 t <- a⊕b
i <- i+ M[i] <- t if a<N
x <- t
B
B
B
Jingke Li (Portland State University) CS322 IR Code Optimization 19 / 26
A statement S : t ← a 1 ⊕ a 2 is loop-invariant within loop L if, for each operand ai ,
An iterative algorithm can be used to find all loop-invariant statements.
Not all loop-invariant statements can be hoisted out of a loop.
t <- 0
if a>=N
i <- i+1t <- a⊕b M[i] <- t
x <- t
B
B
B
B
t <- 0
i <- i+ t <- aM[i] <- t⊕b t <- 0M[j] <- t if a<N
x <- t
B
B
B
t <- 0
i <- i+ M[j] <- tt <- a⊕b M[i] <- tif a<N
x <- t
B
B
B
Jingke Li (Portland State University) CS322 IR Code Optimization 21 / 26
t <- 0
i <- i+ t <- a⊕b M[i] <- t if a<N
x <- t
B
B
B
t <- 0 t <- a⊕b
i <- i+ M[i] <- t if a<N
x <- t
B
B
B
i, j, t2, and t4 are four induction variables. Since the only use of i and j is the comparison “i>=j” in block B5. However, we have “i>=j” ⇔ “4i>=4j” ⇔ “t2>=t4”
After replacing “i>=j” by “t2>=t4”, both i and j become dead variables, hence can be eliminated.
Jingke Li (Portland State University) CS322 IR Code Optimization 25 / 26
i := m-1j := n t1 := 4n v := a[t1] t2 := 4it4 := 4*j
t2 := t2+ t3 := a[t2]if t3<v goto B
t4 := t4-4t5 := a[t4] if t5>v goto B
if t2>=t4 goto B
x := t a[t2] := t5a[t4] := x goto B
x := t t14 := a[t1]a[t2] := t a[t1] := x
B
B
B
B
B5 B