Docsity

IR Code Optimization Techniques - Prof. Jingke Li, Papers of Computer Science

Toggle sidebar
bg1
IR Code Optimization
Jingke Li
Portland State University
Jingke Li (Portland State University) CS322 IR Code Optimization 1 / 26
IR Code Optimization
Optimizations transform a program into a functionally-equivalent program
with better performance. They can b e performed at various stages and
levels.
Advantages of IR-Level Optimization:
•All operations are explicit, so cost estimations can be accurate.
•Optimizations at this level are machine-independent, hence the results
are portable across different target machines.
Scopes of Optimization:
•Local — Transforming code by analyzing a single basic block.
•Global — Transforming code by analyzing a whole subroutine.
•Inter-Procedural — Trans··· by analyzing the whole program.
Concepts and Techniques:
•Basic blocks & flow graphs
•Control-flow analysis & data-flow analysis
Jingke Li (Portland State University) CS322 IR Code Optimization 2 / 26
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download IR Code Optimization Techniques - Prof. Jingke Li and more Papers Computer Science in PDF only on Docsity!

IR Code Optimization

Jingke Li

Portland State University

Jingke Li (Portland State University) CS322 IR Code Optimization 1 / 26

IR Code Optimization

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:

  • (^) All operations are explicit, so cost estimations can be accurate.
  • Optimizations at this level are machine-independent, hence the results are portable across different target machines.

Scopes of Optimization:

  • Local — Transforming code by analyzing a single basic block.
  • Global — Transforming code by analyzing a whole subroutine.
  • Inter-Procedural — Trans· · · by analyzing the whole program.

Concepts and Techniques:

  • (^) Basic blocks & flow graphs
  • Control-flow analysis & data-flow analysis

Central Theme: Redundancy Elimination

A common IR code optimization theme is removing redundant computations. The following are some concrete examples:

  • (^) Common Subexpression Elimination — Based on lexical representation, applicable to global scope.
  • Partial Redundancy Elimination — More powerful than CSE.
  • Copy Propagation — A companion optimization to CSE.
  • Value Numbering — Value based, very efficient, applies to a single block.
  • Superlocal Value Numbering — Extends VN to multiple blocks.
  • Loop Invariant Elimination — Removes a special form of redundancy.

Jingke Li (Portland State University) CS322 IR Code Optimization 3 / 26

Common Subexpression Elimination

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.

A Global CSE Example (cont.)

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

CSE Algorithm

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:

  1. Compute available expressions for all expressions.
  2. At each node n : w := x ⊕ y , where the expression x ⊕ y is available:
    • Search backwards for the evaluations of x ⊕ y that reach n. Replace each evaluation v := x ⊕ y found in the search by t := x ⊕ y ; v := t. Replace n by w := t.

An Illustrating Example

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

An Improved CSE Algorithm

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:

  • Reduce the number of new temps by assigning a unique name to each unique expression.
  • Avoid backward search by a separate traversal of the CFG.

Algorithm:

  1. Compute available expressions for all expressions.
  2. Initialize an array Name[e] = φ for all expressions.
  3. At each node n : w := x ⊕ y , where the expression x ⊕ y (denoted e below) is available: - If Name[e] = φ, allocate new name t and set Name[e] = t. - Else let t = Name[e]. - Replace n by w := t.
  4. In a subsequent traversal of CFG, at each node of form v := e, if Name[e] 6 = φ, let t = Name[e]; replace the node by t := e; v := t.

Copy Propagation

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

The Cascading Problem

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

Value Numbering

The Idea:

  • (^) Each variable is assumed to have a unique initial value.
  • (^) Each unique value is assigned a unique number.
  • (^) An expression’s value is represented by a corresponding symbolic expression based on the operands’ numbers, e.g. expression “x + y ”’s value is “ 1 ”+“ 2 ”, if “ 1 ” and “ 2 ” are x and y ’s value numbers, respectively.
  • (^) Each unique expression value is also assigned a unique number.
  • (^) When a new variable or expression is encountered, check to see if it has been assigned a number, if so, use the number, otherwise assign it a new number.
  • Use a hash table for efficient number lookup.

Jingke Li (Portland State University) CS322 IR Code Optimization 15 / 26

A Value-Numbering Example

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.

Loop Invariant Code Motion

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

Loop Invariant Criteria

A statement S : t ← a 1 ⊕ a 2 is loop-invariant within loop L if, for each operand ai ,

  1. ai is a constant, or
  2. all the definitions of ai that reach S are outside the loop, or
  3. only one definition of ai reaches S, and that definition is loop-invariant.

An iterative algorithm can be used to find all loop-invariant statements.

Counter Examples

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

PRE vs. Loop Invariant Code Motion

  • A loop invariant expression is partially redundant.
  • PRE hence can remove this redundancy.

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

Induction-Variable Elimination (cont.)

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

Final CFG for Quicksort

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