blob: c36b1436d3fdaa206b4f4f2bfd55f5ad26d2f4fb [file] [log] [blame]
Nicolas Geoffray622d9c32014-05-12 16:11:02 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "builder.h"
18#include "dex_file.h"
19#include "dex_instruction.h"
20#include "nodes.h"
21#include "optimizing_unit_test.h"
22#include "ssa_liveness_analysis.h"
23#include "utils/arena_allocator.h"
24#include "pretty_printer.h"
25
26#include "gtest/gtest.h"
27
28namespace art {
29
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +010030static HGraph* TestCode(const uint16_t* data, ArenaAllocator* allocator) {
31 HGraphBuilder builder(allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010032 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
33 HGraph* graph = builder.BuildGraph(*item);
34 graph->BuildDominatorTree();
35 graph->FindNaturalLoops();
36 return graph;
37}
38
39TEST(FindLoopsTest, CFG1) {
40 // Constant is not used.
41 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
42 Instruction::CONST_4 | 0 | 0,
43 Instruction::RETURN_VOID);
44
45 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +010046 ArenaAllocator allocator(&arena);
47 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010048 for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) {
49 ASSERT_EQ(graph->GetBlocks().Get(i)->GetLoopInformation(), nullptr);
50 }
51}
52
53TEST(FindLoopsTest, CFG2) {
54 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
55 Instruction::CONST_4 | 0 | 0,
56 Instruction::RETURN);
57
58 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +010059 ArenaAllocator allocator(&arena);
60 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010061 for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) {
62 ASSERT_EQ(graph->GetBlocks().Get(i)->GetLoopInformation(), nullptr);
63 }
64}
65
66TEST(FindLoopsTest, CFG3) {
67 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
68 Instruction::CONST_4 | 3 << 12 | 0,
69 Instruction::CONST_4 | 4 << 12 | 1 << 8,
70 Instruction::ADD_INT_2ADDR | 1 << 12,
71 Instruction::GOTO | 0x100,
72 Instruction::RETURN);
73
74 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +010075 ArenaAllocator allocator(&arena);
76 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010077 for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) {
78 ASSERT_EQ(graph->GetBlocks().Get(i)->GetLoopInformation(), nullptr);
79 }
80}
81
82TEST(FindLoopsTest, CFG4) {
83 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
84 Instruction::CONST_4 | 0 | 0,
85 Instruction::IF_EQ, 4,
86 Instruction::CONST_4 | 4 << 12 | 0,
87 Instruction::GOTO | 0x200,
88 Instruction::CONST_4 | 5 << 12 | 0,
89 Instruction::RETURN | 0 << 8);
90
91 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +010092 ArenaAllocator allocator(&arena);
93 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010094 for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) {
95 ASSERT_EQ(graph->GetBlocks().Get(i)->GetLoopInformation(), nullptr);
96 }
97}
98
99TEST(FindLoopsTest, CFG5) {
100 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
101 Instruction::CONST_4 | 0 | 0,
102 Instruction::IF_EQ, 3,
103 Instruction::CONST_4 | 4 << 12 | 0,
104 Instruction::RETURN | 0 << 8);
105
106 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100107 ArenaAllocator allocator(&arena);
108 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100109 for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) {
110 ASSERT_EQ(graph->GetBlocks().Get(i)->GetLoopInformation(), nullptr);
111 }
112}
113
114static void TestBlock(HGraph* graph,
115 int block_id,
116 bool is_loop_header,
117 int parent_loop_header_id,
118 const int* blocks_in_loop = nullptr,
119 size_t number_of_blocks = 0) {
120 HBasicBlock* block = graph->GetBlocks().Get(block_id);
121 ASSERT_EQ(block->IsLoopHeader(), is_loop_header);
122 if (parent_loop_header_id == -1) {
123 ASSERT_EQ(block->GetLoopInformation(), nullptr);
124 } else {
125 ASSERT_EQ(block->GetLoopInformation()->GetHeader()->GetBlockId(), parent_loop_header_id);
126 }
127
128 if (blocks_in_loop != nullptr) {
129 HLoopInformation* info = block->GetLoopInformation();
130 const BitVector& blocks = info->GetBlocks();
131 ASSERT_EQ(blocks.NumSetBits(), number_of_blocks);
132 for (size_t i = 0; i < number_of_blocks; ++i) {
133 ASSERT_TRUE(blocks.IsBitSet(blocks_in_loop[i]));
134 }
135 } else {
136 ASSERT_FALSE(block->IsLoopHeader());
137 }
138}
139
140TEST(FindLoopsTest, Loop1) {
141 // Simple loop with one preheader and one back edge.
142 // var a = 0;
143 // while (a == a) {
144 // }
145 // return;
146 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
147 Instruction::CONST_4 | 0 | 0,
148 Instruction::IF_EQ, 3,
149 Instruction::GOTO | 0xFE00,
150 Instruction::RETURN_VOID);
151
152 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100153 ArenaAllocator allocator(&arena);
154 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100155
156 TestBlock(graph, 0, false, -1); // entry block
157 TestBlock(graph, 1, false, -1); // pre header
158 const int blocks2[] = {2, 3};
159 TestBlock(graph, 2, true, 2, blocks2, 2); // loop header
160 TestBlock(graph, 3, false, 2); // block in loop
161 TestBlock(graph, 4, false, -1); // return block
162 TestBlock(graph, 5, false, -1); // exit block
163}
164
165TEST(FindLoopsTest, Loop2) {
166 // Make sure we support a preheader of a loop not being the first predecessor
167 // in the predecessor list of the header.
168 // var a = 0;
169 // while (a == a) {
170 // }
171 // return a;
172 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
173 Instruction::CONST_4 | 0 | 0,
174 Instruction::GOTO | 0x400,
175 Instruction::IF_EQ, 4,
176 Instruction::GOTO | 0xFE00,
177 Instruction::GOTO | 0xFD00,
178 Instruction::RETURN | 0 << 8);
179
180 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100181 ArenaAllocator allocator(&arena);
182 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100183
184 TestBlock(graph, 0, false, -1); // entry block
185 TestBlock(graph, 1, false, -1); // goto block
186 const int blocks2[] = {2, 3};
187 TestBlock(graph, 2, true, 2, blocks2, 2); // loop header
188 TestBlock(graph, 3, false, 2); // block in loop
189 TestBlock(graph, 4, false, -1); // pre header
190 TestBlock(graph, 5, false, -1); // return block
191 TestBlock(graph, 6, false, -1); // exit block
192}
193
194TEST(FindLoopsTest, Loop3) {
195 // Make sure we create a preheader of a loop when a header originally has two
196 // incoming blocks and one back edge.
197 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
198 Instruction::CONST_4 | 0 | 0,
199 Instruction::IF_EQ, 3,
200 Instruction::GOTO | 0x100,
201 Instruction::IF_EQ, 3,
202 Instruction::GOTO | 0xFE00,
203 Instruction::RETURN | 0 << 8);
204
205 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100206 ArenaAllocator allocator(&arena);
207 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100208
209 TestBlock(graph, 0, false, -1); // entry block
210 TestBlock(graph, 1, false, -1); // goto block
211 TestBlock(graph, 2, false, -1);
212 const int blocks2[] = {3, 4};
213 TestBlock(graph, 3, true, 3, blocks2, 2); // loop header
214 TestBlock(graph, 4, false, 3); // block in loop
215 TestBlock(graph, 5, false, -1); // pre header
216 TestBlock(graph, 6, false, -1); // return block
217 TestBlock(graph, 7, false, -1); // exit block
218 TestBlock(graph, 8, false, -1); // synthesized pre header
219}
220
221TEST(FindLoopsTest, Loop4) {
222 // Test loop with originally two back edges.
223 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
224 Instruction::CONST_4 | 0 | 0,
225 Instruction::IF_EQ, 6,
226 Instruction::IF_EQ, 3,
227 Instruction::GOTO | 0xFC00,
228 Instruction::GOTO | 0xFB00,
229 Instruction::RETURN | 0 << 8);
230
231 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100232 ArenaAllocator allocator(&arena);
233 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100234
235 TestBlock(graph, 0, false, -1); // entry block
236 TestBlock(graph, 1, false, -1); // pre header
237 const int blocks2[] = {2, 3, 4, 5, 8};
238 TestBlock(graph, 2, true, 2, blocks2, 5); // loop header
239 TestBlock(graph, 3, false, 2); // block in loop
240 TestBlock(graph, 4, false, 2); // original back edge
241 TestBlock(graph, 5, false, 2); // original back edge
242 TestBlock(graph, 6, false, -1); // return block
243 TestBlock(graph, 7, false, -1); // exit block
244 TestBlock(graph, 8, false, 2); // synthesized back edge
245}
246
247
248TEST(FindLoopsTest, Loop5) {
249 // Test loop with two exit edges.
250 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
251 Instruction::CONST_4 | 0 | 0,
252 Instruction::IF_EQ, 6,
253 Instruction::IF_EQ, 3,
254 Instruction::GOTO | 0x0200,
255 Instruction::GOTO | 0xFB00,
256 Instruction::RETURN | 0 << 8);
257
258 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100259 ArenaAllocator allocator(&arena);
260 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100261
262 TestBlock(graph, 0, false, -1); // entry block
263 TestBlock(graph, 1, false, -1); // pre header
264 const int blocks2[] = {2, 3, 5};
265 TestBlock(graph, 2, true, 2, blocks2, 3); // loop header
266 TestBlock(graph, 3, false, 2); // block in loop
267 TestBlock(graph, 4, false, -1); // loop exit
268 TestBlock(graph, 5, false, 2); // back edge
269 TestBlock(graph, 6, false, -1); // return block
270 TestBlock(graph, 7, false, -1); // exit block
271 TestBlock(graph, 8, false, -1); // synthesized block at the loop exit
272}
273
274TEST(FindLoopsTest, InnerLoop) {
275 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
276 Instruction::CONST_4 | 0 | 0,
277 Instruction::IF_EQ, 6,
278 Instruction::IF_EQ, 3,
279 Instruction::GOTO | 0xFE00, // inner loop
280 Instruction::GOTO | 0xFB00,
281 Instruction::RETURN | 0 << 8);
282
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100283 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100284 ArenaAllocator allocator(&arena);
285 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100286
287 TestBlock(graph, 0, false, -1); // entry block
288 TestBlock(graph, 1, false, -1); // pre header of outer loop
289 const int blocks2[] = {2, 3, 4, 5, 8};
290 TestBlock(graph, 2, true, 2, blocks2, 5); // outer loop header
291 const int blocks3[] = {3, 4};
292 TestBlock(graph, 3, true, 3, blocks3, 2); // inner loop header
293 TestBlock(graph, 4, false, 3); // back edge on inner loop
294 TestBlock(graph, 5, false, 2); // back edge on outer loop
295 TestBlock(graph, 6, false, -1); // return block
296 TestBlock(graph, 7, false, -1); // exit block
297 TestBlock(graph, 8, false, 2); // synthesized block as pre header of inner loop
298
299 ASSERT_TRUE(graph->GetBlocks().Get(3)->GetLoopInformation()->IsIn(
300 *graph->GetBlocks().Get(2)->GetLoopInformation()));
301 ASSERT_FALSE(graph->GetBlocks().Get(2)->GetLoopInformation()->IsIn(
302 *graph->GetBlocks().Get(3)->GetLoopInformation()));
303}
304
305TEST(FindLoopsTest, TwoLoops) {
306 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
307 Instruction::CONST_4 | 0 | 0,
308 Instruction::IF_EQ, 3,
309 Instruction::GOTO | 0xFE00, // first loop
310 Instruction::IF_EQ, 3,
311 Instruction::GOTO | 0xFE00, // second loop
312 Instruction::RETURN | 0 << 8);
313
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100314 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100315 ArenaAllocator allocator(&arena);
316 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100317
318 TestBlock(graph, 0, false, -1); // entry block
319 TestBlock(graph, 1, false, -1); // pre header of first loop
320 const int blocks2[] = {2, 3};
321 TestBlock(graph, 2, true, 2, blocks2, 2); // first loop header
322 TestBlock(graph, 3, false, 2); // back edge of first loop
323 const int blocks4[] = {4, 5};
324 TestBlock(graph, 4, true, 4, blocks4, 2); // second loop header
325 TestBlock(graph, 5, false, 4); // back edge of second loop
326 TestBlock(graph, 6, false, -1); // return block
327 TestBlock(graph, 7, false, -1); // exit block
328
329 ASSERT_FALSE(graph->GetBlocks().Get(4)->GetLoopInformation()->IsIn(
330 *graph->GetBlocks().Get(2)->GetLoopInformation()));
331 ASSERT_FALSE(graph->GetBlocks().Get(2)->GetLoopInformation()->IsIn(
332 *graph->GetBlocks().Get(4)->GetLoopInformation()));
333}
334
335TEST(FindLoopsTest, NonNaturalLoop) {
336 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
337 Instruction::CONST_4 | 0 | 0,
338 Instruction::IF_EQ, 3,
339 Instruction::GOTO | 0x0100,
340 Instruction::IF_EQ, 3,
341 Instruction::GOTO | 0xFD00,
342 Instruction::RETURN | 0 << 8);
343
344 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100345 ArenaAllocator allocator(&arena);
346 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100347 ASSERT_TRUE(graph->GetBlocks().Get(3)->IsLoopHeader());
348 HLoopInformation* info = graph->GetBlocks().Get(3)->GetLoopInformation();
349 ASSERT_FALSE(info->GetHeader()->Dominates(info->GetBackEdges().Get(0)));
350}
351
352TEST(FindLoopsTest, DoWhileLoop) {
353 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
354 Instruction::CONST_4 | 0 | 0,
355 Instruction::GOTO | 0x0100,
356 Instruction::IF_EQ, 0xFFFF,
357 Instruction::RETURN | 0 << 8);
358
359 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100360 ArenaAllocator allocator(&arena);
361 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100362
363 TestBlock(graph, 0, false, -1); // entry block
364 TestBlock(graph, 1, false, -1); // pre header of first loop
365 const int blocks2[] = {2, 3, 6};
366 TestBlock(graph, 2, true, 2, blocks2, 3); // loop header
367 TestBlock(graph, 3, false, 2); // back edge of first loop
368 TestBlock(graph, 4, false, -1); // return block
369 TestBlock(graph, 5, false, -1); // exit block
370 TestBlock(graph, 6, false, 2); // synthesized block to avoid a critical edge
371}
372
373} // namespace art