Debian Codegraph @

pass/reg_test.go

raw symbols SCIP
1 package pass_test
2
3 import (
4 "testing"
5
6 "github.com/mmcloughlin/avo/attr"
7 "github.com/mmcloughlin/avo/build"
8 "github.com/mmcloughlin/avo/ir"
9 "github.com/mmcloughlin/avo/operand"
10 "github.com/mmcloughlin/avo/pass"
11 "github.com/mmcloughlin/avo/reg"
12 )
13
14 func TestZeroExtend32BitOutputs(t *testing.T) {
15 collection := reg.NewCollection()
16 v16 := collection.GP16()
17 v32 := collection.GP32()
18
19 i := &ir.Instruction{
20 Outputs: []operand.Op{
21 reg.R8B,
22 reg.R9W,
23 reg.R10L,
24 reg.R11,
25 v16,
26 v32,
27 },
28 }
29
30 err := pass.ZeroExtend32BitOutputs(i)
31 if err != nil {
32 t.Fatal(err)
33 }
34
35 got := i.Outputs
36 expect := []reg.Register{
37 reg.R8B,
38 reg.R9W,
39 reg.R10, // converted from R10L
40 reg.R11,
41 v16,
42 v32.As64(), // converted from 32-bit
43 }
44
45 if len(expect) != len(got) {
46 t.Fatal("length mismatch")
47 }
48
49 for j := range got {
50 r, ok := got[j].(reg.Register)
51 if !ok {
52 t.Fatalf("expected register; got %s", got[j].Asm())
53 }
54
55 if !reg.Equal(expect[j], r) {
56 t.Fatalf("got %s; expect %s", expect[j].Asm(), r.Asm())
57 }
58 }
59 }
60
61 func TestLivenessBasic(t *testing.T) {
62 // Build: a = 1, b = 2, a = a+b
63 ctx := build.NewContext()
64 ctx.Function("add")
65 a := ctx.GP64()
66 b := ctx.GP64()
67 ctx.MOVQ(operand.U64(1), a)
68 ctx.MOVQ(operand.U64(2), b)
69 ctx.ADDQ(a, b)
70
71 AssertLiveness(t, ctx,
72 [][]reg.Register{
73 {},
74 {a},
75 {a, b},
76 },
77 [][]reg.Register{
78 {a},
79 {a, b},
80 {},
81 },
82 )
83 }
84
85 func AssertLiveness(t *testing.T, ctx *build.Context, in, out [][]reg.Register) {
86 t.Helper()
87 fn := ConstructLiveness(t, ctx)
88 is := fn.Instructions()
89
90 if len(in) != len(is) || len(out) != len(is) {
91 t.Fatalf("%d instructions: %d/%d in/out expectations", len(is), len(in), len(out))
92 }
93
94 for idx, i := range is {
95 AssertRegistersMatchSet(t, in[idx], i.LiveIn)
96 AssertRegistersMatchSet(t, out[idx], i.LiveOut)
97 }
98 }
99
100 func AssertRegistersMatchSet(t *testing.T, rs []reg.Register, s reg.MaskSet) {
101 t.Helper()
102 if !s.Equals(reg.NewMaskSetFromRegisters(rs)) {
103 t.Fatalf("register slice does not match set: %#v and %#v", rs, s)
104 }
105 }
106
107 func ConstructLiveness(t *testing.T, ctx *build.Context) *ir.Function {
108 t.Helper()
109 return BuildFunction(t, ctx, pass.LabelTarget, pass.CFG, pass.Liveness)
110 }
111
112 func TestAllocateRegistersBasePointerDeprioritized(t *testing.T) {
113 // Construct a function that requires n general-purpose registers all live
114 // at once. Choose n to be the maximal possible number of registers without
115 // touching the base pointer.
116 n := 14
117
118 ctx := build.NewContext()
119 ctx.Function("sum")
120 ctx.SignatureExpr("func() uint64")
121
122 x := make([]reg.GPVirtual, n)
123 for i := 0; i < n; i++ {
124 x[i] = ctx.GP64()
125 ctx.MOVQ(operand.U64(i), x[i])
126 }
127
128 for i := 1; i < n; i++ {
129 ctx.ADDQ(x[i], x[0])
130 }
131
132 ctx.Store(x[0], ctx.ReturnIndex(0))
133 ctx.RET()
134
135 // Build and compile the function up to register allocation.
136 fn := BuildFunction(t, ctx, pass.LabelTarget, pass.CFG, pass.Liveness, pass.AllocateRegisters, pass.BindRegisters)
137
138 // Verify this function uses n registers, but not the base pointer.
139 ps := map[reg.Physical]bool{}
140 for _, i := range fn.Instructions() {
141 for _, r := range i.OutputRegisters() {
142 ps[reg.ToPhysical(r)] = true
143 }
144 }
145
146 if len(ps) != n {
147 t.Fatalf("expected function to require %d registers", n)
148 }
149
150 for p := range ps {
151 if (p.Info() & reg.BasePointer) != 0 {
152 t.Fatal("base pointer used")
153 }
154 }
155 }
156
157 func TestEnsureBasePointerCalleeSavedFrameless(t *testing.T) {
158 // Construct a function that writes to the base pointer.
159 ctx := build.NewContext()
160 ctx.Function("clobber")
161 ctx.ADDQ(reg.RAX, reg.RBP)
162
163 // Build the function with the EnsureBasePointerCalleeSaved pass.
164 fn := BuildFunction(t, ctx, pass.EnsureBasePointerCalleeSaved)
165
166 // Since the function was frameless, expect that the pass would have
167 expect := 8
168 if fn.LocalSize != expect {
169 t.Fatalf("expected frame size %d; got %d", expect, fn.LocalSize)
170 }
171 }
172
173 func TestEnsureBasePointerCalleeSavedWithFrame(t *testing.T) {
174 // Construct a function that writes to the base pointer, but already has a
175 // stack frame.
176 expect := 64
177
178 ctx := build.NewContext()
179 ctx.Function("clobber")
180 ctx.AllocLocal(expect)
181 ctx.ADDQ(reg.RAX, reg.RBP)
182
183 // Build the function with the EnsureBasePointerCalleeSaved pass.
184 fn := BuildFunction(t, ctx, pass.EnsureBasePointerCalleeSaved)
185
186 // Expect that since the function already has a stack frame, there's no need to increase its size.
187 if fn.LocalSize != expect {
188 t.Fatalf("expected frame size %d; got %d", expect, fn.LocalSize)
189 }
190 }
191
192 func TestEnsureBasePointerCalleeSavedNOFRAME(t *testing.T) {
193 // Construct a NOFRAME function that writes to base pointer.
194 ctx := build.NewContext()
195 ctx.Function("clobber")
196 ctx.Attributes(attr.NOFRAME)
197 ctx.ADDQ(reg.RAX, reg.RBP)
198
199 // Build the function.
200 fn := BuildFunction(t, ctx)
201
202 // Expect the pass to fail due to NOFRAME exception.
203 if err := pass.EnsureBasePointerCalleeSaved(fn); err == nil {
204 t.Fatal("expected error from NOFRAME function that clobbers base pointer")
205 }
206 }