Presentation is loading. Please wait.

Presentation is loading. Please wait.

Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register.

Similar presentations


Presentation on theme: "Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register."— Presentation transcript:

1 Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register allocation Register allocation Popular techniques Popular techniques Local vs. global Local vs. global Graph coloring Graph coloring Bin packing Bin packing

2 Local Register Assignment Given Given Control-flow graph of basic blocks Control-flow graph of basic blocks List of 3-addr statements per BB List of 3-addr statements per BB Set of “live” scalar values per stmt Set of “live” scalar values per stmt Sets of scalar values used, defined per stmt Sets of scalar values used, defined per stmt Design a local register assignment/allocation algorithm Design a local register assignment/allocation algorithm

3 Graph Coloring Assign a color to each node in graph Assign a color to each node in graph Two nodes connected by an edge must have different colors Two nodes connected by an edge must have different colors Classic problem in graph theory Classic problem in graph theory NP complete NP complete But good heuristics exist for register allocation But good heuristics exist for register allocation

4 Live Ranges def y def x use y def x def y use x def x use x use y

5 Graph Coloring Register Assign Each value is allocated a (symbolic) register Each value is allocated a (symbolic) register “Variables” interfere iff live ranges overlap “Variables” interfere iff live ranges overlap Two interfering values cannot share register Two interfering values cannot share register How can we tell if two values interfere? How can we tell if two values interfere? s1s2 s3s4

6 Interference Graph Values and interference Values and interference Nodes are the values Nodes are the values Edge between two nodes iff they interfere Edge between two nodes iff they interfere s1s2 s3s4

7 Graph Coloring Example

8 3 Colors

9 Heuristics for Register Coloring Coloring a graph with N colors Coloring a graph with N colors For each node, m For each node, m If degree(m) < N If degree(m) < N Node can always be colored, because Node can always be colored, because After coloring adjacent nodes, at least one color left for current node After coloring adjacent nodes, at least one color left for current node If degree(m) >= N If degree(m) >= N Still may be colorable with N colors Still may be colorable with N colors

10 Heuristics for Register Coloring Remove nodes that have degree < N Remove nodes that have degree < N Push the removed nodes onto a stack Push the removed nodes onto a stack When all the nodes have degree >= N When all the nodes have degree >= N Find a node to spill (no color for that node) Find a node to spill (no color for that node) Remove that node Remove that node When graph empty, start to color When graph empty, start to color Pop a node from stack back Pop a node from stack back Color node different from adjacent (colored) nodes Color node different from adjacent (colored) nodes

11 Another Coloring Example s1s2 s3s4 s0 N = 3

12 Another Coloring Example s1s2 s3s4 s0 N = 3 s4

13 Another Coloring Example s1s2 s3s4 s0 N = 3 s4

14 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3

15 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3 s2

16 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3 s2

17 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3 s2

18 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3

19 Another Coloring Example s1s2 s3s4 s0 N = 3 s4 s3

20 Another Coloring Example s1s2 s3s4 s0 N = 3 s4

21 Another Coloring Example s1s2 s3s4 s0 N = 3 s4

22 Another Coloring Example s1s2 s3s4 s0 N = 3

23 Another Coloring Example s1s2 s3s4 s0 N = 3

24 Which value to pick? One with interference degree >= N One with interference degree >= N One with minimal spill cost (cost of placing value in memory rather than in register) One with minimal spill cost (cost of placing value in memory rather than in register) What is spill cost? What is spill cost? Cost of extra load and store instructions Cost of extra load and store instructions

25 One Way to Compute Spill Cost Goal: give priority to values used in loops Goal: give priority to values used in loops So assume loops execute 10 times So assume loops execute 10 times Spill cost = defCost + useCost Spill cost = defCost + useCost defCost = sum over all definitions of cost of a store times 10 nestingDepthOfLoop defCost = sum over all definitions of cost of a store times 10 nestingDepthOfLoop useCost = sum over all uses of cost of a load times 10 nestingDepthOfLoop useCost = sum over all uses of cost of a load times 10 nestingDepthOfLoop Choose the value with the lowest spill cost Choose the value with the lowest spill cost


Download ppt "Register Usage Keep as many values in registers as possible Keep as many values in registers as possible Register assignment Register assignment Register."

Similar presentations


Ads by Google