blob: 130f0e970f61ae5e1e4a9ef7d51d831fa39e3ce6 [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
19#include "code_generator_arm.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010020#include "code_generator_arm64.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000021#include "code_generator_x86.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "code_generator_x86_64.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070023#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000024#include "dex/verified_method.h"
25#include "driver/dex_compilation_unit.h"
26#include "gc_map_builder.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000027#include "leb128.h"
28#include "mapping_table.h"
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010029#include "mirror/array-inl.h"
30#include "mirror/object_array-inl.h"
31#include "mirror/object_reference.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010032#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000034#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000035#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036
37namespace art {
38
Alexandre Rames88c13cd2015-04-14 17:35:39 +010039// Return whether a location is consistent with a type.
40static bool CheckType(Primitive::Type type, Location location) {
41 if (location.IsFpuRegister()
42 || (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresFpuRegister))) {
43 return (type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble);
44 } else if (location.IsRegister() ||
45 (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresRegister))) {
46 return Primitive::IsIntegralType(type) || (type == Primitive::kPrimNot);
47 } else if (location.IsRegisterPair()) {
48 return type == Primitive::kPrimLong;
49 } else if (location.IsFpuRegisterPair()) {
50 return type == Primitive::kPrimDouble;
51 } else if (location.IsStackSlot()) {
52 return (Primitive::IsIntegralType(type) && type != Primitive::kPrimLong)
53 || (type == Primitive::kPrimFloat)
54 || (type == Primitive::kPrimNot);
55 } else if (location.IsDoubleStackSlot()) {
56 return (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
57 } else if (location.IsConstant()) {
58 if (location.GetConstant()->IsIntConstant()) {
59 return Primitive::IsIntegralType(type) && (type != Primitive::kPrimLong);
60 } else if (location.GetConstant()->IsNullConstant()) {
61 return type == Primitive::kPrimNot;
62 } else if (location.GetConstant()->IsLongConstant()) {
63 return type == Primitive::kPrimLong;
64 } else if (location.GetConstant()->IsFloatConstant()) {
65 return type == Primitive::kPrimFloat;
66 } else {
67 return location.GetConstant()->IsDoubleConstant()
68 && (type == Primitive::kPrimDouble);
69 }
70 } else {
71 return location.IsInvalid() || (location.GetPolicy() == Location::kAny);
72 }
73}
74
75// Check that a location summary is consistent with an instruction.
76static bool CheckTypeConsistency(HInstruction* instruction) {
77 LocationSummary* locations = instruction->GetLocations();
78 if (locations == nullptr) {
79 return true;
80 }
81
82 if (locations->Out().IsUnallocated()
83 && (locations->Out().GetPolicy() == Location::kSameAsFirstInput)) {
84 DCHECK(CheckType(instruction->GetType(), locations->InAt(0)))
85 << instruction->GetType()
86 << " " << locations->InAt(0);
87 } else {
88 DCHECK(CheckType(instruction->GetType(), locations->Out()))
89 << instruction->GetType()
90 << " " << locations->Out();
91 }
92
93 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
94 DCHECK(CheckType(instruction->InputAt(i)->GetType(), locations->InAt(i)))
95 << instruction->InputAt(i)->GetType()
96 << " " << locations->InAt(i);
97 }
98
99 HEnvironment* environment = instruction->GetEnvironment();
100 for (size_t i = 0; i < instruction->EnvironmentSize(); ++i) {
101 if (environment->GetInstructionAt(i) != nullptr) {
102 Primitive::Type type = environment->GetInstructionAt(i)->GetType();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100103 DCHECK(CheckType(type, environment->GetLocationAt(i)))
104 << type << " " << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100105 } else {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100106 DCHECK(environment->GetLocationAt(i).IsInvalid())
107 << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100108 }
109 }
110 return true;
111}
112
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100113size_t CodeGenerator::GetCacheOffset(uint32_t index) {
114 return mirror::ObjectArray<mirror::Object>::OffsetOfElement(index).SizeValue();
115}
116
Mathieu Chartiere401d142015-04-22 13:56:20 -0700117size_t CodeGenerator::GetCachePointerOffset(uint32_t index) {
118 auto pointer_size = InstructionSetPointerSize(GetInstructionSet());
119 return mirror::Array::DataOffset(pointer_size).Uint32Value() + pointer_size * index;
120}
121
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100122void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000123 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100124 if (!is_leaf) {
125 MarkNotLeaf();
126 }
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700127 const bool is_64_bit = Is64BitInstructionSet(GetInstructionSet());
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000128 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
129 + GetGraph()->GetTemporariesVRegSlots()
130 + 1 /* filler */,
131 0, /* the baseline compiler does not have live registers at slow path */
132 0, /* the baseline compiler does not have live registers at slow path */
133 GetGraph()->GetMaximumNumberOfOutVRegs()
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700134 + (is_64_bit ? 2 : 1) /* current method */,
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000135 GetGraph()->GetBlocks());
136 CompileInternal(allocator, /* is_baseline */ true);
137}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100138
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000139bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
140 DCHECK_EQ(block_order_->Get(current_block_index_), current);
141 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
142}
143
144HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
145 for (size_t i = current_block_index_ + 1; i < block_order_->Size(); ++i) {
146 HBasicBlock* block = block_order_->Get(i);
David Brazdil46e2a392015-03-16 17:31:52 +0000147 if (!block->IsSingleGoto()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000148 return block;
149 }
150 }
151 return nullptr;
152}
153
154HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdil46e2a392015-03-16 17:31:52 +0000155 while (block->IsSingleGoto()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000156 block = block->GetSuccessors().Get(0);
157 }
158 return block;
159}
160
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000161void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Roland Levillain3e3d7332015-04-28 11:00:54 +0100162 is_baseline_ = is_baseline;
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100163 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000164 DCHECK_EQ(current_block_index_, 0u);
165 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100166 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000167 for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) {
168 HBasicBlock* block = block_order_->Get(current_block_index_);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000169 // Don't generate code for an empty block. Its predecessors will branch to its successor
170 // directly. Also, the label of that block will not be emitted, so this helps catch
171 // errors where we reference that label.
David Brazdil46e2a392015-03-16 17:31:52 +0000172 if (block->IsSingleGoto()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100173 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100174 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
175 HInstruction* current = it.Current();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000176 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000177 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000178 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100179 DCHECK(CheckTypeConsistency(current));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100180 current->Accept(instruction_visitor);
181 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000182 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000183
184 // Generate the slow paths.
185 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
186 slow_paths_.Get(i)->EmitNativeCode(this);
187 }
188
189 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000190 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000191}
192
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100193void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000194 // The register allocator already called `InitializeCodeGeneration`,
195 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000196 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100197 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000198 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000199}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100200
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000201void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100202 size_t code_size = GetAssembler()->CodeSize();
203 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000204
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100205 MemoryRegion code(buffer, code_size);
206 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000207}
208
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100209size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
210 for (size_t i = 0; i < length; ++i) {
211 if (!array[i]) {
212 array[i] = true;
213 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100214 }
215 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100216 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000217 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000218}
219
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000220size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
221 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000222 if (!array[i] && !array[i + 1]) {
223 array[i] = true;
224 array[i + 1] = true;
225 return i;
226 }
227 }
228 LOG(FATAL) << "Could not find a register in baseline register allocator";
229 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100230}
231
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000232void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
233 size_t maximum_number_of_live_core_registers,
234 size_t maximum_number_of_live_fp_registers,
235 size_t number_of_out_slots,
236 const GrowableArray<HBasicBlock*>& block_order) {
237 block_order_ = &block_order;
238 DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock());
239 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), block_order_->Get(1)));
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000240 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100241 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
242
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000243 if (number_of_spill_slots == 0
244 && !HasAllocatedCalleeSaveRegisters()
245 && IsLeafMethod()
246 && !RequiresCurrentMethod()) {
247 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
248 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
249 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
250 } else {
251 SetFrameSize(RoundUp(
252 number_of_spill_slots * kVRegSize
253 + number_of_out_slots * kVRegSize
254 + maximum_number_of_live_core_registers * GetWordSize()
255 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
256 + FrameEntrySpillSize(),
257 kStackAlignment));
258 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100259}
260
261Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
262 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000263 // The type of the previous instruction tells us if we need a single or double stack slot.
264 Primitive::Type type = temp->GetType();
265 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100266 // Use the temporary region (right below the dex registers).
267 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
268 - kVRegSize // filler
269 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000270 - ((temp_size + temp->GetIndex()) * kVRegSize);
271 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100272}
273
274int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
275 uint16_t reg_number = local->GetRegNumber();
276 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
277 if (reg_number >= number_of_locals) {
278 // Local is a parameter of the method. It is stored in the caller's frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode.
280 return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100281 + (reg_number - number_of_locals) * kVRegSize;
282 } else {
283 // Local is a temporary in this method. It is stored in this method's frame.
284 return GetFrameSize() - FrameEntrySpillSize()
285 - kVRegSize // filler.
286 - (number_of_locals * kVRegSize)
287 + (reg_number * kVRegSize);
288 }
289}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100290
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100291void CodeGenerator::CreateCommonInvokeLocationSummary(
Nicolas Geoffray4e40c262015-06-03 12:02:38 +0100292 HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100293 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
294 LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100295
296 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
297 HInstruction* input = invoke->InputAt(i);
298 locations->SetInAt(i, visitor->GetNextLocation(input->GetType()));
299 }
300
301 locations->SetOut(visitor->GetReturnLocation(invoke->GetType()));
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100302
303 if (invoke->IsInvokeStaticOrDirect()) {
304 HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect();
305 if (call->IsStringInit()) {
306 locations->AddTemp(visitor->GetMethodLocation());
307 } else if (call->IsRecursive()) {
308 locations->SetInAt(call->GetCurrentMethodInputIndex(), visitor->GetMethodLocation());
309 } else {
310 locations->AddTemp(visitor->GetMethodLocation());
311 locations->SetInAt(call->GetCurrentMethodInputIndex(), Location::RequiresRegister());
312 }
313 } else {
314 locations->AddTemp(visitor->GetMethodLocation());
315 }
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100316}
317
Mark Mendell5f874182015-03-04 15:42:45 -0500318void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
319 // The DCHECKS below check that a register is not specified twice in
320 // the summary. The out location can overlap with an input, so we need
321 // to special case it.
322 if (location.IsRegister()) {
323 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
324 blocked_core_registers_[location.reg()] = true;
325 } else if (location.IsFpuRegister()) {
326 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
327 blocked_fpu_registers_[location.reg()] = true;
328 } else if (location.IsFpuRegisterPair()) {
329 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
330 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
331 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
332 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
333 } else if (location.IsRegisterPair()) {
334 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
335 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
336 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
337 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
338 }
339}
340
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100341void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
342 LocationSummary* locations = instruction->GetLocations();
343 if (locations == nullptr) return;
344
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100345 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
346 blocked_core_registers_[i] = false;
347 }
348
349 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
350 blocked_fpu_registers_[i] = false;
351 }
352
353 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
354 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100355 }
356
357 // Mark all fixed input, temp and output registers as used.
358 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500359 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100360 }
361
362 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
363 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500364 BlockIfInRegister(loc);
365 }
366 Location result_location = locations->Out();
367 if (locations->OutputCanOverlapWithInputs()) {
368 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100369 }
370
Mark Mendell5f874182015-03-04 15:42:45 -0500371 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100372
373 // Allocate all unallocated input locations.
374 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
375 Location loc = locations->InAt(i);
376 HInstruction* input = instruction->InputAt(i);
377 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100378 if ((loc.GetPolicy() == Location::kRequiresRegister)
379 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100380 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100381 } else {
382 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
383 HLoadLocal* load = input->AsLoadLocal();
384 if (load != nullptr) {
385 loc = GetStackLocation(load);
386 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 }
389 }
390 locations->SetInAt(i, loc);
391 }
392 }
393
394 // Allocate all unallocated temp locations.
395 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
396 Location loc = locations->GetTemp(i);
397 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000398 switch (loc.GetPolicy()) {
399 case Location::kRequiresRegister:
400 // Allocate a core register (large enough to fit a 32-bit integer).
401 loc = AllocateFreeRegister(Primitive::kPrimInt);
402 break;
403
404 case Location::kRequiresFpuRegister:
405 // Allocate a core register (large enough to fit a 64-bit double).
406 loc = AllocateFreeRegister(Primitive::kPrimDouble);
407 break;
408
409 default:
410 LOG(FATAL) << "Unexpected policy for temporary location "
411 << loc.GetPolicy();
412 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100413 locations->SetTempAt(i, loc);
414 }
415 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100416 if (result_location.IsUnallocated()) {
417 switch (result_location.GetPolicy()) {
418 case Location::kAny:
419 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100420 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100421 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100422 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423 case Location::kSameAsFirstInput:
424 result_location = locations->InAt(0);
425 break;
426 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000427 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428 }
429}
430
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000431void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
432 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100434 if (instruction->IsTemporary()) {
435 HInstruction* previous = instruction->GetPrevious();
436 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
437 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100438 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439 return;
440 }
441 AllocateRegistersLocally(instruction);
442 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000443 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000444 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000445 if (location.IsValid()) {
446 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000447 if (input->GetNext()->IsTemporary()) {
448 // If the input was stored in a temporary, use that temporary to
449 // perform the move.
450 Move(input->GetNext(), location, instruction);
451 } else {
452 Move(input, location, instruction);
453 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000454 }
455 }
456}
457
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000458void CodeGenerator::AllocateLocations(HInstruction* instruction) {
459 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100460 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000461 LocationSummary* locations = instruction->GetLocations();
462 if (!instruction->IsSuspendCheckEntry()) {
463 if (locations != nullptr && locations->CanCall()) {
464 MarkNotLeaf();
465 }
466 if (instruction->NeedsCurrentMethod()) {
467 SetRequiresCurrentMethod();
468 }
469 }
470}
471
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000472CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000473 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000474 const InstructionSetFeatures& isa_features,
475 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000476 switch (instruction_set) {
477 case kArm:
478 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000479 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000480 *isa_features.AsArmInstructionSetFeatures(),
481 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000482 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100483 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000484 return new arm64::CodeGeneratorARM64(graph,
485 *isa_features.AsArm64InstructionSetFeatures(),
486 compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100487 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000488 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000489 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000490 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400491 return new x86::CodeGeneratorX86(graph,
492 *isa_features.AsX86InstructionSetFeatures(),
493 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000494 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700495 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400496 return new x86_64::CodeGeneratorX86_64(graph,
497 *isa_features.AsX86_64InstructionSetFeatures(),
498 compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700499 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000500 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000501 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000502 }
503}
504
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000505void CodeGenerator::BuildNativeGCMap(
506 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
507 const std::vector<uint8_t>& gc_map_raw =
508 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
509 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
510
Vladimir Markobd8c7252015-06-12 10:06:32 +0100511 uint32_t max_native_offset = stack_map_stream_.ComputeMaxNativePcOffset();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000512
Vladimir Markobd8c7252015-06-12 10:06:32 +0100513 size_t num_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
514 GcMapBuilder builder(data, num_stack_maps, max_native_offset, dex_gc_map.RegWidth());
515 for (size_t i = 0; i != num_stack_maps; ++i) {
516 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
517 uint32_t native_offset = stack_map_entry.native_pc_offset;
518 uint32_t dex_pc = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000519 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800520 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000521 builder.AddEntry(native_offset, references);
522 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000523}
524
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100525void CodeGenerator::BuildSourceMap(DefaultSrcMap* src_map) const {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100526 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
527 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
528 uint32_t pc2dex_offset = stack_map_entry.native_pc_offset;
529 int32_t pc2dex_dalvik_offset = stack_map_entry.dex_pc;
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100530 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
531 }
532}
533
534void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000535 uint32_t pc2dex_data_size = 0u;
Vladimir Markobd8c7252015-06-12 10:06:32 +0100536 uint32_t pc2dex_entries = stack_map_stream_.GetNumberOfStackMaps();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000537 uint32_t pc2dex_offset = 0u;
538 int32_t pc2dex_dalvik_offset = 0;
539 uint32_t dex2pc_data_size = 0u;
540 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000541 uint32_t dex2pc_offset = 0u;
542 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000543
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000544 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100545 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
546 pc2dex_data_size += UnsignedLeb128Size(stack_map_entry.native_pc_offset - pc2dex_offset);
547 pc2dex_data_size += SignedLeb128Size(stack_map_entry.dex_pc - pc2dex_dalvik_offset);
548 pc2dex_offset = stack_map_entry.native_pc_offset;
549 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000550 }
551
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000552 // Walk over the blocks and find which ones correspond to catch block entries.
553 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
554 HBasicBlock* block = graph_->GetBlocks().Get(i);
555 if (block->IsCatchBlock()) {
556 intptr_t native_pc = GetAddressOf(block);
557 ++dex2pc_entries;
558 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
559 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
560 dex2pc_offset = native_pc;
561 dex2pc_dalvik_offset = block->GetDexPc();
562 }
563 }
564
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000565 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
566 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
567 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
568 data->resize(data_size);
569
570 uint8_t* data_ptr = &(*data)[0];
571 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000572
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000573 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
574 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
575 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
576 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
577
578 pc2dex_offset = 0u;
579 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000580 dex2pc_offset = 0u;
581 dex2pc_dalvik_offset = 0u;
582
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000583 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100584 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
585 DCHECK(pc2dex_offset <= stack_map_entry.native_pc_offset);
586 write_pos = EncodeUnsignedLeb128(write_pos, stack_map_entry.native_pc_offset - pc2dex_offset);
587 write_pos = EncodeSignedLeb128(write_pos, stack_map_entry.dex_pc - pc2dex_dalvik_offset);
588 pc2dex_offset = stack_map_entry.native_pc_offset;
589 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000590 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000591
592 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
593 HBasicBlock* block = graph_->GetBlocks().Get(i);
594 if (block->IsCatchBlock()) {
595 intptr_t native_pc = GetAddressOf(block);
596 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
597 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
598 dex2pc_offset = native_pc;
599 dex2pc_dalvik_offset = block->GetDexPc();
600 }
601 }
602
603
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000604 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
605 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
606
607 if (kIsDebugBuild) {
608 // Verify the encoded table holds the expected data.
609 MappingTable table(data_ptr);
610 CHECK_EQ(table.TotalSize(), total_entries);
611 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
612 auto it = table.PcToDexBegin();
613 auto it2 = table.DexToPcBegin();
614 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100615 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
616 CHECK_EQ(stack_map_entry.native_pc_offset, it.NativePcOffset());
617 CHECK_EQ(stack_map_entry.dex_pc, it.DexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000618 ++it;
619 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000620 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
621 HBasicBlock* block = graph_->GetBlocks().Get(i);
622 if (block->IsCatchBlock()) {
623 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
624 CHECK_EQ(block->GetDexPc(), it2.DexPc());
625 ++it2;
626 }
627 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000628 CHECK(it == table.PcToDexEnd());
629 CHECK(it2 == table.DexToPcEnd());
630 }
631}
632
633void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
634 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100635 // We currently don't use callee-saved registers.
636 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000637 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
638 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000639 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
640
641 *data = vmap_encoder.GetData();
642}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000643
Nicolas Geoffray39468442014-09-02 15:17:15 +0100644void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100645 uint32_t size = stack_map_stream_.PrepareForFillIn();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100646 data->resize(size);
647 MemoryRegion region(data->data(), size);
648 stack_map_stream_.FillIn(region);
649}
650
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000651void CodeGenerator::RecordPcInfo(HInstruction* instruction,
652 uint32_t dex_pc,
653 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000654 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000655 // The code generated for some type conversions may call the
656 // runtime, thus normally requiring a subsequent call to this
657 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000658 // information for certain instructions, which are considered "atomic"
659 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000660 // Therefore we do not currently record PC information for such
661 // instructions. As this may change later, we added this special
662 // case so that code generators may nevertheless call
663 // CodeGenerator::RecordPcInfo without triggering an error in
664 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
665 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000666 if (instruction->IsTypeConversion()) {
667 return;
668 }
669 if (instruction->IsRem()) {
670 Primitive::Type type = instruction->AsRem()->GetResultType();
671 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
672 return;
673 }
674 }
Roland Levillain624279f2014-12-04 11:54:28 +0000675 }
676
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100677 uint32_t outer_dex_pc = dex_pc;
678 uint32_t outer_environment_size = 0;
679 uint32_t inlining_depth = 0;
680 if (instruction != nullptr) {
681 for (HEnvironment* environment = instruction->GetEnvironment();
682 environment != nullptr;
683 environment = environment->GetParent()) {
684 outer_dex_pc = environment->GetDexPc();
685 outer_environment_size = environment->Size();
686 if (environment != instruction->GetEnvironment()) {
687 inlining_depth++;
688 }
689 }
690 }
691
Nicolas Geoffray39468442014-09-02 15:17:15 +0100692 // Collect PC infos for the mapping table.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100693 uint32_t native_pc = GetAssembler()->CodeSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100694
Nicolas Geoffray39468442014-09-02 15:17:15 +0100695 if (instruction == nullptr) {
696 // For stack overflow checks.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100697 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100698 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000699 return;
700 }
701 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100702
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000703 uint32_t register_mask = locations->GetRegisterMask();
704 if (locations->OnlyCallsOnSlowPath()) {
705 // In case of slow path, we currently set the location of caller-save registers
706 // to register (instead of their stack location when pushed before the slow-path
707 // call). Therefore register_mask contains both callee-save and caller-save
708 // registers that hold objects. We must remove the caller-save from the mask, since
709 // they will be overwritten by the callee.
710 register_mask &= core_callee_save_mask_;
711 }
712 // The register mask must be a subset of callee-save registers.
713 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100714 stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
715 native_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100716 register_mask,
717 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100718 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100719 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100720
721 EmitEnvironment(instruction->GetEnvironment(), slow_path);
722 stack_map_stream_.EndStackMapEntry();
723}
724
725void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
726 if (environment == nullptr) return;
727
728 if (environment->GetParent() != nullptr) {
729 // We emit the parent environment first.
730 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100731 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
732 environment->GetDexPc(),
733 environment->GetInvokeType(),
734 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100735 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000736
737 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100738 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000739 HInstruction* current = environment->GetInstructionAt(i);
740 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100741 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000742 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100743 }
744
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100745 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000746 switch (location.GetKind()) {
747 case Location::kConstant: {
748 DCHECK_EQ(current, location.GetConstant());
749 if (current->IsLongConstant()) {
750 int64_t value = current->AsLongConstant()->GetValue();
751 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100752 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000753 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100754 DexRegisterLocation::Kind::kConstant, High32Bits(value));
755 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000756 DCHECK_LT(i, environment_size);
757 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000758 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000759 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100760 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000761 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100762 DexRegisterLocation::Kind::kConstant, High32Bits(value));
763 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000764 DCHECK_LT(i, environment_size);
765 } else if (current->IsIntConstant()) {
766 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100767 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000768 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100769 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000770 } else {
771 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000772 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100773 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000774 }
775 break;
776 }
777
778 case Location::kStackSlot: {
779 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100780 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000781 break;
782 }
783
784 case Location::kDoubleStackSlot: {
785 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100786 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000787 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100788 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
789 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000790 DCHECK_LT(i, environment_size);
791 break;
792 }
793
794 case Location::kRegister : {
795 int id = location.reg();
796 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
797 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100798 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000799 if (current->GetType() == Primitive::kPrimLong) {
800 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100801 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
802 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000803 DCHECK_LT(i, environment_size);
804 }
805 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100806 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000807 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100808 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
809 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000810 DCHECK_LT(i, environment_size);
811 }
812 }
813 break;
814 }
815
816 case Location::kFpuRegister : {
817 int id = location.reg();
818 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
819 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100820 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000821 if (current->GetType() == Primitive::kPrimDouble) {
822 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100823 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
824 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000825 DCHECK_LT(i, environment_size);
826 }
827 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100828 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000829 if (current->GetType() == Primitive::kPrimDouble) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100830 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
831 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000832 DCHECK_LT(i, environment_size);
833 }
834 }
835 break;
836 }
837
838 case Location::kFpuRegisterPair : {
839 int low = location.low();
840 int high = location.high();
841 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
842 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100843 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000844 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100845 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000846 }
847 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
848 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100849 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
850 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000851 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100852 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
853 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000854 }
855 DCHECK_LT(i, environment_size);
856 break;
857 }
858
859 case Location::kRegisterPair : {
860 int low = location.low();
861 int high = location.high();
862 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
863 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100864 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000865 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100866 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000867 }
868 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
869 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100870 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000871 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100872 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000873 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100874 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000875 DCHECK_LT(i, environment_size);
876 break;
877 }
878
879 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100880 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000881 break;
882 }
883
884 default:
885 LOG(FATAL) << "Unexpected kind " << location.GetKind();
886 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100887 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100888
889 if (environment->GetParent() != nullptr) {
890 stack_map_stream_.EndInlineInfoEntry();
891 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100892}
893
Calin Juravle77520bc2015-01-12 18:45:46 +0000894bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
895 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +0100896
897 return (first_next_not_move != nullptr)
898 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +0000899}
900
901void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
902 // If we are from a static path don't record the pc as we can't throw NPE.
903 // NB: having the checks here makes the code much less verbose in the arch
904 // specific code generators.
905 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
906 return;
907 }
908
909 if (!compiler_options_.GetImplicitNullChecks()) {
910 return;
911 }
912
Calin Juravle641547a2015-04-21 22:08:51 +0100913 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +0000914 return;
915 }
916
917 // Find the first previous instruction which is not a move.
918 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
919
920 // If the instruction is a null check it means that `instr` is the first user
921 // and needs to record the pc.
922 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
923 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
924 // TODO: The parallel moves modify the environment. Their changes need to be reverted
925 // otherwise the stack maps at the throw point will not be correct.
926 RecordPcInfo(null_check, null_check->GetDexPc());
927 }
928}
929
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100930void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
931 LocationSummary* locations = suspend_check->GetLocations();
932 HBasicBlock* block = suspend_check->GetBlock();
933 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
934 DCHECK(block->IsLoopHeader());
935
936 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
937 HInstruction* current = it.Current();
938 LiveInterval* interval = current->GetLiveInterval();
939 // We only need to clear bits of loop phis containing objects and allocated in register.
940 // Loop phis allocated on stack already have the object in the stack.
941 if (current->GetType() == Primitive::kPrimNot
942 && interval->HasRegister()
943 && interval->HasSpillSlot()) {
944 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
945 }
946 }
947}
948
Nicolas Geoffray90218252015-04-15 11:56:51 +0100949void CodeGenerator::EmitParallelMoves(Location from1,
950 Location to1,
951 Primitive::Type type1,
952 Location from2,
953 Location to2,
954 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000955 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +0100956 parallel_move.AddMove(from1, to1, type1, nullptr);
957 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000958 GetMoveResolver()->EmitNativeCode(&parallel_move);
959}
960
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000961void SlowPathCode::RecordPcInfo(CodeGenerator* codegen, HInstruction* instruction, uint32_t dex_pc) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000962 codegen->RecordPcInfo(instruction, dex_pc, this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000963}
964
965void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
966 RegisterSet* register_set = locations->GetLiveRegisters();
967 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
968 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
969 if (!codegen->IsCoreCalleeSaveRegister(i)) {
970 if (register_set->ContainsCoreRegister(i)) {
971 // If the register holds an object, update the stack mask.
972 if (locations->RegisterContainsObject(i)) {
973 locations->SetStackBit(stack_offset / kVRegSize);
974 }
975 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000976 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
977 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000978 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
979 }
980 }
981 }
982
983 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
984 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
985 if (register_set->ContainsFloatingPointRegister(i)) {
986 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000987 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
988 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000989 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
990 }
991 }
992 }
993}
994
995void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
996 RegisterSet* register_set = locations->GetLiveRegisters();
997 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
998 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
999 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1000 if (register_set->ContainsCoreRegister(i)) {
1001 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1002 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1003 }
1004 }
1005 }
1006
1007 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1008 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1009 if (register_set->ContainsFloatingPointRegister(i)) {
1010 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1011 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1012 }
1013 }
1014 }
1015}
1016
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001017} // namespace art