code_generator.cc revision 3b7537bfc5a6b7ccb18b3970d8edf14b72464af7
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 "code_generator.h" 18 19#ifdef ART_ENABLE_CODEGEN_arm 20#include "code_generator_arm.h" 21#endif 22 23#ifdef ART_ENABLE_CODEGEN_arm64 24#include "code_generator_arm64.h" 25#endif 26 27#ifdef ART_ENABLE_CODEGEN_x86 28#include "code_generator_x86.h" 29#endif 30 31#ifdef ART_ENABLE_CODEGEN_x86_64 32#include "code_generator_x86_64.h" 33#endif 34 35#ifdef ART_ENABLE_CODEGEN_mips 36#include "code_generator_mips.h" 37#endif 38 39#ifdef ART_ENABLE_CODEGEN_mips64 40#include "code_generator_mips64.h" 41#endif 42 43#include "bytecode_utils.h" 44#include "compiled_method.h" 45#include "dex/verified_method.h" 46#include "driver/compiler_driver.h" 47#include "graph_visualizer.h" 48#include "intrinsics.h" 49#include "leb128.h" 50#include "mirror/array-inl.h" 51#include "mirror/object_array-inl.h" 52#include "mirror/object_reference.h" 53#include "mirror/string.h" 54#include "parallel_move_resolver.h" 55#include "ssa_liveness_analysis.h" 56#include "utils/assembler.h" 57 58namespace art { 59 60// Return whether a location is consistent with a type. 61static bool CheckType(Primitive::Type type, Location location) { 62 if (location.IsFpuRegister() 63 || (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresFpuRegister))) { 64 return (type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble); 65 } else if (location.IsRegister() || 66 (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresRegister))) { 67 return Primitive::IsIntegralType(type) || (type == Primitive::kPrimNot); 68 } else if (location.IsRegisterPair()) { 69 return type == Primitive::kPrimLong; 70 } else if (location.IsFpuRegisterPair()) { 71 return type == Primitive::kPrimDouble; 72 } else if (location.IsStackSlot()) { 73 return (Primitive::IsIntegralType(type) && type != Primitive::kPrimLong) 74 || (type == Primitive::kPrimFloat) 75 || (type == Primitive::kPrimNot); 76 } else if (location.IsDoubleStackSlot()) { 77 return (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble); 78 } else if (location.IsConstant()) { 79 if (location.GetConstant()->IsIntConstant()) { 80 return Primitive::IsIntegralType(type) && (type != Primitive::kPrimLong); 81 } else if (location.GetConstant()->IsNullConstant()) { 82 return type == Primitive::kPrimNot; 83 } else if (location.GetConstant()->IsLongConstant()) { 84 return type == Primitive::kPrimLong; 85 } else if (location.GetConstant()->IsFloatConstant()) { 86 return type == Primitive::kPrimFloat; 87 } else { 88 return location.GetConstant()->IsDoubleConstant() 89 && (type == Primitive::kPrimDouble); 90 } 91 } else { 92 return location.IsInvalid() || (location.GetPolicy() == Location::kAny); 93 } 94} 95 96// Check that a location summary is consistent with an instruction. 97static bool CheckTypeConsistency(HInstruction* instruction) { 98 LocationSummary* locations = instruction->GetLocations(); 99 if (locations == nullptr) { 100 return true; 101 } 102 103 if (locations->Out().IsUnallocated() 104 && (locations->Out().GetPolicy() == Location::kSameAsFirstInput)) { 105 DCHECK(CheckType(instruction->GetType(), locations->InAt(0))) 106 << instruction->GetType() 107 << " " << locations->InAt(0); 108 } else { 109 DCHECK(CheckType(instruction->GetType(), locations->Out())) 110 << instruction->GetType() 111 << " " << locations->Out(); 112 } 113 114 HConstInputsRef inputs = instruction->GetInputs(); 115 for (size_t i = 0; i < inputs.size(); ++i) { 116 DCHECK(CheckType(inputs[i]->GetType(), locations->InAt(i))) 117 << inputs[i]->GetType() << " " << locations->InAt(i); 118 } 119 120 HEnvironment* environment = instruction->GetEnvironment(); 121 for (size_t i = 0; i < instruction->EnvironmentSize(); ++i) { 122 if (environment->GetInstructionAt(i) != nullptr) { 123 Primitive::Type type = environment->GetInstructionAt(i)->GetType(); 124 DCHECK(CheckType(type, environment->GetLocationAt(i))) 125 << type << " " << environment->GetLocationAt(i); 126 } else { 127 DCHECK(environment->GetLocationAt(i).IsInvalid()) 128 << environment->GetLocationAt(i); 129 } 130 } 131 return true; 132} 133 134size_t CodeGenerator::GetCacheOffset(uint32_t index) { 135 return sizeof(GcRoot<mirror::Object>) * index; 136} 137 138size_t CodeGenerator::GetCachePointerOffset(uint32_t index) { 139 auto pointer_size = InstructionSetPointerSize(GetInstructionSet()); 140 return static_cast<size_t>(pointer_size) * index; 141} 142 143uint32_t CodeGenerator::GetArrayLengthOffset(HArrayLength* array_length) { 144 return array_length->IsStringLength() 145 ? mirror::String::CountOffset().Uint32Value() 146 : mirror::Array::LengthOffset().Uint32Value(); 147} 148 149uint32_t CodeGenerator::GetArrayDataOffset(HArrayGet* array_get) { 150 DCHECK(array_get->GetType() == Primitive::kPrimChar || !array_get->IsStringCharAt()); 151 return array_get->IsStringCharAt() 152 ? mirror::String::ValueOffset().Uint32Value() 153 : mirror::Array::DataOffset(Primitive::ComponentSize(array_get->GetType())).Uint32Value(); 154} 155 156bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const { 157 DCHECK_EQ((*block_order_)[current_block_index_], current); 158 return GetNextBlockToEmit() == FirstNonEmptyBlock(next); 159} 160 161HBasicBlock* CodeGenerator::GetNextBlockToEmit() const { 162 for (size_t i = current_block_index_ + 1; i < block_order_->size(); ++i) { 163 HBasicBlock* block = (*block_order_)[i]; 164 if (!block->IsSingleJump()) { 165 return block; 166 } 167 } 168 return nullptr; 169} 170 171HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const { 172 while (block->IsSingleJump()) { 173 block = block->GetSuccessors()[0]; 174 } 175 return block; 176} 177 178class DisassemblyScope { 179 public: 180 DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen) 181 : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) { 182 if (codegen_.GetDisassemblyInformation() != nullptr) { 183 start_offset_ = codegen_.GetAssembler().CodeSize(); 184 } 185 } 186 187 ~DisassemblyScope() { 188 // We avoid building this data when we know it will not be used. 189 if (codegen_.GetDisassemblyInformation() != nullptr) { 190 codegen_.GetDisassemblyInformation()->AddInstructionInterval( 191 instruction_, start_offset_, codegen_.GetAssembler().CodeSize()); 192 } 193 } 194 195 private: 196 const CodeGenerator& codegen_; 197 HInstruction* instruction_; 198 size_t start_offset_; 199}; 200 201 202void CodeGenerator::GenerateSlowPaths() { 203 size_t code_start = 0; 204 for (const std::unique_ptr<SlowPathCode>& slow_path_unique_ptr : slow_paths_) { 205 SlowPathCode* slow_path = slow_path_unique_ptr.get(); 206 current_slow_path_ = slow_path; 207 if (disasm_info_ != nullptr) { 208 code_start = GetAssembler()->CodeSize(); 209 } 210 // Record the dex pc at start of slow path (required for java line number mapping). 211 MaybeRecordNativeDebugInfo(slow_path->GetInstruction(), slow_path->GetDexPc(), slow_path); 212 slow_path->EmitNativeCode(this); 213 if (disasm_info_ != nullptr) { 214 disasm_info_->AddSlowPathInterval(slow_path, code_start, GetAssembler()->CodeSize()); 215 } 216 } 217 current_slow_path_ = nullptr; 218} 219 220void CodeGenerator::Compile(CodeAllocator* allocator) { 221 // The register allocator already called `InitializeCodeGeneration`, 222 // where the frame size has been computed. 223 DCHECK(block_order_ != nullptr); 224 Initialize(); 225 226 HGraphVisitor* instruction_visitor = GetInstructionVisitor(); 227 DCHECK_EQ(current_block_index_, 0u); 228 229 size_t frame_start = GetAssembler()->CodeSize(); 230 GenerateFrameEntry(); 231 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_)); 232 if (disasm_info_ != nullptr) { 233 disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize()); 234 } 235 236 for (size_t e = block_order_->size(); current_block_index_ < e; ++current_block_index_) { 237 HBasicBlock* block = (*block_order_)[current_block_index_]; 238 // Don't generate code for an empty block. Its predecessors will branch to its successor 239 // directly. Also, the label of that block will not be emitted, so this helps catch 240 // errors where we reference that label. 241 if (block->IsSingleJump()) continue; 242 Bind(block); 243 // This ensures that we have correct native line mapping for all native instructions. 244 // It is necessary to make stepping over a statement work. Otherwise, any initial 245 // instructions (e.g. moves) would be assumed to be the start of next statement. 246 MaybeRecordNativeDebugInfo(nullptr /* instruction */, block->GetDexPc()); 247 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { 248 HInstruction* current = it.Current(); 249 if (current->HasEnvironment()) { 250 // Create stackmap for HNativeDebugInfo or any instruction which calls native code. 251 // Note that we need correct mapping for the native PC of the call instruction, 252 // so the runtime's stackmap is not sufficient since it is at PC after the call. 253 MaybeRecordNativeDebugInfo(current, block->GetDexPc()); 254 } 255 DisassemblyScope disassembly_scope(current, *this); 256 DCHECK(CheckTypeConsistency(current)); 257 current->Accept(instruction_visitor); 258 } 259 } 260 261 GenerateSlowPaths(); 262 263 // Emit catch stack maps at the end of the stack map stream as expected by the 264 // runtime exception handler. 265 if (graph_->HasTryCatch()) { 266 RecordCatchBlockInfo(); 267 } 268 269 // Finalize instructions in assember; 270 Finalize(allocator); 271} 272 273void CodeGenerator::Finalize(CodeAllocator* allocator) { 274 size_t code_size = GetAssembler()->CodeSize(); 275 uint8_t* buffer = allocator->Allocate(code_size); 276 277 MemoryRegion code(buffer, code_size); 278 GetAssembler()->FinalizeInstructions(code); 279} 280 281void CodeGenerator::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches ATTRIBUTE_UNUSED) { 282 // No linker patches by default. 283} 284 285void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots, 286 size_t maximum_safepoint_spill_size, 287 size_t number_of_out_slots, 288 const ArenaVector<HBasicBlock*>& block_order) { 289 block_order_ = &block_order; 290 DCHECK(!block_order.empty()); 291 DCHECK(block_order[0] == GetGraph()->GetEntryBlock()); 292 ComputeSpillMask(); 293 first_register_slot_in_slow_path_ = RoundUp( 294 (number_of_out_slots + number_of_spill_slots) * kVRegSize, GetPreferredSlotsAlignment()); 295 296 if (number_of_spill_slots == 0 297 && !HasAllocatedCalleeSaveRegisters() 298 && IsLeafMethod() 299 && !RequiresCurrentMethod()) { 300 DCHECK_EQ(maximum_safepoint_spill_size, 0u); 301 SetFrameSize(CallPushesPC() ? GetWordSize() : 0); 302 } else { 303 SetFrameSize(RoundUp( 304 first_register_slot_in_slow_path_ 305 + maximum_safepoint_spill_size 306 + FrameEntrySpillSize(), 307 kStackAlignment)); 308 } 309} 310 311void CodeGenerator::CreateCommonInvokeLocationSummary( 312 HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) { 313 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena(); 314 LocationSummary* locations = new (allocator) LocationSummary(invoke, 315 LocationSummary::kCallOnMainOnly); 316 317 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) { 318 HInstruction* input = invoke->InputAt(i); 319 locations->SetInAt(i, visitor->GetNextLocation(input->GetType())); 320 } 321 322 locations->SetOut(visitor->GetReturnLocation(invoke->GetType())); 323 324 if (invoke->IsInvokeStaticOrDirect()) { 325 HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect(); 326 switch (call->GetMethodLoadKind()) { 327 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive: 328 locations->SetInAt(call->GetSpecialInputIndex(), visitor->GetMethodLocation()); 329 break; 330 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: 331 locations->AddTemp(visitor->GetMethodLocation()); 332 locations->SetInAt(call->GetSpecialInputIndex(), Location::RequiresRegister()); 333 break; 334 default: 335 locations->AddTemp(visitor->GetMethodLocation()); 336 break; 337 } 338 } else { 339 locations->AddTemp(visitor->GetMethodLocation()); 340 } 341} 342 343void CodeGenerator::GenerateInvokeUnresolvedRuntimeCall(HInvokeUnresolved* invoke) { 344 MoveConstant(invoke->GetLocations()->GetTemp(0), invoke->GetDexMethodIndex()); 345 346 // Initialize to anything to silent compiler warnings. 347 QuickEntrypointEnum entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck; 348 switch (invoke->GetOriginalInvokeType()) { 349 case kStatic: 350 entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck; 351 break; 352 case kDirect: 353 entrypoint = kQuickInvokeDirectTrampolineWithAccessCheck; 354 break; 355 case kVirtual: 356 entrypoint = kQuickInvokeVirtualTrampolineWithAccessCheck; 357 break; 358 case kSuper: 359 entrypoint = kQuickInvokeSuperTrampolineWithAccessCheck; 360 break; 361 case kInterface: 362 entrypoint = kQuickInvokeInterfaceTrampolineWithAccessCheck; 363 break; 364 } 365 InvokeRuntime(entrypoint, invoke, invoke->GetDexPc(), nullptr); 366} 367 368void CodeGenerator::CreateUnresolvedFieldLocationSummary( 369 HInstruction* field_access, 370 Primitive::Type field_type, 371 const FieldAccessCallingConvention& calling_convention) { 372 bool is_instance = field_access->IsUnresolvedInstanceFieldGet() 373 || field_access->IsUnresolvedInstanceFieldSet(); 374 bool is_get = field_access->IsUnresolvedInstanceFieldGet() 375 || field_access->IsUnresolvedStaticFieldGet(); 376 377 ArenaAllocator* allocator = field_access->GetBlock()->GetGraph()->GetArena(); 378 LocationSummary* locations = 379 new (allocator) LocationSummary(field_access, LocationSummary::kCallOnMainOnly); 380 381 locations->AddTemp(calling_convention.GetFieldIndexLocation()); 382 383 if (is_instance) { 384 // Add the `this` object for instance field accesses. 385 locations->SetInAt(0, calling_convention.GetObjectLocation()); 386 } 387 388 // Note that pSetXXStatic/pGetXXStatic always takes/returns an int or int64 389 // regardless of the the type. Because of that we forced to special case 390 // the access to floating point values. 391 if (is_get) { 392 if (Primitive::IsFloatingPointType(field_type)) { 393 // The return value will be stored in regular registers while register 394 // allocator expects it in a floating point register. 395 // Note We don't need to request additional temps because the return 396 // register(s) are already blocked due the call and they may overlap with 397 // the input or field index. 398 // The transfer between the two will be done at codegen level. 399 locations->SetOut(calling_convention.GetFpuLocation(field_type)); 400 } else { 401 locations->SetOut(calling_convention.GetReturnLocation(field_type)); 402 } 403 } else { 404 size_t set_index = is_instance ? 1 : 0; 405 if (Primitive::IsFloatingPointType(field_type)) { 406 // The set value comes from a float location while the calling convention 407 // expects it in a regular register location. Allocate a temp for it and 408 // make the transfer at codegen. 409 AddLocationAsTemp(calling_convention.GetSetValueLocation(field_type, is_instance), locations); 410 locations->SetInAt(set_index, calling_convention.GetFpuLocation(field_type)); 411 } else { 412 locations->SetInAt(set_index, 413 calling_convention.GetSetValueLocation(field_type, is_instance)); 414 } 415 } 416} 417 418void CodeGenerator::GenerateUnresolvedFieldAccess( 419 HInstruction* field_access, 420 Primitive::Type field_type, 421 uint32_t field_index, 422 uint32_t dex_pc, 423 const FieldAccessCallingConvention& calling_convention) { 424 LocationSummary* locations = field_access->GetLocations(); 425 426 MoveConstant(locations->GetTemp(0), field_index); 427 428 bool is_instance = field_access->IsUnresolvedInstanceFieldGet() 429 || field_access->IsUnresolvedInstanceFieldSet(); 430 bool is_get = field_access->IsUnresolvedInstanceFieldGet() 431 || field_access->IsUnresolvedStaticFieldGet(); 432 433 if (!is_get && Primitive::IsFloatingPointType(field_type)) { 434 // Copy the float value to be set into the calling convention register. 435 // Note that using directly the temp location is problematic as we don't 436 // support temp register pairs. To avoid boilerplate conversion code, use 437 // the location from the calling convention. 438 MoveLocation(calling_convention.GetSetValueLocation(field_type, is_instance), 439 locations->InAt(is_instance ? 1 : 0), 440 (Primitive::Is64BitType(field_type) ? Primitive::kPrimLong : Primitive::kPrimInt)); 441 } 442 443 QuickEntrypointEnum entrypoint = kQuickSet8Static; // Initialize to anything to avoid warnings. 444 switch (field_type) { 445 case Primitive::kPrimBoolean: 446 entrypoint = is_instance 447 ? (is_get ? kQuickGetBooleanInstance : kQuickSet8Instance) 448 : (is_get ? kQuickGetBooleanStatic : kQuickSet8Static); 449 break; 450 case Primitive::kPrimByte: 451 entrypoint = is_instance 452 ? (is_get ? kQuickGetByteInstance : kQuickSet8Instance) 453 : (is_get ? kQuickGetByteStatic : kQuickSet8Static); 454 break; 455 case Primitive::kPrimShort: 456 entrypoint = is_instance 457 ? (is_get ? kQuickGetShortInstance : kQuickSet16Instance) 458 : (is_get ? kQuickGetShortStatic : kQuickSet16Static); 459 break; 460 case Primitive::kPrimChar: 461 entrypoint = is_instance 462 ? (is_get ? kQuickGetCharInstance : kQuickSet16Instance) 463 : (is_get ? kQuickGetCharStatic : kQuickSet16Static); 464 break; 465 case Primitive::kPrimInt: 466 case Primitive::kPrimFloat: 467 entrypoint = is_instance 468 ? (is_get ? kQuickGet32Instance : kQuickSet32Instance) 469 : (is_get ? kQuickGet32Static : kQuickSet32Static); 470 break; 471 case Primitive::kPrimNot: 472 entrypoint = is_instance 473 ? (is_get ? kQuickGetObjInstance : kQuickSetObjInstance) 474 : (is_get ? kQuickGetObjStatic : kQuickSetObjStatic); 475 break; 476 case Primitive::kPrimLong: 477 case Primitive::kPrimDouble: 478 entrypoint = is_instance 479 ? (is_get ? kQuickGet64Instance : kQuickSet64Instance) 480 : (is_get ? kQuickGet64Static : kQuickSet64Static); 481 break; 482 default: 483 LOG(FATAL) << "Invalid type " << field_type; 484 } 485 InvokeRuntime(entrypoint, field_access, dex_pc, nullptr); 486 487 if (is_get && Primitive::IsFloatingPointType(field_type)) { 488 MoveLocation(locations->Out(), calling_convention.GetReturnLocation(field_type), field_type); 489 } 490} 491 492// TODO: Remove argument `code_generator_supports_read_barrier` when 493// all code generators have read barrier support. 494void CodeGenerator::CreateLoadClassLocationSummary(HLoadClass* cls, 495 Location runtime_type_index_location, 496 Location runtime_return_location, 497 bool code_generator_supports_read_barrier) { 498 ArenaAllocator* allocator = cls->GetBlock()->GetGraph()->GetArena(); 499 LocationSummary::CallKind call_kind = cls->NeedsAccessCheck() 500 ? LocationSummary::kCallOnMainOnly 501 : (((code_generator_supports_read_barrier && kEmitCompilerReadBarrier) || 502 cls->CanCallRuntime()) 503 ? LocationSummary::kCallOnSlowPath 504 : LocationSummary::kNoCall); 505 LocationSummary* locations = new (allocator) LocationSummary(cls, call_kind); 506 if (cls->NeedsAccessCheck()) { 507 locations->SetInAt(0, Location::NoLocation()); 508 locations->AddTemp(runtime_type_index_location); 509 locations->SetOut(runtime_return_location); 510 } else { 511 locations->SetInAt(0, Location::RequiresRegister()); 512 locations->SetOut(Location::RequiresRegister()); 513 } 514} 515 516 517void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const { 518 // The DCHECKS below check that a register is not specified twice in 519 // the summary. The out location can overlap with an input, so we need 520 // to special case it. 521 if (location.IsRegister()) { 522 DCHECK(is_out || !blocked_core_registers_[location.reg()]); 523 blocked_core_registers_[location.reg()] = true; 524 } else if (location.IsFpuRegister()) { 525 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]); 526 blocked_fpu_registers_[location.reg()] = true; 527 } else if (location.IsFpuRegisterPair()) { 528 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]); 529 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true; 530 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]); 531 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true; 532 } else if (location.IsRegisterPair()) { 533 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]); 534 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true; 535 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]); 536 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true; 537 } 538} 539 540void CodeGenerator::AllocateLocations(HInstruction* instruction) { 541 instruction->Accept(GetLocationBuilder()); 542 DCHECK(CheckTypeConsistency(instruction)); 543 LocationSummary* locations = instruction->GetLocations(); 544 if (!instruction->IsSuspendCheckEntry()) { 545 if (locations != nullptr) { 546 if (locations->CanCall()) { 547 MarkNotLeaf(); 548 } else if (locations->Intrinsified() && 549 instruction->IsInvokeStaticOrDirect() && 550 !instruction->AsInvokeStaticOrDirect()->HasCurrentMethodInput()) { 551 // A static method call that has been fully intrinsified, and cannot call on the slow 552 // path or refer to the current method directly, no longer needs current method. 553 return; 554 } 555 } 556 if (instruction->NeedsCurrentMethod()) { 557 SetRequiresCurrentMethod(); 558 } 559 } 560} 561 562void CodeGenerator::MaybeRecordStat(MethodCompilationStat compilation_stat, size_t count) const { 563 if (stats_ != nullptr) { 564 stats_->RecordStat(compilation_stat, count); 565 } 566} 567 568std::unique_ptr<CodeGenerator> CodeGenerator::Create(HGraph* graph, 569 InstructionSet instruction_set, 570 const InstructionSetFeatures& isa_features, 571 const CompilerOptions& compiler_options, 572 OptimizingCompilerStats* stats) { 573 ArenaAllocator* arena = graph->GetArena(); 574 switch (instruction_set) { 575#ifdef ART_ENABLE_CODEGEN_arm 576 case kArm: 577 case kThumb2: { 578 return std::unique_ptr<CodeGenerator>( 579 new (arena) arm::CodeGeneratorARM(graph, 580 *isa_features.AsArmInstructionSetFeatures(), 581 compiler_options, 582 stats)); 583 } 584#endif 585#ifdef ART_ENABLE_CODEGEN_arm64 586 case kArm64: { 587 return std::unique_ptr<CodeGenerator>( 588 new (arena) arm64::CodeGeneratorARM64(graph, 589 *isa_features.AsArm64InstructionSetFeatures(), 590 compiler_options, 591 stats)); 592 } 593#endif 594#ifdef ART_ENABLE_CODEGEN_mips 595 case kMips: { 596 return std::unique_ptr<CodeGenerator>( 597 new (arena) mips::CodeGeneratorMIPS(graph, 598 *isa_features.AsMipsInstructionSetFeatures(), 599 compiler_options, 600 stats)); 601 } 602#endif 603#ifdef ART_ENABLE_CODEGEN_mips64 604 case kMips64: { 605 return std::unique_ptr<CodeGenerator>( 606 new (arena) mips64::CodeGeneratorMIPS64(graph, 607 *isa_features.AsMips64InstructionSetFeatures(), 608 compiler_options, 609 stats)); 610 } 611#endif 612#ifdef ART_ENABLE_CODEGEN_x86 613 case kX86: { 614 return std::unique_ptr<CodeGenerator>( 615 new (arena) x86::CodeGeneratorX86(graph, 616 *isa_features.AsX86InstructionSetFeatures(), 617 compiler_options, 618 stats)); 619 } 620#endif 621#ifdef ART_ENABLE_CODEGEN_x86_64 622 case kX86_64: { 623 return std::unique_ptr<CodeGenerator>( 624 new (arena) x86_64::CodeGeneratorX86_64(graph, 625 *isa_features.AsX86_64InstructionSetFeatures(), 626 compiler_options, 627 stats)); 628 } 629#endif 630 default: 631 return nullptr; 632 } 633} 634 635size_t CodeGenerator::ComputeStackMapsSize() { 636 return stack_map_stream_.PrepareForFillIn(); 637} 638 639static void CheckCovers(uint32_t dex_pc, 640 const HGraph& graph, 641 const CodeInfo& code_info, 642 const ArenaVector<HSuspendCheck*>& loop_headers, 643 ArenaVector<size_t>* covered) { 644 CodeInfoEncoding encoding = code_info.ExtractEncoding(); 645 for (size_t i = 0; i < loop_headers.size(); ++i) { 646 if (loop_headers[i]->GetDexPc() == dex_pc) { 647 if (graph.IsCompilingOsr()) { 648 DCHECK(code_info.GetOsrStackMapForDexPc(dex_pc, encoding).IsValid()); 649 } 650 ++(*covered)[i]; 651 } 652 } 653} 654 655// Debug helper to ensure loop entries in compiled code are matched by 656// dex branch instructions. 657static void CheckLoopEntriesCanBeUsedForOsr(const HGraph& graph, 658 const CodeInfo& code_info, 659 const DexFile::CodeItem& code_item) { 660 if (graph.HasTryCatch()) { 661 // One can write loops through try/catch, which we do not support for OSR anyway. 662 return; 663 } 664 ArenaVector<HSuspendCheck*> loop_headers(graph.GetArena()->Adapter(kArenaAllocMisc)); 665 for (HReversePostOrderIterator it(graph); !it.Done(); it.Advance()) { 666 if (it.Current()->IsLoopHeader()) { 667 HSuspendCheck* suspend_check = it.Current()->GetLoopInformation()->GetSuspendCheck(); 668 if (!suspend_check->GetEnvironment()->IsFromInlinedInvoke()) { 669 loop_headers.push_back(suspend_check); 670 } 671 } 672 } 673 ArenaVector<size_t> covered(loop_headers.size(), 0, graph.GetArena()->Adapter(kArenaAllocMisc)); 674 const uint16_t* code_ptr = code_item.insns_; 675 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_; 676 677 size_t dex_pc = 0; 678 while (code_ptr < code_end) { 679 const Instruction& instruction = *Instruction::At(code_ptr); 680 if (instruction.IsBranch()) { 681 uint32_t target = dex_pc + instruction.GetTargetOffset(); 682 CheckCovers(target, graph, code_info, loop_headers, &covered); 683 } else if (instruction.IsSwitch()) { 684 DexSwitchTable table(instruction, dex_pc); 685 uint16_t num_entries = table.GetNumEntries(); 686 size_t offset = table.GetFirstValueIndex(); 687 688 // Use a larger loop counter type to avoid overflow issues. 689 for (size_t i = 0; i < num_entries; ++i) { 690 // The target of the case. 691 uint32_t target = dex_pc + table.GetEntryAt(i + offset); 692 CheckCovers(target, graph, code_info, loop_headers, &covered); 693 } 694 } 695 dex_pc += instruction.SizeInCodeUnits(); 696 code_ptr += instruction.SizeInCodeUnits(); 697 } 698 699 for (size_t i = 0; i < covered.size(); ++i) { 700 DCHECK_NE(covered[i], 0u) << "Loop in compiled code has no dex branch equivalent"; 701 } 702} 703 704void CodeGenerator::BuildStackMaps(MemoryRegion region, const DexFile::CodeItem& code_item) { 705 stack_map_stream_.FillIn(region); 706 if (kIsDebugBuild) { 707 CheckLoopEntriesCanBeUsedForOsr(*graph_, CodeInfo(region), code_item); 708 } 709} 710 711void CodeGenerator::RecordPcInfo(HInstruction* instruction, 712 uint32_t dex_pc, 713 SlowPathCode* slow_path) { 714 if (instruction != nullptr) { 715 // The code generated for some type conversions 716 // may call the runtime, thus normally requiring a subsequent 717 // call to this method. However, the method verifier does not 718 // produce PC information for certain instructions, which are 719 // considered "atomic" (they cannot join a GC). 720 // Therefore we do not currently record PC information for such 721 // instructions. As this may change later, we added this special 722 // case so that code generators may nevertheless call 723 // CodeGenerator::RecordPcInfo without triggering an error in 724 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x") 725 // thereafter. 726 if (instruction->IsTypeConversion()) { 727 return; 728 } 729 if (instruction->IsRem()) { 730 Primitive::Type type = instruction->AsRem()->GetResultType(); 731 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) { 732 return; 733 } 734 } 735 } 736 737 uint32_t outer_dex_pc = dex_pc; 738 uint32_t outer_environment_size = 0; 739 uint32_t inlining_depth = 0; 740 if (instruction != nullptr) { 741 for (HEnvironment* environment = instruction->GetEnvironment(); 742 environment != nullptr; 743 environment = environment->GetParent()) { 744 outer_dex_pc = environment->GetDexPc(); 745 outer_environment_size = environment->Size(); 746 if (environment != instruction->GetEnvironment()) { 747 inlining_depth++; 748 } 749 } 750 } 751 752 // Collect PC infos for the mapping table. 753 uint32_t native_pc = GetAssembler()->CodePosition(); 754 755 if (instruction == nullptr) { 756 // For stack overflow checks and native-debug-info entries without dex register 757 // mapping (i.e. start of basic block or start of slow path). 758 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0); 759 stack_map_stream_.EndStackMapEntry(); 760 return; 761 } 762 LocationSummary* locations = instruction->GetLocations(); 763 764 uint32_t register_mask = locations->GetRegisterMask(); 765 DCHECK_EQ(register_mask & ~locations->GetLiveRegisters()->GetCoreRegisters(), 0u); 766 if (locations->OnlyCallsOnSlowPath()) { 767 // In case of slow path, we currently set the location of caller-save registers 768 // to register (instead of their stack location when pushed before the slow-path 769 // call). Therefore register_mask contains both callee-save and caller-save 770 // registers that hold objects. We must remove the spilled caller-save from the 771 // mask, since they will be overwritten by the callee. 772 uint32_t spills = GetSlowPathSpills(locations, /* core_registers */ true); 773 register_mask &= ~spills; 774 } else { 775 // The register mask must be a subset of callee-save registers. 776 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask); 777 } 778 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, 779 native_pc, 780 register_mask, 781 locations->GetStackMask(), 782 outer_environment_size, 783 inlining_depth); 784 785 EmitEnvironment(instruction->GetEnvironment(), slow_path); 786 stack_map_stream_.EndStackMapEntry(); 787 788 HLoopInformation* info = instruction->GetBlock()->GetLoopInformation(); 789 if (instruction->IsSuspendCheck() && 790 (info != nullptr) && 791 graph_->IsCompilingOsr() && 792 (inlining_depth == 0)) { 793 DCHECK_EQ(info->GetSuspendCheck(), instruction); 794 // We duplicate the stack map as a marker that this stack map can be an OSR entry. 795 // Duplicating it avoids having the runtime recognize and skip an OSR stack map. 796 DCHECK(info->IsIrreducible()); 797 stack_map_stream_.BeginStackMapEntry( 798 dex_pc, native_pc, register_mask, locations->GetStackMask(), outer_environment_size, 0); 799 EmitEnvironment(instruction->GetEnvironment(), slow_path); 800 stack_map_stream_.EndStackMapEntry(); 801 if (kIsDebugBuild) { 802 HEnvironment* environment = instruction->GetEnvironment(); 803 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) { 804 HInstruction* in_environment = environment->GetInstructionAt(i); 805 if (in_environment != nullptr) { 806 DCHECK(in_environment->IsPhi() || in_environment->IsConstant()); 807 Location location = environment->GetLocationAt(i); 808 DCHECK(location.IsStackSlot() || 809 location.IsDoubleStackSlot() || 810 location.IsConstant() || 811 location.IsInvalid()); 812 if (location.IsStackSlot() || location.IsDoubleStackSlot()) { 813 DCHECK_LT(location.GetStackIndex(), static_cast<int32_t>(GetFrameSize())); 814 } 815 } 816 } 817 } 818 } else if (kIsDebugBuild) { 819 // Ensure stack maps are unique, by checking that the native pc in the stack map 820 // last emitted is different than the native pc of the stack map just emitted. 821 size_t number_of_stack_maps = stack_map_stream_.GetNumberOfStackMaps(); 822 if (number_of_stack_maps > 1) { 823 DCHECK_NE(stack_map_stream_.GetStackMap(number_of_stack_maps - 1).native_pc_offset, 824 stack_map_stream_.GetStackMap(number_of_stack_maps - 2).native_pc_offset); 825 } 826 } 827} 828 829bool CodeGenerator::HasStackMapAtCurrentPc() { 830 uint32_t pc = GetAssembler()->CodeSize(); 831 size_t count = stack_map_stream_.GetNumberOfStackMaps(); 832 return count > 0 && stack_map_stream_.GetStackMap(count - 1).native_pc_offset == pc; 833} 834 835void CodeGenerator::MaybeRecordNativeDebugInfo(HInstruction* instruction, 836 uint32_t dex_pc, 837 SlowPathCode* slow_path) { 838 if (GetCompilerOptions().GetNativeDebuggable() && dex_pc != kNoDexPc) { 839 if (HasStackMapAtCurrentPc()) { 840 // Ensure that we do not collide with the stack map of the previous instruction. 841 GenerateNop(); 842 } 843 RecordPcInfo(instruction, dex_pc, slow_path); 844 } 845} 846 847void CodeGenerator::RecordCatchBlockInfo() { 848 ArenaAllocator* arena = graph_->GetArena(); 849 850 for (HBasicBlock* block : *block_order_) { 851 if (!block->IsCatchBlock()) { 852 continue; 853 } 854 855 uint32_t dex_pc = block->GetDexPc(); 856 uint32_t num_vregs = graph_->GetNumberOfVRegs(); 857 uint32_t inlining_depth = 0; // Inlining of catch blocks is not supported at the moment. 858 uint32_t native_pc = GetAddressOf(block); 859 uint32_t register_mask = 0; // Not used. 860 861 // The stack mask is not used, so we leave it empty. 862 ArenaBitVector* stack_mask = 863 ArenaBitVector::Create(arena, 0, /* expandable */ true, kArenaAllocCodeGenerator); 864 865 stack_map_stream_.BeginStackMapEntry(dex_pc, 866 native_pc, 867 register_mask, 868 stack_mask, 869 num_vregs, 870 inlining_depth); 871 872 HInstruction* current_phi = block->GetFirstPhi(); 873 for (size_t vreg = 0; vreg < num_vregs; ++vreg) { 874 while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) { 875 HInstruction* next_phi = current_phi->GetNext(); 876 DCHECK(next_phi == nullptr || 877 current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber()) 878 << "Phis need to be sorted by vreg number to keep this a linear-time loop."; 879 current_phi = next_phi; 880 } 881 882 if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) { 883 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0); 884 } else { 885 Location location = current_phi->GetLiveInterval()->ToLocation(); 886 switch (location.GetKind()) { 887 case Location::kStackSlot: { 888 stack_map_stream_.AddDexRegisterEntry( 889 DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); 890 break; 891 } 892 case Location::kDoubleStackSlot: { 893 stack_map_stream_.AddDexRegisterEntry( 894 DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); 895 stack_map_stream_.AddDexRegisterEntry( 896 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize)); 897 ++vreg; 898 DCHECK_LT(vreg, num_vregs); 899 break; 900 } 901 default: { 902 // All catch phis must be allocated to a stack slot. 903 LOG(FATAL) << "Unexpected kind " << location.GetKind(); 904 UNREACHABLE(); 905 } 906 } 907 } 908 } 909 910 stack_map_stream_.EndStackMapEntry(); 911 } 912} 913 914void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) { 915 if (environment == nullptr) return; 916 917 if (environment->GetParent() != nullptr) { 918 // We emit the parent environment first. 919 EmitEnvironment(environment->GetParent(), slow_path); 920 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(), 921 environment->GetDexPc(), 922 environment->GetInvokeType(), 923 environment->Size()); 924 } 925 926 // Walk over the environment, and record the location of dex registers. 927 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) { 928 HInstruction* current = environment->GetInstructionAt(i); 929 if (current == nullptr) { 930 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0); 931 continue; 932 } 933 934 Location location = environment->GetLocationAt(i); 935 switch (location.GetKind()) { 936 case Location::kConstant: { 937 DCHECK_EQ(current, location.GetConstant()); 938 if (current->IsLongConstant()) { 939 int64_t value = current->AsLongConstant()->GetValue(); 940 stack_map_stream_.AddDexRegisterEntry( 941 DexRegisterLocation::Kind::kConstant, Low32Bits(value)); 942 stack_map_stream_.AddDexRegisterEntry( 943 DexRegisterLocation::Kind::kConstant, High32Bits(value)); 944 ++i; 945 DCHECK_LT(i, environment_size); 946 } else if (current->IsDoubleConstant()) { 947 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue()); 948 stack_map_stream_.AddDexRegisterEntry( 949 DexRegisterLocation::Kind::kConstant, Low32Bits(value)); 950 stack_map_stream_.AddDexRegisterEntry( 951 DexRegisterLocation::Kind::kConstant, High32Bits(value)); 952 ++i; 953 DCHECK_LT(i, environment_size); 954 } else if (current->IsIntConstant()) { 955 int32_t value = current->AsIntConstant()->GetValue(); 956 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value); 957 } else if (current->IsNullConstant()) { 958 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0); 959 } else { 960 DCHECK(current->IsFloatConstant()) << current->DebugName(); 961 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue()); 962 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value); 963 } 964 break; 965 } 966 967 case Location::kStackSlot: { 968 stack_map_stream_.AddDexRegisterEntry( 969 DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); 970 break; 971 } 972 973 case Location::kDoubleStackSlot: { 974 stack_map_stream_.AddDexRegisterEntry( 975 DexRegisterLocation::Kind::kInStack, location.GetStackIndex()); 976 stack_map_stream_.AddDexRegisterEntry( 977 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize)); 978 ++i; 979 DCHECK_LT(i, environment_size); 980 break; 981 } 982 983 case Location::kRegister : { 984 int id = location.reg(); 985 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) { 986 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id); 987 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); 988 if (current->GetType() == Primitive::kPrimLong) { 989 stack_map_stream_.AddDexRegisterEntry( 990 DexRegisterLocation::Kind::kInStack, offset + kVRegSize); 991 ++i; 992 DCHECK_LT(i, environment_size); 993 } 994 } else { 995 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id); 996 if (current->GetType() == Primitive::kPrimLong) { 997 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id); 998 ++i; 999 DCHECK_LT(i, environment_size); 1000 } 1001 } 1002 break; 1003 } 1004 1005 case Location::kFpuRegister : { 1006 int id = location.reg(); 1007 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) { 1008 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id); 1009 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); 1010 if (current->GetType() == Primitive::kPrimDouble) { 1011 stack_map_stream_.AddDexRegisterEntry( 1012 DexRegisterLocation::Kind::kInStack, offset + kVRegSize); 1013 ++i; 1014 DCHECK_LT(i, environment_size); 1015 } 1016 } else { 1017 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id); 1018 if (current->GetType() == Primitive::kPrimDouble) { 1019 stack_map_stream_.AddDexRegisterEntry( 1020 DexRegisterLocation::Kind::kInFpuRegisterHigh, id); 1021 ++i; 1022 DCHECK_LT(i, environment_size); 1023 } 1024 } 1025 break; 1026 } 1027 1028 case Location::kFpuRegisterPair : { 1029 int low = location.low(); 1030 int high = location.high(); 1031 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) { 1032 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low); 1033 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); 1034 } else { 1035 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low); 1036 } 1037 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) { 1038 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high); 1039 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); 1040 ++i; 1041 } else { 1042 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high); 1043 ++i; 1044 } 1045 DCHECK_LT(i, environment_size); 1046 break; 1047 } 1048 1049 case Location::kRegisterPair : { 1050 int low = location.low(); 1051 int high = location.high(); 1052 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) { 1053 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low); 1054 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); 1055 } else { 1056 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low); 1057 } 1058 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) { 1059 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high); 1060 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset); 1061 } else { 1062 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high); 1063 } 1064 ++i; 1065 DCHECK_LT(i, environment_size); 1066 break; 1067 } 1068 1069 case Location::kInvalid: { 1070 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0); 1071 break; 1072 } 1073 1074 default: 1075 LOG(FATAL) << "Unexpected kind " << location.GetKind(); 1076 } 1077 } 1078 1079 if (environment->GetParent() != nullptr) { 1080 stack_map_stream_.EndInlineInfoEntry(); 1081 } 1082} 1083 1084bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) { 1085 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves(); 1086 1087 return (first_next_not_move != nullptr) 1088 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0)); 1089} 1090 1091void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) { 1092 if (!compiler_options_.GetImplicitNullChecks()) { 1093 return; 1094 } 1095 1096 // If we are from a static path don't record the pc as we can't throw NPE. 1097 // NB: having the checks here makes the code much less verbose in the arch 1098 // specific code generators. 1099 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) { 1100 return; 1101 } 1102 1103 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) { 1104 return; 1105 } 1106 1107 // Find the first previous instruction which is not a move. 1108 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves(); 1109 1110 // If the instruction is a null check it means that `instr` is the first user 1111 // and needs to record the pc. 1112 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) { 1113 HNullCheck* null_check = first_prev_not_move->AsNullCheck(); 1114 // TODO: The parallel moves modify the environment. Their changes need to be 1115 // reverted otherwise the stack maps at the throw point will not be correct. 1116 RecordPcInfo(null_check, null_check->GetDexPc()); 1117 } 1118} 1119 1120LocationSummary* CodeGenerator::CreateNullCheckLocations(HNullCheck* null_check) { 1121 // Note: Using kNoCall allows the method to be treated as leaf (and eliminate the 1122 // HSuspendCheck from entry block). However, it will still get a valid stack frame 1123 // because the HNullCheck needs an environment. 1124 LocationSummary::CallKind call_kind = LocationSummary::kNoCall; 1125 // When throwing from a try block, we may need to retrieve dalvik registers from 1126 // physical registers and we also need to set up stack mask for GC. This is 1127 // implicitly achieved by passing kCallOnSlowPath to the LocationSummary. 1128 bool can_throw_into_catch_block = null_check->CanThrowIntoCatchBlock(); 1129 if (can_throw_into_catch_block) { 1130 call_kind = LocationSummary::kCallOnSlowPath; 1131 } 1132 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(null_check, call_kind); 1133 if (can_throw_into_catch_block && compiler_options_.GetImplicitNullChecks()) { 1134 locations->SetCustomSlowPathCallerSaves(RegisterSet()); // No caller-save registers. 1135 } 1136 locations->SetInAt(0, Location::RequiresRegister()); 1137 DCHECK(!null_check->HasUses()); 1138 return locations; 1139} 1140 1141void CodeGenerator::GenerateNullCheck(HNullCheck* instruction) { 1142 if (compiler_options_.GetImplicitNullChecks()) { 1143 MaybeRecordStat(kImplicitNullCheckGenerated); 1144 GenerateImplicitNullCheck(instruction); 1145 } else { 1146 MaybeRecordStat(kExplicitNullCheckGenerated); 1147 GenerateExplicitNullCheck(instruction); 1148 } 1149} 1150 1151void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const { 1152 LocationSummary* locations = suspend_check->GetLocations(); 1153 HBasicBlock* block = suspend_check->GetBlock(); 1154 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check); 1155 DCHECK(block->IsLoopHeader()); 1156 1157 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { 1158 HInstruction* current = it.Current(); 1159 LiveInterval* interval = current->GetLiveInterval(); 1160 // We only need to clear bits of loop phis containing objects and allocated in register. 1161 // Loop phis allocated on stack already have the object in the stack. 1162 if (current->GetType() == Primitive::kPrimNot 1163 && interval->HasRegister() 1164 && interval->HasSpillSlot()) { 1165 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize); 1166 } 1167 } 1168} 1169 1170void CodeGenerator::EmitParallelMoves(Location from1, 1171 Location to1, 1172 Primitive::Type type1, 1173 Location from2, 1174 Location to2, 1175 Primitive::Type type2) { 1176 HParallelMove parallel_move(GetGraph()->GetArena()); 1177 parallel_move.AddMove(from1, to1, type1, nullptr); 1178 parallel_move.AddMove(from2, to2, type2, nullptr); 1179 GetMoveResolver()->EmitNativeCode(¶llel_move); 1180} 1181 1182void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) { 1183 // Ensure that the call kind indication given to the register allocator is 1184 // coherent with the runtime call generated, and that the GC side effect is 1185 // set when required. 1186 if (slow_path == nullptr) { 1187 DCHECK(instruction->GetLocations()->WillCall()) 1188 << "instruction->DebugName()=" << instruction->DebugName(); 1189 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC())) 1190 << "instruction->DebugName()=" << instruction->DebugName() 1191 << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString(); 1192 } else { 1193 DCHECK(instruction->GetLocations()->CallsOnSlowPath() || slow_path->IsFatal()) 1194 << "instruction->DebugName()=" << instruction->DebugName() 1195 << " slow_path->GetDescription()=" << slow_path->GetDescription(); 1196 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) || 1197 // When (non-Baker) read barriers are enabled, some instructions 1198 // use a slow path to emit a read barrier, which does not trigger 1199 // GC. 1200 (kEmitCompilerReadBarrier && 1201 !kUseBakerReadBarrier && 1202 (instruction->IsInstanceFieldGet() || 1203 instruction->IsStaticFieldGet() || 1204 instruction->IsArrayGet() || 1205 instruction->IsLoadClass() || 1206 instruction->IsLoadString() || 1207 instruction->IsInstanceOf() || 1208 instruction->IsCheckCast() || 1209 (instruction->IsInvokeVirtual() && instruction->GetLocations()->Intrinsified())))) 1210 << "instruction->DebugName()=" << instruction->DebugName() 1211 << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString() 1212 << " slow_path->GetDescription()=" << slow_path->GetDescription(); 1213 } 1214 1215 // Check the coherency of leaf information. 1216 DCHECK(instruction->IsSuspendCheck() 1217 || ((slow_path != nullptr) && slow_path->IsFatal()) 1218 || instruction->GetLocations()->CanCall() 1219 || !IsLeafMethod()) 1220 << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : ""); 1221} 1222 1223void CodeGenerator::ValidateInvokeRuntimeWithoutRecordingPcInfo(HInstruction* instruction, 1224 SlowPathCode* slow_path) { 1225 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath()) 1226 << "instruction->DebugName()=" << instruction->DebugName() 1227 << " slow_path->GetDescription()=" << slow_path->GetDescription(); 1228 // Only the Baker read barrier marking slow path used by certains 1229 // instructions is expected to invoke the runtime without recording 1230 // PC-related information. 1231 DCHECK(kUseBakerReadBarrier); 1232 DCHECK(instruction->IsInstanceFieldGet() || 1233 instruction->IsStaticFieldGet() || 1234 instruction->IsArrayGet() || 1235 instruction->IsArraySet() || 1236 instruction->IsLoadClass() || 1237 instruction->IsLoadString() || 1238 instruction->IsInstanceOf() || 1239 instruction->IsCheckCast() || 1240 (instruction->IsInvokeVirtual() && instruction->GetLocations()->Intrinsified()) || 1241 (instruction->IsInvokeStaticOrDirect() && instruction->GetLocations()->Intrinsified())) 1242 << "instruction->DebugName()=" << instruction->DebugName() 1243 << " slow_path->GetDescription()=" << slow_path->GetDescription(); 1244} 1245 1246void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) { 1247 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath(); 1248 1249 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true); 1250 for (uint32_t i : LowToHighBits(core_spills)) { 1251 // If the register holds an object, update the stack mask. 1252 if (locations->RegisterContainsObject(i)) { 1253 locations->SetStackBit(stack_offset / kVRegSize); 1254 } 1255 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); 1256 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); 1257 saved_core_stack_offsets_[i] = stack_offset; 1258 stack_offset += codegen->SaveCoreRegister(stack_offset, i); 1259 } 1260 1261 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false); 1262 for (size_t i : LowToHighBits(fp_spills)) { 1263 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); 1264 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); 1265 saved_fpu_stack_offsets_[i] = stack_offset; 1266 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i); 1267 } 1268} 1269 1270void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) { 1271 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath(); 1272 1273 const uint32_t core_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ true); 1274 for (uint32_t i : LowToHighBits(core_spills)) { 1275 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); 1276 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); 1277 stack_offset += codegen->RestoreCoreRegister(stack_offset, i); 1278 } 1279 1280 const uint32_t fp_spills = codegen->GetSlowPathSpills(locations, /* core_registers */ false); 1281 for (size_t i : LowToHighBits(fp_spills)) { 1282 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize()); 1283 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters); 1284 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i); 1285 } 1286} 1287 1288void CodeGenerator::CreateSystemArrayCopyLocationSummary(HInvoke* invoke) { 1289 // Check to see if we have known failures that will cause us to have to bail out 1290 // to the runtime, and just generate the runtime call directly. 1291 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant(); 1292 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant(); 1293 1294 // The positions must be non-negative. 1295 if ((src_pos != nullptr && src_pos->GetValue() < 0) || 1296 (dest_pos != nullptr && dest_pos->GetValue() < 0)) { 1297 // We will have to fail anyways. 1298 return; 1299 } 1300 1301 // The length must be >= 0. 1302 HIntConstant* length = invoke->InputAt(4)->AsIntConstant(); 1303 if (length != nullptr) { 1304 int32_t len = length->GetValue(); 1305 if (len < 0) { 1306 // Just call as normal. 1307 return; 1308 } 1309 } 1310 1311 SystemArrayCopyOptimizations optimizations(invoke); 1312 1313 if (optimizations.GetDestinationIsSource()) { 1314 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) { 1315 // We only support backward copying if source and destination are the same. 1316 return; 1317 } 1318 } 1319 1320 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) { 1321 // We currently don't intrinsify primitive copying. 1322 return; 1323 } 1324 1325 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena(); 1326 LocationSummary* locations = new (allocator) LocationSummary(invoke, 1327 LocationSummary::kCallOnSlowPath, 1328 kIntrinsified); 1329 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length). 1330 locations->SetInAt(0, Location::RequiresRegister()); 1331 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1))); 1332 locations->SetInAt(2, Location::RequiresRegister()); 1333 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3))); 1334 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4))); 1335 1336 locations->AddTemp(Location::RequiresRegister()); 1337 locations->AddTemp(Location::RequiresRegister()); 1338 locations->AddTemp(Location::RequiresRegister()); 1339} 1340 1341uint32_t CodeGenerator::GetReferenceSlowFlagOffset() const { 1342 ScopedObjectAccess soa(Thread::Current()); 1343 mirror::Class* klass = mirror::Reference::GetJavaLangRefReference(); 1344 DCHECK(klass->IsInitialized()); 1345 return klass->GetSlowPathFlagOffset().Uint32Value(); 1346} 1347 1348uint32_t CodeGenerator::GetReferenceDisableFlagOffset() const { 1349 ScopedObjectAccess soa(Thread::Current()); 1350 mirror::Class* klass = mirror::Reference::GetJavaLangRefReference(); 1351 DCHECK(klass->IsInitialized()); 1352 return klass->GetDisableIntrinsicFlagOffset().Uint32Value(); 1353} 1354 1355} // namespace art 1356