+ All Categories
Home > Documents > Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c”...

Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c”...

Date post: 29-Aug-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
55
Compilers I Loop Optimizations Dr William Harrison
Transcript
Page 1: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Compilers I

Loop Optimizations Dr William Harrison

Page 2: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Loop Optimizations n  Empirical studies show that much of

program execution occurs within loops n  So, it makes sense to identify loops &

concentrate optimization efforts on that code

n  But what is a loop? n  Within source code, it's obvious n  Within IR/CFG, however,…

n  Loops aren't precisely what your first guess would be

Page 3: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Simple loop optimization

within the source code, that is…

for (i = 1; i++ ; i = 10) { c } c ; … ; c

10 times è

Caveat: •  seems reasonable (removes branches) •  not terribly general: would like to use on while loops but how do you identify the induction variable “i”? •  also, how does this apply at the IR/CFG level?

Page 4: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Ex: Loop invariant code hoisting

n  Say the assignment “açb+c” occurs within a loop n  But “b” and “c” aren't assigned in the loop n  Would like to move “açb+c” somewhere “right in

front of” the loop n  Thereby avoiding redundant reassignments to “a”

n  Problem IR/CFG is a directed graph of basic blocks n  Where, for example, is the “front of” a loop?

Page 5: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

A flow graph

Say “açb+c” occurs in 9, To where might we hoist it?

10 12

11 9

8 7

6 5

4 3

2

1

Page 6: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

A flow graph

Say “açb+c” occurs in 9, To where might we hoist it?

–  Nodes 5,4,2,1 seem likely –  how would I determine that automatically? –  Impact on other optimizations? –  Identifying loops with “dominator tree"

10 12

11 9

8 7

6 5

4 3

2

1

Page 7: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Dominators n  Assume we have a flow graph with

entry node s0 with no predecessors n  I.e., no edge into s0

n  Node d dominates n means that n  d occurs in every path from s0 to n

n  Note that every node dominates itself

Page 8: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

A flow graph

–  for each pair of nodes d,n –  does d dominate n? –  can think of “d dominates n” as a new kind of directed arc in a new graph

–  Question: is it possible that for d≠n that:

–  d dominates n, and –  n dominates d?

10 12

11 9

8 7

6 5

4 3

2

1

Page 9: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Theorem

Let d ≠ e both dominate n. Then, either d dominates e or e dominates d (but not both)

Proof sketch. Assume neither d dominates e nor vice versa.

s0

d e

n

Path to e not containing d exists since d doesn’t dominate e

Page 10: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Theorem

Let d ≠ e both dominate n. Then, either d dominates e or e dominates d (but not both)

Proof sketch. Assume neither d dominates e nor vice versa.

s0

d e

n

Path to e not containing d exists since d doesn’t dominate e

Path from s0 to n not through d exists. ∴ d doesn’t dominate n èç

Page 11: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

flow graph & its dominator tree

10 12

11 9

8 7

6 5

4 3

2

1

2

1

3 4

5 6 7 12

11 8

9

10

Page 12: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

flow graph & its dominator tree Not necessarily predecessor in flow graph

10 12

11 9

8 7

6 5

4 3

2

1

2

1

3 4

5 6 7 12

11 8

9

10

Page 13: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Immediate Dominators

n  An immediate dominator of node n is a node idom(n) such that n  idom(n) ≠n n  idom(n) dominates n n  idom(n) does not dominate any other dominator of n

n  Every node has (at most) one immediate dominator n  How do we know this?

Page 14: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Calculating Dominators

Let D[n] be the set of nodes that dominate n in a particular flow graph G, we get the following

two simultaneous equations*

D[s0] = {s0}D[n] = {n} ∪ (∩p∈pred(n) D[p]) for n ≠ s0

*I wonder how we’d solve equations like this?

Page 15: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Iterative solution

change := trueD[s0] := {s0}foreach n∈ (Nodes(G) \ {s0}) { D[n] := {n} }repeat change := false foreach n∈ (Nodes(G) \ {s0})

T := Nodeforeach p ∈ (pred(n) \ {s0}) { T := T∩D[p] }X := {n} ∪ Tif X ≠D[n] then change := true D[n] = X

⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜

⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟

until (not change)

Page 16: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Backedges are edges nèh where h dominates n

1

3 4

2

5 6

8 7

10

9

12

11

1

3 4

2

5 6

8

7

10

9

12

11

Page 17: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

“Natural” Loops

n  The natural loop of a backedge nèh is n  The set of x such that

n  h dominates x n  there is a path from x to n not containing h

n  h is called the header of this loop

Page 18: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Backedges induce natural loops

1

3 4

2

5 6

8 7

10

9

12

11

The four natural loops are: {2,3} {2,4} {5,8,9,10} {8,9}

Page 19: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Identifying loops

1

3 4

2

5 6

8 7

10

9

12

11

By merging natural loops with identical headers, one identifies all loops •  note that a natural loop is what you think of as a loop •  while a loop inside a compiler is a slightly different animal

Page 20: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Identifying nested loops

1

3 4

2

5 6

8 7

10

9

12

11

•  generally start optimizing inside the innermost loop •  Ex: {8,9} nested within {5,8,9,10} •  Reminder: “⊂” means proper subset

•  i.e., A≠B

Defn: Let A,B be loops with headers a,b s.t. a≠b. B is nested within A iff B⊂A

Page 21: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Loop pre-header n  Many loop optimizations require moving code

from inside a loop to just before its header n  To guarantee that we uniformly have such a

place available, we may insert a loop pre-header n  initially empty basic block with a single edge into

the header n  potentially reduces code duplication, among other

things n  the pre-header block dominates the loop (that's

important)

Page 22: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Inserting a loop pre-header

header

B3

B2 B1

header

B3

B2 B1

preheader

Page 23: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Loop invariant computations

n  Let L be a loop and d be “tça⊗b” n  d is loop invariant for L iff

n  “a” and “b” are constant, or n  definitions reaching “a” and “b” occur outside L, or n  only one definition reaches “a” and one reaches “ b” and

they are loop invariant for L

n  This is a conservative estimate of loop invariance n  i.e., it may report d is not loop invariant when it is

n  but it will never say it is loop invariant when it is not

Page 24: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

To hoist or not to hoist…

L0 tç0 L1 içi+1 tça⊗b M[i]çt if i<N goto L1 L2 xçt

L0 tç0 L1 içi+1 tça⊗b M[i]çt tç0 M[j]çt if i<N goto L1 L2 xçt

L0 tç0 L1 M[j]çt içi+1 tça⊗b M[i]çt tç0 if i<N goto L1 L2 xçt

? ? ?

Q: when is it safe to hoist tça⊗b?

Page 25: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Ex 1: To hoist or not to hoist…

L0 tç0 L1 içi+1 tça⊗b M[i]çt if i<N goto L1 L2 xçt

L0 tç0 tça⊗b L1 içi+1 M[i]çt if i<N goto L1 L2 xçt

Before After

* We must determine the criteria for hoisting

Page 26: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Determining the criteria for safe hoisting

L0 tç0 L1 içi+1 tça⊗b M[i]çt if i<N goto L1 L2 xçt

L0 tç0 tça⊗b L1 içi+1 M[i]çt if i<N goto L1 L2 xçt

Before After irrelevant definition: t not live-out

single definition of t

(tça⊗b) dominates L2

a⊗b is loop invariant

Page 27: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Ex 2: To hoist or not to hoist…

L0 tç0 L1 branch (i≥N) L2 içi+1 tça⊗b M[i]çt goto L1 L2 xçt

L0 tç0 tça⊗b L1 branch (i≥N) L2 içi+1 M[i]çt goto L1 L2 xçt

Before After

Page 28: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

To hoist or not to hoist…

L0 tç0 L1 branch (i≥N) L2 içi+1 tça⊗b M[i]çt goto L1 L2 xçt

L0 tç0 tça⊗b L1 branch (i≥N) L2 içi+1 M[i]çt goto L1 L2 xçt

Before After

can be a⊗b or 0 always a⊗b

always executes

only executes when branch fails

Page 29: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Ex 3: To hoist or not to hoist…

L0 tç0 L1 içi+1 tça⊗b M[i]çt tç0 if i<N goto L1 L2

L0 tç0 tça⊗b L1 içi+1 M[i]çt tç0 if i<N goto L1 L2

Before After

Page 30: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

To hoist or not to hoist…

L0 tç0 L1 içi+1 tça⊗b M[i]çt tç0 if i<N goto L1 L2

L0 tç0 tça⊗b L1 içi+1 M[i]çt tç0 if i<N goto L1 L2

Before After

always a⊗b

sometimes a⊗b

Observe: multiple definitions of t inside L1 complicate matters

Page 31: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Ex 4: To hoist or not to hoist…

L0 tç0 L1 M[j]çt içi+1 tça⊗b M[i]çt if i<N goto L1 L2 xçt

L0 tç0 tça⊗b L1 M[j]çt içi+1 M[i]çt if i<N goto L1 L2 xçt

Before After

sometimes a⊗b

always a⊗b

Page 32: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Safe hoisting of d: tça⊗b

n  d dominates all loop exits where t is live, and

n  only one definition of t in the loop, and n  t is not live-out in the preheader n  assumes d is loop invariant

This is only a conservative approximation! - will say some hoistings are unsafe when they are safe - will not say a hoisting is safe when it isn't

Page 33: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

To hoist or not to hoist…

L0 tç0 L1 içi+1 tça⊗b M[i]çt if i<N goto L1 L2 xçt

L0 tç0 tça⊗b L1 içi+1 M[i]çt if i<N goto L1 L2 xçt

Before After t not live-out

one definition of t

(tça⊗b) dominates L2

a⊗b is loop invariant

*Therefore, hoist!

Page 34: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Induction Variable Analysis n  Some loops have an induction variable

n  a variable “i” that is incremented by a constant or loop invariant amount each iteration

n  for loop-inv. “c”, only definitions of the form: n  “i ç i + c” or “i ç i - c”

n  Other variables may depend entirely on “i” n  these are called derived induction variables

n  Identifying induction variables within a loop enables a variety of loop optimizations n  strength reduction: replacing an expensive operation by a

less expensive one n  induction variable elimination: removing the variable,

thereby (perhaps) shortening the code and reducing register pressure

Page 35: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

An Example sç0 iç0 L1 branch (i>n) L2 jçi*4 kçj+a xçM[k] sçs+x içi+1 goto L1 L2 Before

sç0 iç0 jç0 L1 branch (i>n) L2 jçj+4 kçj+a xçM[k] sçs+x içi+1 goto L1 L2 After

i is an induction variable •  i*4 has values 0,4,8,12,… •  can perform strength reduction

Page 36: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Basic Induction Variables n  Given a loop L with header h

n  “i” is a basic induction variable within L iff n  the only definitions of “i” have the form:

n  “i ç i + c” or “i ç i - c” n  for loop-invariant “c”

n  Detection of basic induction variables is done by inspecting their form

Page 37: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Derived Induction Variables in the family of “i” n  If “i” is an induction variable (basic or

otherwise) for loop L, then n  “j” is a derived induction variable in the

family of “i” means n  all definitions of “j” in L are of the form

n  j ç c*i + d n  where c,d are loop invariant n  may be more than one instruction n  Lingo: j is determined by (i,c,d)

Page 38: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Derived induction variables in the family of “i” n  Key Insight: definitions of such a “j”

may be rewritten w/o reference to “i” n  That is, replace definition(s) with the effect

of “j ç c*i + d” with “j ç j + c*a” n  where “i” is incremented by “a” n  c,d --- loop invariant for L n  N.b., “c*a” is either

n  constant: in which case, calculate it n  loop-inv, but not constant

n  in which case compute in the pre-header

Page 39: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Example: i ← i+4, j ← 2*i+5

i 2*i + 5 0 4 8

12 …

5 13 21 29 …

Initialize j ← 5 iteration j ← j+2*4

a=4,c=2

5 13 21 29 …

after each iteration

0th 1st 2nd 3rd …

Page 40: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Detecting derived induction variables Let “j” be an induction variable for L in the

family of “i” n  “k” is a derived induction variable for “j” in

loop L when: n  (Basic) there is only one definition of “k” in L

n  and that definition is of the form: n  “k ç c * j” n  “k ç j + d”

n  where c,d are loop invariant

Page 41: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

“k” is a derived induction variable for “j” in loop L when:

n  (Basic) only one definition of “k” in L n  and that definition is of the form:

n  “k ç c * j” n  “k ç j + d”

n  where c,d are loop invariant

n  (More Complex) n  the only definition of “j” that reaches “k” is in

the loop n  i.e., “k” depends entirely on a single definition of “j”

n  and, no definition of “i” occurs on any path between the definitions of “j” and “k”

n  i.e., the dependence of “k” on “i” via “j” maintained

Page 42: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Array bounds checking

array m[1..100] of int; … m[i] := 77; …

r1 çfp + offseti branch (r1>100) Lerror branch (r1<1) Lerror <code for assignment> …

“Safe” programming languages insert dynamic checks to array references to avoid “out of bounds” references

Page 43: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Array bounds checking

array m[1..100] of int; … m[i] := 77; …

r1 çfp + offseti branch (r1>100) Lerror branch (r1<1) Lerror <code for assignment> …

Under certain circumstances, may be able to determine that the array reference is safe and eliminate the check •  relies on induction variable analysis

Page 44: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Loop Unrolling Overview n  If the body of a loop L is small, it may be that it

spends most of its execution “looping” rather than “computing”; i.e., n  incrementing induction variables n  branching

n  Simple example at source level; replace n  “for (i=0; i++; i<2) { c }” with n  “i=0; c; i=1; c

n  avoids branching, etc. n  The Problem: how do you do this at the machine

code/IR level? n  AKA Software Pipelining

Page 45: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Unrolling a loop (brute force)

h

Let L be the loop:

where h is the header and are backedges

Page 46: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Unrolling a loop (brute force)

h

Make a copy of L

h’

Page 47: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Unrolling a loop (brute force)

h

Reroute the backedges

h’

Page 48: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Effects of “brute force” unrolling

L1: xçM[i] sçs+x içi+4 branch (i<n) L1

L2:

L1: xçM[i] sçs+x içi+4 branch (i≥n) L2

L1’: xçM[i] sçs+x içi+4 branch (i<n) L1

L2:

Before

After

Q: Does this constitute an improvement?

Page 49: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Empirical Studies show: loops with the following are good candidates for unrolling

n  Single Basic Blocks n  i.e., straight line code n  with a limited number of floating point & memory

operations n  why? Limits unrolling to loops that are most likely to

benefit from instruction scheduling (i.e., ordering code w.r.t. architectural features such as data caches)

n  Small in Length n  otherwise unrolling may have negative impact on

instruction cache performance n  Simple Loop Control

n  simplifies the unrolling transformation

Page 50: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Typical unrolling candidate n  Note that half of the

work is in “looping” n  It’s a loop within a

single basic block n  few instructions n  has a “repeat-until”

structure

L1: xçM[i] sçs+x içi+4 branch (i<n) L1

L2:

Page 51: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Fragile* unrolling (K=2)

L1: xçM[i] sçs+x içi+4 branch (i<n) L1

L2:

L1: xçM[i] sçs+x içi+4 xçM[i] sçs+x içi+4 branch (i<n) L1

L2:

Before

After

*Fragile: must establish that middle branch may be removed and that loop iterates an even number of times

Page 52: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

“Fragile” unrolling (K=2)

L1: xçM[i] sçs+x içi+4 branch (i<n) L1

L2:

L1: xçM[i] sçs+x içi+4 xçM[i+4] sçs+x içi+8 branch (i<n) L1

L2:

Before

After

…can do better with induction variable analysis

Page 53: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Robust* unrolling (K=2)

L1: xçM[i] sçs+x içi+4 if (i<n) L1 else L2

L2:

Before

*Robust: i.e., works for any number of iterations.

if (i<n-8) L1 else L2 L1: xçM[i]

sçs+x xçM[i+4] sçs+x içi+8 if (i<n-8) L1 else L2

L2: xçM[i] sçs+x içi+4 if (i<n) L2 else L3

L3:

After

Page 54: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

Robust* unrolling (K=2)

L1: xçM[i] sçs+x içi+4 if (i<n) L1 else L2

L2:

if (i<n-8) L1 else L2 L1: xçM[i]

sçs+x xçM[i+4] sçs+x içi+8 if (i<n-8) L1 else L2

L2: xçM[i] sçs+x içi+4 if (i<n) L2 else L3

L3:

Before

After

*Robust: i.e., works for any number of iterations.

“twice” block

“once” block

Page 55: Compilers I - GitHub Pages · Ex: Loop invariant code hoisting n Say the assignment “açb+c” occurs within a loop n But “b” and “c” aren't assigned in the loop n Would

That's all, folks!


Recommended