blob: 2bfecc696a96f86ee466aa193b6885e097065a9b [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
Mathieu Chartierb666f482015-02-18 14:33:14 -080017#include "base/arena_allocator.h"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010018#include "builder.h"
19#include "dex_file.h"
20#include "dex_instruction.h"
21#include "nodes.h"
22#include "optimizing_unit_test.h"
23#include "ssa_liveness_analysis.h"
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010024#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) {
David Brazdil5e8b1372015-01-23 14:39:08 +000031 HGraph* graph = new (allocator) HGraph(allocator);
32 HGraphBuilder builder(graph);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010033 const DexFile::CodeItem* item = reinterpret_cast<const DexFile::CodeItem*>(data);
David Brazdil5e8b1372015-01-23 14:39:08 +000034 builder.BuildGraph(*item);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010035 graph->BuildDominatorTree();
Nicolas Geoffrayf5370122014-12-02 11:51:19 +000036 graph->AnalyzeNaturalLoops();
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010037 return graph;
38}
39
40TEST(FindLoopsTest, CFG1) {
41 // Constant is not used.
42 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
43 Instruction::CONST_4 | 0 | 0,
44 Instruction::RETURN_VOID);
45
46 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +010047 ArenaAllocator allocator(&arena);
48 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010049 for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) {
50 ASSERT_EQ(graph->GetBlocks().Get(i)->GetLoopInformation(), nullptr);
51 }
52}
53
54TEST(FindLoopsTest, CFG2) {
55 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
56 Instruction::CONST_4 | 0 | 0,
57 Instruction::RETURN);
58
59 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +010060 ArenaAllocator allocator(&arena);
61 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010062 for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) {
63 ASSERT_EQ(graph->GetBlocks().Get(i)->GetLoopInformation(), nullptr);
64 }
65}
66
67TEST(FindLoopsTest, CFG3) {
68 const uint16_t data[] = TWO_REGISTERS_CODE_ITEM(
69 Instruction::CONST_4 | 3 << 12 | 0,
70 Instruction::CONST_4 | 4 << 12 | 1 << 8,
71 Instruction::ADD_INT_2ADDR | 1 << 12,
72 Instruction::GOTO | 0x100,
73 Instruction::RETURN);
74
75 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +010076 ArenaAllocator allocator(&arena);
77 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010078 for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) {
79 ASSERT_EQ(graph->GetBlocks().Get(i)->GetLoopInformation(), nullptr);
80 }
81}
82
83TEST(FindLoopsTest, CFG4) {
84 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
85 Instruction::CONST_4 | 0 | 0,
86 Instruction::IF_EQ, 4,
87 Instruction::CONST_4 | 4 << 12 | 0,
88 Instruction::GOTO | 0x200,
89 Instruction::CONST_4 | 5 << 12 | 0,
90 Instruction::RETURN | 0 << 8);
91
92 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +010093 ArenaAllocator allocator(&arena);
94 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +010095 for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) {
96 ASSERT_EQ(graph->GetBlocks().Get(i)->GetLoopInformation(), nullptr);
97 }
98}
99
100TEST(FindLoopsTest, CFG5) {
101 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
102 Instruction::CONST_4 | 0 | 0,
103 Instruction::IF_EQ, 3,
104 Instruction::CONST_4 | 4 << 12 | 0,
105 Instruction::RETURN | 0 << 8);
106
107 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100108 ArenaAllocator allocator(&arena);
109 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100110 for (size_t i = 0, e = graph->GetBlocks().Size(); i < e; ++i) {
111 ASSERT_EQ(graph->GetBlocks().Get(i)->GetLoopInformation(), nullptr);
112 }
113}
114
115static void TestBlock(HGraph* graph,
116 int block_id,
117 bool is_loop_header,
118 int parent_loop_header_id,
119 const int* blocks_in_loop = nullptr,
120 size_t number_of_blocks = 0) {
121 HBasicBlock* block = graph->GetBlocks().Get(block_id);
122 ASSERT_EQ(block->IsLoopHeader(), is_loop_header);
123 if (parent_loop_header_id == -1) {
124 ASSERT_EQ(block->GetLoopInformation(), nullptr);
125 } else {
126 ASSERT_EQ(block->GetLoopInformation()->GetHeader()->GetBlockId(), parent_loop_header_id);
127 }
128
129 if (blocks_in_loop != nullptr) {
130 HLoopInformation* info = block->GetLoopInformation();
131 const BitVector& blocks = info->GetBlocks();
132 ASSERT_EQ(blocks.NumSetBits(), number_of_blocks);
133 for (size_t i = 0; i < number_of_blocks; ++i) {
134 ASSERT_TRUE(blocks.IsBitSet(blocks_in_loop[i]));
135 }
136 } else {
137 ASSERT_FALSE(block->IsLoopHeader());
138 }
139}
140
141TEST(FindLoopsTest, Loop1) {
142 // Simple loop with one preheader and one back edge.
143 // var a = 0;
144 // while (a == a) {
145 // }
146 // return;
147 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
148 Instruction::CONST_4 | 0 | 0,
149 Instruction::IF_EQ, 3,
150 Instruction::GOTO | 0xFE00,
151 Instruction::RETURN_VOID);
152
153 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100154 ArenaAllocator allocator(&arena);
155 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100156
157 TestBlock(graph, 0, false, -1); // entry block
158 TestBlock(graph, 1, false, -1); // pre header
159 const int blocks2[] = {2, 3};
160 TestBlock(graph, 2, true, 2, blocks2, 2); // loop header
161 TestBlock(graph, 3, false, 2); // block in loop
162 TestBlock(graph, 4, false, -1); // return block
163 TestBlock(graph, 5, false, -1); // exit block
164}
165
166TEST(FindLoopsTest, Loop2) {
167 // Make sure we support a preheader of a loop not being the first predecessor
168 // in the predecessor list of the header.
169 // var a = 0;
170 // while (a == a) {
171 // }
172 // return a;
173 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
174 Instruction::CONST_4 | 0 | 0,
175 Instruction::GOTO | 0x400,
176 Instruction::IF_EQ, 4,
177 Instruction::GOTO | 0xFE00,
178 Instruction::GOTO | 0xFD00,
179 Instruction::RETURN | 0 << 8);
180
181 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100182 ArenaAllocator allocator(&arena);
183 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100184
185 TestBlock(graph, 0, false, -1); // entry block
186 TestBlock(graph, 1, false, -1); // goto block
187 const int blocks2[] = {2, 3};
188 TestBlock(graph, 2, true, 2, blocks2, 2); // loop header
189 TestBlock(graph, 3, false, 2); // block in loop
190 TestBlock(graph, 4, false, -1); // pre header
191 TestBlock(graph, 5, false, -1); // return block
192 TestBlock(graph, 6, false, -1); // exit block
193}
194
195TEST(FindLoopsTest, Loop3) {
196 // Make sure we create a preheader of a loop when a header originally has two
197 // incoming blocks and one back edge.
198 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
199 Instruction::CONST_4 | 0 | 0,
200 Instruction::IF_EQ, 3,
201 Instruction::GOTO | 0x100,
202 Instruction::IF_EQ, 3,
203 Instruction::GOTO | 0xFE00,
204 Instruction::RETURN | 0 << 8);
205
206 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100207 ArenaAllocator allocator(&arena);
208 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100209
210 TestBlock(graph, 0, false, -1); // entry block
211 TestBlock(graph, 1, false, -1); // goto block
212 TestBlock(graph, 2, false, -1);
213 const int blocks2[] = {3, 4};
214 TestBlock(graph, 3, true, 3, blocks2, 2); // loop header
215 TestBlock(graph, 4, false, 3); // block in loop
216 TestBlock(graph, 5, false, -1); // pre header
217 TestBlock(graph, 6, false, -1); // return block
218 TestBlock(graph, 7, false, -1); // exit block
219 TestBlock(graph, 8, false, -1); // synthesized pre header
220}
221
222TEST(FindLoopsTest, Loop4) {
223 // Test loop with originally two back edges.
224 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
225 Instruction::CONST_4 | 0 | 0,
226 Instruction::IF_EQ, 6,
227 Instruction::IF_EQ, 3,
228 Instruction::GOTO | 0xFC00,
229 Instruction::GOTO | 0xFB00,
230 Instruction::RETURN | 0 << 8);
231
232 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100233 ArenaAllocator allocator(&arena);
234 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100235
236 TestBlock(graph, 0, false, -1); // entry block
237 TestBlock(graph, 1, false, -1); // pre header
238 const int blocks2[] = {2, 3, 4, 5, 8};
239 TestBlock(graph, 2, true, 2, blocks2, 5); // loop header
240 TestBlock(graph, 3, false, 2); // block in loop
241 TestBlock(graph, 4, false, 2); // original back edge
242 TestBlock(graph, 5, false, 2); // original back edge
243 TestBlock(graph, 6, false, -1); // return block
244 TestBlock(graph, 7, false, -1); // exit block
245 TestBlock(graph, 8, false, 2); // synthesized back edge
246}
247
248
249TEST(FindLoopsTest, Loop5) {
250 // Test loop with two exit edges.
251 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
252 Instruction::CONST_4 | 0 | 0,
253 Instruction::IF_EQ, 6,
254 Instruction::IF_EQ, 3,
255 Instruction::GOTO | 0x0200,
256 Instruction::GOTO | 0xFB00,
257 Instruction::RETURN | 0 << 8);
258
259 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100260 ArenaAllocator allocator(&arena);
261 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100262
263 TestBlock(graph, 0, false, -1); // entry block
264 TestBlock(graph, 1, false, -1); // pre header
265 const int blocks2[] = {2, 3, 5};
266 TestBlock(graph, 2, true, 2, blocks2, 3); // loop header
267 TestBlock(graph, 3, false, 2); // block in loop
268 TestBlock(graph, 4, false, -1); // loop exit
269 TestBlock(graph, 5, false, 2); // back edge
270 TestBlock(graph, 6, false, -1); // return block
271 TestBlock(graph, 7, false, -1); // exit block
272 TestBlock(graph, 8, false, -1); // synthesized block at the loop exit
273}
274
275TEST(FindLoopsTest, InnerLoop) {
276 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
277 Instruction::CONST_4 | 0 | 0,
278 Instruction::IF_EQ, 6,
279 Instruction::IF_EQ, 3,
280 Instruction::GOTO | 0xFE00, // inner loop
281 Instruction::GOTO | 0xFB00,
282 Instruction::RETURN | 0 << 8);
283
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100284 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100285 ArenaAllocator allocator(&arena);
286 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100287
288 TestBlock(graph, 0, false, -1); // entry block
289 TestBlock(graph, 1, false, -1); // pre header of outer loop
290 const int blocks2[] = {2, 3, 4, 5, 8};
291 TestBlock(graph, 2, true, 2, blocks2, 5); // outer loop header
292 const int blocks3[] = {3, 4};
293 TestBlock(graph, 3, true, 3, blocks3, 2); // inner loop header
294 TestBlock(graph, 4, false, 3); // back edge on inner loop
295 TestBlock(graph, 5, false, 2); // back edge on outer loop
296 TestBlock(graph, 6, false, -1); // return block
297 TestBlock(graph, 7, false, -1); // exit block
298 TestBlock(graph, 8, false, 2); // synthesized block as pre header of inner loop
299
300 ASSERT_TRUE(graph->GetBlocks().Get(3)->GetLoopInformation()->IsIn(
301 *graph->GetBlocks().Get(2)->GetLoopInformation()));
302 ASSERT_FALSE(graph->GetBlocks().Get(2)->GetLoopInformation()->IsIn(
303 *graph->GetBlocks().Get(3)->GetLoopInformation()));
304}
305
306TEST(FindLoopsTest, TwoLoops) {
307 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
308 Instruction::CONST_4 | 0 | 0,
309 Instruction::IF_EQ, 3,
310 Instruction::GOTO | 0xFE00, // first loop
311 Instruction::IF_EQ, 3,
312 Instruction::GOTO | 0xFE00, // second loop
313 Instruction::RETURN | 0 << 8);
314
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100315 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100316 ArenaAllocator allocator(&arena);
317 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100318
319 TestBlock(graph, 0, false, -1); // entry block
320 TestBlock(graph, 1, false, -1); // pre header of first loop
321 const int blocks2[] = {2, 3};
322 TestBlock(graph, 2, true, 2, blocks2, 2); // first loop header
323 TestBlock(graph, 3, false, 2); // back edge of first loop
324 const int blocks4[] = {4, 5};
325 TestBlock(graph, 4, true, 4, blocks4, 2); // second loop header
326 TestBlock(graph, 5, false, 4); // back edge of second loop
327 TestBlock(graph, 6, false, -1); // return block
328 TestBlock(graph, 7, false, -1); // exit block
329
330 ASSERT_FALSE(graph->GetBlocks().Get(4)->GetLoopInformation()->IsIn(
331 *graph->GetBlocks().Get(2)->GetLoopInformation()));
332 ASSERT_FALSE(graph->GetBlocks().Get(2)->GetLoopInformation()->IsIn(
333 *graph->GetBlocks().Get(4)->GetLoopInformation()));
334}
335
336TEST(FindLoopsTest, NonNaturalLoop) {
337 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
338 Instruction::CONST_4 | 0 | 0,
339 Instruction::IF_EQ, 3,
340 Instruction::GOTO | 0x0100,
341 Instruction::IF_EQ, 3,
342 Instruction::GOTO | 0xFD00,
343 Instruction::RETURN | 0 << 8);
344
345 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100346 ArenaAllocator allocator(&arena);
347 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100348 ASSERT_TRUE(graph->GetBlocks().Get(3)->IsLoopHeader());
349 HLoopInformation* info = graph->GetBlocks().Get(3)->GetLoopInformation();
350 ASSERT_FALSE(info->GetHeader()->Dominates(info->GetBackEdges().Get(0)));
351}
352
353TEST(FindLoopsTest, DoWhileLoop) {
354 const uint16_t data[] = ONE_REGISTER_CODE_ITEM(
355 Instruction::CONST_4 | 0 | 0,
356 Instruction::GOTO | 0x0100,
357 Instruction::IF_EQ, 0xFFFF,
358 Instruction::RETURN | 0 << 8);
359
360 ArenaPool arena;
Nicolas Geoffraye6c96d12014-09-10 10:34:01 +0100361 ArenaAllocator allocator(&arena);
362 HGraph* graph = TestCode(data, &allocator);
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100363
364 TestBlock(graph, 0, false, -1); // entry block
365 TestBlock(graph, 1, false, -1); // pre header of first loop
366 const int blocks2[] = {2, 3, 6};
367 TestBlock(graph, 2, true, 2, blocks2, 3); // loop header
368 TestBlock(graph, 3, false, 2); // back edge of first loop
369 TestBlock(graph, 4, false, -1); // return block
370 TestBlock(graph, 5, false, -1); // exit block
371 TestBlock(graph, 6, false, 2); // synthesized block to avoid a critical edge
372}
373
374} // namespace art