blob: 40ced70948ff879a00bc47d88660150d165299c7 [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.
173 mir->width = 2u;
174 mir->optimization_flags = 0u;
175 merged_df_flags |= MIRGraph::oat_data_flow_attributes_[def->opcode];
176 }
177 cu_.mir_graph->merged_df_flags_ = merged_df_flags;
178
179 code_item_ = static_cast<DexFile::CodeItem*>(
180 cu_.arena.Alloc(sizeof(DexFile::CodeItem), kArenaAllocMisc));
181 memset(code_item_, 0, sizeof(DexFile::CodeItem));
182 code_item_->insns_size_in_code_units_ = 2u * count;
183 cu_.mir_graph->current_code_item_ = cu_.code_item = code_item_;
184 }
185
186 template <size_t count>
187 void PrepareMIRs(const MIRDef (&defs)[count]) {
188 DoPrepareMIRs(defs, count);
189 }
190
191 void PerformClassInitCheckElimination() {
192 cu_.mir_graph->ComputeDFSOrders();
193 bool gate_result = cu_.mir_graph->EliminateClassInitChecksGate();
194 ASSERT_TRUE(gate_result);
195 RepeatingPreOrderDfsIterator iterator(cu_.mir_graph.get());
196 bool change = false;
197 for (BasicBlock *bb = iterator.Next(change); bb != 0; bb = iterator.Next(change)) {
198 change = cu_.mir_graph->EliminateClassInitChecks(bb);
199 }
200 cu_.mir_graph->EliminateClassInitChecksEnd();
201 }
202
203 ClassInitCheckEliminationTest()
204 : pool_(),
205 cu_(&pool_),
206 mir_count_(0u),
207 mirs_(nullptr),
208 code_item_(nullptr) {
209 cu_.mir_graph.reset(new MIRGraph(&cu_, &cu_.arena));
210 }
211
212 ArenaPool pool_;
213 CompilationUnit cu_;
214 size_t mir_count_;
215 MIR* mirs_;
216 DexFile::CodeItem* code_item_;
217};
218
219TEST_F(ClassInitCheckEliminationTest, SingleBlock) {
220 static const SFieldDef sfields[] = {
221 { 0u, 1u, 0u, 0u },
222 { 1u, 1u, 1u, 1u },
223 { 2u, 1u, 2u, 2u },
224 { 3u, 1u, 3u, 3u }, // Same declaring class as sfield[4].
225 { 4u, 1u, 3u, 4u }, // Same declaring class as sfield[3].
226 { 5u, 0u, 0u, 0u }, // Unresolved.
227 };
228 static const BBDef bbs[] = {
229 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
230 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
231 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(3)),
232 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(1)),
233 };
234 static const MIRDef mirs[] = {
235 DEF_MIR(Instruction::SPUT, 3u, 5u), // Unresolved.
236 DEF_MIR(Instruction::SPUT, 3u, 0u),
237 DEF_MIR(Instruction::SGET, 3u, 1u),
238 DEF_MIR(Instruction::SGET, 3u, 2u),
239 DEF_MIR(Instruction::SGET, 3u, 5u), // Unresolved.
240 DEF_MIR(Instruction::SGET, 3u, 0u),
241 DEF_MIR(Instruction::SGET, 3u, 1u),
242 DEF_MIR(Instruction::SGET, 3u, 2u),
243 DEF_MIR(Instruction::SGET, 3u, 5u), // Unresolved.
244 DEF_MIR(Instruction::SGET, 3u, 3u),
245 DEF_MIR(Instruction::SGET, 3u, 4u),
246 };
247 static const bool expected_ignore_clinit_check[] = {
248 false, false, false, false, false, true, true, true, false, false, true
249 };
250
251 PrepareSFields(sfields);
252 PrepareBasicBlocks(bbs);
253 PrepareMIRs(mirs);
254 PerformClassInitCheckElimination();
255 ASSERT_EQ(arraysize(expected_ignore_clinit_check), mir_count_);
256 for (size_t i = 0u; i != arraysize(mirs); ++i) {
257 EXPECT_EQ(expected_ignore_clinit_check[i],
258 (mirs_[i].optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0) << i;
259 }
260}
261
262TEST_F(ClassInitCheckEliminationTest, Diamond) {
263 static const SFieldDef sfields[] = {
264 { 0u, 1u, 0u, 0u },
265 { 1u, 1u, 1u, 1u },
266 { 2u, 1u, 2u, 2u },
267 { 3u, 1u, 3u, 3u },
268 { 4u, 1u, 4u, 4u },
269 { 5u, 1u, 5u, 5u },
270 { 6u, 1u, 6u, 6u },
271 { 7u, 1u, 7u, 7u },
272 { 8u, 1u, 8u, 8u }, // Same declaring class as sfield[9].
273 { 9u, 1u, 8u, 9u }, // Same declaring class as sfield[8].
274 { 10u, 0u, 0u, 0u }, // Unresolved.
275 };
276 static const BBDef bbs[] = {
277 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
278 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
279 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(6)),
280 DEF_BB(kDalvikByteCode, DEF_SUCC2(4, 5), DEF_PRED1(1)),
281 DEF_BB(kDalvikByteCode, DEF_SUCC1(6), DEF_PRED1(3)),
282 DEF_BB(kDalvikByteCode, DEF_SUCC1(6), DEF_PRED1(3)),
283 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED2(4, 5)),
284 };
285 static const MIRDef mirs[] = {
286 // NOTE: MIRs here are ordered by unique tests. They will be put into appropriate blocks.
287 DEF_MIR(Instruction::SGET, 3u, 10u), // Unresolved.
288 DEF_MIR(Instruction::SPUT, 3u, 10u), // Unresolved.
289 DEF_MIR(Instruction::SPUT, 3u, 0u),
290 DEF_MIR(Instruction::SGET, 6u, 0u), // Eliminated (block #3 dominates #6).
291 DEF_MIR(Instruction::SPUT, 4u, 1u),
292 DEF_MIR(Instruction::SGET, 6u, 1u), // Not eliminated (block #4 doesn't dominate #6).
293 DEF_MIR(Instruction::SGET, 3u, 2u),
294 DEF_MIR(Instruction::SGET, 4u, 2u), // Eliminated (block #3 dominates #4).
295 DEF_MIR(Instruction::SGET, 3u, 3u),
296 DEF_MIR(Instruction::SGET, 5u, 3u), // Eliminated (block #3 dominates #5).
297 DEF_MIR(Instruction::SGET, 3u, 4u),
298 DEF_MIR(Instruction::SGET, 6u, 4u), // Eliminated (block #3 dominates #6).
299 DEF_MIR(Instruction::SGET, 4u, 5u),
300 DEF_MIR(Instruction::SGET, 6u, 5u), // Not eliminated (block #4 doesn't dominate #6).
301 DEF_MIR(Instruction::SGET, 5u, 6u),
302 DEF_MIR(Instruction::SGET, 6u, 6u), // Not eliminated (block #5 doesn't dominate #6).
303 DEF_MIR(Instruction::SGET, 4u, 7u),
304 DEF_MIR(Instruction::SGET, 5u, 7u),
305 DEF_MIR(Instruction::SGET, 6u, 7u), // Eliminated (initialized in both blocks #3 and #4).
306 DEF_MIR(Instruction::SGET, 4u, 8u),
307 DEF_MIR(Instruction::SGET, 5u, 9u),
308 DEF_MIR(Instruction::SGET, 6u, 8u), // Eliminated (with sfield[9] in block #5).
309 DEF_MIR(Instruction::SPUT, 6u, 9u), // Eliminated (with sfield[8] in block #4).
310 };
311 static const bool expected_ignore_clinit_check[] = {
312 false, false, // Unresolved: sfield[10], method[2]
313 false, true, // sfield[0]
314 false, false, // sfield[1]
315 false, true, // sfield[2]
316 false, true, // sfield[3]
317 false, true, // sfield[4]
318 false, false, // sfield[5]
319 false, false, // sfield[6]
320 false, false, true, // sfield[7]
321 false, false, true, true, // sfield[8], sfield[9]
322 };
323
324 PrepareSFields(sfields);
325 PrepareBasicBlocks(bbs);
326 PrepareMIRs(mirs);
327 PerformClassInitCheckElimination();
328 ASSERT_EQ(arraysize(expected_ignore_clinit_check), mir_count_);
329 for (size_t i = 0u; i != arraysize(mirs); ++i) {
330 EXPECT_EQ(expected_ignore_clinit_check[i],
331 (mirs_[i].optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0) << i;
332 }
333}
334
335TEST_F(ClassInitCheckEliminationTest, Loop) {
336 static const SFieldDef sfields[] = {
337 { 0u, 1u, 0u, 0u },
338 { 1u, 1u, 1u, 1u },
339 };
340 static const BBDef bbs[] = {
341 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
342 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
343 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(5)),
344 DEF_BB(kDalvikByteCode, DEF_SUCC1(4), DEF_PRED1(1)),
345 DEF_BB(kDalvikByteCode, DEF_SUCC2(5, 4), DEF_PRED2(3, 4)), // "taken" loops to self.
346 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(4)),
347 };
348 static const MIRDef mirs[] = {
349 DEF_MIR(Instruction::SGET, 3u, 0u),
350 DEF_MIR(Instruction::SGET, 4u, 1u),
351 DEF_MIR(Instruction::SGET, 5u, 0u), // Eliminated.
352 DEF_MIR(Instruction::SGET, 5u, 1u), // Eliminated.
353 };
354 static const bool expected_ignore_clinit_check[] = {
355 false, false, true, true
356 };
357
358 PrepareSFields(sfields);
359 PrepareBasicBlocks(bbs);
360 PrepareMIRs(mirs);
361 PerformClassInitCheckElimination();
362 ASSERT_EQ(arraysize(expected_ignore_clinit_check), mir_count_);
363 for (size_t i = 0u; i != arraysize(mirs); ++i) {
364 EXPECT_EQ(expected_ignore_clinit_check[i],
365 (mirs_[i].optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0) << i;
366 }
367}
368
369TEST_F(ClassInitCheckEliminationTest, Catch) {
370 static const SFieldDef sfields[] = {
371 { 0u, 1u, 0u, 0u },
372 { 1u, 1u, 1u, 1u },
373 };
374 static const BBDef bbs[] = {
375 DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()),
376 DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()),
377 DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(5)),
378 DEF_BB(kDalvikByteCode, DEF_SUCC2(5, 4), DEF_PRED1(1)),
379 DEF_BB(kDalvikByteCode, DEF_SUCC1(5), DEF_PRED1(3)), // Catch handler.
380 DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED2(3, 4)),
381 };
382 static const MIRDef mirs[] = {
383 DEF_MIR(Instruction::SGET, 3u, 0u),
384 DEF_MIR(Instruction::SGET, 3u, 1u),
385 DEF_MIR(Instruction::SGET, 4u, 1u),
386 DEF_MIR(Instruction::SGET, 5u, 0u), // Not eliminated.
387 DEF_MIR(Instruction::SGET, 5u, 1u), // Eliminated.
388 };
389 static const bool expected_ignore_clinit_check[] = {
390 false, false, false, false, true
391 };
392
393 PrepareSFields(sfields);
394 PrepareBasicBlocks(bbs);
395 BasicBlock* catch_handler = cu_.mir_graph->GetBasicBlock(4u);
396 catch_handler->catch_entry = true;
397 PrepareMIRs(mirs);
398 PerformClassInitCheckElimination();
399 ASSERT_EQ(arraysize(expected_ignore_clinit_check), mir_count_);
400 for (size_t i = 0u; i != arraysize(mirs); ++i) {
401 EXPECT_EQ(expected_ignore_clinit_check[i],
402 (mirs_[i].optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0) << i;
403 }
404}
405
406} // namespace art