blob: bdc05a968e04fe8e90e6d2bdc42289e90716ed79 [file] [log] [blame]
Vladimir Marko55fff042014-07-10 12:42:52 +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 "mir_graph.h"
18#include "gtest/gtest.h"
19
20namespace art {
21
22class TopologicalSortOrderTest : public testing::Test {
23 protected:
24 struct BBDef {
25 static constexpr size_t kMaxSuccessors = 4;
26 static constexpr size_t kMaxPredecessors = 4;
27
28 BBType type;
29 size_t num_successors;
30 BasicBlockId successors[kMaxPredecessors];
31 size_t num_predecessors;
32 BasicBlockId predecessors[kMaxPredecessors];
33 };
34
35#define DEF_SUCC0() \
36 0u, { }
37#define DEF_SUCC1(s1) \
38 1u, { s1 }
39#define DEF_SUCC2(s1, s2) \
40 2u, { s1, s2 }
41#define DEF_SUCC3(s1, s2, s3) \
42 3u, { s1, s2, s3 }
43#define DEF_SUCC4(s1, s2, s3, s4) \
44 4u, { s1, s2, s3, s4 }
45#define DEF_PRED0() \
46 0u, { }
47#define DEF_PRED1(p1) \
48 1u, { p1 }
49#define DEF_PRED2(p1, p2) \
50 2u, { p1, p2 }
51#define DEF_PRED3(p1, p2, p3) \
52 3u, { p1, p2, p3 }
53#define DEF_PRED4(p1, p2, p3, p4) \
54 4u, { p1, p2, p3, p4 }
55#define DEF_BB(type, succ, pred) \
56 { type, succ, pred }
57
58 void DoPrepareBasicBlocks(const BBDef* defs, size_t count) {
59 cu_.mir_graph->block_id_map_.clear();
60 cu_.mir_graph->block_list_.Reset();
61 ASSERT_LT(3u, count); // null, entry, exit and at least one bytecode block.
62 ASSERT_EQ(kNullBlock, defs[0].type);
63 ASSERT_EQ(kEntryBlock, defs[1].type);
64 ASSERT_EQ(kExitBlock, defs[2].type);
65 for (size_t i = 0u; i != count; ++i) {
66 const BBDef* def = &defs[i];
67 BasicBlock* bb = cu_.mir_graph->NewMemBB(def->type, i);
68 cu_.mir_graph->block_list_.Insert(bb);
69 if (def->num_successors <= 2) {
70 bb->successor_block_list_type = kNotUsed;
71 bb->successor_blocks = nullptr;
72 bb->fall_through = (def->num_successors >= 1) ? def->successors[0] : 0u;
73 bb->taken = (def->num_successors >= 2) ? def->successors[1] : 0u;
74 } else {
75 bb->successor_block_list_type = kPackedSwitch;
76 bb->fall_through = 0u;
77 bb->taken = 0u;
78 bb->successor_blocks = new (&cu_.arena) GrowableArray<SuccessorBlockInfo*>(
79 &cu_.arena, def->num_successors, kGrowableArraySuccessorBlocks);
80 for (size_t j = 0u; j != def->num_successors; ++j) {
81 SuccessorBlockInfo* successor_block_info =
82 static_cast<SuccessorBlockInfo*>(cu_.arena.Alloc(sizeof(SuccessorBlockInfo),
83 kArenaAllocSuccessor));
84 successor_block_info->block = j;
85 successor_block_info->key = 0u; // Not used by class init check elimination.
86 bb->successor_blocks->Insert(successor_block_info);
87 }
88 }
89 bb->predecessors = new (&cu_.arena) GrowableArray<BasicBlockId>(
90 &cu_.arena, def->num_predecessors, kGrowableArrayPredecessors);
91 for (size_t j = 0u; j != def->num_predecessors; ++j) {
92 ASSERT_NE(0u, def->predecessors[j]);
93 bb->predecessors->Insert(def->predecessors[j]);
94 }
95 if (def->type == kDalvikByteCode || def->type == kEntryBlock || def->type == kExitBlock) {
96 bb->data_flow_info = static_cast<BasicBlockDataFlow*>(
97 cu_.arena.Alloc(sizeof(BasicBlockDataFlow), kArenaAllocDFInfo));
98 }
99 }
100 cu_.mir_graph->num_blocks_ = count;
101 ASSERT_EQ(count, cu_.mir_graph->block_list_.Size());
102 cu_.mir_graph->entry_block_ = cu_.mir_graph->block_list_.Get(1);
103 ASSERT_EQ(kEntryBlock, cu_.mir_graph->entry_block_->block_type);
104 cu_.mir_graph->exit_block_ = cu_.mir_graph->block_list_.Get(2);
105 ASSERT_EQ(kExitBlock, cu_.mir_graph->exit_block_->block_type);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700106
107 DexFile::CodeItem* code_item = static_cast<DexFile::CodeItem*>(cu_.arena.Alloc(sizeof(DexFile::CodeItem),
108 kArenaAllocMisc));
109 cu_.mir_graph->current_code_item_ = cu_.code_item = code_item;
Vladimir Marko55fff042014-07-10 12:42:52 +0100110 }
111
112 template <size_t count>
113 void PrepareBasicBlocks(const BBDef (&defs)[count]) {
114 DoPrepareBasicBlocks(defs, count);
115 }
116
117 void ComputeTopologicalSortOrder() {
118 cu_.mir_graph->SSATransformationStart();
119 cu_.mir_graph->ComputeDFSOrders();
120 cu_.mir_graph->ComputeDominators();
121 cu_.mir_graph->ComputeTopologicalSortOrder();
122 cu_.mir_graph->SSATransformationEnd();
123 ASSERT_NE(cu_.mir_graph->topological_order_, nullptr);
124 ASSERT_NE(cu_.mir_graph->topological_order_loop_ends_, nullptr);
125 ASSERT_NE(cu_.mir_graph->topological_order_indexes_, nullptr);
126 ASSERT_EQ(cu_.mir_graph->GetNumBlocks(), cu_.mir_graph->topological_order_indexes_->Size());
127 for (size_t i = 0, size = cu_.mir_graph->GetTopologicalSortOrder()->Size(); i != size; ++i) {
128 ASSERT_LT(cu_.mir_graph->topological_order_->Get(i), cu_.mir_graph->GetNumBlocks());
129 BasicBlockId id = cu_.mir_graph->topological_order_->Get(i);
130 EXPECT_EQ(i, cu_.mir_graph->topological_order_indexes_->Get(id));
131 }
132 }
133
134 void DoCheckOrder(const BasicBlockId* ids, size_t count) {
135 ASSERT_EQ(count, cu_.mir_graph->GetTopologicalSortOrder()->Size());
136 for (size_t i = 0; i != count; ++i) {
137 EXPECT_EQ(ids[i], cu_.mir_graph->GetTopologicalSortOrder()->Get(i)) << i;
138 }
139 }
140
141 template <size_t count>
142 void CheckOrder(const BasicBlockId (&ids)[count]) {
143 DoCheckOrder(ids, count);
144 }
145
146 void DoCheckLoopEnds(const uint16_t* ends, size_t count) {
147 for (size_t i = 0; i != count; ++i) {
148 ASSERT_LT(i, cu_.mir_graph->GetTopologicalSortOrderLoopEnds()->Size());
149 EXPECT_EQ(ends[i], cu_.mir_graph->GetTopologicalSortOrderLoopEnds()->Get(i)) << i;
150 }
151 }
152
153 template <size_t count>
154 void CheckLoopEnds(const uint16_t (&ends)[count]) {
155 DoCheckLoopEnds(ends, count);
156 }
157
158 TopologicalSortOrderTest()
159 : pool_(),
160 cu_(&pool_) {
161 cu_.mir_graph.reset(new MIRGraph(&cu_, &cu_.arena));
162 }
163
164 ArenaPool pool_;
165 CompilationUnit cu_;
166};
167
168TEST_F(TopologicalSortOrderTest, DoWhile) {
169 const BBDef bbs[] = {
170 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
171 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
172 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(5)),
173 DEF_BB(kDalvikByteCode, DEF_SUCC1(4), DEF_PRED1(1)),
174 DEF_BB(kDalvikByteCode, DEF_SUCC2(5, 4), DEF_PRED2(3, 4)), // "taken" loops to self.
175 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(4)),
176 };
177 const BasicBlockId expected_order[] = {
178 1, 3, 4, 5, 2
179 };
180 const uint16_t loop_ends[] = {
181 0, 0, 3, 0, 0
182 };
183
184 PrepareBasicBlocks(bbs);
185 ComputeTopologicalSortOrder();
186 CheckOrder(expected_order);
187 CheckLoopEnds(loop_ends);
188}
189
190TEST_F(TopologicalSortOrderTest, While) {
191 const BBDef bbs[] = {
192 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
193 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
194 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(5)),
195 DEF_BB(kDalvikByteCode, DEF_SUCC2(4, 5), DEF_PRED2(1, 4)),
196 DEF_BB(kDalvikByteCode, DEF_SUCC1(3), DEF_PRED1(3)), // Loops to 3.
197 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(3)),
198 };
199 const BasicBlockId expected_order[] = {
200 1, 3, 4, 5, 2
201 };
202 const uint16_t loop_ends[] = {
203 0, 3, 0, 0, 0
204 };
205
206 PrepareBasicBlocks(bbs);
207 ComputeTopologicalSortOrder();
208 CheckOrder(expected_order);
209 CheckLoopEnds(loop_ends);
210}
211
212TEST_F(TopologicalSortOrderTest, WhileWithTwoBackEdges) {
213 const BBDef bbs[] = {
214 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
215 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
216 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(6)),
217 DEF_BB(kDalvikByteCode, DEF_SUCC2(4, 6), DEF_PRED3(1, 4, 5)),
218 DEF_BB(kDalvikByteCode, DEF_SUCC2(5, 3), DEF_PRED1(3)), // Loops to 3.
219 DEF_BB(kDalvikByteCode, DEF_SUCC1(3), DEF_PRED1(4)), // Loops to 3.
220 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(3)),
221 };
222 const BasicBlockId expected_order[] = {
223 1, 3, 4, 5, 6, 2
224 };
225 const uint16_t loop_ends[] = {
226 0, 4, 0, 0, 0, 0
227 };
228
229 PrepareBasicBlocks(bbs);
230 ComputeTopologicalSortOrder();
231 CheckOrder(expected_order);
232 CheckLoopEnds(loop_ends);
233}
234
235TEST_F(TopologicalSortOrderTest, NestedLoop) {
236 const BBDef bbs[] = {
237 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
238 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
239 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(7)),
240 DEF_BB(kDalvikByteCode, DEF_SUCC2(4, 7), DEF_PRED2(1, 6)),
241 DEF_BB(kDalvikByteCode, DEF_SUCC2(5, 6), DEF_PRED2(3, 5)),
242 DEF_BB(kDalvikByteCode, DEF_SUCC1(4), DEF_PRED1(4)), // Loops to 4.
243 DEF_BB(kDalvikByteCode, DEF_SUCC1(3), DEF_PRED1(4)), // Loops to 3.
244 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(3)),
245 };
246 const BasicBlockId expected_order[] = {
247 1, 3, 4, 5, 6, 7, 2
248 };
249 const uint16_t loop_ends[] = {
250 0, 5, 4, 0, 0, 0, 0
251 };
252
253 PrepareBasicBlocks(bbs);
254 ComputeTopologicalSortOrder();
255 CheckOrder(expected_order);
256 CheckLoopEnds(loop_ends);
257}
258
259TEST_F(TopologicalSortOrderTest, NestedLoopHeadLoops) {
260 const BBDef bbs[] = {
261 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
262 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
263 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(6)),
264 DEF_BB(kDalvikByteCode, DEF_SUCC2(4, 6), DEF_PRED2(1, 4)),
265 DEF_BB(kDalvikByteCode, DEF_SUCC2(5, 3), DEF_PRED2(3, 5)), // Nested head, loops to 3.
266 DEF_BB(kDalvikByteCode, DEF_SUCC1(4), DEF_PRED1(4)), // Loops to 4.
267 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(3)),
268 };
269 const BasicBlockId expected_order[] = {
270 1, 3, 4, 5, 6, 2
271 };
272 const uint16_t loop_ends[] = {
273 0, 4, 4, 0, 0, 0
274 };
275
276 PrepareBasicBlocks(bbs);
277 ComputeTopologicalSortOrder();
278 CheckOrder(expected_order);
279 CheckLoopEnds(loop_ends);
280}
281
282TEST_F(TopologicalSortOrderTest, NestedLoopSameBackBranchBlock) {
283 const BBDef bbs[] = {
284 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
285 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
286 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(6)),
287 DEF_BB(kDalvikByteCode, DEF_SUCC2(4, 6), DEF_PRED2(1, 5)),
288 DEF_BB(kDalvikByteCode, DEF_SUCC1(5), DEF_PRED2(3, 5)),
289 DEF_BB(kDalvikByteCode, DEF_SUCC2(4, 3), DEF_PRED1(4)), // Loops to 4 and 3.
290 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(3)),
291 };
292 const BasicBlockId expected_order[] = {
293 1, 3, 4, 5, 6, 2
294 };
295 const uint16_t loop_ends[] = {
296 0, 4, 4, 0, 0, 0
297 };
298
299 PrepareBasicBlocks(bbs);
300 ComputeTopologicalSortOrder();
301 CheckOrder(expected_order);
302 CheckLoopEnds(loop_ends);
303}
304
305TEST_F(TopologicalSortOrderTest, TwoReorderedInnerLoops) {
306 // This is a simplified version of real code graph where the branch from 8 to 5 must prevent
307 // the block 5 from being considered a loop head before processing the loop 7-8.
308 const BBDef bbs[] = {
309 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
310 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
311 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(9)),
312 DEF_BB(kDalvikByteCode, DEF_SUCC2(4, 9), DEF_PRED2(1, 5)),
313 DEF_BB(kDalvikByteCode, DEF_SUCC2(5, 7), DEF_PRED1(3)), // Branch over loop in 5.
314 DEF_BB(kDalvikByteCode, DEF_SUCC2(6, 3), DEF_PRED3(4, 6, 8)), // Loops to 4; inner loop.
315 DEF_BB(kDalvikByteCode, DEF_SUCC1(5), DEF_PRED1(5)), // Loops to 5.
316 DEF_BB(kDalvikByteCode, DEF_SUCC1(8), DEF_PRED2(4, 8)), // Loop head.
317 DEF_BB(kDalvikByteCode, DEF_SUCC2(7, 5), DEF_PRED1(7)), // Loops to 7; branches to 5.
318 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(3)),
319 };
320 const BasicBlockId expected_order[] = {
321 1, 3, 4, 7, 8, 5, 6, 9, 2
322 };
323 const uint16_t loop_ends[] = {
324 0, 7, 0, 5, 0, 7, 0, 0, 0
325 };
326
327 PrepareBasicBlocks(bbs);
328 ComputeTopologicalSortOrder();
329 CheckOrder(expected_order);
330 CheckLoopEnds(loop_ends);
331}
332
333TEST_F(TopologicalSortOrderTest, NestedLoopWithBackEdgeAfterOuterLoopBackEdge) {
334 // This is a simplified version of real code graph. The back-edge from 7 to the inner
335 // loop head 4 comes after the back-edge from 6 to the outer loop head 3. To make this
336 // appear a bit more complex, there's also a back-edge from 5 to 4.
337 const BBDef bbs[] = {
338 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
339 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
340 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(7)),
341 DEF_BB(kDalvikByteCode, DEF_SUCC1(4), DEF_PRED2(1, 6)), // Outer loop head.
342 DEF_BB(kDalvikByteCode, DEF_SUCC2(5, 6), DEF_PRED3(3, 5, 7)), // Inner loop head.
343 DEF_BB(kDalvikByteCode, DEF_SUCC1(4), DEF_PRED1(4)), // Loops to inner loop head 4.
344 DEF_BB(kDalvikByteCode, DEF_SUCC2(7, 3), DEF_PRED1(4)), // Loops to outer loop head 3.
345 DEF_BB(kDalvikByteCode, DEF_SUCC2(2, 4), DEF_PRED1(6)), // Loops to inner loop head 4.
346 };
347 const BasicBlockId expected_order[] = {
348 // NOTE: The 5 goes before 6 only because 5 is a "fall-through" from 4 while 6 is "taken".
349 1, 3, 4, 5, 6, 7, 2
350 };
351 const uint16_t loop_ends[] = {
352 0, 6, 6, 0, 0, 0, 0
353 };
354
355 PrepareBasicBlocks(bbs);
356 ComputeTopologicalSortOrder();
357 CheckOrder(expected_order);
358 CheckLoopEnds(loop_ends);
359}
360
361TEST_F(TopologicalSortOrderTest, LoopWithTwoEntryPoints) {
362 const BBDef bbs[] = {
363 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
364 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
365 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(7)),
366 DEF_BB(kDalvikByteCode, DEF_SUCC2(5, 4), DEF_PRED1(1)),
367 DEF_BB(kDalvikByteCode, DEF_SUCC1(5), DEF_PRED2(3, 6)), // Fall-back block is chosen as
368 DEF_BB(kDalvikByteCode, DEF_SUCC1(6), DEF_PRED2(3, 4)), // the earlier from these two.
369 DEF_BB(kDalvikByteCode, DEF_SUCC2(4, 7), DEF_PRED1(5)),
370 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(6)),
371 };
372 const BasicBlockId expected_order[] = {
373 1, 3, 4, 5, 6, 7, 2
374 };
375 const uint16_t loop_ends[] = {
376 0, 0, 5, 0, 0, 0, 0
377 };
378
379 PrepareBasicBlocks(bbs);
380 ComputeTopologicalSortOrder();
381 CheckOrder(expected_order);
382 CheckLoopEnds(loop_ends);
383}
384
385} // namespace art