blob: 3bbff6ae1748a06ba4f6f69ccdd3bfc28502da6c [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
35#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -070036#include "code_generator_mips64.h"
Alex Light50fa9932015-08-10 15:30:07 -070037#endif
38
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070039#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000040#include "dex/verified_method.h"
41#include "driver/dex_compilation_unit.h"
42#include "gc_map_builder.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010043#include "graph_visualizer.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000044#include "leb128.h"
45#include "mapping_table.h"
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010046#include "mirror/array-inl.h"
47#include "mirror/object_array-inl.h"
48#include "mirror/object_reference.h"
Alex Light50fa9932015-08-10 15:30:07 -070049#include "parallel_move_resolver.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010050#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000051#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000052#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000053#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000054
55namespace art {
56
Alexandre Rames88c13cd2015-04-14 17:35:39 +010057// Return whether a location is consistent with a type.
58static bool CheckType(Primitive::Type type, Location location) {
59 if (location.IsFpuRegister()
60 || (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresFpuRegister))) {
61 return (type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble);
62 } else if (location.IsRegister() ||
63 (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresRegister))) {
64 return Primitive::IsIntegralType(type) || (type == Primitive::kPrimNot);
65 } else if (location.IsRegisterPair()) {
66 return type == Primitive::kPrimLong;
67 } else if (location.IsFpuRegisterPair()) {
68 return type == Primitive::kPrimDouble;
69 } else if (location.IsStackSlot()) {
70 return (Primitive::IsIntegralType(type) && type != Primitive::kPrimLong)
71 || (type == Primitive::kPrimFloat)
72 || (type == Primitive::kPrimNot);
73 } else if (location.IsDoubleStackSlot()) {
74 return (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
75 } else if (location.IsConstant()) {
76 if (location.GetConstant()->IsIntConstant()) {
77 return Primitive::IsIntegralType(type) && (type != Primitive::kPrimLong);
78 } else if (location.GetConstant()->IsNullConstant()) {
79 return type == Primitive::kPrimNot;
80 } else if (location.GetConstant()->IsLongConstant()) {
81 return type == Primitive::kPrimLong;
82 } else if (location.GetConstant()->IsFloatConstant()) {
83 return type == Primitive::kPrimFloat;
84 } else {
85 return location.GetConstant()->IsDoubleConstant()
86 && (type == Primitive::kPrimDouble);
87 }
88 } else {
89 return location.IsInvalid() || (location.GetPolicy() == Location::kAny);
90 }
91}
92
93// Check that a location summary is consistent with an instruction.
94static bool CheckTypeConsistency(HInstruction* instruction) {
95 LocationSummary* locations = instruction->GetLocations();
96 if (locations == nullptr) {
97 return true;
98 }
99
100 if (locations->Out().IsUnallocated()
101 && (locations->Out().GetPolicy() == Location::kSameAsFirstInput)) {
102 DCHECK(CheckType(instruction->GetType(), locations->InAt(0)))
103 << instruction->GetType()
104 << " " << locations->InAt(0);
105 } else {
106 DCHECK(CheckType(instruction->GetType(), locations->Out()))
107 << instruction->GetType()
108 << " " << locations->Out();
109 }
110
111 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
112 DCHECK(CheckType(instruction->InputAt(i)->GetType(), locations->InAt(i)))
113 << instruction->InputAt(i)->GetType()
114 << " " << locations->InAt(i);
115 }
116
117 HEnvironment* environment = instruction->GetEnvironment();
118 for (size_t i = 0; i < instruction->EnvironmentSize(); ++i) {
119 if (environment->GetInstructionAt(i) != nullptr) {
120 Primitive::Type type = environment->GetInstructionAt(i)->GetType();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100121 DCHECK(CheckType(type, environment->GetLocationAt(i)))
122 << type << " " << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100123 } else {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100124 DCHECK(environment->GetLocationAt(i).IsInvalid())
125 << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100126 }
127 }
128 return true;
129}
130
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100131size_t CodeGenerator::GetCacheOffset(uint32_t index) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100132 return sizeof(GcRoot<mirror::Object>) * index;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100133}
134
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135size_t CodeGenerator::GetCachePointerOffset(uint32_t index) {
136 auto pointer_size = InstructionSetPointerSize(GetInstructionSet());
Vladimir Marko05792b92015-08-03 11:56:49 +0100137 return pointer_size * index;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700138}
139
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100140void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000141 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100142 if (!is_leaf) {
143 MarkNotLeaf();
144 }
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700145 const bool is_64_bit = Is64BitInstructionSet(GetInstructionSet());
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000146 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
147 + GetGraph()->GetTemporariesVRegSlots()
148 + 1 /* filler */,
149 0, /* the baseline compiler does not have live registers at slow path */
150 0, /* the baseline compiler does not have live registers at slow path */
151 GetGraph()->GetMaximumNumberOfOutVRegs()
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700152 + (is_64_bit ? 2 : 1) /* current method */,
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000153 GetGraph()->GetBlocks());
154 CompileInternal(allocator, /* is_baseline */ true);
155}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100156
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000157bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
158 DCHECK_EQ(block_order_->Get(current_block_index_), current);
159 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
160}
161
162HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
163 for (size_t i = current_block_index_ + 1; i < block_order_->Size(); ++i) {
164 HBasicBlock* block = block_order_->Get(i);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000165 if (!block->IsSingleJump()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000166 return block;
167 }
168 }
169 return nullptr;
170}
171
172HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000173 while (block->IsSingleJump()) {
Vladimir Marko60584552015-09-03 13:35:12 +0000174 block = block->GetSuccessor(0);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000175 }
176 return block;
177}
178
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100179class DisassemblyScope {
180 public:
181 DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen)
182 : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) {
183 if (codegen_.GetDisassemblyInformation() != nullptr) {
184 start_offset_ = codegen_.GetAssembler().CodeSize();
185 }
186 }
187
188 ~DisassemblyScope() {
189 // We avoid building this data when we know it will not be used.
190 if (codegen_.GetDisassemblyInformation() != nullptr) {
191 codegen_.GetDisassemblyInformation()->AddInstructionInterval(
192 instruction_, start_offset_, codegen_.GetAssembler().CodeSize());
193 }
194 }
195
196 private:
197 const CodeGenerator& codegen_;
198 HInstruction* instruction_;
199 size_t start_offset_;
200};
201
202
203void CodeGenerator::GenerateSlowPaths() {
204 size_t code_start = 0;
205 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
206 if (disasm_info_ != nullptr) {
207 code_start = GetAssembler()->CodeSize();
208 }
209 slow_paths_.Get(i)->EmitNativeCode(this);
210 if (disasm_info_ != nullptr) {
211 disasm_info_->AddSlowPathInterval(slow_paths_.Get(i), code_start, GetAssembler()->CodeSize());
212 }
213 }
214}
215
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000216void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Roland Levillain3e3d7332015-04-28 11:00:54 +0100217 is_baseline_ = is_baseline;
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100218 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000219 DCHECK_EQ(current_block_index_, 0u);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100220
221 size_t frame_start = GetAssembler()->CodeSize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000222 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100223 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100224 if (disasm_info_ != nullptr) {
225 disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize());
226 }
227
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000228 for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) {
229 HBasicBlock* block = block_order_->Get(current_block_index_);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000230 // Don't generate code for an empty block. Its predecessors will branch to its successor
231 // directly. Also, the label of that block will not be emitted, so this helps catch
232 // errors where we reference that label.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000233 if (block->IsSingleJump()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100234 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100235 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
236 HInstruction* current = it.Current();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100237 DisassemblyScope disassembly_scope(current, *this);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000238 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000239 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000240 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100241 DCHECK(CheckTypeConsistency(current));
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600242 uintptr_t native_pc_begin = GetAssembler()->CodeSize();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100243 current->Accept(instruction_visitor);
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600244 uintptr_t native_pc_end = GetAssembler()->CodeSize();
245 RecordNativeDebugInfo(current->GetDexPc(), native_pc_begin, native_pc_end);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100246 }
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.
253 if (!is_baseline && graph_->HasTryCatch()) {
254 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
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100261void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000262 // The register allocator already called `InitializeCodeGeneration`,
263 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000264 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100265 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000266 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000267}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100268
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000269void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100270 size_t code_size = GetAssembler()->CodeSize();
271 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000272
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100273 MemoryRegion code(buffer, code_size);
274 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000275}
276
Vladimir Marko58155012015-08-19 12:49:41 +0000277void CodeGenerator::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches ATTRIBUTE_UNUSED) {
278 // No linker patches by default.
279}
280
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100281size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
282 for (size_t i = 0; i < length; ++i) {
283 if (!array[i]) {
284 array[i] = true;
285 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100286 }
287 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100288 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000289 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000290}
291
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000292size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
293 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000294 if (!array[i] && !array[i + 1]) {
295 array[i] = true;
296 array[i + 1] = true;
297 return i;
298 }
299 }
300 LOG(FATAL) << "Could not find a register in baseline register allocator";
301 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100302}
303
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000304void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
305 size_t maximum_number_of_live_core_registers,
306 size_t maximum_number_of_live_fp_registers,
307 size_t number_of_out_slots,
308 const GrowableArray<HBasicBlock*>& block_order) {
309 block_order_ = &block_order;
310 DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock());
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000311 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100312 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
313
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000314 if (number_of_spill_slots == 0
315 && !HasAllocatedCalleeSaveRegisters()
316 && IsLeafMethod()
317 && !RequiresCurrentMethod()) {
318 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
319 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
320 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
321 } else {
322 SetFrameSize(RoundUp(
323 number_of_spill_slots * kVRegSize
324 + number_of_out_slots * kVRegSize
325 + maximum_number_of_live_core_registers * GetWordSize()
326 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
327 + FrameEntrySpillSize(),
328 kStackAlignment));
329 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100330}
331
332Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
333 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000334 // The type of the previous instruction tells us if we need a single or double stack slot.
335 Primitive::Type type = temp->GetType();
336 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100337 // Use the temporary region (right below the dex registers).
338 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
339 - kVRegSize // filler
340 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000341 - ((temp_size + temp->GetIndex()) * kVRegSize);
342 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100343}
344
345int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
346 uint16_t reg_number = local->GetRegNumber();
347 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
348 if (reg_number >= number_of_locals) {
349 // Local is a parameter of the method. It is stored in the caller's frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350 // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode.
351 return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100352 + (reg_number - number_of_locals) * kVRegSize;
353 } else {
354 // Local is a temporary in this method. It is stored in this method's frame.
355 return GetFrameSize() - FrameEntrySpillSize()
356 - kVRegSize // filler.
357 - (number_of_locals * kVRegSize)
358 + (reg_number * kVRegSize);
359 }
360}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100361
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100362void CodeGenerator::CreateCommonInvokeLocationSummary(
Nicolas Geoffray4e40c262015-06-03 12:02:38 +0100363 HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100364 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
365 LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100366
367 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
368 HInstruction* input = invoke->InputAt(i);
369 locations->SetInAt(i, visitor->GetNextLocation(input->GetType()));
370 }
371
372 locations->SetOut(visitor->GetReturnLocation(invoke->GetType()));
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100373
374 if (invoke->IsInvokeStaticOrDirect()) {
375 HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect();
376 if (call->IsStringInit()) {
377 locations->AddTemp(visitor->GetMethodLocation());
378 } else if (call->IsRecursive()) {
379 locations->SetInAt(call->GetCurrentMethodInputIndex(), visitor->GetMethodLocation());
380 } else {
381 locations->AddTemp(visitor->GetMethodLocation());
382 locations->SetInAt(call->GetCurrentMethodInputIndex(), Location::RequiresRegister());
383 }
384 } else {
385 locations->AddTemp(visitor->GetMethodLocation());
386 }
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100387}
388
Mark Mendell5f874182015-03-04 15:42:45 -0500389void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
390 // The DCHECKS below check that a register is not specified twice in
391 // the summary. The out location can overlap with an input, so we need
392 // to special case it.
393 if (location.IsRegister()) {
394 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
395 blocked_core_registers_[location.reg()] = true;
396 } else if (location.IsFpuRegister()) {
397 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
398 blocked_fpu_registers_[location.reg()] = true;
399 } else if (location.IsFpuRegisterPair()) {
400 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
401 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
402 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
403 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
404 } else if (location.IsRegisterPair()) {
405 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
406 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
407 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
408 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
409 }
410}
411
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100412void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
413 LocationSummary* locations = instruction->GetLocations();
414 if (locations == nullptr) return;
415
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100416 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
417 blocked_core_registers_[i] = false;
418 }
419
420 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
421 blocked_fpu_registers_[i] = false;
422 }
423
424 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
425 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100426 }
427
428 // Mark all fixed input, temp and output registers as used.
429 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500430 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100431 }
432
433 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
434 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500435 BlockIfInRegister(loc);
436 }
437 Location result_location = locations->Out();
438 if (locations->OutputCanOverlapWithInputs()) {
439 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100440 }
441
Mark Mendell5f874182015-03-04 15:42:45 -0500442 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100443
444 // Allocate all unallocated input locations.
445 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
446 Location loc = locations->InAt(i);
447 HInstruction* input = instruction->InputAt(i);
448 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100449 if ((loc.GetPolicy() == Location::kRequiresRegister)
450 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100451 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100452 } else {
453 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
454 HLoadLocal* load = input->AsLoadLocal();
455 if (load != nullptr) {
456 loc = GetStackLocation(load);
457 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100458 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100459 }
460 }
461 locations->SetInAt(i, loc);
462 }
463 }
464
465 // Allocate all unallocated temp locations.
466 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
467 Location loc = locations->GetTemp(i);
468 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000469 switch (loc.GetPolicy()) {
470 case Location::kRequiresRegister:
471 // Allocate a core register (large enough to fit a 32-bit integer).
472 loc = AllocateFreeRegister(Primitive::kPrimInt);
473 break;
474
475 case Location::kRequiresFpuRegister:
476 // Allocate a core register (large enough to fit a 64-bit double).
477 loc = AllocateFreeRegister(Primitive::kPrimDouble);
478 break;
479
480 default:
481 LOG(FATAL) << "Unexpected policy for temporary location "
482 << loc.GetPolicy();
483 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100484 locations->SetTempAt(i, loc);
485 }
486 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100487 if (result_location.IsUnallocated()) {
488 switch (result_location.GetPolicy()) {
489 case Location::kAny:
490 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100491 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100492 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100493 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100494 case Location::kSameAsFirstInput:
495 result_location = locations->InAt(0);
496 break;
497 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000498 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100499 }
500}
501
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000502void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
503 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100504 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100505 if (instruction->IsTemporary()) {
506 HInstruction* previous = instruction->GetPrevious();
507 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
508 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100509 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100510 return;
511 }
512 AllocateRegistersLocally(instruction);
513 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000514 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000515 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000516 if (location.IsValid()) {
517 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000518 if (input->GetNext()->IsTemporary()) {
519 // If the input was stored in a temporary, use that temporary to
520 // perform the move.
521 Move(input->GetNext(), location, instruction);
522 } else {
523 Move(input, location, instruction);
524 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000525 }
526 }
527}
528
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000529void CodeGenerator::AllocateLocations(HInstruction* instruction) {
530 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100531 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000532 LocationSummary* locations = instruction->GetLocations();
533 if (!instruction->IsSuspendCheckEntry()) {
534 if (locations != nullptr && locations->CanCall()) {
535 MarkNotLeaf();
536 }
537 if (instruction->NeedsCurrentMethod()) {
538 SetRequiresCurrentMethod();
539 }
540 }
541}
542
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000543CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000544 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000545 const InstructionSetFeatures& isa_features,
546 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000547 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -0700548#ifdef ART_ENABLE_CODEGEN_arm
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000549 case kArm:
550 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000551 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000552 *isa_features.AsArmInstructionSetFeatures(),
553 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000554 }
Alex Light50fa9932015-08-10 15:30:07 -0700555#endif
556#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames5319def2014-10-23 10:03:10 +0100557 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000558 return new arm64::CodeGeneratorARM64(graph,
559 *isa_features.AsArm64InstructionSetFeatures(),
560 compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100561 }
Alex Light50fa9932015-08-10 15:30:07 -0700562#endif
563#ifdef ART_ENABLE_CODEGEN_mips
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000564 case kMips:
Alex Light50fa9932015-08-10 15:30:07 -0700565 UNUSED(compiler_options);
566 UNUSED(graph);
567 UNUSED(isa_features);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000568 return nullptr;
Alex Light50fa9932015-08-10 15:30:07 -0700569#endif
570#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700571 case kMips64: {
572 return new mips64::CodeGeneratorMIPS64(graph,
573 *isa_features.AsMips64InstructionSetFeatures(),
574 compiler_options);
575 }
Alex Light50fa9932015-08-10 15:30:07 -0700576#endif
577#ifdef ART_ENABLE_CODEGEN_x86
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000578 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400579 return new x86::CodeGeneratorX86(graph,
580 *isa_features.AsX86InstructionSetFeatures(),
581 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000582 }
Alex Light50fa9932015-08-10 15:30:07 -0700583#endif
584#ifdef ART_ENABLE_CODEGEN_x86_64
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700585 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400586 return new x86_64::CodeGeneratorX86_64(graph,
587 *isa_features.AsX86_64InstructionSetFeatures(),
588 compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700589 }
Alex Light50fa9932015-08-10 15:30:07 -0700590#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000591 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000592 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000593 }
594}
595
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000596void CodeGenerator::BuildNativeGCMap(
Vladimir Markof9f64412015-09-02 14:05:49 +0100597 ArenaVector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000598 const std::vector<uint8_t>& gc_map_raw =
599 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
600 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
601
Vladimir Markobd8c7252015-06-12 10:06:32 +0100602 uint32_t max_native_offset = stack_map_stream_.ComputeMaxNativePcOffset();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000603
Vladimir Markobd8c7252015-06-12 10:06:32 +0100604 size_t num_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
605 GcMapBuilder builder(data, num_stack_maps, max_native_offset, dex_gc_map.RegWidth());
606 for (size_t i = 0; i != num_stack_maps; ++i) {
607 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
608 uint32_t native_offset = stack_map_entry.native_pc_offset;
609 uint32_t dex_pc = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000610 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800611 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000612 builder.AddEntry(native_offset, references);
613 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000614}
615
Vladimir Markof9f64412015-09-02 14:05:49 +0100616void CodeGenerator::BuildMappingTable(ArenaVector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000617 uint32_t pc2dex_data_size = 0u;
Vladimir Markobd8c7252015-06-12 10:06:32 +0100618 uint32_t pc2dex_entries = stack_map_stream_.GetNumberOfStackMaps();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000619 uint32_t pc2dex_offset = 0u;
620 int32_t pc2dex_dalvik_offset = 0;
621 uint32_t dex2pc_data_size = 0u;
622 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000623 uint32_t dex2pc_offset = 0u;
624 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000625
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000626 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100627 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
628 pc2dex_data_size += UnsignedLeb128Size(stack_map_entry.native_pc_offset - pc2dex_offset);
629 pc2dex_data_size += SignedLeb128Size(stack_map_entry.dex_pc - pc2dex_dalvik_offset);
630 pc2dex_offset = stack_map_entry.native_pc_offset;
631 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000632 }
633
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000634 // Walk over the blocks and find which ones correspond to catch block entries.
635 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
636 HBasicBlock* block = graph_->GetBlocks().Get(i);
637 if (block->IsCatchBlock()) {
638 intptr_t native_pc = GetAddressOf(block);
639 ++dex2pc_entries;
640 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
641 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
642 dex2pc_offset = native_pc;
643 dex2pc_dalvik_offset = block->GetDexPc();
644 }
645 }
646
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000647 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
648 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
649 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
650 data->resize(data_size);
651
652 uint8_t* data_ptr = &(*data)[0];
653 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000654
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000655 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
656 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
657 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
658 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
659
660 pc2dex_offset = 0u;
661 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000662 dex2pc_offset = 0u;
663 dex2pc_dalvik_offset = 0u;
664
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000665 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100666 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
667 DCHECK(pc2dex_offset <= stack_map_entry.native_pc_offset);
668 write_pos = EncodeUnsignedLeb128(write_pos, stack_map_entry.native_pc_offset - pc2dex_offset);
669 write_pos = EncodeSignedLeb128(write_pos, stack_map_entry.dex_pc - pc2dex_dalvik_offset);
670 pc2dex_offset = stack_map_entry.native_pc_offset;
671 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000672 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000673
674 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
675 HBasicBlock* block = graph_->GetBlocks().Get(i);
676 if (block->IsCatchBlock()) {
677 intptr_t native_pc = GetAddressOf(block);
678 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
679 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
680 dex2pc_offset = native_pc;
681 dex2pc_dalvik_offset = block->GetDexPc();
682 }
683 }
684
685
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000686 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
687 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
688
689 if (kIsDebugBuild) {
690 // Verify the encoded table holds the expected data.
691 MappingTable table(data_ptr);
692 CHECK_EQ(table.TotalSize(), total_entries);
693 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
694 auto it = table.PcToDexBegin();
695 auto it2 = table.DexToPcBegin();
696 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100697 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
698 CHECK_EQ(stack_map_entry.native_pc_offset, it.NativePcOffset());
699 CHECK_EQ(stack_map_entry.dex_pc, it.DexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000700 ++it;
701 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000702 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
703 HBasicBlock* block = graph_->GetBlocks().Get(i);
704 if (block->IsCatchBlock()) {
705 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
706 CHECK_EQ(block->GetDexPc(), it2.DexPc());
707 ++it2;
708 }
709 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000710 CHECK(it == table.PcToDexEnd());
711 CHECK(it2 == table.DexToPcEnd());
712 }
713}
714
Vladimir Markof9f64412015-09-02 14:05:49 +0100715void CodeGenerator::BuildVMapTable(ArenaVector<uint8_t>* data) const {
716 Leb128Encoder<ArenaAllocatorAdapter<uint8_t>> vmap_encoder(data);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100717 // We currently don't use callee-saved registers.
718 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000719 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
720 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000721 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000722}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000723
Vladimir Markof9f64412015-09-02 14:05:49 +0100724void CodeGenerator::BuildStackMaps(ArenaVector<uint8_t>* data) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100725 uint32_t size = stack_map_stream_.PrepareForFillIn();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100726 data->resize(size);
727 MemoryRegion region(data->data(), size);
728 stack_map_stream_.FillIn(region);
729}
730
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600731void CodeGenerator::RecordNativeDebugInfo(uint32_t dex_pc,
732 uintptr_t native_pc_begin,
733 uintptr_t native_pc_end) {
734 if (src_map_ != nullptr && dex_pc != kNoDexPc && native_pc_begin != native_pc_end) {
735 src_map_->push_back(SrcMapElem({static_cast<uint32_t>(native_pc_begin),
736 static_cast<int32_t>(dex_pc)}));
737 }
738}
739
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000740void CodeGenerator::RecordPcInfo(HInstruction* instruction,
741 uint32_t dex_pc,
742 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000743 if (instruction != nullptr) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700744 // The code generated for some type conversions and comparisons
745 // may call the runtime, thus normally requiring a subsequent
746 // call to this method. However, the method verifier does not
747 // produce PC information for certain instructions, which are
748 // considered "atomic" (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000749 // Therefore we do not currently record PC information for such
750 // instructions. As this may change later, we added this special
751 // case so that code generators may nevertheless call
752 // CodeGenerator::RecordPcInfo without triggering an error in
753 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
754 // thereafter.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700755 if (instruction->IsTypeConversion() || instruction->IsCompare()) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000756 return;
757 }
758 if (instruction->IsRem()) {
759 Primitive::Type type = instruction->AsRem()->GetResultType();
760 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
761 return;
762 }
763 }
Roland Levillain624279f2014-12-04 11:54:28 +0000764 }
765
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100766 uint32_t outer_dex_pc = dex_pc;
767 uint32_t outer_environment_size = 0;
768 uint32_t inlining_depth = 0;
769 if (instruction != nullptr) {
770 for (HEnvironment* environment = instruction->GetEnvironment();
771 environment != nullptr;
772 environment = environment->GetParent()) {
773 outer_dex_pc = environment->GetDexPc();
774 outer_environment_size = environment->Size();
775 if (environment != instruction->GetEnvironment()) {
776 inlining_depth++;
777 }
778 }
779 }
780
Nicolas Geoffray39468442014-09-02 15:17:15 +0100781 // Collect PC infos for the mapping table.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100782 uint32_t native_pc = GetAssembler()->CodeSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100783
Nicolas Geoffray39468442014-09-02 15:17:15 +0100784 if (instruction == nullptr) {
785 // For stack overflow checks.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100786 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100787 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000788 return;
789 }
790 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100791
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000792 uint32_t register_mask = locations->GetRegisterMask();
793 if (locations->OnlyCallsOnSlowPath()) {
794 // In case of slow path, we currently set the location of caller-save registers
795 // to register (instead of their stack location when pushed before the slow-path
796 // call). Therefore register_mask contains both callee-save and caller-save
797 // registers that hold objects. We must remove the caller-save from the mask, since
798 // they will be overwritten by the callee.
799 register_mask &= core_callee_save_mask_;
800 }
801 // The register mask must be a subset of callee-save registers.
802 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100803 stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
804 native_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100805 register_mask,
806 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100807 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100808 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100809
810 EmitEnvironment(instruction->GetEnvironment(), slow_path);
811 stack_map_stream_.EndStackMapEntry();
812}
813
David Brazdil77a48ae2015-09-15 12:34:04 +0000814void CodeGenerator::RecordCatchBlockInfo() {
815 ArenaAllocator* arena = graph_->GetArena();
816
817 for (size_t i = 0, e = block_order_->Size(); i < e; ++i) {
818 HBasicBlock* block = block_order_->Get(i);
819 if (!block->IsCatchBlock()) {
820 continue;
821 }
822
823 uint32_t dex_pc = block->GetDexPc();
824 uint32_t num_vregs = graph_->GetNumberOfVRegs();
825 uint32_t inlining_depth = 0; // Inlining of catch blocks is not supported at the moment.
826 uint32_t native_pc = GetAddressOf(block);
827 uint32_t register_mask = 0; // Not used.
828
829 // The stack mask is not used, so we leave it empty.
830 ArenaBitVector* stack_mask = new (arena) ArenaBitVector(arena, 0, /* expandable */ true);
831
832 stack_map_stream_.BeginStackMapEntry(dex_pc,
833 native_pc,
834 register_mask,
835 stack_mask,
836 num_vregs,
837 inlining_depth);
838
839 HInstruction* current_phi = block->GetFirstPhi();
840 for (size_t vreg = 0; vreg < num_vregs; ++vreg) {
841 while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) {
842 HInstruction* next_phi = current_phi->GetNext();
843 DCHECK(next_phi == nullptr ||
844 current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber())
845 << "Phis need to be sorted by vreg number to keep this a linear-time loop.";
846 current_phi = next_phi;
847 }
848
849 if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) {
850 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
851 } else {
852 Location location = current_phi->GetLiveInterval()->ToLocation();
853 switch (location.GetKind()) {
854 case Location::kStackSlot: {
855 stack_map_stream_.AddDexRegisterEntry(
856 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
857 break;
858 }
859 case Location::kDoubleStackSlot: {
860 stack_map_stream_.AddDexRegisterEntry(
861 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
862 stack_map_stream_.AddDexRegisterEntry(
863 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
864 ++vreg;
865 DCHECK_LT(vreg, num_vregs);
866 break;
867 }
868 default: {
869 // All catch phis must be allocated to a stack slot.
870 LOG(FATAL) << "Unexpected kind " << location.GetKind();
871 UNREACHABLE();
872 }
873 }
874 }
875 }
876
877 stack_map_stream_.EndStackMapEntry();
878 }
879}
880
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100881void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
882 if (environment == nullptr) return;
883
884 if (environment->GetParent() != nullptr) {
885 // We emit the parent environment first.
886 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100887 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
888 environment->GetDexPc(),
889 environment->GetInvokeType(),
890 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100891 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000892
893 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100894 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000895 HInstruction* current = environment->GetInstructionAt(i);
896 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100897 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000898 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100899 }
900
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100901 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000902 switch (location.GetKind()) {
903 case Location::kConstant: {
904 DCHECK_EQ(current, location.GetConstant());
905 if (current->IsLongConstant()) {
906 int64_t value = current->AsLongConstant()->GetValue();
907 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100908 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000909 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100910 DexRegisterLocation::Kind::kConstant, High32Bits(value));
911 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000912 DCHECK_LT(i, environment_size);
913 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000914 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000915 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100916 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000917 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100918 DexRegisterLocation::Kind::kConstant, High32Bits(value));
919 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000920 DCHECK_LT(i, environment_size);
921 } else if (current->IsIntConstant()) {
922 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100923 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000924 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100925 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000926 } else {
927 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000928 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100929 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000930 }
931 break;
932 }
933
934 case Location::kStackSlot: {
935 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100936 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000937 break;
938 }
939
940 case Location::kDoubleStackSlot: {
941 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100942 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000943 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100944 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
945 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000946 DCHECK_LT(i, environment_size);
947 break;
948 }
949
950 case Location::kRegister : {
951 int id = location.reg();
952 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
953 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100954 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000955 if (current->GetType() == Primitive::kPrimLong) {
956 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100957 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
958 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000959 DCHECK_LT(i, environment_size);
960 }
961 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100962 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000963 if (current->GetType() == Primitive::kPrimLong) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100964 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100965 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000966 DCHECK_LT(i, environment_size);
967 }
968 }
969 break;
970 }
971
972 case Location::kFpuRegister : {
973 int id = location.reg();
974 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
975 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100976 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000977 if (current->GetType() == Primitive::kPrimDouble) {
978 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100979 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
980 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000981 DCHECK_LT(i, environment_size);
982 }
983 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100984 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000985 if (current->GetType() == Primitive::kPrimDouble) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100986 stack_map_stream_.AddDexRegisterEntry(
987 DexRegisterLocation::Kind::kInFpuRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100988 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000989 DCHECK_LT(i, environment_size);
990 }
991 }
992 break;
993 }
994
995 case Location::kFpuRegisterPair : {
996 int low = location.low();
997 int high = location.high();
998 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
999 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001000 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001001 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001002 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001003 }
1004 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
1005 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001006 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1007 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001008 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001009 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
1010 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001011 }
1012 DCHECK_LT(i, environment_size);
1013 break;
1014 }
1015
1016 case Location::kRegisterPair : {
1017 int low = location.low();
1018 int high = location.high();
1019 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
1020 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001021 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001022 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001023 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001024 }
1025 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
1026 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001027 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001028 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001029 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001030 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001031 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001032 DCHECK_LT(i, environment_size);
1033 break;
1034 }
1035
1036 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001037 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001038 break;
1039 }
1040
1041 default:
1042 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1043 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001044 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001045
1046 if (environment->GetParent() != nullptr) {
1047 stack_map_stream_.EndInlineInfoEntry();
1048 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001049}
1050
David Brazdil77a48ae2015-09-15 12:34:04 +00001051bool CodeGenerator::IsImplicitNullCheckAllowed(HNullCheck* null_check) const {
1052 return compiler_options_.GetImplicitNullChecks() &&
1053 // Null checks which might throw into a catch block need to save live
1054 // registers and therefore cannot be done implicitly.
1055 !null_check->CanThrowIntoCatchBlock();
1056}
1057
Calin Juravle77520bc2015-01-12 18:45:46 +00001058bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
1059 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +01001060
1061 return (first_next_not_move != nullptr)
1062 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00001063}
1064
1065void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
1066 // If we are from a static path don't record the pc as we can't throw NPE.
1067 // NB: having the checks here makes the code much less verbose in the arch
1068 // specific code generators.
1069 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
1070 return;
1071 }
1072
Calin Juravle641547a2015-04-21 22:08:51 +01001073 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001074 return;
1075 }
1076
1077 // Find the first previous instruction which is not a move.
1078 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
1079
1080 // If the instruction is a null check it means that `instr` is the first user
1081 // and needs to record the pc.
1082 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
1083 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
David Brazdil77a48ae2015-09-15 12:34:04 +00001084 if (IsImplicitNullCheckAllowed(null_check)) {
1085 // TODO: The parallel moves modify the environment. Their changes need to be
1086 // reverted otherwise the stack maps at the throw point will not be correct.
1087 RecordPcInfo(null_check, null_check->GetDexPc());
1088 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001089 }
1090}
1091
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001092void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
1093 LocationSummary* locations = suspend_check->GetLocations();
1094 HBasicBlock* block = suspend_check->GetBlock();
1095 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
1096 DCHECK(block->IsLoopHeader());
1097
1098 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1099 HInstruction* current = it.Current();
1100 LiveInterval* interval = current->GetLiveInterval();
1101 // We only need to clear bits of loop phis containing objects and allocated in register.
1102 // Loop phis allocated on stack already have the object in the stack.
1103 if (current->GetType() == Primitive::kPrimNot
1104 && interval->HasRegister()
1105 && interval->HasSpillSlot()) {
1106 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
1107 }
1108 }
1109}
1110
Nicolas Geoffray90218252015-04-15 11:56:51 +01001111void CodeGenerator::EmitParallelMoves(Location from1,
1112 Location to1,
1113 Primitive::Type type1,
1114 Location from2,
1115 Location to2,
1116 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001117 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +01001118 parallel_move.AddMove(from1, to1, type1, nullptr);
1119 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001120 GetMoveResolver()->EmitNativeCode(&parallel_move);
1121}
1122
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001123void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) {
1124 // Ensure that the call kind indication given to the register allocator is
1125 // coherent with the runtime call generated, and that the GC side effect is
1126 // set when required.
1127 if (slow_path == nullptr) {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001128 DCHECK(instruction->GetLocations()->WillCall()) << instruction->DebugName();
1129 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()))
1130 << instruction->DebugName() << instruction->GetSideEffects().ToString();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001131 } else {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001132 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal())
1133 << instruction->DebugName() << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001134 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) ||
1135 // Control flow would not come back into the code if a fatal slow
1136 // path is taken, so we do not care if it triggers GC.
1137 slow_path->IsFatal() ||
1138 // HDeoptimize is a special case: we know we are not coming back from
1139 // it into the code.
Roland Levillaindf3f8222015-08-13 12:31:44 +01001140 instruction->IsDeoptimize())
1141 << instruction->DebugName() << instruction->GetSideEffects().ToString()
1142 << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001143 }
1144
1145 // Check the coherency of leaf information.
1146 DCHECK(instruction->IsSuspendCheck()
1147 || ((slow_path != nullptr) && slow_path->IsFatal())
1148 || instruction->GetLocations()->CanCall()
Roland Levillaindf3f8222015-08-13 12:31:44 +01001149 || !IsLeafMethod())
1150 << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : "");
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001151}
1152
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001153void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1154 RegisterSet* register_set = locations->GetLiveRegisters();
1155 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1156 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1157 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1158 if (register_set->ContainsCoreRegister(i)) {
1159 // If the register holds an object, update the stack mask.
1160 if (locations->RegisterContainsObject(i)) {
1161 locations->SetStackBit(stack_offset / kVRegSize);
1162 }
1163 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001164 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1165 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001166 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
1167 }
1168 }
1169 }
1170
1171 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1172 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1173 if (register_set->ContainsFloatingPointRegister(i)) {
1174 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001175 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1176 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001177 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
1178 }
1179 }
1180 }
1181}
1182
1183void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1184 RegisterSet* register_set = locations->GetLiveRegisters();
1185 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1186 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1187 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1188 if (register_set->ContainsCoreRegister(i)) {
1189 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1190 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1191 }
1192 }
1193 }
1194
1195 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1196 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1197 if (register_set->ContainsFloatingPointRegister(i)) {
1198 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1199 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1200 }
1201 }
1202 }
1203}
1204
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001205} // namespace art