summaryrefslogtreecommitdiff
path: root/deps/v8/test/unittests/interpreter/bytecode-register-allocator-unittest.cc
blob: d4dc111d69fb301a6b96d91cfef2194f870ec47f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/v8.h"

#include "src/interpreter/bytecode-array-builder.h"
#include "src/interpreter/bytecode-register-allocator.h"
#include "test/unittests/test-utils.h"

namespace v8 {
namespace internal {
namespace interpreter {

class TemporaryRegisterAllocatorTest : public TestWithIsolateAndZone {
 public:
  TemporaryRegisterAllocatorTest() : allocator_(zone(), 0) {}
  ~TemporaryRegisterAllocatorTest() override {}
  TemporaryRegisterAllocator* allocator() { return &allocator_; }

 private:
  TemporaryRegisterAllocator allocator_;
};

TEST_F(TemporaryRegisterAllocatorTest, FirstAllocation) {
  CHECK_EQ(allocator()->allocation_count(), 0);
  int reg0_index = allocator()->BorrowTemporaryRegister();
  CHECK_EQ(reg0_index, 0);
  CHECK_EQ(allocator()->allocation_count(), 1);
  CHECK(allocator()->RegisterIsLive(Register(reg0_index)));
  allocator()->ReturnTemporaryRegister(reg0_index);
  CHECK(!allocator()->RegisterIsLive(Register(reg0_index)));
  CHECK_EQ(allocator()->allocation_count(), 1);
  CHECK(allocator()->first_temporary_register() == Register(0));
  CHECK(allocator()->last_temporary_register() == Register(0));
}

TEST_F(TemporaryRegisterAllocatorTest, SimpleAllocations) {
  for (int i = 0; i < 13; i++) {
    int reg_index = allocator()->BorrowTemporaryRegister();
    CHECK_EQ(reg_index, i);
    CHECK_EQ(allocator()->allocation_count(), i + 1);
  }
  for (int i = 0; i < 13; i++) {
    CHECK(allocator()->RegisterIsLive(Register(i)));
    allocator()->ReturnTemporaryRegister(i);
    CHECK(!allocator()->RegisterIsLive(Register(i)));
    int reg_index = allocator()->BorrowTemporaryRegister();
    CHECK_EQ(reg_index, i);
    CHECK_EQ(allocator()->allocation_count(), 13);
  }
  for (int i = 0; i < 13; i++) {
    CHECK(allocator()->RegisterIsLive(Register(i)));
    allocator()->ReturnTemporaryRegister(i);
    CHECK(!allocator()->RegisterIsLive(Register(i)));
  }
}

TEST_F(TemporaryRegisterAllocatorTest, SimpleRangeAllocation) {
  static const int kRunLength = 7;
  int start = allocator()->PrepareForConsecutiveTemporaryRegisters(kRunLength);
  CHECK(!allocator()->RegisterIsLive(Register(start)));
  for (int i = 0; i < kRunLength; i++) {
    CHECK(!allocator()->RegisterIsLive(Register(start + i)));
    allocator()->BorrowConsecutiveTemporaryRegister(start + i);
    CHECK(allocator()->RegisterIsLive(Register(start + i)));
  }
}

TEST_F(TemporaryRegisterAllocatorTest, RangeAllocationAbuttingFree) {
  static const int kFreeCount = 3;
  static const int kRunLength = 6;

  for (int i = 0; i < kFreeCount; i++) {
    int to_free = allocator()->BorrowTemporaryRegister();
    CHECK_EQ(to_free, i);
  }
  for (int i = 0; i < kFreeCount; i++) {
    allocator()->ReturnTemporaryRegister(i);
  }

  int start = allocator()->PrepareForConsecutiveTemporaryRegisters(kRunLength);
  CHECK(!allocator()->RegisterIsLive(Register(start)));
  for (int i = 0; i < kRunLength; i++) {
    CHECK(!allocator()->RegisterIsLive(Register(start + i)));
    allocator()->BorrowConsecutiveTemporaryRegister(start + i);
    CHECK(allocator()->RegisterIsLive(Register(start + i)));
  }
}

TEST_F(TemporaryRegisterAllocatorTest, RangeAllocationAbuttingHole) {
  static const int kPreAllocatedCount = 7;
  static const int kPreAllocatedFreeCount = 6;
  static const int kRunLength = 8;

  for (int i = 0; i < kPreAllocatedCount; i++) {
    int to_free = allocator()->BorrowTemporaryRegister();
    CHECK_EQ(to_free, i);
  }
  for (int i = 0; i < kPreAllocatedFreeCount; i++) {
    allocator()->ReturnTemporaryRegister(i);
  }
  int start = allocator()->PrepareForConsecutiveTemporaryRegisters(kRunLength);
  CHECK(!allocator()->RegisterIsLive(Register(start)));
  CHECK_EQ(start, kPreAllocatedCount);
  for (int i = 0; i < kRunLength; i++) {
    CHECK(!allocator()->RegisterIsLive(Register(start + i)));
    allocator()->BorrowConsecutiveTemporaryRegister(start + i);
    CHECK(allocator()->RegisterIsLive(Register(start + i)));
  }
}

TEST_F(TemporaryRegisterAllocatorTest, RangeAllocationAvailableInTemporaries) {
  static const int kNotRunLength = 13;
  static const int kRunLength = 8;

  // Allocate big batch
  for (int i = 0; i < kNotRunLength * 2 + kRunLength; i++) {
    int allocated = allocator()->BorrowTemporaryRegister();
    CHECK_EQ(allocated, i);
  }
  // Free every other register either side of target.
  for (int i = 0; i < kNotRunLength; i++) {
    if ((i & 2) == 1) {
      allocator()->ReturnTemporaryRegister(i);
      allocator()->ReturnTemporaryRegister(kNotRunLength + kRunLength + i);
    }
  }
  // Free all registers for target.
  for (int i = kNotRunLength; i < kNotRunLength + kRunLength; i++) {
    allocator()->ReturnTemporaryRegister(i);
  }

  int start = allocator()->PrepareForConsecutiveTemporaryRegisters(kRunLength);
  CHECK_EQ(start, kNotRunLength);
  for (int i = 0; i < kRunLength; i++) {
    CHECK(!allocator()->RegisterIsLive(Register(start + i)));
    allocator()->BorrowConsecutiveTemporaryRegister(start + i);
    CHECK(allocator()->RegisterIsLive(Register(start + i)));
  }
}

TEST_F(TemporaryRegisterAllocatorTest, NotInRange) {
  for (int i = 0; i < 10; i++) {
    int reg = allocator()->BorrowTemporaryRegisterNotInRange(2, 5);
    CHECK(reg == i || (reg > 2 && reg == i + 4));
  }
  for (int i = 0; i < 10; i++) {
    if (i < 2) {
      allocator()->ReturnTemporaryRegister(i);
    } else {
      allocator()->ReturnTemporaryRegister(i + 4);
    }
  }
  int reg0 = allocator()->BorrowTemporaryRegisterNotInRange(0, 3);
  CHECK_EQ(reg0, 4);
  int reg1 = allocator()->BorrowTemporaryRegisterNotInRange(3, 10);
  CHECK_EQ(reg1, 2);
  int reg2 = allocator()->BorrowTemporaryRegisterNotInRange(2, 6);
  CHECK_EQ(reg2, 1);
  allocator()->ReturnTemporaryRegister(reg0);
  allocator()->ReturnTemporaryRegister(reg1);
  allocator()->ReturnTemporaryRegister(reg2);
}

class BytecodeRegisterAllocatorTest : public TestWithIsolateAndZone {
 public:
  BytecodeRegisterAllocatorTest() {}
  ~BytecodeRegisterAllocatorTest() override {}
};

TEST_F(BytecodeRegisterAllocatorTest, TemporariesRecycled) {
  BytecodeArrayBuilder builder(isolate(), zone(), 0, 0, 0);

  int first;
  {
    BytecodeRegisterAllocator allocator(zone(),
                                        builder.temporary_register_allocator());
    first = allocator.NewRegister().index();
    allocator.NewRegister();
    allocator.NewRegister();
    allocator.NewRegister();
  }

  int second;
  {
    BytecodeRegisterAllocator allocator(zone(),
                                        builder.temporary_register_allocator());
    second = allocator.NewRegister().index();
  }

  CHECK_EQ(first, second);
}

TEST_F(BytecodeRegisterAllocatorTest, ConsecutiveRegisters) {
  BytecodeArrayBuilder builder(isolate(), zone(), 0, 0, 0);
  BytecodeRegisterAllocator allocator(zone(),
                                      builder.temporary_register_allocator());
  allocator.PrepareForConsecutiveAllocations(4);
  Register reg0 = allocator.NextConsecutiveRegister();
  Register other = allocator.NewRegister();
  Register reg1 = allocator.NextConsecutiveRegister();
  Register reg2 = allocator.NextConsecutiveRegister();
  Register reg3 = allocator.NextConsecutiveRegister();
  USE(other);

  CHECK(Register::AreContiguous(reg0, reg1, reg2, reg3));
}

}  // namespace interpreter
}  // namespace internal
}  // namespace v8