blob: ea0b9eca9aacec6774a118fc36a6927a380fb0f2 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator.h"
18
Alex Light50fa9932015-08-10 15:30:07 -070019#ifdef ART_ENABLE_CODEGEN_arm
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000020#include "code_generator_arm.h"
Alex Light50fa9932015-08-10 15:30:07 -070021#endif
22
23#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "code_generator_arm64.h"
Alex Light50fa9932015-08-10 15:30:07 -070025#endif
26
27#ifdef ART_ENABLE_CODEGEN_x86
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028#include "code_generator_x86.h"
Alex Light50fa9932015-08-10 15:30:07 -070029#endif
30
31#ifdef ART_ENABLE_CODEGEN_x86_64
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "code_generator_x86_64.h"
Alex Light50fa9932015-08-10 15:30:07 -070033#endif
34
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020035#ifdef ART_ENABLE_CODEGEN_mips
36#include "code_generator_mips.h"
37#endif
38
Alex Light50fa9932015-08-10 15:30:07 -070039#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -070040#include "code_generator_mips64.h"
Alex Light50fa9932015-08-10 15:30:07 -070041#endif
42
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070043#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000044#include "dex/verified_method.h"
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +000045#include "driver/compiler_driver.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000046#include "gc_map_builder.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010047#include "graph_visualizer.h"
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +010048#include "intrinsics.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000049#include "leb128.h"
50#include "mapping_table.h"
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010051#include "mirror/array-inl.h"
52#include "mirror/object_array-inl.h"
53#include "mirror/object_reference.h"
Alex Light50fa9932015-08-10 15:30:07 -070054#include "parallel_move_resolver.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010055#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000056#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000057#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000058#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000059
60namespace art {
61
Alexandre Rames88c13cd2015-04-14 17:35:39 +010062// Return whether a location is consistent with a type.
63static bool CheckType(Primitive::Type type, Location location) {
64 if (location.IsFpuRegister()
65 || (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresFpuRegister))) {
66 return (type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble);
67 } else if (location.IsRegister() ||
68 (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresRegister))) {
69 return Primitive::IsIntegralType(type) || (type == Primitive::kPrimNot);
70 } else if (location.IsRegisterPair()) {
71 return type == Primitive::kPrimLong;
72 } else if (location.IsFpuRegisterPair()) {
73 return type == Primitive::kPrimDouble;
74 } else if (location.IsStackSlot()) {
75 return (Primitive::IsIntegralType(type) && type != Primitive::kPrimLong)
76 || (type == Primitive::kPrimFloat)
77 || (type == Primitive::kPrimNot);
78 } else if (location.IsDoubleStackSlot()) {
79 return (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
80 } else if (location.IsConstant()) {
81 if (location.GetConstant()->IsIntConstant()) {
82 return Primitive::IsIntegralType(type) && (type != Primitive::kPrimLong);
83 } else if (location.GetConstant()->IsNullConstant()) {
84 return type == Primitive::kPrimNot;
85 } else if (location.GetConstant()->IsLongConstant()) {
86 return type == Primitive::kPrimLong;
87 } else if (location.GetConstant()->IsFloatConstant()) {
88 return type == Primitive::kPrimFloat;
89 } else {
90 return location.GetConstant()->IsDoubleConstant()
91 && (type == Primitive::kPrimDouble);
92 }
93 } else {
94 return location.IsInvalid() || (location.GetPolicy() == Location::kAny);
95 }
96}
97
98// Check that a location summary is consistent with an instruction.
99static bool CheckTypeConsistency(HInstruction* instruction) {
100 LocationSummary* locations = instruction->GetLocations();
101 if (locations == nullptr) {
102 return true;
103 }
104
105 if (locations->Out().IsUnallocated()
106 && (locations->Out().GetPolicy() == Location::kSameAsFirstInput)) {
107 DCHECK(CheckType(instruction->GetType(), locations->InAt(0)))
108 << instruction->GetType()
109 << " " << locations->InAt(0);
110 } else {
111 DCHECK(CheckType(instruction->GetType(), locations->Out()))
112 << instruction->GetType()
113 << " " << locations->Out();
114 }
115
116 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
117 DCHECK(CheckType(instruction->InputAt(i)->GetType(), locations->InAt(i)))
118 << instruction->InputAt(i)->GetType()
119 << " " << locations->InAt(i);
120 }
121
122 HEnvironment* environment = instruction->GetEnvironment();
123 for (size_t i = 0; i < instruction->EnvironmentSize(); ++i) {
124 if (environment->GetInstructionAt(i) != nullptr) {
125 Primitive::Type type = environment->GetInstructionAt(i)->GetType();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100126 DCHECK(CheckType(type, environment->GetLocationAt(i)))
127 << type << " " << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100128 } else {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100129 DCHECK(environment->GetLocationAt(i).IsInvalid())
130 << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100131 }
132 }
133 return true;
134}
135
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100136size_t CodeGenerator::GetCacheOffset(uint32_t index) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100137 return sizeof(GcRoot<mirror::Object>) * index;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100138}
139
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140size_t CodeGenerator::GetCachePointerOffset(uint32_t index) {
141 auto pointer_size = InstructionSetPointerSize(GetInstructionSet());
Vladimir Marko05792b92015-08-03 11:56:49 +0100142 return pointer_size * index;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700143}
144
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100145void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000146 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100147 if (!is_leaf) {
148 MarkNotLeaf();
149 }
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700150 const bool is_64_bit = Is64BitInstructionSet(GetInstructionSet());
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000151 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
152 + GetGraph()->GetTemporariesVRegSlots()
153 + 1 /* filler */,
154 0, /* the baseline compiler does not have live registers at slow path */
155 0, /* the baseline compiler does not have live registers at slow path */
156 GetGraph()->GetMaximumNumberOfOutVRegs()
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700157 + (is_64_bit ? 2 : 1) /* current method */,
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000158 GetGraph()->GetBlocks());
159 CompileInternal(allocator, /* is_baseline */ true);
160}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100161
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000162bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100163 DCHECK_EQ((*block_order_)[current_block_index_], current);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000164 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
165}
166
167HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100168 for (size_t i = current_block_index_ + 1; i < block_order_->size(); ++i) {
169 HBasicBlock* block = (*block_order_)[i];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000170 if (!block->IsSingleJump()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000171 return block;
172 }
173 }
174 return nullptr;
175}
176
177HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000178 while (block->IsSingleJump()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100179 block = block->GetSuccessors()[0];
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000180 }
181 return block;
182}
183
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100184class DisassemblyScope {
185 public:
186 DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen)
187 : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) {
188 if (codegen_.GetDisassemblyInformation() != nullptr) {
189 start_offset_ = codegen_.GetAssembler().CodeSize();
190 }
191 }
192
193 ~DisassemblyScope() {
194 // We avoid building this data when we know it will not be used.
195 if (codegen_.GetDisassemblyInformation() != nullptr) {
196 codegen_.GetDisassemblyInformation()->AddInstructionInterval(
197 instruction_, start_offset_, codegen_.GetAssembler().CodeSize());
198 }
199 }
200
201 private:
202 const CodeGenerator& codegen_;
203 HInstruction* instruction_;
204 size_t start_offset_;
205};
206
207
208void CodeGenerator::GenerateSlowPaths() {
209 size_t code_start = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100210 for (SlowPathCode* slow_path : slow_paths_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000211 current_slow_path_ = slow_path;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100212 if (disasm_info_ != nullptr) {
213 code_start = GetAssembler()->CodeSize();
214 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100215 slow_path->EmitNativeCode(this);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100216 if (disasm_info_ != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100217 disasm_info_->AddSlowPathInterval(slow_path, code_start, GetAssembler()->CodeSize());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100218 }
219 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000220 current_slow_path_ = nullptr;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100221}
222
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000223void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Roland Levillain3e3d7332015-04-28 11:00:54 +0100224 is_baseline_ = is_baseline;
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100225 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000226 DCHECK_EQ(current_block_index_, 0u);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100227
228 size_t frame_start = GetAssembler()->CodeSize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000229 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100230 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100231 if (disasm_info_ != nullptr) {
232 disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize());
233 }
234
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100235 for (size_t e = block_order_->size(); current_block_index_ < e; ++current_block_index_) {
236 HBasicBlock* block = (*block_order_)[current_block_index_];
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000237 // Don't generate code for an empty block. Its predecessors will branch to its successor
238 // directly. Also, the label of that block will not be emitted, so this helps catch
239 // errors where we reference that label.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000240 if (block->IsSingleJump()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100241 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100242 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
243 HInstruction* current = it.Current();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100244 DisassemblyScope disassembly_scope(current, *this);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000245 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000246 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000247 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100248 DCHECK(CheckTypeConsistency(current));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100249 current->Accept(instruction_visitor);
250 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000251 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000252
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100253 GenerateSlowPaths();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000254
David Brazdil77a48ae2015-09-15 12:34:04 +0000255 // Emit catch stack maps at the end of the stack map stream as expected by the
256 // runtime exception handler.
257 if (!is_baseline && graph_->HasTryCatch()) {
258 RecordCatchBlockInfo();
259 }
260
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000261 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000262 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000263}
264
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100265void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000266 // The register allocator already called `InitializeCodeGeneration`,
267 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000268 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100269 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000270 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000271}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100272
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000273void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100274 size_t code_size = GetAssembler()->CodeSize();
275 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000276
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100277 MemoryRegion code(buffer, code_size);
278 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000279}
280
Vladimir Marko58155012015-08-19 12:49:41 +0000281void CodeGenerator::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches ATTRIBUTE_UNUSED) {
282 // No linker patches by default.
283}
284
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100285size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
286 for (size_t i = 0; i < length; ++i) {
287 if (!array[i]) {
288 array[i] = true;
289 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100290 }
291 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100292 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000293 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000294}
295
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000296size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
297 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000298 if (!array[i] && !array[i + 1]) {
299 array[i] = true;
300 array[i + 1] = true;
301 return i;
302 }
303 }
304 LOG(FATAL) << "Could not find a register in baseline register allocator";
305 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100306}
307
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000308void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
309 size_t maximum_number_of_live_core_registers,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000310 size_t maximum_number_of_live_fpu_registers,
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000311 size_t number_of_out_slots,
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100312 const ArenaVector<HBasicBlock*>& block_order) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000313 block_order_ = &block_order;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100314 DCHECK(!block_order.empty());
315 DCHECK(block_order[0] == GetGraph()->GetEntryBlock());
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000316 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100317 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
318
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000319 if (number_of_spill_slots == 0
320 && !HasAllocatedCalleeSaveRegisters()
321 && IsLeafMethod()
322 && !RequiresCurrentMethod()) {
323 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000324 DCHECK_EQ(maximum_number_of_live_fpu_registers, 0u);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000325 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
326 } else {
327 SetFrameSize(RoundUp(
328 number_of_spill_slots * kVRegSize
329 + number_of_out_slots * kVRegSize
330 + maximum_number_of_live_core_registers * GetWordSize()
Roland Levillain0d5a2812015-11-13 10:07:31 +0000331 + maximum_number_of_live_fpu_registers * GetFloatingPointSpillSlotSize()
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000332 + FrameEntrySpillSize(),
333 kStackAlignment));
334 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100335}
336
337Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
338 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000339 // The type of the previous instruction tells us if we need a single or double stack slot.
340 Primitive::Type type = temp->GetType();
341 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100342 // Use the temporary region (right below the dex registers).
343 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
344 - kVRegSize // filler
345 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000346 - ((temp_size + temp->GetIndex()) * kVRegSize);
347 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100348}
349
350int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
351 uint16_t reg_number = local->GetRegNumber();
352 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
353 if (reg_number >= number_of_locals) {
354 // Local is a parameter of the method. It is stored in the caller's frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700355 // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode.
356 return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100357 + (reg_number - number_of_locals) * kVRegSize;
358 } else {
359 // Local is a temporary in this method. It is stored in this method's frame.
360 return GetFrameSize() - FrameEntrySpillSize()
361 - kVRegSize // filler.
362 - (number_of_locals * kVRegSize)
363 + (reg_number * kVRegSize);
364 }
365}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100366
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100367void CodeGenerator::CreateCommonInvokeLocationSummary(
Nicolas Geoffray4e40c262015-06-03 12:02:38 +0100368 HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100369 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
370 LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100371
372 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
373 HInstruction* input = invoke->InputAt(i);
374 locations->SetInAt(i, visitor->GetNextLocation(input->GetType()));
375 }
376
377 locations->SetOut(visitor->GetReturnLocation(invoke->GetType()));
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100378
379 if (invoke->IsInvokeStaticOrDirect()) {
380 HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect();
Vladimir Markodc151b22015-10-15 18:02:30 +0100381 switch (call->GetMethodLoadKind()) {
382 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000383 locations->SetInAt(call->GetSpecialInputIndex(), visitor->GetMethodLocation());
Vladimir Markodc151b22015-10-15 18:02:30 +0100384 break;
385 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod:
386 locations->AddTemp(visitor->GetMethodLocation());
Vladimir Markoc53c0792015-11-19 15:48:33 +0000387 locations->SetInAt(call->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Markodc151b22015-10-15 18:02:30 +0100388 break;
389 default:
390 locations->AddTemp(visitor->GetMethodLocation());
391 break;
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100392 }
393 } else {
394 locations->AddTemp(visitor->GetMethodLocation());
395 }
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100396}
397
Calin Juravle175dc732015-08-25 15:42:32 +0100398void CodeGenerator::GenerateInvokeUnresolvedRuntimeCall(HInvokeUnresolved* invoke) {
399 MoveConstant(invoke->GetLocations()->GetTemp(0), invoke->GetDexMethodIndex());
400
401 // Initialize to anything to silent compiler warnings.
402 QuickEntrypointEnum entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
403 switch (invoke->GetOriginalInvokeType()) {
404 case kStatic:
405 entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
406 break;
407 case kDirect:
408 entrypoint = kQuickInvokeDirectTrampolineWithAccessCheck;
409 break;
410 case kVirtual:
411 entrypoint = kQuickInvokeVirtualTrampolineWithAccessCheck;
412 break;
413 case kSuper:
414 entrypoint = kQuickInvokeSuperTrampolineWithAccessCheck;
415 break;
416 case kInterface:
417 entrypoint = kQuickInvokeInterfaceTrampolineWithAccessCheck;
418 break;
419 }
420 InvokeRuntime(entrypoint, invoke, invoke->GetDexPc(), nullptr);
421}
422
Calin Juravlee460d1d2015-09-29 04:52:17 +0100423void CodeGenerator::CreateUnresolvedFieldLocationSummary(
424 HInstruction* field_access,
425 Primitive::Type field_type,
426 const FieldAccessCallingConvention& calling_convention) {
427 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
428 || field_access->IsUnresolvedInstanceFieldSet();
429 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
430 || field_access->IsUnresolvedStaticFieldGet();
431
432 ArenaAllocator* allocator = field_access->GetBlock()->GetGraph()->GetArena();
433 LocationSummary* locations =
434 new (allocator) LocationSummary(field_access, LocationSummary::kCall);
435
436 locations->AddTemp(calling_convention.GetFieldIndexLocation());
437
438 if (is_instance) {
439 // Add the `this` object for instance field accesses.
440 locations->SetInAt(0, calling_convention.GetObjectLocation());
441 }
442
443 // Note that pSetXXStatic/pGetXXStatic always takes/returns an int or int64
444 // regardless of the the type. Because of that we forced to special case
445 // the access to floating point values.
446 if (is_get) {
447 if (Primitive::IsFloatingPointType(field_type)) {
448 // The return value will be stored in regular registers while register
449 // allocator expects it in a floating point register.
450 // Note We don't need to request additional temps because the return
451 // register(s) are already blocked due the call and they may overlap with
452 // the input or field index.
453 // The transfer between the two will be done at codegen level.
454 locations->SetOut(calling_convention.GetFpuLocation(field_type));
455 } else {
456 locations->SetOut(calling_convention.GetReturnLocation(field_type));
457 }
458 } else {
459 size_t set_index = is_instance ? 1 : 0;
460 if (Primitive::IsFloatingPointType(field_type)) {
461 // The set value comes from a float location while the calling convention
462 // expects it in a regular register location. Allocate a temp for it and
463 // make the transfer at codegen.
464 AddLocationAsTemp(calling_convention.GetSetValueLocation(field_type, is_instance), locations);
465 locations->SetInAt(set_index, calling_convention.GetFpuLocation(field_type));
466 } else {
467 locations->SetInAt(set_index,
468 calling_convention.GetSetValueLocation(field_type, is_instance));
469 }
470 }
471}
472
473void CodeGenerator::GenerateUnresolvedFieldAccess(
474 HInstruction* field_access,
475 Primitive::Type field_type,
476 uint32_t field_index,
477 uint32_t dex_pc,
478 const FieldAccessCallingConvention& calling_convention) {
479 LocationSummary* locations = field_access->GetLocations();
480
481 MoveConstant(locations->GetTemp(0), field_index);
482
483 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
484 || field_access->IsUnresolvedInstanceFieldSet();
485 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
486 || field_access->IsUnresolvedStaticFieldGet();
487
488 if (!is_get && Primitive::IsFloatingPointType(field_type)) {
489 // Copy the float value to be set into the calling convention register.
490 // Note that using directly the temp location is problematic as we don't
491 // support temp register pairs. To avoid boilerplate conversion code, use
492 // the location from the calling convention.
493 MoveLocation(calling_convention.GetSetValueLocation(field_type, is_instance),
494 locations->InAt(is_instance ? 1 : 0),
495 (Primitive::Is64BitType(field_type) ? Primitive::kPrimLong : Primitive::kPrimInt));
496 }
497
498 QuickEntrypointEnum entrypoint = kQuickSet8Static; // Initialize to anything to avoid warnings.
499 switch (field_type) {
500 case Primitive::kPrimBoolean:
501 entrypoint = is_instance
502 ? (is_get ? kQuickGetBooleanInstance : kQuickSet8Instance)
503 : (is_get ? kQuickGetBooleanStatic : kQuickSet8Static);
504 break;
505 case Primitive::kPrimByte:
506 entrypoint = is_instance
507 ? (is_get ? kQuickGetByteInstance : kQuickSet8Instance)
508 : (is_get ? kQuickGetByteStatic : kQuickSet8Static);
509 break;
510 case Primitive::kPrimShort:
511 entrypoint = is_instance
512 ? (is_get ? kQuickGetShortInstance : kQuickSet16Instance)
513 : (is_get ? kQuickGetShortStatic : kQuickSet16Static);
514 break;
515 case Primitive::kPrimChar:
516 entrypoint = is_instance
517 ? (is_get ? kQuickGetCharInstance : kQuickSet16Instance)
518 : (is_get ? kQuickGetCharStatic : kQuickSet16Static);
519 break;
520 case Primitive::kPrimInt:
521 case Primitive::kPrimFloat:
522 entrypoint = is_instance
523 ? (is_get ? kQuickGet32Instance : kQuickSet32Instance)
524 : (is_get ? kQuickGet32Static : kQuickSet32Static);
525 break;
526 case Primitive::kPrimNot:
527 entrypoint = is_instance
528 ? (is_get ? kQuickGetObjInstance : kQuickSetObjInstance)
529 : (is_get ? kQuickGetObjStatic : kQuickSetObjStatic);
530 break;
531 case Primitive::kPrimLong:
532 case Primitive::kPrimDouble:
533 entrypoint = is_instance
534 ? (is_get ? kQuickGet64Instance : kQuickSet64Instance)
535 : (is_get ? kQuickGet64Static : kQuickSet64Static);
536 break;
537 default:
538 LOG(FATAL) << "Invalid type " << field_type;
539 }
540 InvokeRuntime(entrypoint, field_access, dex_pc, nullptr);
541
542 if (is_get && Primitive::IsFloatingPointType(field_type)) {
543 MoveLocation(locations->Out(), calling_convention.GetReturnLocation(field_type), field_type);
544 }
545}
546
Roland Levillain0d5a2812015-11-13 10:07:31 +0000547// TODO: Remove argument `code_generator_supports_read_barrier` when
548// all code generators have read barrier support.
Calin Juravle98893e12015-10-02 21:05:03 +0100549void CodeGenerator::CreateLoadClassLocationSummary(HLoadClass* cls,
550 Location runtime_type_index_location,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000551 Location runtime_return_location,
552 bool code_generator_supports_read_barrier) {
Calin Juravle98893e12015-10-02 21:05:03 +0100553 ArenaAllocator* allocator = cls->GetBlock()->GetGraph()->GetArena();
554 LocationSummary::CallKind call_kind = cls->NeedsAccessCheck()
555 ? LocationSummary::kCall
Roland Levillain0d5a2812015-11-13 10:07:31 +0000556 : (((code_generator_supports_read_barrier && kEmitCompilerReadBarrier) ||
557 cls->CanCallRuntime())
558 ? LocationSummary::kCallOnSlowPath
559 : LocationSummary::kNoCall);
Calin Juravle98893e12015-10-02 21:05:03 +0100560 LocationSummary* locations = new (allocator) LocationSummary(cls, call_kind);
Calin Juravle98893e12015-10-02 21:05:03 +0100561 if (cls->NeedsAccessCheck()) {
Calin Juravle580b6092015-10-06 17:35:58 +0100562 locations->SetInAt(0, Location::NoLocation());
Calin Juravle98893e12015-10-02 21:05:03 +0100563 locations->AddTemp(runtime_type_index_location);
564 locations->SetOut(runtime_return_location);
565 } else {
Calin Juravle580b6092015-10-06 17:35:58 +0100566 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle98893e12015-10-02 21:05:03 +0100567 locations->SetOut(Location::RequiresRegister());
568 }
569}
570
571
Mark Mendell5f874182015-03-04 15:42:45 -0500572void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
573 // The DCHECKS below check that a register is not specified twice in
574 // the summary. The out location can overlap with an input, so we need
575 // to special case it.
576 if (location.IsRegister()) {
577 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
578 blocked_core_registers_[location.reg()] = true;
579 } else if (location.IsFpuRegister()) {
580 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
581 blocked_fpu_registers_[location.reg()] = true;
582 } else if (location.IsFpuRegisterPair()) {
583 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
584 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
585 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
586 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
587 } else if (location.IsRegisterPair()) {
588 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
589 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
590 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
591 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
592 }
593}
594
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100595void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
596 LocationSummary* locations = instruction->GetLocations();
597 if (locations == nullptr) return;
598
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100599 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
600 blocked_core_registers_[i] = false;
601 }
602
603 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
604 blocked_fpu_registers_[i] = false;
605 }
606
607 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
608 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100609 }
610
611 // Mark all fixed input, temp and output registers as used.
612 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500613 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100614 }
615
616 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
617 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500618 BlockIfInRegister(loc);
619 }
620 Location result_location = locations->Out();
621 if (locations->OutputCanOverlapWithInputs()) {
622 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100623 }
624
Mark Mendell5f874182015-03-04 15:42:45 -0500625 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100626
627 // Allocate all unallocated input locations.
628 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
629 Location loc = locations->InAt(i);
630 HInstruction* input = instruction->InputAt(i);
631 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100632 if ((loc.GetPolicy() == Location::kRequiresRegister)
633 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100634 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100635 } else {
636 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
637 HLoadLocal* load = input->AsLoadLocal();
638 if (load != nullptr) {
639 loc = GetStackLocation(load);
640 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100641 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100642 }
643 }
644 locations->SetInAt(i, loc);
645 }
646 }
647
648 // Allocate all unallocated temp locations.
649 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
650 Location loc = locations->GetTemp(i);
651 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000652 switch (loc.GetPolicy()) {
653 case Location::kRequiresRegister:
654 // Allocate a core register (large enough to fit a 32-bit integer).
655 loc = AllocateFreeRegister(Primitive::kPrimInt);
656 break;
657
658 case Location::kRequiresFpuRegister:
659 // Allocate a core register (large enough to fit a 64-bit double).
660 loc = AllocateFreeRegister(Primitive::kPrimDouble);
661 break;
662
663 default:
664 LOG(FATAL) << "Unexpected policy for temporary location "
665 << loc.GetPolicy();
666 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100667 locations->SetTempAt(i, loc);
668 }
669 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100670 if (result_location.IsUnallocated()) {
671 switch (result_location.GetPolicy()) {
672 case Location::kAny:
673 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100674 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100675 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100676 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100677 case Location::kSameAsFirstInput:
678 result_location = locations->InAt(0);
679 break;
680 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000681 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100682 }
683}
684
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000685void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
686 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100687 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100688 if (instruction->IsTemporary()) {
689 HInstruction* previous = instruction->GetPrevious();
690 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
691 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100692 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100693 return;
694 }
695 AllocateRegistersLocally(instruction);
696 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000697 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000698 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000699 if (location.IsValid()) {
700 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000701 if (input->GetNext()->IsTemporary()) {
702 // If the input was stored in a temporary, use that temporary to
703 // perform the move.
704 Move(input->GetNext(), location, instruction);
705 } else {
706 Move(input, location, instruction);
707 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000708 }
709 }
710}
711
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000712void CodeGenerator::AllocateLocations(HInstruction* instruction) {
713 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100714 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000715 LocationSummary* locations = instruction->GetLocations();
716 if (!instruction->IsSuspendCheckEntry()) {
717 if (locations != nullptr && locations->CanCall()) {
718 MarkNotLeaf();
719 }
720 if (instruction->NeedsCurrentMethod()) {
721 SetRequiresCurrentMethod();
722 }
723 }
724}
725
Serban Constantinescuecc43662015-08-13 13:33:12 +0100726void CodeGenerator::MaybeRecordStat(MethodCompilationStat compilation_stat, size_t count) const {
727 if (stats_ != nullptr) {
728 stats_->RecordStat(compilation_stat, count);
729 }
730}
731
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000732CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000733 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000734 const InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100735 const CompilerOptions& compiler_options,
736 OptimizingCompilerStats* stats) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000737 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -0700738#ifdef ART_ENABLE_CODEGEN_arm
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000739 case kArm:
740 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000741 return new arm::CodeGeneratorARM(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100742 *isa_features.AsArmInstructionSetFeatures(),
743 compiler_options,
744 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000745 }
Alex Light50fa9932015-08-10 15:30:07 -0700746#endif
747#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames5319def2014-10-23 10:03:10 +0100748 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000749 return new arm64::CodeGeneratorARM64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100750 *isa_features.AsArm64InstructionSetFeatures(),
751 compiler_options,
752 stats);
Alexandre Rames5319def2014-10-23 10:03:10 +0100753 }
Alex Light50fa9932015-08-10 15:30:07 -0700754#endif
755#ifdef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200756 case kMips: {
757 return new mips::CodeGeneratorMIPS(graph,
758 *isa_features.AsMipsInstructionSetFeatures(),
759 compiler_options,
760 stats);
761 }
Alex Light50fa9932015-08-10 15:30:07 -0700762#endif
763#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700764 case kMips64: {
765 return new mips64::CodeGeneratorMIPS64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100766 *isa_features.AsMips64InstructionSetFeatures(),
767 compiler_options,
768 stats);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700769 }
Alex Light50fa9932015-08-10 15:30:07 -0700770#endif
771#ifdef ART_ENABLE_CODEGEN_x86
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000772 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400773 return new x86::CodeGeneratorX86(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100774 *isa_features.AsX86InstructionSetFeatures(),
775 compiler_options,
776 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000777 }
Alex Light50fa9932015-08-10 15:30:07 -0700778#endif
779#ifdef ART_ENABLE_CODEGEN_x86_64
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700780 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400781 return new x86_64::CodeGeneratorX86_64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100782 *isa_features.AsX86_64InstructionSetFeatures(),
783 compiler_options,
784 stats);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700785 }
Alex Light50fa9932015-08-10 15:30:07 -0700786#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000787 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000788 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000789 }
790}
791
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000792void CodeGenerator::BuildNativeGCMap(
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000793 ArenaVector<uint8_t>* data, const CompilerDriver& compiler_driver) const {
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000794 const std::vector<uint8_t>& gc_map_raw =
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000795 compiler_driver.GetVerifiedMethod(&GetGraph()->GetDexFile(), GetGraph()->GetMethodIdx())
796 ->GetDexGcMap();
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000797 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
798
Vladimir Markobd8c7252015-06-12 10:06:32 +0100799 uint32_t max_native_offset = stack_map_stream_.ComputeMaxNativePcOffset();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000800
Vladimir Markobd8c7252015-06-12 10:06:32 +0100801 size_t num_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
802 GcMapBuilder builder(data, num_stack_maps, max_native_offset, dex_gc_map.RegWidth());
803 for (size_t i = 0; i != num_stack_maps; ++i) {
804 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
805 uint32_t native_offset = stack_map_entry.native_pc_offset;
806 uint32_t dex_pc = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000807 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800808 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000809 builder.AddEntry(native_offset, references);
810 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000811}
812
Vladimir Markof9f64412015-09-02 14:05:49 +0100813void CodeGenerator::BuildMappingTable(ArenaVector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000814 uint32_t pc2dex_data_size = 0u;
Vladimir Markobd8c7252015-06-12 10:06:32 +0100815 uint32_t pc2dex_entries = stack_map_stream_.GetNumberOfStackMaps();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000816 uint32_t pc2dex_offset = 0u;
817 int32_t pc2dex_dalvik_offset = 0;
818 uint32_t dex2pc_data_size = 0u;
819 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000820 uint32_t dex2pc_offset = 0u;
821 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000822
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000823 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100824 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
825 pc2dex_data_size += UnsignedLeb128Size(stack_map_entry.native_pc_offset - pc2dex_offset);
826 pc2dex_data_size += SignedLeb128Size(stack_map_entry.dex_pc - pc2dex_dalvik_offset);
827 pc2dex_offset = stack_map_entry.native_pc_offset;
828 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000829 }
830
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000831 // Walk over the blocks and find which ones correspond to catch block entries.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100832 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000833 if (block->IsCatchBlock()) {
834 intptr_t native_pc = GetAddressOf(block);
835 ++dex2pc_entries;
836 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
837 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
838 dex2pc_offset = native_pc;
839 dex2pc_dalvik_offset = block->GetDexPc();
840 }
841 }
842
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000843 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
844 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
845 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
846 data->resize(data_size);
847
848 uint8_t* data_ptr = &(*data)[0];
849 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000850
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000851 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
852 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
853 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
854 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
855
856 pc2dex_offset = 0u;
857 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000858 dex2pc_offset = 0u;
859 dex2pc_dalvik_offset = 0u;
860
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000861 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100862 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
863 DCHECK(pc2dex_offset <= stack_map_entry.native_pc_offset);
864 write_pos = EncodeUnsignedLeb128(write_pos, stack_map_entry.native_pc_offset - pc2dex_offset);
865 write_pos = EncodeSignedLeb128(write_pos, stack_map_entry.dex_pc - pc2dex_dalvik_offset);
866 pc2dex_offset = stack_map_entry.native_pc_offset;
867 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000868 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000869
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100870 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000871 if (block->IsCatchBlock()) {
872 intptr_t native_pc = GetAddressOf(block);
873 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
874 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
875 dex2pc_offset = native_pc;
876 dex2pc_dalvik_offset = block->GetDexPc();
877 }
878 }
879
880
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000881 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
882 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
883
884 if (kIsDebugBuild) {
885 // Verify the encoded table holds the expected data.
886 MappingTable table(data_ptr);
887 CHECK_EQ(table.TotalSize(), total_entries);
888 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
889 auto it = table.PcToDexBegin();
890 auto it2 = table.DexToPcBegin();
891 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100892 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
893 CHECK_EQ(stack_map_entry.native_pc_offset, it.NativePcOffset());
894 CHECK_EQ(stack_map_entry.dex_pc, it.DexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000895 ++it;
896 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100897 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000898 if (block->IsCatchBlock()) {
899 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
900 CHECK_EQ(block->GetDexPc(), it2.DexPc());
901 ++it2;
902 }
903 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000904 CHECK(it == table.PcToDexEnd());
905 CHECK(it2 == table.DexToPcEnd());
906 }
907}
908
Vladimir Markof9f64412015-09-02 14:05:49 +0100909void CodeGenerator::BuildVMapTable(ArenaVector<uint8_t>* data) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100910 Leb128Encoder<ArenaVector<uint8_t>> vmap_encoder(data);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100911 // We currently don't use callee-saved registers.
912 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000913 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
914 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000915 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000916}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000917
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000918size_t CodeGenerator::ComputeStackMapsSize() {
919 return stack_map_stream_.PrepareForFillIn();
920}
921
922void CodeGenerator::BuildStackMaps(MemoryRegion region) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100923 stack_map_stream_.FillIn(region);
924}
925
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000926void CodeGenerator::RecordPcInfo(HInstruction* instruction,
927 uint32_t dex_pc,
928 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000929 if (instruction != nullptr) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700930 // The code generated for some type conversions and comparisons
931 // may call the runtime, thus normally requiring a subsequent
932 // call to this method. However, the method verifier does not
933 // produce PC information for certain instructions, which are
934 // considered "atomic" (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000935 // Therefore we do not currently record PC information for such
936 // instructions. As this may change later, we added this special
937 // case so that code generators may nevertheless call
938 // CodeGenerator::RecordPcInfo without triggering an error in
939 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
940 // thereafter.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700941 if (instruction->IsTypeConversion() || instruction->IsCompare()) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000942 return;
943 }
944 if (instruction->IsRem()) {
945 Primitive::Type type = instruction->AsRem()->GetResultType();
946 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
947 return;
948 }
949 }
Roland Levillain624279f2014-12-04 11:54:28 +0000950 }
951
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100952 uint32_t outer_dex_pc = dex_pc;
953 uint32_t outer_environment_size = 0;
954 uint32_t inlining_depth = 0;
955 if (instruction != nullptr) {
956 for (HEnvironment* environment = instruction->GetEnvironment();
957 environment != nullptr;
958 environment = environment->GetParent()) {
959 outer_dex_pc = environment->GetDexPc();
960 outer_environment_size = environment->Size();
961 if (environment != instruction->GetEnvironment()) {
962 inlining_depth++;
963 }
964 }
965 }
966
Nicolas Geoffray39468442014-09-02 15:17:15 +0100967 // Collect PC infos for the mapping table.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100968 uint32_t native_pc = GetAssembler()->CodeSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100969
Nicolas Geoffray39468442014-09-02 15:17:15 +0100970 if (instruction == nullptr) {
971 // For stack overflow checks.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100972 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100973 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000974 return;
975 }
976 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100977
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000978 uint32_t register_mask = locations->GetRegisterMask();
979 if (locations->OnlyCallsOnSlowPath()) {
980 // In case of slow path, we currently set the location of caller-save registers
981 // to register (instead of their stack location when pushed before the slow-path
982 // call). Therefore register_mask contains both callee-save and caller-save
983 // registers that hold objects. We must remove the caller-save from the mask, since
984 // they will be overwritten by the callee.
985 register_mask &= core_callee_save_mask_;
986 }
987 // The register mask must be a subset of callee-save registers.
988 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100989 stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
990 native_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100991 register_mask,
992 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100993 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100994 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100995
996 EmitEnvironment(instruction->GetEnvironment(), slow_path);
997 stack_map_stream_.EndStackMapEntry();
998}
999
David Srbeckyb7070a22016-01-08 18:13:53 +00001000bool CodeGenerator::HasStackMapAtCurrentPc() {
1001 uint32_t pc = GetAssembler()->CodeSize();
1002 size_t count = stack_map_stream_.GetNumberOfStackMaps();
1003 return count > 0 && stack_map_stream_.GetStackMap(count - 1).native_pc_offset == pc;
1004}
1005
David Brazdil77a48ae2015-09-15 12:34:04 +00001006void CodeGenerator::RecordCatchBlockInfo() {
1007 ArenaAllocator* arena = graph_->GetArena();
1008
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001009 for (HBasicBlock* block : *block_order_) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001010 if (!block->IsCatchBlock()) {
1011 continue;
1012 }
1013
1014 uint32_t dex_pc = block->GetDexPc();
1015 uint32_t num_vregs = graph_->GetNumberOfVRegs();
1016 uint32_t inlining_depth = 0; // Inlining of catch blocks is not supported at the moment.
1017 uint32_t native_pc = GetAddressOf(block);
1018 uint32_t register_mask = 0; // Not used.
1019
1020 // The stack mask is not used, so we leave it empty.
1021 ArenaBitVector* stack_mask = new (arena) ArenaBitVector(arena, 0, /* expandable */ true);
1022
1023 stack_map_stream_.BeginStackMapEntry(dex_pc,
1024 native_pc,
1025 register_mask,
1026 stack_mask,
1027 num_vregs,
1028 inlining_depth);
1029
1030 HInstruction* current_phi = block->GetFirstPhi();
1031 for (size_t vreg = 0; vreg < num_vregs; ++vreg) {
1032 while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) {
1033 HInstruction* next_phi = current_phi->GetNext();
1034 DCHECK(next_phi == nullptr ||
1035 current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber())
1036 << "Phis need to be sorted by vreg number to keep this a linear-time loop.";
1037 current_phi = next_phi;
1038 }
1039
1040 if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) {
1041 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
1042 } else {
1043 Location location = current_phi->GetLiveInterval()->ToLocation();
1044 switch (location.GetKind()) {
1045 case Location::kStackSlot: {
1046 stack_map_stream_.AddDexRegisterEntry(
1047 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
1048 break;
1049 }
1050 case Location::kDoubleStackSlot: {
1051 stack_map_stream_.AddDexRegisterEntry(
1052 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
1053 stack_map_stream_.AddDexRegisterEntry(
1054 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
1055 ++vreg;
1056 DCHECK_LT(vreg, num_vregs);
1057 break;
1058 }
1059 default: {
1060 // All catch phis must be allocated to a stack slot.
1061 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1062 UNREACHABLE();
1063 }
1064 }
1065 }
1066 }
1067
1068 stack_map_stream_.EndStackMapEntry();
1069 }
1070}
1071
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001072void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
1073 if (environment == nullptr) return;
1074
1075 if (environment->GetParent() != nullptr) {
1076 // We emit the parent environment first.
1077 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001078 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
1079 environment->GetDexPc(),
1080 environment->GetInvokeType(),
1081 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001082 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001083
1084 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001085 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001086 HInstruction* current = environment->GetInstructionAt(i);
1087 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001088 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001089 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001090 }
1091
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001092 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001093 switch (location.GetKind()) {
1094 case Location::kConstant: {
1095 DCHECK_EQ(current, location.GetConstant());
1096 if (current->IsLongConstant()) {
1097 int64_t value = current->AsLongConstant()->GetValue();
1098 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001099 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001100 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001101 DexRegisterLocation::Kind::kConstant, High32Bits(value));
1102 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001103 DCHECK_LT(i, environment_size);
1104 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001105 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001106 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001107 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001108 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001109 DexRegisterLocation::Kind::kConstant, High32Bits(value));
1110 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001111 DCHECK_LT(i, environment_size);
1112 } else if (current->IsIntConstant()) {
1113 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001114 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001115 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001116 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001117 } else {
1118 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +00001119 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001120 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001121 }
1122 break;
1123 }
1124
1125 case Location::kStackSlot: {
1126 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001127 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001128 break;
1129 }
1130
1131 case Location::kDoubleStackSlot: {
1132 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001133 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001134 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001135 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
1136 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001137 DCHECK_LT(i, environment_size);
1138 break;
1139 }
1140
1141 case Location::kRegister : {
1142 int id = location.reg();
1143 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
1144 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001145 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001146 if (current->GetType() == Primitive::kPrimLong) {
1147 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001148 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
1149 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001150 DCHECK_LT(i, environment_size);
1151 }
1152 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001153 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001154 if (current->GetType() == Primitive::kPrimLong) {
David Brazdild9cb68e2015-08-25 13:52:43 +01001155 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001156 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001157 DCHECK_LT(i, environment_size);
1158 }
1159 }
1160 break;
1161 }
1162
1163 case Location::kFpuRegister : {
1164 int id = location.reg();
1165 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
1166 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001167 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001168 if (current->GetType() == Primitive::kPrimDouble) {
1169 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001170 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
1171 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001172 DCHECK_LT(i, environment_size);
1173 }
1174 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001175 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001176 if (current->GetType() == Primitive::kPrimDouble) {
David Brazdild9cb68e2015-08-25 13:52:43 +01001177 stack_map_stream_.AddDexRegisterEntry(
1178 DexRegisterLocation::Kind::kInFpuRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001179 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001180 DCHECK_LT(i, environment_size);
1181 }
1182 }
1183 break;
1184 }
1185
1186 case Location::kFpuRegisterPair : {
1187 int low = location.low();
1188 int high = location.high();
1189 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
1190 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001191 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001192 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001193 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001194 }
1195 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
1196 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001197 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1198 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001199 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001200 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
1201 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001202 }
1203 DCHECK_LT(i, environment_size);
1204 break;
1205 }
1206
1207 case Location::kRegisterPair : {
1208 int low = location.low();
1209 int high = location.high();
1210 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
1211 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001212 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001213 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001214 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001215 }
1216 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
1217 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001218 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001219 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001220 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001221 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001222 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001223 DCHECK_LT(i, environment_size);
1224 break;
1225 }
1226
1227 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001228 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001229 break;
1230 }
1231
1232 default:
1233 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1234 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001235 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001236
1237 if (environment->GetParent() != nullptr) {
1238 stack_map_stream_.EndInlineInfoEntry();
1239 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001240}
1241
David Brazdil77a48ae2015-09-15 12:34:04 +00001242bool CodeGenerator::IsImplicitNullCheckAllowed(HNullCheck* null_check) const {
1243 return compiler_options_.GetImplicitNullChecks() &&
1244 // Null checks which might throw into a catch block need to save live
1245 // registers and therefore cannot be done implicitly.
1246 !null_check->CanThrowIntoCatchBlock();
1247}
1248
Calin Juravle77520bc2015-01-12 18:45:46 +00001249bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
1250 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +01001251
1252 return (first_next_not_move != nullptr)
1253 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00001254}
1255
1256void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
1257 // If we are from a static path don't record the pc as we can't throw NPE.
1258 // NB: having the checks here makes the code much less verbose in the arch
1259 // specific code generators.
1260 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
1261 return;
1262 }
1263
Calin Juravle641547a2015-04-21 22:08:51 +01001264 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001265 return;
1266 }
1267
1268 // Find the first previous instruction which is not a move.
1269 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
1270
1271 // If the instruction is a null check it means that `instr` is the first user
1272 // and needs to record the pc.
1273 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
1274 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
David Brazdil77a48ae2015-09-15 12:34:04 +00001275 if (IsImplicitNullCheckAllowed(null_check)) {
1276 // TODO: The parallel moves modify the environment. Their changes need to be
1277 // reverted otherwise the stack maps at the throw point will not be correct.
1278 RecordPcInfo(null_check, null_check->GetDexPc());
1279 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001280 }
1281}
1282
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001283void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
1284 LocationSummary* locations = suspend_check->GetLocations();
1285 HBasicBlock* block = suspend_check->GetBlock();
1286 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
1287 DCHECK(block->IsLoopHeader());
1288
1289 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1290 HInstruction* current = it.Current();
1291 LiveInterval* interval = current->GetLiveInterval();
1292 // We only need to clear bits of loop phis containing objects and allocated in register.
1293 // Loop phis allocated on stack already have the object in the stack.
1294 if (current->GetType() == Primitive::kPrimNot
1295 && interval->HasRegister()
1296 && interval->HasSpillSlot()) {
1297 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
1298 }
1299 }
1300}
1301
Nicolas Geoffray90218252015-04-15 11:56:51 +01001302void CodeGenerator::EmitParallelMoves(Location from1,
1303 Location to1,
1304 Primitive::Type type1,
1305 Location from2,
1306 Location to2,
1307 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001308 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +01001309 parallel_move.AddMove(from1, to1, type1, nullptr);
1310 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001311 GetMoveResolver()->EmitNativeCode(&parallel_move);
1312}
1313
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001314void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) {
1315 // Ensure that the call kind indication given to the register allocator is
1316 // coherent with the runtime call generated, and that the GC side effect is
1317 // set when required.
1318 if (slow_path == nullptr) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001319 DCHECK(instruction->GetLocations()->WillCall())
1320 << "instruction->DebugName()=" << instruction->DebugName();
Roland Levillaindf3f8222015-08-13 12:31:44 +01001321 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()))
Roland Levillain0d5a2812015-11-13 10:07:31 +00001322 << "instruction->DebugName()=" << instruction->DebugName()
1323 << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001324 } else {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001325 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal())
Roland Levillain0d5a2812015-11-13 10:07:31 +00001326 << "instruction->DebugName()=" << instruction->DebugName()
1327 << " slow_path->GetDescription()=" << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001328 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) ||
Roland Levillain0d5a2812015-11-13 10:07:31 +00001329 // When read barriers are enabled, some instructions use a
1330 // slow path to emit a read barrier, which does not trigger
1331 // GC, is not fatal, nor is emitted by HDeoptimize
1332 // instructions.
1333 (kEmitCompilerReadBarrier &&
1334 (instruction->IsInstanceFieldGet() ||
1335 instruction->IsStaticFieldGet() ||
1336 instruction->IsArraySet() ||
1337 instruction->IsArrayGet() ||
1338 instruction->IsLoadClass() ||
1339 instruction->IsLoadString() ||
1340 instruction->IsInstanceOf() ||
1341 instruction->IsCheckCast())))
1342 << "instruction->DebugName()=" << instruction->DebugName()
1343 << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString()
1344 << " slow_path->GetDescription()=" << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001345 }
1346
1347 // Check the coherency of leaf information.
1348 DCHECK(instruction->IsSuspendCheck()
1349 || ((slow_path != nullptr) && slow_path->IsFatal())
1350 || instruction->GetLocations()->CanCall()
Roland Levillaindf3f8222015-08-13 12:31:44 +01001351 || !IsLeafMethod())
1352 << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : "");
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001353}
1354
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001355void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001356 RegisterSet* live_registers = locations->GetLiveRegisters();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001357 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001358
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001359 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1360 if (!codegen->IsCoreCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001361 if (live_registers->ContainsCoreRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001362 // If the register holds an object, update the stack mask.
1363 if (locations->RegisterContainsObject(i)) {
1364 locations->SetStackBit(stack_offset / kVRegSize);
1365 }
1366 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001367 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1368 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001369 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
1370 }
1371 }
1372 }
1373
1374 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1375 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001376 if (live_registers->ContainsFloatingPointRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001377 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001378 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1379 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001380 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
1381 }
1382 }
1383 }
1384}
1385
1386void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001387 RegisterSet* live_registers = locations->GetLiveRegisters();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001388 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001389
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001390 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1391 if (!codegen->IsCoreCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001392 if (live_registers->ContainsCoreRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001393 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Roland Levillain0d5a2812015-11-13 10:07:31 +00001394 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001395 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1396 }
1397 }
1398 }
1399
1400 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1401 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001402 if (live_registers->ContainsFloatingPointRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001403 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Roland Levillain0d5a2812015-11-13 10:07:31 +00001404 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001405 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1406 }
1407 }
1408 }
1409}
1410
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001411void CodeGenerator::CreateSystemArrayCopyLocationSummary(HInvoke* invoke) {
1412 // Check to see if we have known failures that will cause us to have to bail out
1413 // to the runtime, and just generate the runtime call directly.
1414 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1415 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
1416
1417 // The positions must be non-negative.
1418 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
1419 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
1420 // We will have to fail anyways.
1421 return;
1422 }
1423
1424 // The length must be >= 0.
1425 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1426 if (length != nullptr) {
1427 int32_t len = length->GetValue();
1428 if (len < 0) {
1429 // Just call as normal.
1430 return;
1431 }
1432 }
1433
1434 SystemArrayCopyOptimizations optimizations(invoke);
1435
1436 if (optimizations.GetDestinationIsSource()) {
1437 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
1438 // We only support backward copying if source and destination are the same.
1439 return;
1440 }
1441 }
1442
1443 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
1444 // We currently don't intrinsify primitive copying.
1445 return;
1446 }
1447
1448 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
1449 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1450 LocationSummary::kCallOnSlowPath,
1451 kIntrinsified);
1452 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
1453 locations->SetInAt(0, Location::RequiresRegister());
1454 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1455 locations->SetInAt(2, Location::RequiresRegister());
1456 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
1457 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
1458
1459 locations->AddTemp(Location::RequiresRegister());
1460 locations->AddTemp(Location::RequiresRegister());
1461 locations->AddTemp(Location::RequiresRegister());
1462}
1463
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001464} // namespace art