code_generator_arm.cc revision b8b97695d178337736b61609220613b92f344d45
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_arm.h" 18 19#include "arch/arm/instruction_set_features_arm.h" 20#include "art_method.h" 21#include "code_generator_utils.h" 22#include "compiled_method.h" 23#include "entrypoints/quick/quick_entrypoints.h" 24#include "gc/accounting/card_table.h" 25#include "intrinsics.h" 26#include "intrinsics_arm.h" 27#include "mirror/array-inl.h" 28#include "mirror/class-inl.h" 29#include "thread.h" 30#include "utils/arm/assembler_arm.h" 31#include "utils/arm/managed_register_arm.h" 32#include "utils/assembler.h" 33#include "utils/stack_checks.h" 34 35namespace art { 36 37namespace arm { 38 39static bool ExpectedPairLayout(Location location) { 40 // We expected this for both core and fpu register pairs. 41 return ((location.low() & 1) == 0) && (location.low() + 1 == location.high()); 42} 43 44static constexpr int kCurrentMethodStackOffset = 0; 45static constexpr Register kMethodRegisterArgument = R0; 46 47// We unconditionally allocate R5 to ensure we can do long operations 48// with baseline. 49static constexpr Register kCoreSavedRegisterForBaseline = R5; 50static constexpr Register kCoreCalleeSaves[] = 51 { R5, R6, R7, R8, R10, R11, LR }; 52static constexpr SRegister kFpuCalleeSaves[] = 53 { S16, S17, S18, S19, S20, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31 }; 54 55// D31 cannot be split into two S registers, and the register allocator only works on 56// S registers. Therefore there is no need to block it. 57static constexpr DRegister DTMP = D31; 58 59static constexpr uint32_t kPackedSwitchJumpTableThreshold = 6; 60 61#define __ down_cast<ArmAssembler*>(codegen->GetAssembler())-> 62#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kArmWordSize, x).Int32Value() 63 64class NullCheckSlowPathARM : public SlowPathCode { 65 public: 66 explicit NullCheckSlowPathARM(HNullCheck* instruction) : instruction_(instruction) {} 67 68 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { 69 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen); 70 __ Bind(GetEntryLabel()); 71 if (instruction_->CanThrowIntoCatchBlock()) { 72 // Live registers will be restored in the catch block if caught. 73 SaveLiveRegisters(codegen, instruction_->GetLocations()); 74 } 75 arm_codegen->InvokeRuntime( 76 QUICK_ENTRY_POINT(pThrowNullPointer), instruction_, instruction_->GetDexPc(), this); 77 } 78 79 bool IsFatal() const OVERRIDE { return true; } 80 81 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathARM"; } 82 83 private: 84 HNullCheck* const instruction_; 85 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathARM); 86}; 87 88class DivZeroCheckSlowPathARM : public SlowPathCode { 89 public: 90 explicit DivZeroCheckSlowPathARM(HDivZeroCheck* instruction) : instruction_(instruction) {} 91 92 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { 93 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen); 94 __ Bind(GetEntryLabel()); 95 if (instruction_->CanThrowIntoCatchBlock()) { 96 // Live registers will be restored in the catch block if caught. 97 SaveLiveRegisters(codegen, instruction_->GetLocations()); 98 } 99 arm_codegen->InvokeRuntime( 100 QUICK_ENTRY_POINT(pThrowDivZero), instruction_, instruction_->GetDexPc(), this); 101 } 102 103 bool IsFatal() const OVERRIDE { return true; } 104 105 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathARM"; } 106 107 private: 108 HDivZeroCheck* const instruction_; 109 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathARM); 110}; 111 112class SuspendCheckSlowPathARM : public SlowPathCode { 113 public: 114 SuspendCheckSlowPathARM(HSuspendCheck* instruction, HBasicBlock* successor) 115 : instruction_(instruction), successor_(successor) {} 116 117 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { 118 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen); 119 __ Bind(GetEntryLabel()); 120 SaveLiveRegisters(codegen, instruction_->GetLocations()); 121 arm_codegen->InvokeRuntime( 122 QUICK_ENTRY_POINT(pTestSuspend), instruction_, instruction_->GetDexPc(), this); 123 RestoreLiveRegisters(codegen, instruction_->GetLocations()); 124 if (successor_ == nullptr) { 125 __ b(GetReturnLabel()); 126 } else { 127 __ b(arm_codegen->GetLabelOf(successor_)); 128 } 129 } 130 131 Label* GetReturnLabel() { 132 DCHECK(successor_ == nullptr); 133 return &return_label_; 134 } 135 136 HBasicBlock* GetSuccessor() const { 137 return successor_; 138 } 139 140 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathARM"; } 141 142 private: 143 HSuspendCheck* const instruction_; 144 // If not null, the block to branch to after the suspend check. 145 HBasicBlock* const successor_; 146 147 // If `successor_` is null, the label to branch to after the suspend check. 148 Label return_label_; 149 150 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathARM); 151}; 152 153class BoundsCheckSlowPathARM : public SlowPathCode { 154 public: 155 explicit BoundsCheckSlowPathARM(HBoundsCheck* instruction) 156 : instruction_(instruction) {} 157 158 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { 159 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen); 160 LocationSummary* locations = instruction_->GetLocations(); 161 162 __ Bind(GetEntryLabel()); 163 if (instruction_->CanThrowIntoCatchBlock()) { 164 // Live registers will be restored in the catch block if caught. 165 SaveLiveRegisters(codegen, instruction_->GetLocations()); 166 } 167 // We're moving two locations to locations that could overlap, so we need a parallel 168 // move resolver. 169 InvokeRuntimeCallingConvention calling_convention; 170 codegen->EmitParallelMoves( 171 locations->InAt(0), 172 Location::RegisterLocation(calling_convention.GetRegisterAt(0)), 173 Primitive::kPrimInt, 174 locations->InAt(1), 175 Location::RegisterLocation(calling_convention.GetRegisterAt(1)), 176 Primitive::kPrimInt); 177 arm_codegen->InvokeRuntime( 178 QUICK_ENTRY_POINT(pThrowArrayBounds), instruction_, instruction_->GetDexPc(), this); 179 } 180 181 bool IsFatal() const OVERRIDE { return true; } 182 183 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathARM"; } 184 185 private: 186 HBoundsCheck* const instruction_; 187 188 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathARM); 189}; 190 191class LoadClassSlowPathARM : public SlowPathCode { 192 public: 193 LoadClassSlowPathARM(HLoadClass* cls, 194 HInstruction* at, 195 uint32_t dex_pc, 196 bool do_clinit) 197 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) { 198 DCHECK(at->IsLoadClass() || at->IsClinitCheck()); 199 } 200 201 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { 202 LocationSummary* locations = at_->GetLocations(); 203 204 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen); 205 __ Bind(GetEntryLabel()); 206 SaveLiveRegisters(codegen, locations); 207 208 InvokeRuntimeCallingConvention calling_convention; 209 __ LoadImmediate(calling_convention.GetRegisterAt(0), cls_->GetTypeIndex()); 210 int32_t entry_point_offset = do_clinit_ 211 ? QUICK_ENTRY_POINT(pInitializeStaticStorage) 212 : QUICK_ENTRY_POINT(pInitializeType); 213 arm_codegen->InvokeRuntime(entry_point_offset, at_, dex_pc_, this); 214 215 // Move the class to the desired location. 216 Location out = locations->Out(); 217 if (out.IsValid()) { 218 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg())); 219 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0)); 220 } 221 RestoreLiveRegisters(codegen, locations); 222 __ b(GetExitLabel()); 223 } 224 225 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathARM"; } 226 227 private: 228 // The class this slow path will load. 229 HLoadClass* const cls_; 230 231 // The instruction where this slow path is happening. 232 // (Might be the load class or an initialization check). 233 HInstruction* const at_; 234 235 // The dex PC of `at_`. 236 const uint32_t dex_pc_; 237 238 // Whether to initialize the class. 239 const bool do_clinit_; 240 241 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathARM); 242}; 243 244class LoadStringSlowPathARM : public SlowPathCode { 245 public: 246 explicit LoadStringSlowPathARM(HLoadString* instruction) : instruction_(instruction) {} 247 248 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { 249 LocationSummary* locations = instruction_->GetLocations(); 250 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg())); 251 252 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen); 253 __ Bind(GetEntryLabel()); 254 SaveLiveRegisters(codegen, locations); 255 256 InvokeRuntimeCallingConvention calling_convention; 257 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction_->GetStringIndex()); 258 arm_codegen->InvokeRuntime( 259 QUICK_ENTRY_POINT(pResolveString), instruction_, instruction_->GetDexPc(), this); 260 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0)); 261 262 RestoreLiveRegisters(codegen, locations); 263 __ b(GetExitLabel()); 264 } 265 266 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathARM"; } 267 268 private: 269 HLoadString* const instruction_; 270 271 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathARM); 272}; 273 274class TypeCheckSlowPathARM : public SlowPathCode { 275 public: 276 TypeCheckSlowPathARM(HInstruction* instruction, bool is_fatal) 277 : instruction_(instruction), is_fatal_(is_fatal) {} 278 279 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { 280 LocationSummary* locations = instruction_->GetLocations(); 281 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0) 282 : locations->Out(); 283 DCHECK(instruction_->IsCheckCast() 284 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg())); 285 286 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen); 287 __ Bind(GetEntryLabel()); 288 289 if (instruction_->IsCheckCast()) { 290 // The codegen for the instruction overwrites `temp`, so put it back in place. 291 Register obj = locations->InAt(0).AsRegister<Register>(); 292 Register temp = locations->GetTemp(0).AsRegister<Register>(); 293 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); 294 __ LoadFromOffset(kLoadWord, temp, obj, class_offset); 295 __ MaybeUnpoisonHeapReference(temp); 296 } 297 298 if (!is_fatal_) { 299 SaveLiveRegisters(codegen, locations); 300 } 301 302 // We're moving two locations to locations that could overlap, so we need a parallel 303 // move resolver. 304 InvokeRuntimeCallingConvention calling_convention; 305 codegen->EmitParallelMoves( 306 locations->InAt(1), 307 Location::RegisterLocation(calling_convention.GetRegisterAt(0)), 308 Primitive::kPrimNot, 309 object_class, 310 Location::RegisterLocation(calling_convention.GetRegisterAt(1)), 311 Primitive::kPrimNot); 312 313 if (instruction_->IsInstanceOf()) { 314 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial), 315 instruction_, 316 instruction_->GetDexPc(), 317 this); 318 arm_codegen->Move32(locations->Out(), Location::RegisterLocation(R0)); 319 } else { 320 DCHECK(instruction_->IsCheckCast()); 321 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), 322 instruction_, 323 instruction_->GetDexPc(), 324 this); 325 } 326 327 if (!is_fatal_) { 328 RestoreLiveRegisters(codegen, locations); 329 __ b(GetExitLabel()); 330 } 331 } 332 333 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathARM"; } 334 335 bool IsFatal() const OVERRIDE { return is_fatal_; } 336 337 private: 338 HInstruction* const instruction_; 339 const bool is_fatal_; 340 341 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathARM); 342}; 343 344class DeoptimizationSlowPathARM : public SlowPathCode { 345 public: 346 explicit DeoptimizationSlowPathARM(HInstruction* instruction) 347 : instruction_(instruction) {} 348 349 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { 350 __ Bind(GetEntryLabel()); 351 SaveLiveRegisters(codegen, instruction_->GetLocations()); 352 DCHECK(instruction_->IsDeoptimize()); 353 HDeoptimize* deoptimize = instruction_->AsDeoptimize(); 354 uint32_t dex_pc = deoptimize->GetDexPc(); 355 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen); 356 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize), instruction_, dex_pc, this); 357 } 358 359 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathARM"; } 360 361 private: 362 HInstruction* const instruction_; 363 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathARM); 364}; 365 366class ArraySetSlowPathARM : public SlowPathCode { 367 public: 368 explicit ArraySetSlowPathARM(HInstruction* instruction) : instruction_(instruction) {} 369 370 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE { 371 LocationSummary* locations = instruction_->GetLocations(); 372 __ Bind(GetEntryLabel()); 373 SaveLiveRegisters(codegen, locations); 374 375 InvokeRuntimeCallingConvention calling_convention; 376 HParallelMove parallel_move(codegen->GetGraph()->GetArena()); 377 parallel_move.AddMove( 378 locations->InAt(0), 379 Location::RegisterLocation(calling_convention.GetRegisterAt(0)), 380 Primitive::kPrimNot, 381 nullptr); 382 parallel_move.AddMove( 383 locations->InAt(1), 384 Location::RegisterLocation(calling_convention.GetRegisterAt(1)), 385 Primitive::kPrimInt, 386 nullptr); 387 parallel_move.AddMove( 388 locations->InAt(2), 389 Location::RegisterLocation(calling_convention.GetRegisterAt(2)), 390 Primitive::kPrimNot, 391 nullptr); 392 codegen->GetMoveResolver()->EmitNativeCode(¶llel_move); 393 394 CodeGeneratorARM* arm_codegen = down_cast<CodeGeneratorARM*>(codegen); 395 arm_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject), 396 instruction_, 397 instruction_->GetDexPc(), 398 this); 399 RestoreLiveRegisters(codegen, locations); 400 __ b(GetExitLabel()); 401 } 402 403 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathARM"; } 404 405 private: 406 HInstruction* const instruction_; 407 408 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathARM); 409}; 410 411#undef __ 412#define __ down_cast<ArmAssembler*>(GetAssembler())-> 413 414inline Condition ARMCondition(IfCondition cond) { 415 switch (cond) { 416 case kCondEQ: return EQ; 417 case kCondNE: return NE; 418 case kCondLT: return LT; 419 case kCondLE: return LE; 420 case kCondGT: return GT; 421 case kCondGE: return GE; 422 case kCondB: return LO; 423 case kCondBE: return LS; 424 case kCondA: return HI; 425 case kCondAE: return HS; 426 } 427 LOG(FATAL) << "Unreachable"; 428 UNREACHABLE(); 429} 430 431// Maps signed condition to unsigned condition. 432inline Condition ARMUnsignedCondition(IfCondition cond) { 433 switch (cond) { 434 case kCondEQ: return EQ; 435 case kCondNE: return NE; 436 // Signed to unsigned. 437 case kCondLT: return LO; 438 case kCondLE: return LS; 439 case kCondGT: return HI; 440 case kCondGE: return HS; 441 // Unsigned remain unchanged. 442 case kCondB: return LO; 443 case kCondBE: return LS; 444 case kCondA: return HI; 445 case kCondAE: return HS; 446 } 447 LOG(FATAL) << "Unreachable"; 448 UNREACHABLE(); 449} 450 451void CodeGeneratorARM::DumpCoreRegister(std::ostream& stream, int reg) const { 452 stream << Register(reg); 453} 454 455void CodeGeneratorARM::DumpFloatingPointRegister(std::ostream& stream, int reg) const { 456 stream << SRegister(reg); 457} 458 459size_t CodeGeneratorARM::SaveCoreRegister(size_t stack_index, uint32_t reg_id) { 460 __ StoreToOffset(kStoreWord, static_cast<Register>(reg_id), SP, stack_index); 461 return kArmWordSize; 462} 463 464size_t CodeGeneratorARM::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) { 465 __ LoadFromOffset(kLoadWord, static_cast<Register>(reg_id), SP, stack_index); 466 return kArmWordSize; 467} 468 469size_t CodeGeneratorARM::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) { 470 __ StoreSToOffset(static_cast<SRegister>(reg_id), SP, stack_index); 471 return kArmWordSize; 472} 473 474size_t CodeGeneratorARM::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) { 475 __ LoadSFromOffset(static_cast<SRegister>(reg_id), SP, stack_index); 476 return kArmWordSize; 477} 478 479CodeGeneratorARM::CodeGeneratorARM(HGraph* graph, 480 const ArmInstructionSetFeatures& isa_features, 481 const CompilerOptions& compiler_options, 482 OptimizingCompilerStats* stats) 483 : CodeGenerator(graph, 484 kNumberOfCoreRegisters, 485 kNumberOfSRegisters, 486 kNumberOfRegisterPairs, 487 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves), 488 arraysize(kCoreCalleeSaves)), 489 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves), 490 arraysize(kFpuCalleeSaves)), 491 compiler_options, 492 stats), 493 block_labels_(nullptr), 494 location_builder_(graph, this), 495 instruction_visitor_(graph, this), 496 move_resolver_(graph->GetArena(), this), 497 assembler_(), 498 isa_features_(isa_features), 499 method_patches_(MethodReferenceComparator(), 500 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)), 501 call_patches_(MethodReferenceComparator(), 502 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)), 503 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) { 504 // Always save the LR register to mimic Quick. 505 AddAllocatedRegister(Location::RegisterLocation(LR)); 506} 507 508void CodeGeneratorARM::Finalize(CodeAllocator* allocator) { 509 // Ensure that we fix up branches and literal loads and emit the literal pool. 510 __ FinalizeCode(); 511 512 // Adjust native pc offsets in stack maps. 513 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) { 514 uint32_t old_position = stack_map_stream_.GetStackMap(i).native_pc_offset; 515 uint32_t new_position = __ GetAdjustedPosition(old_position); 516 stack_map_stream_.SetStackMapNativePcOffset(i, new_position); 517 } 518 // Adjust pc offsets for the disassembly information. 519 if (disasm_info_ != nullptr) { 520 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval(); 521 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start); 522 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end); 523 for (auto& it : *disasm_info_->GetInstructionIntervals()) { 524 it.second.start = __ GetAdjustedPosition(it.second.start); 525 it.second.end = __ GetAdjustedPosition(it.second.end); 526 } 527 for (auto& it : *disasm_info_->GetSlowPathIntervals()) { 528 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start); 529 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end); 530 } 531 } 532 533 CodeGenerator::Finalize(allocator); 534} 535 536Location CodeGeneratorARM::AllocateFreeRegister(Primitive::Type type) const { 537 switch (type) { 538 case Primitive::kPrimLong: { 539 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs); 540 ArmManagedRegister pair = 541 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg)); 542 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]); 543 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]); 544 545 blocked_core_registers_[pair.AsRegisterPairLow()] = true; 546 blocked_core_registers_[pair.AsRegisterPairHigh()] = true; 547 UpdateBlockedPairRegisters(); 548 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh()); 549 } 550 551 case Primitive::kPrimByte: 552 case Primitive::kPrimBoolean: 553 case Primitive::kPrimChar: 554 case Primitive::kPrimShort: 555 case Primitive::kPrimInt: 556 case Primitive::kPrimNot: { 557 int reg = FindFreeEntry(blocked_core_registers_, kNumberOfCoreRegisters); 558 // Block all register pairs that contain `reg`. 559 for (int i = 0; i < kNumberOfRegisterPairs; i++) { 560 ArmManagedRegister current = 561 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i)); 562 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) { 563 blocked_register_pairs_[i] = true; 564 } 565 } 566 return Location::RegisterLocation(reg); 567 } 568 569 case Primitive::kPrimFloat: { 570 int reg = FindFreeEntry(blocked_fpu_registers_, kNumberOfSRegisters); 571 return Location::FpuRegisterLocation(reg); 572 } 573 574 case Primitive::kPrimDouble: { 575 int reg = FindTwoFreeConsecutiveAlignedEntries(blocked_fpu_registers_, kNumberOfSRegisters); 576 DCHECK_EQ(reg % 2, 0); 577 return Location::FpuRegisterPairLocation(reg, reg + 1); 578 } 579 580 case Primitive::kPrimVoid: 581 LOG(FATAL) << "Unreachable type " << type; 582 } 583 584 return Location(); 585} 586 587void CodeGeneratorARM::SetupBlockedRegisters(bool is_baseline) const { 588 // Don't allocate the dalvik style register pair passing. 589 blocked_register_pairs_[R1_R2] = true; 590 591 // Stack register, LR and PC are always reserved. 592 blocked_core_registers_[SP] = true; 593 blocked_core_registers_[LR] = true; 594 blocked_core_registers_[PC] = true; 595 596 // Reserve thread register. 597 blocked_core_registers_[TR] = true; 598 599 // Reserve temp register. 600 blocked_core_registers_[IP] = true; 601 602 if (is_baseline) { 603 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) { 604 blocked_core_registers_[kCoreCalleeSaves[i]] = true; 605 } 606 607 blocked_core_registers_[kCoreSavedRegisterForBaseline] = false; 608 } 609 610 if (is_baseline || GetGraph()->IsDebuggable()) { 611 // Stubs do not save callee-save floating point registers. If the graph 612 // is debuggable, we need to deal with these registers differently. For 613 // now, just block them. 614 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) { 615 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true; 616 } 617 } 618 619 UpdateBlockedPairRegisters(); 620} 621 622void CodeGeneratorARM::UpdateBlockedPairRegisters() const { 623 for (int i = 0; i < kNumberOfRegisterPairs; i++) { 624 ArmManagedRegister current = 625 ArmManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i)); 626 if (blocked_core_registers_[current.AsRegisterPairLow()] 627 || blocked_core_registers_[current.AsRegisterPairHigh()]) { 628 blocked_register_pairs_[i] = true; 629 } 630 } 631} 632 633InstructionCodeGeneratorARM::InstructionCodeGeneratorARM(HGraph* graph, CodeGeneratorARM* codegen) 634 : HGraphVisitor(graph), 635 assembler_(codegen->GetAssembler()), 636 codegen_(codegen) {} 637 638void CodeGeneratorARM::ComputeSpillMask() { 639 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_; 640 // Save one extra register for baseline. Note that on thumb2, there is no easy 641 // instruction to restore just the PC, so this actually helps both baseline 642 // and non-baseline to save and restore at least two registers at entry and exit. 643 core_spill_mask_ |= (1 << kCoreSavedRegisterForBaseline); 644 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved"; 645 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_; 646 // We use vpush and vpop for saving and restoring floating point registers, which take 647 // a SRegister and the number of registers to save/restore after that SRegister. We 648 // therefore update the `fpu_spill_mask_` to also contain those registers not allocated, 649 // but in the range. 650 if (fpu_spill_mask_ != 0) { 651 uint32_t least_significant_bit = LeastSignificantBit(fpu_spill_mask_); 652 uint32_t most_significant_bit = MostSignificantBit(fpu_spill_mask_); 653 for (uint32_t i = least_significant_bit + 1 ; i < most_significant_bit; ++i) { 654 fpu_spill_mask_ |= (1 << i); 655 } 656 } 657} 658 659static dwarf::Reg DWARFReg(Register reg) { 660 return dwarf::Reg::ArmCore(static_cast<int>(reg)); 661} 662 663static dwarf::Reg DWARFReg(SRegister reg) { 664 return dwarf::Reg::ArmFp(static_cast<int>(reg)); 665} 666 667void CodeGeneratorARM::GenerateFrameEntry() { 668 bool skip_overflow_check = 669 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kArm); 670 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks()); 671 __ Bind(&frame_entry_label_); 672 673 if (HasEmptyFrame()) { 674 return; 675 } 676 677 if (!skip_overflow_check) { 678 __ AddConstant(IP, SP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kArm))); 679 __ LoadFromOffset(kLoadWord, IP, IP, 0); 680 RecordPcInfo(nullptr, 0); 681 } 682 683 __ PushList(core_spill_mask_); 684 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(core_spill_mask_)); 685 __ cfi().RelOffsetForMany(DWARFReg(kMethodRegisterArgument), 0, core_spill_mask_, kArmWordSize); 686 if (fpu_spill_mask_ != 0) { 687 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_)); 688 __ vpushs(start_register, POPCOUNT(fpu_spill_mask_)); 689 __ cfi().AdjustCFAOffset(kArmWordSize * POPCOUNT(fpu_spill_mask_)); 690 __ cfi().RelOffsetForMany(DWARFReg(S0), 0, fpu_spill_mask_, kArmWordSize); 691 } 692 int adjust = GetFrameSize() - FrameEntrySpillSize(); 693 __ AddConstant(SP, -adjust); 694 __ cfi().AdjustCFAOffset(adjust); 695 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, 0); 696} 697 698void CodeGeneratorARM::GenerateFrameExit() { 699 if (HasEmptyFrame()) { 700 __ bx(LR); 701 return; 702 } 703 __ cfi().RememberState(); 704 int adjust = GetFrameSize() - FrameEntrySpillSize(); 705 __ AddConstant(SP, adjust); 706 __ cfi().AdjustCFAOffset(-adjust); 707 if (fpu_spill_mask_ != 0) { 708 SRegister start_register = SRegister(LeastSignificantBit(fpu_spill_mask_)); 709 __ vpops(start_register, POPCOUNT(fpu_spill_mask_)); 710 __ cfi().AdjustCFAOffset(-kArmPointerSize * POPCOUNT(fpu_spill_mask_)); 711 __ cfi().RestoreMany(DWARFReg(SRegister(0)), fpu_spill_mask_); 712 } 713 // Pop LR into PC to return. 714 DCHECK_NE(core_spill_mask_ & (1 << LR), 0U); 715 uint32_t pop_mask = (core_spill_mask_ & (~(1 << LR))) | 1 << PC; 716 __ PopList(pop_mask); 717 __ cfi().RestoreState(); 718 __ cfi().DefCFAOffset(GetFrameSize()); 719} 720 721void CodeGeneratorARM::Bind(HBasicBlock* block) { 722 Label* label = GetLabelOf(block); 723 __ BindTrackedLabel(label); 724} 725 726Location CodeGeneratorARM::GetStackLocation(HLoadLocal* load) const { 727 switch (load->GetType()) { 728 case Primitive::kPrimLong: 729 case Primitive::kPrimDouble: 730 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal())); 731 732 case Primitive::kPrimInt: 733 case Primitive::kPrimNot: 734 case Primitive::kPrimFloat: 735 return Location::StackSlot(GetStackSlot(load->GetLocal())); 736 737 case Primitive::kPrimBoolean: 738 case Primitive::kPrimByte: 739 case Primitive::kPrimChar: 740 case Primitive::kPrimShort: 741 case Primitive::kPrimVoid: 742 LOG(FATAL) << "Unexpected type " << load->GetType(); 743 UNREACHABLE(); 744 } 745 746 LOG(FATAL) << "Unreachable"; 747 UNREACHABLE(); 748} 749 750Location InvokeDexCallingConventionVisitorARM::GetNextLocation(Primitive::Type type) { 751 switch (type) { 752 case Primitive::kPrimBoolean: 753 case Primitive::kPrimByte: 754 case Primitive::kPrimChar: 755 case Primitive::kPrimShort: 756 case Primitive::kPrimInt: 757 case Primitive::kPrimNot: { 758 uint32_t index = gp_index_++; 759 uint32_t stack_index = stack_index_++; 760 if (index < calling_convention.GetNumberOfRegisters()) { 761 return Location::RegisterLocation(calling_convention.GetRegisterAt(index)); 762 } else { 763 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index)); 764 } 765 } 766 767 case Primitive::kPrimLong: { 768 uint32_t index = gp_index_; 769 uint32_t stack_index = stack_index_; 770 gp_index_ += 2; 771 stack_index_ += 2; 772 if (index + 1 < calling_convention.GetNumberOfRegisters()) { 773 if (calling_convention.GetRegisterAt(index) == R1) { 774 // Skip R1, and use R2_R3 instead. 775 gp_index_++; 776 index++; 777 } 778 } 779 if (index + 1 < calling_convention.GetNumberOfRegisters()) { 780 DCHECK_EQ(calling_convention.GetRegisterAt(index) + 1, 781 calling_convention.GetRegisterAt(index + 1)); 782 783 return Location::RegisterPairLocation(calling_convention.GetRegisterAt(index), 784 calling_convention.GetRegisterAt(index + 1)); 785 } else { 786 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index)); 787 } 788 } 789 790 case Primitive::kPrimFloat: { 791 uint32_t stack_index = stack_index_++; 792 if (float_index_ % 2 == 0) { 793 float_index_ = std::max(double_index_, float_index_); 794 } 795 if (float_index_ < calling_convention.GetNumberOfFpuRegisters()) { 796 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(float_index_++)); 797 } else { 798 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index)); 799 } 800 } 801 802 case Primitive::kPrimDouble: { 803 double_index_ = std::max(double_index_, RoundUp(float_index_, 2)); 804 uint32_t stack_index = stack_index_; 805 stack_index_ += 2; 806 if (double_index_ + 1 < calling_convention.GetNumberOfFpuRegisters()) { 807 uint32_t index = double_index_; 808 double_index_ += 2; 809 Location result = Location::FpuRegisterPairLocation( 810 calling_convention.GetFpuRegisterAt(index), 811 calling_convention.GetFpuRegisterAt(index + 1)); 812 DCHECK(ExpectedPairLayout(result)); 813 return result; 814 } else { 815 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index)); 816 } 817 } 818 819 case Primitive::kPrimVoid: 820 LOG(FATAL) << "Unexpected parameter type " << type; 821 break; 822 } 823 return Location(); 824} 825 826Location InvokeDexCallingConventionVisitorARM::GetReturnLocation(Primitive::Type type) const { 827 switch (type) { 828 case Primitive::kPrimBoolean: 829 case Primitive::kPrimByte: 830 case Primitive::kPrimChar: 831 case Primitive::kPrimShort: 832 case Primitive::kPrimInt: 833 case Primitive::kPrimNot: { 834 return Location::RegisterLocation(R0); 835 } 836 837 case Primitive::kPrimFloat: { 838 return Location::FpuRegisterLocation(S0); 839 } 840 841 case Primitive::kPrimLong: { 842 return Location::RegisterPairLocation(R0, R1); 843 } 844 845 case Primitive::kPrimDouble: { 846 return Location::FpuRegisterPairLocation(S0, S1); 847 } 848 849 case Primitive::kPrimVoid: 850 return Location(); 851 } 852 853 UNREACHABLE(); 854} 855 856Location InvokeDexCallingConventionVisitorARM::GetMethodLocation() const { 857 return Location::RegisterLocation(kMethodRegisterArgument); 858} 859 860void CodeGeneratorARM::Move32(Location destination, Location source) { 861 if (source.Equals(destination)) { 862 return; 863 } 864 if (destination.IsRegister()) { 865 if (source.IsRegister()) { 866 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>()); 867 } else if (source.IsFpuRegister()) { 868 __ vmovrs(destination.AsRegister<Register>(), source.AsFpuRegister<SRegister>()); 869 } else { 870 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex()); 871 } 872 } else if (destination.IsFpuRegister()) { 873 if (source.IsRegister()) { 874 __ vmovsr(destination.AsFpuRegister<SRegister>(), source.AsRegister<Register>()); 875 } else if (source.IsFpuRegister()) { 876 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>()); 877 } else { 878 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex()); 879 } 880 } else { 881 DCHECK(destination.IsStackSlot()) << destination; 882 if (source.IsRegister()) { 883 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex()); 884 } else if (source.IsFpuRegister()) { 885 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex()); 886 } else { 887 DCHECK(source.IsStackSlot()) << source; 888 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex()); 889 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex()); 890 } 891 } 892} 893 894void CodeGeneratorARM::Move64(Location destination, Location source) { 895 if (source.Equals(destination)) { 896 return; 897 } 898 if (destination.IsRegisterPair()) { 899 if (source.IsRegisterPair()) { 900 EmitParallelMoves( 901 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()), 902 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()), 903 Primitive::kPrimInt, 904 Location::RegisterLocation(source.AsRegisterPairLow<Register>()), 905 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()), 906 Primitive::kPrimInt); 907 } else if (source.IsFpuRegister()) { 908 UNIMPLEMENTED(FATAL); 909 } else if (source.IsFpuRegisterPair()) { 910 __ vmovrrd(destination.AsRegisterPairLow<Register>(), 911 destination.AsRegisterPairHigh<Register>(), 912 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())); 913 } else { 914 DCHECK(source.IsDoubleStackSlot()); 915 DCHECK(ExpectedPairLayout(destination)); 916 __ LoadFromOffset(kLoadWordPair, destination.AsRegisterPairLow<Register>(), 917 SP, source.GetStackIndex()); 918 } 919 } else if (destination.IsFpuRegisterPair()) { 920 if (source.IsDoubleStackSlot()) { 921 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), 922 SP, 923 source.GetStackIndex()); 924 } else if (source.IsRegisterPair()) { 925 __ vmovdrr(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), 926 source.AsRegisterPairLow<Register>(), 927 source.AsRegisterPairHigh<Register>()); 928 } else { 929 UNIMPLEMENTED(FATAL); 930 } 931 } else { 932 DCHECK(destination.IsDoubleStackSlot()); 933 if (source.IsRegisterPair()) { 934 // No conflict possible, so just do the moves. 935 if (source.AsRegisterPairLow<Register>() == R1) { 936 DCHECK_EQ(source.AsRegisterPairHigh<Register>(), R2); 937 __ StoreToOffset(kStoreWord, R1, SP, destination.GetStackIndex()); 938 __ StoreToOffset(kStoreWord, R2, SP, destination.GetHighStackIndex(kArmWordSize)); 939 } else { 940 __ StoreToOffset(kStoreWordPair, source.AsRegisterPairLow<Register>(), 941 SP, destination.GetStackIndex()); 942 } 943 } else if (source.IsFpuRegisterPair()) { 944 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()), 945 SP, 946 destination.GetStackIndex()); 947 } else { 948 DCHECK(source.IsDoubleStackSlot()); 949 EmitParallelMoves( 950 Location::StackSlot(source.GetStackIndex()), 951 Location::StackSlot(destination.GetStackIndex()), 952 Primitive::kPrimInt, 953 Location::StackSlot(source.GetHighStackIndex(kArmWordSize)), 954 Location::StackSlot(destination.GetHighStackIndex(kArmWordSize)), 955 Primitive::kPrimInt); 956 } 957 } 958} 959 960void CodeGeneratorARM::Move(HInstruction* instruction, Location location, HInstruction* move_for) { 961 LocationSummary* locations = instruction->GetLocations(); 962 if (instruction->IsCurrentMethod()) { 963 Move32(location, Location::StackSlot(kCurrentMethodStackOffset)); 964 } else if (locations != nullptr && locations->Out().Equals(location)) { 965 return; 966 } else if (locations != nullptr && locations->Out().IsConstant()) { 967 HConstant* const_to_move = locations->Out().GetConstant(); 968 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) { 969 int32_t value = GetInt32ValueOf(const_to_move); 970 if (location.IsRegister()) { 971 __ LoadImmediate(location.AsRegister<Register>(), value); 972 } else { 973 DCHECK(location.IsStackSlot()); 974 __ LoadImmediate(IP, value); 975 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex()); 976 } 977 } else { 978 DCHECK(const_to_move->IsLongConstant()) << const_to_move->DebugName(); 979 int64_t value = const_to_move->AsLongConstant()->GetValue(); 980 if (location.IsRegisterPair()) { 981 __ LoadImmediate(location.AsRegisterPairLow<Register>(), Low32Bits(value)); 982 __ LoadImmediate(location.AsRegisterPairHigh<Register>(), High32Bits(value)); 983 } else { 984 DCHECK(location.IsDoubleStackSlot()); 985 __ LoadImmediate(IP, Low32Bits(value)); 986 __ StoreToOffset(kStoreWord, IP, SP, location.GetStackIndex()); 987 __ LoadImmediate(IP, High32Bits(value)); 988 __ StoreToOffset(kStoreWord, IP, SP, location.GetHighStackIndex(kArmWordSize)); 989 } 990 } 991 } else if (instruction->IsLoadLocal()) { 992 uint32_t stack_slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal()); 993 switch (instruction->GetType()) { 994 case Primitive::kPrimBoolean: 995 case Primitive::kPrimByte: 996 case Primitive::kPrimChar: 997 case Primitive::kPrimShort: 998 case Primitive::kPrimInt: 999 case Primitive::kPrimNot: 1000 case Primitive::kPrimFloat: 1001 Move32(location, Location::StackSlot(stack_slot)); 1002 break; 1003 1004 case Primitive::kPrimLong: 1005 case Primitive::kPrimDouble: 1006 Move64(location, Location::DoubleStackSlot(stack_slot)); 1007 break; 1008 1009 default: 1010 LOG(FATAL) << "Unexpected type " << instruction->GetType(); 1011 } 1012 } else if (instruction->IsTemporary()) { 1013 Location temp_location = GetTemporaryLocation(instruction->AsTemporary()); 1014 if (temp_location.IsStackSlot()) { 1015 Move32(location, temp_location); 1016 } else { 1017 DCHECK(temp_location.IsDoubleStackSlot()); 1018 Move64(location, temp_location); 1019 } 1020 } else { 1021 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary()); 1022 switch (instruction->GetType()) { 1023 case Primitive::kPrimBoolean: 1024 case Primitive::kPrimByte: 1025 case Primitive::kPrimChar: 1026 case Primitive::kPrimShort: 1027 case Primitive::kPrimNot: 1028 case Primitive::kPrimInt: 1029 case Primitive::kPrimFloat: 1030 Move32(location, locations->Out()); 1031 break; 1032 1033 case Primitive::kPrimLong: 1034 case Primitive::kPrimDouble: 1035 Move64(location, locations->Out()); 1036 break; 1037 1038 default: 1039 LOG(FATAL) << "Unexpected type " << instruction->GetType(); 1040 } 1041 } 1042} 1043 1044void CodeGeneratorARM::MoveConstant(Location location, int32_t value) { 1045 DCHECK(location.IsRegister()); 1046 __ LoadImmediate(location.AsRegister<Register>(), value); 1047} 1048 1049void CodeGeneratorARM::MoveLocation(Location dst, Location src, Primitive::Type dst_type) { 1050 if (Primitive::Is64BitType(dst_type)) { 1051 Move64(dst, src); 1052 } else { 1053 Move32(dst, src); 1054 } 1055} 1056 1057void CodeGeneratorARM::AddLocationAsTemp(Location location, LocationSummary* locations) { 1058 if (location.IsRegister()) { 1059 locations->AddTemp(location); 1060 } else if (location.IsRegisterPair()) { 1061 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>())); 1062 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>())); 1063 } else { 1064 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location; 1065 } 1066} 1067 1068void CodeGeneratorARM::InvokeRuntime(QuickEntrypointEnum entrypoint, 1069 HInstruction* instruction, 1070 uint32_t dex_pc, 1071 SlowPathCode* slow_path) { 1072 InvokeRuntime(GetThreadOffset<kArmWordSize>(entrypoint).Int32Value(), 1073 instruction, 1074 dex_pc, 1075 slow_path); 1076} 1077 1078void CodeGeneratorARM::InvokeRuntime(int32_t entry_point_offset, 1079 HInstruction* instruction, 1080 uint32_t dex_pc, 1081 SlowPathCode* slow_path) { 1082 ValidateInvokeRuntime(instruction, slow_path); 1083 __ LoadFromOffset(kLoadWord, LR, TR, entry_point_offset); 1084 __ blx(LR); 1085 RecordPcInfo(instruction, dex_pc, slow_path); 1086} 1087 1088void InstructionCodeGeneratorARM::HandleGoto(HInstruction* got, HBasicBlock* successor) { 1089 DCHECK(!successor->IsExitBlock()); 1090 1091 HBasicBlock* block = got->GetBlock(); 1092 HInstruction* previous = got->GetPrevious(); 1093 1094 HLoopInformation* info = block->GetLoopInformation(); 1095 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) { 1096 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck()); 1097 GenerateSuspendCheck(info->GetSuspendCheck(), successor); 1098 return; 1099 } 1100 1101 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) { 1102 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr); 1103 } 1104 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) { 1105 __ b(codegen_->GetLabelOf(successor)); 1106 } 1107} 1108 1109void LocationsBuilderARM::VisitGoto(HGoto* got) { 1110 got->SetLocations(nullptr); 1111} 1112 1113void InstructionCodeGeneratorARM::VisitGoto(HGoto* got) { 1114 HandleGoto(got, got->GetSuccessor()); 1115} 1116 1117void LocationsBuilderARM::VisitTryBoundary(HTryBoundary* try_boundary) { 1118 try_boundary->SetLocations(nullptr); 1119} 1120 1121void InstructionCodeGeneratorARM::VisitTryBoundary(HTryBoundary* try_boundary) { 1122 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor(); 1123 if (!successor->IsExitBlock()) { 1124 HandleGoto(try_boundary, successor); 1125 } 1126} 1127 1128void LocationsBuilderARM::VisitExit(HExit* exit) { 1129 exit->SetLocations(nullptr); 1130} 1131 1132void InstructionCodeGeneratorARM::VisitExit(HExit* exit ATTRIBUTE_UNUSED) { 1133} 1134 1135void InstructionCodeGeneratorARM::GenerateCompareWithImmediate(Register left, int32_t right) { 1136 ShifterOperand operand; 1137 if (GetAssembler()->ShifterOperandCanHold(R0, left, CMP, right, &operand)) { 1138 __ cmp(left, operand); 1139 } else { 1140 Register temp = IP; 1141 __ LoadImmediate(temp, right); 1142 __ cmp(left, ShifterOperand(temp)); 1143 } 1144} 1145 1146void InstructionCodeGeneratorARM::GenerateFPJumps(HCondition* cond, 1147 Label* true_label, 1148 Label* false_label) { 1149 __ vmstat(); // transfer FP status register to ARM APSR. 1150 // TODO: merge into a single branch (except "equal or unordered" and "not equal") 1151 if (cond->IsFPConditionTrueIfNaN()) { 1152 __ b(true_label, VS); // VS for unordered. 1153 } else if (cond->IsFPConditionFalseIfNaN()) { 1154 __ b(false_label, VS); // VS for unordered. 1155 } 1156 __ b(true_label, ARMCondition(cond->GetCondition())); 1157} 1158 1159void InstructionCodeGeneratorARM::GenerateLongComparesAndJumps(HCondition* cond, 1160 Label* true_label, 1161 Label* false_label) { 1162 LocationSummary* locations = cond->GetLocations(); 1163 Location left = locations->InAt(0); 1164 Location right = locations->InAt(1); 1165 IfCondition if_cond = cond->GetCondition(); 1166 1167 Register left_high = left.AsRegisterPairHigh<Register>(); 1168 Register left_low = left.AsRegisterPairLow<Register>(); 1169 IfCondition true_high_cond = if_cond; 1170 IfCondition false_high_cond = cond->GetOppositeCondition(); 1171 Condition final_condition = ARMUnsignedCondition(if_cond); // unsigned on lower part 1172 1173 // Set the conditions for the test, remembering that == needs to be 1174 // decided using the low words. 1175 // TODO: consider avoiding jumps with temporary and CMP low+SBC high 1176 switch (if_cond) { 1177 case kCondEQ: 1178 case kCondNE: 1179 // Nothing to do. 1180 break; 1181 case kCondLT: 1182 false_high_cond = kCondGT; 1183 break; 1184 case kCondLE: 1185 true_high_cond = kCondLT; 1186 break; 1187 case kCondGT: 1188 false_high_cond = kCondLT; 1189 break; 1190 case kCondGE: 1191 true_high_cond = kCondGT; 1192 break; 1193 case kCondB: 1194 false_high_cond = kCondA; 1195 break; 1196 case kCondBE: 1197 true_high_cond = kCondB; 1198 break; 1199 case kCondA: 1200 false_high_cond = kCondB; 1201 break; 1202 case kCondAE: 1203 true_high_cond = kCondA; 1204 break; 1205 } 1206 if (right.IsConstant()) { 1207 int64_t value = right.GetConstant()->AsLongConstant()->GetValue(); 1208 int32_t val_low = Low32Bits(value); 1209 int32_t val_high = High32Bits(value); 1210 1211 GenerateCompareWithImmediate(left_high, val_high); 1212 if (if_cond == kCondNE) { 1213 __ b(true_label, ARMCondition(true_high_cond)); 1214 } else if (if_cond == kCondEQ) { 1215 __ b(false_label, ARMCondition(false_high_cond)); 1216 } else { 1217 __ b(true_label, ARMCondition(true_high_cond)); 1218 __ b(false_label, ARMCondition(false_high_cond)); 1219 } 1220 // Must be equal high, so compare the lows. 1221 GenerateCompareWithImmediate(left_low, val_low); 1222 } else { 1223 Register right_high = right.AsRegisterPairHigh<Register>(); 1224 Register right_low = right.AsRegisterPairLow<Register>(); 1225 1226 __ cmp(left_high, ShifterOperand(right_high)); 1227 if (if_cond == kCondNE) { 1228 __ b(true_label, ARMCondition(true_high_cond)); 1229 } else if (if_cond == kCondEQ) { 1230 __ b(false_label, ARMCondition(false_high_cond)); 1231 } else { 1232 __ b(true_label, ARMCondition(true_high_cond)); 1233 __ b(false_label, ARMCondition(false_high_cond)); 1234 } 1235 // Must be equal high, so compare the lows. 1236 __ cmp(left_low, ShifterOperand(right_low)); 1237 } 1238 // The last comparison might be unsigned. 1239 // TODO: optimize cases where this is always true/false 1240 __ b(true_label, final_condition); 1241} 1242 1243void InstructionCodeGeneratorARM::GenerateCompareTestAndBranch(HIf* if_instr, 1244 HCondition* condition, 1245 Label* true_target, 1246 Label* false_target, 1247 Label* always_true_target) { 1248 LocationSummary* locations = condition->GetLocations(); 1249 Location left = locations->InAt(0); 1250 Location right = locations->InAt(1); 1251 1252 // We don't want true_target as a nullptr. 1253 if (true_target == nullptr) { 1254 true_target = always_true_target; 1255 } 1256 bool falls_through = (false_target == nullptr); 1257 1258 // FP compares don't like null false_targets. 1259 if (false_target == nullptr) { 1260 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor()); 1261 } 1262 1263 Primitive::Type type = condition->InputAt(0)->GetType(); 1264 switch (type) { 1265 case Primitive::kPrimLong: 1266 GenerateLongComparesAndJumps(condition, true_target, false_target); 1267 break; 1268 case Primitive::kPrimFloat: 1269 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>()); 1270 GenerateFPJumps(condition, true_target, false_target); 1271 break; 1272 case Primitive::kPrimDouble: 1273 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()), 1274 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>())); 1275 GenerateFPJumps(condition, true_target, false_target); 1276 break; 1277 default: 1278 LOG(FATAL) << "Unexpected compare type " << type; 1279 } 1280 1281 if (!falls_through) { 1282 __ b(false_target); 1283 } 1284} 1285 1286void InstructionCodeGeneratorARM::GenerateTestAndBranch(HInstruction* instruction, 1287 Label* true_target, 1288 Label* false_target, 1289 Label* always_true_target) { 1290 HInstruction* cond = instruction->InputAt(0); 1291 if (cond->IsIntConstant()) { 1292 // Constant condition, statically compared against 1. 1293 int32_t cond_value = cond->AsIntConstant()->GetValue(); 1294 if (cond_value == 1) { 1295 if (always_true_target != nullptr) { 1296 __ b(always_true_target); 1297 } 1298 return; 1299 } else { 1300 DCHECK_EQ(cond_value, 0); 1301 } 1302 } else { 1303 // Can we optimize the jump if we know that the next block is the true case? 1304 HCondition* condition = cond->AsCondition(); 1305 bool can_jump_to_false = CanReverseCondition(always_true_target, false_target, condition); 1306 if (condition == nullptr || condition->NeedsMaterialization()) { 1307 // Condition has been materialized, compare the output to 0. 1308 DCHECK(instruction->GetLocations()->InAt(0).IsRegister()); 1309 if (can_jump_to_false) { 1310 __ CompareAndBranchIfZero(instruction->GetLocations()->InAt(0).AsRegister<Register>(), 1311 false_target); 1312 return; 1313 } 1314 __ CompareAndBranchIfNonZero(instruction->GetLocations()->InAt(0).AsRegister<Register>(), 1315 true_target); 1316 } else { 1317 // Condition has not been materialized, use its inputs as the 1318 // comparison and its condition as the branch condition. 1319 Primitive::Type type = (condition != nullptr) 1320 ? cond->InputAt(0)->GetType() 1321 : Primitive::kPrimInt; 1322 // Is this a long or FP comparison that has been folded into the HCondition? 1323 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) { 1324 // Generate the comparison directly. 1325 GenerateCompareTestAndBranch(instruction->AsIf(), condition, 1326 true_target, false_target, always_true_target); 1327 return; 1328 } 1329 1330 LocationSummary* locations = cond->GetLocations(); 1331 DCHECK(locations->InAt(0).IsRegister()) << locations->InAt(0); 1332 Register left = locations->InAt(0).AsRegister<Register>(); 1333 Location right = locations->InAt(1); 1334 if (right.IsRegister()) { 1335 __ cmp(left, ShifterOperand(right.AsRegister<Register>())); 1336 } else { 1337 DCHECK(right.IsConstant()); 1338 GenerateCompareWithImmediate(left, CodeGenerator::GetInt32ValueOf(right.GetConstant())); 1339 } 1340 if (can_jump_to_false) { 1341 __ b(false_target, ARMCondition(condition->GetOppositeCondition())); 1342 return; 1343 } 1344 1345 __ b(true_target, ARMCondition(condition->GetCondition())); 1346 } 1347 } 1348 if (false_target != nullptr) { 1349 __ b(false_target); 1350 } 1351} 1352 1353void LocationsBuilderARM::VisitIf(HIf* if_instr) { 1354 LocationSummary* locations = 1355 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall); 1356 HInstruction* cond = if_instr->InputAt(0); 1357 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) { 1358 locations->SetInAt(0, Location::RequiresRegister()); 1359 } 1360} 1361 1362void InstructionCodeGeneratorARM::VisitIf(HIf* if_instr) { 1363 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor()); 1364 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor()); 1365 Label* always_true_target = true_target; 1366 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), 1367 if_instr->IfTrueSuccessor())) { 1368 always_true_target = nullptr; 1369 } 1370 if (codegen_->GoesToNextBlock(if_instr->GetBlock(), 1371 if_instr->IfFalseSuccessor())) { 1372 false_target = nullptr; 1373 } 1374 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target); 1375} 1376 1377void LocationsBuilderARM::VisitDeoptimize(HDeoptimize* deoptimize) { 1378 LocationSummary* locations = new (GetGraph()->GetArena()) 1379 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath); 1380 HInstruction* cond = deoptimize->InputAt(0); 1381 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) { 1382 locations->SetInAt(0, Location::RequiresRegister()); 1383 } 1384} 1385 1386void InstructionCodeGeneratorARM::VisitDeoptimize(HDeoptimize* deoptimize) { 1387 SlowPathCode* slow_path = new (GetGraph()->GetArena()) 1388 DeoptimizationSlowPathARM(deoptimize); 1389 codegen_->AddSlowPath(slow_path); 1390 Label* slow_path_entry = slow_path->GetEntryLabel(); 1391 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry); 1392} 1393 1394void LocationsBuilderARM::VisitCondition(HCondition* cond) { 1395 LocationSummary* locations = 1396 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall); 1397 // Handle the long/FP comparisons made in instruction simplification. 1398 switch (cond->InputAt(0)->GetType()) { 1399 case Primitive::kPrimLong: 1400 locations->SetInAt(0, Location::RequiresRegister()); 1401 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1))); 1402 if (cond->NeedsMaterialization()) { 1403 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap); 1404 } 1405 break; 1406 1407 case Primitive::kPrimFloat: 1408 case Primitive::kPrimDouble: 1409 locations->SetInAt(0, Location::RequiresFpuRegister()); 1410 locations->SetInAt(1, Location::RequiresFpuRegister()); 1411 if (cond->NeedsMaterialization()) { 1412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 1413 } 1414 break; 1415 1416 default: 1417 locations->SetInAt(0, Location::RequiresRegister()); 1418 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1))); 1419 if (cond->NeedsMaterialization()) { 1420 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 1421 } 1422 } 1423} 1424 1425void InstructionCodeGeneratorARM::VisitCondition(HCondition* cond) { 1426 if (!cond->NeedsMaterialization()) { 1427 return; 1428 } 1429 1430 LocationSummary* locations = cond->GetLocations(); 1431 Location left = locations->InAt(0); 1432 Location right = locations->InAt(1); 1433 Register out = locations->Out().AsRegister<Register>(); 1434 Label true_label, false_label; 1435 1436 switch (cond->InputAt(0)->GetType()) { 1437 default: { 1438 // Integer case. 1439 if (right.IsRegister()) { 1440 __ cmp(left.AsRegister<Register>(), ShifterOperand(right.AsRegister<Register>())); 1441 } else { 1442 DCHECK(right.IsConstant()); 1443 GenerateCompareWithImmediate(left.AsRegister<Register>(), 1444 CodeGenerator::GetInt32ValueOf(right.GetConstant())); 1445 } 1446 __ it(ARMCondition(cond->GetCondition()), kItElse); 1447 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(1), 1448 ARMCondition(cond->GetCondition())); 1449 __ mov(locations->Out().AsRegister<Register>(), ShifterOperand(0), 1450 ARMCondition(cond->GetOppositeCondition())); 1451 return; 1452 } 1453 case Primitive::kPrimLong: 1454 GenerateLongComparesAndJumps(cond, &true_label, &false_label); 1455 break; 1456 case Primitive::kPrimFloat: 1457 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>()); 1458 GenerateFPJumps(cond, &true_label, &false_label); 1459 break; 1460 case Primitive::kPrimDouble: 1461 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()), 1462 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>())); 1463 GenerateFPJumps(cond, &true_label, &false_label); 1464 break; 1465 } 1466 1467 // Convert the jumps into the result. 1468 Label done_label; 1469 1470 // False case: result = 0. 1471 __ Bind(&false_label); 1472 __ LoadImmediate(out, 0); 1473 __ b(&done_label); 1474 1475 // True case: result = 1. 1476 __ Bind(&true_label); 1477 __ LoadImmediate(out, 1); 1478 __ Bind(&done_label); 1479} 1480 1481void LocationsBuilderARM::VisitEqual(HEqual* comp) { 1482 VisitCondition(comp); 1483} 1484 1485void InstructionCodeGeneratorARM::VisitEqual(HEqual* comp) { 1486 VisitCondition(comp); 1487} 1488 1489void LocationsBuilderARM::VisitNotEqual(HNotEqual* comp) { 1490 VisitCondition(comp); 1491} 1492 1493void InstructionCodeGeneratorARM::VisitNotEqual(HNotEqual* comp) { 1494 VisitCondition(comp); 1495} 1496 1497void LocationsBuilderARM::VisitLessThan(HLessThan* comp) { 1498 VisitCondition(comp); 1499} 1500 1501void InstructionCodeGeneratorARM::VisitLessThan(HLessThan* comp) { 1502 VisitCondition(comp); 1503} 1504 1505void LocationsBuilderARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) { 1506 VisitCondition(comp); 1507} 1508 1509void InstructionCodeGeneratorARM::VisitLessThanOrEqual(HLessThanOrEqual* comp) { 1510 VisitCondition(comp); 1511} 1512 1513void LocationsBuilderARM::VisitGreaterThan(HGreaterThan* comp) { 1514 VisitCondition(comp); 1515} 1516 1517void InstructionCodeGeneratorARM::VisitGreaterThan(HGreaterThan* comp) { 1518 VisitCondition(comp); 1519} 1520 1521void LocationsBuilderARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) { 1522 VisitCondition(comp); 1523} 1524 1525void InstructionCodeGeneratorARM::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) { 1526 VisitCondition(comp); 1527} 1528 1529void LocationsBuilderARM::VisitBelow(HBelow* comp) { 1530 VisitCondition(comp); 1531} 1532 1533void InstructionCodeGeneratorARM::VisitBelow(HBelow* comp) { 1534 VisitCondition(comp); 1535} 1536 1537void LocationsBuilderARM::VisitBelowOrEqual(HBelowOrEqual* comp) { 1538 VisitCondition(comp); 1539} 1540 1541void InstructionCodeGeneratorARM::VisitBelowOrEqual(HBelowOrEqual* comp) { 1542 VisitCondition(comp); 1543} 1544 1545void LocationsBuilderARM::VisitAbove(HAbove* comp) { 1546 VisitCondition(comp); 1547} 1548 1549void InstructionCodeGeneratorARM::VisitAbove(HAbove* comp) { 1550 VisitCondition(comp); 1551} 1552 1553void LocationsBuilderARM::VisitAboveOrEqual(HAboveOrEqual* comp) { 1554 VisitCondition(comp); 1555} 1556 1557void InstructionCodeGeneratorARM::VisitAboveOrEqual(HAboveOrEqual* comp) { 1558 VisitCondition(comp); 1559} 1560 1561void LocationsBuilderARM::VisitLocal(HLocal* local) { 1562 local->SetLocations(nullptr); 1563} 1564 1565void InstructionCodeGeneratorARM::VisitLocal(HLocal* local) { 1566 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock()); 1567} 1568 1569void LocationsBuilderARM::VisitLoadLocal(HLoadLocal* load) { 1570 load->SetLocations(nullptr); 1571} 1572 1573void InstructionCodeGeneratorARM::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) { 1574 // Nothing to do, this is driven by the code generator. 1575} 1576 1577void LocationsBuilderARM::VisitStoreLocal(HStoreLocal* store) { 1578 LocationSummary* locations = 1579 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall); 1580 switch (store->InputAt(1)->GetType()) { 1581 case Primitive::kPrimBoolean: 1582 case Primitive::kPrimByte: 1583 case Primitive::kPrimChar: 1584 case Primitive::kPrimShort: 1585 case Primitive::kPrimInt: 1586 case Primitive::kPrimNot: 1587 case Primitive::kPrimFloat: 1588 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal()))); 1589 break; 1590 1591 case Primitive::kPrimLong: 1592 case Primitive::kPrimDouble: 1593 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal()))); 1594 break; 1595 1596 default: 1597 LOG(FATAL) << "Unexpected local type " << store->InputAt(1)->GetType(); 1598 } 1599} 1600 1601void InstructionCodeGeneratorARM::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) { 1602} 1603 1604void LocationsBuilderARM::VisitIntConstant(HIntConstant* constant) { 1605 LocationSummary* locations = 1606 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall); 1607 locations->SetOut(Location::ConstantLocation(constant)); 1608} 1609 1610void InstructionCodeGeneratorARM::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) { 1611 // Will be generated at use site. 1612} 1613 1614void LocationsBuilderARM::VisitNullConstant(HNullConstant* constant) { 1615 LocationSummary* locations = 1616 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall); 1617 locations->SetOut(Location::ConstantLocation(constant)); 1618} 1619 1620void InstructionCodeGeneratorARM::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) { 1621 // Will be generated at use site. 1622} 1623 1624void LocationsBuilderARM::VisitLongConstant(HLongConstant* constant) { 1625 LocationSummary* locations = 1626 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall); 1627 locations->SetOut(Location::ConstantLocation(constant)); 1628} 1629 1630void InstructionCodeGeneratorARM::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) { 1631 // Will be generated at use site. 1632} 1633 1634void LocationsBuilderARM::VisitFloatConstant(HFloatConstant* constant) { 1635 LocationSummary* locations = 1636 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall); 1637 locations->SetOut(Location::ConstantLocation(constant)); 1638} 1639 1640void InstructionCodeGeneratorARM::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) { 1641 // Will be generated at use site. 1642} 1643 1644void LocationsBuilderARM::VisitDoubleConstant(HDoubleConstant* constant) { 1645 LocationSummary* locations = 1646 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall); 1647 locations->SetOut(Location::ConstantLocation(constant)); 1648} 1649 1650void InstructionCodeGeneratorARM::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) { 1651 // Will be generated at use site. 1652} 1653 1654void LocationsBuilderARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) { 1655 memory_barrier->SetLocations(nullptr); 1656} 1657 1658void InstructionCodeGeneratorARM::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) { 1659 GenerateMemoryBarrier(memory_barrier->GetBarrierKind()); 1660} 1661 1662void LocationsBuilderARM::VisitReturnVoid(HReturnVoid* ret) { 1663 ret->SetLocations(nullptr); 1664} 1665 1666void InstructionCodeGeneratorARM::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) { 1667 codegen_->GenerateFrameExit(); 1668} 1669 1670void LocationsBuilderARM::VisitReturn(HReturn* ret) { 1671 LocationSummary* locations = 1672 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall); 1673 locations->SetInAt(0, parameter_visitor_.GetReturnLocation(ret->InputAt(0)->GetType())); 1674} 1675 1676void InstructionCodeGeneratorARM::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) { 1677 codegen_->GenerateFrameExit(); 1678} 1679 1680void LocationsBuilderARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) { 1681 // The trampoline uses the same calling convention as dex calling conventions, 1682 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain 1683 // the method_idx. 1684 HandleInvoke(invoke); 1685} 1686 1687void InstructionCodeGeneratorARM::VisitInvokeUnresolved(HInvokeUnresolved* invoke) { 1688 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke); 1689} 1690 1691void LocationsBuilderARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) { 1692 // When we do not run baseline, explicit clinit checks triggered by static 1693 // invokes must have been pruned by art::PrepareForRegisterAllocation. 1694 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck()); 1695 1696 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(), 1697 codegen_->GetAssembler(), 1698 codegen_->GetInstructionSetFeatures()); 1699 if (intrinsic.TryDispatch(invoke)) { 1700 return; 1701 } 1702 1703 HandleInvoke(invoke); 1704} 1705 1706static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorARM* codegen) { 1707 if (invoke->GetLocations()->Intrinsified()) { 1708 IntrinsicCodeGeneratorARM intrinsic(codegen); 1709 intrinsic.Dispatch(invoke); 1710 return true; 1711 } 1712 return false; 1713} 1714 1715void InstructionCodeGeneratorARM::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) { 1716 // When we do not run baseline, explicit clinit checks triggered by static 1717 // invokes must have been pruned by art::PrepareForRegisterAllocation. 1718 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck()); 1719 1720 if (TryGenerateIntrinsicCode(invoke, codegen_)) { 1721 return; 1722 } 1723 1724 LocationSummary* locations = invoke->GetLocations(); 1725 codegen_->GenerateStaticOrDirectCall( 1726 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation()); 1727 codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); 1728} 1729 1730void LocationsBuilderARM::HandleInvoke(HInvoke* invoke) { 1731 InvokeDexCallingConventionVisitorARM calling_convention_visitor; 1732 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor); 1733} 1734 1735void LocationsBuilderARM::VisitInvokeVirtual(HInvokeVirtual* invoke) { 1736 IntrinsicLocationsBuilderARM intrinsic(GetGraph()->GetArena(), 1737 codegen_->GetAssembler(), 1738 codegen_->GetInstructionSetFeatures()); 1739 if (intrinsic.TryDispatch(invoke)) { 1740 return; 1741 } 1742 1743 HandleInvoke(invoke); 1744} 1745 1746void InstructionCodeGeneratorARM::VisitInvokeVirtual(HInvokeVirtual* invoke) { 1747 if (TryGenerateIntrinsicCode(invoke, codegen_)) { 1748 return; 1749 } 1750 1751 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0)); 1752 DCHECK(!codegen_->IsLeafMethod()); 1753 codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); 1754} 1755 1756void LocationsBuilderARM::VisitInvokeInterface(HInvokeInterface* invoke) { 1757 HandleInvoke(invoke); 1758 // Add the hidden argument. 1759 invoke->GetLocations()->AddTemp(Location::RegisterLocation(R12)); 1760} 1761 1762void InstructionCodeGeneratorARM::VisitInvokeInterface(HInvokeInterface* invoke) { 1763 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError. 1764 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>(); 1765 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset( 1766 invoke->GetImtIndex() % mirror::Class::kImtSize, kArmPointerSize).Uint32Value(); 1767 LocationSummary* locations = invoke->GetLocations(); 1768 Location receiver = locations->InAt(0); 1769 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); 1770 1771 // Set the hidden argument. 1772 __ LoadImmediate(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(), 1773 invoke->GetDexMethodIndex()); 1774 1775 // temp = object->GetClass(); 1776 if (receiver.IsStackSlot()) { 1777 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex()); 1778 __ LoadFromOffset(kLoadWord, temp, temp, class_offset); 1779 } else { 1780 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset); 1781 } 1782 codegen_->MaybeRecordImplicitNullCheck(invoke); 1783 __ MaybeUnpoisonHeapReference(temp); 1784 // temp = temp->GetImtEntryAt(method_offset); 1785 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset( 1786 kArmWordSize).Int32Value(); 1787 __ LoadFromOffset(kLoadWord, temp, temp, method_offset); 1788 // LR = temp->GetEntryPoint(); 1789 __ LoadFromOffset(kLoadWord, LR, temp, entry_point); 1790 // LR(); 1791 __ blx(LR); 1792 DCHECK(!codegen_->IsLeafMethod()); 1793 codegen_->RecordPcInfo(invoke, invoke->GetDexPc()); 1794} 1795 1796void LocationsBuilderARM::VisitNeg(HNeg* neg) { 1797 LocationSummary* locations = 1798 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall); 1799 switch (neg->GetResultType()) { 1800 case Primitive::kPrimInt: { 1801 locations->SetInAt(0, Location::RequiresRegister()); 1802 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 1803 break; 1804 } 1805 case Primitive::kPrimLong: { 1806 locations->SetInAt(0, Location::RequiresRegister()); 1807 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap); 1808 break; 1809 } 1810 1811 case Primitive::kPrimFloat: 1812 case Primitive::kPrimDouble: 1813 locations->SetInAt(0, Location::RequiresFpuRegister()); 1814 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); 1815 break; 1816 1817 default: 1818 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType(); 1819 } 1820} 1821 1822void InstructionCodeGeneratorARM::VisitNeg(HNeg* neg) { 1823 LocationSummary* locations = neg->GetLocations(); 1824 Location out = locations->Out(); 1825 Location in = locations->InAt(0); 1826 switch (neg->GetResultType()) { 1827 case Primitive::kPrimInt: 1828 DCHECK(in.IsRegister()); 1829 __ rsb(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(0)); 1830 break; 1831 1832 case Primitive::kPrimLong: 1833 DCHECK(in.IsRegisterPair()); 1834 // out.lo = 0 - in.lo (and update the carry/borrow (C) flag) 1835 __ rsbs(out.AsRegisterPairLow<Register>(), 1836 in.AsRegisterPairLow<Register>(), 1837 ShifterOperand(0)); 1838 // We cannot emit an RSC (Reverse Subtract with Carry) 1839 // instruction here, as it does not exist in the Thumb-2 1840 // instruction set. We use the following approach 1841 // using SBC and SUB instead. 1842 // 1843 // out.hi = -C 1844 __ sbc(out.AsRegisterPairHigh<Register>(), 1845 out.AsRegisterPairHigh<Register>(), 1846 ShifterOperand(out.AsRegisterPairHigh<Register>())); 1847 // out.hi = out.hi - in.hi 1848 __ sub(out.AsRegisterPairHigh<Register>(), 1849 out.AsRegisterPairHigh<Register>(), 1850 ShifterOperand(in.AsRegisterPairHigh<Register>())); 1851 break; 1852 1853 case Primitive::kPrimFloat: 1854 DCHECK(in.IsFpuRegister()); 1855 __ vnegs(out.AsFpuRegister<SRegister>(), in.AsFpuRegister<SRegister>()); 1856 break; 1857 1858 case Primitive::kPrimDouble: 1859 DCHECK(in.IsFpuRegisterPair()); 1860 __ vnegd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), 1861 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>())); 1862 break; 1863 1864 default: 1865 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType(); 1866 } 1867} 1868 1869void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) { 1870 Primitive::Type result_type = conversion->GetResultType(); 1871 Primitive::Type input_type = conversion->GetInputType(); 1872 DCHECK_NE(result_type, input_type); 1873 1874 // The float-to-long, double-to-long and long-to-float type conversions 1875 // rely on a call to the runtime. 1876 LocationSummary::CallKind call_kind = 1877 (((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble) 1878 && result_type == Primitive::kPrimLong) 1879 || (input_type == Primitive::kPrimLong && result_type == Primitive::kPrimFloat)) 1880 ? LocationSummary::kCall 1881 : LocationSummary::kNoCall; 1882 LocationSummary* locations = 1883 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind); 1884 1885 // The Java language does not allow treating boolean as an integral type but 1886 // our bit representation makes it safe. 1887 1888 switch (result_type) { 1889 case Primitive::kPrimByte: 1890 switch (input_type) { 1891 case Primitive::kPrimBoolean: 1892 // Boolean input is a result of code transformations. 1893 case Primitive::kPrimShort: 1894 case Primitive::kPrimInt: 1895 case Primitive::kPrimChar: 1896 // Processing a Dex `int-to-byte' instruction. 1897 locations->SetInAt(0, Location::RequiresRegister()); 1898 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 1899 break; 1900 1901 default: 1902 LOG(FATAL) << "Unexpected type conversion from " << input_type 1903 << " to " << result_type; 1904 } 1905 break; 1906 1907 case Primitive::kPrimShort: 1908 switch (input_type) { 1909 case Primitive::kPrimBoolean: 1910 // Boolean input is a result of code transformations. 1911 case Primitive::kPrimByte: 1912 case Primitive::kPrimInt: 1913 case Primitive::kPrimChar: 1914 // Processing a Dex `int-to-short' instruction. 1915 locations->SetInAt(0, Location::RequiresRegister()); 1916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 1917 break; 1918 1919 default: 1920 LOG(FATAL) << "Unexpected type conversion from " << input_type 1921 << " to " << result_type; 1922 } 1923 break; 1924 1925 case Primitive::kPrimInt: 1926 switch (input_type) { 1927 case Primitive::kPrimLong: 1928 // Processing a Dex `long-to-int' instruction. 1929 locations->SetInAt(0, Location::Any()); 1930 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 1931 break; 1932 1933 case Primitive::kPrimFloat: 1934 // Processing a Dex `float-to-int' instruction. 1935 locations->SetInAt(0, Location::RequiresFpuRegister()); 1936 locations->SetOut(Location::RequiresRegister()); 1937 locations->AddTemp(Location::RequiresFpuRegister()); 1938 break; 1939 1940 case Primitive::kPrimDouble: 1941 // Processing a Dex `double-to-int' instruction. 1942 locations->SetInAt(0, Location::RequiresFpuRegister()); 1943 locations->SetOut(Location::RequiresRegister()); 1944 locations->AddTemp(Location::RequiresFpuRegister()); 1945 break; 1946 1947 default: 1948 LOG(FATAL) << "Unexpected type conversion from " << input_type 1949 << " to " << result_type; 1950 } 1951 break; 1952 1953 case Primitive::kPrimLong: 1954 switch (input_type) { 1955 case Primitive::kPrimBoolean: 1956 // Boolean input is a result of code transformations. 1957 case Primitive::kPrimByte: 1958 case Primitive::kPrimShort: 1959 case Primitive::kPrimInt: 1960 case Primitive::kPrimChar: 1961 // Processing a Dex `int-to-long' instruction. 1962 locations->SetInAt(0, Location::RequiresRegister()); 1963 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 1964 break; 1965 1966 case Primitive::kPrimFloat: { 1967 // Processing a Dex `float-to-long' instruction. 1968 InvokeRuntimeCallingConvention calling_convention; 1969 locations->SetInAt(0, Location::FpuRegisterLocation( 1970 calling_convention.GetFpuRegisterAt(0))); 1971 locations->SetOut(Location::RegisterPairLocation(R0, R1)); 1972 break; 1973 } 1974 1975 case Primitive::kPrimDouble: { 1976 // Processing a Dex `double-to-long' instruction. 1977 InvokeRuntimeCallingConvention calling_convention; 1978 locations->SetInAt(0, Location::FpuRegisterPairLocation( 1979 calling_convention.GetFpuRegisterAt(0), 1980 calling_convention.GetFpuRegisterAt(1))); 1981 locations->SetOut(Location::RegisterPairLocation(R0, R1)); 1982 break; 1983 } 1984 1985 default: 1986 LOG(FATAL) << "Unexpected type conversion from " << input_type 1987 << " to " << result_type; 1988 } 1989 break; 1990 1991 case Primitive::kPrimChar: 1992 switch (input_type) { 1993 case Primitive::kPrimBoolean: 1994 // Boolean input is a result of code transformations. 1995 case Primitive::kPrimByte: 1996 case Primitive::kPrimShort: 1997 case Primitive::kPrimInt: 1998 // Processing a Dex `int-to-char' instruction. 1999 locations->SetInAt(0, Location::RequiresRegister()); 2000 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 2001 break; 2002 2003 default: 2004 LOG(FATAL) << "Unexpected type conversion from " << input_type 2005 << " to " << result_type; 2006 } 2007 break; 2008 2009 case Primitive::kPrimFloat: 2010 switch (input_type) { 2011 case Primitive::kPrimBoolean: 2012 // Boolean input is a result of code transformations. 2013 case Primitive::kPrimByte: 2014 case Primitive::kPrimShort: 2015 case Primitive::kPrimInt: 2016 case Primitive::kPrimChar: 2017 // Processing a Dex `int-to-float' instruction. 2018 locations->SetInAt(0, Location::RequiresRegister()); 2019 locations->SetOut(Location::RequiresFpuRegister()); 2020 break; 2021 2022 case Primitive::kPrimLong: { 2023 // Processing a Dex `long-to-float' instruction. 2024 InvokeRuntimeCallingConvention calling_convention; 2025 locations->SetInAt(0, Location::RegisterPairLocation( 2026 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1))); 2027 locations->SetOut(Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0))); 2028 break; 2029 } 2030 2031 case Primitive::kPrimDouble: 2032 // Processing a Dex `double-to-float' instruction. 2033 locations->SetInAt(0, Location::RequiresFpuRegister()); 2034 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); 2035 break; 2036 2037 default: 2038 LOG(FATAL) << "Unexpected type conversion from " << input_type 2039 << " to " << result_type; 2040 }; 2041 break; 2042 2043 case Primitive::kPrimDouble: 2044 switch (input_type) { 2045 case Primitive::kPrimBoolean: 2046 // Boolean input is a result of code transformations. 2047 case Primitive::kPrimByte: 2048 case Primitive::kPrimShort: 2049 case Primitive::kPrimInt: 2050 case Primitive::kPrimChar: 2051 // Processing a Dex `int-to-double' instruction. 2052 locations->SetInAt(0, Location::RequiresRegister()); 2053 locations->SetOut(Location::RequiresFpuRegister()); 2054 break; 2055 2056 case Primitive::kPrimLong: 2057 // Processing a Dex `long-to-double' instruction. 2058 locations->SetInAt(0, Location::RequiresRegister()); 2059 locations->SetOut(Location::RequiresFpuRegister()); 2060 locations->AddTemp(Location::RequiresFpuRegister()); 2061 locations->AddTemp(Location::RequiresFpuRegister()); 2062 break; 2063 2064 case Primitive::kPrimFloat: 2065 // Processing a Dex `float-to-double' instruction. 2066 locations->SetInAt(0, Location::RequiresFpuRegister()); 2067 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); 2068 break; 2069 2070 default: 2071 LOG(FATAL) << "Unexpected type conversion from " << input_type 2072 << " to " << result_type; 2073 }; 2074 break; 2075 2076 default: 2077 LOG(FATAL) << "Unexpected type conversion from " << input_type 2078 << " to " << result_type; 2079 } 2080} 2081 2082void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversion) { 2083 LocationSummary* locations = conversion->GetLocations(); 2084 Location out = locations->Out(); 2085 Location in = locations->InAt(0); 2086 Primitive::Type result_type = conversion->GetResultType(); 2087 Primitive::Type input_type = conversion->GetInputType(); 2088 DCHECK_NE(result_type, input_type); 2089 switch (result_type) { 2090 case Primitive::kPrimByte: 2091 switch (input_type) { 2092 case Primitive::kPrimBoolean: 2093 // Boolean input is a result of code transformations. 2094 case Primitive::kPrimShort: 2095 case Primitive::kPrimInt: 2096 case Primitive::kPrimChar: 2097 // Processing a Dex `int-to-byte' instruction. 2098 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 8); 2099 break; 2100 2101 default: 2102 LOG(FATAL) << "Unexpected type conversion from " << input_type 2103 << " to " << result_type; 2104 } 2105 break; 2106 2107 case Primitive::kPrimShort: 2108 switch (input_type) { 2109 case Primitive::kPrimBoolean: 2110 // Boolean input is a result of code transformations. 2111 case Primitive::kPrimByte: 2112 case Primitive::kPrimInt: 2113 case Primitive::kPrimChar: 2114 // Processing a Dex `int-to-short' instruction. 2115 __ sbfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16); 2116 break; 2117 2118 default: 2119 LOG(FATAL) << "Unexpected type conversion from " << input_type 2120 << " to " << result_type; 2121 } 2122 break; 2123 2124 case Primitive::kPrimInt: 2125 switch (input_type) { 2126 case Primitive::kPrimLong: 2127 // Processing a Dex `long-to-int' instruction. 2128 DCHECK(out.IsRegister()); 2129 if (in.IsRegisterPair()) { 2130 __ Mov(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>()); 2131 } else if (in.IsDoubleStackSlot()) { 2132 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), SP, in.GetStackIndex()); 2133 } else { 2134 DCHECK(in.IsConstant()); 2135 DCHECK(in.GetConstant()->IsLongConstant()); 2136 int64_t value = in.GetConstant()->AsLongConstant()->GetValue(); 2137 __ LoadImmediate(out.AsRegister<Register>(), static_cast<int32_t>(value)); 2138 } 2139 break; 2140 2141 case Primitive::kPrimFloat: { 2142 // Processing a Dex `float-to-int' instruction. 2143 SRegister temp = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>(); 2144 __ vmovs(temp, in.AsFpuRegister<SRegister>()); 2145 __ vcvtis(temp, temp); 2146 __ vmovrs(out.AsRegister<Register>(), temp); 2147 break; 2148 } 2149 2150 case Primitive::kPrimDouble: { 2151 // Processing a Dex `double-to-int' instruction. 2152 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>(); 2153 DRegister temp_d = FromLowSToD(temp_s); 2154 __ vmovd(temp_d, FromLowSToD(in.AsFpuRegisterPairLow<SRegister>())); 2155 __ vcvtid(temp_s, temp_d); 2156 __ vmovrs(out.AsRegister<Register>(), temp_s); 2157 break; 2158 } 2159 2160 default: 2161 LOG(FATAL) << "Unexpected type conversion from " << input_type 2162 << " to " << result_type; 2163 } 2164 break; 2165 2166 case Primitive::kPrimLong: 2167 switch (input_type) { 2168 case Primitive::kPrimBoolean: 2169 // Boolean input is a result of code transformations. 2170 case Primitive::kPrimByte: 2171 case Primitive::kPrimShort: 2172 case Primitive::kPrimInt: 2173 case Primitive::kPrimChar: 2174 // Processing a Dex `int-to-long' instruction. 2175 DCHECK(out.IsRegisterPair()); 2176 DCHECK(in.IsRegister()); 2177 __ Mov(out.AsRegisterPairLow<Register>(), in.AsRegister<Register>()); 2178 // Sign extension. 2179 __ Asr(out.AsRegisterPairHigh<Register>(), 2180 out.AsRegisterPairLow<Register>(), 2181 31); 2182 break; 2183 2184 case Primitive::kPrimFloat: 2185 // Processing a Dex `float-to-long' instruction. 2186 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l), 2187 conversion, 2188 conversion->GetDexPc(), 2189 nullptr); 2190 break; 2191 2192 case Primitive::kPrimDouble: 2193 // Processing a Dex `double-to-long' instruction. 2194 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l), 2195 conversion, 2196 conversion->GetDexPc(), 2197 nullptr); 2198 break; 2199 2200 default: 2201 LOG(FATAL) << "Unexpected type conversion from " << input_type 2202 << " to " << result_type; 2203 } 2204 break; 2205 2206 case Primitive::kPrimChar: 2207 switch (input_type) { 2208 case Primitive::kPrimBoolean: 2209 // Boolean input is a result of code transformations. 2210 case Primitive::kPrimByte: 2211 case Primitive::kPrimShort: 2212 case Primitive::kPrimInt: 2213 // Processing a Dex `int-to-char' instruction. 2214 __ ubfx(out.AsRegister<Register>(), in.AsRegister<Register>(), 0, 16); 2215 break; 2216 2217 default: 2218 LOG(FATAL) << "Unexpected type conversion from " << input_type 2219 << " to " << result_type; 2220 } 2221 break; 2222 2223 case Primitive::kPrimFloat: 2224 switch (input_type) { 2225 case Primitive::kPrimBoolean: 2226 // Boolean input is a result of code transformations. 2227 case Primitive::kPrimByte: 2228 case Primitive::kPrimShort: 2229 case Primitive::kPrimInt: 2230 case Primitive::kPrimChar: { 2231 // Processing a Dex `int-to-float' instruction. 2232 __ vmovsr(out.AsFpuRegister<SRegister>(), in.AsRegister<Register>()); 2233 __ vcvtsi(out.AsFpuRegister<SRegister>(), out.AsFpuRegister<SRegister>()); 2234 break; 2235 } 2236 2237 case Primitive::kPrimLong: 2238 // Processing a Dex `long-to-float' instruction. 2239 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pL2f), 2240 conversion, 2241 conversion->GetDexPc(), 2242 nullptr); 2243 break; 2244 2245 case Primitive::kPrimDouble: 2246 // Processing a Dex `double-to-float' instruction. 2247 __ vcvtsd(out.AsFpuRegister<SRegister>(), 2248 FromLowSToD(in.AsFpuRegisterPairLow<SRegister>())); 2249 break; 2250 2251 default: 2252 LOG(FATAL) << "Unexpected type conversion from " << input_type 2253 << " to " << result_type; 2254 }; 2255 break; 2256 2257 case Primitive::kPrimDouble: 2258 switch (input_type) { 2259 case Primitive::kPrimBoolean: 2260 // Boolean input is a result of code transformations. 2261 case Primitive::kPrimByte: 2262 case Primitive::kPrimShort: 2263 case Primitive::kPrimInt: 2264 case Primitive::kPrimChar: { 2265 // Processing a Dex `int-to-double' instruction. 2266 __ vmovsr(out.AsFpuRegisterPairLow<SRegister>(), in.AsRegister<Register>()); 2267 __ vcvtdi(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), 2268 out.AsFpuRegisterPairLow<SRegister>()); 2269 break; 2270 } 2271 2272 case Primitive::kPrimLong: { 2273 // Processing a Dex `long-to-double' instruction. 2274 Register low = in.AsRegisterPairLow<Register>(); 2275 Register high = in.AsRegisterPairHigh<Register>(); 2276 SRegister out_s = out.AsFpuRegisterPairLow<SRegister>(); 2277 DRegister out_d = FromLowSToD(out_s); 2278 SRegister temp_s = locations->GetTemp(0).AsFpuRegisterPairLow<SRegister>(); 2279 DRegister temp_d = FromLowSToD(temp_s); 2280 SRegister constant_s = locations->GetTemp(1).AsFpuRegisterPairLow<SRegister>(); 2281 DRegister constant_d = FromLowSToD(constant_s); 2282 2283 // temp_d = int-to-double(high) 2284 __ vmovsr(temp_s, high); 2285 __ vcvtdi(temp_d, temp_s); 2286 // constant_d = k2Pow32EncodingForDouble 2287 __ LoadDImmediate(constant_d, bit_cast<double, int64_t>(k2Pow32EncodingForDouble)); 2288 // out_d = unsigned-to-double(low) 2289 __ vmovsr(out_s, low); 2290 __ vcvtdu(out_d, out_s); 2291 // out_d += temp_d * constant_d 2292 __ vmlad(out_d, temp_d, constant_d); 2293 break; 2294 } 2295 2296 case Primitive::kPrimFloat: 2297 // Processing a Dex `float-to-double' instruction. 2298 __ vcvtds(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), 2299 in.AsFpuRegister<SRegister>()); 2300 break; 2301 2302 default: 2303 LOG(FATAL) << "Unexpected type conversion from " << input_type 2304 << " to " << result_type; 2305 }; 2306 break; 2307 2308 default: 2309 LOG(FATAL) << "Unexpected type conversion from " << input_type 2310 << " to " << result_type; 2311 } 2312} 2313 2314void LocationsBuilderARM::VisitAdd(HAdd* add) { 2315 LocationSummary* locations = 2316 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall); 2317 switch (add->GetResultType()) { 2318 case Primitive::kPrimInt: { 2319 locations->SetInAt(0, Location::RequiresRegister()); 2320 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1))); 2321 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 2322 break; 2323 } 2324 2325 case Primitive::kPrimLong: { 2326 locations->SetInAt(0, Location::RequiresRegister()); 2327 locations->SetInAt(1, Location::RequiresRegister()); 2328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 2329 break; 2330 } 2331 2332 case Primitive::kPrimFloat: 2333 case Primitive::kPrimDouble: { 2334 locations->SetInAt(0, Location::RequiresFpuRegister()); 2335 locations->SetInAt(1, Location::RequiresFpuRegister()); 2336 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); 2337 break; 2338 } 2339 2340 default: 2341 LOG(FATAL) << "Unexpected add type " << add->GetResultType(); 2342 } 2343} 2344 2345void InstructionCodeGeneratorARM::VisitAdd(HAdd* add) { 2346 LocationSummary* locations = add->GetLocations(); 2347 Location out = locations->Out(); 2348 Location first = locations->InAt(0); 2349 Location second = locations->InAt(1); 2350 switch (add->GetResultType()) { 2351 case Primitive::kPrimInt: 2352 if (second.IsRegister()) { 2353 __ add(out.AsRegister<Register>(), 2354 first.AsRegister<Register>(), 2355 ShifterOperand(second.AsRegister<Register>())); 2356 } else { 2357 __ AddConstant(out.AsRegister<Register>(), 2358 first.AsRegister<Register>(), 2359 second.GetConstant()->AsIntConstant()->GetValue()); 2360 } 2361 break; 2362 2363 case Primitive::kPrimLong: { 2364 DCHECK(second.IsRegisterPair()); 2365 __ adds(out.AsRegisterPairLow<Register>(), 2366 first.AsRegisterPairLow<Register>(), 2367 ShifterOperand(second.AsRegisterPairLow<Register>())); 2368 __ adc(out.AsRegisterPairHigh<Register>(), 2369 first.AsRegisterPairHigh<Register>(), 2370 ShifterOperand(second.AsRegisterPairHigh<Register>())); 2371 break; 2372 } 2373 2374 case Primitive::kPrimFloat: 2375 __ vadds(out.AsFpuRegister<SRegister>(), 2376 first.AsFpuRegister<SRegister>(), 2377 second.AsFpuRegister<SRegister>()); 2378 break; 2379 2380 case Primitive::kPrimDouble: 2381 __ vaddd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), 2382 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()), 2383 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>())); 2384 break; 2385 2386 default: 2387 LOG(FATAL) << "Unexpected add type " << add->GetResultType(); 2388 } 2389} 2390 2391void LocationsBuilderARM::VisitSub(HSub* sub) { 2392 LocationSummary* locations = 2393 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall); 2394 switch (sub->GetResultType()) { 2395 case Primitive::kPrimInt: { 2396 locations->SetInAt(0, Location::RequiresRegister()); 2397 locations->SetInAt(1, Location::RegisterOrConstant(sub->InputAt(1))); 2398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 2399 break; 2400 } 2401 2402 case Primitive::kPrimLong: { 2403 locations->SetInAt(0, Location::RequiresRegister()); 2404 locations->SetInAt(1, Location::RequiresRegister()); 2405 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 2406 break; 2407 } 2408 case Primitive::kPrimFloat: 2409 case Primitive::kPrimDouble: { 2410 locations->SetInAt(0, Location::RequiresFpuRegister()); 2411 locations->SetInAt(1, Location::RequiresFpuRegister()); 2412 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); 2413 break; 2414 } 2415 default: 2416 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType(); 2417 } 2418} 2419 2420void InstructionCodeGeneratorARM::VisitSub(HSub* sub) { 2421 LocationSummary* locations = sub->GetLocations(); 2422 Location out = locations->Out(); 2423 Location first = locations->InAt(0); 2424 Location second = locations->InAt(1); 2425 switch (sub->GetResultType()) { 2426 case Primitive::kPrimInt: { 2427 if (second.IsRegister()) { 2428 __ sub(out.AsRegister<Register>(), 2429 first.AsRegister<Register>(), 2430 ShifterOperand(second.AsRegister<Register>())); 2431 } else { 2432 __ AddConstant(out.AsRegister<Register>(), 2433 first.AsRegister<Register>(), 2434 -second.GetConstant()->AsIntConstant()->GetValue()); 2435 } 2436 break; 2437 } 2438 2439 case Primitive::kPrimLong: { 2440 DCHECK(second.IsRegisterPair()); 2441 __ subs(out.AsRegisterPairLow<Register>(), 2442 first.AsRegisterPairLow<Register>(), 2443 ShifterOperand(second.AsRegisterPairLow<Register>())); 2444 __ sbc(out.AsRegisterPairHigh<Register>(), 2445 first.AsRegisterPairHigh<Register>(), 2446 ShifterOperand(second.AsRegisterPairHigh<Register>())); 2447 break; 2448 } 2449 2450 case Primitive::kPrimFloat: { 2451 __ vsubs(out.AsFpuRegister<SRegister>(), 2452 first.AsFpuRegister<SRegister>(), 2453 second.AsFpuRegister<SRegister>()); 2454 break; 2455 } 2456 2457 case Primitive::kPrimDouble: { 2458 __ vsubd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), 2459 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()), 2460 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>())); 2461 break; 2462 } 2463 2464 2465 default: 2466 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType(); 2467 } 2468} 2469 2470void LocationsBuilderARM::VisitMul(HMul* mul) { 2471 LocationSummary* locations = 2472 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall); 2473 switch (mul->GetResultType()) { 2474 case Primitive::kPrimInt: 2475 case Primitive::kPrimLong: { 2476 locations->SetInAt(0, Location::RequiresRegister()); 2477 locations->SetInAt(1, Location::RequiresRegister()); 2478 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 2479 break; 2480 } 2481 2482 case Primitive::kPrimFloat: 2483 case Primitive::kPrimDouble: { 2484 locations->SetInAt(0, Location::RequiresFpuRegister()); 2485 locations->SetInAt(1, Location::RequiresFpuRegister()); 2486 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); 2487 break; 2488 } 2489 2490 default: 2491 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType(); 2492 } 2493} 2494 2495void InstructionCodeGeneratorARM::VisitMul(HMul* mul) { 2496 LocationSummary* locations = mul->GetLocations(); 2497 Location out = locations->Out(); 2498 Location first = locations->InAt(0); 2499 Location second = locations->InAt(1); 2500 switch (mul->GetResultType()) { 2501 case Primitive::kPrimInt: { 2502 __ mul(out.AsRegister<Register>(), 2503 first.AsRegister<Register>(), 2504 second.AsRegister<Register>()); 2505 break; 2506 } 2507 case Primitive::kPrimLong: { 2508 Register out_hi = out.AsRegisterPairHigh<Register>(); 2509 Register out_lo = out.AsRegisterPairLow<Register>(); 2510 Register in1_hi = first.AsRegisterPairHigh<Register>(); 2511 Register in1_lo = first.AsRegisterPairLow<Register>(); 2512 Register in2_hi = second.AsRegisterPairHigh<Register>(); 2513 Register in2_lo = second.AsRegisterPairLow<Register>(); 2514 2515 // Extra checks to protect caused by the existence of R1_R2. 2516 // The algorithm is wrong if out.hi is either in1.lo or in2.lo: 2517 // (e.g. in1=r0_r1, in2=r2_r3 and out=r1_r2); 2518 DCHECK_NE(out_hi, in1_lo); 2519 DCHECK_NE(out_hi, in2_lo); 2520 2521 // input: in1 - 64 bits, in2 - 64 bits 2522 // output: out 2523 // formula: out.hi : out.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo 2524 // parts: out.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32] 2525 // parts: out.lo = (in1.lo * in2.lo)[31:0] 2526 2527 // IP <- in1.lo * in2.hi 2528 __ mul(IP, in1_lo, in2_hi); 2529 // out.hi <- in1.lo * in2.hi + in1.hi * in2.lo 2530 __ mla(out_hi, in1_hi, in2_lo, IP); 2531 // out.lo <- (in1.lo * in2.lo)[31:0]; 2532 __ umull(out_lo, IP, in1_lo, in2_lo); 2533 // out.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32] 2534 __ add(out_hi, out_hi, ShifterOperand(IP)); 2535 break; 2536 } 2537 2538 case Primitive::kPrimFloat: { 2539 __ vmuls(out.AsFpuRegister<SRegister>(), 2540 first.AsFpuRegister<SRegister>(), 2541 second.AsFpuRegister<SRegister>()); 2542 break; 2543 } 2544 2545 case Primitive::kPrimDouble: { 2546 __ vmuld(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), 2547 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()), 2548 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>())); 2549 break; 2550 } 2551 2552 default: 2553 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType(); 2554 } 2555} 2556 2557void InstructionCodeGeneratorARM::DivRemOneOrMinusOne(HBinaryOperation* instruction) { 2558 DCHECK(instruction->IsDiv() || instruction->IsRem()); 2559 DCHECK(instruction->GetResultType() == Primitive::kPrimInt); 2560 2561 LocationSummary* locations = instruction->GetLocations(); 2562 Location second = locations->InAt(1); 2563 DCHECK(second.IsConstant()); 2564 2565 Register out = locations->Out().AsRegister<Register>(); 2566 Register dividend = locations->InAt(0).AsRegister<Register>(); 2567 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue(); 2568 DCHECK(imm == 1 || imm == -1); 2569 2570 if (instruction->IsRem()) { 2571 __ LoadImmediate(out, 0); 2572 } else { 2573 if (imm == 1) { 2574 __ Mov(out, dividend); 2575 } else { 2576 __ rsb(out, dividend, ShifterOperand(0)); 2577 } 2578 } 2579} 2580 2581void InstructionCodeGeneratorARM::DivRemByPowerOfTwo(HBinaryOperation* instruction) { 2582 DCHECK(instruction->IsDiv() || instruction->IsRem()); 2583 DCHECK(instruction->GetResultType() == Primitive::kPrimInt); 2584 2585 LocationSummary* locations = instruction->GetLocations(); 2586 Location second = locations->InAt(1); 2587 DCHECK(second.IsConstant()); 2588 2589 Register out = locations->Out().AsRegister<Register>(); 2590 Register dividend = locations->InAt(0).AsRegister<Register>(); 2591 Register temp = locations->GetTemp(0).AsRegister<Register>(); 2592 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue(); 2593 uint32_t abs_imm = static_cast<uint32_t>(std::abs(imm)); 2594 DCHECK(IsPowerOfTwo(abs_imm)); 2595 int ctz_imm = CTZ(abs_imm); 2596 2597 if (ctz_imm == 1) { 2598 __ Lsr(temp, dividend, 32 - ctz_imm); 2599 } else { 2600 __ Asr(temp, dividend, 31); 2601 __ Lsr(temp, temp, 32 - ctz_imm); 2602 } 2603 __ add(out, temp, ShifterOperand(dividend)); 2604 2605 if (instruction->IsDiv()) { 2606 __ Asr(out, out, ctz_imm); 2607 if (imm < 0) { 2608 __ rsb(out, out, ShifterOperand(0)); 2609 } 2610 } else { 2611 __ ubfx(out, out, 0, ctz_imm); 2612 __ sub(out, out, ShifterOperand(temp)); 2613 } 2614} 2615 2616void InstructionCodeGeneratorARM::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) { 2617 DCHECK(instruction->IsDiv() || instruction->IsRem()); 2618 DCHECK(instruction->GetResultType() == Primitive::kPrimInt); 2619 2620 LocationSummary* locations = instruction->GetLocations(); 2621 Location second = locations->InAt(1); 2622 DCHECK(second.IsConstant()); 2623 2624 Register out = locations->Out().AsRegister<Register>(); 2625 Register dividend = locations->InAt(0).AsRegister<Register>(); 2626 Register temp1 = locations->GetTemp(0).AsRegister<Register>(); 2627 Register temp2 = locations->GetTemp(1).AsRegister<Register>(); 2628 int64_t imm = second.GetConstant()->AsIntConstant()->GetValue(); 2629 2630 int64_t magic; 2631 int shift; 2632 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift); 2633 2634 __ LoadImmediate(temp1, magic); 2635 __ smull(temp2, temp1, dividend, temp1); 2636 2637 if (imm > 0 && magic < 0) { 2638 __ add(temp1, temp1, ShifterOperand(dividend)); 2639 } else if (imm < 0 && magic > 0) { 2640 __ sub(temp1, temp1, ShifterOperand(dividend)); 2641 } 2642 2643 if (shift != 0) { 2644 __ Asr(temp1, temp1, shift); 2645 } 2646 2647 if (instruction->IsDiv()) { 2648 __ sub(out, temp1, ShifterOperand(temp1, ASR, 31)); 2649 } else { 2650 __ sub(temp1, temp1, ShifterOperand(temp1, ASR, 31)); 2651 // TODO: Strength reduction for mls. 2652 __ LoadImmediate(temp2, imm); 2653 __ mls(out, temp1, temp2, dividend); 2654 } 2655} 2656 2657void InstructionCodeGeneratorARM::GenerateDivRemConstantIntegral(HBinaryOperation* instruction) { 2658 DCHECK(instruction->IsDiv() || instruction->IsRem()); 2659 DCHECK(instruction->GetResultType() == Primitive::kPrimInt); 2660 2661 LocationSummary* locations = instruction->GetLocations(); 2662 Location second = locations->InAt(1); 2663 DCHECK(second.IsConstant()); 2664 2665 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue(); 2666 if (imm == 0) { 2667 // Do not generate anything. DivZeroCheck would prevent any code to be executed. 2668 } else if (imm == 1 || imm == -1) { 2669 DivRemOneOrMinusOne(instruction); 2670 } else if (IsPowerOfTwo(std::abs(imm))) { 2671 DivRemByPowerOfTwo(instruction); 2672 } else { 2673 DCHECK(imm <= -2 || imm >= 2); 2674 GenerateDivRemWithAnyConstant(instruction); 2675 } 2676} 2677 2678void LocationsBuilderARM::VisitDiv(HDiv* div) { 2679 LocationSummary::CallKind call_kind = LocationSummary::kNoCall; 2680 if (div->GetResultType() == Primitive::kPrimLong) { 2681 // pLdiv runtime call. 2682 call_kind = LocationSummary::kCall; 2683 } else if (div->GetResultType() == Primitive::kPrimInt && div->InputAt(1)->IsConstant()) { 2684 // sdiv will be replaced by other instruction sequence. 2685 } else if (div->GetResultType() == Primitive::kPrimInt && 2686 !codegen_->GetInstructionSetFeatures().HasDivideInstruction()) { 2687 // pIdivmod runtime call. 2688 call_kind = LocationSummary::kCall; 2689 } 2690 2691 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind); 2692 2693 switch (div->GetResultType()) { 2694 case Primitive::kPrimInt: { 2695 if (div->InputAt(1)->IsConstant()) { 2696 locations->SetInAt(0, Location::RequiresRegister()); 2697 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1))); 2698 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 2699 int32_t abs_imm = std::abs(div->InputAt(1)->AsIntConstant()->GetValue()); 2700 if (abs_imm <= 1) { 2701 // No temp register required. 2702 } else { 2703 locations->AddTemp(Location::RequiresRegister()); 2704 if (!IsPowerOfTwo(abs_imm)) { 2705 locations->AddTemp(Location::RequiresRegister()); 2706 } 2707 } 2708 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) { 2709 locations->SetInAt(0, Location::RequiresRegister()); 2710 locations->SetInAt(1, Location::RequiresRegister()); 2711 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 2712 } else { 2713 InvokeRuntimeCallingConvention calling_convention; 2714 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); 2715 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); 2716 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but 2717 // we only need the former. 2718 locations->SetOut(Location::RegisterLocation(R0)); 2719 } 2720 break; 2721 } 2722 case Primitive::kPrimLong: { 2723 InvokeRuntimeCallingConvention calling_convention; 2724 locations->SetInAt(0, Location::RegisterPairLocation( 2725 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1))); 2726 locations->SetInAt(1, Location::RegisterPairLocation( 2727 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3))); 2728 locations->SetOut(Location::RegisterPairLocation(R0, R1)); 2729 break; 2730 } 2731 case Primitive::kPrimFloat: 2732 case Primitive::kPrimDouble: { 2733 locations->SetInAt(0, Location::RequiresFpuRegister()); 2734 locations->SetInAt(1, Location::RequiresFpuRegister()); 2735 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); 2736 break; 2737 } 2738 2739 default: 2740 LOG(FATAL) << "Unexpected div type " << div->GetResultType(); 2741 } 2742} 2743 2744void InstructionCodeGeneratorARM::VisitDiv(HDiv* div) { 2745 LocationSummary* locations = div->GetLocations(); 2746 Location out = locations->Out(); 2747 Location first = locations->InAt(0); 2748 Location second = locations->InAt(1); 2749 2750 switch (div->GetResultType()) { 2751 case Primitive::kPrimInt: { 2752 if (second.IsConstant()) { 2753 GenerateDivRemConstantIntegral(div); 2754 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) { 2755 __ sdiv(out.AsRegister<Register>(), 2756 first.AsRegister<Register>(), 2757 second.AsRegister<Register>()); 2758 } else { 2759 InvokeRuntimeCallingConvention calling_convention; 2760 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>()); 2761 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>()); 2762 DCHECK_EQ(R0, out.AsRegister<Register>()); 2763 2764 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), div, div->GetDexPc(), nullptr); 2765 } 2766 break; 2767 } 2768 2769 case Primitive::kPrimLong: { 2770 InvokeRuntimeCallingConvention calling_convention; 2771 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>()); 2772 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>()); 2773 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>()); 2774 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>()); 2775 DCHECK_EQ(R0, out.AsRegisterPairLow<Register>()); 2776 DCHECK_EQ(R1, out.AsRegisterPairHigh<Register>()); 2777 2778 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv), div, div->GetDexPc(), nullptr); 2779 break; 2780 } 2781 2782 case Primitive::kPrimFloat: { 2783 __ vdivs(out.AsFpuRegister<SRegister>(), 2784 first.AsFpuRegister<SRegister>(), 2785 second.AsFpuRegister<SRegister>()); 2786 break; 2787 } 2788 2789 case Primitive::kPrimDouble: { 2790 __ vdivd(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), 2791 FromLowSToD(first.AsFpuRegisterPairLow<SRegister>()), 2792 FromLowSToD(second.AsFpuRegisterPairLow<SRegister>())); 2793 break; 2794 } 2795 2796 default: 2797 LOG(FATAL) << "Unexpected div type " << div->GetResultType(); 2798 } 2799} 2800 2801void LocationsBuilderARM::VisitRem(HRem* rem) { 2802 Primitive::Type type = rem->GetResultType(); 2803 2804 // Most remainders are implemented in the runtime. 2805 LocationSummary::CallKind call_kind = LocationSummary::kCall; 2806 if (rem->GetResultType() == Primitive::kPrimInt && rem->InputAt(1)->IsConstant()) { 2807 // sdiv will be replaced by other instruction sequence. 2808 call_kind = LocationSummary::kNoCall; 2809 } else if ((rem->GetResultType() == Primitive::kPrimInt) 2810 && codegen_->GetInstructionSetFeatures().HasDivideInstruction()) { 2811 // Have hardware divide instruction for int, do it with three instructions. 2812 call_kind = LocationSummary::kNoCall; 2813 } 2814 2815 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind); 2816 2817 switch (type) { 2818 case Primitive::kPrimInt: { 2819 if (rem->InputAt(1)->IsConstant()) { 2820 locations->SetInAt(0, Location::RequiresRegister()); 2821 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1))); 2822 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 2823 int32_t abs_imm = std::abs(rem->InputAt(1)->AsIntConstant()->GetValue()); 2824 if (abs_imm <= 1) { 2825 // No temp register required. 2826 } else { 2827 locations->AddTemp(Location::RequiresRegister()); 2828 if (!IsPowerOfTwo(abs_imm)) { 2829 locations->AddTemp(Location::RequiresRegister()); 2830 } 2831 } 2832 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) { 2833 locations->SetInAt(0, Location::RequiresRegister()); 2834 locations->SetInAt(1, Location::RequiresRegister()); 2835 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 2836 locations->AddTemp(Location::RequiresRegister()); 2837 } else { 2838 InvokeRuntimeCallingConvention calling_convention; 2839 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); 2840 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); 2841 // Note: divrem will compute both the quotient and the remainder as the pair R0 and R1, but 2842 // we only need the latter. 2843 locations->SetOut(Location::RegisterLocation(R1)); 2844 } 2845 break; 2846 } 2847 case Primitive::kPrimLong: { 2848 InvokeRuntimeCallingConvention calling_convention; 2849 locations->SetInAt(0, Location::RegisterPairLocation( 2850 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1))); 2851 locations->SetInAt(1, Location::RegisterPairLocation( 2852 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3))); 2853 // The runtime helper puts the output in R2,R3. 2854 locations->SetOut(Location::RegisterPairLocation(R2, R3)); 2855 break; 2856 } 2857 case Primitive::kPrimFloat: { 2858 InvokeRuntimeCallingConvention calling_convention; 2859 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0))); 2860 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1))); 2861 locations->SetOut(Location::FpuRegisterLocation(S0)); 2862 break; 2863 } 2864 2865 case Primitive::kPrimDouble: { 2866 InvokeRuntimeCallingConvention calling_convention; 2867 locations->SetInAt(0, Location::FpuRegisterPairLocation( 2868 calling_convention.GetFpuRegisterAt(0), calling_convention.GetFpuRegisterAt(1))); 2869 locations->SetInAt(1, Location::FpuRegisterPairLocation( 2870 calling_convention.GetFpuRegisterAt(2), calling_convention.GetFpuRegisterAt(3))); 2871 locations->SetOut(Location::Location::FpuRegisterPairLocation(S0, S1)); 2872 break; 2873 } 2874 2875 default: 2876 LOG(FATAL) << "Unexpected rem type " << type; 2877 } 2878} 2879 2880void InstructionCodeGeneratorARM::VisitRem(HRem* rem) { 2881 LocationSummary* locations = rem->GetLocations(); 2882 Location out = locations->Out(); 2883 Location first = locations->InAt(0); 2884 Location second = locations->InAt(1); 2885 2886 Primitive::Type type = rem->GetResultType(); 2887 switch (type) { 2888 case Primitive::kPrimInt: { 2889 if (second.IsConstant()) { 2890 GenerateDivRemConstantIntegral(rem); 2891 } else if (codegen_->GetInstructionSetFeatures().HasDivideInstruction()) { 2892 Register reg1 = first.AsRegister<Register>(); 2893 Register reg2 = second.AsRegister<Register>(); 2894 Register temp = locations->GetTemp(0).AsRegister<Register>(); 2895 2896 // temp = reg1 / reg2 (integer division) 2897 // dest = reg1 - temp * reg2 2898 __ sdiv(temp, reg1, reg2); 2899 __ mls(out.AsRegister<Register>(), temp, reg2, reg1); 2900 } else { 2901 InvokeRuntimeCallingConvention calling_convention; 2902 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegister<Register>()); 2903 DCHECK_EQ(calling_convention.GetRegisterAt(1), second.AsRegister<Register>()); 2904 DCHECK_EQ(R1, out.AsRegister<Register>()); 2905 2906 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pIdivmod), rem, rem->GetDexPc(), nullptr); 2907 } 2908 break; 2909 } 2910 2911 case Primitive::kPrimLong: { 2912 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod), rem, rem->GetDexPc(), nullptr); 2913 break; 2914 } 2915 2916 case Primitive::kPrimFloat: { 2917 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmodf), rem, rem->GetDexPc(), nullptr); 2918 break; 2919 } 2920 2921 case Primitive::kPrimDouble: { 2922 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pFmod), rem, rem->GetDexPc(), nullptr); 2923 break; 2924 } 2925 2926 default: 2927 LOG(FATAL) << "Unexpected rem type " << type; 2928 } 2929} 2930 2931void LocationsBuilderARM::VisitDivZeroCheck(HDivZeroCheck* instruction) { 2932 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock() 2933 ? LocationSummary::kCallOnSlowPath 2934 : LocationSummary::kNoCall; 2935 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind); 2936 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0))); 2937 if (instruction->HasUses()) { 2938 locations->SetOut(Location::SameAsFirstInput()); 2939 } 2940} 2941 2942void InstructionCodeGeneratorARM::VisitDivZeroCheck(HDivZeroCheck* instruction) { 2943 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathARM(instruction); 2944 codegen_->AddSlowPath(slow_path); 2945 2946 LocationSummary* locations = instruction->GetLocations(); 2947 Location value = locations->InAt(0); 2948 2949 switch (instruction->GetType()) { 2950 case Primitive::kPrimByte: 2951 case Primitive::kPrimChar: 2952 case Primitive::kPrimShort: 2953 case Primitive::kPrimInt: { 2954 if (value.IsRegister()) { 2955 __ CompareAndBranchIfZero(value.AsRegister<Register>(), slow_path->GetEntryLabel()); 2956 } else { 2957 DCHECK(value.IsConstant()) << value; 2958 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) { 2959 __ b(slow_path->GetEntryLabel()); 2960 } 2961 } 2962 break; 2963 } 2964 case Primitive::kPrimLong: { 2965 if (value.IsRegisterPair()) { 2966 __ orrs(IP, 2967 value.AsRegisterPairLow<Register>(), 2968 ShifterOperand(value.AsRegisterPairHigh<Register>())); 2969 __ b(slow_path->GetEntryLabel(), EQ); 2970 } else { 2971 DCHECK(value.IsConstant()) << value; 2972 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) { 2973 __ b(slow_path->GetEntryLabel()); 2974 } 2975 } 2976 break; 2977 default: 2978 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType(); 2979 } 2980 } 2981} 2982 2983void LocationsBuilderARM::HandleShift(HBinaryOperation* op) { 2984 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr()); 2985 2986 LocationSummary* locations = 2987 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall); 2988 2989 switch (op->GetResultType()) { 2990 case Primitive::kPrimInt: { 2991 locations->SetInAt(0, Location::RequiresRegister()); 2992 locations->SetInAt(1, Location::RegisterOrConstant(op->InputAt(1))); 2993 // Make the output overlap, as it will be used to hold the masked 2994 // second input. 2995 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap); 2996 break; 2997 } 2998 case Primitive::kPrimLong: { 2999 locations->SetInAt(0, Location::RequiresRegister()); 3000 locations->SetInAt(1, Location::RequiresRegister()); 3001 locations->AddTemp(Location::RequiresRegister()); 3002 locations->SetOut(Location::RequiresRegister()); 3003 break; 3004 } 3005 default: 3006 LOG(FATAL) << "Unexpected operation type " << op->GetResultType(); 3007 } 3008} 3009 3010void InstructionCodeGeneratorARM::HandleShift(HBinaryOperation* op) { 3011 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr()); 3012 3013 LocationSummary* locations = op->GetLocations(); 3014 Location out = locations->Out(); 3015 Location first = locations->InAt(0); 3016 Location second = locations->InAt(1); 3017 3018 Primitive::Type type = op->GetResultType(); 3019 switch (type) { 3020 case Primitive::kPrimInt: { 3021 Register out_reg = out.AsRegister<Register>(); 3022 Register first_reg = first.AsRegister<Register>(); 3023 // Arm doesn't mask the shift count so we need to do it ourselves. 3024 if (second.IsRegister()) { 3025 Register second_reg = second.AsRegister<Register>(); 3026 __ and_(out_reg, second_reg, ShifterOperand(kMaxIntShiftValue)); 3027 if (op->IsShl()) { 3028 __ Lsl(out_reg, first_reg, out_reg); 3029 } else if (op->IsShr()) { 3030 __ Asr(out_reg, first_reg, out_reg); 3031 } else { 3032 __ Lsr(out_reg, first_reg, out_reg); 3033 } 3034 } else { 3035 int32_t cst = second.GetConstant()->AsIntConstant()->GetValue(); 3036 uint32_t shift_value = static_cast<uint32_t>(cst & kMaxIntShiftValue); 3037 if (shift_value == 0) { // arm does not support shifting with 0 immediate. 3038 __ Mov(out_reg, first_reg); 3039 } else if (op->IsShl()) { 3040 __ Lsl(out_reg, first_reg, shift_value); 3041 } else if (op->IsShr()) { 3042 __ Asr(out_reg, first_reg, shift_value); 3043 } else { 3044 __ Lsr(out_reg, first_reg, shift_value); 3045 } 3046 } 3047 break; 3048 } 3049 case Primitive::kPrimLong: { 3050 Register o_h = out.AsRegisterPairHigh<Register>(); 3051 Register o_l = out.AsRegisterPairLow<Register>(); 3052 3053 Register temp = locations->GetTemp(0).AsRegister<Register>(); 3054 3055 Register high = first.AsRegisterPairHigh<Register>(); 3056 Register low = first.AsRegisterPairLow<Register>(); 3057 3058 Register second_reg = second.AsRegister<Register>(); 3059 3060 if (op->IsShl()) { 3061 __ and_(o_l, second_reg, ShifterOperand(kMaxLongShiftValue)); 3062 // Shift the high part 3063 __ Lsl(o_h, high, o_l); 3064 // Shift the low part and `or` what overflew on the high part 3065 __ rsb(temp, o_l, ShifterOperand(kArmBitsPerWord)); 3066 __ Lsr(temp, low, temp); 3067 __ orr(o_h, o_h, ShifterOperand(temp)); 3068 // If the shift is > 32 bits, override the high part 3069 __ subs(temp, o_l, ShifterOperand(kArmBitsPerWord)); 3070 __ it(PL); 3071 __ Lsl(o_h, low, temp, PL); 3072 // Shift the low part 3073 __ Lsl(o_l, low, o_l); 3074 } else if (op->IsShr()) { 3075 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue)); 3076 // Shift the low part 3077 __ Lsr(o_l, low, o_h); 3078 // Shift the high part and `or` what underflew on the low part 3079 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord)); 3080 __ Lsl(temp, high, temp); 3081 __ orr(o_l, o_l, ShifterOperand(temp)); 3082 // If the shift is > 32 bits, override the low part 3083 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord)); 3084 __ it(PL); 3085 __ Asr(o_l, high, temp, PL); 3086 // Shift the high part 3087 __ Asr(o_h, high, o_h); 3088 } else { 3089 __ and_(o_h, second_reg, ShifterOperand(kMaxLongShiftValue)); 3090 // same as Shr except we use `Lsr`s and not `Asr`s 3091 __ Lsr(o_l, low, o_h); 3092 __ rsb(temp, o_h, ShifterOperand(kArmBitsPerWord)); 3093 __ Lsl(temp, high, temp); 3094 __ orr(o_l, o_l, ShifterOperand(temp)); 3095 __ subs(temp, o_h, ShifterOperand(kArmBitsPerWord)); 3096 __ it(PL); 3097 __ Lsr(o_l, high, temp, PL); 3098 __ Lsr(o_h, high, o_h); 3099 } 3100 break; 3101 } 3102 default: 3103 LOG(FATAL) << "Unexpected operation type " << type; 3104 } 3105} 3106 3107void LocationsBuilderARM::VisitShl(HShl* shl) { 3108 HandleShift(shl); 3109} 3110 3111void InstructionCodeGeneratorARM::VisitShl(HShl* shl) { 3112 HandleShift(shl); 3113} 3114 3115void LocationsBuilderARM::VisitShr(HShr* shr) { 3116 HandleShift(shr); 3117} 3118 3119void InstructionCodeGeneratorARM::VisitShr(HShr* shr) { 3120 HandleShift(shr); 3121} 3122 3123void LocationsBuilderARM::VisitUShr(HUShr* ushr) { 3124 HandleShift(ushr); 3125} 3126 3127void InstructionCodeGeneratorARM::VisitUShr(HUShr* ushr) { 3128 HandleShift(ushr); 3129} 3130 3131void LocationsBuilderARM::VisitNewInstance(HNewInstance* instruction) { 3132 LocationSummary* locations = 3133 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall); 3134 InvokeRuntimeCallingConvention calling_convention; 3135 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0))); 3136 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); 3137 locations->SetOut(Location::RegisterLocation(R0)); 3138} 3139 3140void InstructionCodeGeneratorARM::VisitNewInstance(HNewInstance* instruction) { 3141 InvokeRuntimeCallingConvention calling_convention; 3142 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex()); 3143 // Note: if heap poisoning is enabled, the entry point takes cares 3144 // of poisoning the reference. 3145 codegen_->InvokeRuntime(instruction->GetEntrypoint(), 3146 instruction, 3147 instruction->GetDexPc(), 3148 nullptr); 3149} 3150 3151void LocationsBuilderARM::VisitNewArray(HNewArray* instruction) { 3152 LocationSummary* locations = 3153 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall); 3154 InvokeRuntimeCallingConvention calling_convention; 3155 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0))); 3156 locations->SetOut(Location::RegisterLocation(R0)); 3157 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); 3158 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2))); 3159} 3160 3161void InstructionCodeGeneratorARM::VisitNewArray(HNewArray* instruction) { 3162 InvokeRuntimeCallingConvention calling_convention; 3163 __ LoadImmediate(calling_convention.GetRegisterAt(0), instruction->GetTypeIndex()); 3164 // Note: if heap poisoning is enabled, the entry point takes cares 3165 // of poisoning the reference. 3166 codegen_->InvokeRuntime(instruction->GetEntrypoint(), 3167 instruction, 3168 instruction->GetDexPc(), 3169 nullptr); 3170} 3171 3172void LocationsBuilderARM::VisitParameterValue(HParameterValue* instruction) { 3173 LocationSummary* locations = 3174 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); 3175 Location location = parameter_visitor_.GetNextLocation(instruction->GetType()); 3176 if (location.IsStackSlot()) { 3177 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); 3178 } else if (location.IsDoubleStackSlot()) { 3179 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize()); 3180 } 3181 locations->SetOut(location); 3182} 3183 3184void InstructionCodeGeneratorARM::VisitParameterValue( 3185 HParameterValue* instruction ATTRIBUTE_UNUSED) { 3186 // Nothing to do, the parameter is already at its location. 3187} 3188 3189void LocationsBuilderARM::VisitCurrentMethod(HCurrentMethod* instruction) { 3190 LocationSummary* locations = 3191 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); 3192 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument)); 3193} 3194 3195void InstructionCodeGeneratorARM::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) { 3196 // Nothing to do, the method is already at its location. 3197} 3198 3199void LocationsBuilderARM::VisitNot(HNot* not_) { 3200 LocationSummary* locations = 3201 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall); 3202 locations->SetInAt(0, Location::RequiresRegister()); 3203 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 3204} 3205 3206void InstructionCodeGeneratorARM::VisitNot(HNot* not_) { 3207 LocationSummary* locations = not_->GetLocations(); 3208 Location out = locations->Out(); 3209 Location in = locations->InAt(0); 3210 switch (not_->GetResultType()) { 3211 case Primitive::kPrimInt: 3212 __ mvn(out.AsRegister<Register>(), ShifterOperand(in.AsRegister<Register>())); 3213 break; 3214 3215 case Primitive::kPrimLong: 3216 __ mvn(out.AsRegisterPairLow<Register>(), 3217 ShifterOperand(in.AsRegisterPairLow<Register>())); 3218 __ mvn(out.AsRegisterPairHigh<Register>(), 3219 ShifterOperand(in.AsRegisterPairHigh<Register>())); 3220 break; 3221 3222 default: 3223 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType(); 3224 } 3225} 3226 3227void LocationsBuilderARM::VisitBooleanNot(HBooleanNot* bool_not) { 3228 LocationSummary* locations = 3229 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall); 3230 locations->SetInAt(0, Location::RequiresRegister()); 3231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 3232} 3233 3234void InstructionCodeGeneratorARM::VisitBooleanNot(HBooleanNot* bool_not) { 3235 LocationSummary* locations = bool_not->GetLocations(); 3236 Location out = locations->Out(); 3237 Location in = locations->InAt(0); 3238 __ eor(out.AsRegister<Register>(), in.AsRegister<Register>(), ShifterOperand(1)); 3239} 3240 3241void LocationsBuilderARM::VisitCompare(HCompare* compare) { 3242 LocationSummary* locations = 3243 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall); 3244 switch (compare->InputAt(0)->GetType()) { 3245 case Primitive::kPrimLong: { 3246 locations->SetInAt(0, Location::RequiresRegister()); 3247 locations->SetInAt(1, Location::RequiresRegister()); 3248 // Output overlaps because it is written before doing the low comparison. 3249 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap); 3250 break; 3251 } 3252 case Primitive::kPrimFloat: 3253 case Primitive::kPrimDouble: { 3254 locations->SetInAt(0, Location::RequiresFpuRegister()); 3255 locations->SetInAt(1, Location::RequiresFpuRegister()); 3256 locations->SetOut(Location::RequiresRegister()); 3257 break; 3258 } 3259 default: 3260 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType(); 3261 } 3262} 3263 3264void InstructionCodeGeneratorARM::VisitCompare(HCompare* compare) { 3265 LocationSummary* locations = compare->GetLocations(); 3266 Register out = locations->Out().AsRegister<Register>(); 3267 Location left = locations->InAt(0); 3268 Location right = locations->InAt(1); 3269 3270 Label less, greater, done; 3271 Primitive::Type type = compare->InputAt(0)->GetType(); 3272 switch (type) { 3273 case Primitive::kPrimLong: { 3274 __ cmp(left.AsRegisterPairHigh<Register>(), 3275 ShifterOperand(right.AsRegisterPairHigh<Register>())); // Signed compare. 3276 __ b(&less, LT); 3277 __ b(&greater, GT); 3278 // Do LoadImmediate before the last `cmp`, as LoadImmediate might affect the status flags. 3279 __ LoadImmediate(out, 0); 3280 __ cmp(left.AsRegisterPairLow<Register>(), 3281 ShifterOperand(right.AsRegisterPairLow<Register>())); // Unsigned compare. 3282 break; 3283 } 3284 case Primitive::kPrimFloat: 3285 case Primitive::kPrimDouble: { 3286 __ LoadImmediate(out, 0); 3287 if (type == Primitive::kPrimFloat) { 3288 __ vcmps(left.AsFpuRegister<SRegister>(), right.AsFpuRegister<SRegister>()); 3289 } else { 3290 __ vcmpd(FromLowSToD(left.AsFpuRegisterPairLow<SRegister>()), 3291 FromLowSToD(right.AsFpuRegisterPairLow<SRegister>())); 3292 } 3293 __ vmstat(); // transfer FP status register to ARM APSR. 3294 __ b(compare->IsGtBias() ? &greater : &less, VS); // VS for unordered. 3295 break; 3296 } 3297 default: 3298 LOG(FATAL) << "Unexpected compare type " << type; 3299 } 3300 __ b(&done, EQ); 3301 __ b(&less, LO); // LO is for both: unsigned compare for longs and 'less than' for floats. 3302 3303 __ Bind(&greater); 3304 __ LoadImmediate(out, 1); 3305 __ b(&done); 3306 3307 __ Bind(&less); 3308 __ LoadImmediate(out, -1); 3309 3310 __ Bind(&done); 3311} 3312 3313void LocationsBuilderARM::VisitPhi(HPhi* instruction) { 3314 LocationSummary* locations = 3315 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); 3316 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) { 3317 locations->SetInAt(i, Location::Any()); 3318 } 3319 locations->SetOut(Location::Any()); 3320} 3321 3322void InstructionCodeGeneratorARM::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) { 3323 LOG(FATAL) << "Unreachable"; 3324} 3325 3326void InstructionCodeGeneratorARM::GenerateMemoryBarrier(MemBarrierKind kind) { 3327 // TODO (ported from quick): revisit Arm barrier kinds 3328 DmbOptions flavor = DmbOptions::ISH; // quiet c++ warnings 3329 switch (kind) { 3330 case MemBarrierKind::kAnyStore: 3331 case MemBarrierKind::kLoadAny: 3332 case MemBarrierKind::kAnyAny: { 3333 flavor = DmbOptions::ISH; 3334 break; 3335 } 3336 case MemBarrierKind::kStoreStore: { 3337 flavor = DmbOptions::ISHST; 3338 break; 3339 } 3340 default: 3341 LOG(FATAL) << "Unexpected memory barrier " << kind; 3342 } 3343 __ dmb(flavor); 3344} 3345 3346void InstructionCodeGeneratorARM::GenerateWideAtomicLoad(Register addr, 3347 uint32_t offset, 3348 Register out_lo, 3349 Register out_hi) { 3350 if (offset != 0) { 3351 __ LoadImmediate(out_lo, offset); 3352 __ add(IP, addr, ShifterOperand(out_lo)); 3353 addr = IP; 3354 } 3355 __ ldrexd(out_lo, out_hi, addr); 3356} 3357 3358void InstructionCodeGeneratorARM::GenerateWideAtomicStore(Register addr, 3359 uint32_t offset, 3360 Register value_lo, 3361 Register value_hi, 3362 Register temp1, 3363 Register temp2, 3364 HInstruction* instruction) { 3365 Label fail; 3366 if (offset != 0) { 3367 __ LoadImmediate(temp1, offset); 3368 __ add(IP, addr, ShifterOperand(temp1)); 3369 addr = IP; 3370 } 3371 __ Bind(&fail); 3372 // We need a load followed by store. (The address used in a STREX instruction must 3373 // be the same as the address in the most recently executed LDREX instruction.) 3374 __ ldrexd(temp1, temp2, addr); 3375 codegen_->MaybeRecordImplicitNullCheck(instruction); 3376 __ strexd(temp1, value_lo, value_hi, addr); 3377 __ CompareAndBranchIfNonZero(temp1, &fail); 3378} 3379 3380void LocationsBuilderARM::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) { 3381 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet()); 3382 3383 LocationSummary* locations = 3384 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); 3385 locations->SetInAt(0, Location::RequiresRegister()); 3386 3387 Primitive::Type field_type = field_info.GetFieldType(); 3388 if (Primitive::IsFloatingPointType(field_type)) { 3389 locations->SetInAt(1, Location::RequiresFpuRegister()); 3390 } else { 3391 locations->SetInAt(1, Location::RequiresRegister()); 3392 } 3393 3394 bool is_wide = field_type == Primitive::kPrimLong || field_type == Primitive::kPrimDouble; 3395 bool generate_volatile = field_info.IsVolatile() 3396 && is_wide 3397 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd(); 3398 bool needs_write_barrier = 3399 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1)); 3400 // Temporary registers for the write barrier. 3401 // TODO: consider renaming StoreNeedsWriteBarrier to StoreNeedsGCMark. 3402 if (needs_write_barrier) { 3403 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too. 3404 locations->AddTemp(Location::RequiresRegister()); 3405 } else if (generate_volatile) { 3406 // Arm encoding have some additional constraints for ldrexd/strexd: 3407 // - registers need to be consecutive 3408 // - the first register should be even but not R14. 3409 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever 3410 // enable Arm encoding. 3411 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet()); 3412 3413 locations->AddTemp(Location::RequiresRegister()); 3414 locations->AddTemp(Location::RequiresRegister()); 3415 if (field_type == Primitive::kPrimDouble) { 3416 // For doubles we need two more registers to copy the value. 3417 locations->AddTemp(Location::RegisterLocation(R2)); 3418 locations->AddTemp(Location::RegisterLocation(R3)); 3419 } 3420 } 3421} 3422 3423void InstructionCodeGeneratorARM::HandleFieldSet(HInstruction* instruction, 3424 const FieldInfo& field_info, 3425 bool value_can_be_null) { 3426 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet()); 3427 3428 LocationSummary* locations = instruction->GetLocations(); 3429 Register base = locations->InAt(0).AsRegister<Register>(); 3430 Location value = locations->InAt(1); 3431 3432 bool is_volatile = field_info.IsVolatile(); 3433 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd(); 3434 Primitive::Type field_type = field_info.GetFieldType(); 3435 uint32_t offset = field_info.GetFieldOffset().Uint32Value(); 3436 bool needs_write_barrier = 3437 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1)); 3438 3439 if (is_volatile) { 3440 GenerateMemoryBarrier(MemBarrierKind::kAnyStore); 3441 } 3442 3443 switch (field_type) { 3444 case Primitive::kPrimBoolean: 3445 case Primitive::kPrimByte: { 3446 __ StoreToOffset(kStoreByte, value.AsRegister<Register>(), base, offset); 3447 break; 3448 } 3449 3450 case Primitive::kPrimShort: 3451 case Primitive::kPrimChar: { 3452 __ StoreToOffset(kStoreHalfword, value.AsRegister<Register>(), base, offset); 3453 break; 3454 } 3455 3456 case Primitive::kPrimInt: 3457 case Primitive::kPrimNot: { 3458 if (kPoisonHeapReferences && needs_write_barrier) { 3459 // Note that in the case where `value` is a null reference, 3460 // we do not enter this block, as a null reference does not 3461 // need poisoning. 3462 DCHECK_EQ(field_type, Primitive::kPrimNot); 3463 Register temp = locations->GetTemp(0).AsRegister<Register>(); 3464 __ Mov(temp, value.AsRegister<Register>()); 3465 __ PoisonHeapReference(temp); 3466 __ StoreToOffset(kStoreWord, temp, base, offset); 3467 } else { 3468 __ StoreToOffset(kStoreWord, value.AsRegister<Register>(), base, offset); 3469 } 3470 break; 3471 } 3472 3473 case Primitive::kPrimLong: { 3474 if (is_volatile && !atomic_ldrd_strd) { 3475 GenerateWideAtomicStore(base, offset, 3476 value.AsRegisterPairLow<Register>(), 3477 value.AsRegisterPairHigh<Register>(), 3478 locations->GetTemp(0).AsRegister<Register>(), 3479 locations->GetTemp(1).AsRegister<Register>(), 3480 instruction); 3481 } else { 3482 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), base, offset); 3483 codegen_->MaybeRecordImplicitNullCheck(instruction); 3484 } 3485 break; 3486 } 3487 3488 case Primitive::kPrimFloat: { 3489 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), base, offset); 3490 break; 3491 } 3492 3493 case Primitive::kPrimDouble: { 3494 DRegister value_reg = FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()); 3495 if (is_volatile && !atomic_ldrd_strd) { 3496 Register value_reg_lo = locations->GetTemp(0).AsRegister<Register>(); 3497 Register value_reg_hi = locations->GetTemp(1).AsRegister<Register>(); 3498 3499 __ vmovrrd(value_reg_lo, value_reg_hi, value_reg); 3500 3501 GenerateWideAtomicStore(base, offset, 3502 value_reg_lo, 3503 value_reg_hi, 3504 locations->GetTemp(2).AsRegister<Register>(), 3505 locations->GetTemp(3).AsRegister<Register>(), 3506 instruction); 3507 } else { 3508 __ StoreDToOffset(value_reg, base, offset); 3509 codegen_->MaybeRecordImplicitNullCheck(instruction); 3510 } 3511 break; 3512 } 3513 3514 case Primitive::kPrimVoid: 3515 LOG(FATAL) << "Unreachable type " << field_type; 3516 UNREACHABLE(); 3517 } 3518 3519 // Longs and doubles are handled in the switch. 3520 if (field_type != Primitive::kPrimLong && field_type != Primitive::kPrimDouble) { 3521 codegen_->MaybeRecordImplicitNullCheck(instruction); 3522 } 3523 3524 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) { 3525 Register temp = locations->GetTemp(0).AsRegister<Register>(); 3526 Register card = locations->GetTemp(1).AsRegister<Register>(); 3527 codegen_->MarkGCCard( 3528 temp, card, base, value.AsRegister<Register>(), value_can_be_null); 3529 } 3530 3531 if (is_volatile) { 3532 GenerateMemoryBarrier(MemBarrierKind::kAnyAny); 3533 } 3534} 3535 3536void LocationsBuilderARM::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) { 3537 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet()); 3538 LocationSummary* locations = 3539 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); 3540 locations->SetInAt(0, Location::RequiresRegister()); 3541 3542 bool volatile_for_double = field_info.IsVolatile() 3543 && (field_info.GetFieldType() == Primitive::kPrimDouble) 3544 && !codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd(); 3545 bool overlap = field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong); 3546 3547 if (Primitive::IsFloatingPointType(instruction->GetType())) { 3548 locations->SetOut(Location::RequiresFpuRegister()); 3549 } else { 3550 locations->SetOut(Location::RequiresRegister(), 3551 (overlap ? Location::kOutputOverlap : Location::kNoOutputOverlap)); 3552 } 3553 if (volatile_for_double) { 3554 // Arm encoding have some additional constraints for ldrexd/strexd: 3555 // - registers need to be consecutive 3556 // - the first register should be even but not R14. 3557 // We don't test for Arm yet, and the assertion makes sure that we revisit this if we ever 3558 // enable Arm encoding. 3559 DCHECK_EQ(InstructionSet::kThumb2, codegen_->GetInstructionSet()); 3560 locations->AddTemp(Location::RequiresRegister()); 3561 locations->AddTemp(Location::RequiresRegister()); 3562 } 3563} 3564 3565Location LocationsBuilderARM::ArmEncodableConstantOrRegister(HInstruction* constant, 3566 Opcode opcode) { 3567 DCHECK(!Primitive::IsFloatingPointType(constant->GetType())); 3568 if (constant->IsConstant() && 3569 CanEncodeConstantAsImmediate(constant->AsConstant(), opcode)) { 3570 return Location::ConstantLocation(constant->AsConstant()); 3571 } 3572 return Location::RequiresRegister(); 3573} 3574 3575bool LocationsBuilderARM::CanEncodeConstantAsImmediate(HConstant* input_cst, 3576 Opcode opcode) { 3577 uint64_t value = static_cast<uint64_t>(Int64FromConstant(input_cst)); 3578 if (Primitive::Is64BitType(input_cst->GetType())) { 3579 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode) && 3580 CanEncodeConstantAsImmediate(High32Bits(value), opcode); 3581 } else { 3582 return CanEncodeConstantAsImmediate(Low32Bits(value), opcode); 3583 } 3584} 3585 3586bool LocationsBuilderARM::CanEncodeConstantAsImmediate(uint32_t value, Opcode opcode) { 3587 ShifterOperand so; 3588 ArmAssembler* assembler = codegen_->GetAssembler(); 3589 if (assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, opcode, value, &so)) { 3590 return true; 3591 } 3592 Opcode neg_opcode = kNoOperand; 3593 switch (opcode) { 3594 case AND: 3595 neg_opcode = BIC; 3596 break; 3597 case ORR: 3598 neg_opcode = ORN; 3599 break; 3600 default: 3601 return false; 3602 } 3603 return assembler->ShifterOperandCanHold(kNoRegister, kNoRegister, neg_opcode, ~value, &so); 3604} 3605 3606void InstructionCodeGeneratorARM::HandleFieldGet(HInstruction* instruction, 3607 const FieldInfo& field_info) { 3608 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet()); 3609 3610 LocationSummary* locations = instruction->GetLocations(); 3611 Register base = locations->InAt(0).AsRegister<Register>(); 3612 Location out = locations->Out(); 3613 bool is_volatile = field_info.IsVolatile(); 3614 bool atomic_ldrd_strd = codegen_->GetInstructionSetFeatures().HasAtomicLdrdAndStrd(); 3615 Primitive::Type field_type = field_info.GetFieldType(); 3616 uint32_t offset = field_info.GetFieldOffset().Uint32Value(); 3617 3618 switch (field_type) { 3619 case Primitive::kPrimBoolean: { 3620 __ LoadFromOffset(kLoadUnsignedByte, out.AsRegister<Register>(), base, offset); 3621 break; 3622 } 3623 3624 case Primitive::kPrimByte: { 3625 __ LoadFromOffset(kLoadSignedByte, out.AsRegister<Register>(), base, offset); 3626 break; 3627 } 3628 3629 case Primitive::kPrimShort: { 3630 __ LoadFromOffset(kLoadSignedHalfword, out.AsRegister<Register>(), base, offset); 3631 break; 3632 } 3633 3634 case Primitive::kPrimChar: { 3635 __ LoadFromOffset(kLoadUnsignedHalfword, out.AsRegister<Register>(), base, offset); 3636 break; 3637 } 3638 3639 case Primitive::kPrimInt: 3640 case Primitive::kPrimNot: { 3641 __ LoadFromOffset(kLoadWord, out.AsRegister<Register>(), base, offset); 3642 break; 3643 } 3644 3645 case Primitive::kPrimLong: { 3646 if (is_volatile && !atomic_ldrd_strd) { 3647 GenerateWideAtomicLoad(base, offset, 3648 out.AsRegisterPairLow<Register>(), 3649 out.AsRegisterPairHigh<Register>()); 3650 } else { 3651 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), base, offset); 3652 } 3653 break; 3654 } 3655 3656 case Primitive::kPrimFloat: { 3657 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), base, offset); 3658 break; 3659 } 3660 3661 case Primitive::kPrimDouble: { 3662 DRegister out_reg = FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()); 3663 if (is_volatile && !atomic_ldrd_strd) { 3664 Register lo = locations->GetTemp(0).AsRegister<Register>(); 3665 Register hi = locations->GetTemp(1).AsRegister<Register>(); 3666 GenerateWideAtomicLoad(base, offset, lo, hi); 3667 codegen_->MaybeRecordImplicitNullCheck(instruction); 3668 __ vmovdrr(out_reg, lo, hi); 3669 } else { 3670 __ LoadDFromOffset(out_reg, base, offset); 3671 codegen_->MaybeRecordImplicitNullCheck(instruction); 3672 } 3673 break; 3674 } 3675 3676 case Primitive::kPrimVoid: 3677 LOG(FATAL) << "Unreachable type " << field_type; 3678 UNREACHABLE(); 3679 } 3680 3681 // Doubles are handled in the switch. 3682 if (field_type != Primitive::kPrimDouble) { 3683 codegen_->MaybeRecordImplicitNullCheck(instruction); 3684 } 3685 3686 if (is_volatile) { 3687 GenerateMemoryBarrier(MemBarrierKind::kLoadAny); 3688 } 3689 3690 if (field_type == Primitive::kPrimNot) { 3691 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>()); 3692 } 3693} 3694 3695void LocationsBuilderARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { 3696 HandleFieldSet(instruction, instruction->GetFieldInfo()); 3697} 3698 3699void InstructionCodeGeneratorARM::VisitInstanceFieldSet(HInstanceFieldSet* instruction) { 3700 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull()); 3701} 3702 3703void LocationsBuilderARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) { 3704 HandleFieldGet(instruction, instruction->GetFieldInfo()); 3705} 3706 3707void InstructionCodeGeneratorARM::VisitInstanceFieldGet(HInstanceFieldGet* instruction) { 3708 HandleFieldGet(instruction, instruction->GetFieldInfo()); 3709} 3710 3711void LocationsBuilderARM::VisitStaticFieldGet(HStaticFieldGet* instruction) { 3712 HandleFieldGet(instruction, instruction->GetFieldInfo()); 3713} 3714 3715void InstructionCodeGeneratorARM::VisitStaticFieldGet(HStaticFieldGet* instruction) { 3716 HandleFieldGet(instruction, instruction->GetFieldInfo()); 3717} 3718 3719void LocationsBuilderARM::VisitStaticFieldSet(HStaticFieldSet* instruction) { 3720 HandleFieldSet(instruction, instruction->GetFieldInfo()); 3721} 3722 3723void InstructionCodeGeneratorARM::VisitStaticFieldSet(HStaticFieldSet* instruction) { 3724 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull()); 3725} 3726 3727void LocationsBuilderARM::VisitUnresolvedInstanceFieldGet( 3728 HUnresolvedInstanceFieldGet* instruction) { 3729 FieldAccessCallingConventionARM calling_convention; 3730 codegen_->CreateUnresolvedFieldLocationSummary( 3731 instruction, instruction->GetFieldType(), calling_convention); 3732} 3733 3734void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldGet( 3735 HUnresolvedInstanceFieldGet* instruction) { 3736 FieldAccessCallingConventionARM calling_convention; 3737 codegen_->GenerateUnresolvedFieldAccess(instruction, 3738 instruction->GetFieldType(), 3739 instruction->GetFieldIndex(), 3740 instruction->GetDexPc(), 3741 calling_convention); 3742} 3743 3744void LocationsBuilderARM::VisitUnresolvedInstanceFieldSet( 3745 HUnresolvedInstanceFieldSet* instruction) { 3746 FieldAccessCallingConventionARM calling_convention; 3747 codegen_->CreateUnresolvedFieldLocationSummary( 3748 instruction, instruction->GetFieldType(), calling_convention); 3749} 3750 3751void InstructionCodeGeneratorARM::VisitUnresolvedInstanceFieldSet( 3752 HUnresolvedInstanceFieldSet* instruction) { 3753 FieldAccessCallingConventionARM calling_convention; 3754 codegen_->GenerateUnresolvedFieldAccess(instruction, 3755 instruction->GetFieldType(), 3756 instruction->GetFieldIndex(), 3757 instruction->GetDexPc(), 3758 calling_convention); 3759} 3760 3761void LocationsBuilderARM::VisitUnresolvedStaticFieldGet( 3762 HUnresolvedStaticFieldGet* instruction) { 3763 FieldAccessCallingConventionARM calling_convention; 3764 codegen_->CreateUnresolvedFieldLocationSummary( 3765 instruction, instruction->GetFieldType(), calling_convention); 3766} 3767 3768void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldGet( 3769 HUnresolvedStaticFieldGet* instruction) { 3770 FieldAccessCallingConventionARM calling_convention; 3771 codegen_->GenerateUnresolvedFieldAccess(instruction, 3772 instruction->GetFieldType(), 3773 instruction->GetFieldIndex(), 3774 instruction->GetDexPc(), 3775 calling_convention); 3776} 3777 3778void LocationsBuilderARM::VisitUnresolvedStaticFieldSet( 3779 HUnresolvedStaticFieldSet* instruction) { 3780 FieldAccessCallingConventionARM calling_convention; 3781 codegen_->CreateUnresolvedFieldLocationSummary( 3782 instruction, instruction->GetFieldType(), calling_convention); 3783} 3784 3785void InstructionCodeGeneratorARM::VisitUnresolvedStaticFieldSet( 3786 HUnresolvedStaticFieldSet* instruction) { 3787 FieldAccessCallingConventionARM calling_convention; 3788 codegen_->GenerateUnresolvedFieldAccess(instruction, 3789 instruction->GetFieldType(), 3790 instruction->GetFieldIndex(), 3791 instruction->GetDexPc(), 3792 calling_convention); 3793} 3794 3795void LocationsBuilderARM::VisitNullCheck(HNullCheck* instruction) { 3796 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock() 3797 ? LocationSummary::kCallOnSlowPath 3798 : LocationSummary::kNoCall; 3799 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind); 3800 locations->SetInAt(0, Location::RequiresRegister()); 3801 if (instruction->HasUses()) { 3802 locations->SetOut(Location::SameAsFirstInput()); 3803 } 3804} 3805 3806void InstructionCodeGeneratorARM::GenerateImplicitNullCheck(HNullCheck* instruction) { 3807 if (codegen_->CanMoveNullCheckToUser(instruction)) { 3808 return; 3809 } 3810 Location obj = instruction->GetLocations()->InAt(0); 3811 3812 __ LoadFromOffset(kLoadWord, IP, obj.AsRegister<Register>(), 0); 3813 codegen_->RecordPcInfo(instruction, instruction->GetDexPc()); 3814} 3815 3816void InstructionCodeGeneratorARM::GenerateExplicitNullCheck(HNullCheck* instruction) { 3817 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathARM(instruction); 3818 codegen_->AddSlowPath(slow_path); 3819 3820 LocationSummary* locations = instruction->GetLocations(); 3821 Location obj = locations->InAt(0); 3822 3823 __ CompareAndBranchIfZero(obj.AsRegister<Register>(), slow_path->GetEntryLabel()); 3824} 3825 3826void InstructionCodeGeneratorARM::VisitNullCheck(HNullCheck* instruction) { 3827 if (codegen_->IsImplicitNullCheckAllowed(instruction)) { 3828 GenerateImplicitNullCheck(instruction); 3829 } else { 3830 GenerateExplicitNullCheck(instruction); 3831 } 3832} 3833 3834void LocationsBuilderARM::VisitArrayGet(HArrayGet* instruction) { 3835 LocationSummary* locations = 3836 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); 3837 locations->SetInAt(0, Location::RequiresRegister()); 3838 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1))); 3839 if (Primitive::IsFloatingPointType(instruction->GetType())) { 3840 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap); 3841 } else { 3842 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 3843 } 3844} 3845 3846void InstructionCodeGeneratorARM::VisitArrayGet(HArrayGet* instruction) { 3847 LocationSummary* locations = instruction->GetLocations(); 3848 Register obj = locations->InAt(0).AsRegister<Register>(); 3849 Location index = locations->InAt(1); 3850 Primitive::Type type = instruction->GetType(); 3851 3852 switch (type) { 3853 case Primitive::kPrimBoolean: { 3854 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value(); 3855 Register out = locations->Out().AsRegister<Register>(); 3856 if (index.IsConstant()) { 3857 size_t offset = 3858 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset; 3859 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset); 3860 } else { 3861 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>())); 3862 __ LoadFromOffset(kLoadUnsignedByte, out, IP, data_offset); 3863 } 3864 break; 3865 } 3866 3867 case Primitive::kPrimByte: { 3868 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value(); 3869 Register out = locations->Out().AsRegister<Register>(); 3870 if (index.IsConstant()) { 3871 size_t offset = 3872 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset; 3873 __ LoadFromOffset(kLoadSignedByte, out, obj, offset); 3874 } else { 3875 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>())); 3876 __ LoadFromOffset(kLoadSignedByte, out, IP, data_offset); 3877 } 3878 break; 3879 } 3880 3881 case Primitive::kPrimShort: { 3882 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value(); 3883 Register out = locations->Out().AsRegister<Register>(); 3884 if (index.IsConstant()) { 3885 size_t offset = 3886 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset; 3887 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset); 3888 } else { 3889 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2)); 3890 __ LoadFromOffset(kLoadSignedHalfword, out, IP, data_offset); 3891 } 3892 break; 3893 } 3894 3895 case Primitive::kPrimChar: { 3896 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value(); 3897 Register out = locations->Out().AsRegister<Register>(); 3898 if (index.IsConstant()) { 3899 size_t offset = 3900 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset; 3901 __ LoadFromOffset(kLoadUnsignedHalfword, out, obj, offset); 3902 } else { 3903 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2)); 3904 __ LoadFromOffset(kLoadUnsignedHalfword, out, IP, data_offset); 3905 } 3906 break; 3907 } 3908 3909 case Primitive::kPrimInt: 3910 case Primitive::kPrimNot: { 3911 static_assert(sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t), 3912 "art::mirror::HeapReference<mirror::Object> and int32_t have different sizes."); 3913 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value(); 3914 Register out = locations->Out().AsRegister<Register>(); 3915 if (index.IsConstant()) { 3916 size_t offset = 3917 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset; 3918 __ LoadFromOffset(kLoadWord, out, obj, offset); 3919 } else { 3920 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4)); 3921 __ LoadFromOffset(kLoadWord, out, IP, data_offset); 3922 } 3923 break; 3924 } 3925 3926 case Primitive::kPrimLong: { 3927 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value(); 3928 Location out = locations->Out(); 3929 if (index.IsConstant()) { 3930 size_t offset = 3931 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset; 3932 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), obj, offset); 3933 } else { 3934 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8)); 3935 __ LoadFromOffset(kLoadWordPair, out.AsRegisterPairLow<Register>(), IP, data_offset); 3936 } 3937 break; 3938 } 3939 3940 case Primitive::kPrimFloat: { 3941 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value(); 3942 Location out = locations->Out(); 3943 DCHECK(out.IsFpuRegister()); 3944 if (index.IsConstant()) { 3945 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset; 3946 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), obj, offset); 3947 } else { 3948 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4)); 3949 __ LoadSFromOffset(out.AsFpuRegister<SRegister>(), IP, data_offset); 3950 } 3951 break; 3952 } 3953 3954 case Primitive::kPrimDouble: { 3955 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value(); 3956 Location out = locations->Out(); 3957 DCHECK(out.IsFpuRegisterPair()); 3958 if (index.IsConstant()) { 3959 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset; 3960 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), obj, offset); 3961 } else { 3962 __ add(IP, obj, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8)); 3963 __ LoadDFromOffset(FromLowSToD(out.AsFpuRegisterPairLow<SRegister>()), IP, data_offset); 3964 } 3965 break; 3966 } 3967 3968 case Primitive::kPrimVoid: 3969 LOG(FATAL) << "Unreachable type " << type; 3970 UNREACHABLE(); 3971 } 3972 codegen_->MaybeRecordImplicitNullCheck(instruction); 3973 3974 if (type == Primitive::kPrimNot) { 3975 Register out = locations->Out().AsRegister<Register>(); 3976 __ MaybeUnpoisonHeapReference(out); 3977 } 3978} 3979 3980void LocationsBuilderARM::VisitArraySet(HArraySet* instruction) { 3981 Primitive::Type value_type = instruction->GetComponentType(); 3982 3983 bool needs_write_barrier = 3984 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue()); 3985 bool may_need_runtime_call = instruction->NeedsTypeCheck(); 3986 3987 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary( 3988 instruction, 3989 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall); 3990 locations->SetInAt(0, Location::RequiresRegister()); 3991 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1))); 3992 if (Primitive::IsFloatingPointType(value_type)) { 3993 locations->SetInAt(2, Location::RequiresFpuRegister()); 3994 } else { 3995 locations->SetInAt(2, Location::RequiresRegister()); 3996 } 3997 3998 if (needs_write_barrier) { 3999 // Temporary registers for the write barrier. 4000 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too. 4001 locations->AddTemp(Location::RequiresRegister()); 4002 } 4003} 4004 4005void InstructionCodeGeneratorARM::VisitArraySet(HArraySet* instruction) { 4006 LocationSummary* locations = instruction->GetLocations(); 4007 Register array = locations->InAt(0).AsRegister<Register>(); 4008 Location index = locations->InAt(1); 4009 Primitive::Type value_type = instruction->GetComponentType(); 4010 bool may_need_runtime_call = locations->CanCall(); 4011 bool needs_write_barrier = 4012 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue()); 4013 4014 switch (value_type) { 4015 case Primitive::kPrimBoolean: 4016 case Primitive::kPrimByte: { 4017 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value(); 4018 Register value = locations->InAt(2).AsRegister<Register>(); 4019 if (index.IsConstant()) { 4020 size_t offset = 4021 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset; 4022 __ StoreToOffset(kStoreByte, value, array, offset); 4023 } else { 4024 __ add(IP, array, ShifterOperand(index.AsRegister<Register>())); 4025 __ StoreToOffset(kStoreByte, value, IP, data_offset); 4026 } 4027 break; 4028 } 4029 4030 case Primitive::kPrimShort: 4031 case Primitive::kPrimChar: { 4032 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value(); 4033 Register value = locations->InAt(2).AsRegister<Register>(); 4034 if (index.IsConstant()) { 4035 size_t offset = 4036 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset; 4037 __ StoreToOffset(kStoreHalfword, value, array, offset); 4038 } else { 4039 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_2)); 4040 __ StoreToOffset(kStoreHalfword, value, IP, data_offset); 4041 } 4042 break; 4043 } 4044 4045 case Primitive::kPrimNot: { 4046 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value(); 4047 Register value = locations->InAt(2).AsRegister<Register>(); 4048 Register source = value; 4049 4050 if (instruction->InputAt(2)->IsNullConstant()) { 4051 // Just setting null. 4052 if (index.IsConstant()) { 4053 size_t offset = 4054 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset; 4055 __ StoreToOffset(kStoreWord, source, array, offset); 4056 } else { 4057 DCHECK(index.IsRegister()) << index; 4058 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4)); 4059 __ StoreToOffset(kStoreWord, source, IP, data_offset); 4060 } 4061 break; 4062 } 4063 4064 DCHECK(needs_write_barrier); 4065 Register temp1 = locations->GetTemp(0).AsRegister<Register>(); 4066 Register temp2 = locations->GetTemp(1).AsRegister<Register>(); 4067 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); 4068 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value(); 4069 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value(); 4070 Label done; 4071 SlowPathCode* slow_path = nullptr; 4072 4073 if (may_need_runtime_call) { 4074 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathARM(instruction); 4075 codegen_->AddSlowPath(slow_path); 4076 if (instruction->GetValueCanBeNull()) { 4077 Label non_zero; 4078 __ CompareAndBranchIfNonZero(value, &non_zero); 4079 if (index.IsConstant()) { 4080 size_t offset = 4081 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset; 4082 __ StoreToOffset(kStoreWord, value, array, offset); 4083 } else { 4084 DCHECK(index.IsRegister()) << index; 4085 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4)); 4086 __ StoreToOffset(kStoreWord, value, IP, data_offset); 4087 } 4088 codegen_->MaybeRecordImplicitNullCheck(instruction); 4089 __ b(&done); 4090 __ Bind(&non_zero); 4091 } 4092 4093 __ LoadFromOffset(kLoadWord, temp1, array, class_offset); 4094 codegen_->MaybeRecordImplicitNullCheck(instruction); 4095 __ MaybeUnpoisonHeapReference(temp1); 4096 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset); 4097 __ LoadFromOffset(kLoadWord, temp2, value, class_offset); 4098 // No need to poison/unpoison, we're comparing two poisoined references. 4099 __ cmp(temp1, ShifterOperand(temp2)); 4100 if (instruction->StaticTypeOfArrayIsObjectArray()) { 4101 Label do_put; 4102 __ b(&do_put, EQ); 4103 __ MaybeUnpoisonHeapReference(temp1); 4104 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset); 4105 // No need to poison/unpoison, we're comparing against null. 4106 __ CompareAndBranchIfNonZero(temp1, slow_path->GetEntryLabel()); 4107 __ Bind(&do_put); 4108 } else { 4109 __ b(slow_path->GetEntryLabel(), NE); 4110 } 4111 } 4112 4113 if (kPoisonHeapReferences) { 4114 // Note that in the case where `value` is a null reference, 4115 // we do not enter this block, as a null reference does not 4116 // need poisoning. 4117 DCHECK_EQ(value_type, Primitive::kPrimNot); 4118 __ Mov(temp1, value); 4119 __ PoisonHeapReference(temp1); 4120 source = temp1; 4121 } 4122 4123 if (index.IsConstant()) { 4124 size_t offset = 4125 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset; 4126 __ StoreToOffset(kStoreWord, source, array, offset); 4127 } else { 4128 DCHECK(index.IsRegister()) << index; 4129 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4)); 4130 __ StoreToOffset(kStoreWord, source, IP, data_offset); 4131 } 4132 4133 if (!may_need_runtime_call) { 4134 codegen_->MaybeRecordImplicitNullCheck(instruction); 4135 } 4136 4137 codegen_->MarkGCCard(temp1, temp2, array, value, instruction->GetValueCanBeNull()); 4138 4139 if (done.IsLinked()) { 4140 __ Bind(&done); 4141 } 4142 4143 if (slow_path != nullptr) { 4144 __ Bind(slow_path->GetExitLabel()); 4145 } 4146 4147 break; 4148 } 4149 4150 case Primitive::kPrimInt: { 4151 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value(); 4152 Register value = locations->InAt(2).AsRegister<Register>(); 4153 if (index.IsConstant()) { 4154 size_t offset = 4155 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset; 4156 __ StoreToOffset(kStoreWord, value, array, offset); 4157 } else { 4158 DCHECK(index.IsRegister()) << index; 4159 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4)); 4160 __ StoreToOffset(kStoreWord, value, IP, data_offset); 4161 } 4162 4163 codegen_->MaybeRecordImplicitNullCheck(instruction); 4164 break; 4165 } 4166 4167 case Primitive::kPrimLong: { 4168 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value(); 4169 Location value = locations->InAt(2); 4170 if (index.IsConstant()) { 4171 size_t offset = 4172 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset; 4173 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), array, offset); 4174 } else { 4175 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8)); 4176 __ StoreToOffset(kStoreWordPair, value.AsRegisterPairLow<Register>(), IP, data_offset); 4177 } 4178 break; 4179 } 4180 4181 case Primitive::kPrimFloat: { 4182 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value(); 4183 Location value = locations->InAt(2); 4184 DCHECK(value.IsFpuRegister()); 4185 if (index.IsConstant()) { 4186 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset; 4187 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), array, offset); 4188 } else { 4189 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_4)); 4190 __ StoreSToOffset(value.AsFpuRegister<SRegister>(), IP, data_offset); 4191 } 4192 break; 4193 } 4194 4195 case Primitive::kPrimDouble: { 4196 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value(); 4197 Location value = locations->InAt(2); 4198 DCHECK(value.IsFpuRegisterPair()); 4199 if (index.IsConstant()) { 4200 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset; 4201 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), array, offset); 4202 } else { 4203 __ add(IP, array, ShifterOperand(index.AsRegister<Register>(), LSL, TIMES_8)); 4204 __ StoreDToOffset(FromLowSToD(value.AsFpuRegisterPairLow<SRegister>()), IP, data_offset); 4205 } 4206 4207 break; 4208 } 4209 4210 case Primitive::kPrimVoid: 4211 LOG(FATAL) << "Unreachable type " << value_type; 4212 UNREACHABLE(); 4213 } 4214 4215 // Ints and objects are handled in the switch. 4216 if (value_type != Primitive::kPrimInt && value_type != Primitive::kPrimNot) { 4217 codegen_->MaybeRecordImplicitNullCheck(instruction); 4218 } 4219} 4220 4221void LocationsBuilderARM::VisitArrayLength(HArrayLength* instruction) { 4222 LocationSummary* locations = 4223 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); 4224 locations->SetInAt(0, Location::RequiresRegister()); 4225 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 4226} 4227 4228void InstructionCodeGeneratorARM::VisitArrayLength(HArrayLength* instruction) { 4229 LocationSummary* locations = instruction->GetLocations(); 4230 uint32_t offset = mirror::Array::LengthOffset().Uint32Value(); 4231 Register obj = locations->InAt(0).AsRegister<Register>(); 4232 Register out = locations->Out().AsRegister<Register>(); 4233 __ LoadFromOffset(kLoadWord, out, obj, offset); 4234 codegen_->MaybeRecordImplicitNullCheck(instruction); 4235} 4236 4237void LocationsBuilderARM::VisitBoundsCheck(HBoundsCheck* instruction) { 4238 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock() 4239 ? LocationSummary::kCallOnSlowPath 4240 : LocationSummary::kNoCall; 4241 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind); 4242 locations->SetInAt(0, Location::RequiresRegister()); 4243 locations->SetInAt(1, Location::RequiresRegister()); 4244 if (instruction->HasUses()) { 4245 locations->SetOut(Location::SameAsFirstInput()); 4246 } 4247} 4248 4249void InstructionCodeGeneratorARM::VisitBoundsCheck(HBoundsCheck* instruction) { 4250 LocationSummary* locations = instruction->GetLocations(); 4251 SlowPathCode* slow_path = 4252 new (GetGraph()->GetArena()) BoundsCheckSlowPathARM(instruction); 4253 codegen_->AddSlowPath(slow_path); 4254 4255 Register index = locations->InAt(0).AsRegister<Register>(); 4256 Register length = locations->InAt(1).AsRegister<Register>(); 4257 4258 __ cmp(index, ShifterOperand(length)); 4259 __ b(slow_path->GetEntryLabel(), HS); 4260} 4261 4262void CodeGeneratorARM::MarkGCCard(Register temp, 4263 Register card, 4264 Register object, 4265 Register value, 4266 bool can_be_null) { 4267 Label is_null; 4268 if (can_be_null) { 4269 __ CompareAndBranchIfZero(value, &is_null); 4270 } 4271 __ LoadFromOffset(kLoadWord, card, TR, Thread::CardTableOffset<kArmWordSize>().Int32Value()); 4272 __ Lsr(temp, object, gc::accounting::CardTable::kCardShift); 4273 __ strb(card, Address(card, temp)); 4274 if (can_be_null) { 4275 __ Bind(&is_null); 4276 } 4277} 4278 4279void LocationsBuilderARM::VisitTemporary(HTemporary* temp) { 4280 temp->SetLocations(nullptr); 4281} 4282 4283void InstructionCodeGeneratorARM::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) { 4284 // Nothing to do, this is driven by the code generator. 4285} 4286 4287void LocationsBuilderARM::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) { 4288 LOG(FATAL) << "Unreachable"; 4289} 4290 4291void InstructionCodeGeneratorARM::VisitParallelMove(HParallelMove* instruction) { 4292 codegen_->GetMoveResolver()->EmitNativeCode(instruction); 4293} 4294 4295void LocationsBuilderARM::VisitSuspendCheck(HSuspendCheck* instruction) { 4296 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath); 4297} 4298 4299void InstructionCodeGeneratorARM::VisitSuspendCheck(HSuspendCheck* instruction) { 4300 HBasicBlock* block = instruction->GetBlock(); 4301 if (block->GetLoopInformation() != nullptr) { 4302 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction); 4303 // The back edge will generate the suspend check. 4304 return; 4305 } 4306 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) { 4307 // The goto will generate the suspend check. 4308 return; 4309 } 4310 GenerateSuspendCheck(instruction, nullptr); 4311} 4312 4313void InstructionCodeGeneratorARM::GenerateSuspendCheck(HSuspendCheck* instruction, 4314 HBasicBlock* successor) { 4315 SuspendCheckSlowPathARM* slow_path = 4316 down_cast<SuspendCheckSlowPathARM*>(instruction->GetSlowPath()); 4317 if (slow_path == nullptr) { 4318 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathARM(instruction, successor); 4319 instruction->SetSlowPath(slow_path); 4320 codegen_->AddSlowPath(slow_path); 4321 if (successor != nullptr) { 4322 DCHECK(successor->IsLoopHeader()); 4323 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction); 4324 } 4325 } else { 4326 DCHECK_EQ(slow_path->GetSuccessor(), successor); 4327 } 4328 4329 __ LoadFromOffset( 4330 kLoadUnsignedHalfword, IP, TR, Thread::ThreadFlagsOffset<kArmWordSize>().Int32Value()); 4331 if (successor == nullptr) { 4332 __ CompareAndBranchIfNonZero(IP, slow_path->GetEntryLabel()); 4333 __ Bind(slow_path->GetReturnLabel()); 4334 } else { 4335 __ CompareAndBranchIfZero(IP, codegen_->GetLabelOf(successor)); 4336 __ b(slow_path->GetEntryLabel()); 4337 } 4338} 4339 4340ArmAssembler* ParallelMoveResolverARM::GetAssembler() const { 4341 return codegen_->GetAssembler(); 4342} 4343 4344void ParallelMoveResolverARM::EmitMove(size_t index) { 4345 MoveOperands* move = moves_[index]; 4346 Location source = move->GetSource(); 4347 Location destination = move->GetDestination(); 4348 4349 if (source.IsRegister()) { 4350 if (destination.IsRegister()) { 4351 __ Mov(destination.AsRegister<Register>(), source.AsRegister<Register>()); 4352 } else { 4353 DCHECK(destination.IsStackSlot()); 4354 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), 4355 SP, destination.GetStackIndex()); 4356 } 4357 } else if (source.IsStackSlot()) { 4358 if (destination.IsRegister()) { 4359 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), 4360 SP, source.GetStackIndex()); 4361 } else if (destination.IsFpuRegister()) { 4362 __ LoadSFromOffset(destination.AsFpuRegister<SRegister>(), SP, source.GetStackIndex()); 4363 } else { 4364 DCHECK(destination.IsStackSlot()); 4365 __ LoadFromOffset(kLoadWord, IP, SP, source.GetStackIndex()); 4366 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex()); 4367 } 4368 } else if (source.IsFpuRegister()) { 4369 if (destination.IsFpuRegister()) { 4370 __ vmovs(destination.AsFpuRegister<SRegister>(), source.AsFpuRegister<SRegister>()); 4371 } else { 4372 DCHECK(destination.IsStackSlot()); 4373 __ StoreSToOffset(source.AsFpuRegister<SRegister>(), SP, destination.GetStackIndex()); 4374 } 4375 } else if (source.IsDoubleStackSlot()) { 4376 if (destination.IsDoubleStackSlot()) { 4377 __ LoadDFromOffset(DTMP, SP, source.GetStackIndex()); 4378 __ StoreDToOffset(DTMP, SP, destination.GetStackIndex()); 4379 } else if (destination.IsRegisterPair()) { 4380 DCHECK(ExpectedPairLayout(destination)); 4381 __ LoadFromOffset( 4382 kLoadWordPair, destination.AsRegisterPairLow<Register>(), SP, source.GetStackIndex()); 4383 } else { 4384 DCHECK(destination.IsFpuRegisterPair()) << destination; 4385 __ LoadDFromOffset(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), 4386 SP, 4387 source.GetStackIndex()); 4388 } 4389 } else if (source.IsRegisterPair()) { 4390 if (destination.IsRegisterPair()) { 4391 __ Mov(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>()); 4392 __ Mov(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>()); 4393 } else { 4394 DCHECK(destination.IsDoubleStackSlot()) << destination; 4395 DCHECK(ExpectedPairLayout(source)); 4396 __ StoreToOffset( 4397 kStoreWordPair, source.AsRegisterPairLow<Register>(), SP, destination.GetStackIndex()); 4398 } 4399 } else if (source.IsFpuRegisterPair()) { 4400 if (destination.IsFpuRegisterPair()) { 4401 __ vmovd(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), 4402 FromLowSToD(source.AsFpuRegisterPairLow<SRegister>())); 4403 } else { 4404 DCHECK(destination.IsDoubleStackSlot()) << destination; 4405 __ StoreDToOffset(FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()), 4406 SP, 4407 destination.GetStackIndex()); 4408 } 4409 } else { 4410 DCHECK(source.IsConstant()) << source; 4411 HConstant* constant = source.GetConstant(); 4412 if (constant->IsIntConstant() || constant->IsNullConstant()) { 4413 int32_t value = CodeGenerator::GetInt32ValueOf(constant); 4414 if (destination.IsRegister()) { 4415 __ LoadImmediate(destination.AsRegister<Register>(), value); 4416 } else { 4417 DCHECK(destination.IsStackSlot()); 4418 __ LoadImmediate(IP, value); 4419 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex()); 4420 } 4421 } else if (constant->IsLongConstant()) { 4422 int64_t value = constant->AsLongConstant()->GetValue(); 4423 if (destination.IsRegisterPair()) { 4424 __ LoadImmediate(destination.AsRegisterPairLow<Register>(), Low32Bits(value)); 4425 __ LoadImmediate(destination.AsRegisterPairHigh<Register>(), High32Bits(value)); 4426 } else { 4427 DCHECK(destination.IsDoubleStackSlot()) << destination; 4428 __ LoadImmediate(IP, Low32Bits(value)); 4429 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex()); 4430 __ LoadImmediate(IP, High32Bits(value)); 4431 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize)); 4432 } 4433 } else if (constant->IsDoubleConstant()) { 4434 double value = constant->AsDoubleConstant()->GetValue(); 4435 if (destination.IsFpuRegisterPair()) { 4436 __ LoadDImmediate(FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()), value); 4437 } else { 4438 DCHECK(destination.IsDoubleStackSlot()) << destination; 4439 uint64_t int_value = bit_cast<uint64_t, double>(value); 4440 __ LoadImmediate(IP, Low32Bits(int_value)); 4441 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex()); 4442 __ LoadImmediate(IP, High32Bits(int_value)); 4443 __ StoreToOffset(kStoreWord, IP, SP, destination.GetHighStackIndex(kArmWordSize)); 4444 } 4445 } else { 4446 DCHECK(constant->IsFloatConstant()) << constant->DebugName(); 4447 float value = constant->AsFloatConstant()->GetValue(); 4448 if (destination.IsFpuRegister()) { 4449 __ LoadSImmediate(destination.AsFpuRegister<SRegister>(), value); 4450 } else { 4451 DCHECK(destination.IsStackSlot()); 4452 __ LoadImmediate(IP, bit_cast<int32_t, float>(value)); 4453 __ StoreToOffset(kStoreWord, IP, SP, destination.GetStackIndex()); 4454 } 4455 } 4456 } 4457} 4458 4459void ParallelMoveResolverARM::Exchange(Register reg, int mem) { 4460 __ Mov(IP, reg); 4461 __ LoadFromOffset(kLoadWord, reg, SP, mem); 4462 __ StoreToOffset(kStoreWord, IP, SP, mem); 4463} 4464 4465void ParallelMoveResolverARM::Exchange(int mem1, int mem2) { 4466 ScratchRegisterScope ensure_scratch(this, IP, R0, codegen_->GetNumberOfCoreRegisters()); 4467 int stack_offset = ensure_scratch.IsSpilled() ? kArmWordSize : 0; 4468 __ LoadFromOffset(kLoadWord, static_cast<Register>(ensure_scratch.GetRegister()), 4469 SP, mem1 + stack_offset); 4470 __ LoadFromOffset(kLoadWord, IP, SP, mem2 + stack_offset); 4471 __ StoreToOffset(kStoreWord, static_cast<Register>(ensure_scratch.GetRegister()), 4472 SP, mem2 + stack_offset); 4473 __ StoreToOffset(kStoreWord, IP, SP, mem1 + stack_offset); 4474} 4475 4476void ParallelMoveResolverARM::EmitSwap(size_t index) { 4477 MoveOperands* move = moves_[index]; 4478 Location source = move->GetSource(); 4479 Location destination = move->GetDestination(); 4480 4481 if (source.IsRegister() && destination.IsRegister()) { 4482 DCHECK_NE(source.AsRegister<Register>(), IP); 4483 DCHECK_NE(destination.AsRegister<Register>(), IP); 4484 __ Mov(IP, source.AsRegister<Register>()); 4485 __ Mov(source.AsRegister<Register>(), destination.AsRegister<Register>()); 4486 __ Mov(destination.AsRegister<Register>(), IP); 4487 } else if (source.IsRegister() && destination.IsStackSlot()) { 4488 Exchange(source.AsRegister<Register>(), destination.GetStackIndex()); 4489 } else if (source.IsStackSlot() && destination.IsRegister()) { 4490 Exchange(destination.AsRegister<Register>(), source.GetStackIndex()); 4491 } else if (source.IsStackSlot() && destination.IsStackSlot()) { 4492 Exchange(source.GetStackIndex(), destination.GetStackIndex()); 4493 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) { 4494 __ vmovrs(IP, source.AsFpuRegister<SRegister>()); 4495 __ vmovs(source.AsFpuRegister<SRegister>(), destination.AsFpuRegister<SRegister>()); 4496 __ vmovsr(destination.AsFpuRegister<SRegister>(), IP); 4497 } else if (source.IsRegisterPair() && destination.IsRegisterPair()) { 4498 __ vmovdrr(DTMP, source.AsRegisterPairLow<Register>(), source.AsRegisterPairHigh<Register>()); 4499 __ Mov(source.AsRegisterPairLow<Register>(), destination.AsRegisterPairLow<Register>()); 4500 __ Mov(source.AsRegisterPairHigh<Register>(), destination.AsRegisterPairHigh<Register>()); 4501 __ vmovrrd(destination.AsRegisterPairLow<Register>(), 4502 destination.AsRegisterPairHigh<Register>(), 4503 DTMP); 4504 } else if (source.IsRegisterPair() || destination.IsRegisterPair()) { 4505 Register low_reg = source.IsRegisterPair() 4506 ? source.AsRegisterPairLow<Register>() 4507 : destination.AsRegisterPairLow<Register>(); 4508 int mem = source.IsRegisterPair() 4509 ? destination.GetStackIndex() 4510 : source.GetStackIndex(); 4511 DCHECK(ExpectedPairLayout(source.IsRegisterPair() ? source : destination)); 4512 __ vmovdrr(DTMP, low_reg, static_cast<Register>(low_reg + 1)); 4513 __ LoadFromOffset(kLoadWordPair, low_reg, SP, mem); 4514 __ StoreDToOffset(DTMP, SP, mem); 4515 } else if (source.IsFpuRegisterPair() && destination.IsFpuRegisterPair()) { 4516 DRegister first = FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()); 4517 DRegister second = FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()); 4518 __ vmovd(DTMP, first); 4519 __ vmovd(first, second); 4520 __ vmovd(second, DTMP); 4521 } else if (source.IsFpuRegisterPair() || destination.IsFpuRegisterPair()) { 4522 DRegister reg = source.IsFpuRegisterPair() 4523 ? FromLowSToD(source.AsFpuRegisterPairLow<SRegister>()) 4524 : FromLowSToD(destination.AsFpuRegisterPairLow<SRegister>()); 4525 int mem = source.IsFpuRegisterPair() 4526 ? destination.GetStackIndex() 4527 : source.GetStackIndex(); 4528 __ vmovd(DTMP, reg); 4529 __ LoadDFromOffset(reg, SP, mem); 4530 __ StoreDToOffset(DTMP, SP, mem); 4531 } else if (source.IsFpuRegister() || destination.IsFpuRegister()) { 4532 SRegister reg = source.IsFpuRegister() ? source.AsFpuRegister<SRegister>() 4533 : destination.AsFpuRegister<SRegister>(); 4534 int mem = source.IsFpuRegister() 4535 ? destination.GetStackIndex() 4536 : source.GetStackIndex(); 4537 4538 __ vmovrs(IP, reg); 4539 __ LoadSFromOffset(reg, SP, mem); 4540 __ StoreToOffset(kStoreWord, IP, SP, mem); 4541 } else if (source.IsDoubleStackSlot() && destination.IsDoubleStackSlot()) { 4542 Exchange(source.GetStackIndex(), destination.GetStackIndex()); 4543 Exchange(source.GetHighStackIndex(kArmWordSize), destination.GetHighStackIndex(kArmWordSize)); 4544 } else { 4545 LOG(FATAL) << "Unimplemented" << source << " <-> " << destination; 4546 } 4547} 4548 4549void ParallelMoveResolverARM::SpillScratch(int reg) { 4550 __ Push(static_cast<Register>(reg)); 4551} 4552 4553void ParallelMoveResolverARM::RestoreScratch(int reg) { 4554 __ Pop(static_cast<Register>(reg)); 4555} 4556 4557void LocationsBuilderARM::VisitLoadClass(HLoadClass* cls) { 4558 InvokeRuntimeCallingConvention calling_convention; 4559 CodeGenerator::CreateLoadClassLocationSummary( 4560 cls, 4561 Location::RegisterLocation(calling_convention.GetRegisterAt(0)), 4562 Location::RegisterLocation(R0)); 4563} 4564 4565void InstructionCodeGeneratorARM::VisitLoadClass(HLoadClass* cls) { 4566 LocationSummary* locations = cls->GetLocations(); 4567 if (cls->NeedsAccessCheck()) { 4568 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex()); 4569 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess), 4570 cls, 4571 cls->GetDexPc(), 4572 nullptr); 4573 return; 4574 } 4575 4576 Register out = locations->Out().AsRegister<Register>(); 4577 Register current_method = locations->InAt(0).AsRegister<Register>(); 4578 if (cls->IsReferrersClass()) { 4579 DCHECK(!cls->CanCallRuntime()); 4580 DCHECK(!cls->MustGenerateClinitCheck()); 4581 __ LoadFromOffset( 4582 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value()); 4583 } else { 4584 DCHECK(cls->CanCallRuntime()); 4585 __ LoadFromOffset(kLoadWord, 4586 out, 4587 current_method, 4588 ArtMethod::DexCacheResolvedTypesOffset(kArmPointerSize).Int32Value()); 4589 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())); 4590 // TODO: We will need a read barrier here. 4591 4592 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM( 4593 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck()); 4594 codegen_->AddSlowPath(slow_path); 4595 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel()); 4596 if (cls->MustGenerateClinitCheck()) { 4597 GenerateClassInitializationCheck(slow_path, out); 4598 } else { 4599 __ Bind(slow_path->GetExitLabel()); 4600 } 4601 } 4602} 4603 4604void LocationsBuilderARM::VisitClinitCheck(HClinitCheck* check) { 4605 LocationSummary* locations = 4606 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath); 4607 locations->SetInAt(0, Location::RequiresRegister()); 4608 if (check->HasUses()) { 4609 locations->SetOut(Location::SameAsFirstInput()); 4610 } 4611} 4612 4613void InstructionCodeGeneratorARM::VisitClinitCheck(HClinitCheck* check) { 4614 // We assume the class is not null. 4615 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathARM( 4616 check->GetLoadClass(), check, check->GetDexPc(), true); 4617 codegen_->AddSlowPath(slow_path); 4618 GenerateClassInitializationCheck(slow_path, 4619 check->GetLocations()->InAt(0).AsRegister<Register>()); 4620} 4621 4622void InstructionCodeGeneratorARM::GenerateClassInitializationCheck( 4623 SlowPathCode* slow_path, Register class_reg) { 4624 __ LoadFromOffset(kLoadWord, IP, class_reg, mirror::Class::StatusOffset().Int32Value()); 4625 __ cmp(IP, ShifterOperand(mirror::Class::kStatusInitialized)); 4626 __ b(slow_path->GetEntryLabel(), LT); 4627 // Even if the initialized flag is set, we may be in a situation where caches are not synced 4628 // properly. Therefore, we do a memory fence. 4629 __ dmb(ISH); 4630 __ Bind(slow_path->GetExitLabel()); 4631} 4632 4633void LocationsBuilderARM::VisitLoadString(HLoadString* load) { 4634 LocationSummary* locations = 4635 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath); 4636 locations->SetInAt(0, Location::RequiresRegister()); 4637 locations->SetOut(Location::RequiresRegister()); 4638} 4639 4640void InstructionCodeGeneratorARM::VisitLoadString(HLoadString* load) { 4641 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathARM(load); 4642 codegen_->AddSlowPath(slow_path); 4643 4644 LocationSummary* locations = load->GetLocations(); 4645 Register out = locations->Out().AsRegister<Register>(); 4646 Register current_method = locations->InAt(0).AsRegister<Register>(); 4647 __ LoadFromOffset( 4648 kLoadWord, out, current_method, ArtMethod::DeclaringClassOffset().Int32Value()); 4649 __ LoadFromOffset(kLoadWord, out, out, mirror::Class::DexCacheStringsOffset().Int32Value()); 4650 __ LoadFromOffset(kLoadWord, out, out, CodeGenerator::GetCacheOffset(load->GetStringIndex())); 4651 // TODO: We will need a read barrier here. 4652 __ CompareAndBranchIfZero(out, slow_path->GetEntryLabel()); 4653 __ Bind(slow_path->GetExitLabel()); 4654} 4655 4656static int32_t GetExceptionTlsOffset() { 4657 return Thread::ExceptionOffset<kArmWordSize>().Int32Value(); 4658} 4659 4660void LocationsBuilderARM::VisitLoadException(HLoadException* load) { 4661 LocationSummary* locations = 4662 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall); 4663 locations->SetOut(Location::RequiresRegister()); 4664} 4665 4666void InstructionCodeGeneratorARM::VisitLoadException(HLoadException* load) { 4667 Register out = load->GetLocations()->Out().AsRegister<Register>(); 4668 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset()); 4669} 4670 4671void LocationsBuilderARM::VisitClearException(HClearException* clear) { 4672 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall); 4673} 4674 4675void InstructionCodeGeneratorARM::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) { 4676 __ LoadImmediate(IP, 0); 4677 __ StoreToOffset(kStoreWord, IP, TR, GetExceptionTlsOffset()); 4678} 4679 4680void LocationsBuilderARM::VisitThrow(HThrow* instruction) { 4681 LocationSummary* locations = 4682 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall); 4683 InvokeRuntimeCallingConvention calling_convention; 4684 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); 4685} 4686 4687void InstructionCodeGeneratorARM::VisitThrow(HThrow* instruction) { 4688 codegen_->InvokeRuntime( 4689 QUICK_ENTRY_POINT(pDeliverException), instruction, instruction->GetDexPc(), nullptr); 4690} 4691 4692void LocationsBuilderARM::VisitInstanceOf(HInstanceOf* instruction) { 4693 LocationSummary::CallKind call_kind = LocationSummary::kNoCall; 4694 switch (instruction->GetTypeCheckKind()) { 4695 case TypeCheckKind::kExactCheck: 4696 case TypeCheckKind::kAbstractClassCheck: 4697 case TypeCheckKind::kClassHierarchyCheck: 4698 case TypeCheckKind::kArrayObjectCheck: 4699 call_kind = LocationSummary::kNoCall; 4700 break; 4701 case TypeCheckKind::kUnresolvedCheck: 4702 case TypeCheckKind::kInterfaceCheck: 4703 call_kind = LocationSummary::kCall; 4704 break; 4705 case TypeCheckKind::kArrayCheck: 4706 call_kind = LocationSummary::kCallOnSlowPath; 4707 break; 4708 } 4709 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind); 4710 if (call_kind != LocationSummary::kCall) { 4711 locations->SetInAt(0, Location::RequiresRegister()); 4712 locations->SetInAt(1, Location::RequiresRegister()); 4713 // The out register is used as a temporary, so it overlaps with the inputs. 4714 // Note that TypeCheckSlowPathARM uses this register too. 4715 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap); 4716 } else { 4717 InvokeRuntimeCallingConvention calling_convention; 4718 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); 4719 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); 4720 locations->SetOut(Location::RegisterLocation(R0)); 4721 } 4722} 4723 4724void InstructionCodeGeneratorARM::VisitInstanceOf(HInstanceOf* instruction) { 4725 LocationSummary* locations = instruction->GetLocations(); 4726 Register obj = locations->InAt(0).AsRegister<Register>(); 4727 Register cls = locations->InAt(1).AsRegister<Register>(); 4728 Register out = locations->Out().AsRegister<Register>(); 4729 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); 4730 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value(); 4731 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value(); 4732 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value(); 4733 Label done, zero; 4734 SlowPathCode* slow_path = nullptr; 4735 4736 // Return 0 if `obj` is null. 4737 // avoid null check if we know obj is not null. 4738 if (instruction->MustDoNullCheck()) { 4739 __ CompareAndBranchIfZero(obj, &zero); 4740 } 4741 4742 // In case of an interface/unresolved check, we put the object class into the object register. 4743 // This is safe, as the register is caller-save, and the object must be in another 4744 // register if it survives the runtime call. 4745 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) || 4746 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck) 4747 ? obj 4748 : out; 4749 __ LoadFromOffset(kLoadWord, target, obj, class_offset); 4750 __ MaybeUnpoisonHeapReference(target); 4751 4752 switch (instruction->GetTypeCheckKind()) { 4753 case TypeCheckKind::kExactCheck: { 4754 __ cmp(out, ShifterOperand(cls)); 4755 // Classes must be equal for the instanceof to succeed. 4756 __ b(&zero, NE); 4757 __ LoadImmediate(out, 1); 4758 __ b(&done); 4759 break; 4760 } 4761 case TypeCheckKind::kAbstractClassCheck: { 4762 // If the class is abstract, we eagerly fetch the super class of the 4763 // object to avoid doing a comparison we know will fail. 4764 Label loop; 4765 __ Bind(&loop); 4766 __ LoadFromOffset(kLoadWord, out, out, super_offset); 4767 __ MaybeUnpoisonHeapReference(out); 4768 // If `out` is null, we use it for the result, and jump to `done`. 4769 __ CompareAndBranchIfZero(out, &done); 4770 __ cmp(out, ShifterOperand(cls)); 4771 __ b(&loop, NE); 4772 __ LoadImmediate(out, 1); 4773 if (zero.IsLinked()) { 4774 __ b(&done); 4775 } 4776 break; 4777 } 4778 case TypeCheckKind::kClassHierarchyCheck: { 4779 // Walk over the class hierarchy to find a match. 4780 Label loop, success; 4781 __ Bind(&loop); 4782 __ cmp(out, ShifterOperand(cls)); 4783 __ b(&success, EQ); 4784 __ LoadFromOffset(kLoadWord, out, out, super_offset); 4785 __ MaybeUnpoisonHeapReference(out); 4786 __ CompareAndBranchIfNonZero(out, &loop); 4787 // If `out` is null, we use it for the result, and jump to `done`. 4788 __ b(&done); 4789 __ Bind(&success); 4790 __ LoadImmediate(out, 1); 4791 if (zero.IsLinked()) { 4792 __ b(&done); 4793 } 4794 break; 4795 } 4796 case TypeCheckKind::kArrayObjectCheck: { 4797 // Do an exact check. 4798 Label exact_check; 4799 __ cmp(out, ShifterOperand(cls)); 4800 __ b(&exact_check, EQ); 4801 // Otherwise, we need to check that the object's class is a non primitive array. 4802 __ LoadFromOffset(kLoadWord, out, out, component_offset); 4803 __ MaybeUnpoisonHeapReference(out); 4804 // If `out` is null, we use it for the result, and jump to `done`. 4805 __ CompareAndBranchIfZero(out, &done); 4806 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset); 4807 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot"); 4808 __ CompareAndBranchIfNonZero(out, &zero); 4809 __ Bind(&exact_check); 4810 __ LoadImmediate(out, 1); 4811 __ b(&done); 4812 break; 4813 } 4814 case TypeCheckKind::kArrayCheck: { 4815 __ cmp(out, ShifterOperand(cls)); 4816 DCHECK(locations->OnlyCallsOnSlowPath()); 4817 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM( 4818 instruction, /* is_fatal */ false); 4819 codegen_->AddSlowPath(slow_path); 4820 __ b(slow_path->GetEntryLabel(), NE); 4821 __ LoadImmediate(out, 1); 4822 if (zero.IsLinked()) { 4823 __ b(&done); 4824 } 4825 break; 4826 } 4827 case TypeCheckKind::kUnresolvedCheck: 4828 case TypeCheckKind::kInterfaceCheck: 4829 default: { 4830 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial), 4831 instruction, 4832 instruction->GetDexPc(), 4833 nullptr); 4834 if (zero.IsLinked()) { 4835 __ b(&done); 4836 } 4837 break; 4838 } 4839 } 4840 4841 if (zero.IsLinked()) { 4842 __ Bind(&zero); 4843 __ LoadImmediate(out, 0); 4844 } 4845 4846 if (done.IsLinked()) { 4847 __ Bind(&done); 4848 } 4849 4850 if (slow_path != nullptr) { 4851 __ Bind(slow_path->GetExitLabel()); 4852 } 4853} 4854 4855void LocationsBuilderARM::VisitCheckCast(HCheckCast* instruction) { 4856 LocationSummary::CallKind call_kind = LocationSummary::kNoCall; 4857 bool throws_into_catch = instruction->CanThrowIntoCatchBlock(); 4858 4859 switch (instruction->GetTypeCheckKind()) { 4860 case TypeCheckKind::kExactCheck: 4861 case TypeCheckKind::kAbstractClassCheck: 4862 case TypeCheckKind::kClassHierarchyCheck: 4863 case TypeCheckKind::kArrayObjectCheck: 4864 call_kind = throws_into_catch 4865 ? LocationSummary::kCallOnSlowPath 4866 : LocationSummary::kNoCall; 4867 break; 4868 case TypeCheckKind::kUnresolvedCheck: 4869 case TypeCheckKind::kInterfaceCheck: 4870 call_kind = LocationSummary::kCall; 4871 break; 4872 case TypeCheckKind::kArrayCheck: 4873 call_kind = LocationSummary::kCallOnSlowPath; 4874 break; 4875 } 4876 4877 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary( 4878 instruction, call_kind); 4879 if (call_kind != LocationSummary::kCall) { 4880 locations->SetInAt(0, Location::RequiresRegister()); 4881 locations->SetInAt(1, Location::RequiresRegister()); 4882 // Note that TypeCheckSlowPathARM uses this register too. 4883 locations->AddTemp(Location::RequiresRegister()); 4884 } else { 4885 InvokeRuntimeCallingConvention calling_convention; 4886 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); 4887 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1))); 4888 } 4889} 4890 4891void InstructionCodeGeneratorARM::VisitCheckCast(HCheckCast* instruction) { 4892 LocationSummary* locations = instruction->GetLocations(); 4893 Register obj = locations->InAt(0).AsRegister<Register>(); 4894 Register cls = locations->InAt(1).AsRegister<Register>(); 4895 Register temp = locations->WillCall() 4896 ? Register(kNoRegister) 4897 : locations->GetTemp(0).AsRegister<Register>(); 4898 4899 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); 4900 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value(); 4901 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value(); 4902 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value(); 4903 SlowPathCode* slow_path = nullptr; 4904 4905 if (!locations->WillCall()) { 4906 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathARM( 4907 instruction, !locations->CanCall()); 4908 codegen_->AddSlowPath(slow_path); 4909 } 4910 4911 Label done; 4912 // Avoid null check if we know obj is not null. 4913 if (instruction->MustDoNullCheck()) { 4914 __ CompareAndBranchIfZero(obj, &done); 4915 } 4916 4917 if (locations->WillCall()) { 4918 __ LoadFromOffset(kLoadWord, obj, obj, class_offset); 4919 __ MaybeUnpoisonHeapReference(obj); 4920 } else { 4921 __ LoadFromOffset(kLoadWord, temp, obj, class_offset); 4922 __ MaybeUnpoisonHeapReference(temp); 4923 } 4924 4925 switch (instruction->GetTypeCheckKind()) { 4926 case TypeCheckKind::kExactCheck: 4927 case TypeCheckKind::kArrayCheck: { 4928 __ cmp(temp, ShifterOperand(cls)); 4929 // Jump to slow path for throwing the exception or doing a 4930 // more involved array check. 4931 __ b(slow_path->GetEntryLabel(), NE); 4932 break; 4933 } 4934 case TypeCheckKind::kAbstractClassCheck: { 4935 // If the class is abstract, we eagerly fetch the super class of the 4936 // object to avoid doing a comparison we know will fail. 4937 Label loop; 4938 __ Bind(&loop); 4939 __ LoadFromOffset(kLoadWord, temp, temp, super_offset); 4940 __ MaybeUnpoisonHeapReference(temp); 4941 // Jump to the slow path to throw the exception. 4942 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel()); 4943 __ cmp(temp, ShifterOperand(cls)); 4944 __ b(&loop, NE); 4945 break; 4946 } 4947 case TypeCheckKind::kClassHierarchyCheck: { 4948 // Walk over the class hierarchy to find a match. 4949 Label loop; 4950 __ Bind(&loop); 4951 __ cmp(temp, ShifterOperand(cls)); 4952 __ b(&done, EQ); 4953 __ LoadFromOffset(kLoadWord, temp, temp, super_offset); 4954 __ MaybeUnpoisonHeapReference(temp); 4955 __ CompareAndBranchIfNonZero(temp, &loop); 4956 // Jump to the slow path to throw the exception. 4957 __ b(slow_path->GetEntryLabel()); 4958 break; 4959 } 4960 case TypeCheckKind::kArrayObjectCheck: { 4961 // Do an exact check. 4962 __ cmp(temp, ShifterOperand(cls)); 4963 __ b(&done, EQ); 4964 // Otherwise, we need to check that the object's class is a non primitive array. 4965 __ LoadFromOffset(kLoadWord, temp, temp, component_offset); 4966 __ MaybeUnpoisonHeapReference(temp); 4967 __ CompareAndBranchIfZero(temp, slow_path->GetEntryLabel()); 4968 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset); 4969 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot"); 4970 __ CompareAndBranchIfNonZero(temp, slow_path->GetEntryLabel()); 4971 break; 4972 } 4973 case TypeCheckKind::kUnresolvedCheck: 4974 case TypeCheckKind::kInterfaceCheck: 4975 default: 4976 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast), 4977 instruction, 4978 instruction->GetDexPc(), 4979 nullptr); 4980 break; 4981 } 4982 __ Bind(&done); 4983 4984 if (slow_path != nullptr) { 4985 __ Bind(slow_path->GetExitLabel()); 4986 } 4987} 4988 4989void LocationsBuilderARM::VisitMonitorOperation(HMonitorOperation* instruction) { 4990 LocationSummary* locations = 4991 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall); 4992 InvokeRuntimeCallingConvention calling_convention; 4993 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0))); 4994} 4995 4996void InstructionCodeGeneratorARM::VisitMonitorOperation(HMonitorOperation* instruction) { 4997 codegen_->InvokeRuntime(instruction->IsEnter() 4998 ? QUICK_ENTRY_POINT(pLockObject) : QUICK_ENTRY_POINT(pUnlockObject), 4999 instruction, 5000 instruction->GetDexPc(), 5001 nullptr); 5002} 5003 5004void LocationsBuilderARM::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction, AND); } 5005void LocationsBuilderARM::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction, ORR); } 5006void LocationsBuilderARM::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction, EOR); } 5007 5008void LocationsBuilderARM::HandleBitwiseOperation(HBinaryOperation* instruction, Opcode opcode) { 5009 LocationSummary* locations = 5010 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); 5011 DCHECK(instruction->GetResultType() == Primitive::kPrimInt 5012 || instruction->GetResultType() == Primitive::kPrimLong); 5013 // Note: GVN reorders commutative operations to have the constant on the right hand side. 5014 locations->SetInAt(0, Location::RequiresRegister()); 5015 locations->SetInAt(1, ArmEncodableConstantOrRegister(instruction->InputAt(1), opcode)); 5016 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap); 5017} 5018 5019void InstructionCodeGeneratorARM::VisitAnd(HAnd* instruction) { 5020 HandleBitwiseOperation(instruction); 5021} 5022 5023void InstructionCodeGeneratorARM::VisitOr(HOr* instruction) { 5024 HandleBitwiseOperation(instruction); 5025} 5026 5027void InstructionCodeGeneratorARM::VisitXor(HXor* instruction) { 5028 HandleBitwiseOperation(instruction); 5029} 5030 5031void InstructionCodeGeneratorARM::GenerateAndConst(Register out, Register first, uint32_t value) { 5032 // Optimize special cases for individual halfs of `and-long` (`and` is simplified earlier). 5033 if (value == 0xffffffffu) { 5034 if (out != first) { 5035 __ mov(out, ShifterOperand(first)); 5036 } 5037 return; 5038 } 5039 if (value == 0u) { 5040 __ mov(out, ShifterOperand(0)); 5041 return; 5042 } 5043 ShifterOperand so; 5044 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, AND, value, &so)) { 5045 __ and_(out, first, so); 5046 } else { 5047 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, BIC, ~value, &so)); 5048 __ bic(out, first, ShifterOperand(~value)); 5049 } 5050} 5051 5052void InstructionCodeGeneratorARM::GenerateOrrConst(Register out, Register first, uint32_t value) { 5053 // Optimize special cases for individual halfs of `or-long` (`or` is simplified earlier). 5054 if (value == 0u) { 5055 if (out != first) { 5056 __ mov(out, ShifterOperand(first)); 5057 } 5058 return; 5059 } 5060 if (value == 0xffffffffu) { 5061 __ mvn(out, ShifterOperand(0)); 5062 return; 5063 } 5064 ShifterOperand so; 5065 if (__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORR, value, &so)) { 5066 __ orr(out, first, so); 5067 } else { 5068 DCHECK(__ ShifterOperandCanHold(kNoRegister, kNoRegister, ORN, ~value, &so)); 5069 __ orn(out, first, ShifterOperand(~value)); 5070 } 5071} 5072 5073void InstructionCodeGeneratorARM::GenerateEorConst(Register out, Register first, uint32_t value) { 5074 // Optimize special case for individual halfs of `xor-long` (`xor` is simplified earlier). 5075 if (value == 0u) { 5076 if (out != first) { 5077 __ mov(out, ShifterOperand(first)); 5078 } 5079 return; 5080 } 5081 __ eor(out, first, ShifterOperand(value)); 5082} 5083 5084void InstructionCodeGeneratorARM::HandleBitwiseOperation(HBinaryOperation* instruction) { 5085 LocationSummary* locations = instruction->GetLocations(); 5086 Location first = locations->InAt(0); 5087 Location second = locations->InAt(1); 5088 Location out = locations->Out(); 5089 5090 if (second.IsConstant()) { 5091 uint64_t value = static_cast<uint64_t>(Int64FromConstant(second.GetConstant())); 5092 uint32_t value_low = Low32Bits(value); 5093 if (instruction->GetResultType() == Primitive::kPrimInt) { 5094 Register first_reg = first.AsRegister<Register>(); 5095 Register out_reg = out.AsRegister<Register>(); 5096 if (instruction->IsAnd()) { 5097 GenerateAndConst(out_reg, first_reg, value_low); 5098 } else if (instruction->IsOr()) { 5099 GenerateOrrConst(out_reg, first_reg, value_low); 5100 } else { 5101 DCHECK(instruction->IsXor()); 5102 GenerateEorConst(out_reg, first_reg, value_low); 5103 } 5104 } else { 5105 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong); 5106 uint32_t value_high = High32Bits(value); 5107 Register first_low = first.AsRegisterPairLow<Register>(); 5108 Register first_high = first.AsRegisterPairHigh<Register>(); 5109 Register out_low = out.AsRegisterPairLow<Register>(); 5110 Register out_high = out.AsRegisterPairHigh<Register>(); 5111 if (instruction->IsAnd()) { 5112 GenerateAndConst(out_low, first_low, value_low); 5113 GenerateAndConst(out_high, first_high, value_high); 5114 } else if (instruction->IsOr()) { 5115 GenerateOrrConst(out_low, first_low, value_low); 5116 GenerateOrrConst(out_high, first_high, value_high); 5117 } else { 5118 DCHECK(instruction->IsXor()); 5119 GenerateEorConst(out_low, first_low, value_low); 5120 GenerateEorConst(out_high, first_high, value_high); 5121 } 5122 } 5123 return; 5124 } 5125 5126 if (instruction->GetResultType() == Primitive::kPrimInt) { 5127 Register first_reg = first.AsRegister<Register>(); 5128 ShifterOperand second_reg(second.AsRegister<Register>()); 5129 Register out_reg = out.AsRegister<Register>(); 5130 if (instruction->IsAnd()) { 5131 __ and_(out_reg, first_reg, second_reg); 5132 } else if (instruction->IsOr()) { 5133 __ orr(out_reg, first_reg, second_reg); 5134 } else { 5135 DCHECK(instruction->IsXor()); 5136 __ eor(out_reg, first_reg, second_reg); 5137 } 5138 } else { 5139 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong); 5140 Register first_low = first.AsRegisterPairLow<Register>(); 5141 Register first_high = first.AsRegisterPairHigh<Register>(); 5142 ShifterOperand second_low(second.AsRegisterPairLow<Register>()); 5143 ShifterOperand second_high(second.AsRegisterPairHigh<Register>()); 5144 Register out_low = out.AsRegisterPairLow<Register>(); 5145 Register out_high = out.AsRegisterPairHigh<Register>(); 5146 if (instruction->IsAnd()) { 5147 __ and_(out_low, first_low, second_low); 5148 __ and_(out_high, first_high, second_high); 5149 } else if (instruction->IsOr()) { 5150 __ orr(out_low, first_low, second_low); 5151 __ orr(out_high, first_high, second_high); 5152 } else { 5153 DCHECK(instruction->IsXor()); 5154 __ eor(out_low, first_low, second_low); 5155 __ eor(out_high, first_high, second_high); 5156 } 5157 } 5158} 5159 5160HInvokeStaticOrDirect::DispatchInfo CodeGeneratorARM::GetSupportedInvokeStaticOrDirectDispatch( 5161 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info, 5162 MethodReference target_method) { 5163 if (desired_dispatch_info.method_load_kind == 5164 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative) { 5165 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod. 5166 return HInvokeStaticOrDirect::DispatchInfo { 5167 HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod, 5168 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod, 5169 0u, 5170 0u 5171 }; 5172 } 5173 if (desired_dispatch_info.code_ptr_location == 5174 HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative) { 5175 const DexFile& outer_dex_file = GetGraph()->GetDexFile(); 5176 if (&outer_dex_file != target_method.dex_file) { 5177 // Calls across dex files are more likely to exceed the available BL range, 5178 // so use absolute patch with fixup if available and kCallArtMethod otherwise. 5179 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = 5180 (desired_dispatch_info.method_load_kind == 5181 HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup) 5182 ? HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup 5183 : HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod; 5184 return HInvokeStaticOrDirect::DispatchInfo { 5185 desired_dispatch_info.method_load_kind, 5186 code_ptr_location, 5187 desired_dispatch_info.method_load_data, 5188 0u 5189 }; 5190 } 5191 } 5192 return desired_dispatch_info; 5193} 5194 5195void CodeGeneratorARM::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) { 5196 // For better instruction scheduling we load the direct code pointer before the method pointer. 5197 switch (invoke->GetCodePtrLocation()) { 5198 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup: 5199 // LR = code address from literal pool with link-time patch. 5200 __ LoadLiteral(LR, DeduplicateMethodCodeLiteral(invoke->GetTargetMethod())); 5201 break; 5202 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect: 5203 // LR = invoke->GetDirectCodePtr(); 5204 __ LoadImmediate(LR, invoke->GetDirectCodePtr()); 5205 break; 5206 default: 5207 break; 5208 } 5209 5210 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp. 5211 switch (invoke->GetMethodLoadKind()) { 5212 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: 5213 // temp = thread->string_init_entrypoint 5214 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, invoke->GetStringInitOffset()); 5215 break; 5216 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive: 5217 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex()); 5218 break; 5219 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress: 5220 __ LoadImmediate(temp.AsRegister<Register>(), invoke->GetMethodAddress()); 5221 break; 5222 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup: 5223 __ LoadLiteral(temp.AsRegister<Register>(), 5224 DeduplicateMethodAddressLiteral(invoke->GetTargetMethod())); 5225 break; 5226 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: 5227 // TODO: Implement this type. 5228 // Currently filtered out by GetSupportedInvokeStaticOrDirectDispatch(). 5229 LOG(FATAL) << "Unsupported"; 5230 UNREACHABLE(); 5231 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: { 5232 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex()); 5233 Register method_reg; 5234 Register reg = temp.AsRegister<Register>(); 5235 if (current_method.IsRegister()) { 5236 method_reg = current_method.AsRegister<Register>(); 5237 } else { 5238 DCHECK(invoke->GetLocations()->Intrinsified()); 5239 DCHECK(!current_method.IsValid()); 5240 method_reg = reg; 5241 __ LoadFromOffset(kLoadWord, reg, SP, kCurrentMethodStackOffset); 5242 } 5243 // temp = current_method->dex_cache_resolved_methods_; 5244 __ LoadFromOffset( 5245 kLoadWord, reg, method_reg, ArtMethod::DexCacheResolvedMethodsOffset( 5246 kArmPointerSize).Int32Value()); 5247 // temp = temp[index_in_cache] 5248 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index; 5249 __ LoadFromOffset(kLoadWord, reg, reg, CodeGenerator::GetCachePointerOffset(index_in_cache)); 5250 break; 5251 } 5252 } 5253 5254 switch (invoke->GetCodePtrLocation()) { 5255 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf: 5256 __ bl(GetFrameEntryLabel()); 5257 break; 5258 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: 5259 relative_call_patches_.emplace_back(invoke->GetTargetMethod()); 5260 __ BindTrackedLabel(&relative_call_patches_.back().label); 5261 // Arbitrarily branch to the BL itself, override at link time. 5262 __ bl(&relative_call_patches_.back().label); 5263 break; 5264 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup: 5265 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect: 5266 // LR prepared above for better instruction scheduling. 5267 // LR() 5268 __ blx(LR); 5269 break; 5270 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod: 5271 // LR = callee_method->entry_point_from_quick_compiled_code_ 5272 __ LoadFromOffset( 5273 kLoadWord, LR, callee_method.AsRegister<Register>(), 5274 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kArmWordSize).Int32Value()); 5275 // LR() 5276 __ blx(LR); 5277 break; 5278 } 5279 5280 DCHECK(!IsLeafMethod()); 5281} 5282 5283void CodeGeneratorARM::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_location) { 5284 Register temp = temp_location.AsRegister<Register>(); 5285 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset( 5286 invoke->GetVTableIndex(), kArmPointerSize).Uint32Value(); 5287 LocationSummary* locations = invoke->GetLocations(); 5288 Location receiver = locations->InAt(0); 5289 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value(); 5290 // temp = object->GetClass(); 5291 DCHECK(receiver.IsRegister()); 5292 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset); 5293 MaybeRecordImplicitNullCheck(invoke); 5294 __ MaybeUnpoisonHeapReference(temp); 5295 // temp = temp->GetMethodAt(method_offset); 5296 uint32_t entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset( 5297 kArmWordSize).Int32Value(); 5298 __ LoadFromOffset(kLoadWord, temp, temp, method_offset); 5299 // LR = temp->GetEntryPoint(); 5300 __ LoadFromOffset(kLoadWord, LR, temp, entry_point); 5301 // LR(); 5302 __ blx(LR); 5303} 5304 5305void CodeGeneratorARM::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) { 5306 DCHECK(linker_patches->empty()); 5307 size_t size = method_patches_.size() + call_patches_.size() + relative_call_patches_.size(); 5308 linker_patches->reserve(size); 5309 for (const auto& entry : method_patches_) { 5310 const MethodReference& target_method = entry.first; 5311 Literal* literal = entry.second; 5312 DCHECK(literal->GetLabel()->IsBound()); 5313 uint32_t literal_offset = literal->GetLabel()->Position(); 5314 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, 5315 target_method.dex_file, 5316 target_method.dex_method_index)); 5317 } 5318 for (const auto& entry : call_patches_) { 5319 const MethodReference& target_method = entry.first; 5320 Literal* literal = entry.second; 5321 DCHECK(literal->GetLabel()->IsBound()); 5322 uint32_t literal_offset = literal->GetLabel()->Position(); 5323 linker_patches->push_back(LinkerPatch::CodePatch(literal_offset, 5324 target_method.dex_file, 5325 target_method.dex_method_index)); 5326 } 5327 for (const MethodPatchInfo<Label>& info : relative_call_patches_) { 5328 uint32_t literal_offset = info.label.Position(); 5329 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset, 5330 info.target_method.dex_file, 5331 info.target_method.dex_method_index)); 5332 } 5333} 5334 5335Literal* CodeGeneratorARM::DeduplicateMethodLiteral(MethodReference target_method, 5336 MethodToLiteralMap* map) { 5337 // Look up the literal for target_method. 5338 auto lb = map->lower_bound(target_method); 5339 if (lb != map->end() && !map->key_comp()(target_method, lb->first)) { 5340 return lb->second; 5341 } 5342 // We don't have a literal for this method yet, insert a new one. 5343 Literal* literal = __ NewLiteral<uint32_t>(0u); 5344 map->PutBefore(lb, target_method, literal); 5345 return literal; 5346} 5347 5348Literal* CodeGeneratorARM::DeduplicateMethodAddressLiteral(MethodReference target_method) { 5349 return DeduplicateMethodLiteral(target_method, &method_patches_); 5350} 5351 5352Literal* CodeGeneratorARM::DeduplicateMethodCodeLiteral(MethodReference target_method) { 5353 return DeduplicateMethodLiteral(target_method, &call_patches_); 5354} 5355 5356void LocationsBuilderARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) { 5357 // Nothing to do, this should be removed during prepare for register allocator. 5358 LOG(FATAL) << "Unreachable"; 5359} 5360 5361void InstructionCodeGeneratorARM::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) { 5362 // Nothing to do, this should be removed during prepare for register allocator. 5363 LOG(FATAL) << "Unreachable"; 5364} 5365 5366void LocationsBuilderARM::VisitFakeString(HFakeString* instruction) { 5367 DCHECK(codegen_->IsBaseline()); 5368 LocationSummary* locations = 5369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall); 5370 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant())); 5371} 5372 5373void InstructionCodeGeneratorARM::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) { 5374 DCHECK(codegen_->IsBaseline()); 5375 // Will be generated at use site. 5376} 5377 5378// Simple implementation of packed switch - generate cascaded compare/jumps. 5379void LocationsBuilderARM::VisitPackedSwitch(HPackedSwitch* switch_instr) { 5380 LocationSummary* locations = 5381 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall); 5382 locations->SetInAt(0, Location::RequiresRegister()); 5383 if (switch_instr->GetNumEntries() >= kPackedSwitchJumpTableThreshold && 5384 codegen_->GetAssembler()->IsThumb()) { 5385 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the table base. 5386 if (switch_instr->GetStartValue() != 0) { 5387 locations->AddTemp(Location::RequiresRegister()); // We need a temp for the bias. 5388 } 5389 } 5390} 5391 5392void InstructionCodeGeneratorARM::VisitPackedSwitch(HPackedSwitch* switch_instr) { 5393 int32_t lower_bound = switch_instr->GetStartValue(); 5394 uint32_t num_entries = switch_instr->GetNumEntries(); 5395 LocationSummary* locations = switch_instr->GetLocations(); 5396 Register value_reg = locations->InAt(0).AsRegister<Register>(); 5397 HBasicBlock* default_block = switch_instr->GetDefaultBlock(); 5398 5399 if (num_entries < kPackedSwitchJumpTableThreshold || !codegen_->GetAssembler()->IsThumb()) { 5400 // Create a series of compare/jumps. 5401 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors(); 5402 for (uint32_t i = 0; i < num_entries; i++) { 5403 GenerateCompareWithImmediate(value_reg, lower_bound + i); 5404 __ b(codegen_->GetLabelOf(successors[i]), EQ); 5405 } 5406 5407 // And the default for any other value. 5408 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) { 5409 __ b(codegen_->GetLabelOf(default_block)); 5410 } 5411 } else { 5412 // Create a table lookup. 5413 Register temp_reg = locations->GetTemp(0).AsRegister<Register>(); 5414 5415 // Materialize a pointer to the switch table 5416 std::vector<Label*> labels(num_entries); 5417 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors(); 5418 for (uint32_t i = 0; i < num_entries; i++) { 5419 labels[i] = codegen_->GetLabelOf(successors[i]); 5420 } 5421 JumpTable* table = __ CreateJumpTable(std::move(labels), temp_reg); 5422 5423 // Remove the bias. 5424 Register key_reg; 5425 if (lower_bound != 0) { 5426 key_reg = locations->GetTemp(1).AsRegister<Register>(); 5427 __ AddConstant(key_reg, value_reg, -lower_bound); 5428 } else { 5429 key_reg = value_reg; 5430 } 5431 5432 // Check whether the value is in the table, jump to default block if not. 5433 __ CmpConstant(key_reg, num_entries - 1); 5434 __ b(codegen_->GetLabelOf(default_block), Condition::HI); 5435 5436 // Load the displacement from the table. 5437 __ ldr(temp_reg, Address(temp_reg, key_reg, Shift::LSL, 2)); 5438 5439 // Dispatch is a direct add to the PC (for Thumb2). 5440 __ EmitJumpTableDispatch(table, temp_reg); 5441 } 5442} 5443 5444void CodeGeneratorARM::MoveFromReturnRegister(Location trg, Primitive::Type type) { 5445 if (!trg.IsValid()) { 5446 DCHECK(type == Primitive::kPrimVoid); 5447 return; 5448 } 5449 5450 DCHECK_NE(type, Primitive::kPrimVoid); 5451 5452 Location return_loc = InvokeDexCallingConventionVisitorARM().GetReturnLocation(type); 5453 if (return_loc.Equals(trg)) { 5454 return; 5455 } 5456 5457 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged 5458 // with the last branch. 5459 if (type == Primitive::kPrimLong) { 5460 HParallelMove parallel_move(GetGraph()->GetArena()); 5461 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimInt, nullptr); 5462 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimInt, nullptr); 5463 GetMoveResolver()->EmitNativeCode(¶llel_move); 5464 } else if (type == Primitive::kPrimDouble) { 5465 HParallelMove parallel_move(GetGraph()->GetArena()); 5466 parallel_move.AddMove(return_loc.ToLow(), trg.ToLow(), Primitive::kPrimFloat, nullptr); 5467 parallel_move.AddMove(return_loc.ToHigh(), trg.ToHigh(), Primitive::kPrimFloat, nullptr); 5468 GetMoveResolver()->EmitNativeCode(¶llel_move); 5469 } else { 5470 // Let the parallel move resolver take care of all of this. 5471 HParallelMove parallel_move(GetGraph()->GetArena()); 5472 parallel_move.AddMove(return_loc, trg, type, nullptr); 5473 GetMoveResolver()->EmitNativeCode(¶llel_move); 5474 } 5475} 5476 5477#undef __ 5478#undef QUICK_ENTRY_POINT 5479 5480} // namespace arm 5481} // namespace art 5482