blob: 29c353a4e6263988a53f3836273e9a1603780d03 [file] [log] [blame]
Vladimir Markobfea9c22014-01-17 17:49:33 +00001/*
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 <vector>
18
19#include "compiler_internals.h"
20#include "dataflow_iterator.h"
21#include "dataflow_iterator-inl.h"
22#include "gtest/gtest.h"
23
24namespace art {
25
26class ClassInitCheckEliminationTest : public testing::Test {
27 protected:
28 struct SFieldDef {
29 uint16_t field_idx;
30 uintptr_t declaring_dex_file;
31 uint16_t declaring_class_idx;
32 uint16_t declaring_field_idx;
33 };
34
35 struct BBDef {
36 static constexpr size_t kMaxSuccessors = 4;
37 static constexpr size_t kMaxPredecessors = 4;
38
39 BBType type;
40 size_t num_successors;
41 BasicBlockId successors[kMaxPredecessors];
42 size_t num_predecessors;
43 BasicBlockId predecessors[kMaxPredecessors];
44 };
45
46 struct MIRDef {
47 Instruction::Code opcode;
48 BasicBlockId bbid;
49 uint32_t field_or_method_info;
50 };
51
52#define DEF_SUCC0() \
53 0u, { }
54#define DEF_SUCC1(s1) \
55 1u, { s1 }
56#define DEF_SUCC2(s1, s2) \
57 2u, { s1, s2 }
58#define DEF_SUCC3(s1, s2, s3) \
59 3u, { s1, s2, s3 }
60#define DEF_SUCC4(s1, s2, s3, s4) \
61 4u, { s1, s2, s3, s4 }
62#define DEF_PRED0() \
63 0u, { }
64#define DEF_PRED1(p1) \
65 1u, { p1 }
66#define DEF_PRED2(p1, p2) \
67 2u, { p1, p2 }
68#define DEF_PRED3(p1, p2, p3) \
69 3u, { p1, p2, p3 }
70#define DEF_PRED4(p1, p2, p3, p4) \
71 4u, { p1, p2, p3, p4 }
72#define DEF_BB(type, succ, pred) \
73 { type, succ, pred }
74
75#define DEF_MIR(opcode, bb, field_info) \
76 { opcode, bb, field_info }
77
78 void DoPrepareSFields(const SFieldDef* defs, size_t count) {
79 cu_.mir_graph->sfield_lowering_infos_.Reset();
80 cu_.mir_graph->sfield_lowering_infos_.Resize(count);
81 for (size_t i = 0u; i != count; ++i) {
82 const SFieldDef* def = &defs[i];
83 MirSFieldLoweringInfo field_info(def->field_idx);
84 if (def->declaring_dex_file != 0u) {
85 field_info.declaring_dex_file_ = reinterpret_cast<const DexFile*>(def->declaring_dex_file);
86 field_info.declaring_class_idx_ = def->declaring_class_idx;
87 field_info.declaring_field_idx_ = def->declaring_field_idx;
88 field_info.flags_ = MirSFieldLoweringInfo::kFlagIsStatic;
89 }
90 ASSERT_EQ(def->declaring_dex_file != 0u, field_info.IsResolved());
91 ASSERT_FALSE(field_info.IsInitialized());
92 cu_.mir_graph->sfield_lowering_infos_.Insert(field_info);
93 }
94 }
95
96 template <size_t count>
97 void PrepareSFields(const SFieldDef (&defs)[count]) {
98 DoPrepareSFields(defs, count);
99 }
100
101 void DoPrepareBasicBlocks(const BBDef* defs, size_t count) {
102 cu_.mir_graph->block_id_map_.clear();
103 cu_.mir_graph->block_list_.Reset();
104 ASSERT_LT(3u, count); // null, entry, exit and at least one bytecode block.
105 ASSERT_EQ(kNullBlock, defs[0].type);
106 ASSERT_EQ(kEntryBlock, defs[1].type);
107 ASSERT_EQ(kExitBlock, defs[2].type);
108 for (size_t i = 0u; i != count; ++i) {
109 const BBDef* def = &defs[i];
110 BasicBlock* bb = cu_.mir_graph->NewMemBB(def->type, i);
111 cu_.mir_graph->block_list_.Insert(bb);
112 if (def->num_successors <= 2) {
113 bb->successor_block_list_type = kNotUsed;
114 bb->successor_blocks = nullptr;
115 bb->fall_through = (def->num_successors >= 1) ? def->successors[0] : 0u;
116 bb->taken = (def->num_successors >= 2) ? def->successors[1] : 0u;
117 } else {
118 bb->successor_block_list_type = kPackedSwitch;
119 bb->fall_through = 0u;
120 bb->taken = 0u;
121 bb->successor_blocks = new (&cu_.arena) GrowableArray<SuccessorBlockInfo*>(
122 &cu_.arena, def->num_successors, kGrowableArraySuccessorBlocks);
123 for (size_t j = 0u; j != def->num_successors; ++j) {
124 SuccessorBlockInfo* successor_block_info =
125 static_cast<SuccessorBlockInfo*>(cu_.arena.Alloc(sizeof(SuccessorBlockInfo),
126 kArenaAllocSuccessor));
127 successor_block_info->block = j;
128 successor_block_info->key = 0u; // Not used by class init check elimination.
129 bb->successor_blocks->Insert(successor_block_info);
130 }
131 }
132 bb->predecessors = new (&cu_.arena) GrowableArray<BasicBlockId>(
133 &cu_.arena, def->num_predecessors, kGrowableArrayPredecessors);
134 for (size_t j = 0u; j != def->num_predecessors; ++j) {
135 ASSERT_NE(0u, def->predecessors[j]);
136 bb->predecessors->Insert(def->predecessors[j]);
137 }
138 if (def->type == kDalvikByteCode || def->type == kEntryBlock || def->type == kExitBlock) {
139 bb->data_flow_info = static_cast<BasicBlockDataFlow*>(
140 cu_.arena.Alloc(sizeof(BasicBlockDataFlow), kArenaAllocDFInfo));
141 }
142 }
143 cu_.mir_graph->num_blocks_ = count;
144 ASSERT_EQ(count, cu_.mir_graph->block_list_.Size());
145 cu_.mir_graph->entry_block_ = cu_.mir_graph->block_list_.Get(1);
146 ASSERT_EQ(kEntryBlock, cu_.mir_graph->entry_block_->block_type);
147 cu_.mir_graph->exit_block_ = cu_.mir_graph->block_list_.Get(2);
148 ASSERT_EQ(kExitBlock, cu_.mir_graph->exit_block_->block_type);
149 }
150
151 template <size_t count>
152 void PrepareBasicBlocks(const BBDef (&defs)[count]) {
153 DoPrepareBasicBlocks(defs, count);
154 }
155
156 void DoPrepareMIRs(const MIRDef* defs, size_t count) {
157 mir_count_ = count;
158 mirs_ = reinterpret_cast<MIR*>(cu_.arena.Alloc(sizeof(MIR) * count, kArenaAllocMIR));
159 uint64_t merged_df_flags = 0u;
160 for (size_t i = 0u; i != count; ++i) {
161 const MIRDef* def = &defs[i];
162 MIR* mir = &mirs_[i];
163 mir->dalvikInsn.opcode = def->opcode;
164 ASSERT_LT(def->bbid, cu_.mir_graph->block_list_.Size());
165 BasicBlock* bb = cu_.mir_graph->block_list_.Get(def->bbid);
Jean Christophe Beylercdacac42014-03-13 14:54:59 -0700166 bb->AppendMIR(mir);
Vladimir Markobfea9c22014-01-17 17:49:33 +0000167 if (def->opcode >= Instruction::SGET && def->opcode <= Instruction::SPUT_SHORT) {
168 ASSERT_LT(def->field_or_method_info, cu_.mir_graph->sfield_lowering_infos_.Size());
169 mir->meta.sfield_lowering_info = def->field_or_method_info;
170 }
171 mir->ssa_rep = nullptr;
172 mir->offset = 2 * i; // All insns need to be at least 2 code units long.
Vladimir Markobfea9c22014-01-17 17:49:33 +0000173 mir->optimization_flags = 0u;
Jean Christophe Beylercc794c32014-05-02 09:34:13 -0700174 merged_df_flags |= MIRGraph::GetDataFlowAttributes(def->opcode);
Vladimir Markobfea9c22014-01-17 17:49:33 +0000175 }
176 cu_.mir_graph->merged_df_flags_ = merged_df_flags;
177
178 code_item_ = static_cast<DexFile::CodeItem*>(
179 cu_.arena.Alloc(sizeof(DexFile::CodeItem), kArenaAllocMisc));
180 memset(code_item_, 0, sizeof(DexFile::CodeItem));
181 code_item_->insns_size_in_code_units_ = 2u * count;
182 cu_.mir_graph->current_code_item_ = cu_.code_item = code_item_;
183 }
184
185 template <size_t count>
186 void PrepareMIRs(const MIRDef (&defs)[count]) {
187 DoPrepareMIRs(defs, count);
188 }
189
190 void PerformClassInitCheckElimination() {
Vladimir Markoc9360ce2014-06-05 20:09:47 +0100191 cu_.mir_graph->SSATransformationStart();
Vladimir Markobfea9c22014-01-17 17:49:33 +0000192 cu_.mir_graph->ComputeDFSOrders();
Vladimir Markoc9360ce2014-06-05 20:09:47 +0100193 cu_.mir_graph->SSATransformationEnd();
Vladimir Markobfea9c22014-01-17 17:49:33 +0000194 bool gate_result = cu_.mir_graph->EliminateClassInitChecksGate();
195 ASSERT_TRUE(gate_result);
196 RepeatingPreOrderDfsIterator iterator(cu_.mir_graph.get());
197 bool change = false;
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700198 for (BasicBlock* bb = iterator.Next(change); bb != nullptr; bb = iterator.Next(change)) {
Vladimir Markobfea9c22014-01-17 17:49:33 +0000199 change = cu_.mir_graph->EliminateClassInitChecks(bb);
200 }
201 cu_.mir_graph->EliminateClassInitChecksEnd();
202 }
203
204 ClassInitCheckEliminationTest()
205 : pool_(),
206 cu_(&pool_),
207 mir_count_(0u),
208 mirs_(nullptr),
209 code_item_(nullptr) {
210 cu_.mir_graph.reset(new MIRGraph(&cu_, &cu_.arena));
211 }
212
213 ArenaPool pool_;
214 CompilationUnit cu_;
215 size_t mir_count_;
216 MIR* mirs_;
217 DexFile::CodeItem* code_item_;
218};
219
220TEST_F(ClassInitCheckEliminationTest, SingleBlock) {
221 static const SFieldDef sfields[] = {
222 { 0u, 1u, 0u, 0u },
223 { 1u, 1u, 1u, 1u },
224 { 2u, 1u, 2u, 2u },
225 { 3u, 1u, 3u, 3u }, // Same declaring class as sfield[4].
226 { 4u, 1u, 3u, 4u }, // Same declaring class as sfield[3].
227 { 5u, 0u, 0u, 0u }, // Unresolved.
228 };
229 static const BBDef bbs[] = {
230 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
231 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
232 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(3)),
233 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(1)),
234 };
235 static const MIRDef mirs[] = {
236 DEF_MIR(Instruction::SPUT, 3u, 5u), // Unresolved.
237 DEF_MIR(Instruction::SPUT, 3u, 0u),
238 DEF_MIR(Instruction::SGET, 3u, 1u),
239 DEF_MIR(Instruction::SGET, 3u, 2u),
240 DEF_MIR(Instruction::SGET, 3u, 5u), // Unresolved.
241 DEF_MIR(Instruction::SGET, 3u, 0u),
242 DEF_MIR(Instruction::SGET, 3u, 1u),
243 DEF_MIR(Instruction::SGET, 3u, 2u),
244 DEF_MIR(Instruction::SGET, 3u, 5u), // Unresolved.
245 DEF_MIR(Instruction::SGET, 3u, 3u),
246 DEF_MIR(Instruction::SGET, 3u, 4u),
247 };
248 static const bool expected_ignore_clinit_check[] = {
249 false, false, false, false, false, true, true, true, false, false, true
250 };
251
252 PrepareSFields(sfields);
253 PrepareBasicBlocks(bbs);
254 PrepareMIRs(mirs);
255 PerformClassInitCheckElimination();
256 ASSERT_EQ(arraysize(expected_ignore_clinit_check), mir_count_);
257 for (size_t i = 0u; i != arraysize(mirs); ++i) {
258 EXPECT_EQ(expected_ignore_clinit_check[i],
259 (mirs_[i].optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0) << i;
260 }
261}
262
263TEST_F(ClassInitCheckEliminationTest, Diamond) {
264 static const SFieldDef sfields[] = {
265 { 0u, 1u, 0u, 0u },
266 { 1u, 1u, 1u, 1u },
267 { 2u, 1u, 2u, 2u },
268 { 3u, 1u, 3u, 3u },
269 { 4u, 1u, 4u, 4u },
270 { 5u, 1u, 5u, 5u },
271 { 6u, 1u, 6u, 6u },
272 { 7u, 1u, 7u, 7u },
273 { 8u, 1u, 8u, 8u }, // Same declaring class as sfield[9].
274 { 9u, 1u, 8u, 9u }, // Same declaring class as sfield[8].
275 { 10u, 0u, 0u, 0u }, // Unresolved.
276 };
277 static const BBDef bbs[] = {
278 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
279 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
280 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(6)),
281 DEF_BB(kDalvikByteCode, DEF_SUCC2(4, 5), DEF_PRED1(1)),
282 DEF_BB(kDalvikByteCode, DEF_SUCC1(6), DEF_PRED1(3)),
283 DEF_BB(kDalvikByteCode, DEF_SUCC1(6), DEF_PRED1(3)),
284 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED2(4, 5)),
285 };
286 static const MIRDef mirs[] = {
287 // NOTE: MIRs here are ordered by unique tests. They will be put into appropriate blocks.
288 DEF_MIR(Instruction::SGET, 3u, 10u), // Unresolved.
289 DEF_MIR(Instruction::SPUT, 3u, 10u), // Unresolved.
290 DEF_MIR(Instruction::SPUT, 3u, 0u),
291 DEF_MIR(Instruction::SGET, 6u, 0u), // Eliminated (block #3 dominates #6).
292 DEF_MIR(Instruction::SPUT, 4u, 1u),
293 DEF_MIR(Instruction::SGET, 6u, 1u), // Not eliminated (block #4 doesn't dominate #6).
294 DEF_MIR(Instruction::SGET, 3u, 2u),
295 DEF_MIR(Instruction::SGET, 4u, 2u), // Eliminated (block #3 dominates #4).
296 DEF_MIR(Instruction::SGET, 3u, 3u),
297 DEF_MIR(Instruction::SGET, 5u, 3u), // Eliminated (block #3 dominates #5).
298 DEF_MIR(Instruction::SGET, 3u, 4u),
299 DEF_MIR(Instruction::SGET, 6u, 4u), // Eliminated (block #3 dominates #6).
300 DEF_MIR(Instruction::SGET, 4u, 5u),
301 DEF_MIR(Instruction::SGET, 6u, 5u), // Not eliminated (block #4 doesn't dominate #6).
302 DEF_MIR(Instruction::SGET, 5u, 6u),
303 DEF_MIR(Instruction::SGET, 6u, 6u), // Not eliminated (block #5 doesn't dominate #6).
304 DEF_MIR(Instruction::SGET, 4u, 7u),
305 DEF_MIR(Instruction::SGET, 5u, 7u),
306 DEF_MIR(Instruction::SGET, 6u, 7u), // Eliminated (initialized in both blocks #3 and #4).
307 DEF_MIR(Instruction::SGET, 4u, 8u),
308 DEF_MIR(Instruction::SGET, 5u, 9u),
309 DEF_MIR(Instruction::SGET, 6u, 8u), // Eliminated (with sfield[9] in block #5).
310 DEF_MIR(Instruction::SPUT, 6u, 9u), // Eliminated (with sfield[8] in block #4).
311 };
312 static const bool expected_ignore_clinit_check[] = {
313 false, false, // Unresolved: sfield[10], method[2]
314 false, true, // sfield[0]
315 false, false, // sfield[1]
316 false, true, // sfield[2]
317 false, true, // sfield[3]
318 false, true, // sfield[4]
319 false, false, // sfield[5]
320 false, false, // sfield[6]
321 false, false, true, // sfield[7]
322 false, false, true, true, // sfield[8], sfield[9]
323 };
324
325 PrepareSFields(sfields);
326 PrepareBasicBlocks(bbs);
327 PrepareMIRs(mirs);
328 PerformClassInitCheckElimination();
329 ASSERT_EQ(arraysize(expected_ignore_clinit_check), mir_count_);
330 for (size_t i = 0u; i != arraysize(mirs); ++i) {
331 EXPECT_EQ(expected_ignore_clinit_check[i],
332 (mirs_[i].optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0) << i;
333 }
334}
335
336TEST_F(ClassInitCheckEliminationTest, Loop) {
337 static const SFieldDef sfields[] = {
338 { 0u, 1u, 0u, 0u },
339 { 1u, 1u, 1u, 1u },
340 };
341 static const BBDef bbs[] = {
342 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
343 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
344 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(5)),
345 DEF_BB(kDalvikByteCode, DEF_SUCC1(4), DEF_PRED1(1)),
346 DEF_BB(kDalvikByteCode, DEF_SUCC2(5, 4), DEF_PRED2(3, 4)), // "taken" loops to self.
347 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(4)),
348 };
349 static const MIRDef mirs[] = {
350 DEF_MIR(Instruction::SGET, 3u, 0u),
351 DEF_MIR(Instruction::SGET, 4u, 1u),
352 DEF_MIR(Instruction::SGET, 5u, 0u), // Eliminated.
353 DEF_MIR(Instruction::SGET, 5u, 1u), // Eliminated.
354 };
355 static const bool expected_ignore_clinit_check[] = {
356 false, false, true, true
357 };
358
359 PrepareSFields(sfields);
360 PrepareBasicBlocks(bbs);
361 PrepareMIRs(mirs);
362 PerformClassInitCheckElimination();
363 ASSERT_EQ(arraysize(expected_ignore_clinit_check), mir_count_);
364 for (size_t i = 0u; i != arraysize(mirs); ++i) {
365 EXPECT_EQ(expected_ignore_clinit_check[i],
366 (mirs_[i].optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0) << i;
367 }
368}
369
370TEST_F(ClassInitCheckEliminationTest, Catch) {
371 static const SFieldDef sfields[] = {
372 { 0u, 1u, 0u, 0u },
373 { 1u, 1u, 1u, 1u },
374 };
375 static const BBDef bbs[] = {
376 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
377 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
378 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(5)),
379 DEF_BB(kDalvikByteCode, DEF_SUCC2(5, 4), DEF_PRED1(1)),
380 DEF_BB(kDalvikByteCode, DEF_SUCC1(5), DEF_PRED1(3)), // Catch handler.
381 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED2(3, 4)),
382 };
383 static const MIRDef mirs[] = {
384 DEF_MIR(Instruction::SGET, 3u, 0u),
385 DEF_MIR(Instruction::SGET, 3u, 1u),
386 DEF_MIR(Instruction::SGET, 4u, 1u),
387 DEF_MIR(Instruction::SGET, 5u, 0u), // Not eliminated.
388 DEF_MIR(Instruction::SGET, 5u, 1u), // Eliminated.
389 };
390 static const bool expected_ignore_clinit_check[] = {
391 false, false, false, false, true
392 };
393
394 PrepareSFields(sfields);
395 PrepareBasicBlocks(bbs);
396 BasicBlock* catch_handler = cu_.mir_graph->GetBasicBlock(4u);
397 catch_handler->catch_entry = true;
398 PrepareMIRs(mirs);
399 PerformClassInitCheckElimination();
400 ASSERT_EQ(arraysize(expected_ignore_clinit_check), mir_count_);
401 for (size_t i = 0u; i != arraysize(mirs); ++i) {
402 EXPECT_EQ(expected_ignore_clinit_check[i],
403 (mirs_[i].optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0) << i;
404 }
405}
406
407} // namespace art