builder.cc revision 5f02c6caf9f38be49e655f8bdeeeb99b6faf9383
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 "builder.h" 18 19#include "art_field-inl.h" 20#include "base/logging.h" 21#include "class_linker.h" 22#include "dex_file-inl.h" 23#include "dex_instruction-inl.h" 24#include "dex/verified_method.h" 25#include "driver/compiler_driver-inl.h" 26#include "driver/compiler_options.h" 27#include "mirror/class_loader.h" 28#include "mirror/dex_cache.h" 29#include "nodes.h" 30#include "primitive.h" 31#include "scoped_thread_state_change.h" 32#include "thread.h" 33 34namespace art { 35 36/** 37 * Helper class to add HTemporary instructions. This class is used when 38 * converting a DEX instruction to multiple HInstruction, and where those 39 * instructions do not die at the following instruction, but instead spans 40 * multiple instructions. 41 */ 42class Temporaries : public ValueObject { 43 public: 44 explicit Temporaries(HGraph* graph) : graph_(graph), index_(0) {} 45 46 void Add(HInstruction* instruction) { 47 HInstruction* temp = new (graph_->GetArena()) HTemporary(index_); 48 instruction->GetBlock()->AddInstruction(temp); 49 50 DCHECK(temp->GetPrevious() == instruction); 51 52 size_t offset; 53 if (instruction->GetType() == Primitive::kPrimLong 54 || instruction->GetType() == Primitive::kPrimDouble) { 55 offset = 2; 56 } else { 57 offset = 1; 58 } 59 index_ += offset; 60 61 graph_->UpdateTemporariesVRegSlots(index_); 62 } 63 64 private: 65 HGraph* const graph_; 66 67 // Current index in the temporary stack, updated by `Add`. 68 size_t index_; 69}; 70 71class SwitchTable : public ValueObject { 72 public: 73 SwitchTable(const Instruction& instruction, uint32_t dex_pc, bool sparse) 74 : instruction_(instruction), dex_pc_(dex_pc), sparse_(sparse) { 75 int32_t table_offset = instruction.VRegB_31t(); 76 const uint16_t* table = reinterpret_cast<const uint16_t*>(&instruction) + table_offset; 77 if (sparse) { 78 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kSparseSwitchSignature)); 79 } else { 80 CHECK_EQ(table[0], static_cast<uint16_t>(Instruction::kPackedSwitchSignature)); 81 } 82 num_entries_ = table[1]; 83 values_ = reinterpret_cast<const int32_t*>(&table[2]); 84 } 85 86 uint16_t GetNumEntries() const { 87 return num_entries_; 88 } 89 90 void CheckIndex(size_t index) const { 91 if (sparse_) { 92 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order. 93 DCHECK_LT(index, 2 * static_cast<size_t>(num_entries_)); 94 } else { 95 // In a packed table, we have the starting key and num_entries_ values. 96 DCHECK_LT(index, 1 + static_cast<size_t>(num_entries_)); 97 } 98 } 99 100 int32_t GetEntryAt(size_t index) const { 101 CheckIndex(index); 102 return values_[index]; 103 } 104 105 uint32_t GetDexPcForIndex(size_t index) const { 106 CheckIndex(index); 107 return dex_pc_ + 108 (reinterpret_cast<const int16_t*>(values_ + index) - 109 reinterpret_cast<const int16_t*>(&instruction_)); 110 } 111 112 // Index of the first value in the table. 113 size_t GetFirstValueIndex() const { 114 if (sparse_) { 115 // In a sparse table, we have num_entries_ keys and num_entries_ values, in that order. 116 return num_entries_; 117 } else { 118 // In a packed table, we have the starting key and num_entries_ values. 119 return 1; 120 } 121 } 122 123 private: 124 const Instruction& instruction_; 125 const uint32_t dex_pc_; 126 127 // Whether this is a sparse-switch table (or a packed-switch one). 128 const bool sparse_; 129 130 // This can't be const as it needs to be computed off of the given instruction, and complicated 131 // expressions in the initializer list seemed very ugly. 132 uint16_t num_entries_; 133 134 const int32_t* values_; 135 136 DISALLOW_COPY_AND_ASSIGN(SwitchTable); 137}; 138 139void HGraphBuilder::InitializeLocals(uint16_t count) { 140 graph_->SetNumberOfVRegs(count); 141 locals_.SetSize(count); 142 for (int i = 0; i < count; i++) { 143 HLocal* local = new (arena_) HLocal(i); 144 entry_block_->AddInstruction(local); 145 locals_.Put(i, local); 146 } 147} 148 149void HGraphBuilder::InitializeParameters(uint16_t number_of_parameters) { 150 // dex_compilation_unit_ is null only when unit testing. 151 if (dex_compilation_unit_ == nullptr) { 152 return; 153 } 154 155 graph_->SetNumberOfInVRegs(number_of_parameters); 156 const char* shorty = dex_compilation_unit_->GetShorty(); 157 int locals_index = locals_.Size() - number_of_parameters; 158 int parameter_index = 0; 159 160 if (!dex_compilation_unit_->IsStatic()) { 161 // Add the implicit 'this' argument, not expressed in the signature. 162 HParameterValue* parameter = 163 new (arena_) HParameterValue(parameter_index++, Primitive::kPrimNot, true); 164 entry_block_->AddInstruction(parameter); 165 HLocal* local = GetLocalAt(locals_index++); 166 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter)); 167 number_of_parameters--; 168 } 169 170 uint32_t pos = 1; 171 for (int i = 0; i < number_of_parameters; i++) { 172 HParameterValue* parameter = 173 new (arena_) HParameterValue(parameter_index++, Primitive::GetType(shorty[pos++])); 174 entry_block_->AddInstruction(parameter); 175 HLocal* local = GetLocalAt(locals_index++); 176 // Store the parameter value in the local that the dex code will use 177 // to reference that parameter. 178 entry_block_->AddInstruction(new (arena_) HStoreLocal(local, parameter)); 179 bool is_wide = (parameter->GetType() == Primitive::kPrimLong) 180 || (parameter->GetType() == Primitive::kPrimDouble); 181 if (is_wide) { 182 i++; 183 locals_index++; 184 parameter_index++; 185 } 186 } 187} 188 189template<typename T> 190void HGraphBuilder::If_22t(const Instruction& instruction, uint32_t dex_pc) { 191 int32_t target_offset = instruction.GetTargetOffset(); 192 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset); 193 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits()); 194 DCHECK(branch_target != nullptr); 195 DCHECK(fallthrough_target != nullptr); 196 PotentiallyAddSuspendCheck(branch_target, dex_pc); 197 HInstruction* first = LoadLocal(instruction.VRegA(), Primitive::kPrimInt); 198 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); 199 T* comparison = new (arena_) T(first, second); 200 current_block_->AddInstruction(comparison); 201 HInstruction* ifinst = new (arena_) HIf(comparison); 202 current_block_->AddInstruction(ifinst); 203 current_block_->AddSuccessor(branch_target); 204 current_block_->AddSuccessor(fallthrough_target); 205 current_block_ = nullptr; 206} 207 208template<typename T> 209void HGraphBuilder::If_21t(const Instruction& instruction, uint32_t dex_pc) { 210 int32_t target_offset = instruction.GetTargetOffset(); 211 HBasicBlock* branch_target = FindBlockStartingAt(dex_pc + target_offset); 212 HBasicBlock* fallthrough_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits()); 213 DCHECK(branch_target != nullptr); 214 DCHECK(fallthrough_target != nullptr); 215 PotentiallyAddSuspendCheck(branch_target, dex_pc); 216 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt); 217 T* comparison = new (arena_) T(value, graph_->GetIntConstant(0)); 218 current_block_->AddInstruction(comparison); 219 HInstruction* ifinst = new (arena_) HIf(comparison); 220 current_block_->AddInstruction(ifinst); 221 current_block_->AddSuccessor(branch_target); 222 current_block_->AddSuccessor(fallthrough_target); 223 current_block_ = nullptr; 224} 225 226void HGraphBuilder::MaybeRecordStat(MethodCompilationStat compilation_stat) { 227 if (compilation_stats_ != nullptr) { 228 compilation_stats_->RecordStat(compilation_stat); 229 } 230} 231 232bool HGraphBuilder::SkipCompilation(const DexFile::CodeItem& code_item, 233 size_t number_of_branches) { 234 const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions(); 235 CompilerOptions::CompilerFilter compiler_filter = compiler_options.GetCompilerFilter(); 236 if (compiler_filter == CompilerOptions::kEverything) { 237 return false; 238 } 239 240 if (compiler_options.IsHugeMethod(code_item.insns_size_in_code_units_)) { 241 VLOG(compiler) << "Skip compilation of huge method " 242 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_) 243 << ": " << code_item.insns_size_in_code_units_ << " code units"; 244 MaybeRecordStat(MethodCompilationStat::kNotCompiledHugeMethod); 245 return true; 246 } 247 248 // If it's large and contains no branches, it's likely to be machine generated initialization. 249 if (compiler_options.IsLargeMethod(code_item.insns_size_in_code_units_) 250 && (number_of_branches == 0)) { 251 VLOG(compiler) << "Skip compilation of large method with no branch " 252 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_) 253 << ": " << code_item.insns_size_in_code_units_ << " code units"; 254 MaybeRecordStat(MethodCompilationStat::kNotCompiledLargeMethodNoBranches); 255 return true; 256 } 257 258 return false; 259} 260 261bool HGraphBuilder::BuildGraph(const DexFile::CodeItem& code_item) { 262 DCHECK(graph_->GetBlocks().IsEmpty()); 263 264 const uint16_t* code_ptr = code_item.insns_; 265 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_; 266 code_start_ = code_ptr; 267 268 // Setup the graph with the entry block and exit block. 269 entry_block_ = new (arena_) HBasicBlock(graph_, 0); 270 graph_->AddBlock(entry_block_); 271 exit_block_ = new (arena_) HBasicBlock(graph_, kNoDexPc); 272 graph_->SetEntryBlock(entry_block_); 273 graph_->SetExitBlock(exit_block_); 274 275 InitializeLocals(code_item.registers_size_); 276 graph_->SetMaximumNumberOfOutVRegs(code_item.outs_size_); 277 278 // Compute the number of dex instructions, blocks, and branches. We will 279 // check these values against limits given to the compiler. 280 size_t number_of_branches = 0; 281 282 // To avoid splitting blocks, we compute ahead of time the instructions that 283 // start a new block, and create these blocks. 284 ComputeBranchTargets(code_ptr, code_end, &number_of_branches); 285 286 // Note that the compiler driver is null when unit testing. 287 if ((compiler_driver_ != nullptr) && SkipCompilation(code_item, number_of_branches)) { 288 return false; 289 } 290 291 // Also create blocks for catch handlers. 292 if (code_item.tries_size_ != 0) { 293 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(code_item, 0); 294 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); 295 for (uint32_t idx = 0; idx < handlers_size; ++idx) { 296 CatchHandlerIterator iterator(handlers_ptr); 297 for (; iterator.HasNext(); iterator.Next()) { 298 uint32_t address = iterator.GetHandlerAddress(); 299 HBasicBlock* block = FindBlockStartingAt(address); 300 if (block == nullptr) { 301 block = new (arena_) HBasicBlock(graph_, address); 302 branch_targets_.Put(address, block); 303 } 304 block->SetIsCatchBlock(); 305 } 306 handlers_ptr = iterator.EndDataPointer(); 307 } 308 } 309 310 InitializeParameters(code_item.ins_size_); 311 312 size_t dex_pc = 0; 313 while (code_ptr < code_end) { 314 // Update the current block if dex_pc starts a new block. 315 MaybeUpdateCurrentBlock(dex_pc); 316 const Instruction& instruction = *Instruction::At(code_ptr); 317 if (!AnalyzeDexInstruction(instruction, dex_pc)) { 318 return false; 319 } 320 dex_pc += instruction.SizeInCodeUnits(); 321 code_ptr += instruction.SizeInCodeUnits(); 322 } 323 324 // Add the exit block at the end to give it the highest id. 325 graph_->AddBlock(exit_block_); 326 exit_block_->AddInstruction(new (arena_) HExit()); 327 // Add the suspend check to the entry block. 328 entry_block_->AddInstruction(new (arena_) HSuspendCheck(0)); 329 entry_block_->AddInstruction(new (arena_) HGoto()); 330 331 return true; 332} 333 334void HGraphBuilder::MaybeUpdateCurrentBlock(size_t index) { 335 HBasicBlock* block = FindBlockStartingAt(index); 336 if (block == nullptr) { 337 return; 338 } 339 340 if (current_block_ != nullptr) { 341 // Branching instructions clear current_block, so we know 342 // the last instruction of the current block is not a branching 343 // instruction. We add an unconditional goto to the found block. 344 current_block_->AddInstruction(new (arena_) HGoto()); 345 current_block_->AddSuccessor(block); 346 } 347 graph_->AddBlock(block); 348 current_block_ = block; 349} 350 351void HGraphBuilder::ComputeBranchTargets(const uint16_t* code_ptr, 352 const uint16_t* code_end, 353 size_t* number_of_branches) { 354 branch_targets_.SetSize(code_end - code_ptr); 355 356 // Create the first block for the dex instructions, single successor of the entry block. 357 HBasicBlock* block = new (arena_) HBasicBlock(graph_, 0); 358 branch_targets_.Put(0, block); 359 entry_block_->AddSuccessor(block); 360 361 // Iterate over all instructions and find branching instructions. Create blocks for 362 // the locations these instructions branch to. 363 uint32_t dex_pc = 0; 364 while (code_ptr < code_end) { 365 const Instruction& instruction = *Instruction::At(code_ptr); 366 if (instruction.IsBranch()) { 367 (*number_of_branches)++; 368 int32_t target = instruction.GetTargetOffset() + dex_pc; 369 // Create a block for the target instruction. 370 if (FindBlockStartingAt(target) == nullptr) { 371 block = new (arena_) HBasicBlock(graph_, target); 372 branch_targets_.Put(target, block); 373 } 374 dex_pc += instruction.SizeInCodeUnits(); 375 code_ptr += instruction.SizeInCodeUnits(); 376 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) { 377 block = new (arena_) HBasicBlock(graph_, dex_pc); 378 branch_targets_.Put(dex_pc, block); 379 } 380 } else if (instruction.IsSwitch()) { 381 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH); 382 383 uint16_t num_entries = table.GetNumEntries(); 384 385 // In a packed-switch, the entry at index 0 is the starting key. In a sparse-switch, the 386 // entry at index 0 is the first key, and values are after *all* keys. 387 size_t offset = table.GetFirstValueIndex(); 388 389 // Use a larger loop counter type to avoid overflow issues. 390 for (size_t i = 0; i < num_entries; ++i) { 391 // The target of the case. 392 uint32_t target = dex_pc + table.GetEntryAt(i + offset); 393 if (FindBlockStartingAt(target) == nullptr) { 394 block = new (arena_) HBasicBlock(graph_, target); 395 branch_targets_.Put(target, block); 396 } 397 398 // The next case gets its own block. 399 if (i < num_entries) { 400 block = new (arena_) HBasicBlock(graph_, target); 401 branch_targets_.Put(table.GetDexPcForIndex(i), block); 402 } 403 } 404 405 // Fall-through. Add a block if there is more code afterwards. 406 dex_pc += instruction.SizeInCodeUnits(); 407 code_ptr += instruction.SizeInCodeUnits(); 408 if ((code_ptr < code_end) && (FindBlockStartingAt(dex_pc) == nullptr)) { 409 block = new (arena_) HBasicBlock(graph_, dex_pc); 410 branch_targets_.Put(dex_pc, block); 411 } 412 } else { 413 code_ptr += instruction.SizeInCodeUnits(); 414 dex_pc += instruction.SizeInCodeUnits(); 415 } 416 } 417} 418 419HBasicBlock* HGraphBuilder::FindBlockStartingAt(int32_t index) const { 420 DCHECK_GE(index, 0); 421 return branch_targets_.Get(index); 422} 423 424template<typename T> 425void HGraphBuilder::Unop_12x(const Instruction& instruction, Primitive::Type type) { 426 HInstruction* first = LoadLocal(instruction.VRegB(), type); 427 current_block_->AddInstruction(new (arena_) T(type, first)); 428 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); 429} 430 431void HGraphBuilder::Conversion_12x(const Instruction& instruction, 432 Primitive::Type input_type, 433 Primitive::Type result_type, 434 uint32_t dex_pc) { 435 HInstruction* first = LoadLocal(instruction.VRegB(), input_type); 436 current_block_->AddInstruction(new (arena_) HTypeConversion(result_type, first, dex_pc)); 437 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); 438} 439 440template<typename T> 441void HGraphBuilder::Binop_23x(const Instruction& instruction, Primitive::Type type) { 442 HInstruction* first = LoadLocal(instruction.VRegB(), type); 443 HInstruction* second = LoadLocal(instruction.VRegC(), type); 444 current_block_->AddInstruction(new (arena_) T(type, first, second)); 445 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); 446} 447 448template<typename T> 449void HGraphBuilder::Binop_23x(const Instruction& instruction, 450 Primitive::Type type, 451 uint32_t dex_pc) { 452 HInstruction* first = LoadLocal(instruction.VRegB(), type); 453 HInstruction* second = LoadLocal(instruction.VRegC(), type); 454 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc)); 455 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); 456} 457 458template<typename T> 459void HGraphBuilder::Binop_23x_shift(const Instruction& instruction, 460 Primitive::Type type) { 461 HInstruction* first = LoadLocal(instruction.VRegB(), type); 462 HInstruction* second = LoadLocal(instruction.VRegC(), Primitive::kPrimInt); 463 current_block_->AddInstruction(new (arena_) T(type, first, second)); 464 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); 465} 466 467void HGraphBuilder::Binop_23x_cmp(const Instruction& instruction, 468 Primitive::Type type, 469 HCompare::Bias bias) { 470 HInstruction* first = LoadLocal(instruction.VRegB(), type); 471 HInstruction* second = LoadLocal(instruction.VRegC(), type); 472 current_block_->AddInstruction(new (arena_) HCompare(type, first, second, bias)); 473 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); 474} 475 476template<typename T> 477void HGraphBuilder::Binop_12x(const Instruction& instruction, Primitive::Type type) { 478 HInstruction* first = LoadLocal(instruction.VRegA(), type); 479 HInstruction* second = LoadLocal(instruction.VRegB(), type); 480 current_block_->AddInstruction(new (arena_) T(type, first, second)); 481 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); 482} 483 484template<typename T> 485void HGraphBuilder::Binop_12x_shift(const Instruction& instruction, Primitive::Type type) { 486 HInstruction* first = LoadLocal(instruction.VRegA(), type); 487 HInstruction* second = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); 488 current_block_->AddInstruction(new (arena_) T(type, first, second)); 489 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); 490} 491 492template<typename T> 493void HGraphBuilder::Binop_12x(const Instruction& instruction, 494 Primitive::Type type, 495 uint32_t dex_pc) { 496 HInstruction* first = LoadLocal(instruction.VRegA(), type); 497 HInstruction* second = LoadLocal(instruction.VRegB(), type); 498 current_block_->AddInstruction(new (arena_) T(type, first, second, dex_pc)); 499 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); 500} 501 502template<typename T> 503void HGraphBuilder::Binop_22s(const Instruction& instruction, bool reverse) { 504 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); 505 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22s()); 506 if (reverse) { 507 std::swap(first, second); 508 } 509 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second)); 510 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); 511} 512 513template<typename T> 514void HGraphBuilder::Binop_22b(const Instruction& instruction, bool reverse) { 515 HInstruction* first = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); 516 HInstruction* second = graph_->GetIntConstant(instruction.VRegC_22b()); 517 if (reverse) { 518 std::swap(first, second); 519 } 520 current_block_->AddInstruction(new (arena_) T(Primitive::kPrimInt, first, second)); 521 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); 522} 523 524static bool RequiresConstructorBarrier(const DexCompilationUnit* cu, const CompilerDriver& driver) { 525 // dex compilation unit is null only when unit testing. 526 if (cu == nullptr) { 527 return false; 528 } 529 530 Thread* self = Thread::Current(); 531 return cu->IsConstructor() 532 && driver.RequiresConstructorBarrier(self, cu->GetDexFile(), cu->GetClassDefIndex()); 533} 534 535void HGraphBuilder::BuildReturn(const Instruction& instruction, Primitive::Type type) { 536 if (type == Primitive::kPrimVoid) { 537 // Note that we might insert redundant barriers when inlining `super` calls. 538 // TODO: add a data flow analysis to get rid of duplicate barriers. 539 if (RequiresConstructorBarrier(dex_compilation_unit_, *compiler_driver_)) { 540 current_block_->AddInstruction(new (arena_) HMemoryBarrier(kStoreStore)); 541 } 542 current_block_->AddInstruction(new (arena_) HReturnVoid()); 543 } else { 544 HInstruction* value = LoadLocal(instruction.VRegA(), type); 545 current_block_->AddInstruction(new (arena_) HReturn(value)); 546 } 547 current_block_->AddSuccessor(exit_block_); 548 current_block_ = nullptr; 549} 550 551bool HGraphBuilder::BuildInvoke(const Instruction& instruction, 552 uint32_t dex_pc, 553 uint32_t method_idx, 554 uint32_t number_of_vreg_arguments, 555 bool is_range, 556 uint32_t* args, 557 uint32_t register_index) { 558 Instruction::Code opcode = instruction.Opcode(); 559 InvokeType invoke_type; 560 switch (opcode) { 561 case Instruction::INVOKE_STATIC: 562 case Instruction::INVOKE_STATIC_RANGE: 563 invoke_type = kStatic; 564 break; 565 case Instruction::INVOKE_DIRECT: 566 case Instruction::INVOKE_DIRECT_RANGE: 567 invoke_type = kDirect; 568 break; 569 case Instruction::INVOKE_VIRTUAL: 570 case Instruction::INVOKE_VIRTUAL_RANGE: 571 invoke_type = kVirtual; 572 break; 573 case Instruction::INVOKE_INTERFACE: 574 case Instruction::INVOKE_INTERFACE_RANGE: 575 invoke_type = kInterface; 576 break; 577 case Instruction::INVOKE_SUPER_RANGE: 578 case Instruction::INVOKE_SUPER: 579 invoke_type = kSuper; 580 break; 581 default: 582 LOG(FATAL) << "Unexpected invoke op: " << opcode; 583 return false; 584 } 585 586 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx); 587 const DexFile::ProtoId& proto_id = dex_file_->GetProtoId(method_id.proto_idx_); 588 const char* descriptor = dex_file_->StringDataByIdx(proto_id.shorty_idx_); 589 Primitive::Type return_type = Primitive::GetType(descriptor[0]); 590 bool is_instance_call = invoke_type != kStatic; 591 size_t number_of_arguments = strlen(descriptor) - (is_instance_call ? 0 : 1); 592 593 MethodReference target_method(dex_file_, method_idx); 594 uintptr_t direct_code; 595 uintptr_t direct_method; 596 int table_index; 597 InvokeType optimized_invoke_type = invoke_type; 598 599 if (!compiler_driver_->ComputeInvokeInfo(dex_compilation_unit_, dex_pc, true, true, 600 &optimized_invoke_type, &target_method, &table_index, 601 &direct_code, &direct_method)) { 602 VLOG(compiler) << "Did not compile " << PrettyMethod(method_idx, *dex_file_) 603 << " because a method call could not be resolved"; 604 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod); 605 return false; 606 } 607 DCHECK(optimized_invoke_type != kSuper); 608 609 // By default, consider that the called method implicitly requires 610 // an initialization check of its declaring method. 611 HInvokeStaticOrDirect::ClinitCheckRequirement clinit_check_requirement = 612 HInvokeStaticOrDirect::ClinitCheckRequirement::kImplicit; 613 // Potential class initialization check, in the case of a static method call. 614 HClinitCheck* clinit_check = nullptr; 615 616 HInvoke* invoke = nullptr; 617 618 if (optimized_invoke_type == kVirtual) { 619 invoke = new (arena_) HInvokeVirtual( 620 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index); 621 } else if (optimized_invoke_type == kInterface) { 622 invoke = new (arena_) HInvokeInterface( 623 arena_, number_of_arguments, return_type, dex_pc, method_idx, table_index); 624 } else { 625 DCHECK(optimized_invoke_type == kDirect || optimized_invoke_type == kStatic); 626 // Sharpening to kDirect only works if we compile PIC. 627 DCHECK((optimized_invoke_type == invoke_type) || (optimized_invoke_type != kDirect) 628 || compiler_driver_->GetCompilerOptions().GetCompilePic()); 629 bool is_recursive = 630 (target_method.dex_method_index == dex_compilation_unit_->GetDexMethodIndex()); 631 DCHECK(!is_recursive || (target_method.dex_file == dex_compilation_unit_->GetDexFile())); 632 633 if (optimized_invoke_type == kStatic) { 634 ScopedObjectAccess soa(Thread::Current()); 635 StackHandleScope<4> hs(soa.Self()); 636 Handle<mirror::DexCache> dex_cache(hs.NewHandle( 637 dex_compilation_unit_->GetClassLinker()->FindDexCache( 638 *dex_compilation_unit_->GetDexFile()))); 639 Handle<mirror::ClassLoader> class_loader(hs.NewHandle( 640 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader()))); 641 mirror::ArtMethod* resolved_method = compiler_driver_->ResolveMethod( 642 soa, dex_cache, class_loader, dex_compilation_unit_, method_idx, 643 optimized_invoke_type); 644 645 if (resolved_method == nullptr) { 646 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedMethod); 647 return false; 648 } 649 650 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile(); 651 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle( 652 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file))); 653 Handle<mirror::Class> referrer_class(hs.NewHandle(GetOutermostCompilingClass())); 654 655 // The index at which the method's class is stored in the DexCache's type array. 656 uint32_t storage_index = DexFile::kDexNoIndex; 657 bool is_referrer_class = (resolved_method->GetDeclaringClass() == referrer_class.Get()); 658 if (is_referrer_class) { 659 storage_index = referrer_class->GetDexTypeIndex(); 660 } else if (outer_dex_cache.Get() == dex_cache.Get()) { 661 // Get `storage_index` from IsClassOfStaticMethodAvailableToReferrer. 662 compiler_driver_->IsClassOfStaticMethodAvailableToReferrer(outer_dex_cache.Get(), 663 referrer_class.Get(), 664 resolved_method, 665 method_idx, 666 &storage_index); 667 } 668 669 if (referrer_class.Get()->IsSubClass(resolved_method->GetDeclaringClass())) { 670 // If the referrer class is the declaring class or a subclass 671 // of the declaring class, no class initialization is needed 672 // before the static method call. 673 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone; 674 } else if (storage_index != DexFile::kDexNoIndex) { 675 // If the method's class type index is available, check 676 // whether we should add an explicit class initialization 677 // check for its declaring class before the static method call. 678 679 // TODO: find out why this check is needed. 680 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache( 681 *outer_compilation_unit_->GetDexFile(), storage_index); 682 bool is_initialized = 683 resolved_method->GetDeclaringClass()->IsInitialized() && is_in_dex_cache; 684 685 if (is_initialized) { 686 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kNone; 687 } else { 688 clinit_check_requirement = HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit; 689 HLoadClass* load_class = 690 new (arena_) HLoadClass(storage_index, is_referrer_class, dex_pc); 691 current_block_->AddInstruction(load_class); 692 clinit_check = new (arena_) HClinitCheck(load_class, dex_pc); 693 current_block_->AddInstruction(clinit_check); 694 ++number_of_arguments; 695 } 696 } 697 } 698 699 invoke = new (arena_) HInvokeStaticOrDirect( 700 arena_, number_of_arguments, return_type, dex_pc, target_method.dex_method_index, 701 is_recursive, invoke_type, optimized_invoke_type, clinit_check_requirement); 702 } 703 704 size_t start_index = 0; 705 Temporaries temps(graph_); 706 if (is_instance_call) { 707 HInstruction* arg = LoadLocal(is_range ? register_index : args[0], Primitive::kPrimNot); 708 HNullCheck* null_check = new (arena_) HNullCheck(arg, dex_pc); 709 current_block_->AddInstruction(null_check); 710 temps.Add(null_check); 711 invoke->SetArgumentAt(0, null_check); 712 start_index = 1; 713 } 714 715 uint32_t descriptor_index = 1; 716 uint32_t argument_index = start_index; 717 for (size_t i = start_index; i < number_of_vreg_arguments; i++, argument_index++) { 718 Primitive::Type type = Primitive::GetType(descriptor[descriptor_index++]); 719 bool is_wide = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble); 720 if (!is_range && is_wide && args[i] + 1 != args[i + 1]) { 721 LOG(WARNING) << "Non sequential register pair in " << dex_compilation_unit_->GetSymbol() 722 << " at " << dex_pc; 723 // We do not implement non sequential register pair. 724 MaybeRecordStat(MethodCompilationStat::kNotCompiledNonSequentialRegPair); 725 return false; 726 } 727 HInstruction* arg = LoadLocal(is_range ? register_index + i : args[i], type); 728 invoke->SetArgumentAt(argument_index, arg); 729 if (is_wide) { 730 i++; 731 } 732 } 733 734 if (clinit_check_requirement == HInvokeStaticOrDirect::ClinitCheckRequirement::kExplicit) { 735 // Add the class initialization check as last input of `invoke`. 736 DCHECK(clinit_check != nullptr); 737 invoke->SetArgumentAt(argument_index++, clinit_check); 738 } 739 740 DCHECK_EQ(argument_index, number_of_arguments); 741 current_block_->AddInstruction(invoke); 742 latest_result_ = invoke; 743 return true; 744} 745 746bool HGraphBuilder::BuildInstanceFieldAccess(const Instruction& instruction, 747 uint32_t dex_pc, 748 bool is_put) { 749 uint32_t source_or_dest_reg = instruction.VRegA_22c(); 750 uint32_t obj_reg = instruction.VRegB_22c(); 751 uint16_t field_index = instruction.VRegC_22c(); 752 753 ScopedObjectAccess soa(Thread::Current()); 754 ArtField* resolved_field = 755 compiler_driver_->ComputeInstanceFieldInfo(field_index, dex_compilation_unit_, is_put, soa); 756 757 if (resolved_field == nullptr) { 758 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField); 759 return false; 760 } 761 762 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType(); 763 764 HInstruction* object = LoadLocal(obj_reg, Primitive::kPrimNot); 765 current_block_->AddInstruction(new (arena_) HNullCheck(object, dex_pc)); 766 if (is_put) { 767 Temporaries temps(graph_); 768 HInstruction* null_check = current_block_->GetLastInstruction(); 769 // We need one temporary for the null check. 770 temps.Add(null_check); 771 HInstruction* value = LoadLocal(source_or_dest_reg, field_type); 772 current_block_->AddInstruction(new (arena_) HInstanceFieldSet( 773 null_check, 774 value, 775 field_type, 776 resolved_field->GetOffset(), 777 resolved_field->IsVolatile())); 778 } else { 779 current_block_->AddInstruction(new (arena_) HInstanceFieldGet( 780 current_block_->GetLastInstruction(), 781 field_type, 782 resolved_field->GetOffset(), 783 resolved_field->IsVolatile())); 784 785 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction()); 786 } 787 return true; 788} 789 790mirror::Class* HGraphBuilder::GetOutermostCompilingClass() const { 791 ScopedObjectAccess soa(Thread::Current()); 792 StackHandleScope<2> hs(soa.Self()); 793 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile(); 794 Handle<mirror::ClassLoader> class_loader(hs.NewHandle( 795 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader()))); 796 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle( 797 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file))); 798 799 return compiler_driver_->ResolveCompilingMethodsClass( 800 soa, outer_dex_cache, class_loader, outer_compilation_unit_); 801} 802 803bool HGraphBuilder::IsOutermostCompilingClass(uint16_t type_index) const { 804 ScopedObjectAccess soa(Thread::Current()); 805 StackHandleScope<4> hs(soa.Self()); 806 Handle<mirror::DexCache> dex_cache(hs.NewHandle( 807 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile()))); 808 Handle<mirror::ClassLoader> class_loader(hs.NewHandle( 809 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader()))); 810 Handle<mirror::Class> cls(hs.NewHandle(compiler_driver_->ResolveClass( 811 soa, dex_cache, class_loader, type_index, dex_compilation_unit_))); 812 Handle<mirror::Class> compiling_class(hs.NewHandle(GetOutermostCompilingClass())); 813 814 return compiling_class.Get() == cls.Get(); 815} 816 817bool HGraphBuilder::BuildStaticFieldAccess(const Instruction& instruction, 818 uint32_t dex_pc, 819 bool is_put) { 820 uint32_t source_or_dest_reg = instruction.VRegA_21c(); 821 uint16_t field_index = instruction.VRegB_21c(); 822 823 ScopedObjectAccess soa(Thread::Current()); 824 StackHandleScope<4> hs(soa.Self()); 825 Handle<mirror::DexCache> dex_cache(hs.NewHandle( 826 dex_compilation_unit_->GetClassLinker()->FindDexCache(*dex_compilation_unit_->GetDexFile()))); 827 Handle<mirror::ClassLoader> class_loader(hs.NewHandle( 828 soa.Decode<mirror::ClassLoader*>(dex_compilation_unit_->GetClassLoader()))); 829 ArtField* resolved_field = compiler_driver_->ResolveField( 830 soa, dex_cache, class_loader, dex_compilation_unit_, field_index, true); 831 832 if (resolved_field == nullptr) { 833 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnresolvedField); 834 return false; 835 } 836 837 const DexFile& outer_dex_file = *outer_compilation_unit_->GetDexFile(); 838 Handle<mirror::DexCache> outer_dex_cache(hs.NewHandle( 839 outer_compilation_unit_->GetClassLinker()->FindDexCache(outer_dex_file))); 840 Handle<mirror::Class> referrer_class(hs.NewHandle(GetOutermostCompilingClass())); 841 842 // The index at which the field's class is stored in the DexCache's type array. 843 uint32_t storage_index; 844 bool is_referrer_class = (referrer_class.Get() == resolved_field->GetDeclaringClass()); 845 if (is_referrer_class) { 846 storage_index = referrer_class->GetDexTypeIndex(); 847 } else if (outer_dex_cache.Get() != dex_cache.Get()) { 848 // The compiler driver cannot currently understand multiple dex caches involved. Just bailout. 849 return false; 850 } else { 851 std::pair<bool, bool> pair = compiler_driver_->IsFastStaticField( 852 outer_dex_cache.Get(), 853 referrer_class.Get(), 854 resolved_field, 855 field_index, 856 &storage_index); 857 bool can_easily_access = is_put ? pair.second : pair.first; 858 if (!can_easily_access) { 859 return false; 860 } 861 } 862 863 // TODO: find out why this check is needed. 864 bool is_in_dex_cache = compiler_driver_->CanAssumeTypeIsPresentInDexCache( 865 *outer_compilation_unit_->GetDexFile(), storage_index); 866 bool is_initialized = resolved_field->GetDeclaringClass()->IsInitialized() && is_in_dex_cache; 867 868 HLoadClass* constant = new (arena_) HLoadClass(storage_index, is_referrer_class, dex_pc); 869 current_block_->AddInstruction(constant); 870 871 HInstruction* cls = constant; 872 if (!is_initialized && !is_referrer_class) { 873 cls = new (arena_) HClinitCheck(constant, dex_pc); 874 current_block_->AddInstruction(cls); 875 } 876 877 Primitive::Type field_type = resolved_field->GetTypeAsPrimitiveType(); 878 if (is_put) { 879 // We need to keep the class alive before loading the value. 880 Temporaries temps(graph_); 881 temps.Add(cls); 882 HInstruction* value = LoadLocal(source_or_dest_reg, field_type); 883 DCHECK_EQ(value->GetType(), field_type); 884 current_block_->AddInstruction( 885 new (arena_) HStaticFieldSet(cls, value, field_type, resolved_field->GetOffset(), 886 resolved_field->IsVolatile())); 887 } else { 888 current_block_->AddInstruction( 889 new (arena_) HStaticFieldGet(cls, field_type, resolved_field->GetOffset(), 890 resolved_field->IsVolatile())); 891 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction()); 892 } 893 return true; 894} 895 896void HGraphBuilder::BuildCheckedDivRem(uint16_t out_vreg, 897 uint16_t first_vreg, 898 int64_t second_vreg_or_constant, 899 uint32_t dex_pc, 900 Primitive::Type type, 901 bool second_is_constant, 902 bool isDiv) { 903 DCHECK(type == Primitive::kPrimInt || type == Primitive::kPrimLong); 904 905 HInstruction* first = LoadLocal(first_vreg, type); 906 HInstruction* second = nullptr; 907 if (second_is_constant) { 908 if (type == Primitive::kPrimInt) { 909 second = graph_->GetIntConstant(second_vreg_or_constant); 910 } else { 911 second = graph_->GetLongConstant(second_vreg_or_constant); 912 } 913 } else { 914 second = LoadLocal(second_vreg_or_constant, type); 915 } 916 917 if (!second_is_constant 918 || (type == Primitive::kPrimInt && second->AsIntConstant()->GetValue() == 0) 919 || (type == Primitive::kPrimLong && second->AsLongConstant()->GetValue() == 0)) { 920 second = new (arena_) HDivZeroCheck(second, dex_pc); 921 Temporaries temps(graph_); 922 current_block_->AddInstruction(second); 923 temps.Add(current_block_->GetLastInstruction()); 924 } 925 926 if (isDiv) { 927 current_block_->AddInstruction(new (arena_) HDiv(type, first, second, dex_pc)); 928 } else { 929 current_block_->AddInstruction(new (arena_) HRem(type, first, second, dex_pc)); 930 } 931 UpdateLocal(out_vreg, current_block_->GetLastInstruction()); 932} 933 934void HGraphBuilder::BuildArrayAccess(const Instruction& instruction, 935 uint32_t dex_pc, 936 bool is_put, 937 Primitive::Type anticipated_type) { 938 uint8_t source_or_dest_reg = instruction.VRegA_23x(); 939 uint8_t array_reg = instruction.VRegB_23x(); 940 uint8_t index_reg = instruction.VRegC_23x(); 941 942 // We need one temporary for the null check, one for the index, and one for the length. 943 Temporaries temps(graph_); 944 945 HInstruction* object = LoadLocal(array_reg, Primitive::kPrimNot); 946 object = new (arena_) HNullCheck(object, dex_pc); 947 current_block_->AddInstruction(object); 948 temps.Add(object); 949 950 HInstruction* length = new (arena_) HArrayLength(object); 951 current_block_->AddInstruction(length); 952 temps.Add(length); 953 HInstruction* index = LoadLocal(index_reg, Primitive::kPrimInt); 954 index = new (arena_) HBoundsCheck(index, length, dex_pc); 955 current_block_->AddInstruction(index); 956 temps.Add(index); 957 if (is_put) { 958 HInstruction* value = LoadLocal(source_or_dest_reg, anticipated_type); 959 // TODO: Insert a type check node if the type is Object. 960 current_block_->AddInstruction(new (arena_) HArraySet( 961 object, index, value, anticipated_type, dex_pc)); 962 } else { 963 current_block_->AddInstruction(new (arena_) HArrayGet(object, index, anticipated_type)); 964 UpdateLocal(source_or_dest_reg, current_block_->GetLastInstruction()); 965 } 966 graph_->SetHasArrayAccesses(true); 967} 968 969void HGraphBuilder::BuildFilledNewArray(uint32_t dex_pc, 970 uint32_t type_index, 971 uint32_t number_of_vreg_arguments, 972 bool is_range, 973 uint32_t* args, 974 uint32_t register_index) { 975 HInstruction* length = graph_->GetIntConstant(number_of_vreg_arguments); 976 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index) 977 ? kQuickAllocArrayWithAccessCheck 978 : kQuickAllocArray; 979 HInstruction* object = new (arena_) HNewArray(length, dex_pc, type_index, entrypoint); 980 current_block_->AddInstruction(object); 981 982 const char* descriptor = dex_file_->StringByTypeIdx(type_index); 983 DCHECK_EQ(descriptor[0], '[') << descriptor; 984 char primitive = descriptor[1]; 985 DCHECK(primitive == 'I' 986 || primitive == 'L' 987 || primitive == '[') << descriptor; 988 bool is_reference_array = (primitive == 'L') || (primitive == '['); 989 Primitive::Type type = is_reference_array ? Primitive::kPrimNot : Primitive::kPrimInt; 990 991 Temporaries temps(graph_); 992 temps.Add(object); 993 for (size_t i = 0; i < number_of_vreg_arguments; ++i) { 994 HInstruction* value = LoadLocal(is_range ? register_index + i : args[i], type); 995 HInstruction* index = graph_->GetIntConstant(i); 996 current_block_->AddInstruction( 997 new (arena_) HArraySet(object, index, value, type, dex_pc)); 998 } 999 latest_result_ = object; 1000} 1001 1002template <typename T> 1003void HGraphBuilder::BuildFillArrayData(HInstruction* object, 1004 const T* data, 1005 uint32_t element_count, 1006 Primitive::Type anticipated_type, 1007 uint32_t dex_pc) { 1008 for (uint32_t i = 0; i < element_count; ++i) { 1009 HInstruction* index = graph_->GetIntConstant(i); 1010 HInstruction* value = graph_->GetIntConstant(data[i]); 1011 current_block_->AddInstruction(new (arena_) HArraySet( 1012 object, index, value, anticipated_type, dex_pc)); 1013 } 1014} 1015 1016void HGraphBuilder::BuildFillArrayData(const Instruction& instruction, uint32_t dex_pc) { 1017 Temporaries temps(graph_); 1018 HInstruction* array = LoadLocal(instruction.VRegA_31t(), Primitive::kPrimNot); 1019 HNullCheck* null_check = new (arena_) HNullCheck(array, dex_pc); 1020 current_block_->AddInstruction(null_check); 1021 temps.Add(null_check); 1022 1023 HInstruction* length = new (arena_) HArrayLength(null_check); 1024 current_block_->AddInstruction(length); 1025 1026 int32_t payload_offset = instruction.VRegB_31t() + dex_pc; 1027 const Instruction::ArrayDataPayload* payload = 1028 reinterpret_cast<const Instruction::ArrayDataPayload*>(code_start_ + payload_offset); 1029 const uint8_t* data = payload->data; 1030 uint32_t element_count = payload->element_count; 1031 1032 // Implementation of this DEX instruction seems to be that the bounds check is 1033 // done before doing any stores. 1034 HInstruction* last_index = graph_->GetIntConstant(payload->element_count - 1); 1035 current_block_->AddInstruction(new (arena_) HBoundsCheck(last_index, length, dex_pc)); 1036 1037 switch (payload->element_width) { 1038 case 1: 1039 BuildFillArrayData(null_check, 1040 reinterpret_cast<const int8_t*>(data), 1041 element_count, 1042 Primitive::kPrimByte, 1043 dex_pc); 1044 break; 1045 case 2: 1046 BuildFillArrayData(null_check, 1047 reinterpret_cast<const int16_t*>(data), 1048 element_count, 1049 Primitive::kPrimShort, 1050 dex_pc); 1051 break; 1052 case 4: 1053 BuildFillArrayData(null_check, 1054 reinterpret_cast<const int32_t*>(data), 1055 element_count, 1056 Primitive::kPrimInt, 1057 dex_pc); 1058 break; 1059 case 8: 1060 BuildFillWideArrayData(null_check, 1061 reinterpret_cast<const int64_t*>(data), 1062 element_count, 1063 dex_pc); 1064 break; 1065 default: 1066 LOG(FATAL) << "Unknown element width for " << payload->element_width; 1067 } 1068} 1069 1070void HGraphBuilder::BuildFillWideArrayData(HInstruction* object, 1071 const int64_t* data, 1072 uint32_t element_count, 1073 uint32_t dex_pc) { 1074 for (uint32_t i = 0; i < element_count; ++i) { 1075 HInstruction* index = graph_->GetIntConstant(i); 1076 HInstruction* value = graph_->GetLongConstant(data[i]); 1077 current_block_->AddInstruction(new (arena_) HArraySet( 1078 object, index, value, Primitive::kPrimLong, dex_pc)); 1079 } 1080} 1081 1082bool HGraphBuilder::BuildTypeCheck(const Instruction& instruction, 1083 uint8_t destination, 1084 uint8_t reference, 1085 uint16_t type_index, 1086 uint32_t dex_pc) { 1087 bool type_known_final; 1088 bool type_known_abstract; 1089 // `CanAccessTypeWithoutChecks` will tell whether the method being 1090 // built is trying to access its own class, so that the generated 1091 // code can optimize for this case. However, the optimization does not 1092 // work for inlining, so we use `IsOutermostCompilingClass` instead. 1093 bool dont_use_is_referrers_class; 1094 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks( 1095 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index, 1096 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class); 1097 if (!can_access) { 1098 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType); 1099 return false; 1100 } 1101 HInstruction* object = LoadLocal(reference, Primitive::kPrimNot); 1102 HLoadClass* cls = new (arena_) HLoadClass( 1103 type_index, IsOutermostCompilingClass(type_index), dex_pc); 1104 current_block_->AddInstruction(cls); 1105 // The class needs a temporary before being used by the type check. 1106 Temporaries temps(graph_); 1107 temps.Add(cls); 1108 if (instruction.Opcode() == Instruction::INSTANCE_OF) { 1109 current_block_->AddInstruction( 1110 new (arena_) HInstanceOf(object, cls, type_known_final, dex_pc)); 1111 UpdateLocal(destination, current_block_->GetLastInstruction()); 1112 } else { 1113 DCHECK_EQ(instruction.Opcode(), Instruction::CHECK_CAST); 1114 current_block_->AddInstruction( 1115 new (arena_) HCheckCast(object, cls, type_known_final, dex_pc)); 1116 } 1117 return true; 1118} 1119 1120bool HGraphBuilder::NeedsAccessCheck(uint32_t type_index) const { 1121 return !compiler_driver_->CanAccessInstantiableTypeWithoutChecks( 1122 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index); 1123} 1124 1125void HGraphBuilder::BuildPackedSwitch(const Instruction& instruction, uint32_t dex_pc) { 1126 SwitchTable table(instruction, dex_pc, false); 1127 1128 // Value to test against. 1129 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt); 1130 1131 uint16_t num_entries = table.GetNumEntries(); 1132 // There should be at least one entry here. 1133 DCHECK_GT(num_entries, 0U); 1134 1135 // Chained cmp-and-branch, starting from starting_key. 1136 int32_t starting_key = table.GetEntryAt(0); 1137 1138 for (size_t i = 1; i <= num_entries; i++) { 1139 BuildSwitchCaseHelper(instruction, i, i == num_entries, table, value, starting_key + i - 1, 1140 table.GetEntryAt(i), dex_pc); 1141 } 1142} 1143 1144void HGraphBuilder::BuildSparseSwitch(const Instruction& instruction, uint32_t dex_pc) { 1145 SwitchTable table(instruction, dex_pc, true); 1146 1147 // Value to test against. 1148 HInstruction* value = LoadLocal(instruction.VRegA(), Primitive::kPrimInt); 1149 1150 uint16_t num_entries = table.GetNumEntries(); 1151 1152 for (size_t i = 0; i < num_entries; i++) { 1153 BuildSwitchCaseHelper(instruction, i, i == static_cast<size_t>(num_entries) - 1, table, value, 1154 table.GetEntryAt(i), table.GetEntryAt(i + num_entries), dex_pc); 1155 } 1156} 1157 1158void HGraphBuilder::BuildSwitchCaseHelper(const Instruction& instruction, size_t index, 1159 bool is_last_case, const SwitchTable& table, 1160 HInstruction* value, int32_t case_value_int, 1161 int32_t target_offset, uint32_t dex_pc) { 1162 HBasicBlock* case_target = FindBlockStartingAt(dex_pc + target_offset); 1163 DCHECK(case_target != nullptr); 1164 PotentiallyAddSuspendCheck(case_target, dex_pc); 1165 1166 // The current case's value. 1167 HInstruction* this_case_value = graph_->GetIntConstant(case_value_int); 1168 1169 // Compare value and this_case_value. 1170 HEqual* comparison = new (arena_) HEqual(value, this_case_value); 1171 current_block_->AddInstruction(comparison); 1172 HInstruction* ifinst = new (arena_) HIf(comparison); 1173 current_block_->AddInstruction(ifinst); 1174 1175 // Case hit: use the target offset to determine where to go. 1176 current_block_->AddSuccessor(case_target); 1177 1178 // Case miss: go to the next case (or default fall-through). 1179 // When there is a next case, we use the block stored with the table offset representing this 1180 // case (that is where we registered them in ComputeBranchTargets). 1181 // When there is no next case, we use the following instruction. 1182 // TODO: Find a good way to peel the last iteration to avoid conditional, but still have re-use. 1183 if (!is_last_case) { 1184 HBasicBlock* next_case_target = FindBlockStartingAt(table.GetDexPcForIndex(index)); 1185 DCHECK(next_case_target != nullptr); 1186 current_block_->AddSuccessor(next_case_target); 1187 1188 // Need to manually add the block, as there is no dex-pc transition for the cases. 1189 graph_->AddBlock(next_case_target); 1190 1191 current_block_ = next_case_target; 1192 } else { 1193 HBasicBlock* default_target = FindBlockStartingAt(dex_pc + instruction.SizeInCodeUnits()); 1194 DCHECK(default_target != nullptr); 1195 current_block_->AddSuccessor(default_target); 1196 current_block_ = nullptr; 1197 } 1198} 1199 1200void HGraphBuilder::PotentiallyAddSuspendCheck(HBasicBlock* target, uint32_t dex_pc) { 1201 int32_t target_offset = target->GetDexPc() - dex_pc; 1202 if (target_offset <= 0) { 1203 // DX generates back edges to the first encountered return. We can save 1204 // time of later passes by not adding redundant suspend checks. 1205 HInstruction* last_in_target = target->GetLastInstruction(); 1206 if (last_in_target != nullptr && 1207 (last_in_target->IsReturn() || last_in_target->IsReturnVoid())) { 1208 return; 1209 } 1210 1211 // Add a suspend check to backward branches which may potentially loop. We 1212 // can remove them after we recognize loops in the graph. 1213 current_block_->AddInstruction(new (arena_) HSuspendCheck(dex_pc)); 1214 } 1215} 1216 1217bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32_t dex_pc) { 1218 if (current_block_ == nullptr) { 1219 return true; // Dead code 1220 } 1221 1222 switch (instruction.Opcode()) { 1223 case Instruction::CONST_4: { 1224 int32_t register_index = instruction.VRegA(); 1225 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_11n()); 1226 UpdateLocal(register_index, constant); 1227 break; 1228 } 1229 1230 case Instruction::CONST_16: { 1231 int32_t register_index = instruction.VRegA(); 1232 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21s()); 1233 UpdateLocal(register_index, constant); 1234 break; 1235 } 1236 1237 case Instruction::CONST: { 1238 int32_t register_index = instruction.VRegA(); 1239 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_31i()); 1240 UpdateLocal(register_index, constant); 1241 break; 1242 } 1243 1244 case Instruction::CONST_HIGH16: { 1245 int32_t register_index = instruction.VRegA(); 1246 HIntConstant* constant = graph_->GetIntConstant(instruction.VRegB_21h() << 16); 1247 UpdateLocal(register_index, constant); 1248 break; 1249 } 1250 1251 case Instruction::CONST_WIDE_16: { 1252 int32_t register_index = instruction.VRegA(); 1253 // Get 16 bits of constant value, sign extended to 64 bits. 1254 int64_t value = instruction.VRegB_21s(); 1255 value <<= 48; 1256 value >>= 48; 1257 HLongConstant* constant = graph_->GetLongConstant(value); 1258 UpdateLocal(register_index, constant); 1259 break; 1260 } 1261 1262 case Instruction::CONST_WIDE_32: { 1263 int32_t register_index = instruction.VRegA(); 1264 // Get 32 bits of constant value, sign extended to 64 bits. 1265 int64_t value = instruction.VRegB_31i(); 1266 value <<= 32; 1267 value >>= 32; 1268 HLongConstant* constant = graph_->GetLongConstant(value); 1269 UpdateLocal(register_index, constant); 1270 break; 1271 } 1272 1273 case Instruction::CONST_WIDE: { 1274 int32_t register_index = instruction.VRegA(); 1275 HLongConstant* constant = graph_->GetLongConstant(instruction.VRegB_51l()); 1276 UpdateLocal(register_index, constant); 1277 break; 1278 } 1279 1280 case Instruction::CONST_WIDE_HIGH16: { 1281 int32_t register_index = instruction.VRegA(); 1282 int64_t value = static_cast<int64_t>(instruction.VRegB_21h()) << 48; 1283 HLongConstant* constant = graph_->GetLongConstant(value); 1284 UpdateLocal(register_index, constant); 1285 break; 1286 } 1287 1288 // Note that the SSA building will refine the types. 1289 case Instruction::MOVE: 1290 case Instruction::MOVE_FROM16: 1291 case Instruction::MOVE_16: { 1292 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimInt); 1293 UpdateLocal(instruction.VRegA(), value); 1294 break; 1295 } 1296 1297 // Note that the SSA building will refine the types. 1298 case Instruction::MOVE_WIDE: 1299 case Instruction::MOVE_WIDE_FROM16: 1300 case Instruction::MOVE_WIDE_16: { 1301 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimLong); 1302 UpdateLocal(instruction.VRegA(), value); 1303 break; 1304 } 1305 1306 case Instruction::MOVE_OBJECT: 1307 case Instruction::MOVE_OBJECT_16: 1308 case Instruction::MOVE_OBJECT_FROM16: { 1309 HInstruction* value = LoadLocal(instruction.VRegB(), Primitive::kPrimNot); 1310 UpdateLocal(instruction.VRegA(), value); 1311 break; 1312 } 1313 1314 case Instruction::RETURN_VOID: { 1315 BuildReturn(instruction, Primitive::kPrimVoid); 1316 break; 1317 } 1318 1319#define IF_XX(comparison, cond) \ 1320 case Instruction::IF_##cond: If_22t<comparison>(instruction, dex_pc); break; \ 1321 case Instruction::IF_##cond##Z: If_21t<comparison>(instruction, dex_pc); break 1322 1323 IF_XX(HEqual, EQ); 1324 IF_XX(HNotEqual, NE); 1325 IF_XX(HLessThan, LT); 1326 IF_XX(HLessThanOrEqual, LE); 1327 IF_XX(HGreaterThan, GT); 1328 IF_XX(HGreaterThanOrEqual, GE); 1329 1330 case Instruction::GOTO: 1331 case Instruction::GOTO_16: 1332 case Instruction::GOTO_32: { 1333 int32_t offset = instruction.GetTargetOffset(); 1334 HBasicBlock* target = FindBlockStartingAt(offset + dex_pc); 1335 DCHECK(target != nullptr); 1336 PotentiallyAddSuspendCheck(target, dex_pc); 1337 current_block_->AddInstruction(new (arena_) HGoto()); 1338 current_block_->AddSuccessor(target); 1339 current_block_ = nullptr; 1340 break; 1341 } 1342 1343 case Instruction::RETURN: { 1344 DCHECK_NE(return_type_, Primitive::kPrimNot); 1345 DCHECK_NE(return_type_, Primitive::kPrimLong); 1346 DCHECK_NE(return_type_, Primitive::kPrimDouble); 1347 BuildReturn(instruction, return_type_); 1348 break; 1349 } 1350 1351 case Instruction::RETURN_OBJECT: { 1352 DCHECK(return_type_ == Primitive::kPrimNot); 1353 BuildReturn(instruction, return_type_); 1354 break; 1355 } 1356 1357 case Instruction::RETURN_WIDE: { 1358 DCHECK(return_type_ == Primitive::kPrimDouble || return_type_ == Primitive::kPrimLong); 1359 BuildReturn(instruction, return_type_); 1360 break; 1361 } 1362 1363 case Instruction::INVOKE_DIRECT: 1364 case Instruction::INVOKE_INTERFACE: 1365 case Instruction::INVOKE_STATIC: 1366 case Instruction::INVOKE_SUPER: 1367 case Instruction::INVOKE_VIRTUAL: { 1368 uint32_t method_idx = instruction.VRegB_35c(); 1369 uint32_t number_of_vreg_arguments = instruction.VRegA_35c(); 1370 uint32_t args[5]; 1371 instruction.GetVarArgs(args); 1372 if (!BuildInvoke(instruction, dex_pc, method_idx, 1373 number_of_vreg_arguments, false, args, -1)) { 1374 return false; 1375 } 1376 break; 1377 } 1378 1379 case Instruction::INVOKE_DIRECT_RANGE: 1380 case Instruction::INVOKE_INTERFACE_RANGE: 1381 case Instruction::INVOKE_STATIC_RANGE: 1382 case Instruction::INVOKE_SUPER_RANGE: 1383 case Instruction::INVOKE_VIRTUAL_RANGE: { 1384 uint32_t method_idx = instruction.VRegB_3rc(); 1385 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc(); 1386 uint32_t register_index = instruction.VRegC(); 1387 if (!BuildInvoke(instruction, dex_pc, method_idx, 1388 number_of_vreg_arguments, true, nullptr, register_index)) { 1389 return false; 1390 } 1391 break; 1392 } 1393 1394 case Instruction::NEG_INT: { 1395 Unop_12x<HNeg>(instruction, Primitive::kPrimInt); 1396 break; 1397 } 1398 1399 case Instruction::NEG_LONG: { 1400 Unop_12x<HNeg>(instruction, Primitive::kPrimLong); 1401 break; 1402 } 1403 1404 case Instruction::NEG_FLOAT: { 1405 Unop_12x<HNeg>(instruction, Primitive::kPrimFloat); 1406 break; 1407 } 1408 1409 case Instruction::NEG_DOUBLE: { 1410 Unop_12x<HNeg>(instruction, Primitive::kPrimDouble); 1411 break; 1412 } 1413 1414 case Instruction::NOT_INT: { 1415 Unop_12x<HNot>(instruction, Primitive::kPrimInt); 1416 break; 1417 } 1418 1419 case Instruction::NOT_LONG: { 1420 Unop_12x<HNot>(instruction, Primitive::kPrimLong); 1421 break; 1422 } 1423 1424 case Instruction::INT_TO_LONG: { 1425 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimLong, dex_pc); 1426 break; 1427 } 1428 1429 case Instruction::INT_TO_FLOAT: { 1430 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimFloat, dex_pc); 1431 break; 1432 } 1433 1434 case Instruction::INT_TO_DOUBLE: { 1435 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimDouble, dex_pc); 1436 break; 1437 } 1438 1439 case Instruction::LONG_TO_INT: { 1440 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimInt, dex_pc); 1441 break; 1442 } 1443 1444 case Instruction::LONG_TO_FLOAT: { 1445 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimFloat, dex_pc); 1446 break; 1447 } 1448 1449 case Instruction::LONG_TO_DOUBLE: { 1450 Conversion_12x(instruction, Primitive::kPrimLong, Primitive::kPrimDouble, dex_pc); 1451 break; 1452 } 1453 1454 case Instruction::FLOAT_TO_INT: { 1455 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimInt, dex_pc); 1456 break; 1457 } 1458 1459 case Instruction::FLOAT_TO_LONG: { 1460 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimLong, dex_pc); 1461 break; 1462 } 1463 1464 case Instruction::FLOAT_TO_DOUBLE: { 1465 Conversion_12x(instruction, Primitive::kPrimFloat, Primitive::kPrimDouble, dex_pc); 1466 break; 1467 } 1468 1469 case Instruction::DOUBLE_TO_INT: { 1470 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimInt, dex_pc); 1471 break; 1472 } 1473 1474 case Instruction::DOUBLE_TO_LONG: { 1475 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimLong, dex_pc); 1476 break; 1477 } 1478 1479 case Instruction::DOUBLE_TO_FLOAT: { 1480 Conversion_12x(instruction, Primitive::kPrimDouble, Primitive::kPrimFloat, dex_pc); 1481 break; 1482 } 1483 1484 case Instruction::INT_TO_BYTE: { 1485 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte, dex_pc); 1486 break; 1487 } 1488 1489 case Instruction::INT_TO_SHORT: { 1490 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort, dex_pc); 1491 break; 1492 } 1493 1494 case Instruction::INT_TO_CHAR: { 1495 Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar, dex_pc); 1496 break; 1497 } 1498 1499 case Instruction::ADD_INT: { 1500 Binop_23x<HAdd>(instruction, Primitive::kPrimInt); 1501 break; 1502 } 1503 1504 case Instruction::ADD_LONG: { 1505 Binop_23x<HAdd>(instruction, Primitive::kPrimLong); 1506 break; 1507 } 1508 1509 case Instruction::ADD_DOUBLE: { 1510 Binop_23x<HAdd>(instruction, Primitive::kPrimDouble); 1511 break; 1512 } 1513 1514 case Instruction::ADD_FLOAT: { 1515 Binop_23x<HAdd>(instruction, Primitive::kPrimFloat); 1516 break; 1517 } 1518 1519 case Instruction::SUB_INT: { 1520 Binop_23x<HSub>(instruction, Primitive::kPrimInt); 1521 break; 1522 } 1523 1524 case Instruction::SUB_LONG: { 1525 Binop_23x<HSub>(instruction, Primitive::kPrimLong); 1526 break; 1527 } 1528 1529 case Instruction::SUB_FLOAT: { 1530 Binop_23x<HSub>(instruction, Primitive::kPrimFloat); 1531 break; 1532 } 1533 1534 case Instruction::SUB_DOUBLE: { 1535 Binop_23x<HSub>(instruction, Primitive::kPrimDouble); 1536 break; 1537 } 1538 1539 case Instruction::ADD_INT_2ADDR: { 1540 Binop_12x<HAdd>(instruction, Primitive::kPrimInt); 1541 break; 1542 } 1543 1544 case Instruction::MUL_INT: { 1545 Binop_23x<HMul>(instruction, Primitive::kPrimInt); 1546 break; 1547 } 1548 1549 case Instruction::MUL_LONG: { 1550 Binop_23x<HMul>(instruction, Primitive::kPrimLong); 1551 break; 1552 } 1553 1554 case Instruction::MUL_FLOAT: { 1555 Binop_23x<HMul>(instruction, Primitive::kPrimFloat); 1556 break; 1557 } 1558 1559 case Instruction::MUL_DOUBLE: { 1560 Binop_23x<HMul>(instruction, Primitive::kPrimDouble); 1561 break; 1562 } 1563 1564 case Instruction::DIV_INT: { 1565 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(), 1566 dex_pc, Primitive::kPrimInt, false, true); 1567 break; 1568 } 1569 1570 case Instruction::DIV_LONG: { 1571 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(), 1572 dex_pc, Primitive::kPrimLong, false, true); 1573 break; 1574 } 1575 1576 case Instruction::DIV_FLOAT: { 1577 Binop_23x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc); 1578 break; 1579 } 1580 1581 case Instruction::DIV_DOUBLE: { 1582 Binop_23x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc); 1583 break; 1584 } 1585 1586 case Instruction::REM_INT: { 1587 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(), 1588 dex_pc, Primitive::kPrimInt, false, false); 1589 break; 1590 } 1591 1592 case Instruction::REM_LONG: { 1593 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(), 1594 dex_pc, Primitive::kPrimLong, false, false); 1595 break; 1596 } 1597 1598 case Instruction::REM_FLOAT: { 1599 Binop_23x<HRem>(instruction, Primitive::kPrimFloat, dex_pc); 1600 break; 1601 } 1602 1603 case Instruction::REM_DOUBLE: { 1604 Binop_23x<HRem>(instruction, Primitive::kPrimDouble, dex_pc); 1605 break; 1606 } 1607 1608 case Instruction::AND_INT: { 1609 Binop_23x<HAnd>(instruction, Primitive::kPrimInt); 1610 break; 1611 } 1612 1613 case Instruction::AND_LONG: { 1614 Binop_23x<HAnd>(instruction, Primitive::kPrimLong); 1615 break; 1616 } 1617 1618 case Instruction::SHL_INT: { 1619 Binop_23x_shift<HShl>(instruction, Primitive::kPrimInt); 1620 break; 1621 } 1622 1623 case Instruction::SHL_LONG: { 1624 Binop_23x_shift<HShl>(instruction, Primitive::kPrimLong); 1625 break; 1626 } 1627 1628 case Instruction::SHR_INT: { 1629 Binop_23x_shift<HShr>(instruction, Primitive::kPrimInt); 1630 break; 1631 } 1632 1633 case Instruction::SHR_LONG: { 1634 Binop_23x_shift<HShr>(instruction, Primitive::kPrimLong); 1635 break; 1636 } 1637 1638 case Instruction::USHR_INT: { 1639 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimInt); 1640 break; 1641 } 1642 1643 case Instruction::USHR_LONG: { 1644 Binop_23x_shift<HUShr>(instruction, Primitive::kPrimLong); 1645 break; 1646 } 1647 1648 case Instruction::OR_INT: { 1649 Binop_23x<HOr>(instruction, Primitive::kPrimInt); 1650 break; 1651 } 1652 1653 case Instruction::OR_LONG: { 1654 Binop_23x<HOr>(instruction, Primitive::kPrimLong); 1655 break; 1656 } 1657 1658 case Instruction::XOR_INT: { 1659 Binop_23x<HXor>(instruction, Primitive::kPrimInt); 1660 break; 1661 } 1662 1663 case Instruction::XOR_LONG: { 1664 Binop_23x<HXor>(instruction, Primitive::kPrimLong); 1665 break; 1666 } 1667 1668 case Instruction::ADD_LONG_2ADDR: { 1669 Binop_12x<HAdd>(instruction, Primitive::kPrimLong); 1670 break; 1671 } 1672 1673 case Instruction::ADD_DOUBLE_2ADDR: { 1674 Binop_12x<HAdd>(instruction, Primitive::kPrimDouble); 1675 break; 1676 } 1677 1678 case Instruction::ADD_FLOAT_2ADDR: { 1679 Binop_12x<HAdd>(instruction, Primitive::kPrimFloat); 1680 break; 1681 } 1682 1683 case Instruction::SUB_INT_2ADDR: { 1684 Binop_12x<HSub>(instruction, Primitive::kPrimInt); 1685 break; 1686 } 1687 1688 case Instruction::SUB_LONG_2ADDR: { 1689 Binop_12x<HSub>(instruction, Primitive::kPrimLong); 1690 break; 1691 } 1692 1693 case Instruction::SUB_FLOAT_2ADDR: { 1694 Binop_12x<HSub>(instruction, Primitive::kPrimFloat); 1695 break; 1696 } 1697 1698 case Instruction::SUB_DOUBLE_2ADDR: { 1699 Binop_12x<HSub>(instruction, Primitive::kPrimDouble); 1700 break; 1701 } 1702 1703 case Instruction::MUL_INT_2ADDR: { 1704 Binop_12x<HMul>(instruction, Primitive::kPrimInt); 1705 break; 1706 } 1707 1708 case Instruction::MUL_LONG_2ADDR: { 1709 Binop_12x<HMul>(instruction, Primitive::kPrimLong); 1710 break; 1711 } 1712 1713 case Instruction::MUL_FLOAT_2ADDR: { 1714 Binop_12x<HMul>(instruction, Primitive::kPrimFloat); 1715 break; 1716 } 1717 1718 case Instruction::MUL_DOUBLE_2ADDR: { 1719 Binop_12x<HMul>(instruction, Primitive::kPrimDouble); 1720 break; 1721 } 1722 1723 case Instruction::DIV_INT_2ADDR: { 1724 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(), 1725 dex_pc, Primitive::kPrimInt, false, true); 1726 break; 1727 } 1728 1729 case Instruction::DIV_LONG_2ADDR: { 1730 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(), 1731 dex_pc, Primitive::kPrimLong, false, true); 1732 break; 1733 } 1734 1735 case Instruction::REM_INT_2ADDR: { 1736 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(), 1737 dex_pc, Primitive::kPrimInt, false, false); 1738 break; 1739 } 1740 1741 case Instruction::REM_LONG_2ADDR: { 1742 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegA(), instruction.VRegB(), 1743 dex_pc, Primitive::kPrimLong, false, false); 1744 break; 1745 } 1746 1747 case Instruction::REM_FLOAT_2ADDR: { 1748 Binop_12x<HRem>(instruction, Primitive::kPrimFloat, dex_pc); 1749 break; 1750 } 1751 1752 case Instruction::REM_DOUBLE_2ADDR: { 1753 Binop_12x<HRem>(instruction, Primitive::kPrimDouble, dex_pc); 1754 break; 1755 } 1756 1757 case Instruction::SHL_INT_2ADDR: { 1758 Binop_12x_shift<HShl>(instruction, Primitive::kPrimInt); 1759 break; 1760 } 1761 1762 case Instruction::SHL_LONG_2ADDR: { 1763 Binop_12x_shift<HShl>(instruction, Primitive::kPrimLong); 1764 break; 1765 } 1766 1767 case Instruction::SHR_INT_2ADDR: { 1768 Binop_12x_shift<HShr>(instruction, Primitive::kPrimInt); 1769 break; 1770 } 1771 1772 case Instruction::SHR_LONG_2ADDR: { 1773 Binop_12x_shift<HShr>(instruction, Primitive::kPrimLong); 1774 break; 1775 } 1776 1777 case Instruction::USHR_INT_2ADDR: { 1778 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimInt); 1779 break; 1780 } 1781 1782 case Instruction::USHR_LONG_2ADDR: { 1783 Binop_12x_shift<HUShr>(instruction, Primitive::kPrimLong); 1784 break; 1785 } 1786 1787 case Instruction::DIV_FLOAT_2ADDR: { 1788 Binop_12x<HDiv>(instruction, Primitive::kPrimFloat, dex_pc); 1789 break; 1790 } 1791 1792 case Instruction::DIV_DOUBLE_2ADDR: { 1793 Binop_12x<HDiv>(instruction, Primitive::kPrimDouble, dex_pc); 1794 break; 1795 } 1796 1797 case Instruction::AND_INT_2ADDR: { 1798 Binop_12x<HAnd>(instruction, Primitive::kPrimInt); 1799 break; 1800 } 1801 1802 case Instruction::AND_LONG_2ADDR: { 1803 Binop_12x<HAnd>(instruction, Primitive::kPrimLong); 1804 break; 1805 } 1806 1807 case Instruction::OR_INT_2ADDR: { 1808 Binop_12x<HOr>(instruction, Primitive::kPrimInt); 1809 break; 1810 } 1811 1812 case Instruction::OR_LONG_2ADDR: { 1813 Binop_12x<HOr>(instruction, Primitive::kPrimLong); 1814 break; 1815 } 1816 1817 case Instruction::XOR_INT_2ADDR: { 1818 Binop_12x<HXor>(instruction, Primitive::kPrimInt); 1819 break; 1820 } 1821 1822 case Instruction::XOR_LONG_2ADDR: { 1823 Binop_12x<HXor>(instruction, Primitive::kPrimLong); 1824 break; 1825 } 1826 1827 case Instruction::ADD_INT_LIT16: { 1828 Binop_22s<HAdd>(instruction, false); 1829 break; 1830 } 1831 1832 case Instruction::AND_INT_LIT16: { 1833 Binop_22s<HAnd>(instruction, false); 1834 break; 1835 } 1836 1837 case Instruction::OR_INT_LIT16: { 1838 Binop_22s<HOr>(instruction, false); 1839 break; 1840 } 1841 1842 case Instruction::XOR_INT_LIT16: { 1843 Binop_22s<HXor>(instruction, false); 1844 break; 1845 } 1846 1847 case Instruction::RSUB_INT: { 1848 Binop_22s<HSub>(instruction, true); 1849 break; 1850 } 1851 1852 case Instruction::MUL_INT_LIT16: { 1853 Binop_22s<HMul>(instruction, false); 1854 break; 1855 } 1856 1857 case Instruction::ADD_INT_LIT8: { 1858 Binop_22b<HAdd>(instruction, false); 1859 break; 1860 } 1861 1862 case Instruction::AND_INT_LIT8: { 1863 Binop_22b<HAnd>(instruction, false); 1864 break; 1865 } 1866 1867 case Instruction::OR_INT_LIT8: { 1868 Binop_22b<HOr>(instruction, false); 1869 break; 1870 } 1871 1872 case Instruction::XOR_INT_LIT8: { 1873 Binop_22b<HXor>(instruction, false); 1874 break; 1875 } 1876 1877 case Instruction::RSUB_INT_LIT8: { 1878 Binop_22b<HSub>(instruction, true); 1879 break; 1880 } 1881 1882 case Instruction::MUL_INT_LIT8: { 1883 Binop_22b<HMul>(instruction, false); 1884 break; 1885 } 1886 1887 case Instruction::DIV_INT_LIT16: 1888 case Instruction::DIV_INT_LIT8: { 1889 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(), 1890 dex_pc, Primitive::kPrimInt, true, true); 1891 break; 1892 } 1893 1894 case Instruction::REM_INT_LIT16: 1895 case Instruction::REM_INT_LIT8: { 1896 BuildCheckedDivRem(instruction.VRegA(), instruction.VRegB(), instruction.VRegC(), 1897 dex_pc, Primitive::kPrimInt, true, false); 1898 break; 1899 } 1900 1901 case Instruction::SHL_INT_LIT8: { 1902 Binop_22b<HShl>(instruction, false); 1903 break; 1904 } 1905 1906 case Instruction::SHR_INT_LIT8: { 1907 Binop_22b<HShr>(instruction, false); 1908 break; 1909 } 1910 1911 case Instruction::USHR_INT_LIT8: { 1912 Binop_22b<HUShr>(instruction, false); 1913 break; 1914 } 1915 1916 case Instruction::NEW_INSTANCE: { 1917 uint16_t type_index = instruction.VRegB_21c(); 1918 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index) 1919 ? kQuickAllocObjectWithAccessCheck 1920 : kQuickAllocObject; 1921 1922 current_block_->AddInstruction(new (arena_) HNewInstance(dex_pc, type_index, entrypoint)); 1923 UpdateLocal(instruction.VRegA(), current_block_->GetLastInstruction()); 1924 break; 1925 } 1926 1927 case Instruction::NEW_ARRAY: { 1928 uint16_t type_index = instruction.VRegC_22c(); 1929 HInstruction* length = LoadLocal(instruction.VRegB_22c(), Primitive::kPrimInt); 1930 QuickEntrypointEnum entrypoint = NeedsAccessCheck(type_index) 1931 ? kQuickAllocArrayWithAccessCheck 1932 : kQuickAllocArray; 1933 current_block_->AddInstruction( 1934 new (arena_) HNewArray(length, dex_pc, type_index, entrypoint)); 1935 UpdateLocal(instruction.VRegA_22c(), current_block_->GetLastInstruction()); 1936 break; 1937 } 1938 1939 case Instruction::FILLED_NEW_ARRAY: { 1940 uint32_t number_of_vreg_arguments = instruction.VRegA_35c(); 1941 uint32_t type_index = instruction.VRegB_35c(); 1942 uint32_t args[5]; 1943 instruction.GetVarArgs(args); 1944 BuildFilledNewArray(dex_pc, type_index, number_of_vreg_arguments, false, args, 0); 1945 break; 1946 } 1947 1948 case Instruction::FILLED_NEW_ARRAY_RANGE: { 1949 uint32_t number_of_vreg_arguments = instruction.VRegA_3rc(); 1950 uint32_t type_index = instruction.VRegB_3rc(); 1951 uint32_t register_index = instruction.VRegC_3rc(); 1952 BuildFilledNewArray( 1953 dex_pc, type_index, number_of_vreg_arguments, true, nullptr, register_index); 1954 break; 1955 } 1956 1957 case Instruction::FILL_ARRAY_DATA: { 1958 BuildFillArrayData(instruction, dex_pc); 1959 break; 1960 } 1961 1962 case Instruction::MOVE_RESULT: 1963 case Instruction::MOVE_RESULT_WIDE: 1964 case Instruction::MOVE_RESULT_OBJECT: 1965 UpdateLocal(instruction.VRegA(), latest_result_); 1966 latest_result_ = nullptr; 1967 break; 1968 1969 case Instruction::CMP_LONG: { 1970 Binop_23x_cmp(instruction, Primitive::kPrimLong, HCompare::kNoBias); 1971 break; 1972 } 1973 1974 case Instruction::CMPG_FLOAT: { 1975 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kGtBias); 1976 break; 1977 } 1978 1979 case Instruction::CMPG_DOUBLE: { 1980 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kGtBias); 1981 break; 1982 } 1983 1984 case Instruction::CMPL_FLOAT: { 1985 Binop_23x_cmp(instruction, Primitive::kPrimFloat, HCompare::kLtBias); 1986 break; 1987 } 1988 1989 case Instruction::CMPL_DOUBLE: { 1990 Binop_23x_cmp(instruction, Primitive::kPrimDouble, HCompare::kLtBias); 1991 break; 1992 } 1993 1994 case Instruction::NOP: 1995 break; 1996 1997 case Instruction::IGET: 1998 case Instruction::IGET_WIDE: 1999 case Instruction::IGET_OBJECT: 2000 case Instruction::IGET_BOOLEAN: 2001 case Instruction::IGET_BYTE: 2002 case Instruction::IGET_CHAR: 2003 case Instruction::IGET_SHORT: { 2004 if (!BuildInstanceFieldAccess(instruction, dex_pc, false)) { 2005 return false; 2006 } 2007 break; 2008 } 2009 2010 case Instruction::IPUT: 2011 case Instruction::IPUT_WIDE: 2012 case Instruction::IPUT_OBJECT: 2013 case Instruction::IPUT_BOOLEAN: 2014 case Instruction::IPUT_BYTE: 2015 case Instruction::IPUT_CHAR: 2016 case Instruction::IPUT_SHORT: { 2017 if (!BuildInstanceFieldAccess(instruction, dex_pc, true)) { 2018 return false; 2019 } 2020 break; 2021 } 2022 2023 case Instruction::SGET: 2024 case Instruction::SGET_WIDE: 2025 case Instruction::SGET_OBJECT: 2026 case Instruction::SGET_BOOLEAN: 2027 case Instruction::SGET_BYTE: 2028 case Instruction::SGET_CHAR: 2029 case Instruction::SGET_SHORT: { 2030 if (!BuildStaticFieldAccess(instruction, dex_pc, false)) { 2031 return false; 2032 } 2033 break; 2034 } 2035 2036 case Instruction::SPUT: 2037 case Instruction::SPUT_WIDE: 2038 case Instruction::SPUT_OBJECT: 2039 case Instruction::SPUT_BOOLEAN: 2040 case Instruction::SPUT_BYTE: 2041 case Instruction::SPUT_CHAR: 2042 case Instruction::SPUT_SHORT: { 2043 if (!BuildStaticFieldAccess(instruction, dex_pc, true)) { 2044 return false; 2045 } 2046 break; 2047 } 2048 2049#define ARRAY_XX(kind, anticipated_type) \ 2050 case Instruction::AGET##kind: { \ 2051 BuildArrayAccess(instruction, dex_pc, false, anticipated_type); \ 2052 break; \ 2053 } \ 2054 case Instruction::APUT##kind: { \ 2055 BuildArrayAccess(instruction, dex_pc, true, anticipated_type); \ 2056 break; \ 2057 } 2058 2059 ARRAY_XX(, Primitive::kPrimInt); 2060 ARRAY_XX(_WIDE, Primitive::kPrimLong); 2061 ARRAY_XX(_OBJECT, Primitive::kPrimNot); 2062 ARRAY_XX(_BOOLEAN, Primitive::kPrimBoolean); 2063 ARRAY_XX(_BYTE, Primitive::kPrimByte); 2064 ARRAY_XX(_CHAR, Primitive::kPrimChar); 2065 ARRAY_XX(_SHORT, Primitive::kPrimShort); 2066 2067 case Instruction::ARRAY_LENGTH: { 2068 HInstruction* object = LoadLocal(instruction.VRegB_12x(), Primitive::kPrimNot); 2069 // No need for a temporary for the null check, it is the only input of the following 2070 // instruction. 2071 object = new (arena_) HNullCheck(object, dex_pc); 2072 current_block_->AddInstruction(object); 2073 current_block_->AddInstruction(new (arena_) HArrayLength(object)); 2074 UpdateLocal(instruction.VRegA_12x(), current_block_->GetLastInstruction()); 2075 break; 2076 } 2077 2078 case Instruction::CONST_STRING: { 2079 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_21c(), dex_pc)); 2080 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction()); 2081 break; 2082 } 2083 2084 case Instruction::CONST_STRING_JUMBO: { 2085 current_block_->AddInstruction(new (arena_) HLoadString(instruction.VRegB_31c(), dex_pc)); 2086 UpdateLocal(instruction.VRegA_31c(), current_block_->GetLastInstruction()); 2087 break; 2088 } 2089 2090 case Instruction::CONST_CLASS: { 2091 uint16_t type_index = instruction.VRegB_21c(); 2092 bool type_known_final; 2093 bool type_known_abstract; 2094 bool dont_use_is_referrers_class; 2095 // `CanAccessTypeWithoutChecks` will tell whether the method being 2096 // built is trying to access its own class, so that the generated 2097 // code can optimize for this case. However, the optimization does not 2098 // work for inlining, so we use `IsOutermostCompilingClass` instead. 2099 bool can_access = compiler_driver_->CanAccessTypeWithoutChecks( 2100 dex_compilation_unit_->GetDexMethodIndex(), *dex_file_, type_index, 2101 &type_known_final, &type_known_abstract, &dont_use_is_referrers_class); 2102 if (!can_access) { 2103 MaybeRecordStat(MethodCompilationStat::kNotCompiledCantAccesType); 2104 return false; 2105 } 2106 current_block_->AddInstruction( 2107 new (arena_) HLoadClass(type_index, IsOutermostCompilingClass(type_index), dex_pc)); 2108 UpdateLocal(instruction.VRegA_21c(), current_block_->GetLastInstruction()); 2109 break; 2110 } 2111 2112 case Instruction::MOVE_EXCEPTION: { 2113 current_block_->AddInstruction(new (arena_) HLoadException()); 2114 UpdateLocal(instruction.VRegA_11x(), current_block_->GetLastInstruction()); 2115 break; 2116 } 2117 2118 case Instruction::THROW: { 2119 HInstruction* exception = LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot); 2120 current_block_->AddInstruction(new (arena_) HThrow(exception, dex_pc)); 2121 // A throw instruction must branch to the exit block. 2122 current_block_->AddSuccessor(exit_block_); 2123 // We finished building this block. Set the current block to null to avoid 2124 // adding dead instructions to it. 2125 current_block_ = nullptr; 2126 break; 2127 } 2128 2129 case Instruction::INSTANCE_OF: { 2130 uint8_t destination = instruction.VRegA_22c(); 2131 uint8_t reference = instruction.VRegB_22c(); 2132 uint16_t type_index = instruction.VRegC_22c(); 2133 if (!BuildTypeCheck(instruction, destination, reference, type_index, dex_pc)) { 2134 return false; 2135 } 2136 break; 2137 } 2138 2139 case Instruction::CHECK_CAST: { 2140 uint8_t reference = instruction.VRegA_21c(); 2141 uint16_t type_index = instruction.VRegB_21c(); 2142 if (!BuildTypeCheck(instruction, -1, reference, type_index, dex_pc)) { 2143 return false; 2144 } 2145 break; 2146 } 2147 2148 case Instruction::MONITOR_ENTER: { 2149 current_block_->AddInstruction(new (arena_) HMonitorOperation( 2150 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot), 2151 HMonitorOperation::kEnter, 2152 dex_pc)); 2153 break; 2154 } 2155 2156 case Instruction::MONITOR_EXIT: { 2157 current_block_->AddInstruction(new (arena_) HMonitorOperation( 2158 LoadLocal(instruction.VRegA_11x(), Primitive::kPrimNot), 2159 HMonitorOperation::kExit, 2160 dex_pc)); 2161 break; 2162 } 2163 2164 case Instruction::PACKED_SWITCH: { 2165 BuildPackedSwitch(instruction, dex_pc); 2166 break; 2167 } 2168 2169 case Instruction::SPARSE_SWITCH: { 2170 BuildSparseSwitch(instruction, dex_pc); 2171 break; 2172 } 2173 2174 default: 2175 VLOG(compiler) << "Did not compile " 2176 << PrettyMethod(dex_compilation_unit_->GetDexMethodIndex(), *dex_file_) 2177 << " because of unhandled instruction " 2178 << instruction.Name(); 2179 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnhandledInstruction); 2180 return false; 2181 } 2182 return true; 2183} // NOLINT(readability/fn_size) 2184 2185HLocal* HGraphBuilder::GetLocalAt(int register_index) const { 2186 return locals_.Get(register_index); 2187} 2188 2189void HGraphBuilder::UpdateLocal(int register_index, HInstruction* instruction) const { 2190 HLocal* local = GetLocalAt(register_index); 2191 current_block_->AddInstruction(new (arena_) HStoreLocal(local, instruction)); 2192} 2193 2194HInstruction* HGraphBuilder::LoadLocal(int register_index, Primitive::Type type) const { 2195 HLocal* local = GetLocalAt(register_index); 2196 current_block_->AddInstruction(new (arena_) HLoadLocal(local, type)); 2197 return current_block_->GetLastInstruction(); 2198} 2199 2200} // namespace art 2201