blob: 32869ec0b4fe79fdc2c53d7e63602ef58f631cf1 [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 }
David Srbecky9cd6d372016-02-09 15:24:47 +0000198 // Record the dex pc at start of slow path (required for java line number mapping).
David Srbeckyd28f4a02016-03-14 17:14:24 +0000199 MaybeRecordNativeDebugInfo(slow_path->GetInstruction(), slow_path->GetDexPc(), slow_path);
Vladimir Marko225b6462015-09-28 12:17:40 +0100200 slow_path->EmitNativeCode(this);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100201 if (disasm_info_ != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100202 disasm_info_->AddSlowPathInterval(slow_path, code_start, GetAssembler()->CodeSize());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100203 }
204 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000205 current_slow_path_ = nullptr;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100206}
207
David Brazdil58282f42016-01-14 12:45:10 +0000208void CodeGenerator::Compile(CodeAllocator* allocator) {
209 // The register allocator already called `InitializeCodeGeneration`,
210 // where the frame size has been computed.
211 DCHECK(block_order_ != nullptr);
212 Initialize();
213
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100214 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000215 DCHECK_EQ(current_block_index_, 0u);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100216
217 size_t frame_start = GetAssembler()->CodeSize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000218 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100219 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100220 if (disasm_info_ != nullptr) {
221 disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize());
222 }
223
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100224 for (size_t e = block_order_->size(); current_block_index_ < e; ++current_block_index_) {
225 HBasicBlock* block = (*block_order_)[current_block_index_];
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000226 // Don't generate code for an empty block. Its predecessors will branch to its successor
227 // directly. Also, the label of that block will not be emitted, so this helps catch
228 // errors where we reference that label.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000229 if (block->IsSingleJump()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100230 Bind(block);
David Srbeckyc7098ff2016-02-09 14:30:11 +0000231 // This ensures that we have correct native line mapping for all native instructions.
232 // It is necessary to make stepping over a statement work. Otherwise, any initial
233 // instructions (e.g. moves) would be assumed to be the start of next statement.
234 MaybeRecordNativeDebugInfo(nullptr /* instruction */, block->GetDexPc());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100235 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
236 HInstruction* current = it.Current();
David Srbeckyd28f4a02016-03-14 17:14:24 +0000237 if (current->HasEnvironment()) {
238 // Create stackmap for HNativeDebugInfo or any instruction which calls native code.
239 // Note that we need correct mapping for the native PC of the call instruction,
240 // so the runtime's stackmap is not sufficient since it is at PC after the call.
241 MaybeRecordNativeDebugInfo(current, block->GetDexPc());
242 }
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100243 DisassemblyScope disassembly_scope(current, *this);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100244 DCHECK(CheckTypeConsistency(current));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100245 current->Accept(instruction_visitor);
246 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000247 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000248
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100249 GenerateSlowPaths();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000250
David Brazdil77a48ae2015-09-15 12:34:04 +0000251 // Emit catch stack maps at the end of the stack map stream as expected by the
252 // runtime exception handler.
David Brazdil58282f42016-01-14 12:45:10 +0000253 if (graph_->HasTryCatch()) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000254 RecordCatchBlockInfo();
255 }
256
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000257 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000258 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000259}
260
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000261void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100262 size_t code_size = GetAssembler()->CodeSize();
263 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000264
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100265 MemoryRegion code(buffer, code_size);
266 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000267}
268
Vladimir Marko58155012015-08-19 12:49:41 +0000269void CodeGenerator::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches ATTRIBUTE_UNUSED) {
270 // No linker patches by default.
271}
272
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000273void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
274 size_t maximum_number_of_live_core_registers,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000275 size_t maximum_number_of_live_fpu_registers,
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000276 size_t number_of_out_slots,
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100277 const ArenaVector<HBasicBlock*>& block_order) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000278 block_order_ = &block_order;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100279 DCHECK(!block_order.empty());
280 DCHECK(block_order[0] == GetGraph()->GetEntryBlock());
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000281 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100282 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
283
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000284 if (number_of_spill_slots == 0
285 && !HasAllocatedCalleeSaveRegisters()
286 && IsLeafMethod()
287 && !RequiresCurrentMethod()) {
288 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000289 DCHECK_EQ(maximum_number_of_live_fpu_registers, 0u);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000290 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
291 } else {
292 SetFrameSize(RoundUp(
293 number_of_spill_slots * kVRegSize
294 + number_of_out_slots * kVRegSize
295 + maximum_number_of_live_core_registers * GetWordSize()
Roland Levillain0d5a2812015-11-13 10:07:31 +0000296 + maximum_number_of_live_fpu_registers * GetFloatingPointSpillSlotSize()
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000297 + FrameEntrySpillSize(),
298 kStackAlignment));
299 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100300}
301
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100302int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
303 uint16_t reg_number = local->GetRegNumber();
304 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
305 if (reg_number >= number_of_locals) {
306 // Local is a parameter of the method. It is stored in the caller's frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307 // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode.
308 return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100309 + (reg_number - number_of_locals) * kVRegSize;
310 } else {
311 // Local is a temporary in this method. It is stored in this method's frame.
312 return GetFrameSize() - FrameEntrySpillSize()
313 - kVRegSize // filler.
314 - (number_of_locals * kVRegSize)
315 + (reg_number * kVRegSize);
316 }
317}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100318
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100319void CodeGenerator::CreateCommonInvokeLocationSummary(
Nicolas Geoffray4e40c262015-06-03 12:02:38 +0100320 HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100321 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
322 LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100323
324 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
325 HInstruction* input = invoke->InputAt(i);
326 locations->SetInAt(i, visitor->GetNextLocation(input->GetType()));
327 }
328
329 locations->SetOut(visitor->GetReturnLocation(invoke->GetType()));
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100330
331 if (invoke->IsInvokeStaticOrDirect()) {
332 HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect();
Vladimir Markodc151b22015-10-15 18:02:30 +0100333 switch (call->GetMethodLoadKind()) {
334 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +0000335 locations->SetInAt(call->GetSpecialInputIndex(), visitor->GetMethodLocation());
Vladimir Markodc151b22015-10-15 18:02:30 +0100336 break;
337 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod:
338 locations->AddTemp(visitor->GetMethodLocation());
Vladimir Markoc53c0792015-11-19 15:48:33 +0000339 locations->SetInAt(call->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Markodc151b22015-10-15 18:02:30 +0100340 break;
341 default:
342 locations->AddTemp(visitor->GetMethodLocation());
343 break;
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100344 }
345 } else {
346 locations->AddTemp(visitor->GetMethodLocation());
347 }
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100348}
349
Calin Juravle175dc732015-08-25 15:42:32 +0100350void CodeGenerator::GenerateInvokeUnresolvedRuntimeCall(HInvokeUnresolved* invoke) {
351 MoveConstant(invoke->GetLocations()->GetTemp(0), invoke->GetDexMethodIndex());
352
353 // Initialize to anything to silent compiler warnings.
354 QuickEntrypointEnum entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
355 switch (invoke->GetOriginalInvokeType()) {
356 case kStatic:
357 entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
358 break;
359 case kDirect:
360 entrypoint = kQuickInvokeDirectTrampolineWithAccessCheck;
361 break;
362 case kVirtual:
363 entrypoint = kQuickInvokeVirtualTrampolineWithAccessCheck;
364 break;
365 case kSuper:
366 entrypoint = kQuickInvokeSuperTrampolineWithAccessCheck;
367 break;
368 case kInterface:
369 entrypoint = kQuickInvokeInterfaceTrampolineWithAccessCheck;
370 break;
371 }
372 InvokeRuntime(entrypoint, invoke, invoke->GetDexPc(), nullptr);
373}
374
Calin Juravlee460d1d2015-09-29 04:52:17 +0100375void CodeGenerator::CreateUnresolvedFieldLocationSummary(
376 HInstruction* field_access,
377 Primitive::Type field_type,
378 const FieldAccessCallingConvention& calling_convention) {
379 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
380 || field_access->IsUnresolvedInstanceFieldSet();
381 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
382 || field_access->IsUnresolvedStaticFieldGet();
383
384 ArenaAllocator* allocator = field_access->GetBlock()->GetGraph()->GetArena();
385 LocationSummary* locations =
386 new (allocator) LocationSummary(field_access, LocationSummary::kCall);
387
388 locations->AddTemp(calling_convention.GetFieldIndexLocation());
389
390 if (is_instance) {
391 // Add the `this` object for instance field accesses.
392 locations->SetInAt(0, calling_convention.GetObjectLocation());
393 }
394
395 // Note that pSetXXStatic/pGetXXStatic always takes/returns an int or int64
396 // regardless of the the type. Because of that we forced to special case
397 // the access to floating point values.
398 if (is_get) {
399 if (Primitive::IsFloatingPointType(field_type)) {
400 // The return value will be stored in regular registers while register
401 // allocator expects it in a floating point register.
402 // Note We don't need to request additional temps because the return
403 // register(s) are already blocked due the call and they may overlap with
404 // the input or field index.
405 // The transfer between the two will be done at codegen level.
406 locations->SetOut(calling_convention.GetFpuLocation(field_type));
407 } else {
408 locations->SetOut(calling_convention.GetReturnLocation(field_type));
409 }
410 } else {
411 size_t set_index = is_instance ? 1 : 0;
412 if (Primitive::IsFloatingPointType(field_type)) {
413 // The set value comes from a float location while the calling convention
414 // expects it in a regular register location. Allocate a temp for it and
415 // make the transfer at codegen.
416 AddLocationAsTemp(calling_convention.GetSetValueLocation(field_type, is_instance), locations);
417 locations->SetInAt(set_index, calling_convention.GetFpuLocation(field_type));
418 } else {
419 locations->SetInAt(set_index,
420 calling_convention.GetSetValueLocation(field_type, is_instance));
421 }
422 }
423}
424
425void CodeGenerator::GenerateUnresolvedFieldAccess(
426 HInstruction* field_access,
427 Primitive::Type field_type,
428 uint32_t field_index,
429 uint32_t dex_pc,
430 const FieldAccessCallingConvention& calling_convention) {
431 LocationSummary* locations = field_access->GetLocations();
432
433 MoveConstant(locations->GetTemp(0), field_index);
434
435 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
436 || field_access->IsUnresolvedInstanceFieldSet();
437 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
438 || field_access->IsUnresolvedStaticFieldGet();
439
440 if (!is_get && Primitive::IsFloatingPointType(field_type)) {
441 // Copy the float value to be set into the calling convention register.
442 // Note that using directly the temp location is problematic as we don't
443 // support temp register pairs. To avoid boilerplate conversion code, use
444 // the location from the calling convention.
445 MoveLocation(calling_convention.GetSetValueLocation(field_type, is_instance),
446 locations->InAt(is_instance ? 1 : 0),
447 (Primitive::Is64BitType(field_type) ? Primitive::kPrimLong : Primitive::kPrimInt));
448 }
449
450 QuickEntrypointEnum entrypoint = kQuickSet8Static; // Initialize to anything to avoid warnings.
451 switch (field_type) {
452 case Primitive::kPrimBoolean:
453 entrypoint = is_instance
454 ? (is_get ? kQuickGetBooleanInstance : kQuickSet8Instance)
455 : (is_get ? kQuickGetBooleanStatic : kQuickSet8Static);
456 break;
457 case Primitive::kPrimByte:
458 entrypoint = is_instance
459 ? (is_get ? kQuickGetByteInstance : kQuickSet8Instance)
460 : (is_get ? kQuickGetByteStatic : kQuickSet8Static);
461 break;
462 case Primitive::kPrimShort:
463 entrypoint = is_instance
464 ? (is_get ? kQuickGetShortInstance : kQuickSet16Instance)
465 : (is_get ? kQuickGetShortStatic : kQuickSet16Static);
466 break;
467 case Primitive::kPrimChar:
468 entrypoint = is_instance
469 ? (is_get ? kQuickGetCharInstance : kQuickSet16Instance)
470 : (is_get ? kQuickGetCharStatic : kQuickSet16Static);
471 break;
472 case Primitive::kPrimInt:
473 case Primitive::kPrimFloat:
474 entrypoint = is_instance
475 ? (is_get ? kQuickGet32Instance : kQuickSet32Instance)
476 : (is_get ? kQuickGet32Static : kQuickSet32Static);
477 break;
478 case Primitive::kPrimNot:
479 entrypoint = is_instance
480 ? (is_get ? kQuickGetObjInstance : kQuickSetObjInstance)
481 : (is_get ? kQuickGetObjStatic : kQuickSetObjStatic);
482 break;
483 case Primitive::kPrimLong:
484 case Primitive::kPrimDouble:
485 entrypoint = is_instance
486 ? (is_get ? kQuickGet64Instance : kQuickSet64Instance)
487 : (is_get ? kQuickGet64Static : kQuickSet64Static);
488 break;
489 default:
490 LOG(FATAL) << "Invalid type " << field_type;
491 }
492 InvokeRuntime(entrypoint, field_access, dex_pc, nullptr);
493
494 if (is_get && Primitive::IsFloatingPointType(field_type)) {
495 MoveLocation(locations->Out(), calling_convention.GetReturnLocation(field_type), field_type);
496 }
497}
498
Roland Levillain0d5a2812015-11-13 10:07:31 +0000499// TODO: Remove argument `code_generator_supports_read_barrier` when
500// all code generators have read barrier support.
Calin Juravle98893e12015-10-02 21:05:03 +0100501void CodeGenerator::CreateLoadClassLocationSummary(HLoadClass* cls,
502 Location runtime_type_index_location,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000503 Location runtime_return_location,
504 bool code_generator_supports_read_barrier) {
Calin Juravle98893e12015-10-02 21:05:03 +0100505 ArenaAllocator* allocator = cls->GetBlock()->GetGraph()->GetArena();
506 LocationSummary::CallKind call_kind = cls->NeedsAccessCheck()
507 ? LocationSummary::kCall
Roland Levillain0d5a2812015-11-13 10:07:31 +0000508 : (((code_generator_supports_read_barrier && kEmitCompilerReadBarrier) ||
509 cls->CanCallRuntime())
510 ? LocationSummary::kCallOnSlowPath
511 : LocationSummary::kNoCall);
Calin Juravle98893e12015-10-02 21:05:03 +0100512 LocationSummary* locations = new (allocator) LocationSummary(cls, call_kind);
Calin Juravle98893e12015-10-02 21:05:03 +0100513 if (cls->NeedsAccessCheck()) {
Calin Juravle580b6092015-10-06 17:35:58 +0100514 locations->SetInAt(0, Location::NoLocation());
Calin Juravle98893e12015-10-02 21:05:03 +0100515 locations->AddTemp(runtime_type_index_location);
516 locations->SetOut(runtime_return_location);
517 } else {
Calin Juravle580b6092015-10-06 17:35:58 +0100518 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle98893e12015-10-02 21:05:03 +0100519 locations->SetOut(Location::RequiresRegister());
520 }
521}
522
523
Mark Mendell5f874182015-03-04 15:42:45 -0500524void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
525 // The DCHECKS below check that a register is not specified twice in
526 // the summary. The out location can overlap with an input, so we need
527 // to special case it.
528 if (location.IsRegister()) {
529 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
530 blocked_core_registers_[location.reg()] = true;
531 } else if (location.IsFpuRegister()) {
532 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
533 blocked_fpu_registers_[location.reg()] = true;
534 } else if (location.IsFpuRegisterPair()) {
535 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
536 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
537 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
538 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
539 } else if (location.IsRegisterPair()) {
540 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
541 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
542 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
543 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
544 }
545}
546
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000547void CodeGenerator::AllocateLocations(HInstruction* instruction) {
548 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100549 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000550 LocationSummary* locations = instruction->GetLocations();
551 if (!instruction->IsSuspendCheckEntry()) {
Aart Bikd1c40452016-03-02 16:06:13 -0800552 if (locations != nullptr) {
553 if (locations->CanCall()) {
554 MarkNotLeaf();
555 } else if (locations->Intrinsified() &&
556 instruction->IsInvokeStaticOrDirect() &&
557 !instruction->AsInvokeStaticOrDirect()->HasCurrentMethodInput()) {
558 // A static method call that has been fully intrinsified, and cannot call on the slow
559 // path or refer to the current method directly, no longer needs current method.
560 return;
561 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000562 }
563 if (instruction->NeedsCurrentMethod()) {
564 SetRequiresCurrentMethod();
565 }
566 }
567}
568
Serban Constantinescuecc43662015-08-13 13:33:12 +0100569void CodeGenerator::MaybeRecordStat(MethodCompilationStat compilation_stat, size_t count) const {
570 if (stats_ != nullptr) {
571 stats_->RecordStat(compilation_stat, count);
572 }
573}
574
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000575CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000576 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000577 const InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100578 const CompilerOptions& compiler_options,
579 OptimizingCompilerStats* stats) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000580 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -0700581#ifdef ART_ENABLE_CODEGEN_arm
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000582 case kArm:
583 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000584 return new arm::CodeGeneratorARM(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100585 *isa_features.AsArmInstructionSetFeatures(),
586 compiler_options,
587 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000588 }
Alex Light50fa9932015-08-10 15:30:07 -0700589#endif
590#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames5319def2014-10-23 10:03:10 +0100591 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000592 return new arm64::CodeGeneratorARM64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100593 *isa_features.AsArm64InstructionSetFeatures(),
594 compiler_options,
595 stats);
Alexandre Rames5319def2014-10-23 10:03:10 +0100596 }
Alex Light50fa9932015-08-10 15:30:07 -0700597#endif
598#ifdef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200599 case kMips: {
600 return new mips::CodeGeneratorMIPS(graph,
601 *isa_features.AsMipsInstructionSetFeatures(),
602 compiler_options,
603 stats);
604 }
Alex Light50fa9932015-08-10 15:30:07 -0700605#endif
606#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700607 case kMips64: {
608 return new mips64::CodeGeneratorMIPS64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100609 *isa_features.AsMips64InstructionSetFeatures(),
610 compiler_options,
611 stats);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700612 }
Alex Light50fa9932015-08-10 15:30:07 -0700613#endif
614#ifdef ART_ENABLE_CODEGEN_x86
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000615 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400616 return new x86::CodeGeneratorX86(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100617 *isa_features.AsX86InstructionSetFeatures(),
618 compiler_options,
619 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000620 }
Alex Light50fa9932015-08-10 15:30:07 -0700621#endif
622#ifdef ART_ENABLE_CODEGEN_x86_64
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700623 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400624 return new x86_64::CodeGeneratorX86_64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100625 *isa_features.AsX86_64InstructionSetFeatures(),
626 compiler_options,
627 stats);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700628 }
Alex Light50fa9932015-08-10 15:30:07 -0700629#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000630 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000631 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000632 }
633}
634
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000635size_t CodeGenerator::ComputeStackMapsSize() {
636 return stack_map_stream_.PrepareForFillIn();
637}
638
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000639static void CheckCovers(uint32_t dex_pc,
640 const HGraph& graph,
641 const CodeInfo& code_info,
642 const ArenaVector<HSuspendCheck*>& loop_headers,
643 ArenaVector<size_t>* covered) {
644 StackMapEncoding encoding = code_info.ExtractEncoding();
645 for (size_t i = 0; i < loop_headers.size(); ++i) {
646 if (loop_headers[i]->GetDexPc() == dex_pc) {
647 if (graph.IsCompilingOsr()) {
648 DCHECK(code_info.GetOsrStackMapForDexPc(dex_pc, encoding).IsValid());
649 }
650 ++(*covered)[i];
651 }
652 }
653}
654
655// Debug helper to ensure loop entries in compiled code are matched by
656// dex branch instructions.
657static void CheckLoopEntriesCanBeUsedForOsr(const HGraph& graph,
658 const CodeInfo& code_info,
659 const DexFile::CodeItem& code_item) {
660 if (graph.HasTryCatch()) {
661 // One can write loops through try/catch, which we do not support for OSR anyway.
662 return;
663 }
664 ArenaVector<HSuspendCheck*> loop_headers(graph.GetArena()->Adapter(kArenaAllocMisc));
665 for (HReversePostOrderIterator it(graph); !it.Done(); it.Advance()) {
666 if (it.Current()->IsLoopHeader()) {
667 HSuspendCheck* suspend_check = it.Current()->GetLoopInformation()->GetSuspendCheck();
668 if (!suspend_check->GetEnvironment()->IsFromInlinedInvoke()) {
669 loop_headers.push_back(suspend_check);
670 }
671 }
672 }
673 ArenaVector<size_t> covered(loop_headers.size(), 0, graph.GetArena()->Adapter(kArenaAllocMisc));
674 const uint16_t* code_ptr = code_item.insns_;
675 const uint16_t* code_end = code_item.insns_ + code_item.insns_size_in_code_units_;
676
677 size_t dex_pc = 0;
678 while (code_ptr < code_end) {
679 const Instruction& instruction = *Instruction::At(code_ptr);
680 if (instruction.IsBranch()) {
681 uint32_t target = dex_pc + instruction.GetTargetOffset();
682 CheckCovers(target, graph, code_info, loop_headers, &covered);
683 } else if (instruction.IsSwitch()) {
684 SwitchTable table(instruction, dex_pc, instruction.Opcode() == Instruction::SPARSE_SWITCH);
685 uint16_t num_entries = table.GetNumEntries();
686 size_t offset = table.GetFirstValueIndex();
687
688 // Use a larger loop counter type to avoid overflow issues.
689 for (size_t i = 0; i < num_entries; ++i) {
690 // The target of the case.
691 uint32_t target = dex_pc + table.GetEntryAt(i + offset);
692 CheckCovers(target, graph, code_info, loop_headers, &covered);
693 }
694 }
695 dex_pc += instruction.SizeInCodeUnits();
696 code_ptr += instruction.SizeInCodeUnits();
697 }
698
699 for (size_t i = 0; i < covered.size(); ++i) {
700 DCHECK_NE(covered[i], 0u) << "Loop in compiled code has no dex branch equivalent";
701 }
702}
703
704void CodeGenerator::BuildStackMaps(MemoryRegion region, const DexFile::CodeItem& code_item) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100705 stack_map_stream_.FillIn(region);
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000706 if (kIsDebugBuild) {
707 CheckLoopEntriesCanBeUsedForOsr(*graph_, CodeInfo(region), code_item);
708 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100709}
710
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000711void CodeGenerator::RecordPcInfo(HInstruction* instruction,
712 uint32_t dex_pc,
713 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000714 if (instruction != nullptr) {
Roland Levillain1693a1f2016-03-15 14:57:31 +0000715 // The code generated for some type conversions
Alexey Frunze4dda3372015-06-01 18:31:49 -0700716 // may call the runtime, thus normally requiring a subsequent
717 // call to this method. However, the method verifier does not
718 // produce PC information for certain instructions, which are
719 // considered "atomic" (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000720 // Therefore we do not currently record PC information for such
721 // instructions. As this may change later, we added this special
722 // case so that code generators may nevertheless call
723 // CodeGenerator::RecordPcInfo without triggering an error in
724 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
725 // thereafter.
Roland Levillain1693a1f2016-03-15 14:57:31 +0000726 if (instruction->IsTypeConversion()) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000727 return;
728 }
729 if (instruction->IsRem()) {
730 Primitive::Type type = instruction->AsRem()->GetResultType();
731 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
732 return;
733 }
734 }
Roland Levillain624279f2014-12-04 11:54:28 +0000735 }
736
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100737 uint32_t outer_dex_pc = dex_pc;
738 uint32_t outer_environment_size = 0;
739 uint32_t inlining_depth = 0;
740 if (instruction != nullptr) {
741 for (HEnvironment* environment = instruction->GetEnvironment();
742 environment != nullptr;
743 environment = environment->GetParent()) {
744 outer_dex_pc = environment->GetDexPc();
745 outer_environment_size = environment->Size();
746 if (environment != instruction->GetEnvironment()) {
747 inlining_depth++;
748 }
749 }
750 }
751
Nicolas Geoffray39468442014-09-02 15:17:15 +0100752 // Collect PC infos for the mapping table.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100753 uint32_t native_pc = GetAssembler()->CodeSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100754
Nicolas Geoffray39468442014-09-02 15:17:15 +0100755 if (instruction == nullptr) {
David Srbeckyc7098ff2016-02-09 14:30:11 +0000756 // For stack overflow checks and native-debug-info entries without dex register
757 // mapping (i.e. start of basic block or start of slow path).
Vladimir Markobd8c7252015-06-12 10:06:32 +0100758 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100759 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000760 return;
761 }
762 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100763
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000764 uint32_t register_mask = locations->GetRegisterMask();
765 if (locations->OnlyCallsOnSlowPath()) {
766 // In case of slow path, we currently set the location of caller-save registers
767 // to register (instead of their stack location when pushed before the slow-path
768 // call). Therefore register_mask contains both callee-save and caller-save
769 // registers that hold objects. We must remove the caller-save from the mask, since
770 // they will be overwritten by the callee.
771 register_mask &= core_callee_save_mask_;
772 }
773 // The register mask must be a subset of callee-save registers.
774 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100775 stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
776 native_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100777 register_mask,
778 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100779 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100780 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100781
782 EmitEnvironment(instruction->GetEnvironment(), slow_path);
783 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayb331feb2016-02-05 16:51:53 +0000784
785 HLoopInformation* info = instruction->GetBlock()->GetLoopInformation();
786 if (instruction->IsSuspendCheck() &&
787 (info != nullptr) &&
788 graph_->IsCompilingOsr() &&
789 (inlining_depth == 0)) {
790 DCHECK_EQ(info->GetSuspendCheck(), instruction);
791 // We duplicate the stack map as a marker that this stack map can be an OSR entry.
792 // Duplicating it avoids having the runtime recognize and skip an OSR stack map.
793 DCHECK(info->IsIrreducible());
794 stack_map_stream_.BeginStackMapEntry(
795 dex_pc, native_pc, register_mask, locations->GetStackMask(), outer_environment_size, 0);
796 EmitEnvironment(instruction->GetEnvironment(), slow_path);
797 stack_map_stream_.EndStackMapEntry();
798 if (kIsDebugBuild) {
799 HEnvironment* environment = instruction->GetEnvironment();
800 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
801 HInstruction* in_environment = environment->GetInstructionAt(i);
802 if (in_environment != nullptr) {
803 DCHECK(in_environment->IsPhi() || in_environment->IsConstant());
804 Location location = environment->GetLocationAt(i);
805 DCHECK(location.IsStackSlot() ||
806 location.IsDoubleStackSlot() ||
807 location.IsConstant() ||
808 location.IsInvalid());
809 if (location.IsStackSlot() || location.IsDoubleStackSlot()) {
810 DCHECK_LT(location.GetStackIndex(), static_cast<int32_t>(GetFrameSize()));
811 }
812 }
813 }
814 }
815 } else if (kIsDebugBuild) {
816 // Ensure stack maps are unique, by checking that the native pc in the stack map
817 // last emitted is different than the native pc of the stack map just emitted.
818 size_t number_of_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
819 if (number_of_stack_maps > 1) {
820 DCHECK_NE(stack_map_stream_.GetStackMap(number_of_stack_maps - 1).native_pc_offset,
821 stack_map_stream_.GetStackMap(number_of_stack_maps - 2).native_pc_offset);
822 }
823 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100824}
825
David Srbeckyb7070a22016-01-08 18:13:53 +0000826bool CodeGenerator::HasStackMapAtCurrentPc() {
827 uint32_t pc = GetAssembler()->CodeSize();
828 size_t count = stack_map_stream_.GetNumberOfStackMaps();
829 return count > 0 && stack_map_stream_.GetStackMap(count - 1).native_pc_offset == pc;
830}
831
David Srbeckyd28f4a02016-03-14 17:14:24 +0000832void CodeGenerator::MaybeRecordNativeDebugInfo(HInstruction* instruction,
833 uint32_t dex_pc,
834 SlowPathCode* slow_path) {
David Srbeckyc7098ff2016-02-09 14:30:11 +0000835 if (GetCompilerOptions().GetNativeDebuggable() && dex_pc != kNoDexPc) {
836 if (HasStackMapAtCurrentPc()) {
837 // Ensure that we do not collide with the stack map of the previous instruction.
838 GenerateNop();
839 }
David Srbeckyd28f4a02016-03-14 17:14:24 +0000840 RecordPcInfo(instruction, dex_pc, slow_path);
David Srbeckyc7098ff2016-02-09 14:30:11 +0000841 }
842}
843
David Brazdil77a48ae2015-09-15 12:34:04 +0000844void CodeGenerator::RecordCatchBlockInfo() {
845 ArenaAllocator* arena = graph_->GetArena();
846
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100847 for (HBasicBlock* block : *block_order_) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000848 if (!block->IsCatchBlock()) {
849 continue;
850 }
851
852 uint32_t dex_pc = block->GetDexPc();
853 uint32_t num_vregs = graph_->GetNumberOfVRegs();
854 uint32_t inlining_depth = 0; // Inlining of catch blocks is not supported at the moment.
855 uint32_t native_pc = GetAddressOf(block);
856 uint32_t register_mask = 0; // Not used.
857
858 // The stack mask is not used, so we leave it empty.
Vladimir Markof6a35de2016-03-21 12:01:50 +0000859 ArenaBitVector* stack_mask =
860 ArenaBitVector::Create(arena, 0, /* expandable */ true, kArenaAllocCodeGenerator);
David Brazdil77a48ae2015-09-15 12:34:04 +0000861
862 stack_map_stream_.BeginStackMapEntry(dex_pc,
863 native_pc,
864 register_mask,
865 stack_mask,
866 num_vregs,
867 inlining_depth);
868
869 HInstruction* current_phi = block->GetFirstPhi();
870 for (size_t vreg = 0; vreg < num_vregs; ++vreg) {
871 while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) {
872 HInstruction* next_phi = current_phi->GetNext();
873 DCHECK(next_phi == nullptr ||
874 current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber())
875 << "Phis need to be sorted by vreg number to keep this a linear-time loop.";
876 current_phi = next_phi;
877 }
878
879 if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) {
880 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
881 } else {
882 Location location = current_phi->GetLiveInterval()->ToLocation();
883 switch (location.GetKind()) {
884 case Location::kStackSlot: {
885 stack_map_stream_.AddDexRegisterEntry(
886 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
887 break;
888 }
889 case Location::kDoubleStackSlot: {
890 stack_map_stream_.AddDexRegisterEntry(
891 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
892 stack_map_stream_.AddDexRegisterEntry(
893 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
894 ++vreg;
895 DCHECK_LT(vreg, num_vregs);
896 break;
897 }
898 default: {
899 // All catch phis must be allocated to a stack slot.
900 LOG(FATAL) << "Unexpected kind " << location.GetKind();
901 UNREACHABLE();
902 }
903 }
904 }
905 }
906
907 stack_map_stream_.EndStackMapEntry();
908 }
909}
910
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100911void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
912 if (environment == nullptr) return;
913
914 if (environment->GetParent() != nullptr) {
915 // We emit the parent environment first.
916 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100917 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
918 environment->GetDexPc(),
919 environment->GetInvokeType(),
920 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100921 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000922
923 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100924 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000925 HInstruction* current = environment->GetInstructionAt(i);
926 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100927 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000928 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100929 }
930
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100931 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000932 switch (location.GetKind()) {
933 case Location::kConstant: {
934 DCHECK_EQ(current, location.GetConstant());
935 if (current->IsLongConstant()) {
936 int64_t value = current->AsLongConstant()->GetValue();
937 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100938 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000939 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100940 DexRegisterLocation::Kind::kConstant, High32Bits(value));
941 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000942 DCHECK_LT(i, environment_size);
943 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000944 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000945 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100946 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000947 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100948 DexRegisterLocation::Kind::kConstant, High32Bits(value));
949 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000950 DCHECK_LT(i, environment_size);
951 } else if (current->IsIntConstant()) {
952 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100953 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000954 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100955 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000956 } else {
957 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000958 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100959 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000960 }
961 break;
962 }
963
964 case Location::kStackSlot: {
965 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100966 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000967 break;
968 }
969
970 case Location::kDoubleStackSlot: {
971 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100972 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000973 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100974 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
975 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000976 DCHECK_LT(i, environment_size);
977 break;
978 }
979
980 case Location::kRegister : {
981 int id = location.reg();
982 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
983 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100984 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000985 if (current->GetType() == Primitive::kPrimLong) {
986 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100987 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
988 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000989 DCHECK_LT(i, environment_size);
990 }
991 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100992 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000993 if (current->GetType() == Primitive::kPrimLong) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100994 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100995 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000996 DCHECK_LT(i, environment_size);
997 }
998 }
999 break;
1000 }
1001
1002 case Location::kFpuRegister : {
1003 int id = location.reg();
1004 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
1005 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001006 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001007 if (current->GetType() == Primitive::kPrimDouble) {
1008 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001009 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
1010 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001011 DCHECK_LT(i, environment_size);
1012 }
1013 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001014 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001015 if (current->GetType() == Primitive::kPrimDouble) {
David Brazdild9cb68e2015-08-25 13:52:43 +01001016 stack_map_stream_.AddDexRegisterEntry(
1017 DexRegisterLocation::Kind::kInFpuRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001018 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001019 DCHECK_LT(i, environment_size);
1020 }
1021 }
1022 break;
1023 }
1024
1025 case Location::kFpuRegisterPair : {
1026 int low = location.low();
1027 int high = location.high();
1028 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
1029 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(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::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001033 }
1034 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
1035 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001036 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1037 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001038 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001039 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
1040 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001041 }
1042 DCHECK_LT(i, environment_size);
1043 break;
1044 }
1045
1046 case Location::kRegisterPair : {
1047 int low = location.low();
1048 int high = location.high();
1049 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
1050 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001051 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001052 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001053 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001054 }
1055 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
1056 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001057 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001058 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001059 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001060 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001061 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001062 DCHECK_LT(i, environment_size);
1063 break;
1064 }
1065
1066 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001067 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001068 break;
1069 }
1070
1071 default:
1072 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1073 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001074 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001075
1076 if (environment->GetParent() != nullptr) {
1077 stack_map_stream_.EndInlineInfoEntry();
1078 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001079}
1080
David Brazdil77a48ae2015-09-15 12:34:04 +00001081bool CodeGenerator::IsImplicitNullCheckAllowed(HNullCheck* null_check) const {
1082 return compiler_options_.GetImplicitNullChecks() &&
1083 // Null checks which might throw into a catch block need to save live
1084 // registers and therefore cannot be done implicitly.
1085 !null_check->CanThrowIntoCatchBlock();
1086}
1087
Calin Juravle77520bc2015-01-12 18:45:46 +00001088bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
1089 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +01001090
1091 return (first_next_not_move != nullptr)
1092 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00001093}
1094
1095void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
1096 // If we are from a static path don't record the pc as we can't throw NPE.
1097 // NB: having the checks here makes the code much less verbose in the arch
1098 // specific code generators.
1099 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
1100 return;
1101 }
1102
Calin Juravle641547a2015-04-21 22:08:51 +01001103 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001104 return;
1105 }
1106
1107 // Find the first previous instruction which is not a move.
1108 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
1109
1110 // If the instruction is a null check it means that `instr` is the first user
1111 // and needs to record the pc.
1112 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
1113 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
David Brazdil77a48ae2015-09-15 12:34:04 +00001114 if (IsImplicitNullCheckAllowed(null_check)) {
1115 // TODO: The parallel moves modify the environment. Their changes need to be
1116 // reverted otherwise the stack maps at the throw point will not be correct.
1117 RecordPcInfo(null_check, null_check->GetDexPc());
1118 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001119 }
1120}
1121
Calin Juravle2ae48182016-03-16 14:05:09 +00001122void CodeGenerator::GenerateNullCheck(HNullCheck* instruction) {
1123 if (IsImplicitNullCheckAllowed(instruction)) {
1124 MaybeRecordStat(kImplicitNullCheckGenerated);
1125 GenerateImplicitNullCheck(instruction);
1126 } else {
1127 MaybeRecordStat(kExplicitNullCheckGenerated);
1128 GenerateExplicitNullCheck(instruction);
1129 }
1130}
1131
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001132void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
1133 LocationSummary* locations = suspend_check->GetLocations();
1134 HBasicBlock* block = suspend_check->GetBlock();
1135 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
1136 DCHECK(block->IsLoopHeader());
1137
1138 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1139 HInstruction* current = it.Current();
1140 LiveInterval* interval = current->GetLiveInterval();
1141 // We only need to clear bits of loop phis containing objects and allocated in register.
1142 // Loop phis allocated on stack already have the object in the stack.
1143 if (current->GetType() == Primitive::kPrimNot
1144 && interval->HasRegister()
1145 && interval->HasSpillSlot()) {
1146 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
1147 }
1148 }
1149}
1150
Nicolas Geoffray90218252015-04-15 11:56:51 +01001151void CodeGenerator::EmitParallelMoves(Location from1,
1152 Location to1,
1153 Primitive::Type type1,
1154 Location from2,
1155 Location to2,
1156 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001157 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +01001158 parallel_move.AddMove(from1, to1, type1, nullptr);
1159 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001160 GetMoveResolver()->EmitNativeCode(&parallel_move);
1161}
1162
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001163void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) {
1164 // Ensure that the call kind indication given to the register allocator is
1165 // coherent with the runtime call generated, and that the GC side effect is
1166 // set when required.
1167 if (slow_path == nullptr) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001168 DCHECK(instruction->GetLocations()->WillCall())
1169 << "instruction->DebugName()=" << instruction->DebugName();
Roland Levillaindf3f8222015-08-13 12:31:44 +01001170 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()))
Roland Levillain0d5a2812015-11-13 10:07:31 +00001171 << "instruction->DebugName()=" << instruction->DebugName()
1172 << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001173 } else {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001174 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal())
Roland Levillain0d5a2812015-11-13 10:07:31 +00001175 << "instruction->DebugName()=" << instruction->DebugName()
1176 << " slow_path->GetDescription()=" << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001177 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) ||
Roland Levillain0d5a2812015-11-13 10:07:31 +00001178 // When read barriers are enabled, some instructions use a
1179 // slow path to emit a read barrier, which does not trigger
1180 // GC, is not fatal, nor is emitted by HDeoptimize
1181 // instructions.
1182 (kEmitCompilerReadBarrier &&
1183 (instruction->IsInstanceFieldGet() ||
1184 instruction->IsStaticFieldGet() ||
1185 instruction->IsArraySet() ||
1186 instruction->IsArrayGet() ||
1187 instruction->IsLoadClass() ||
1188 instruction->IsLoadString() ||
1189 instruction->IsInstanceOf() ||
1190 instruction->IsCheckCast())))
1191 << "instruction->DebugName()=" << instruction->DebugName()
1192 << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString()
1193 << " slow_path->GetDescription()=" << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001194 }
1195
1196 // Check the coherency of leaf information.
1197 DCHECK(instruction->IsSuspendCheck()
1198 || ((slow_path != nullptr) && slow_path->IsFatal())
1199 || instruction->GetLocations()->CanCall()
Roland Levillaindf3f8222015-08-13 12:31:44 +01001200 || !IsLeafMethod())
1201 << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : "");
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001202}
1203
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001204void SlowPathCode::SaveLiveRegisters(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 // If the register holds an object, update the stack mask.
1212 if (locations->RegisterContainsObject(i)) {
1213 locations->SetStackBit(stack_offset / kVRegSize);
1214 }
1215 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001216 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1217 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001218 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
1219 }
1220 }
1221 }
1222
1223 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1224 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001225 if (live_registers->ContainsFloatingPointRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001226 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001227 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1228 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001229 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
1230 }
1231 }
1232 }
1233}
1234
1235void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001236 RegisterSet* live_registers = locations->GetLiveRegisters();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001237 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001238
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001239 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1240 if (!codegen->IsCoreCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001241 if (live_registers->ContainsCoreRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001242 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Roland Levillain0d5a2812015-11-13 10:07:31 +00001243 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001244 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1245 }
1246 }
1247 }
1248
1249 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1250 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001251 if (live_registers->ContainsFloatingPointRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001252 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Roland Levillain0d5a2812015-11-13 10:07:31 +00001253 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001254 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1255 }
1256 }
1257 }
1258}
1259
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001260void CodeGenerator::CreateSystemArrayCopyLocationSummary(HInvoke* invoke) {
1261 // Check to see if we have known failures that will cause us to have to bail out
1262 // to the runtime, and just generate the runtime call directly.
1263 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1264 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
1265
1266 // The positions must be non-negative.
1267 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
1268 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
1269 // We will have to fail anyways.
1270 return;
1271 }
1272
1273 // The length must be >= 0.
1274 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1275 if (length != nullptr) {
1276 int32_t len = length->GetValue();
1277 if (len < 0) {
1278 // Just call as normal.
1279 return;
1280 }
1281 }
1282
1283 SystemArrayCopyOptimizations optimizations(invoke);
1284
1285 if (optimizations.GetDestinationIsSource()) {
1286 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
1287 // We only support backward copying if source and destination are the same.
1288 return;
1289 }
1290 }
1291
1292 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
1293 // We currently don't intrinsify primitive copying.
1294 return;
1295 }
1296
1297 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
1298 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1299 LocationSummary::kCallOnSlowPath,
1300 kIntrinsified);
1301 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
1302 locations->SetInAt(0, Location::RequiresRegister());
1303 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1304 locations->SetInAt(2, Location::RequiresRegister());
1305 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
1306 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
1307
1308 locations->AddTemp(Location::RequiresRegister());
1309 locations->AddTemp(Location::RequiresRegister());
1310 locations->AddTemp(Location::RequiresRegister());
1311}
1312
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001313} // namespace art