Top Banner
Spring 2016 CSCI 565 - Compiler Design Pedro Diniz [email protected] Register Allocation Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations Copyright 2016, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission to make copies of these materials for their personal use.
87

Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Jul 24, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

Register AllocationGlobal Register AllocationWebs and Graph Coloring

Node Splitting and Other Transformations

Copyright 2016, Pedro C. Diniz, all rights reserved.Students enrolled in the Compilers class at the University of Southern California have explicit permission to make copies of these materials for their personal use.

Page 2: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

2

What a Smart Allocator Needs to Do• Determine ranges for each variable can benefit from

using a register (webs)• Determine which of these ranges overlap

(interference)• Find the benefit of keeping each web in a register

(spill cost)• Decide which webs gets a register (allocation)• Split webs if needed (spilling and splitting)• Assign hard registers to webs (assignment)• Generate code including spills (code gen)

Page 3: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

3

Global Register Allocation

What’s harder across multiple blocks?• Could replace a load with a move• Good assignment would obviate the move• Must build a control-flow graph to understand inter-block flow• Can spend an inordinate amount of time adjusting the allocation

...store r4 ⇒ x

load x⇒ r1...

This is an assignment problem,not an allocation problem !

Page 4: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

4

Global Register Allocation

A more complex scenario• Block with multiple predecessors in the control-flow graph• Must get the “right” values in the “right” registers in each

predecessor• In a loop, a block can be its own predecessorsThis adds tremendous complications

...store r4 ⇒ x

load x ⇒ r1...

...store r4 ⇒ x

What if one block has x in a register, but the other does not?

Page 5: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

5

Outline• What is Register allocation and Its Importance• Simple Register Allocators• Webs• Interference Graphs• Graph Coloring• Splitting• More Optimizations

Page 6: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

6

Webs• What needs to Gets Memorized is the Value

• Divide Accesses to a Variable into Multiple Webs– All definitions that reaches a use are in the same web– All uses that use the value defined are in the same web– Divide the Variable into Live Ranges

• Implementation: use DU chains– A du-chain connects a definition to all uses reached by the definition– A web combines du-chains containing a common use

Page 7: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

7

Example

write y

write xread y

write xwrite y

read xwrite x

read x

read xread y

Page 8: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

8

Example

write y

write xread y

write xwrite y

read xwrite x

read x

read xread y

Page 9: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

9

Example

write y

write xread y

write xwrite y

read xwrite x

read x

read xread y

Page 10: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

10

Example

write y

write xread y

write xwrite y

read xwrite x

read x

read xread y

Page 11: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

11

Example

write y

write xread y

write xwrite y

read xwrite x

read x

read xread y

Page 12: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

12

Example

write y

write xread y

write xwrite y

read xwrite x

read x

read xread y

Page 13: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

13

Example

write y

write xread y

write xwrite y

read xwrite x

read x

read xread y

Page 14: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

w1

w2

w3

w4

14

Example

write y

write xread y

write xwrite y

read xwrite x

read x

read xread y

Page 15: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

15

Webs (continued)

• In two Webs of the same Variable:– No use in one web will ever use a value defined by the other web– Thus, no value need to be carried between webs– Each web can be treated independently as values are independent

• Web is used as the Unit of Register Allocation– If a web is allocated to a register, all the uses and definitions within that

web don’t need to load and store from memory– Solves the issue of cross Basic Block register assignment– Different webs may be assigned to different registers or one to register

and one to memory

Page 16: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

16

Outline• What is Register Allocation• A Simple Register Allocator• Webs• Interference Graphs• Graph Coloring• Splitting• More Optimizations

Page 17: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

17

Interference• Two webs interfere if their live ranges

overlap in time– What does time Mean, more precisely?– There exists an instruction common to both

ranges where• They variable values of webs are operands of the

instruction• If there is a single instruction in the overlap

– and the variable for the web that ends at that instruction is an operands and

– the variable for the web that starts at the instruction is the destination of the instruction

• then the webs do not interfere

• Non-interfering webs can be assigned to the same register

.:

a = b op c.:

a

c

.:

a = b op c.:

a

c

Page 18: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

w1

w2

w3

w4

write y

write xread y

write xwrite y

read xwrite x

read x

read xread y

18

Example

Page 19: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

w1

w2

w3

w4

write y

write xread y

write xwrite y

read xwrite x

read x

read xread y

19

ExampleWebs w1 and w2 interfereWebs w2 and w3 interfere

Page 20: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

20

Interference Graph• Representation of Webs & their Interference

– Nodes are the webs– An edge exists between two nodes if they interfere

w1 w2

w3 w4

Page 21: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

21

Example

w1 w2

w3 w4

Page 22: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

w1

w2

w3

w4

write y

write xread y

write xwrite y

read xwrite x

read x

read xread y

22

ExampleWebs w1 and w2 interfereWebs w2 and w3 interfere

w1 w2

w3 w4

Page 23: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

23

Outline• What is Register Allocation• A Simple Register Allocator• Webs• Interference Graphs• Graph Coloring• Splitting• More Optimizations

Page 24: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

24

Reg. Allocation Using Graph Coloring• Each Web is Allocated a Register

– each node gets a register (color)

• If two webs interfere they cannot use the same register– if two nodes have an edge between them, they cannot have the same

color

w1 w2

w3 w4

Page 25: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

25

Graph Coloring• What is the minimum number of colors that takes to color

the nodes of the graph such that any nodes connected with an edge does not have the same color?

• Classic Problem in Graph Theory

Page 26: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

26

Graph Coloring Example

Page 27: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

27

Graph Coloring Example

• 1 Color

Page 28: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

28

Graph Coloring Example

Page 29: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

29

Graph Coloring Example

• 2 Colors

Page 30: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

30

Graph Coloring Example

Page 31: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

31

Graph Coloring Example

• Still 2 Colors

Page 32: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

32

Graph Coloring Example

Page 33: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

33

Graph Coloring Example

• 3 Colors

Page 34: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

34

Heuristics for Register Coloring• Coloring a graph with N colors • If degree < N (degree of a node = # of edges)

– Node can always be colored– After coloring the rest of the nodes, you’ll have at least one color

left to color the current node

• If degree ≥ N– still may be colorable with N colors– exact solution is NP complete

Page 35: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

35

Heuristics for Register Coloring• Remove nodes that have degree < N

– Push the removed nodes onto a stack

• If all the nodes have degree ≥ N – Find a node to spill (no color for that node)– Remove that node

• When empty, start the coloring step– pop a node from stack back– Assign it a color that is different from its connected nodes (since

degree < N, a color should exist)

Page 36: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

36

Coloring ExampleN = 3

w1 w2

w3 w4

w0

Page 37: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

37

Coloring ExampleN = 3

w1 w2

w3 w4

w0

Page 38: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

38

Coloring Example

w1 w2

w3 w4

w0

N = 3

w4

Page 39: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

39

Coloring Example

w1 w2

w3 w4

w0

N = 3

w4w2

Page 40: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

40

Coloring Example

w1 w2

w3

w0

N = 3

w4w2w1

w4

Page 41: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

41

Coloring Example

w1 w2

w3 w4

w0

N = 3

w4w2w1w3

Page 42: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

42

Coloring Example

w1 w2

w3 w4

w0

N = 3

w4w2w1w3

Page 43: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

43

Coloring Example

w1 w2

w3 w4

w0

N = 3

w4w2w1w3

Page 44: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

44

Coloring ExampleN = 3

w4w2w1

w1 w2

w3 w4

w0

Page 45: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

45

Coloring ExampleN = 3

w4w2w1

w1 w2

w3 w4

w0

Page 46: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

46

Coloring ExampleN = 3

w4w2

w1 w2

w3 w4

w0

Page 47: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

47

Coloring ExampleN = 3

w4w2

w1 w2

w3 w4

w0

Page 48: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

48

Coloring ExampleN = 3

w4

w1 w2

w3 w4

w0

Page 49: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

49

Coloring ExampleN = 3

w4

w1 w2

w3 w4

w0

Page 50: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

50

Coloring ExampleN = 3

w1 w2

w3 w4

w0

Page 51: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

51

Coloring ExampleN = 3

w1 w2

w3 w4

w0

Page 52: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

52

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

Page 53: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

53

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4

Page 54: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

54

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4

Page 55: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

55

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3

Page 56: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

56

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3s2

Page 57: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

57

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3s2

Page 58: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

58

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3s2

Page 59: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

59

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3

Page 60: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

60

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4s3

Page 61: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

61

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4

Page 62: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

62

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

s4

Page 63: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

63

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

Page 64: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

64

Another Coloring Example

s1 s2

s3 s4

s0

N = 3

Page 65: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

65

Outline• What is Register Allocation• A simple register Allocator• Webs• Interference Graphs• Graph coloring• Splitting• More Optimizations

Page 66: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

66

Spilling and Splitting• When the graph is non-N-colorable• Select a Web to Spill

– Find the least costly Web to Spill– Use and Defs of that web are read and writes to memory

• Split the web– Split a web into multiple webs so that there will be less interference in

the interference graph making it N-colorable– Spill the value to memory and load it back at the points where the web is

split

Page 67: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

67

Splitting Examplex y z

write zread z

write xwrite yread xread xread y

read z

Page 68: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

68

Splitting Example

write zread z

write xwrite yread xread xread y

read z

x y z

x y

z

Page 69: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

69

Splitting Examplex y z

x y

z

2 colorable?

write zread z

write xwrite yread xread xread y

read z

Page 70: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

70

Splitting Examplex y z

x y

z

2 colorable?NO!

write zread z

write xwrite yread xread xread y

read z

Page 71: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

71

Splitting Examplex y z

write zread z

write xwrite yread xread xread y

read z

Page 72: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

72

Splitting Examplex y z

write zread z

write xwrite yread xread xread y

read z

Page 73: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

73

Splitting Examplex y z

x y

z2

z1

write zread z

write xwrite yread xread xread y

read z

Page 74: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

74

Splitting Example

x y z

x y

z2

z1

2 colorable?

write zread z

write xwrite yread xread xread y

read z

Page 75: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

75

Splitting Examplex y z

x y

z2

z1

2 colorable?YES!

write zread z

write xwrite yread xread xread y

read z

Page 76: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

76

Splitting Examplex y z

r1r2

r1

r1

x y

z2

z1

2 colorable?YES!

write zread z

write xwrite yread xread xread y

read z

Page 77: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

77

Splitting Examplex y z

r1r2

r1

r1

x y

z2

z1

2 colorable?YES!

write zread zstore z

write xwrite yread xread xread y

load zread z

Page 78: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

78

Splitting• Identify a Program Point where the Graph is

not R-colorable (point where # of webs > N)– Pick a web that is not used for the largest enclosing block

around that point of the program– Split that web– Redo the interference graph– Try to re-color the graph

Page 79: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

79

Cost and Benefit of Splitting• Cost of splitting a node

– Proportion to number of times splitted edge has to be crossed dynamically

– Estimate by its loop nesting

• Benefit– Increase colorability of the nodes the splitted web

interferes with– Can approximate by its degree in the interference graph

• Greedy heuristic– pick the live-range with the highest benefit-to-cost ratio to

spill

Page 80: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

80

Outline• Overview of procedure optimizations• What is register allocation• A simple register allocator• Webs• Interference Graphs• Graph coloring• Splitting• More Optimizations

Page 81: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

81

More Transformations• Register Coalescing• Register Targeting (pre-coloring)• Pre-Splitting of Webs• Inter-procedural Register Allocation

Page 82: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

82

Register Coalescing• Find register copy instructions sj = si

• If sj and si do not interfere, combine their webs• Pros

– Similar to copy propagation– Reduce the number of instructions

• Cons– May increase the degree of the combined node– A colorable graph may become non-colorable

Page 83: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

83

Register Targeting (pre-coloring)• Some Variables need to be in Special

Registers at Specific Points in the Execution– first 4 arguments to a function– return value

• Pre-color those webs and bind them to the appropriate register

• Will eliminate unnecessary copy instructions

Page 84: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

84

Pre-Splitting of the Webs• Some Ranges have Very Large “dead” Regions

– Large region where the variable is unused

• Break-up the Ranges– need to pay a small cost in spilling – but the graph will be very easy to color

• Can find Strategic Locations to Break-up– at a call site (need to spill anyway)– around a large loop nest (reserve registers for values used in

the loop)

Page 85: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

85

Inter-Procedural Register Allocation• Saving Registers across Procedure boundaries is

expensive – especially for programs with many small functions

• Calling convention is too general and inefficient

• Customize calling convention per function by doing inter-procedural register allocation

Page 86: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

86

Chaitin-Briggs Allocatorrenumber

build

coalesce

spill costs

simplify

select

spill

Build SSA, build live ranges, rename

Build the interference graph

Fold unneeded copies LRx→ LRy, and < LRx,LRy> ∉GI⇒ combine LRx & LRy

Remove nodes from the graph

Spill uncolored definitions & uses

While stack is non-emptypop n, insert n into GI, & try to color it

Estimate cost for spilling each live range

while N is non-emptyif ∃ n with n°< k then

push n onto stackelse pick n to spill

push n onto stackremove n from GI

Briggs’algorithm (1989)

Page 87: Register Allocation - Information Sciences InstituteGlobal Register Allocation A more complex scenario • Block with multiple predecessors in the control-flow graph • Must get the

Spring 2016CSCI 565 - Compiler Design

Pedro [email protected]

87

Summary• Register Allocation and Assignment

– Very Important Transformations and Optimization– In General Hard Problem (NP-Complete)

• Many Approaches– Local Methods: Top-Down and Bottom-Up– Global Methods: Graph Coloring

• Webs• Interference Graphs• Coloring

– Other Transformations