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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "optimizing_unit_test.h"
#include "ssa_liveness_analysis.h"
#include "utils/arena_allocator.h"
#include "gtest/gtest.h"
namespace art {
TEST(LiveInterval, GetStart) {
ArenaPool pool;
ArenaAllocator allocator(&pool);
{
static constexpr size_t ranges[][2] = {{0, 42}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
ASSERT_EQ(0u, interval->GetStart());
}
{
static constexpr size_t ranges[][2] = {{4, 12}, {14, 16}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
ASSERT_EQ(4u, interval->GetStart());
}
}
TEST(LiveInterval, IsDeadAt) {
ArenaPool pool;
ArenaAllocator allocator(&pool);
{
static constexpr size_t ranges[][2] = {{0, 42}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
ASSERT_TRUE(interval->IsDeadAt(42));
ASSERT_TRUE(interval->IsDeadAt(43));
ASSERT_FALSE(interval->IsDeadAt(41));
ASSERT_FALSE(interval->IsDeadAt(0));
ASSERT_FALSE(interval->IsDeadAt(22));
}
{
static constexpr size_t ranges[][2] = {{4, 12}, {14, 16}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
ASSERT_TRUE(interval->IsDeadAt(16));
ASSERT_TRUE(interval->IsDeadAt(32));
ASSERT_FALSE(interval->IsDeadAt(0));
ASSERT_FALSE(interval->IsDeadAt(4));
ASSERT_FALSE(interval->IsDeadAt(12));
ASSERT_FALSE(interval->IsDeadAt(13));
ASSERT_FALSE(interval->IsDeadAt(14));
ASSERT_FALSE(interval->IsDeadAt(15));
}
}
TEST(LiveInterval, Covers) {
ArenaPool pool;
ArenaAllocator allocator(&pool);
{
static constexpr size_t ranges[][2] = {{0, 42}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
ASSERT_TRUE(interval->Covers(0));
ASSERT_TRUE(interval->Covers(4));
ASSERT_TRUE(interval->Covers(41));
ASSERT_FALSE(interval->Covers(42));
ASSERT_FALSE(interval->Covers(54));
}
{
static constexpr size_t ranges[][2] = {{4, 12}, {14, 16}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
ASSERT_TRUE(interval->Covers(4));
ASSERT_TRUE(interval->Covers(11));
ASSERT_TRUE(interval->Covers(14));
ASSERT_TRUE(interval->Covers(15));
ASSERT_FALSE(interval->Covers(0));
ASSERT_FALSE(interval->Covers(12));
ASSERT_FALSE(interval->Covers(13));
ASSERT_FALSE(interval->Covers(16));
}
}
TEST(LiveInterval, FirstIntersectionWith) {
ArenaPool pool;
ArenaAllocator allocator(&pool);
{
static constexpr size_t ranges1[][2] = {{0, 4}, {8, 10}};
LiveInterval* interval1 = BuildInterval(ranges1, arraysize(ranges1), &allocator);
static constexpr size_t ranges2[][2] = {{5, 6}};
LiveInterval* interval2 = BuildInterval(ranges2, arraysize(ranges2), &allocator);
ASSERT_EQ(kNoLifetime, interval1->FirstIntersectionWith(interval2));
}
{
static constexpr size_t ranges1[][2] = {{0, 4}, {8, 10}};
LiveInterval* interval1 = BuildInterval(ranges1, arraysize(ranges1), &allocator);
static constexpr size_t ranges2[][2] = {{5, 42}};
LiveInterval* interval2 = BuildInterval(ranges2, arraysize(ranges2), &allocator);
ASSERT_EQ(8u, interval1->FirstIntersectionWith(interval2));
}
{
static constexpr size_t ranges1[][2] = {{0, 4}, {8, 10}};
LiveInterval* interval1 = BuildInterval(ranges1, arraysize(ranges1), &allocator);
static constexpr size_t ranges2[][2] = {{5, 6}, {7, 8}, {11, 12}};
LiveInterval* interval2 = BuildInterval(ranges2, arraysize(ranges2), &allocator);
ASSERT_EQ(kNoLifetime, interval1->FirstIntersectionWith(interval2));
}
{
static constexpr size_t ranges1[][2] = {{0, 4}, {8, 10}};
LiveInterval* interval1 = BuildInterval(ranges1, arraysize(ranges1), &allocator);
static constexpr size_t ranges2[][2] = {{5, 6}, {7, 8}, {9, 10}};
LiveInterval* interval2 = BuildInterval(ranges2, arraysize(ranges2), &allocator);
ASSERT_EQ(9u, interval1->FirstIntersectionWith(interval2));
}
{
static constexpr size_t ranges1[][2] = {{0, 1}, {2, 7}, {8, 10}};
LiveInterval* interval1 = BuildInterval(ranges1, arraysize(ranges1), &allocator);
static constexpr size_t ranges2[][2] = {{1, 2}, {6, 7}, {9, 10}};
LiveInterval* interval2 = BuildInterval(ranges2, arraysize(ranges2), &allocator);
ASSERT_EQ(6u, interval1->FirstIntersectionWith(interval2));
}
{
static constexpr size_t ranges1[][2] = {{0, 1}, {2, 8}, {55, 58}};
LiveInterval* interval1 = BuildInterval(ranges1, arraysize(ranges1), &allocator);
static constexpr size_t ranges2[][2] = {{1, 2}, {11, 42}, {43, 48}, {54, 56}};
LiveInterval* interval2 = BuildInterval(ranges2, arraysize(ranges2), &allocator);
ASSERT_EQ(55u, interval1->FirstIntersectionWith(interval2));
}
{
static constexpr size_t ranges1[][2] = {{0, 1}, {2, 8}, {15, 18}, {27, 32}, {41, 53}, {54, 60}};
LiveInterval* interval1 = BuildInterval(ranges1, arraysize(ranges1), &allocator);
static constexpr size_t ranges2[][2] = {{1, 2}, {11, 12}, {19, 25}, {34, 42}, {52, 60}};
LiveInterval* interval2 = BuildInterval(ranges2, arraysize(ranges2), &allocator);
ASSERT_EQ(41u, interval1->FirstIntersectionWith(interval2));
}
}
static bool RangesEquals(LiveInterval* interval,
const size_t expected[][2],
size_t number_of_expected_ranges) {
LiveRange* current = interval->GetFirstRange();
size_t i = 0;
for (;
i < number_of_expected_ranges && current != nullptr;
++i, current = current->GetNext()) {
if (expected[i][0] != current->GetStart()) {
return false;
}
if (expected[i][1] != current->GetEnd()) {
return false;
}
}
if (current != nullptr || i != number_of_expected_ranges) {
return false;
}
return true;
}
TEST(LiveInterval, SplitAt) {
ArenaPool pool;
ArenaAllocator allocator(&pool);
{
// Test within one range.
static constexpr size_t ranges[][2] = {{0, 4}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
LiveInterval* split = interval->SplitAt(1);
static constexpr size_t expected[][2] = {{0, 1}};
ASSERT_TRUE(RangesEquals(interval, expected, arraysize(expected)));
static constexpr size_t expected_split[][2] = {{1, 4}};
ASSERT_TRUE(RangesEquals(split, expected_split, arraysize(expected_split)));
}
{
// Test just before the end of one range.
static constexpr size_t ranges[][2] = {{0, 4}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
LiveInterval* split = interval->SplitAt(3);
static constexpr size_t expected[][2] = {{0, 3}};
ASSERT_TRUE(RangesEquals(interval, expected, arraysize(expected)));
static constexpr size_t expected_split[][2] = {{3, 4}};
ASSERT_TRUE(RangesEquals(split, expected_split, arraysize(expected_split)));
}
{
// Test withing the first range.
static constexpr size_t ranges[][2] = {{0, 4}, {8, 12}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
LiveInterval* split = interval->SplitAt(1);
static constexpr size_t expected[][2] = {{0, 1}};
ASSERT_TRUE(RangesEquals(interval, expected, arraysize(expected)));
static constexpr size_t expected_split[][2] = {{1, 4}, {8, 12}};
ASSERT_TRUE(RangesEquals(split, expected_split, arraysize(expected_split)));
}
{
// Test in a hole.
static constexpr size_t ranges[][2] = {{0, 4}, {8, 12}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
LiveInterval* split = interval->SplitAt(5);
static constexpr size_t expected[][2] = {{0, 4}};
ASSERT_TRUE(RangesEquals(interval, expected, arraysize(expected)));
static constexpr size_t expected_split[][2] = {{8, 12}};
ASSERT_TRUE(RangesEquals(split, expected_split, arraysize(expected_split)));
}
{
// Test withing the second range.
static constexpr size_t ranges[][2] = {{0, 4}, {8, 12}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
LiveInterval* split = interval->SplitAt(9);
static constexpr size_t expected[][2] = {{0, 4}, {8, 9}};
ASSERT_TRUE(RangesEquals(interval, expected, arraysize(expected)));
static constexpr size_t expected_split[][2] = {{9, 12}};
ASSERT_TRUE(RangesEquals(split, expected_split, arraysize(expected_split)));
}
{
// Test at the beginning of the second range.
static constexpr size_t ranges[][2] = {{0, 4}, {6, 10}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
LiveInterval* split = interval->SplitAt(6);
static constexpr size_t expected[][2] = {{0, 4}};
ASSERT_TRUE(RangesEquals(interval, expected, arraysize(expected)));
static constexpr size_t expected_split[][2] = {{6, 10}};
ASSERT_TRUE(RangesEquals(split, expected_split, arraysize(expected_split)));
}
{
// Test at the end of the first range.
static constexpr size_t ranges[][2] = {{0, 4}, {6, 10}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
LiveInterval* split = interval->SplitAt(4);
static constexpr size_t expected[][2] = {{0, 4}};
ASSERT_TRUE(RangesEquals(interval, expected, arraysize(expected)));
static constexpr size_t expected_split[][2] = {{6, 10}};
ASSERT_TRUE(RangesEquals(split, expected_split, arraysize(expected_split)));
}
{
// Test that we get null if we split at a position where the interval is dead.
static constexpr size_t ranges[][2] = {{0, 4}};
LiveInterval* interval = BuildInterval(ranges, arraysize(ranges), &allocator);
LiveInterval* split = interval->SplitAt(5);
ASSERT_TRUE(split == nullptr);
ASSERT_TRUE(RangesEquals(interval, ranges, arraysize(ranges)));
}
}
} // namespace art
|