| 1 | package pass |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "math" |
| 6 | "sort" |
| 7 | |
| 8 | "github.com/mmcloughlin/avo/reg" |
| 9 | ) |
| 10 | |
| 11 | // edge is an edge of the interference graph, indicating that registers X and Y |
| 12 | // must be in non-conflicting registers. |
| 13 | type edge struct { |
| 14 | X, Y reg.ID |
| 15 | } |
| 16 | |
| 17 | // Allocator is a graph-coloring register allocator. |
| 18 | type Allocator struct { |
| 19 | registers []reg.ID |
| 20 | priority map[reg.ID]int |
| 21 | allocation reg.Allocation |
| 22 | edges []*edge |
| 23 | possible map[reg.ID][]reg.ID |
| 24 | } |
| 25 | |
| 26 | // NewAllocator builds an allocator for the given physical registers. |
| 27 | func NewAllocator(rs []reg.Physical) (*Allocator, error) { |
| 28 | // Set of IDs, excluding restricted registers. |
| 29 | idset := map[reg.ID]bool{} |
| 30 | for _, r := range rs { |
| 31 | if (r.Info() & reg.Restricted) != 0 { |
| 32 | continue |
| 33 | } |
| 34 | idset[r.ID()] = true |
| 35 | } |
| 36 | |
| 37 | if len(idset) == 0 { |
| 38 | return nil, errors.New("no allocatable registers") |
| 39 | } |
| 40 | |
| 41 | // Produce slice of unique register IDs. |
| 42 | var ids []reg.ID |
| 43 | for id := range idset { |
| 44 | ids = append(ids, id) |
| 45 | } |
| 46 | |
| 47 | a := &Allocator{ |
| 48 | registers: ids, |
| 49 | priority: map[reg.ID]int{}, |
| 50 | allocation: reg.NewEmptyAllocation(), |
| 51 | possible: map[reg.ID][]reg.ID{}, |
| 52 | } |
| 53 | a.sortregisters() |
| 54 | |
| 55 | return a, nil |
| 56 | } |
| 57 | |
| 58 | // NewAllocatorForKind builds an allocator for the given kind of registers. |
| 59 | func NewAllocatorForKind(k reg.Kind) (*Allocator, error) { |
| 60 | f := reg.FamilyOfKind(k) |
| 61 | if f == nil { |
| 62 | return nil, errors.New("unknown register family") |
| 63 | } |
| 64 | return NewAllocator(f.Registers()) |
| 65 | } |
| 66 | |
| 67 | // SetPriority sets the priority of the given regiser to p. Higher priority |
| 68 | // registers are preferred in allocations. By default all registers have 0 |
| 69 | // priority. Priority will only apply to subsequent Add() calls, therefore |
| 70 | // typically all SetPriority calls should happen at allocator initialization. |
| 71 | func (a *Allocator) SetPriority(id reg.ID, p int) { |
| 72 | a.priority[id] = p |
| 73 | a.sortregisters() |
| 74 | } |
| 75 | |
| 76 | // sortregisters sorts the list of available registers: higher priority first, |
| 77 | // falling back to sorting by ID. |
| 78 | func (a *Allocator) sortregisters() { |
| 79 | sort.Slice(a.registers, func(i, j int) bool { |
| 80 | ri, rj := a.registers[i], a.registers[j] |
| 81 | pi, pj := a.priority[ri], a.priority[rj] |
| 82 | return (pi > pj) || (pi == pj && ri < rj) |
| 83 | }) |
| 84 | } |
| 85 | |
| 86 | // AddInterferenceSet records that r interferes with every register in s. Convenience wrapper around AddInterference. |
| 87 | func (a *Allocator) AddInterferenceSet(r reg.Register, s reg.MaskSet) { |
| 88 | for id, mask := range s { |
| 89 | if (r.Mask() & mask) != 0 { |
| 90 | a.AddInterference(r.ID(), id) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // AddInterference records that x and y must be assigned to non-conflicting physical registers. |
| 96 | func (a *Allocator) AddInterference(x, y reg.ID) { |
| 97 | a.Add(x) |
| 98 | a.Add(y) |
| 99 | a.edges = append(a.edges, &edge{X: x, Y: y}) |
| 100 | } |
| 101 | |
| 102 | // Add adds a register to be allocated. Does nothing if the register has already been added. |
| 103 | func (a *Allocator) Add(v reg.ID) { |
| 104 | if !v.IsVirtual() { |
| 105 | return |
| 106 | } |
| 107 | if _, found := a.possible[v]; found { |
| 108 | return |
| 109 | } |
| 110 | a.possible[v] = a.possibleregisters(v) |
| 111 | } |
| 112 | |
| 113 | // Allocate allocates physical registers. |
| 114 | func (a *Allocator) Allocate() (reg.Allocation, error) { |
| 115 | for { |
| 116 | if err := a.update(); err != nil { |
| 117 | return nil, err |
| 118 | } |
| 119 | |
| 120 | if a.remaining() == 0 { |
| 121 | break |
| 122 | } |
| 123 | |
| 124 | v := a.mostrestricted() |
| 125 | if err := a.alloc(v); err != nil { |
| 126 | return nil, err |
| 127 | } |
| 128 | } |
| 129 | return a.allocation, nil |
| 130 | } |
| 131 | |
| 132 | // update possible allocations based on edges. |
| 133 | func (a *Allocator) update() error { |
| 134 | var rem []*edge |
| 135 | for _, e := range a.edges { |
| 136 | x := a.allocation.LookupDefault(e.X) |
| 137 | y := a.allocation.LookupDefault(e.Y) |
| 138 | switch { |
| 139 | case x.IsVirtual() && y.IsVirtual(): |
| 140 | rem = append(rem, e) |
| 141 | continue |
| 142 | case x.IsPhysical() && y.IsPhysical(): |
| 143 | if x == y { |
| 144 | return errors.New("impossible register allocation") |
| 145 | } |
| 146 | case x.IsPhysical() && y.IsVirtual(): |
| 147 | a.discardconflicting(y, x) |
| 148 | case x.IsVirtual() && y.IsPhysical(): |
| 149 | a.discardconflicting(x, y) |
| 150 | default: |
| 151 | panic("unreachable") |
| 152 | } |
| 153 | } |
| 154 | a.edges = rem |
| 155 | |
| 156 | return nil |
| 157 | } |
| 158 | |
| 159 | // mostrestricted returns the virtual register with the least possibilities. |
| 160 | func (a *Allocator) mostrestricted() reg.ID { |
| 161 | n := int(math.MaxInt32) |
| 162 | var v reg.ID |
| 163 | for w, p := range a.possible { |
| 164 | // On a tie, choose the smallest ID in numeric order. This avoids |
| 165 | // non-deterministic allocations due to map iteration order. |
| 166 | if len(p) < n || (len(p) == n && w < v) { |
| 167 | n = len(p) |
| 168 | v = w |
| 169 | } |
| 170 | } |
| 171 | return v |
| 172 | } |
| 173 | |
| 174 | // discardconflicting removes registers from vs possible list that conflict with p. |
| 175 | func (a *Allocator) discardconflicting(v, p reg.ID) { |
| 176 | a.possible[v] = filterregisters(a.possible[v], func(r reg.ID) bool { |
| 177 | return r != p |
| 178 | }) |
| 179 | } |
| 180 | |
| 181 | // alloc attempts to allocate a register to v. |
| 182 | func (a *Allocator) alloc(v reg.ID) error { |
| 183 | ps := a.possible[v] |
| 184 | if len(ps) == 0 { |
| 185 | return errors.New("failed to allocate registers") |
| 186 | } |
| 187 | p := ps[0] |
| 188 | a.allocation[v] = p |
| 189 | delete(a.possible, v) |
| 190 | return nil |
| 191 | } |
| 192 | |
| 193 | // remaining returns the number of unallocated registers. |
| 194 | func (a *Allocator) remaining() int { |
| 195 | return len(a.possible) |
| 196 | } |
| 197 | |
| 198 | // possibleregisters returns all allocate-able registers for the given virtual. |
| 199 | func (a *Allocator) possibleregisters(v reg.ID) []reg.ID { |
| 200 | return filterregisters(a.registers, func(r reg.ID) bool { |
| 201 | return v.Kind() == r.Kind() |
| 202 | }) |
| 203 | } |
| 204 | |
| 205 | func filterregisters(in []reg.ID, predicate func(reg.ID) bool) []reg.ID { |
| 206 | var rs []reg.ID |
| 207 | for _, r := range in { |
| 208 | if predicate(r) { |
| 209 | rs = append(rs, r) |
| 210 | } |
| 211 | } |
| 212 | return rs |
| 213 | } |