250 likes | 257 Views
Register Usage. Keep as many values in registers as possible Register assignment Register allocation Popular techniques Local vs. global Graph coloring Bin packing. Local Register Assignment. Given Control-flow graph of basic blocks List of 3-addr statements per BB
E N D
Register Usage • Keep as many values in registers as possible • Register assignment • Register allocation • Popular techniques • Local vs. global • Graph coloring • Bin packing
Local Register Assignment • Given • Control-flow graph of basic blocks • List of 3-addr statements per BB • Set of “live” scalar values per stmt • Sets of scalar values used, defined per stmt • Design a local register assignment/allocation algorithm
Graph Coloring • Assign a color to each node in graph • Two nodes connected by an edge must have different colors • Classic problem in graph theory • NP complete • But good heuristics exist for register allocation
Live Ranges def y def x def y def x use y use x use y use x def x use x
Graph Coloring Register Assign • Each value is allocated a (symbolic) register • “Variables” interfere iff live ranges overlap • Two interfering values cannot share register • How can we tell if two values interfere? s1 s2 s3 s4
Interference Graph • Values and interference • Nodes are the values • Edge between two nodes iff they interfere s1 s2 s3 s4
Graph Coloring Example • 3 Colors
Heuristics for Register Coloring • Coloring a graph with N colors • For each node, m • If degree(m) < N • Node can always be colored, because • After coloring adjacent nodes, at least one color left for current node • If degree(m) >= N • Still may be colorable with N colors
Heuristics for Register Coloring • Remove nodes that have degree < N • Push the removed nodes onto a stack • When all the nodes have degree >= N • Find a node to spill (no color for that node) • Remove that node • When graph empty, start to color • Pop a node from stack back • Color node different from adjacent (colored) nodes
Another Coloring Example N = 3 s1 s2 s0 s3 s4
Another Coloring Example N = 3 s1 s2 s0 s4 s3 s4
Another Coloring Example N = 3 s1 s2 s0 s4 s3 s4
Another Coloring Example N = 3 s1 s2 s0 s3 s4 s3 s4
Another Coloring Example N = 3 s1 s2 s2 s0 s3 s4 s3 s4
Another Coloring Example N = 3 s1 s2 s2 s0 s3 s4 s3 s4
Another Coloring Example N = 3 s1 s2 s2 s0 s3 s4 s3 s4
Another Coloring Example N = 3 s1 s2 s0 s3 s4 s3 s4
Another Coloring Example N = 3 s1 s2 s0 s3 s4 s3 s4
Another Coloring Example N = 3 s1 s2 s0 s4 s3 s4
Another Coloring Example N = 3 s1 s2 s0 s4 s3 s4
Another Coloring Example N = 3 s1 s2 s0 s3 s4
Another Coloring Example N = 3 s1 s2 s0 s3 s4
Which value to pick? • One with interference degree >= N • One with minimal spill cost (cost of placing value in memory rather than in register) • What is spill cost? • Cost of extra load and store instructions
One Way to Compute Spill Cost • Goal: give priority to values used in loops • So assume loops execute 10 times • Spill cost = defCost + useCost • defCost = sum over all definitions of cost of a store times 10nestingDepthOfLoop • useCost = sum over all uses of cost of a load times 10nestingDepthOfLoop • Choose the value with the lowest spill cost