| 1 | package pass |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
| 6 | "github.com/mmcloughlin/avo/gotypes" |
| 7 | "github.com/mmcloughlin/avo/ir" |
| 8 | "github.com/mmcloughlin/avo/operand" |
| 9 | "github.com/mmcloughlin/avo/reg" |
| 10 | ) |
| 11 | |
| 12 | // ZeroExtend32BitOutputs applies the rule that "32-bit operands generate a |
| 13 | // 32-bit result, zero-extended to a 64-bit result in the destination |
| 14 | // general-purpose register" (Intel Software Developer’s Manual, Volume 1, |
| 15 | // 3.4.1.1). |
| 16 | func ZeroExtend32BitOutputs(i *ir.Instruction) error { |
| 17 | for j, op := range i.Outputs { |
| 18 | if !operand.IsR32(op) { |
| 19 | continue |
| 20 | } |
| 21 | r, ok := op.(reg.GP) |
| 22 | if !ok { |
| 23 | panic("r32 operand should satisfy reg.GP") |
| 24 | } |
| 25 | i.Outputs[j] = r.As64() |
| 26 | } |
| 27 | return nil |
| 28 | } |
| 29 | |
| 30 | // Liveness computes register liveness. |
| 31 | func Liveness(fn *ir.Function) error { |
| 32 | // Note this implementation is initially naive so as to be "obviously correct". |
| 33 | // There are a well-known optimizations we can apply if necessary. |
| 34 | |
| 35 | is := fn.Instructions() |
| 36 | |
| 37 | // Process instructions in reverse: poor approximation to topological sort. |
| 38 | // TODO(mbm): process instructions in topological sort order |
| 39 | for l, r := 0, len(is)-1; l < r; l, r = l+1, r-1 { |
| 40 | is[l], is[r] = is[r], is[l] |
| 41 | } |
| 42 | |
| 43 | // Initialize. |
| 44 | for _, i := range is { |
| 45 | i.LiveIn = reg.NewMaskSetFromRegisters(i.InputRegisters()) |
| 46 | i.LiveOut = reg.NewEmptyMaskSet() |
| 47 | } |
| 48 | |
| 49 | // Iterative dataflow analysis. |
| 50 | for { |
| 51 | changes := false |
| 52 | |
| 53 | for _, i := range is { |
| 54 | // out[n] = UNION[s IN succ[n]] in[s] |
| 55 | for _, s := range i.Succ { |
| 56 | if s == nil { |
| 57 | continue |
| 58 | } |
| 59 | changes = i.LiveOut.Update(s.LiveIn) || changes |
| 60 | } |
| 61 | |
| 62 | // in[n] = use[n] UNION (out[n] - def[n]) |
| 63 | def := reg.NewMaskSetFromRegisters(i.OutputRegisters()) |
| 64 | changes = i.LiveIn.Update(i.LiveOut.Difference(def)) || changes |
| 65 | } |
| 66 | |
| 67 | if !changes { |
| 68 | break |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | // AllocateRegisters performs register allocation. |
| 76 | func AllocateRegisters(fn *ir.Function) error { |
| 77 | // Initialize one allocator per kind. |
| 78 | as := map[reg.Kind]*Allocator{} |
| 79 | for _, i := range fn.Instructions() { |
| 80 | for _, r := range i.Registers() { |
| 81 | k := r.Kind() |
| 82 | if _, found := as[k]; !found { |
| 83 | a, err := NewAllocatorForKind(k) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | as[k] = a |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // De-prioritize the base pointer register. This can be used as a general |
| 93 | // purpose register, but it's callee-save so needs to be saved/restored if |
| 94 | // it is clobbered. For this reason we prefer to avoid using it unless |
| 95 | // forced to by register pressure. |
| 96 | for k, a := range as { |
| 97 | f := reg.FamilyOfKind(k) |
| 98 | for _, r := range f.Registers() { |
| 99 | if (r.Info() & reg.BasePointer) != 0 { |
| 100 | // Negative priority penalizes this register relative to all |
| 101 | // others (having default zero priority). |
| 102 | a.SetPriority(r.ID(), -1) |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Populate registers to be allocated. |
| 108 | for _, i := range fn.Instructions() { |
| 109 | for _, r := range i.Registers() { |
| 110 | as[r.Kind()].Add(r.ID()) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Record register interferences. |
| 115 | for _, i := range fn.Instructions() { |
| 116 | for _, d := range i.OutputRegisters() { |
| 117 | k := d.Kind() |
| 118 | out := i.LiveOut.OfKind(k) |
| 119 | out.DiscardRegister(d) |
| 120 | as[k].AddInterferenceSet(d, out) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Execute register allocation. |
| 125 | fn.Allocation = reg.NewEmptyAllocation() |
| 126 | for _, a := range as { |
| 127 | al, err := a.Allocate() |
| 128 | if err != nil { |
| 129 | return err |
| 130 | } |
| 131 | if err := fn.Allocation.Merge(al); err != nil { |
| 132 | return err |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return nil |
| 137 | } |
| 138 | |
| 139 | // BindRegisters applies the result of register allocation, replacing all virtual registers with their assigned physical registers. |
| 140 | func BindRegisters(fn *ir.Function) error { |
| 141 | for _, i := range fn.Instructions() { |
| 142 | for idx := range i.Operands { |
| 143 | i.Operands[idx] = operand.ApplyAllocation(i.Operands[idx], fn.Allocation) |
| 144 | } |
| 145 | for idx := range i.Inputs { |
| 146 | i.Inputs[idx] = operand.ApplyAllocation(i.Inputs[idx], fn.Allocation) |
| 147 | } |
| 148 | for idx := range i.Outputs { |
| 149 | i.Outputs[idx] = operand.ApplyAllocation(i.Outputs[idx], fn.Allocation) |
| 150 | } |
| 151 | } |
| 152 | return nil |
| 153 | } |
| 154 | |
| 155 | // VerifyAllocation performs sanity checks following register allocation. |
| 156 | func VerifyAllocation(fn *ir.Function) error { |
| 157 | // All registers should be physical. |
| 158 | for _, i := range fn.Instructions() { |
| 159 | for _, r := range i.Registers() { |
| 160 | if reg.ToPhysical(r) == nil { |
| 161 | return errors.New("non physical register found") |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return nil |
| 167 | } |
| 168 | |
| 169 | // EnsureBasePointerCalleeSaved ensures that the base pointer register will be |
| 170 | // saved and restored if it has been clobbered by the function. |
| 171 | func EnsureBasePointerCalleeSaved(fn *ir.Function) error { |
| 172 | // Check to see if the base pointer is written to. |
| 173 | clobbered := false |
| 174 | for _, i := range fn.Instructions() { |
| 175 | for _, r := range i.OutputRegisters() { |
| 176 | if p := reg.ToPhysical(r); p != nil && (p.Info()®.BasePointer) != 0 { |
| 177 | clobbered = true |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if !clobbered { |
| 183 | return nil |
| 184 | } |
| 185 | |
| 186 | // This function clobbers the base pointer register so we need to ensure it |
| 187 | // will be saved and restored. The Go assembler will do this automatically, |
| 188 | // with a few exceptions detailed below. In summary, we can usually ensure |
| 189 | // this happens by ensuring the function is not frameless (apart from |
| 190 | // NOFRAME functions). |
| 191 | // |
| 192 | // Reference: https://github.com/golang/go/blob/3f4977bd5800beca059defb5de4dc64cd758cbb9/src/cmd/internal/obj/x86/obj6.go#L591-L609 |
| 193 | // |
| 194 | // var bpsize int |
| 195 | // if ctxt.Arch.Family == sys.AMD64 && |
| 196 | // !p.From.Sym.NoFrame() && // (1) below |
| 197 | // !(autoffset == 0 && p.From.Sym.NoSplit()) && // (2) below |
| 198 | // !(autoffset == 0 && !hasCall) { // (3) below |
| 199 | // // Make room to save a base pointer. |
| 200 | // // There are 2 cases we must avoid: |
| 201 | // // 1) If noframe is set (which we do for functions which tail call). |
| 202 | // // 2) Scary runtime internals which would be all messed up by frame pointers. |
| 203 | // // We detect these using a heuristic: frameless nosplit functions. |
| 204 | // // TODO: Maybe someday we label them all with NOFRAME and get rid of this heuristic. |
| 205 | // // For performance, we also want to avoid: |
| 206 | // // 3) Frameless leaf functions |
| 207 | // bpsize = ctxt.Arch.PtrSize |
| 208 | // autoffset += int32(bpsize) |
| 209 | // p.To.Offset += int64(bpsize) |
| 210 | // } else { |
| 211 | // bpsize = 0 |
| 212 | // } |
| 213 | // |
| 214 | if fn.Attributes.NOFRAME() { |
| 215 | return errors.New("NOFRAME function clobbers base pointer register") |
| 216 | } |
| 217 | |
| 218 | if fn.LocalSize == 0 { |
| 219 | fn.AllocLocal(int(gotypes.PointerSize)) |
| 220 | } |
| 221 | |
| 222 | return nil |
| 223 | } |