blob: e1b83f05d6e3e04bce5abf620c6b8f6e881fc9cc [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 Geoffraydc23d832015-02-16 11:15:43 +0000145bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100146 DCHECK_EQ((*block_order_)[current_block_index_], current);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000147 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
148}
149
150HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100151 for (size_t i = current_block_index_ + 1; i < block_order_->size(); ++i) {
152 HBasicBlock* block = (*block_order_)[i];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000153 if (!block->IsSingleJump()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000154 return block;
155 }
156 }
157 return nullptr;
158}
159
160HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000161 while (block->IsSingleJump()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100162 block = block->GetSuccessors()[0];
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000163 }
164 return block;
165}
166
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100167class DisassemblyScope {
168 public:
169 DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen)
170 : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) {
171 if (codegen_.GetDisassemblyInformation() != nullptr) {
172 start_offset_ = codegen_.GetAssembler().CodeSize();
173 }
174 }
175
176 ~DisassemblyScope() {
177 // We avoid building this data when we know it will not be used.
178 if (codegen_.GetDisassemblyInformation() != nullptr) {
179 codegen_.GetDisassemblyInformation()->AddInstructionInterval(
180 instruction_, start_offset_, codegen_.GetAssembler().CodeSize());
181 }
182 }
183
184 private:
185 const CodeGenerator& codegen_;
186 HInstruction* instruction_;
187 size_t start_offset_;
188};
189
190
191void CodeGenerator::GenerateSlowPaths() {
192 size_t code_start = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100193 for (SlowPathCode* slow_path : slow_paths_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000194 current_slow_path_ = slow_path;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100195 if (disasm_info_ != nullptr) {
196 code_start = GetAssembler()->CodeSize();
197 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100198 slow_path->EmitNativeCode(this);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100199 if (disasm_info_ != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100200 disasm_info_->AddSlowPathInterval(slow_path, code_start, GetAssembler()->CodeSize());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100201 }
202 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000203 current_slow_path_ = nullptr;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100204}
205
David Brazdil58282f42016-01-14 12:45:10 +0000206void CodeGenerator::Compile(CodeAllocator* allocator) {
207 // The register allocator already called `InitializeCodeGeneration`,
208 // where the frame size has been computed.
209 DCHECK(block_order_ != nullptr);
210 Initialize();
211
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100212 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000213 DCHECK_EQ(current_block_index_, 0u);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100214
215 size_t frame_start = GetAssembler()->CodeSize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000216 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100217 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100218 if (disasm_info_ != nullptr) {
219 disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize());
220 }
221
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100222 for (size_t e = block_order_->size(); current_block_index_ < e; ++current_block_index_) {
223 HBasicBlock* block = (*block_order_)[current_block_index_];
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000224 // Don't generate code for an empty block. Its predecessors will branch to its successor
225 // directly. Also, the label of that block will not be emitted, so this helps catch
226 // errors where we reference that label.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000227 if (block->IsSingleJump()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100228 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100229 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
230 HInstruction* current = it.Current();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100231 DisassemblyScope disassembly_scope(current, *this);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100232 DCHECK(CheckTypeConsistency(current));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100233 current->Accept(instruction_visitor);
234 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000235 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000236
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100237 GenerateSlowPaths();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000238
David Brazdil77a48ae2015-09-15 12:34:04 +0000239 // Emit catch stack maps at the end of the stack map stream as expected by the
240 // runtime exception handler.
David Brazdil58282f42016-01-14 12:45:10 +0000241 if (graph_->HasTryCatch()) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000242 RecordCatchBlockInfo();
243 }
244
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000245 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000246 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000247}
248
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000249void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100250 size_t code_size = GetAssembler()->CodeSize();
251 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000252
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100253 MemoryRegion code(buffer, code_size);
254 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000255}
256
Vladimir Marko58155012015-08-19 12:49:41 +0000257void CodeGenerator::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches ATTRIBUTE_UNUSED) {
258 // No linker patches by default.
259}
260
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000261void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
262 size_t maximum_number_of_live_core_registers,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000263 size_t maximum_number_of_live_fpu_registers,
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000264 size_t number_of_out_slots,
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100265 const ArenaVector<HBasicBlock*>& block_order) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000266 block_order_ = &block_order;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100267 DCHECK(!block_order.empty());
268 DCHECK(block_order[0] == GetGraph()->GetEntryBlock());
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000269 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100270 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
271
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000272 if (number_of_spill_slots == 0
273 && !HasAllocatedCalleeSaveRegisters()
274 && IsLeafMethod()
275 && !RequiresCurrentMethod()) {
276 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000277 DCHECK_EQ(maximum_number_of_live_fpu_registers, 0u);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000278 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
279 } else {
280 SetFrameSize(RoundUp(
281 number_of_spill_slots * kVRegSize
282 + number_of_out_slots * kVRegSize
283 + maximum_number_of_live_core_registers * GetWordSize()
Roland Levillain0d5a2812015-11-13 10:07:31 +0000284 + maximum_number_of_live_fpu_registers * GetFloatingPointSpillSlotSize()
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000285 + FrameEntrySpillSize(),
286 kStackAlignment));
287 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100288}
289
290Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
291 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000292 // The type of the previous instruction tells us if we need a single or double stack slot.
293 Primitive::Type type = temp->GetType();
294 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100295 // Use the temporary region (right below the dex registers).
296 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
297 - kVRegSize // filler
298 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000299 - ((temp_size + temp->GetIndex()) * kVRegSize);
300 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100301}
302
303int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
304 uint16_t reg_number = local->GetRegNumber();
305 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
306 if (reg_number >= number_of_locals) {
307 // Local is a parameter of the method. It is stored in the caller's frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode.
309 return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100310 + (reg_number - number_of_locals) * kVRegSize;
311 } else {
312 // Local is a temporary in this method. It is stored in this method's frame.
313 return GetFrameSize() - FrameEntrySpillSize()
314 - kVRegSize // filler.
315 - (number_of_locals * kVRegSize)
316 + (reg_number * kVRegSize);
317 }
318}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100319
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100320void CodeGenerator::CreateCommonInvokeLocationSummary(
Nicolas Geoffray4e40c262015-06-03 12:02:38 +0100321 HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100322 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
323 LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100324
325 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
326 HInstruction* input = invoke->InputAt(i);
327 locations->SetInAt(i, visitor->GetNextLocation(input->GetType()));
328 }
329
330 locations->SetOut(visitor->GetReturnLocation(invoke->GetType()));
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100331
332 if (invoke->IsInvokeStaticOrDirect()) {
333 HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect();
Vladimir Markodc151b22015-10-15 18:02:30 +0100334 switch (call->GetMethodLoadKind()) {
335 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000336 locations->SetInAt(call->GetSpecialInputIndex(), visitor->GetMethodLocation());
Vladimir Markodc151b22015-10-15 18:02:30 +0100337 break;
338 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod:
339 locations->AddTemp(visitor->GetMethodLocation());
Vladimir Markoc53c0792015-11-19 15:48:33 +0000340 locations->SetInAt(call->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Markodc151b22015-10-15 18:02:30 +0100341 break;
342 default:
343 locations->AddTemp(visitor->GetMethodLocation());
344 break;
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100345 }
346 } else {
347 locations->AddTemp(visitor->GetMethodLocation());
348 }
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100349}
350
Calin Juravle175dc732015-08-25 15:42:32 +0100351void CodeGenerator::GenerateInvokeUnresolvedRuntimeCall(HInvokeUnresolved* invoke) {
352 MoveConstant(invoke->GetLocations()->GetTemp(0), invoke->GetDexMethodIndex());
353
354 // Initialize to anything to silent compiler warnings.
355 QuickEntrypointEnum entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
356 switch (invoke->GetOriginalInvokeType()) {
357 case kStatic:
358 entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
359 break;
360 case kDirect:
361 entrypoint = kQuickInvokeDirectTrampolineWithAccessCheck;
362 break;
363 case kVirtual:
364 entrypoint = kQuickInvokeVirtualTrampolineWithAccessCheck;
365 break;
366 case kSuper:
367 entrypoint = kQuickInvokeSuperTrampolineWithAccessCheck;
368 break;
369 case kInterface:
370 entrypoint = kQuickInvokeInterfaceTrampolineWithAccessCheck;
371 break;
372 }
373 InvokeRuntime(entrypoint, invoke, invoke->GetDexPc(), nullptr);
374}
375
Calin Juravlee460d1d2015-09-29 04:52:17 +0100376void CodeGenerator::CreateUnresolvedFieldLocationSummary(
377 HInstruction* field_access,
378 Primitive::Type field_type,
379 const FieldAccessCallingConvention& calling_convention) {
380 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
381 || field_access->IsUnresolvedInstanceFieldSet();
382 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
383 || field_access->IsUnresolvedStaticFieldGet();
384
385 ArenaAllocator* allocator = field_access->GetBlock()->GetGraph()->GetArena();
386 LocationSummary* locations =
387 new (allocator) LocationSummary(field_access, LocationSummary::kCall);
388
389 locations->AddTemp(calling_convention.GetFieldIndexLocation());
390
391 if (is_instance) {
392 // Add the `this` object for instance field accesses.
393 locations->SetInAt(0, calling_convention.GetObjectLocation());
394 }
395
396 // Note that pSetXXStatic/pGetXXStatic always takes/returns an int or int64
397 // regardless of the the type. Because of that we forced to special case
398 // the access to floating point values.
399 if (is_get) {
400 if (Primitive::IsFloatingPointType(field_type)) {
401 // The return value will be stored in regular registers while register
402 // allocator expects it in a floating point register.
403 // Note We don't need to request additional temps because the return
404 // register(s) are already blocked due the call and they may overlap with
405 // the input or field index.
406 // The transfer between the two will be done at codegen level.
407 locations->SetOut(calling_convention.GetFpuLocation(field_type));
408 } else {
409 locations->SetOut(calling_convention.GetReturnLocation(field_type));
410 }
411 } else {
412 size_t set_index = is_instance ? 1 : 0;
413 if (Primitive::IsFloatingPointType(field_type)) {
414 // The set value comes from a float location while the calling convention
415 // expects it in a regular register location. Allocate a temp for it and
416 // make the transfer at codegen.
417 AddLocationAsTemp(calling_convention.GetSetValueLocation(field_type, is_instance), locations);
418 locations->SetInAt(set_index, calling_convention.GetFpuLocation(field_type));
419 } else {
420 locations->SetInAt(set_index,
421 calling_convention.GetSetValueLocation(field_type, is_instance));
422 }
423 }
424}
425
426void CodeGenerator::GenerateUnresolvedFieldAccess(
427 HInstruction* field_access,
428 Primitive::Type field_type,
429 uint32_t field_index,
430 uint32_t dex_pc,
431 const FieldAccessCallingConvention& calling_convention) {
432 LocationSummary* locations = field_access->GetLocations();
433
434 MoveConstant(locations->GetTemp(0), field_index);
435
436 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
437 || field_access->IsUnresolvedInstanceFieldSet();
438 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
439 || field_access->IsUnresolvedStaticFieldGet();
440
441 if (!is_get && Primitive::IsFloatingPointType(field_type)) {
442 // Copy the float value to be set into the calling convention register.
443 // Note that using directly the temp location is problematic as we don't
444 // support temp register pairs. To avoid boilerplate conversion code, use
445 // the location from the calling convention.
446 MoveLocation(calling_convention.GetSetValueLocation(field_type, is_instance),
447 locations->InAt(is_instance ? 1 : 0),
448 (Primitive::Is64BitType(field_type) ? Primitive::kPrimLong : Primitive::kPrimInt));
449 }
450
451 QuickEntrypointEnum entrypoint = kQuickSet8Static; // Initialize to anything to avoid warnings.
452 switch (field_type) {
453 case Primitive::kPrimBoolean:
454 entrypoint = is_instance
455 ? (is_get ? kQuickGetBooleanInstance : kQuickSet8Instance)
456 : (is_get ? kQuickGetBooleanStatic : kQuickSet8Static);
457 break;
458 case Primitive::kPrimByte:
459 entrypoint = is_instance
460 ? (is_get ? kQuickGetByteInstance : kQuickSet8Instance)
461 : (is_get ? kQuickGetByteStatic : kQuickSet8Static);
462 break;
463 case Primitive::kPrimShort:
464 entrypoint = is_instance
465 ? (is_get ? kQuickGetShortInstance : kQuickSet16Instance)
466 : (is_get ? kQuickGetShortStatic : kQuickSet16Static);
467 break;
468 case Primitive::kPrimChar:
469 entrypoint = is_instance
470 ? (is_get ? kQuickGetCharInstance : kQuickSet16Instance)
471 : (is_get ? kQuickGetCharStatic : kQuickSet16Static);
472 break;
473 case Primitive::kPrimInt:
474 case Primitive::kPrimFloat:
475 entrypoint = is_instance
476 ? (is_get ? kQuickGet32Instance : kQuickSet32Instance)
477 : (is_get ? kQuickGet32Static : kQuickSet32Static);
478 break;
479 case Primitive::kPrimNot:
480 entrypoint = is_instance
481 ? (is_get ? kQuickGetObjInstance : kQuickSetObjInstance)
482 : (is_get ? kQuickGetObjStatic : kQuickSetObjStatic);
483 break;
484 case Primitive::kPrimLong:
485 case Primitive::kPrimDouble:
486 entrypoint = is_instance
487 ? (is_get ? kQuickGet64Instance : kQuickSet64Instance)
488 : (is_get ? kQuickGet64Static : kQuickSet64Static);
489 break;
490 default:
491 LOG(FATAL) << "Invalid type " << field_type;
492 }
493 InvokeRuntime(entrypoint, field_access, dex_pc, nullptr);
494
495 if (is_get && Primitive::IsFloatingPointType(field_type)) {
496 MoveLocation(locations->Out(), calling_convention.GetReturnLocation(field_type), field_type);
497 }
498}
499
Roland Levillain0d5a2812015-11-13 10:07:31 +0000500// TODO: Remove argument `code_generator_supports_read_barrier` when
501// all code generators have read barrier support.
Calin Juravle98893e12015-10-02 21:05:03 +0100502void CodeGenerator::CreateLoadClassLocationSummary(HLoadClass* cls,
503 Location runtime_type_index_location,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000504 Location runtime_return_location,
505 bool code_generator_supports_read_barrier) {
Calin Juravle98893e12015-10-02 21:05:03 +0100506 ArenaAllocator* allocator = cls->GetBlock()->GetGraph()->GetArena();
507 LocationSummary::CallKind call_kind = cls->NeedsAccessCheck()
508 ? LocationSummary::kCall
Roland Levillain0d5a2812015-11-13 10:07:31 +0000509 : (((code_generator_supports_read_barrier && kEmitCompilerReadBarrier) ||
510 cls->CanCallRuntime())
511 ? LocationSummary::kCallOnSlowPath
512 : LocationSummary::kNoCall);
Calin Juravle98893e12015-10-02 21:05:03 +0100513 LocationSummary* locations = new (allocator) LocationSummary(cls, call_kind);
Calin Juravle98893e12015-10-02 21:05:03 +0100514 if (cls->NeedsAccessCheck()) {
Calin Juravle580b6092015-10-06 17:35:58 +0100515 locations->SetInAt(0, Location::NoLocation());
Calin Juravle98893e12015-10-02 21:05:03 +0100516 locations->AddTemp(runtime_type_index_location);
517 locations->SetOut(runtime_return_location);
518 } else {
Calin Juravle580b6092015-10-06 17:35:58 +0100519 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle98893e12015-10-02 21:05:03 +0100520 locations->SetOut(Location::RequiresRegister());
521 }
522}
523
524
Mark Mendell5f874182015-03-04 15:42:45 -0500525void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
526 // The DCHECKS below check that a register is not specified twice in
527 // the summary. The out location can overlap with an input, so we need
528 // to special case it.
529 if (location.IsRegister()) {
530 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
531 blocked_core_registers_[location.reg()] = true;
532 } else if (location.IsFpuRegister()) {
533 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
534 blocked_fpu_registers_[location.reg()] = true;
535 } else if (location.IsFpuRegisterPair()) {
536 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
537 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
538 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
539 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
540 } else if (location.IsRegisterPair()) {
541 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
542 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
543 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
544 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
545 }
546}
547
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000548void CodeGenerator::AllocateLocations(HInstruction* instruction) {
549 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100550 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000551 LocationSummary* locations = instruction->GetLocations();
552 if (!instruction->IsSuspendCheckEntry()) {
553 if (locations != nullptr && locations->CanCall()) {
554 MarkNotLeaf();
555 }
556 if (instruction->NeedsCurrentMethod()) {
557 SetRequiresCurrentMethod();
558 }
559 }
560}
561
Serban Constantinescuecc43662015-08-13 13:33:12 +0100562void CodeGenerator::MaybeRecordStat(MethodCompilationStat compilation_stat, size_t count) const {
563 if (stats_ != nullptr) {
564 stats_->RecordStat(compilation_stat, count);
565 }
566}
567
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000568CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000569 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000570 const InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100571 const CompilerOptions& compiler_options,
572 OptimizingCompilerStats* stats) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000573 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -0700574#ifdef ART_ENABLE_CODEGEN_arm
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000575 case kArm:
576 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000577 return new arm::CodeGeneratorARM(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100578 *isa_features.AsArmInstructionSetFeatures(),
579 compiler_options,
580 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000581 }
Alex Light50fa9932015-08-10 15:30:07 -0700582#endif
583#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames5319def2014-10-23 10:03:10 +0100584 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000585 return new arm64::CodeGeneratorARM64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100586 *isa_features.AsArm64InstructionSetFeatures(),
587 compiler_options,
588 stats);
Alexandre Rames5319def2014-10-23 10:03:10 +0100589 }
Alex Light50fa9932015-08-10 15:30:07 -0700590#endif
591#ifdef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200592 case kMips: {
593 return new mips::CodeGeneratorMIPS(graph,
594 *isa_features.AsMipsInstructionSetFeatures(),
595 compiler_options,
596 stats);
597 }
Alex Light50fa9932015-08-10 15:30:07 -0700598#endif
599#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700600 case kMips64: {
601 return new mips64::CodeGeneratorMIPS64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100602 *isa_features.AsMips64InstructionSetFeatures(),
603 compiler_options,
604 stats);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700605 }
Alex Light50fa9932015-08-10 15:30:07 -0700606#endif
607#ifdef ART_ENABLE_CODEGEN_x86
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000608 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400609 return new x86::CodeGeneratorX86(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100610 *isa_features.AsX86InstructionSetFeatures(),
611 compiler_options,
612 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000613 }
Alex Light50fa9932015-08-10 15:30:07 -0700614#endif
615#ifdef ART_ENABLE_CODEGEN_x86_64
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700616 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400617 return new x86_64::CodeGeneratorX86_64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100618 *isa_features.AsX86_64InstructionSetFeatures(),
619 compiler_options,
620 stats);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700621 }
Alex Light50fa9932015-08-10 15:30:07 -0700622#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000623 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000624 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000625 }
626}
627
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000628size_t CodeGenerator::ComputeStackMapsSize() {
629 return stack_map_stream_.PrepareForFillIn();
630}
631
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000632static void CheckCovers(uint32_t dex_pc,
633 const HGraph& graph,
634 const CodeInfo& code_info,
635 const ArenaVector<HSuspendCheck*>& loop_headers,
636 ArenaVector<size_t>* covered) {
637 StackMapEncoding encoding = code_info.ExtractEncoding();
638 for (size_t i = 0; i < loop_headers.size(); ++i) {
639 if (loop_headers[i]->GetDexPc() == dex_pc) {
640 if (graph.IsCompilingOsr()) {
641 DCHECK(code_info.GetOsrStackMapForDexPc(dex_pc, encoding).IsValid());
642 }
643 ++(*covered)[i];
644 }
645 }
646}
647
648// Debug helper to ensure loop entries in compiled code are matched by
649// dex branch instructions.
650static void CheckLoopEntriesCanBeUsedForOsr(const HGraph& graph,
651 const CodeInfo& code_info,
652 const DexFile::CodeItem& code_item) {
653 if (graph.HasTryCatch()) {
654 // One can write loops through try/catch, which we do not support for OSR anyway.
655 return;
656 }
657 ArenaVector<HSuspendCheck*> loop_headers(graph.GetArena()->Adapter(kArenaAllocMisc));
658 for (HReversePostOrderIterator it(graph); !it.Done(); it.Advance()) {
659 if (it.Current()->IsLoopHeader()) {
660 HSuspendCheck* suspend_check = it.Current()->GetLoopInformation()->GetSuspendCheck();
661 if (!suspend_check->GetEnvironment()->IsFromInlinedInvoke()) {
662 loop_headers.push_back(suspend_check);
663 }
664 }
665 }
666 ArenaVector<size_t> covered(loop_headers.size(), 0, graph.GetArena()->Adapter(kArenaAllocMisc));
667 const uint16_t* code_ptr = code_item.insns_;
668 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
669
670 size_t dex_pc = 0;
671 while (code_ptr < code_end) {
672 const Instruction& instruction = *Instruction::At(code_ptr);
673 if (instruction.IsBranch()) {
674 uint32_t target = dex_pc + instruction.GetTargetOffset();
675 CheckCovers(target, graph, code_info, loop_headers, &covered);
676 } else if (instruction.IsSwitch()) {
677 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
678 uint16_t num_entries = table.GetNumEntries();
679 size_t offset = table.GetFirstValueIndex();
680
681 // Use a larger loop counter type to avoid overflow issues.
682 for (size_t i = 0; i < num_entries; ++i) {
683 // The target of the case.
684 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
685 CheckCovers(target, graph, code_info, loop_headers, &covered);
686 }
687 }
688 dex_pc += instruction.SizeInCodeUnits();
689 code_ptr += instruction.SizeInCodeUnits();
690 }
691
692 for (size_t i = 0; i < covered.size(); ++i) {
693 DCHECK_NE(covered[i], 0u) << "Loop in compiled code has no dex branch equivalent";
694 }
695}
696
697void CodeGenerator::BuildStackMaps(MemoryRegion region, const DexFile::CodeItem& code_item) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100698 stack_map_stream_.FillIn(region);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000699 if (kIsDebugBuild) {
700 CheckLoopEntriesCanBeUsedForOsr(*graph_, CodeInfo(region), code_item);
701 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100702}
703
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000704void CodeGenerator::RecordPcInfo(HInstruction* instruction,
705 uint32_t dex_pc,
706 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000707 if (instruction != nullptr) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700708 // The code generated for some type conversions and comparisons
709 // may call the runtime, thus normally requiring a subsequent
710 // call to this method. However, the method verifier does not
711 // produce PC information for certain instructions, which are
712 // considered "atomic" (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000713 // Therefore we do not currently record PC information for such
714 // instructions. As this may change later, we added this special
715 // case so that code generators may nevertheless call
716 // CodeGenerator::RecordPcInfo without triggering an error in
717 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
718 // thereafter.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700719 if (instruction->IsTypeConversion() || instruction->IsCompare()) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000720 return;
721 }
722 if (instruction->IsRem()) {
723 Primitive::Type type = instruction->AsRem()->GetResultType();
724 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
725 return;
726 }
727 }
Roland Levillain624279f2014-12-04 11:54:28 +0000728 }
729
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100730 uint32_t outer_dex_pc = dex_pc;
731 uint32_t outer_environment_size = 0;
732 uint32_t inlining_depth = 0;
733 if (instruction != nullptr) {
734 for (HEnvironment* environment = instruction->GetEnvironment();
735 environment != nullptr;
736 environment = environment->GetParent()) {
737 outer_dex_pc = environment->GetDexPc();
738 outer_environment_size = environment->Size();
739 if (environment != instruction->GetEnvironment()) {
740 inlining_depth++;
741 }
742 }
743 }
744
Nicolas Geoffray39468442014-09-02 15:17:15 +0100745 // Collect PC infos for the mapping table.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100746 uint32_t native_pc = GetAssembler()->CodeSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100747
Nicolas Geoffray39468442014-09-02 15:17:15 +0100748 if (instruction == nullptr) {
749 // For stack overflow checks.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100750 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100751 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000752 return;
753 }
754 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100755
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000756 uint32_t register_mask = locations->GetRegisterMask();
757 if (locations->OnlyCallsOnSlowPath()) {
758 // In case of slow path, we currently set the location of caller-save registers
759 // to register (instead of their stack location when pushed before the slow-path
760 // call). Therefore register_mask contains both callee-save and caller-save
761 // registers that hold objects. We must remove the caller-save from the mask, since
762 // they will be overwritten by the callee.
763 register_mask &= core_callee_save_mask_;
764 }
765 // The register mask must be a subset of callee-save registers.
766 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100767 stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
768 native_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100769 register_mask,
770 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100771 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100772 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100773
774 EmitEnvironment(instruction->GetEnvironment(), slow_path);
775 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000776
777 HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
778 if (instruction->IsSuspendCheck() &&
779 (info != nullptr) &&
780 graph_->IsCompilingOsr() &&
781 (inlining_depth == 0)) {
782 DCHECK_EQ(info->GetSuspendCheck(), instruction);
783 // We duplicate the stack map as a marker that this stack map can be an OSR entry.
784 // Duplicating it avoids having the runtime recognize and skip an OSR stack map.
785 DCHECK(info->IsIrreducible());
786 stack_map_stream_.BeginStackMapEntry(
787 dex_pc, native_pc, register_mask, locations->GetStackMask(), outer_environment_size, 0);
788 EmitEnvironment(instruction->GetEnvironment(), slow_path);
789 stack_map_stream_.EndStackMapEntry();
790 if (kIsDebugBuild) {
791 HEnvironment* environment = instruction->GetEnvironment();
792 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
793 HInstruction* in_environment = environment->GetInstructionAt(i);
794 if (in_environment != nullptr) {
795 DCHECK(in_environment->IsPhi() || in_environment->IsConstant());
796 Location location = environment->GetLocationAt(i);
797 DCHECK(location.IsStackSlot() ||
798 location.IsDoubleStackSlot() ||
799 location.IsConstant() ||
800 location.IsInvalid());
801 if (location.IsStackSlot() || location.IsDoubleStackSlot()) {
802 DCHECK_LT(location.GetStackIndex(), static_cast<int32_t>(GetFrameSize()));
803 }
804 }
805 }
806 }
807 } else if (kIsDebugBuild) {
808 // Ensure stack maps are unique, by checking that the native pc in the stack map
809 // last emitted is different than the native pc of the stack map just emitted.
810 size_t number_of_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
811 if (number_of_stack_maps > 1) {
812 DCHECK_NE(stack_map_stream_.GetStackMap(number_of_stack_maps - 1).native_pc_offset,
813 stack_map_stream_.GetStackMap(number_of_stack_maps - 2).native_pc_offset);
814 }
815 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100816}
817
David Srbeckyb7070a22016-01-08 18:13:53 +0000818bool CodeGenerator::HasStackMapAtCurrentPc() {
819 uint32_t pc = GetAssembler()->CodeSize();
820 size_t count = stack_map_stream_.GetNumberOfStackMaps();
821 return count > 0 && stack_map_stream_.GetStackMap(count - 1).native_pc_offset == pc;
822}
823
David Brazdil77a48ae2015-09-15 12:34:04 +0000824void CodeGenerator::RecordCatchBlockInfo() {
825 ArenaAllocator* arena = graph_->GetArena();
826
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100827 for (HBasicBlock* block : *block_order_) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000828 if (!block->IsCatchBlock()) {
829 continue;
830 }
831
832 uint32_t dex_pc = block->GetDexPc();
833 uint32_t num_vregs = graph_->GetNumberOfVRegs();
834 uint32_t inlining_depth = 0; // Inlining of catch blocks is not supported at the moment.
835 uint32_t native_pc = GetAddressOf(block);
836 uint32_t register_mask = 0; // Not used.
837
838 // The stack mask is not used, so we leave it empty.
839 ArenaBitVector* stack_mask = new (arena) ArenaBitVector(arena, 0, /* expandable */ true);
840
841 stack_map_stream_.BeginStackMapEntry(dex_pc,
842 native_pc,
843 register_mask,
844 stack_mask,
845 num_vregs,
846 inlining_depth);
847
848 HInstruction* current_phi = block->GetFirstPhi();
849 for (size_t vreg = 0; vreg < num_vregs; ++vreg) {
850 while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) {
851 HInstruction* next_phi = current_phi->GetNext();
852 DCHECK(next_phi == nullptr ||
853 current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber())
854 << "Phis need to be sorted by vreg number to keep this a linear-time loop.";
855 current_phi = next_phi;
856 }
857
858 if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) {
859 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
860 } else {
861 Location location = current_phi->GetLiveInterval()->ToLocation();
862 switch (location.GetKind()) {
863 case Location::kStackSlot: {
864 stack_map_stream_.AddDexRegisterEntry(
865 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
866 break;
867 }
868 case Location::kDoubleStackSlot: {
869 stack_map_stream_.AddDexRegisterEntry(
870 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
871 stack_map_stream_.AddDexRegisterEntry(
872 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
873 ++vreg;
874 DCHECK_LT(vreg, num_vregs);
875 break;
876 }
877 default: {
878 // All catch phis must be allocated to a stack slot.
879 LOG(FATAL) << "Unexpected kind " << location.GetKind();
880 UNREACHABLE();
881 }
882 }
883 }
884 }
885
886 stack_map_stream_.EndStackMapEntry();
887 }
888}
889
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100890void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
891 if (environment == nullptr) return;
892
893 if (environment->GetParent() != nullptr) {
894 // We emit the parent environment first.
895 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100896 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
897 environment->GetDexPc(),
898 environment->GetInvokeType(),
899 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100900 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000901
902 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100903 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000904 HInstruction* current = environment->GetInstructionAt(i);
905 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100906 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000907 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100908 }
909
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100910 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000911 switch (location.GetKind()) {
912 case Location::kConstant: {
913 DCHECK_EQ(current, location.GetConstant());
914 if (current->IsLongConstant()) {
915 int64_t value = current->AsLongConstant()->GetValue();
916 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100917 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000918 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100919 DexRegisterLocation::Kind::kConstant, High32Bits(value));
920 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000921 DCHECK_LT(i, environment_size);
922 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000923 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000924 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100925 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000926 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100927 DexRegisterLocation::Kind::kConstant, High32Bits(value));
928 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000929 DCHECK_LT(i, environment_size);
930 } else if (current->IsIntConstant()) {
931 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100932 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000933 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100934 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000935 } else {
936 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000937 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100938 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000939 }
940 break;
941 }
942
943 case Location::kStackSlot: {
944 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100945 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000946 break;
947 }
948
949 case Location::kDoubleStackSlot: {
950 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100951 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000952 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100953 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
954 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000955 DCHECK_LT(i, environment_size);
956 break;
957 }
958
959 case Location::kRegister : {
960 int id = location.reg();
961 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
962 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100963 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000964 if (current->GetType() == Primitive::kPrimLong) {
965 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100966 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
967 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000968 DCHECK_LT(i, environment_size);
969 }
970 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100971 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000972 if (current->GetType() == Primitive::kPrimLong) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100973 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100974 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000975 DCHECK_LT(i, environment_size);
976 }
977 }
978 break;
979 }
980
981 case Location::kFpuRegister : {
982 int id = location.reg();
983 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
984 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100985 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000986 if (current->GetType() == Primitive::kPrimDouble) {
987 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100988 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
989 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000990 DCHECK_LT(i, environment_size);
991 }
992 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100993 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000994 if (current->GetType() == Primitive::kPrimDouble) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100995 stack_map_stream_.AddDexRegisterEntry(
996 DexRegisterLocation::Kind::kInFpuRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100997 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000998 DCHECK_LT(i, environment_size);
999 }
1000 }
1001 break;
1002 }
1003
1004 case Location::kFpuRegisterPair : {
1005 int low = location.low();
1006 int high = location.high();
1007 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
1008 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001009 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001010 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001011 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001012 }
1013 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
1014 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001015 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1016 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001017 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001018 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
1019 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001020 }
1021 DCHECK_LT(i, environment_size);
1022 break;
1023 }
1024
1025 case Location::kRegisterPair : {
1026 int low = location.low();
1027 int high = location.high();
1028 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
1029 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001030 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001031 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001032 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001033 }
1034 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
1035 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001036 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001037 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001038 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001039 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001040 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001041 DCHECK_LT(i, environment_size);
1042 break;
1043 }
1044
1045 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001046 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001047 break;
1048 }
1049
1050 default:
1051 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1052 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001053 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001054
1055 if (environment->GetParent() != nullptr) {
1056 stack_map_stream_.EndInlineInfoEntry();
1057 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001058}
1059
David Brazdil77a48ae2015-09-15 12:34:04 +00001060bool CodeGenerator::IsImplicitNullCheckAllowed(HNullCheck* null_check) const {
1061 return compiler_options_.GetImplicitNullChecks() &&
1062 // Null checks which might throw into a catch block need to save live
1063 // registers and therefore cannot be done implicitly.
1064 !null_check->CanThrowIntoCatchBlock();
1065}
1066
Calin Juravle77520bc2015-01-12 18:45:46 +00001067bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
1068 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +01001069
1070 return (first_next_not_move != nullptr)
1071 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00001072}
1073
1074void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
1075 // If we are from a static path don't record the pc as we can't throw NPE.
1076 // NB: having the checks here makes the code much less verbose in the arch
1077 // specific code generators.
1078 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
1079 return;
1080 }
1081
Calin Juravle641547a2015-04-21 22:08:51 +01001082 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001083 return;
1084 }
1085
1086 // Find the first previous instruction which is not a move.
1087 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
1088
1089 // If the instruction is a null check it means that `instr` is the first user
1090 // and needs to record the pc.
1091 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
1092 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
David Brazdil77a48ae2015-09-15 12:34:04 +00001093 if (IsImplicitNullCheckAllowed(null_check)) {
1094 // TODO: The parallel moves modify the environment. Their changes need to be
1095 // reverted otherwise the stack maps at the throw point will not be correct.
1096 RecordPcInfo(null_check, null_check->GetDexPc());
1097 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001098 }
1099}
1100
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001101void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
1102 LocationSummary* locations = suspend_check->GetLocations();
1103 HBasicBlock* block = suspend_check->GetBlock();
1104 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
1105 DCHECK(block->IsLoopHeader());
1106
1107 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1108 HInstruction* current = it.Current();
1109 LiveInterval* interval = current->GetLiveInterval();
1110 // We only need to clear bits of loop phis containing objects and allocated in register.
1111 // Loop phis allocated on stack already have the object in the stack.
1112 if (current->GetType() == Primitive::kPrimNot
1113 && interval->HasRegister()
1114 && interval->HasSpillSlot()) {
1115 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
1116 }
1117 }
1118}
1119
Nicolas Geoffray90218252015-04-15 11:56:51 +01001120void CodeGenerator::EmitParallelMoves(Location from1,
1121 Location to1,
1122 Primitive::Type type1,
1123 Location from2,
1124 Location to2,
1125 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001126 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +01001127 parallel_move.AddMove(from1, to1, type1, nullptr);
1128 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001129 GetMoveResolver()->EmitNativeCode(&parallel_move);
1130}
1131
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001132void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) {
1133 // Ensure that the call kind indication given to the register allocator is
1134 // coherent with the runtime call generated, and that the GC side effect is
1135 // set when required.
1136 if (slow_path == nullptr) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001137 DCHECK(instruction->GetLocations()->WillCall())
1138 << "instruction->DebugName()=" << instruction->DebugName();
Roland Levillaindf3f8222015-08-13 12:31:44 +01001139 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()))
Roland Levillain0d5a2812015-11-13 10:07:31 +00001140 << "instruction->DebugName()=" << instruction->DebugName()
1141 << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001142 } else {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001143 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal())
Roland Levillain0d5a2812015-11-13 10:07:31 +00001144 << "instruction->DebugName()=" << instruction->DebugName()
1145 << " slow_path->GetDescription()=" << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001146 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) ||
Roland Levillain0d5a2812015-11-13 10:07:31 +00001147 // When read barriers are enabled, some instructions use a
1148 // slow path to emit a read barrier, which does not trigger
1149 // GC, is not fatal, nor is emitted by HDeoptimize
1150 // instructions.
1151 (kEmitCompilerReadBarrier &&
1152 (instruction->IsInstanceFieldGet() ||
1153 instruction->IsStaticFieldGet() ||
1154 instruction->IsArraySet() ||
1155 instruction->IsArrayGet() ||
1156 instruction->IsLoadClass() ||
1157 instruction->IsLoadString() ||
1158 instruction->IsInstanceOf() ||
1159 instruction->IsCheckCast())))
1160 << "instruction->DebugName()=" << instruction->DebugName()
1161 << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString()
1162 << " slow_path->GetDescription()=" << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001163 }
1164
1165 // Check the coherency of leaf information.
1166 DCHECK(instruction->IsSuspendCheck()
1167 || ((slow_path != nullptr) && slow_path->IsFatal())
1168 || instruction->GetLocations()->CanCall()
Roland Levillaindf3f8222015-08-13 12:31:44 +01001169 || !IsLeafMethod())
1170 << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : "");
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001171}
1172
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001173void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001174 RegisterSet* live_registers = locations->GetLiveRegisters();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001175 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001176
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001177 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1178 if (!codegen->IsCoreCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001179 if (live_registers->ContainsCoreRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001180 // If the register holds an object, update the stack mask.
1181 if (locations->RegisterContainsObject(i)) {
1182 locations->SetStackBit(stack_offset / kVRegSize);
1183 }
1184 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001185 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1186 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001187 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
1188 }
1189 }
1190 }
1191
1192 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1193 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001194 if (live_registers->ContainsFloatingPointRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001195 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001196 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1197 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001198 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
1199 }
1200 }
1201 }
1202}
1203
1204void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001205 RegisterSet* live_registers = locations->GetLiveRegisters();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001206 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001207
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001208 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1209 if (!codegen->IsCoreCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001210 if (live_registers->ContainsCoreRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001211 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Roland Levillain0d5a2812015-11-13 10:07:31 +00001212 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001213 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1214 }
1215 }
1216 }
1217
1218 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1219 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001220 if (live_registers->ContainsFloatingPointRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001221 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Roland Levillain0d5a2812015-11-13 10:07:31 +00001222 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001223 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1224 }
1225 }
1226 }
1227}
1228
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001229void CodeGenerator::CreateSystemArrayCopyLocationSummary(HInvoke* invoke) {
1230 // Check to see if we have known failures that will cause us to have to bail out
1231 // to the runtime, and just generate the runtime call directly.
1232 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1233 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
1234
1235 // The positions must be non-negative.
1236 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
1237 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
1238 // We will have to fail anyways.
1239 return;
1240 }
1241
1242 // The length must be >= 0.
1243 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1244 if (length != nullptr) {
1245 int32_t len = length->GetValue();
1246 if (len < 0) {
1247 // Just call as normal.
1248 return;
1249 }
1250 }
1251
1252 SystemArrayCopyOptimizations optimizations(invoke);
1253
1254 if (optimizations.GetDestinationIsSource()) {
1255 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
1256 // We only support backward copying if source and destination are the same.
1257 return;
1258 }
1259 }
1260
1261 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
1262 // We currently don't intrinsify primitive copying.
1263 return;
1264 }
1265
1266 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
1267 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1268 LocationSummary::kCallOnSlowPath,
1269 kIntrinsified);
1270 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
1271 locations->SetInAt(0, Location::RequiresRegister());
1272 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1273 locations->SetInAt(2, Location::RequiresRegister());
1274 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
1275 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
1276
1277 locations->AddTemp(Location::RequiresRegister());
1278 locations->AddTemp(Location::RequiresRegister());
1279 locations->AddTemp(Location::RequiresRegister());
1280}
1281
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001282} // namespace art