1d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray/*
2d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
3d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray *
4d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
5d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * you may not use this file except in compliance with the License.
6d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * You may obtain a copy of the License at
7d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray *
8d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
9d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray *
10d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software
11d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
12d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * See the License for the specific language governing permissions and
14d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray * limitations under the License.
15d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray */
16d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray
17d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#include "code_generator.h"
18d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray
1950fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#ifdef ART_ENABLE_CODEGEN_arm
20d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#include "code_generator_arm.h"
2150fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#endif
2250fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light
2350fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#ifdef ART_ENABLE_CODEGEN_arm64
245319defdf502fc4569316473846b83180ec08035Alexandre Rames#include "code_generator_arm64.h"
2550fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#endif
2650fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light
2750fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#ifdef ART_ENABLE_CODEGEN_x86
28d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#include "code_generator_x86.h"
2950fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#endif
3050fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light
3150fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#ifdef ART_ENABLE_CODEGEN_x86_64
329cf35523764d829ae0470dae2d5dd99be469c841Nicolas Geoffray#include "code_generator_x86_64.h"
3350fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#endif
3450fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light
35f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic#ifdef ART_ENABLE_CODEGEN_mips
36f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic#include "code_generator_mips.h"
37f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic#endif
38f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic
3950fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#ifdef ART_ENABLE_CODEGEN_mips64
404dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze#include "code_generator_mips64.h"
4150fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#endif
4250fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light
4386ea7eeabe30c98bbe1651a51d03cb89776724e7David Brazdil#include "bytecode_utils.h"
44e3ea83811d47152c00abea24a9b420651a33b496Yevgeny Rouban#include "compiled_method.h"
4592cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "dex/verified_method.h"
46d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray#include "driver/compiler_driver.h"
47eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames#include "graph_visualizer.h"
485bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray#include "intrinsics.h"
498ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray#include "leb128.h"
5019a19cffd197a28ae4c9c3e59eff6352fd392241Nicolas Geoffray#include "mirror/array-inl.h"
5119a19cffd197a28ae4c9c3e59eff6352fd392241Nicolas Geoffray#include "mirror/object_array-inl.h"
5219a19cffd197a28ae4c9c3e59eff6352fd392241Nicolas Geoffray#include "mirror/object_reference.h"
5350fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#include "parallel_move_resolver.h"
543c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "ssa_liveness_analysis.h"
55d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray#include "utils/assembler.h"
56d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray
57d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffraynamespace art {
58d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray
5988c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames// Return whether a location is consistent with a type.
6088c13cddc3a4184908662b0f3de796565d348c76Alexandre Ramesstatic bool CheckType(Primitive::Type type, Location location) {
6188c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  if (location.IsFpuRegister()
6288c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames      || (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresFpuRegister))) {
6388c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    return (type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble);
6488c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  } else if (location.IsRegister() ||
6588c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames             (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresRegister))) {
6688c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    return Primitive::IsIntegralType(type) || (type == Primitive::kPrimNot);
6788c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  } else if (location.IsRegisterPair()) {
6888c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    return type == Primitive::kPrimLong;
6988c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  } else if (location.IsFpuRegisterPair()) {
7088c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    return type == Primitive::kPrimDouble;
7188c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  } else if (location.IsStackSlot()) {
7288c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    return (Primitive::IsIntegralType(type) && type != Primitive::kPrimLong)
7388c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames           || (type == Primitive::kPrimFloat)
7488c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames           || (type == Primitive::kPrimNot);
7588c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  } else if (location.IsDoubleStackSlot()) {
7688c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    return (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
7788c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  } else if (location.IsConstant()) {
7888c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    if (location.GetConstant()->IsIntConstant()) {
7988c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames      return Primitive::IsIntegralType(type) && (type != Primitive::kPrimLong);
8088c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    } else if (location.GetConstant()->IsNullConstant()) {
8188c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames      return type == Primitive::kPrimNot;
8288c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    } else if (location.GetConstant()->IsLongConstant()) {
8388c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames      return type == Primitive::kPrimLong;
8488c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    } else if (location.GetConstant()->IsFloatConstant()) {
8588c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames      return type == Primitive::kPrimFloat;
8688c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    } else {
8788c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames      return location.GetConstant()->IsDoubleConstant()
8888c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames          && (type == Primitive::kPrimDouble);
8988c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    }
9088c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  } else {
9188c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    return location.IsInvalid() || (location.GetPolicy() == Location::kAny);
9288c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  }
9388c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames}
9488c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames
9588c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames// Check that a location summary is consistent with an instruction.
9688c13cddc3a4184908662b0f3de796565d348c76Alexandre Ramesstatic bool CheckTypeConsistency(HInstruction* instruction) {
9788c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  LocationSummary* locations = instruction->GetLocations();
9888c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  if (locations == nullptr) {
9988c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    return true;
10088c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  }
10188c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames
10288c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  if (locations->Out().IsUnallocated()
10388c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames      && (locations->Out().GetPolicy() == Location::kSameAsFirstInput)) {
10488c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    DCHECK(CheckType(instruction->GetType(), locations->InAt(0)))
10588c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames        << instruction->GetType()
10688c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames        << " " << locations->InAt(0);
10788c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  } else {
10888c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    DCHECK(CheckType(instruction->GetType(), locations->Out()))
10988c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames        << instruction->GetType()
11088c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames        << " " << locations->Out();
11188c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  }
11288c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames
11388c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
11488c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    DCHECK(CheckType(instruction->InputAt(i)->GetType(), locations->InAt(i)))
11588c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames      << instruction->InputAt(i)->GetType()
11688c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames      << " " << locations->InAt(i);
11788c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  }
11888c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames
11988c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  HEnvironment* environment = instruction->GetEnvironment();
12088c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  for (size_t i = 0; i < instruction->EnvironmentSize(); ++i) {
12188c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    if (environment->GetInstructionAt(i) != nullptr) {
12288c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames      Primitive::Type type = environment->GetInstructionAt(i)->GetType();
1230a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray      DCHECK(CheckType(type, environment->GetLocationAt(i)))
1240a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray        << type << " " << environment->GetLocationAt(i);
12588c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    } else {
1260a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray      DCHECK(environment->GetLocationAt(i).IsInvalid())
1270a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray        << environment->GetLocationAt(i);
12888c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames    }
12988c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  }
13088c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  return true;
13188c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames}
13288c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames
13319a19cffd197a28ae4c9c3e59eff6352fd392241Nicolas Geoffraysize_t CodeGenerator::GetCacheOffset(uint32_t index) {
13405792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko  return sizeof(GcRoot<mirror::Object>) * index;
13519a19cffd197a28ae4c9c3e59eff6352fd392241Nicolas Geoffray}
13619a19cffd197a28ae4c9c3e59eff6352fd392241Nicolas Geoffray
137e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartiersize_t CodeGenerator::GetCachePointerOffset(uint32_t index) {
138e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  auto pointer_size = InstructionSetPointerSize(GetInstructionSet());
13905792b98980741111b4d0a24d68cff2a8e070a3aVladimir Marko  return pointer_size * index;
140e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier}
141e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier
142dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffraybool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
143fa6b93c4b69e6d7ddfa2a4ed0aff01b0608c5a3aVladimir Marko  DCHECK_EQ((*block_order_)[current_block_index_], current);
144dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray  return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
145dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray}
146dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray
147dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas GeoffrayHBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
148fa6b93c4b69e6d7ddfa2a4ed0aff01b0608c5a3aVladimir Marko  for (size_t i = current_block_index_ + 1; i < block_order_->size(); ++i) {
149fa6b93c4b69e6d7ddfa2a4ed0aff01b0608c5a3aVladimir Marko    HBasicBlock* block = (*block_order_)[i];
150fc6a86ab2b70781e72b807c1798b83829ca7f931David Brazdil    if (!block->IsSingleJump()) {
151dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray      return block;
152dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray    }
153dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray  }
154dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray  return nullptr;
155dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray}
156dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray
157dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas GeoffrayHBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
158fc6a86ab2b70781e72b807c1798b83829ca7f931David Brazdil  while (block->IsSingleJump()) {
159ec7802a102d49ab5c17495118d4fe0bcc7287bebVladimir Marko    block = block->GetSuccessors()[0];
160dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray  }
161dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray  return block;
162dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray}
163dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray
164eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Ramesclass DisassemblyScope {
165eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames public:
166eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen)
167eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames      : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) {
168eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    if (codegen_.GetDisassemblyInformation() != nullptr) {
169eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames      start_offset_ = codegen_.GetAssembler().CodeSize();
170eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    }
171eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
172eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
173eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  ~DisassemblyScope() {
174eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    // We avoid building this data when we know it will not be used.
175eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    if (codegen_.GetDisassemblyInformation() != nullptr) {
176eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames      codegen_.GetDisassemblyInformation()->AddInstructionInterval(
177eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames          instruction_, start_offset_, codegen_.GetAssembler().CodeSize());
178eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    }
179eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
180eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
181eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames private:
182eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  const CodeGenerator& codegen_;
183eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  HInstruction* instruction_;
184eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  size_t start_offset_;
185eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames};
186eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
187eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
188eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Ramesvoid CodeGenerator::GenerateSlowPaths() {
189eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  size_t code_start = 0;
190c393d63aa2b8f6984672fdd4de631bbeff14b6a2Alexandre Rames  for (const std::unique_ptr<SlowPathCode>& slow_path_unique_ptr : slow_paths_) {
191c393d63aa2b8f6984672fdd4de631bbeff14b6a2Alexandre Rames    SlowPathCode* slow_path = slow_path_unique_ptr.get();
1920f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko    current_slow_path_ = slow_path;
193eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    if (disasm_info_ != nullptr) {
194eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames      code_start = GetAssembler()->CodeSize();
195eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    }
1969cd6d378bd573cdc14d049d32bdd22a97fa4d84aDavid Srbecky    // Record the dex pc at start of slow path (required for java line number mapping).
197d28f4a00933a4a3b8d5e9db73b8532924d0f989dDavid Srbecky    MaybeRecordNativeDebugInfo(slow_path->GetInstruction(), slow_path->GetDexPc(), slow_path);
198225b6464a58ebe11c156144653f11a1c6607f4ebVladimir Marko    slow_path->EmitNativeCode(this);
199eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    if (disasm_info_ != nullptr) {
200225b6464a58ebe11c156144653f11a1c6607f4ebVladimir Marko      disasm_info_->AddSlowPathInterval(slow_path, code_start, GetAssembler()->CodeSize());
201eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    }
202eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
2030f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko  current_slow_path_ = nullptr;
204eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames}
205eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
20658282f4510961317b8d5a364a6f740a78926716fDavid Brazdilvoid CodeGenerator::Compile(CodeAllocator* allocator) {
20758282f4510961317b8d5a364a6f740a78926716fDavid Brazdil  // The register allocator already called `InitializeCodeGeneration`,
20858282f4510961317b8d5a364a6f740a78926716fDavid Brazdil  // where the frame size has been computed.
20958282f4510961317b8d5a364a6f740a78926716fDavid Brazdil  DCHECK(block_order_ != nullptr);
21058282f4510961317b8d5a364a6f740a78926716fDavid Brazdil  Initialize();
21158282f4510961317b8d5a364a6f740a78926716fDavid Brazdil
2128a16d97fb8f031822b206e65f9109a071da40563Nicolas Geoffray  HGraphVisitor* instruction_visitor = GetInstructionVisitor();
2134c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray  DCHECK_EQ(current_block_index_, 0u);
214eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
215eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  size_t frame_start = GetAssembler()->CodeSize();
2164c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray  GenerateFrameEntry();
217c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
218eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  if (disasm_info_ != nullptr) {
219eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize());
220eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
221eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
222fa6b93c4b69e6d7ddfa2a4ed0aff01b0608c5a3aVladimir Marko  for (size_t e = block_order_->size(); current_block_index_ < e; ++current_block_index_) {
223fa6b93c4b69e6d7ddfa2a4ed0aff01b0608c5a3aVladimir Marko    HBasicBlock* block = (*block_order_)[current_block_index_];
224dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray    // Don't generate code for an empty block. Its predecessors will branch to its successor
225dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray    // directly. Also, the label of that block will not be emitted, so this helps catch
226dc23d8318db08cb42e20f1d16dbc416798951a8bNicolas Geoffray    // errors where we reference that label.
227fc6a86ab2b70781e72b807c1798b83829ca7f931David Brazdil    if (block->IsSingleJump()) continue;
22892a73aef279be78e3c2b04db1713076183933436Nicolas Geoffray    Bind(block);
229c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky    // This ensures that we have correct native line mapping for all native instructions.
230c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky    // It is necessary to make stepping over a statement work. Otherwise, any initial
231c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky    // instructions (e.g. moves) would be assumed to be the start of next statement.
232c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky    MaybeRecordNativeDebugInfo(nullptr /* instruction */, block->GetDexPc());
23386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
23486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      HInstruction* current = it.Current();
235d28f4a00933a4a3b8d5e9db73b8532924d0f989dDavid Srbecky      if (current->HasEnvironment()) {
236d28f4a00933a4a3b8d5e9db73b8532924d0f989dDavid Srbecky        // Create stackmap for HNativeDebugInfo or any instruction which calls native code.
237d28f4a00933a4a3b8d5e9db73b8532924d0f989dDavid Srbecky        // Note that we need correct mapping for the native PC of the call instruction,
238d28f4a00933a4a3b8d5e9db73b8532924d0f989dDavid Srbecky        // so the runtime's stackmap is not sufficient since it is at PC after the call.
239d28f4a00933a4a3b8d5e9db73b8532924d0f989dDavid Srbecky        MaybeRecordNativeDebugInfo(current, block->GetDexPc());
240d28f4a00933a4a3b8d5e9db73b8532924d0f989dDavid Srbecky      }
241eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames      DisassemblyScope disassembly_scope(current, *this);
24288c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames      DCHECK(CheckTypeConsistency(current));
24386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      current->Accept(instruction_visitor);
24486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    }
245d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray  }
2464c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray
247eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  GenerateSlowPaths();
2484c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray
24977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  // Emit catch stack maps at the end of the stack map stream as expected by the
25077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  // runtime exception handler.
25158282f4510961317b8d5a364a6f740a78926716fDavid Brazdil  if (graph_->HasTryCatch()) {
25277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    RecordCatchBlockInfo();
25377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  }
25477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
2554c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray  // Finalize instructions in assember;
25632f5b4d2c8c9b52e9522941c159577b21752d0faSerban Constantinescu  Finalize(allocator);
257d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray}
258d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray
25932f5b4d2c8c9b52e9522941c159577b21752d0faSerban Constantinescuvoid CodeGenerator::Finalize(CodeAllocator* allocator) {
26086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  size_t code_size = GetAssembler()->CodeSize();
26186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  uint8_t* buffer = allocator->Allocate(code_size);
26232f5b4d2c8c9b52e9522941c159577b21752d0faSerban Constantinescu
26386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  MemoryRegion code(buffer, code_size);
26486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  GetAssembler()->FinalizeInstructions(code);
265bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray}
266bab4ed7057799a4fadc6283108ab56f389d117d4Nicolas Geoffray
267581550137ee3a068a14224870e71aeee924a0646Vladimir Markovoid CodeGenerator::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches ATTRIBUTE_UNUSED) {
268581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // No linker patches by default.
269581550137ee3a068a14224870e71aeee924a0646Vladimir Marko}
270581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
2714c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffrayvoid CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
2724c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                             size_t maximum_number_of_live_core_registers,
2730d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain                                             size_t maximum_number_of_live_fpu_registers,
2744c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray                                             size_t number_of_out_slots,
275fa6b93c4b69e6d7ddfa2a4ed0aff01b0608c5a3aVladimir Marko                                             const ArenaVector<HBasicBlock*>& block_order) {
2764c204bafbc8d596894f8cb8ec696f5be1c6f12d8Nicolas Geoffray  block_order_ = &block_order;
277fa6b93c4b69e6d7ddfa2a4ed0aff01b0608c5a3aVladimir Marko  DCHECK(!block_order.empty());
278fa6b93c4b69e6d7ddfa2a4ed0aff01b0608c5a3aVladimir Marko  DCHECK(block_order[0] == GetGraph()->GetEntryBlock());
2794dee636d21d9ce54386cdfbb824e5eb2a9c1af0dNicolas Geoffray  ComputeSpillMask();
2803bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
2813bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
282c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray  if (number_of_spill_slots == 0
283c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      && !HasAllocatedCalleeSaveRegisters()
284c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      && IsLeafMethod()
285c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      && !RequiresCurrentMethod()) {
286c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray    DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
2870d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    DCHECK_EQ(maximum_number_of_live_fpu_registers, 0u);
288c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray    SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
289c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray  } else {
290c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray    SetFrameSize(RoundUp(
291c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray        number_of_spill_slots * kVRegSize
292c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray        + number_of_out_slots * kVRegSize
293c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray        + maximum_number_of_live_core_registers * GetWordSize()
2940d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain        + maximum_number_of_live_fpu_registers * GetFloatingPointSpillSlotSize()
295c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray        + FrameEntrySpillSize(),
296c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray        kStackAlignment));
297c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray  }
298ab032bc1ff57831106fdac6a91a136293609401fNicolas Geoffray}
299ab032bc1ff57831106fdac6a91a136293609401fNicolas Geoffray
300fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffrayvoid CodeGenerator::CreateCommonInvokeLocationSummary(
3014e40c2691d42608f871b48b102155c80cf8b27e3Nicolas Geoffray    HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) {
302fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray  ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
303fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray  LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall);
304fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray
305fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray  for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
306fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray    HInstruction* input = invoke->InputAt(i);
307fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray    locations->SetInAt(i, visitor->GetNextLocation(input->GetType()));
308fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray  }
309fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray
310fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray  locations->SetOut(visitor->GetReturnLocation(invoke->GetType()));
31194015b939060f5041d408d48717f22443e55b6adNicolas Geoffray
31294015b939060f5041d408d48717f22443e55b6adNicolas Geoffray  if (invoke->IsInvokeStaticOrDirect()) {
31394015b939060f5041d408d48717f22443e55b6adNicolas Geoffray    HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect();
314dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko    switch (call->GetMethodLoadKind()) {
315dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko      case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
316c53c0797a78a89d637e4230503cc1feb27e855a8Vladimir Marko        locations->SetInAt(call->GetSpecialInputIndex(), visitor->GetMethodLocation());
317dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko        break;
318dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko      case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod:
319dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko        locations->AddTemp(visitor->GetMethodLocation());
320c53c0797a78a89d637e4230503cc1feb27e855a8Vladimir Marko        locations->SetInAt(call->GetSpecialInputIndex(), Location::RequiresRegister());
321dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko        break;
322dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko      default:
323dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko        locations->AddTemp(visitor->GetMethodLocation());
324dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko        break;
32594015b939060f5041d408d48717f22443e55b6adNicolas Geoffray    }
32694015b939060f5041d408d48717f22443e55b6adNicolas Geoffray  } else {
32794015b939060f5041d408d48717f22443e55b6adNicolas Geoffray    locations->AddTemp(visitor->GetMethodLocation());
32894015b939060f5041d408d48717f22443e55b6adNicolas Geoffray  }
329fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray}
330fd88f16100cceafbfde1b4f095f17e89444d6fa8Nicolas Geoffray
331175dc732c80e6f2afd83209348124df349290ba8Calin Juravlevoid CodeGenerator::GenerateInvokeUnresolvedRuntimeCall(HInvokeUnresolved* invoke) {
332175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  MoveConstant(invoke->GetLocations()->GetTemp(0), invoke->GetDexMethodIndex());
333175dc732c80e6f2afd83209348124df349290ba8Calin Juravle
334175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  // Initialize to anything to silent compiler warnings.
335175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  QuickEntrypointEnum entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
336175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  switch (invoke->GetOriginalInvokeType()) {
337175dc732c80e6f2afd83209348124df349290ba8Calin Juravle    case kStatic:
338175dc732c80e6f2afd83209348124df349290ba8Calin Juravle      entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
339175dc732c80e6f2afd83209348124df349290ba8Calin Juravle      break;
340175dc732c80e6f2afd83209348124df349290ba8Calin Juravle    case kDirect:
341175dc732c80e6f2afd83209348124df349290ba8Calin Juravle      entrypoint = kQuickInvokeDirectTrampolineWithAccessCheck;
342175dc732c80e6f2afd83209348124df349290ba8Calin Juravle      break;
343175dc732c80e6f2afd83209348124df349290ba8Calin Juravle    case kVirtual:
344175dc732c80e6f2afd83209348124df349290ba8Calin Juravle      entrypoint = kQuickInvokeVirtualTrampolineWithAccessCheck;
345175dc732c80e6f2afd83209348124df349290ba8Calin Juravle      break;
346175dc732c80e6f2afd83209348124df349290ba8Calin Juravle    case kSuper:
347175dc732c80e6f2afd83209348124df349290ba8Calin Juravle      entrypoint = kQuickInvokeSuperTrampolineWithAccessCheck;
348175dc732c80e6f2afd83209348124df349290ba8Calin Juravle      break;
349175dc732c80e6f2afd83209348124df349290ba8Calin Juravle    case kInterface:
350175dc732c80e6f2afd83209348124df349290ba8Calin Juravle      entrypoint = kQuickInvokeInterfaceTrampolineWithAccessCheck;
351175dc732c80e6f2afd83209348124df349290ba8Calin Juravle      break;
352175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  }
353175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  InvokeRuntime(entrypoint, invoke, invoke->GetDexPc(), nullptr);
354175dc732c80e6f2afd83209348124df349290ba8Calin Juravle}
355175dc732c80e6f2afd83209348124df349290ba8Calin Juravle
356e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravlevoid CodeGenerator::CreateUnresolvedFieldLocationSummary(
357e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    HInstruction* field_access,
358e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    Primitive::Type field_type,
359e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    const FieldAccessCallingConvention& calling_convention) {
360e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
361e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      || field_access->IsUnresolvedInstanceFieldSet();
362e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  bool is_get = field_access->IsUnresolvedInstanceFieldGet()
363e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      || field_access->IsUnresolvedStaticFieldGet();
364e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
365e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  ArenaAllocator* allocator = field_access->GetBlock()->GetGraph()->GetArena();
366e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  LocationSummary* locations =
367e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      new (allocator) LocationSummary(field_access, LocationSummary::kCall);
368e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
369e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  locations->AddTemp(calling_convention.GetFieldIndexLocation());
370e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
371e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  if (is_instance) {
372e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    // Add the `this` object for instance field accesses.
373e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    locations->SetInAt(0, calling_convention.GetObjectLocation());
374e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  }
375e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
376e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  // Note that pSetXXStatic/pGetXXStatic always takes/returns an int or int64
377e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  // regardless of the the type. Because of that we forced to special case
378e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  // the access to floating point values.
379e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  if (is_get) {
380e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    if (Primitive::IsFloatingPointType(field_type)) {
381e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      // The return value will be stored in regular registers while register
382e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      // allocator expects it in a floating point register.
383e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      // Note We don't need to request additional temps because the return
384e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      // register(s) are already blocked due the call and they may overlap with
385e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      // the input or field index.
386e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      // The transfer between the two will be done at codegen level.
387e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      locations->SetOut(calling_convention.GetFpuLocation(field_type));
388e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    } else {
389e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      locations->SetOut(calling_convention.GetReturnLocation(field_type));
390e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    }
391e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  } else {
392e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle     size_t set_index = is_instance ? 1 : 0;
393e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle     if (Primitive::IsFloatingPointType(field_type)) {
394e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      // The set value comes from a float location while the calling convention
395e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      // expects it in a regular register location. Allocate a temp for it and
396e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      // make the transfer at codegen.
397e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      AddLocationAsTemp(calling_convention.GetSetValueLocation(field_type, is_instance), locations);
398e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      locations->SetInAt(set_index, calling_convention.GetFpuLocation(field_type));
399e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    } else {
400e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      locations->SetInAt(set_index,
401e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          calling_convention.GetSetValueLocation(field_type, is_instance));
402e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    }
403e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  }
404e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle}
405e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
406e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravlevoid CodeGenerator::GenerateUnresolvedFieldAccess(
407e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    HInstruction* field_access,
408e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    Primitive::Type field_type,
409e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    uint32_t field_index,
410e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    uint32_t dex_pc,
411e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    const FieldAccessCallingConvention& calling_convention) {
412e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  LocationSummary* locations = field_access->GetLocations();
413e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
414e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  MoveConstant(locations->GetTemp(0), field_index);
415e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
416e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
417e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      || field_access->IsUnresolvedInstanceFieldSet();
418e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  bool is_get = field_access->IsUnresolvedInstanceFieldGet()
419e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      || field_access->IsUnresolvedStaticFieldGet();
420e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
421e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  if (!is_get && Primitive::IsFloatingPointType(field_type)) {
422e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    // Copy the float value to be set into the calling convention register.
423e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    // Note that using directly the temp location is problematic as we don't
424e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    // support temp register pairs. To avoid boilerplate conversion code, use
425e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    // the location from the calling convention.
426e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    MoveLocation(calling_convention.GetSetValueLocation(field_type, is_instance),
427e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle                 locations->InAt(is_instance ? 1 : 0),
428e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle                 (Primitive::Is64BitType(field_type) ? Primitive::kPrimLong : Primitive::kPrimInt));
429e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  }
430e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
431e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  QuickEntrypointEnum entrypoint = kQuickSet8Static;  // Initialize to anything to avoid warnings.
432e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  switch (field_type) {
433e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    case Primitive::kPrimBoolean:
434e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      entrypoint = is_instance
435e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          ? (is_get ? kQuickGetBooleanInstance : kQuickSet8Instance)
436e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          : (is_get ? kQuickGetBooleanStatic : kQuickSet8Static);
437e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      break;
438e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    case Primitive::kPrimByte:
439e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      entrypoint = is_instance
440e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          ? (is_get ? kQuickGetByteInstance : kQuickSet8Instance)
441e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          : (is_get ? kQuickGetByteStatic : kQuickSet8Static);
442e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      break;
443e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    case Primitive::kPrimShort:
444e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      entrypoint = is_instance
445e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          ? (is_get ? kQuickGetShortInstance : kQuickSet16Instance)
446e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          : (is_get ? kQuickGetShortStatic : kQuickSet16Static);
447e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      break;
448e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    case Primitive::kPrimChar:
449e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      entrypoint = is_instance
450e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          ? (is_get ? kQuickGetCharInstance : kQuickSet16Instance)
451e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          : (is_get ? kQuickGetCharStatic : kQuickSet16Static);
452e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      break;
453e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    case Primitive::kPrimInt:
454e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    case Primitive::kPrimFloat:
455e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      entrypoint = is_instance
456e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          ? (is_get ? kQuickGet32Instance : kQuickSet32Instance)
457e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          : (is_get ? kQuickGet32Static : kQuickSet32Static);
458e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      break;
459e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    case Primitive::kPrimNot:
460e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      entrypoint = is_instance
461e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          ? (is_get ? kQuickGetObjInstance : kQuickSetObjInstance)
462e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          : (is_get ? kQuickGetObjStatic : kQuickSetObjStatic);
463e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      break;
464e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    case Primitive::kPrimLong:
465e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    case Primitive::kPrimDouble:
466e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      entrypoint = is_instance
467e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          ? (is_get ? kQuickGet64Instance : kQuickSet64Instance)
468e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle          : (is_get ? kQuickGet64Static : kQuickSet64Static);
469e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      break;
470e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    default:
471e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle      LOG(FATAL) << "Invalid type " << field_type;
472e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  }
473e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  InvokeRuntime(entrypoint, field_access, dex_pc, nullptr);
474e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
475e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  if (is_get && Primitive::IsFloatingPointType(field_type)) {
476e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle    MoveLocation(locations->Out(), calling_convention.GetReturnLocation(field_type), field_type);
477e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle  }
478e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle}
479e460d1df1f789c7c8bb97024a8efbd713ac175e9Calin Juravle
4800d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain// TODO: Remove argument `code_generator_supports_read_barrier` when
4810d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain// all code generators have read barrier support.
48298893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravlevoid CodeGenerator::CreateLoadClassLocationSummary(HLoadClass* cls,
48398893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle                                                   Location runtime_type_index_location,
4840d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain                                                   Location runtime_return_location,
4850d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain                                                   bool code_generator_supports_read_barrier) {
48698893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle  ArenaAllocator* allocator = cls->GetBlock()->GetGraph()->GetArena();
48798893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle  LocationSummary::CallKind call_kind = cls->NeedsAccessCheck()
48898893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle      ? LocationSummary::kCall
4890d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain      : (((code_generator_supports_read_barrier && kEmitCompilerReadBarrier) ||
4900d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain          cls->CanCallRuntime())
4910d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain            ? LocationSummary::kCallOnSlowPath
4920d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain            : LocationSummary::kNoCall);
49398893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle  LocationSummary* locations = new (allocator) LocationSummary(cls, call_kind);
49498893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle  if (cls->NeedsAccessCheck()) {
495580b609cd6cfef46108156457df42254d11e72a7Calin Juravle    locations->SetInAt(0, Location::NoLocation());
49698893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle    locations->AddTemp(runtime_type_index_location);
49798893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle    locations->SetOut(runtime_return_location);
49898893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle  } else {
499580b609cd6cfef46108156457df42254d11e72a7Calin Juravle    locations->SetInAt(0, Location::RequiresRegister());
50098893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle    locations->SetOut(Location::RequiresRegister());
50198893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle  }
50298893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle}
50398893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle
50498893e146b0ff0e1fd1d7c29252f1d1e75a163f2Calin Juravle
5055f8741860d465410bfed495dbb5f794590d338daMark Mendellvoid CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
5065f8741860d465410bfed495dbb5f794590d338daMark Mendell  // The DCHECKS below check that a register is not specified twice in
5075f8741860d465410bfed495dbb5f794590d338daMark Mendell  // the summary. The out location can overlap with an input, so we need
5085f8741860d465410bfed495dbb5f794590d338daMark Mendell  // to special case it.
5095f8741860d465410bfed495dbb5f794590d338daMark Mendell  if (location.IsRegister()) {
5105f8741860d465410bfed495dbb5f794590d338daMark Mendell    DCHECK(is_out || !blocked_core_registers_[location.reg()]);
5115f8741860d465410bfed495dbb5f794590d338daMark Mendell    blocked_core_registers_[location.reg()] = true;
5125f8741860d465410bfed495dbb5f794590d338daMark Mendell  } else if (location.IsFpuRegister()) {
5135f8741860d465410bfed495dbb5f794590d338daMark Mendell    DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
5145f8741860d465410bfed495dbb5f794590d338daMark Mendell    blocked_fpu_registers_[location.reg()] = true;
5155f8741860d465410bfed495dbb5f794590d338daMark Mendell  } else if (location.IsFpuRegisterPair()) {
5165f8741860d465410bfed495dbb5f794590d338daMark Mendell    DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
5175f8741860d465410bfed495dbb5f794590d338daMark Mendell    blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
5185f8741860d465410bfed495dbb5f794590d338daMark Mendell    DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
5195f8741860d465410bfed495dbb5f794590d338daMark Mendell    blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
5205f8741860d465410bfed495dbb5f794590d338daMark Mendell  } else if (location.IsRegisterPair()) {
5215f8741860d465410bfed495dbb5f794590d338daMark Mendell    DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
5225f8741860d465410bfed495dbb5f794590d338daMark Mendell    blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
5235f8741860d465410bfed495dbb5f794590d338daMark Mendell    DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
5245f8741860d465410bfed495dbb5f794590d338daMark Mendell    blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
5255f8741860d465410bfed495dbb5f794590d338daMark Mendell  }
5265f8741860d465410bfed495dbb5f794590d338daMark Mendell}
5275f8741860d465410bfed495dbb5f794590d338daMark Mendell
528c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffrayvoid CodeGenerator::AllocateLocations(HInstruction* instruction) {
529c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray  instruction->Accept(GetLocationBuilder());
53088c13cddc3a4184908662b0f3de796565d348c76Alexandre Rames  DCHECK(CheckTypeConsistency(instruction));
531c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray  LocationSummary* locations = instruction->GetLocations();
532c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray  if (!instruction->IsSuspendCheckEntry()) {
533d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik    if (locations != nullptr) {
534d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik      if (locations->CanCall()) {
535d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik        MarkNotLeaf();
536d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik      } else if (locations->Intrinsified() &&
537d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik                 instruction->IsInvokeStaticOrDirect() &&
538d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik                 !instruction->AsInvokeStaticOrDirect()->HasCurrentMethodInput()) {
539d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik        // A static method call that has been fully intrinsified, and cannot call on the slow
540d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik        // path or refer to the current method directly, no longer needs current method.
541d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik        return;
542d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik      }
543c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray    }
544c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray    if (instruction->NeedsCurrentMethod()) {
545c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray      SetRequiresCurrentMethod();
546c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray    }
547c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray  }
548c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray}
549c0572a451944f78397619dec34a38c36c11e9d2aNicolas Geoffray
550ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescuvoid CodeGenerator::MaybeRecordStat(MethodCompilationStat compilation_stat, size_t count) const {
551ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescu  if (stats_ != nullptr) {
552ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescu    stats_->RecordStat(compilation_stat, count);
553ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescu  }
554ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescu}
555ecc4366670e12b4812ef1653f7c8d52234ca1b1fSerban Constantinescu
556d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Markostd::unique_ptr<CodeGenerator> CodeGenerator::Create(HGraph* graph,
557d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                                     InstructionSet instruction_set,
558d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                                     const InstructionSetFeatures& isa_features,
559d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                                     const CompilerOptions& compiler_options,
560d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                                     OptimizingCompilerStats* stats) {
561d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko  ArenaAllocator* arena = graph->GetArena();
562d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray  switch (instruction_set) {
56350fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#ifdef ART_ENABLE_CODEGEN_arm
564d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray    case kArm:
565d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray    case kThumb2: {
566d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko      return std::unique_ptr<CodeGenerator>(
567d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko          new (arena) arm::CodeGeneratorARM(graph,
568d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                            *isa_features.AsArmInstructionSetFeatures(),
569d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                            compiler_options,
570d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                            stats));
571d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray    }
57250fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#endif
57350fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#ifdef ART_ENABLE_CODEGEN_arm64
5745319defdf502fc4569316473846b83180ec08035Alexandre Rames    case kArm64: {
575d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko      return std::unique_ptr<CodeGenerator>(
576d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko          new (arena) arm64::CodeGeneratorARM64(graph,
577d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                                *isa_features.AsArm64InstructionSetFeatures(),
578d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                                compiler_options,
579d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                                stats));
5805319defdf502fc4569316473846b83180ec08035Alexandre Rames    }
58150fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#endif
58250fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#ifdef ART_ENABLE_CODEGEN_mips
583f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic    case kMips: {
584d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko      return std::unique_ptr<CodeGenerator>(
585d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko          new (arena) mips::CodeGeneratorMIPS(graph,
586d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                              *isa_features.AsMipsInstructionSetFeatures(),
587d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                              compiler_options,
588d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                              stats));
589f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic    }
59050fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#endif
59150fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#ifdef ART_ENABLE_CODEGEN_mips64
5924dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze    case kMips64: {
593d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko      return std::unique_ptr<CodeGenerator>(
594d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko          new (arena) mips64::CodeGeneratorMIPS64(graph,
595d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                                  *isa_features.AsMips64InstructionSetFeatures(),
596d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                                  compiler_options,
597d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                                  stats));
5984dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze    }
59950fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#endif
60050fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#ifdef ART_ENABLE_CODEGEN_x86
601d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray    case kX86: {
602d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko      return std::unique_ptr<CodeGenerator>(
603d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko          new (arena) x86::CodeGeneratorX86(graph,
604d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                            *isa_features.AsX86InstructionSetFeatures(),
605d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                            compiler_options,
606d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                            stats));
6076a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko    }
60850fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#endif
60950fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#ifdef ART_ENABLE_CODEGEN_x86_64
6106a58cb16d803c9a7b3a75ccac8be19dd9d4e520dDmitry Petrochenko    case kX86_64: {
611d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko      return std::unique_ptr<CodeGenerator>(
612d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko          new (arena) x86_64::CodeGeneratorX86_64(graph,
613d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                                  *isa_features.AsX86_64InstructionSetFeatures(),
614d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                                  compiler_options,
615d58b837ae41c6d8ce010c362e8f85bd938715900Vladimir Marko                                                  stats));
616d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray    }
61750fa993d67f8a20322c27c1a77e7efcf826531fcAlex Light#endif
618d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray    default:
619787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray      return nullptr;
620d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray  }
621d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray}
622d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray
623d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffraysize_t CodeGenerator::ComputeStackMapsSize() {
624d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  return stack_map_stream_.PrepareForFillIn();
625d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray}
626d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
627b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffraystatic void CheckCovers(uint32_t dex_pc,
628b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                        const HGraph& graph,
629b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                        const CodeInfo& code_info,
630b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                        const ArenaVector<HSuspendCheck*>& loop_headers,
631b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                        ArenaVector<size_t>* covered) {
63209ed09866da6d8c7448ef297c148bfa577a247c2David Srbecky  CodeInfoEncoding encoding = code_info.ExtractEncoding();
633b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  for (size_t i = 0; i < loop_headers.size(); ++i) {
634b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    if (loop_headers[i]->GetDexPc() == dex_pc) {
635b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      if (graph.IsCompilingOsr()) {
636b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray        DCHECK(code_info.GetOsrStackMapForDexPc(dex_pc, encoding).IsValid());
637b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      }
638b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      ++(*covered)[i];
639b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    }
640b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  }
641b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray}
642b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray
643b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray// Debug helper to ensure loop entries in compiled code are matched by
644b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray// dex branch instructions.
645b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffraystatic void CheckLoopEntriesCanBeUsedForOsr(const HGraph& graph,
646b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                                            const CodeInfo& code_info,
647b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                                            const DexFile::CodeItem& code_item) {
648b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  if (graph.HasTryCatch()) {
649b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    // One can write loops through try/catch, which we do not support for OSR anyway.
650b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    return;
651b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  }
652b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  ArenaVector<HSuspendCheck*> loop_headers(graph.GetArena()->Adapter(kArenaAllocMisc));
653b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  for (HReversePostOrderIterator it(graph); !it.Done(); it.Advance()) {
654b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    if (it.Current()->IsLoopHeader()) {
655b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      HSuspendCheck* suspend_check = it.Current()->GetLoopInformation()->GetSuspendCheck();
656b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      if (!suspend_check->GetEnvironment()->IsFromInlinedInvoke()) {
657b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray        loop_headers.push_back(suspend_check);
658b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      }
659b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    }
660b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  }
661b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  ArenaVector<size_t> covered(loop_headers.size(), 0, graph.GetArena()->Adapter(kArenaAllocMisc));
662b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  const uint16_t* code_ptr = code_item.insns_;
663b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
664b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray
665b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  size_t dex_pc = 0;
666b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  while (code_ptr < code_end) {
667b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    const Instruction& instruction = *Instruction::At(code_ptr);
668b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    if (instruction.IsBranch()) {
669b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      uint32_t target = dex_pc + instruction.GetTargetOffset();
670b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      CheckCovers(target, graph, code_info, loop_headers, &covered);
671b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    } else if (instruction.IsSwitch()) {
67286ea7eeabe30c98bbe1651a51d03cb89776724e7David Brazdil      DexSwitchTable table(instruction, dex_pc);
673b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      uint16_t num_entries = table.GetNumEntries();
674b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      size_t offset = table.GetFirstValueIndex();
675b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray
676b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      // Use a larger loop counter type to avoid overflow issues.
677b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      for (size_t i = 0; i < num_entries; ++i) {
678b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray        // The target of the case.
679b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray        uint32_t target = dex_pc + table.GetEntryAt(i + offset);
680b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray        CheckCovers(target, graph, code_info, loop_headers, &covered);
681b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      }
682b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    }
683b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    dex_pc += instruction.SizeInCodeUnits();
684b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    code_ptr += instruction.SizeInCodeUnits();
685b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  }
686b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray
687b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  for (size_t i = 0; i < covered.size(); ++i) {
688b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    DCHECK_NE(covered[i], 0u) << "Loop in compiled code has no dex branch equivalent";
689b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  }
690b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray}
691b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray
692b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffrayvoid CodeGenerator::BuildStackMaps(MemoryRegion region, const DexFile::CodeItem& code_item) {
6933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  stack_map_stream_.FillIn(region);
694b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  if (kIsDebugBuild) {
695b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    CheckLoopEntriesCanBeUsedForOsr(*graph_, CodeInfo(region), code_item);
696b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  }
6973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray}
6983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
699eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffrayvoid CodeGenerator::RecordPcInfo(HInstruction* instruction,
700eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray                                 uint32_t dex_pc,
701eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray                                 SlowPathCode* slow_path) {
702d2ec87d84057174d4884ee16f652cbcfd31362e9Calin Juravle  if (instruction != nullptr) {
7031693a1f9c83a0bf5a29fa18ddc2d87e04e049233Roland Levillain    // The code generated for some type conversions
7044dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze    // may call the runtime, thus normally requiring a subsequent
7054dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze    // call to this method. However, the method verifier does not
7064dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze    // produce PC information for certain instructions, which are
7074dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze    // considered "atomic" (they cannot join a GC).
708624279f3c70f9904cbaf428078981b05d3b324c0Roland Levillain    // Therefore we do not currently record PC information for such
709624279f3c70f9904cbaf428078981b05d3b324c0Roland Levillain    // instructions.  As this may change later, we added this special
710624279f3c70f9904cbaf428078981b05d3b324c0Roland Levillain    // case so that code generators may nevertheless call
711624279f3c70f9904cbaf428078981b05d3b324c0Roland Levillain    // CodeGenerator::RecordPcInfo without triggering an error in
712624279f3c70f9904cbaf428078981b05d3b324c0Roland Levillain    // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
713624279f3c70f9904cbaf428078981b05d3b324c0Roland Levillain    // thereafter.
7141693a1f9c83a0bf5a29fa18ddc2d87e04e049233Roland Levillain    if (instruction->IsTypeConversion()) {
715d2ec87d84057174d4884ee16f652cbcfd31362e9Calin Juravle      return;
716d2ec87d84057174d4884ee16f652cbcfd31362e9Calin Juravle    }
717d2ec87d84057174d4884ee16f652cbcfd31362e9Calin Juravle    if (instruction->IsRem()) {
718d2ec87d84057174d4884ee16f652cbcfd31362e9Calin Juravle      Primitive::Type type = instruction->AsRem()->GetResultType();
719d2ec87d84057174d4884ee16f652cbcfd31362e9Calin Juravle      if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
720d2ec87d84057174d4884ee16f652cbcfd31362e9Calin Juravle        return;
721d2ec87d84057174d4884ee16f652cbcfd31362e9Calin Juravle      }
722d2ec87d84057174d4884ee16f652cbcfd31362e9Calin Juravle    }
723624279f3c70f9904cbaf428078981b05d3b324c0Roland Levillain  }
724624279f3c70f9904cbaf428078981b05d3b324c0Roland Levillain
725b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray  uint32_t outer_dex_pc = dex_pc;
726b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray  uint32_t outer_environment_size = 0;
727b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray  uint32_t inlining_depth = 0;
728b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray  if (instruction != nullptr) {
729b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray    for (HEnvironment* environment = instruction->GetEnvironment();
730b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray         environment != nullptr;
731b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray         environment = environment->GetParent()) {
732b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray      outer_dex_pc = environment->GetDexPc();
733b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray      outer_environment_size = environment->Size();
734b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray      if (environment != instruction->GetEnvironment()) {
735b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray        inlining_depth++;
736b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray      }
737b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray    }
738b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray  }
739b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray
7403946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  // Collect PC infos for the mapping table.
741bd8c725e465cc7f44062745a6f2b73248f5159edVladimir Marko  uint32_t native_pc = GetAssembler()->CodeSize();
7423946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
7433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  if (instruction == nullptr) {
744c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky    // For stack overflow checks and native-debug-info entries without dex register
745c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky    // mapping (i.e. start of basic block or start of slow path).
746bd8c725e465cc7f44062745a6f2b73248f5159edVladimir Marko    stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
7474f46ac5179967dda5966f2dcecf2cf08977951efCalin Juravle    stack_map_stream_.EndStackMapEntry();
748eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray    return;
749eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray  }
750eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray  LocationSummary* locations = instruction->GetLocations();
751eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray
752eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray  uint32_t register_mask = locations->GetRegisterMask();
753eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray  if (locations->OnlyCallsOnSlowPath()) {
754eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray    // In case of slow path, we currently set the location of caller-save registers
755eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray    // to register (instead of their stack location when pushed before the slow-path
756eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray    // call). Therefore register_mask contains both callee-save and caller-save
757eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray    // registers that hold objects. We must remove the caller-save from the mask, since
758eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray    // they will be overwritten by the callee.
759eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray    register_mask &= core_callee_save_mask_;
760eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray  }
761eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray  // The register mask must be a subset of callee-save registers.
762eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray  DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
763bd8c725e465cc7f44062745a6f2b73248f5159edVladimir Marko  stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
764bd8c725e465cc7f44062745a6f2b73248f5159edVladimir Marko                                       native_pc,
7654f46ac5179967dda5966f2dcecf2cf08977951efCalin Juravle                                       register_mask,
7664f46ac5179967dda5966f2dcecf2cf08977951efCalin Juravle                                       locations->GetStackMask(),
767b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray                                       outer_environment_size,
7684f46ac5179967dda5966f2dcecf2cf08977951efCalin Juravle                                       inlining_depth);
769b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray
770b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray  EmitEnvironment(instruction->GetEnvironment(), slow_path);
771b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray  stack_map_stream_.EndStackMapEntry();
772b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray
773b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
774b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  if (instruction->IsSuspendCheck() &&
775b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      (info != nullptr) &&
776b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      graph_->IsCompilingOsr() &&
777b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      (inlining_depth == 0)) {
778b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    DCHECK_EQ(info->GetSuspendCheck(), instruction);
779b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    // We duplicate the stack map as a marker that this stack map can be an OSR entry.
780b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    // Duplicating it avoids having the runtime recognize and skip an OSR stack map.
781b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    DCHECK(info->IsIrreducible());
782b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    stack_map_stream_.BeginStackMapEntry(
783b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray        dex_pc, native_pc, register_mask, locations->GetStackMask(), outer_environment_size, 0);
784b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    EmitEnvironment(instruction->GetEnvironment(), slow_path);
785b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    stack_map_stream_.EndStackMapEntry();
786b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    if (kIsDebugBuild) {
787b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      HEnvironment* environment = instruction->GetEnvironment();
788b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
789b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray        HInstruction* in_environment = environment->GetInstructionAt(i);
790b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray        if (in_environment != nullptr) {
791b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray          DCHECK(in_environment->IsPhi() || in_environment->IsConstant());
792b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray          Location location = environment->GetLocationAt(i);
793b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray          DCHECK(location.IsStackSlot() ||
794b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                 location.IsDoubleStackSlot() ||
795b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                 location.IsConstant() ||
796b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                 location.IsInvalid());
797b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray          if (location.IsStackSlot() || location.IsDoubleStackSlot()) {
798b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray            DCHECK_LT(location.GetStackIndex(), static_cast<int32_t>(GetFrameSize()));
799b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray          }
800b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray        }
801b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      }
802b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    }
803b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  } else if (kIsDebugBuild) {
804b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    // Ensure stack maps are unique, by checking that the native pc in the stack map
805b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    // last emitted is different than the native pc of the stack map just emitted.
806b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    size_t number_of_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
807b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    if (number_of_stack_maps > 1) {
808b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      DCHECK_NE(stack_map_stream_.GetStackMap(number_of_stack_maps - 1).native_pc_offset,
809b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                stack_map_stream_.GetStackMap(number_of_stack_maps - 2).native_pc_offset);
810b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray    }
811b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  }
812b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray}
813b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray
814b7070a2db8b0b7eca14f01f932be305be64ded57David Srbeckybool CodeGenerator::HasStackMapAtCurrentPc() {
815b7070a2db8b0b7eca14f01f932be305be64ded57David Srbecky  uint32_t pc = GetAssembler()->CodeSize();
816b7070a2db8b0b7eca14f01f932be305be64ded57David Srbecky  size_t count = stack_map_stream_.GetNumberOfStackMaps();
817b7070a2db8b0b7eca14f01f932be305be64ded57David Srbecky  return count > 0 && stack_map_stream_.GetStackMap(count - 1).native_pc_offset == pc;
818b7070a2db8b0b7eca14f01f932be305be64ded57David Srbecky}
819b7070a2db8b0b7eca14f01f932be305be64ded57David Srbecky
820d28f4a00933a4a3b8d5e9db73b8532924d0f989dDavid Srbeckyvoid CodeGenerator::MaybeRecordNativeDebugInfo(HInstruction* instruction,
821d28f4a00933a4a3b8d5e9db73b8532924d0f989dDavid Srbecky                                               uint32_t dex_pc,
822d28f4a00933a4a3b8d5e9db73b8532924d0f989dDavid Srbecky                                               SlowPathCode* slow_path) {
823c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky  if (GetCompilerOptions().GetNativeDebuggable() && dex_pc != kNoDexPc) {
824c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky    if (HasStackMapAtCurrentPc()) {
825c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky      // Ensure that we do not collide with the stack map of the previous instruction.
826c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky      GenerateNop();
827c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky    }
828d28f4a00933a4a3b8d5e9db73b8532924d0f989dDavid Srbecky    RecordPcInfo(instruction, dex_pc, slow_path);
829c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky  }
830c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky}
831c7098ff991bb4e00a800d315d1c36f52a9cb0149David Srbecky
83277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdilvoid CodeGenerator::RecordCatchBlockInfo() {
83377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  ArenaAllocator* arena = graph_->GetArena();
83477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
835fa6b93c4b69e6d7ddfa2a4ed0aff01b0608c5a3aVladimir Marko  for (HBasicBlock* block : *block_order_) {
83677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    if (!block->IsCatchBlock()) {
83777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      continue;
83877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    }
83977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
84077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    uint32_t dex_pc = block->GetDexPc();
84177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    uint32_t num_vregs = graph_->GetNumberOfVRegs();
84277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    uint32_t inlining_depth = 0;  // Inlining of catch blocks is not supported at the moment.
84377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    uint32_t native_pc = GetAddressOf(block);
84477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    uint32_t register_mask = 0;   // Not used.
84577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
84677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    // The stack mask is not used, so we leave it empty.
847f6a35de9eeefb20f6446f1b4815b4dcb0161d09cVladimir Marko    ArenaBitVector* stack_mask =
848f6a35de9eeefb20f6446f1b4815b4dcb0161d09cVladimir Marko        ArenaBitVector::Create(arena, 0, /* expandable */ true, kArenaAllocCodeGenerator);
84977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
85077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    stack_map_stream_.BeginStackMapEntry(dex_pc,
85177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                                         native_pc,
85277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                                         register_mask,
85377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                                         stack_mask,
85477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                                         num_vregs,
85577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                                         inlining_depth);
85677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
85777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    HInstruction* current_phi = block->GetFirstPhi();
85877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    for (size_t vreg = 0; vreg < num_vregs; ++vreg) {
85977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) {
86077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      HInstruction* next_phi = current_phi->GetNext();
86177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      DCHECK(next_phi == nullptr ||
86277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil             current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber())
86377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          << "Phis need to be sorted by vreg number to keep this a linear-time loop.";
86477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      current_phi = next_phi;
86577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    }
86677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
86777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) {
86877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
86977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      } else {
87077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        Location location = current_phi->GetLiveInterval()->ToLocation();
87177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        switch (location.GetKind()) {
87277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          case Location::kStackSlot: {
87377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil            stack_map_stream_.AddDexRegisterEntry(
87477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
87577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil            break;
87677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          }
87777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          case Location::kDoubleStackSlot: {
87877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil            stack_map_stream_.AddDexRegisterEntry(
87977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
88077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil            stack_map_stream_.AddDexRegisterEntry(
88177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil                DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
88277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil            ++vreg;
88377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil            DCHECK_LT(vreg, num_vregs);
88477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil            break;
88577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          }
88677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          default: {
88777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil            // All catch phis must be allocated to a stack slot.
88877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil            LOG(FATAL) << "Unexpected kind " << location.GetKind();
88977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil            UNREACHABLE();
89077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil          }
89177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil        }
89277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      }
89377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    }
89477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
89577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    stack_map_stream_.EndStackMapEntry();
89677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  }
89777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil}
89877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
899b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffrayvoid CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
900b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray  if (environment == nullptr) return;
901b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray
902b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray  if (environment->GetParent() != nullptr) {
903b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray    // We emit the parent environment first.
904b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray    EmitEnvironment(environment->GetParent(), slow_path);
905b176d7c6c8c01a50317f837a78de5da57ee84fb2Nicolas Geoffray    stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
906b176d7c6c8c01a50317f837a78de5da57ee84fb2Nicolas Geoffray                                           environment->GetDexPc(),
907b176d7c6c8c01a50317f837a78de5da57ee84fb2Nicolas Geoffray                                           environment->GetInvokeType(),
908b176d7c6c8c01a50317f837a78de5da57ee84fb2Nicolas Geoffray                                           environment->Size());
9090a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray  }
910eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray
911eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray  // Walk over the environment, and record the location of dex registers.
912b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray  for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
913eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray    HInstruction* current = environment->GetInstructionAt(i);
914eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray    if (current == nullptr) {
915b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray      stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
916eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      continue;
917eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray    }
918eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray
9190a23d74dc2751440822960eab218be4cb8843647Nicolas Geoffray    Location location = environment->GetLocationAt(i);
920eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray    switch (location.GetKind()) {
921eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      case Location::kConstant: {
922eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        DCHECK_EQ(current, location.GetConstant());
923eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        if (current->IsLongConstant()) {
924eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          int64_t value = current->AsLongConstant()->GetValue();
925eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(
926b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray              DexRegisterLocation::Kind::kConstant, Low32Bits(value));
927eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(
928b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray              DexRegisterLocation::Kind::kConstant, High32Bits(value));
929b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          ++i;
930eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          DCHECK_LT(i, environment_size);
931eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        } else if (current->IsDoubleConstant()) {
932da4d79bc9a4aeb9da7c6259ce4c9c1c3bf545eb8Roland Levillain          int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
933eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(
934b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray              DexRegisterLocation::Kind::kConstant, Low32Bits(value));
935eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(
936b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray              DexRegisterLocation::Kind::kConstant, High32Bits(value));
937b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          ++i;
938eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          DCHECK_LT(i, environment_size);
939eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        } else if (current->IsIntConstant()) {
940eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          int32_t value = current->AsIntConstant()->GetValue();
941b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
942eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        } else if (current->IsNullConstant()) {
943b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
944eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        } else {
945eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          DCHECK(current->IsFloatConstant()) << current->DebugName();
946da4d79bc9a4aeb9da7c6259ce4c9c1c3bf545eb8Roland Levillain          int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
947b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
948eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        }
949eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        break;
950eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      }
951eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray
952eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      case Location::kStackSlot: {
953eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        stack_map_stream_.AddDexRegisterEntry(
954b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray            DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
955eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        break;
956eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      }
957eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray
958eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      case Location::kDoubleStackSlot: {
959eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        stack_map_stream_.AddDexRegisterEntry(
960b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray            DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
961eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        stack_map_stream_.AddDexRegisterEntry(
962b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray            DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
963b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray        ++i;
964eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        DCHECK_LT(i, environment_size);
965eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        break;
966eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      }
967eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray
968eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      case Location::kRegister : {
969eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        int id = location.reg();
970eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
971eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
972b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
973eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          if (current->GetType() == Primitive::kPrimLong) {
974eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray            stack_map_stream_.AddDexRegisterEntry(
975b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray                DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
976b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray            ++i;
977eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray            DCHECK_LT(i, environment_size);
978eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          }
979eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        } else {
980b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
981eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          if (current->GetType() == Primitive::kPrimLong) {
982d9cb68e3212d31d61445fb7e8446f68991720009David Brazdil            stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id);
983b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray            ++i;
984eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray            DCHECK_LT(i, environment_size);
985eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          }
986eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        }
987eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        break;
988eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      }
989eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray
990eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      case Location::kFpuRegister : {
991eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        int id = location.reg();
992eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
993eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
994b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
995eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          if (current->GetType() == Primitive::kPrimDouble) {
996eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray            stack_map_stream_.AddDexRegisterEntry(
997b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray                DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
998b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray            ++i;
999eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray            DCHECK_LT(i, environment_size);
1000eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          }
1001eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        } else {
1002b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
1003eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          if (current->GetType() == Primitive::kPrimDouble) {
1004d9cb68e3212d31d61445fb7e8446f68991720009David Brazdil            stack_map_stream_.AddDexRegisterEntry(
1005d9cb68e3212d31d61445fb7e8446f68991720009David Brazdil                DexRegisterLocation::Kind::kInFpuRegisterHigh, id);
1006b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray            ++i;
1007eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray            DCHECK_LT(i, environment_size);
1008eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          }
1009eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        }
1010eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        break;
1011eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      }
1012eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray
1013eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      case Location::kFpuRegisterPair : {
1014eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        int low = location.low();
1015eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        int high = location.high();
1016eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
1017eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
1018b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1019eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        } else {
1020b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
1021eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        }
1022eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
1023eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
1024b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1025b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          ++i;
1026eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        } else {
1027b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
1028b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          ++i;
1029eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        }
1030eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        DCHECK_LT(i, environment_size);
1031eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        break;
1032eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      }
1033eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray
1034eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      case Location::kRegisterPair : {
1035eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        int low = location.low();
1036eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        int high = location.high();
1037eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
1038eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
1039b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1040eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        } else {
1041b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
1042eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        }
1043eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
1044eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray          uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
1045b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1046eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        } else {
1047b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray          stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
1048eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        }
1049b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray        ++i;
1050eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        DCHECK_LT(i, environment_size);
1051eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        break;
1052eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      }
1053eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray
1054eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      case Location::kInvalid: {
1055b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray        stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
1056eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        break;
1057eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      }
1058eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray
1059eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray      default:
1060eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        LOG(FATAL) << "Unexpected kind " << location.GetKind();
10613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    }
10623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
1063b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray
1064b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray  if (environment->GetParent() != nullptr) {
1065b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray    stack_map_stream_.EndInlineInfoEntry();
1066b1d0f3f7e92fdcc92fe2d4c48cbb1262c005583fNicolas Geoffray  }
10673946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray}
10683946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
106977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdilbool CodeGenerator::IsImplicitNullCheckAllowed(HNullCheck* null_check) const {
107077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  return compiler_options_.GetImplicitNullChecks() &&
107177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil         // Null checks which might throw into a catch block need to save live
107277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil         // registers and therefore cannot be done implicitly.
107377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil         !null_check->CanThrowIntoCatchBlock();
107477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil}
107577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
107677520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravlebool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
107777520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle  HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
1078641547a5f18ca2ea54469cceadcfef64f132e5e0Calin Juravle
1079641547a5f18ca2ea54469cceadcfef64f132e5e0Calin Juravle  return (first_next_not_move != nullptr)
1080641547a5f18ca2ea54469cceadcfef64f132e5e0Calin Juravle      && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
108177520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle}
108277520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle
108377520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravlevoid CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
108477520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle  // If we are from a static path don't record the pc as we can't throw NPE.
108577520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle  // NB: having the checks here makes the code much less verbose in the arch
108677520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle  // specific code generators.
108777520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle  if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
108877520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle    return;
108977520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle  }
109077520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle
1091641547a5f18ca2ea54469cceadcfef64f132e5e0Calin Juravle  if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
109277520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle    return;
109377520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle  }
109477520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle
109577520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle  // Find the first previous instruction which is not a move.
109677520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle  HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
109777520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle
109877520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle  // If the instruction is a null check it means that `instr` is the first user
109977520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle  // and needs to record the pc.
110077520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle  if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
110177520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle    HNullCheck* null_check = first_prev_not_move->AsNullCheck();
110277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    if (IsImplicitNullCheckAllowed(null_check)) {
110377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // TODO: The parallel moves modify the environment. Their changes need to be
110477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // reverted otherwise the stack maps at the throw point will not be correct.
110577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      RecordPcInfo(null_check, null_check->GetDexPc());
110677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    }
110777520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle  }
110877520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle}
110977520bca97ec44e3758510cebd0f20e3bb4584eaCalin Juravle
11102ae48182573da7087bffc2873730bc758ec29696Calin Juravlevoid CodeGenerator::GenerateNullCheck(HNullCheck* instruction) {
11112ae48182573da7087bffc2873730bc758ec29696Calin Juravle  if (IsImplicitNullCheckAllowed(instruction)) {
11122ae48182573da7087bffc2873730bc758ec29696Calin Juravle    MaybeRecordStat(kImplicitNullCheckGenerated);
11132ae48182573da7087bffc2873730bc758ec29696Calin Juravle    GenerateImplicitNullCheck(instruction);
11142ae48182573da7087bffc2873730bc758ec29696Calin Juravle  } else {
11152ae48182573da7087bffc2873730bc758ec29696Calin Juravle    MaybeRecordStat(kExplicitNullCheckGenerated);
11162ae48182573da7087bffc2873730bc758ec29696Calin Juravle    GenerateExplicitNullCheck(instruction);
11172ae48182573da7087bffc2873730bc758ec29696Calin Juravle  }
11182ae48182573da7087bffc2873730bc758ec29696Calin Juravle}
11192ae48182573da7087bffc2873730bc758ec29696Calin Juravle
1120a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffrayvoid CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
1121a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  LocationSummary* locations = suspend_check->GetLocations();
1122a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  HBasicBlock* block = suspend_check->GetBlock();
1123a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
1124a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  DCHECK(block->IsLoopHeader());
1125a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray
1126a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1127a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray    HInstruction* current = it.Current();
1128a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray    LiveInterval* interval = current->GetLiveInterval();
1129a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray    // We only need to clear bits of loop phis containing objects and allocated in register.
1130a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray    // Loop phis allocated on stack already have the object in the stack.
1131a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray    if (current->GetType() == Primitive::kPrimNot
1132a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray        && interval->HasRegister()
1133a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray        && interval->HasSpillSlot()) {
1134a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray      locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
1135a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray    }
1136a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  }
1137a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray}
1138a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray
11399021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffrayvoid CodeGenerator::EmitParallelMoves(Location from1,
11409021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray                                      Location to1,
11419021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray                                      Primitive::Type type1,
11429021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray                                      Location from2,
11439021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray                                      Location to2,
11449021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray                                      Primitive::Type type2) {
1145a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  HParallelMove parallel_move(GetGraph()->GetArena());
11469021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray  parallel_move.AddMove(from1, to1, type1, nullptr);
11479021825d1e73998b99c81e89c73796f6f2845471Nicolas Geoffray  parallel_move.AddMove(from2, to2, type2, nullptr);
1148a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  GetMoveResolver()->EmitNativeCode(&parallel_move);
1149a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray}
1150a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray
115178e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Ramesvoid CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) {
115278e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames  // Ensure that the call kind indication given to the register allocator is
115378e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames  // coherent with the runtime call generated, and that the GC side effect is
115478e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames  // set when required.
115578e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames  if (slow_path == nullptr) {
11560d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    DCHECK(instruction->GetLocations()->WillCall())
11570d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain        << "instruction->DebugName()=" << instruction->DebugName();
1158df3f8227badd0276177774a72f1bcb181688d954Roland Levillain    DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()))
11590d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain        << "instruction->DebugName()=" << instruction->DebugName()
11600d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain        << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString();
116178e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames  } else {
1162df3f8227badd0276177774a72f1bcb181688d954Roland Levillain    DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal())
11630d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain        << "instruction->DebugName()=" << instruction->DebugName()
11640d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain        << " slow_path->GetDescription()=" << slow_path->GetDescription();
116578e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames    DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) ||
11660d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain           // When read barriers are enabled, some instructions use a
11670d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain           // slow path to emit a read barrier, which does not trigger
11680d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain           // GC, is not fatal, nor is emitted by HDeoptimize
11690d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain           // instructions.
11700d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain           (kEmitCompilerReadBarrier &&
11710d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain            (instruction->IsInstanceFieldGet() ||
11720d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain             instruction->IsStaticFieldGet() ||
11730d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain             instruction->IsArraySet() ||
11740d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain             instruction->IsArrayGet() ||
11750d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain             instruction->IsLoadClass() ||
11760d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain             instruction->IsLoadString() ||
11770d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain             instruction->IsInstanceOf() ||
11780d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain             instruction->IsCheckCast())))
11790d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain        << "instruction->DebugName()=" << instruction->DebugName()
11800d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain        << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString()
11810d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain        << " slow_path->GetDescription()=" << slow_path->GetDescription();
118278e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames  }
118378e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames
118478e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames  // Check the coherency of leaf information.
118578e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames  DCHECK(instruction->IsSuspendCheck()
118678e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames         || ((slow_path != nullptr) && slow_path->IsFatal())
118778e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames         || instruction->GetLocations()->CanCall()
1188df3f8227badd0276177774a72f1bcb181688d954Roland Levillain         || !IsLeafMethod())
1189df3f8227badd0276177774a72f1bcb181688d954Roland Levillain      << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : "");
119078e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames}
119178e3ef6bc5f8aa149f2f8bf0c78ce854c2f910faAlexandre Rames
1192a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffrayvoid SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
11930d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  RegisterSet* live_registers = locations->GetLiveRegisters();
1194a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
11950d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain
1196a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1197a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray    if (!codegen->IsCoreCalleeSaveRegister(i)) {
11980d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain      if (live_registers->ContainsCoreRegister(i)) {
1199988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray        // If the register holds an object, update the stack mask.
1200988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray        if (locations->RegisterContainsObject(i)) {
1201988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray          locations->SetStackBit(stack_offset / kVRegSize);
1202988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray        }
1203a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray        DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1204eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1205eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        saved_core_stack_offsets_[i] = stack_offset;
1206a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray        stack_offset += codegen->SaveCoreRegister(stack_offset, i);
12073bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray      }
12083bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
12093bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  }
12103bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
1211a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1212a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray    if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
12130d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain      if (live_registers->ContainsFloatingPointRegister(i)) {
1214a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray        DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1215eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1216eeefa1276e83776f08704a3db4237423b0627e20Nicolas Geoffray        saved_fpu_stack_offsets_[i] = stack_offset;
1217a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray        stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
1218988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray      }
12193bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
12203bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  }
12213bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray}
12223bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
1223a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffrayvoid SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
12240d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  RegisterSet* live_registers = locations->GetLiveRegisters();
1225a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
12260d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain
1227a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1228a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray    if (!codegen->IsCoreCalleeSaveRegister(i)) {
12290d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain      if (live_registers->ContainsCoreRegister(i)) {
1230a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray        DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
12310d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain        DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1232a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray        stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1233988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray      }
12343bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
12353bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  }
12363bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
1237a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray  for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1238a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray    if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
12390d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain      if (live_registers->ContainsFloatingPointRegister(i)) {
1240a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray        DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
12410d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain        DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1242a8ac9130b872c080299afacf5dcaab513d13ea87Nicolas Geoffray        stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1243988939683c26c0b1c8808fc206add6337319509aNicolas Geoffray      }
12443bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray    }
12453bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray  }
12463bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray}
12473bca0df855f0e575c6ee020ed016999fc8f14122Nicolas Geoffray
12485bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffrayvoid CodeGenerator::CreateSystemArrayCopyLocationSummary(HInvoke* invoke) {
12495bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  // Check to see if we have known failures that will cause us to have to bail out
12505bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  // to the runtime, and just generate the runtime call directly.
12515bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
12525bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
12535bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray
12545bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  // The positions must be non-negative.
12555bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
12565bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray      (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
12575bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray    // We will have to fail anyways.
12585bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray    return;
12595bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  }
12605bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray
12615bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  // The length must be >= 0.
12625bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
12635bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  if (length != nullptr) {
12645bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray    int32_t len = length->GetValue();
12655bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray    if (len < 0) {
12665bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray      // Just call as normal.
12675bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray      return;
12685bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray    }
12695bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  }
12705bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray
12715bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  SystemArrayCopyOptimizations optimizations(invoke);
12725bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray
12735bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  if (optimizations.GetDestinationIsSource()) {
12745bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray    if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
12755bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray      // We only support backward copying if source and destination are the same.
12765bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray      return;
12775bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray    }
12785bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  }
12795bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray
12805bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
12815bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray    // We currently don't intrinsify primitive copying.
12825bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray    return;
12835bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  }
12845bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray
12855bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
12865bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  LocationSummary* locations = new (allocator) LocationSummary(invoke,
12875bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray                                                               LocationSummary::kCallOnSlowPath,
12885bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray                                                               kIntrinsified);
12895bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
12905bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  locations->SetInAt(0, Location::RequiresRegister());
12915bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
12925bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  locations->SetInAt(2, Location::RequiresRegister());
12935bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
12945bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
12955bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray
12965bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  locations->AddTemp(Location::RequiresRegister());
12975bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  locations->AddTemp(Location::RequiresRegister());
12985bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray  locations->AddTemp(Location::RequiresRegister());
12995bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray}
13005bd05a5c9492189ec28edaf6396d6a39ddf03367Nicolas Geoffray
1301d4dd255db1d110ceb5551f6d95ff31fb57420994Nicolas Geoffray}  // namespace art
1302