Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 24 | namespace art { |
| 25 | |
| 26 | class 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) { |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 79 | cu_.mir_graph->sfield_lowering_infos_.clear(); |
| 80 | cu_.mir_graph->sfield_lowering_infos_.reserve(count); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 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()); |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 92 | cu_.mir_graph->sfield_lowering_infos_.push_back(field_info); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 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(); |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 103 | cu_.mir_graph->block_list_.clear(); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 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]; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 110 | BasicBlock* bb = cu_.mir_graph->CreateNewBB(def->type); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 111 | if (def->num_successors <= 2) { |
| 112 | bb->successor_block_list_type = kNotUsed; |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 113 | bb->fall_through = (def->num_successors >= 1) ? def->successors[0] : 0u; |
| 114 | bb->taken = (def->num_successors >= 2) ? def->successors[1] : 0u; |
| 115 | } else { |
| 116 | bb->successor_block_list_type = kPackedSwitch; |
| 117 | bb->fall_through = 0u; |
| 118 | bb->taken = 0u; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 119 | bb->successor_blocks.reserve(def->num_successors); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 120 | for (size_t j = 0u; j != def->num_successors; ++j) { |
| 121 | SuccessorBlockInfo* successor_block_info = |
| 122 | static_cast<SuccessorBlockInfo*>(cu_.arena.Alloc(sizeof(SuccessorBlockInfo), |
| 123 | kArenaAllocSuccessor)); |
| 124 | successor_block_info->block = j; |
| 125 | successor_block_info->key = 0u; // Not used by class init check elimination. |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 126 | bb->successor_blocks.push_back(successor_block_info); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 129 | bb->predecessors.assign(def->predecessors, def->predecessors + def->num_predecessors); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 130 | if (def->type == kDalvikByteCode || def->type == kEntryBlock || def->type == kExitBlock) { |
| 131 | bb->data_flow_info = static_cast<BasicBlockDataFlow*>( |
| 132 | cu_.arena.Alloc(sizeof(BasicBlockDataFlow), kArenaAllocDFInfo)); |
| 133 | } |
| 134 | } |
| 135 | cu_.mir_graph->num_blocks_ = count; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 136 | ASSERT_EQ(count, cu_.mir_graph->block_list_.size()); |
| 137 | cu_.mir_graph->entry_block_ = cu_.mir_graph->block_list_[1]; |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 138 | ASSERT_EQ(kEntryBlock, cu_.mir_graph->entry_block_->block_type); |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 139 | cu_.mir_graph->exit_block_ = cu_.mir_graph->block_list_[2]; |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 140 | ASSERT_EQ(kExitBlock, cu_.mir_graph->exit_block_->block_type); |
| 141 | } |
| 142 | |
| 143 | template <size_t count> |
| 144 | void PrepareBasicBlocks(const BBDef (&defs)[count]) { |
| 145 | DoPrepareBasicBlocks(defs, count); |
| 146 | } |
| 147 | |
| 148 | void DoPrepareMIRs(const MIRDef* defs, size_t count) { |
| 149 | mir_count_ = count; |
| 150 | mirs_ = reinterpret_cast<MIR*>(cu_.arena.Alloc(sizeof(MIR) * count, kArenaAllocMIR)); |
| 151 | uint64_t merged_df_flags = 0u; |
| 152 | for (size_t i = 0u; i != count; ++i) { |
| 153 | const MIRDef* def = &defs[i]; |
| 154 | MIR* mir = &mirs_[i]; |
| 155 | mir->dalvikInsn.opcode = def->opcode; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 156 | ASSERT_LT(def->bbid, cu_.mir_graph->block_list_.size()); |
| 157 | BasicBlock* bb = cu_.mir_graph->block_list_[def->bbid]; |
Jean Christophe Beyler | cdacac4 | 2014-03-13 14:54:59 -0700 | [diff] [blame] | 158 | bb->AppendMIR(mir); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 159 | if (def->opcode >= Instruction::SGET && def->opcode <= Instruction::SPUT_SHORT) { |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 160 | ASSERT_LT(def->field_or_method_info, cu_.mir_graph->sfield_lowering_infos_.size()); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 161 | mir->meta.sfield_lowering_info = def->field_or_method_info; |
| 162 | } |
| 163 | mir->ssa_rep = nullptr; |
| 164 | mir->offset = 2 * i; // All insns need to be at least 2 code units long. |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 165 | mir->optimization_flags = 0u; |
Jean Christophe Beyler | cc794c3 | 2014-05-02 09:34:13 -0700 | [diff] [blame] | 166 | merged_df_flags |= MIRGraph::GetDataFlowAttributes(def->opcode); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 167 | } |
| 168 | cu_.mir_graph->merged_df_flags_ = merged_df_flags; |
| 169 | |
| 170 | code_item_ = static_cast<DexFile::CodeItem*>( |
| 171 | cu_.arena.Alloc(sizeof(DexFile::CodeItem), kArenaAllocMisc)); |
| 172 | memset(code_item_, 0, sizeof(DexFile::CodeItem)); |
| 173 | code_item_->insns_size_in_code_units_ = 2u * count; |
Razvan A Lupusoru | 7503597 | 2014-09-11 15:24:59 -0700 | [diff] [blame^] | 174 | cu_.mir_graph->current_code_item_ = code_item_; |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | template <size_t count> |
| 178 | void PrepareMIRs(const MIRDef (&defs)[count]) { |
| 179 | DoPrepareMIRs(defs, count); |
| 180 | } |
| 181 | |
| 182 | void PerformClassInitCheckElimination() { |
Vladimir Marko | c9360ce | 2014-06-05 20:09:47 +0100 | [diff] [blame] | 183 | cu_.mir_graph->SSATransformationStart(); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 184 | cu_.mir_graph->ComputeDFSOrders(); |
Vladimir Marko | 622bdbe | 2014-06-19 14:59:05 +0100 | [diff] [blame] | 185 | cu_.mir_graph->ComputeDominators(); |
| 186 | cu_.mir_graph->ComputeTopologicalSortOrder(); |
Vladimir Marko | c9360ce | 2014-06-05 20:09:47 +0100 | [diff] [blame] | 187 | cu_.mir_graph->SSATransformationEnd(); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 188 | bool gate_result = cu_.mir_graph->EliminateClassInitChecksGate(); |
| 189 | ASSERT_TRUE(gate_result); |
Vladimir Marko | 55fff04 | 2014-07-10 12:42:52 +0100 | [diff] [blame] | 190 | LoopRepeatingTopologicalSortIterator iterator(cu_.mir_graph.get()); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 191 | bool change = false; |
Jean Christophe Beyler | 2469e60 | 2014-05-06 20:36:55 -0700 | [diff] [blame] | 192 | for (BasicBlock* bb = iterator.Next(change); bb != nullptr; bb = iterator.Next(change)) { |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 193 | change = cu_.mir_graph->EliminateClassInitChecks(bb); |
| 194 | } |
| 195 | cu_.mir_graph->EliminateClassInitChecksEnd(); |
| 196 | } |
| 197 | |
| 198 | ClassInitCheckEliminationTest() |
| 199 | : pool_(), |
| 200 | cu_(&pool_), |
| 201 | mir_count_(0u), |
| 202 | mirs_(nullptr), |
| 203 | code_item_(nullptr) { |
| 204 | cu_.mir_graph.reset(new MIRGraph(&cu_, &cu_.arena)); |
| 205 | } |
| 206 | |
| 207 | ArenaPool pool_; |
| 208 | CompilationUnit cu_; |
| 209 | size_t mir_count_; |
| 210 | MIR* mirs_; |
| 211 | DexFile::CodeItem* code_item_; |
| 212 | }; |
| 213 | |
| 214 | TEST_F(ClassInitCheckEliminationTest, SingleBlock) { |
| 215 | static const SFieldDef sfields[] = { |
| 216 | { 0u, 1u, 0u, 0u }, |
| 217 | { 1u, 1u, 1u, 1u }, |
| 218 | { 2u, 1u, 2u, 2u }, |
| 219 | { 3u, 1u, 3u, 3u }, // Same declaring class as sfield[4]. |
| 220 | { 4u, 1u, 3u, 4u }, // Same declaring class as sfield[3]. |
| 221 | { 5u, 0u, 0u, 0u }, // Unresolved. |
| 222 | }; |
| 223 | static const BBDef bbs[] = { |
| 224 | DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()), |
| 225 | DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()), |
| 226 | DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(3)), |
| 227 | DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(1)), |
| 228 | }; |
| 229 | static const MIRDef mirs[] = { |
| 230 | DEF_MIR(Instruction::SPUT, 3u, 5u), // Unresolved. |
| 231 | DEF_MIR(Instruction::SPUT, 3u, 0u), |
| 232 | DEF_MIR(Instruction::SGET, 3u, 1u), |
| 233 | DEF_MIR(Instruction::SGET, 3u, 2u), |
| 234 | DEF_MIR(Instruction::SGET, 3u, 5u), // Unresolved. |
| 235 | DEF_MIR(Instruction::SGET, 3u, 0u), |
| 236 | DEF_MIR(Instruction::SGET, 3u, 1u), |
| 237 | DEF_MIR(Instruction::SGET, 3u, 2u), |
| 238 | DEF_MIR(Instruction::SGET, 3u, 5u), // Unresolved. |
| 239 | DEF_MIR(Instruction::SGET, 3u, 3u), |
| 240 | DEF_MIR(Instruction::SGET, 3u, 4u), |
| 241 | }; |
| 242 | static const bool expected_ignore_clinit_check[] = { |
Vladimir Marko | f418f32 | 2014-07-09 14:45:36 +0100 | [diff] [blame] | 243 | false, false, false, false, true, true, true, true, true, false, true |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | PrepareSFields(sfields); |
| 247 | PrepareBasicBlocks(bbs); |
| 248 | PrepareMIRs(mirs); |
| 249 | PerformClassInitCheckElimination(); |
| 250 | ASSERT_EQ(arraysize(expected_ignore_clinit_check), mir_count_); |
| 251 | for (size_t i = 0u; i != arraysize(mirs); ++i) { |
| 252 | EXPECT_EQ(expected_ignore_clinit_check[i], |
| 253 | (mirs_[i].optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0) << i; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | TEST_F(ClassInitCheckEliminationTest, Diamond) { |
| 258 | static const SFieldDef sfields[] = { |
| 259 | { 0u, 1u, 0u, 0u }, |
| 260 | { 1u, 1u, 1u, 1u }, |
| 261 | { 2u, 1u, 2u, 2u }, |
| 262 | { 3u, 1u, 3u, 3u }, |
| 263 | { 4u, 1u, 4u, 4u }, |
| 264 | { 5u, 1u, 5u, 5u }, |
| 265 | { 6u, 1u, 6u, 6u }, |
| 266 | { 7u, 1u, 7u, 7u }, |
| 267 | { 8u, 1u, 8u, 8u }, // Same declaring class as sfield[9]. |
| 268 | { 9u, 1u, 8u, 9u }, // Same declaring class as sfield[8]. |
| 269 | { 10u, 0u, 0u, 0u }, // Unresolved. |
| 270 | }; |
| 271 | static const BBDef bbs[] = { |
| 272 | DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()), |
| 273 | DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()), |
| 274 | DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(6)), |
| 275 | DEF_BB(kDalvikByteCode, DEF_SUCC2(4, 5), DEF_PRED1(1)), |
| 276 | DEF_BB(kDalvikByteCode, DEF_SUCC1(6), DEF_PRED1(3)), |
| 277 | DEF_BB(kDalvikByteCode, DEF_SUCC1(6), DEF_PRED1(3)), |
| 278 | DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED2(4, 5)), |
| 279 | }; |
| 280 | static const MIRDef mirs[] = { |
| 281 | // NOTE: MIRs here are ordered by unique tests. They will be put into appropriate blocks. |
| 282 | DEF_MIR(Instruction::SGET, 3u, 10u), // Unresolved. |
| 283 | DEF_MIR(Instruction::SPUT, 3u, 10u), // Unresolved. |
| 284 | DEF_MIR(Instruction::SPUT, 3u, 0u), |
| 285 | DEF_MIR(Instruction::SGET, 6u, 0u), // Eliminated (block #3 dominates #6). |
| 286 | DEF_MIR(Instruction::SPUT, 4u, 1u), |
| 287 | DEF_MIR(Instruction::SGET, 6u, 1u), // Not eliminated (block #4 doesn't dominate #6). |
| 288 | DEF_MIR(Instruction::SGET, 3u, 2u), |
| 289 | DEF_MIR(Instruction::SGET, 4u, 2u), // Eliminated (block #3 dominates #4). |
| 290 | DEF_MIR(Instruction::SGET, 3u, 3u), |
| 291 | DEF_MIR(Instruction::SGET, 5u, 3u), // Eliminated (block #3 dominates #5). |
| 292 | DEF_MIR(Instruction::SGET, 3u, 4u), |
| 293 | DEF_MIR(Instruction::SGET, 6u, 4u), // Eliminated (block #3 dominates #6). |
| 294 | DEF_MIR(Instruction::SGET, 4u, 5u), |
| 295 | DEF_MIR(Instruction::SGET, 6u, 5u), // Not eliminated (block #4 doesn't dominate #6). |
| 296 | DEF_MIR(Instruction::SGET, 5u, 6u), |
| 297 | DEF_MIR(Instruction::SGET, 6u, 6u), // Not eliminated (block #5 doesn't dominate #6). |
| 298 | DEF_MIR(Instruction::SGET, 4u, 7u), |
| 299 | DEF_MIR(Instruction::SGET, 5u, 7u), |
| 300 | DEF_MIR(Instruction::SGET, 6u, 7u), // Eliminated (initialized in both blocks #3 and #4). |
| 301 | DEF_MIR(Instruction::SGET, 4u, 8u), |
| 302 | DEF_MIR(Instruction::SGET, 5u, 9u), |
| 303 | DEF_MIR(Instruction::SGET, 6u, 8u), // Eliminated (with sfield[9] in block #5). |
| 304 | DEF_MIR(Instruction::SPUT, 6u, 9u), // Eliminated (with sfield[8] in block #4). |
| 305 | }; |
| 306 | static const bool expected_ignore_clinit_check[] = { |
Vladimir Marko | f418f32 | 2014-07-09 14:45:36 +0100 | [diff] [blame] | 307 | false, true, // Unresolved: sfield[10], method[2] |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 308 | false, true, // sfield[0] |
| 309 | false, false, // sfield[1] |
| 310 | false, true, // sfield[2] |
| 311 | false, true, // sfield[3] |
| 312 | false, true, // sfield[4] |
| 313 | false, false, // sfield[5] |
| 314 | false, false, // sfield[6] |
| 315 | false, false, true, // sfield[7] |
| 316 | false, false, true, true, // sfield[8], sfield[9] |
| 317 | }; |
| 318 | |
| 319 | PrepareSFields(sfields); |
| 320 | PrepareBasicBlocks(bbs); |
| 321 | PrepareMIRs(mirs); |
| 322 | PerformClassInitCheckElimination(); |
| 323 | ASSERT_EQ(arraysize(expected_ignore_clinit_check), mir_count_); |
| 324 | for (size_t i = 0u; i != arraysize(mirs); ++i) { |
| 325 | EXPECT_EQ(expected_ignore_clinit_check[i], |
| 326 | (mirs_[i].optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0) << i; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | TEST_F(ClassInitCheckEliminationTest, Loop) { |
| 331 | static const SFieldDef sfields[] = { |
| 332 | { 0u, 1u, 0u, 0u }, |
| 333 | { 1u, 1u, 1u, 1u }, |
| 334 | }; |
| 335 | static const BBDef bbs[] = { |
| 336 | DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()), |
| 337 | DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()), |
| 338 | DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(5)), |
| 339 | DEF_BB(kDalvikByteCode, DEF_SUCC1(4), DEF_PRED1(1)), |
| 340 | DEF_BB(kDalvikByteCode, DEF_SUCC2(5, 4), DEF_PRED2(3, 4)), // "taken" loops to self. |
| 341 | DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED1(4)), |
| 342 | }; |
| 343 | static const MIRDef mirs[] = { |
| 344 | DEF_MIR(Instruction::SGET, 3u, 0u), |
| 345 | DEF_MIR(Instruction::SGET, 4u, 1u), |
| 346 | DEF_MIR(Instruction::SGET, 5u, 0u), // Eliminated. |
| 347 | DEF_MIR(Instruction::SGET, 5u, 1u), // Eliminated. |
| 348 | }; |
| 349 | static const bool expected_ignore_clinit_check[] = { |
| 350 | false, false, true, true |
| 351 | }; |
| 352 | |
| 353 | PrepareSFields(sfields); |
| 354 | PrepareBasicBlocks(bbs); |
| 355 | PrepareMIRs(mirs); |
| 356 | PerformClassInitCheckElimination(); |
| 357 | ASSERT_EQ(arraysize(expected_ignore_clinit_check), mir_count_); |
| 358 | for (size_t i = 0u; i != arraysize(mirs); ++i) { |
| 359 | EXPECT_EQ(expected_ignore_clinit_check[i], |
| 360 | (mirs_[i].optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0) << i; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | TEST_F(ClassInitCheckEliminationTest, Catch) { |
| 365 | static const SFieldDef sfields[] = { |
| 366 | { 0u, 1u, 0u, 0u }, |
| 367 | { 1u, 1u, 1u, 1u }, |
Vladimir Marko | 0a810d2 | 2014-07-11 14:44:36 +0100 | [diff] [blame] | 368 | { 2u, 1u, 2u, 2u }, |
| 369 | { 3u, 1u, 3u, 3u }, |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 370 | }; |
| 371 | static const BBDef bbs[] = { |
| 372 | DEF_BB(kNullBlock, DEF_SUCC0(), DEF_PRED0()), |
| 373 | DEF_BB(kEntryBlock, DEF_SUCC1(3), DEF_PRED0()), |
Vladimir Marko | 0a810d2 | 2014-07-11 14:44:36 +0100 | [diff] [blame] | 374 | DEF_BB(kExitBlock, DEF_SUCC0(), DEF_PRED1(6)), |
| 375 | DEF_BB(kDalvikByteCode, DEF_SUCC1(4), DEF_PRED1(1)), // The top. |
| 376 | DEF_BB(kDalvikByteCode, DEF_SUCC1(6), DEF_PRED1(3)), // The throwing insn. |
| 377 | DEF_BB(kDalvikByteCode, DEF_SUCC1(6), DEF_PRED1(3)), // Catch handler. |
| 378 | DEF_BB(kDalvikByteCode, DEF_SUCC1(2), DEF_PRED2(4, 5)), // The merged block. |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 379 | }; |
| 380 | static const MIRDef mirs[] = { |
Vladimir Marko | 0a810d2 | 2014-07-11 14:44:36 +0100 | [diff] [blame] | 381 | DEF_MIR(Instruction::SGET, 3u, 0u), // Before the exception edge. |
| 382 | DEF_MIR(Instruction::SGET, 3u, 1u), // Before the exception edge. |
| 383 | DEF_MIR(Instruction::SGET, 4u, 2u), // After the exception edge. |
| 384 | DEF_MIR(Instruction::SGET, 4u, 3u), // After the exception edge. |
| 385 | DEF_MIR(Instruction::SGET, 5u, 0u), // In catch handler; class init check eliminated. |
| 386 | DEF_MIR(Instruction::SGET, 5u, 2u), // In catch handler; class init check not eliminated. |
| 387 | DEF_MIR(Instruction::SGET, 6u, 0u), // Class init check eliminated. |
| 388 | DEF_MIR(Instruction::SGET, 6u, 1u), // Class init check eliminated. |
| 389 | DEF_MIR(Instruction::SGET, 6u, 2u), // Class init check eliminated. |
| 390 | DEF_MIR(Instruction::SGET, 6u, 3u), // Class init check not eliminated. |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 391 | }; |
| 392 | static const bool expected_ignore_clinit_check[] = { |
Vladimir Marko | 0a810d2 | 2014-07-11 14:44:36 +0100 | [diff] [blame] | 393 | false, false, false, false, true, false, true, true, true, false |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | PrepareSFields(sfields); |
| 397 | PrepareBasicBlocks(bbs); |
Vladimir Marko | 0a810d2 | 2014-07-11 14:44:36 +0100 | [diff] [blame] | 398 | BasicBlock* catch_handler = cu_.mir_graph->GetBasicBlock(5u); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 399 | catch_handler->catch_entry = true; |
Vladimir Marko | 0a810d2 | 2014-07-11 14:44:36 +0100 | [diff] [blame] | 400 | // Add successor block info to the check block. |
| 401 | BasicBlock* check_bb = cu_.mir_graph->GetBasicBlock(3u); |
| 402 | check_bb->successor_block_list_type = kCatch; |
Vladimir Marko | 0a810d2 | 2014-07-11 14:44:36 +0100 | [diff] [blame] | 403 | SuccessorBlockInfo* successor_block_info = reinterpret_cast<SuccessorBlockInfo*> |
| 404 | (cu_.arena.Alloc(sizeof(SuccessorBlockInfo), kArenaAllocSuccessor)); |
| 405 | successor_block_info->block = catch_handler->id; |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 406 | check_bb->successor_blocks.push_back(successor_block_info); |
Vladimir Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 407 | PrepareMIRs(mirs); |
| 408 | PerformClassInitCheckElimination(); |
| 409 | ASSERT_EQ(arraysize(expected_ignore_clinit_check), mir_count_); |
| 410 | for (size_t i = 0u; i != arraysize(mirs); ++i) { |
| 411 | EXPECT_EQ(expected_ignore_clinit_check[i], |
| 412 | (mirs_[i].optimization_flags & MIR_IGNORE_CLINIT_CHECK) != 0) << i; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | } // namespace art |