blob: 0cd63a679ce0885e719735abd2b1e34c743fd6bb [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
Mark Mendell5f874182015-03-04 15:42:45 -0500291void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
292 // The DCHECKS below check that a register is not specified twice in
293 // the summary. The out location can overlap with an input, so we need
294 // to special case it.
295 if (location.IsRegister()) {
296 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
297 blocked_core_registers_[location.reg()] = true;
298 } else if (location.IsFpuRegister()) {
299 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
300 blocked_fpu_registers_[location.reg()] = true;
301 } else if (location.IsFpuRegisterPair()) {
302 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
303 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
304 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
305 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
306 } else if (location.IsRegisterPair()) {
307 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
308 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
309 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
310 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
311 }
312}
313
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100314void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
315 LocationSummary* locations = instruction->GetLocations();
316 if (locations == nullptr) return;
317
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100318 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
319 blocked_core_registers_[i] = false;
320 }
321
322 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
323 blocked_fpu_registers_[i] = false;
324 }
325
326 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
327 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100328 }
329
330 // Mark all fixed input, temp and output registers as used.
331 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500332 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100333 }
334
335 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
336 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500337 BlockIfInRegister(loc);
338 }
339 Location result_location = locations->Out();
340 if (locations->OutputCanOverlapWithInputs()) {
341 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100342 }
343
Mark Mendell5f874182015-03-04 15:42:45 -0500344 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100345
346 // Allocate all unallocated input locations.
347 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
348 Location loc = locations->InAt(i);
349 HInstruction* input = instruction->InputAt(i);
350 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100351 if ((loc.GetPolicy() == Location::kRequiresRegister)
352 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100353 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100354 } else {
355 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
356 HLoadLocal* load = input->AsLoadLocal();
357 if (load != nullptr) {
358 loc = GetStackLocation(load);
359 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100360 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100361 }
362 }
363 locations->SetInAt(i, loc);
364 }
365 }
366
367 // Allocate all unallocated temp locations.
368 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
369 Location loc = locations->GetTemp(i);
370 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000371 switch (loc.GetPolicy()) {
372 case Location::kRequiresRegister:
373 // Allocate a core register (large enough to fit a 32-bit integer).
374 loc = AllocateFreeRegister(Primitive::kPrimInt);
375 break;
376
377 case Location::kRequiresFpuRegister:
378 // Allocate a core register (large enough to fit a 64-bit double).
379 loc = AllocateFreeRegister(Primitive::kPrimDouble);
380 break;
381
382 default:
383 LOG(FATAL) << "Unexpected policy for temporary location "
384 << loc.GetPolicy();
385 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100386 locations->SetTempAt(i, loc);
387 }
388 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100389 if (result_location.IsUnallocated()) {
390 switch (result_location.GetPolicy()) {
391 case Location::kAny:
392 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100393 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100394 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100395 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100396 case Location::kSameAsFirstInput:
397 result_location = locations->InAt(0);
398 break;
399 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000400 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100401 }
402}
403
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000404void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
405 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100406 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100407 if (instruction->IsTemporary()) {
408 HInstruction* previous = instruction->GetPrevious();
409 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
410 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100411 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100412 return;
413 }
414 AllocateRegistersLocally(instruction);
415 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000416 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000417 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000418 if (location.IsValid()) {
419 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000420 if (input->GetNext()->IsTemporary()) {
421 // If the input was stored in a temporary, use that temporary to
422 // perform the move.
423 Move(input->GetNext(), location, instruction);
424 } else {
425 Move(input, location, instruction);
426 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000427 }
428 }
429}
430
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000431void CodeGenerator::AllocateLocations(HInstruction* instruction) {
432 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100433 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000434 LocationSummary* locations = instruction->GetLocations();
435 if (!instruction->IsSuspendCheckEntry()) {
436 if (locations != nullptr && locations->CanCall()) {
437 MarkNotLeaf();
438 }
439 if (instruction->NeedsCurrentMethod()) {
440 SetRequiresCurrentMethod();
441 }
442 }
443}
444
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000445CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000446 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000447 const InstructionSetFeatures& isa_features,
448 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000449 switch (instruction_set) {
450 case kArm:
451 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000452 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000453 *isa_features.AsArmInstructionSetFeatures(),
454 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000455 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100456 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000457 return new arm64::CodeGeneratorARM64(graph,
458 *isa_features.AsArm64InstructionSetFeatures(),
459 compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100460 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000461 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000462 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000463 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400464 return new x86::CodeGeneratorX86(graph,
465 *isa_features.AsX86InstructionSetFeatures(),
466 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000467 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700468 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400469 return new x86_64::CodeGeneratorX86_64(graph,
470 *isa_features.AsX86_64InstructionSetFeatures(),
471 compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700472 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000473 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000474 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000475 }
476}
477
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000478void CodeGenerator::BuildNativeGCMap(
479 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
480 const std::vector<uint8_t>& gc_map_raw =
481 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
482 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
483
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000484 uint32_t max_native_offset = 0;
485 for (size_t i = 0; i < pc_infos_.Size(); i++) {
486 uint32_t native_offset = pc_infos_.Get(i).native_pc;
487 if (native_offset > max_native_offset) {
488 max_native_offset = native_offset;
489 }
490 }
491
492 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
493 for (size_t i = 0; i < pc_infos_.Size(); i++) {
494 struct PcInfo pc_info = pc_infos_.Get(i);
495 uint32_t native_offset = pc_info.native_pc;
496 uint32_t dex_pc = pc_info.dex_pc;
497 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800498 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000499 builder.AddEntry(native_offset, references);
500 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000501}
502
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100503void CodeGenerator::BuildSourceMap(DefaultSrcMap* src_map) const {
504 for (size_t i = 0; i < pc_infos_.Size(); i++) {
505 struct PcInfo pc_info = pc_infos_.Get(i);
506 uint32_t pc2dex_offset = pc_info.native_pc;
507 int32_t pc2dex_dalvik_offset = pc_info.dex_pc;
508 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
509 }
510}
511
512void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000513 uint32_t pc2dex_data_size = 0u;
514 uint32_t pc2dex_entries = pc_infos_.Size();
515 uint32_t pc2dex_offset = 0u;
516 int32_t pc2dex_dalvik_offset = 0;
517 uint32_t dex2pc_data_size = 0u;
518 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000519 uint32_t dex2pc_offset = 0u;
520 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000521
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000522 for (size_t i = 0; i < pc2dex_entries; i++) {
523 struct PcInfo pc_info = pc_infos_.Get(i);
524 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
525 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
526 pc2dex_offset = pc_info.native_pc;
527 pc2dex_dalvik_offset = pc_info.dex_pc;
528 }
529
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000530 // Walk over the blocks and find which ones correspond to catch block entries.
531 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
532 HBasicBlock* block = graph_->GetBlocks().Get(i);
533 if (block->IsCatchBlock()) {
534 intptr_t native_pc = GetAddressOf(block);
535 ++dex2pc_entries;
536 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
537 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
538 dex2pc_offset = native_pc;
539 dex2pc_dalvik_offset = block->GetDexPc();
540 }
541 }
542
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000543 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
544 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
545 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
546 data->resize(data_size);
547
548 uint8_t* data_ptr = &(*data)[0];
549 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000550
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000551 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
552 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
553 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
554 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
555
556 pc2dex_offset = 0u;
557 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000558 dex2pc_offset = 0u;
559 dex2pc_dalvik_offset = 0u;
560
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000561 for (size_t i = 0; i < pc2dex_entries; i++) {
562 struct PcInfo pc_info = pc_infos_.Get(i);
563 DCHECK(pc2dex_offset <= pc_info.native_pc);
564 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
565 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
566 pc2dex_offset = pc_info.native_pc;
567 pc2dex_dalvik_offset = pc_info.dex_pc;
568 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000569
570 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
571 HBasicBlock* block = graph_->GetBlocks().Get(i);
572 if (block->IsCatchBlock()) {
573 intptr_t native_pc = GetAddressOf(block);
574 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
575 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
576 dex2pc_offset = native_pc;
577 dex2pc_dalvik_offset = block->GetDexPc();
578 }
579 }
580
581
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000582 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
583 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
584
585 if (kIsDebugBuild) {
586 // Verify the encoded table holds the expected data.
587 MappingTable table(data_ptr);
588 CHECK_EQ(table.TotalSize(), total_entries);
589 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
590 auto it = table.PcToDexBegin();
591 auto it2 = table.DexToPcBegin();
592 for (size_t i = 0; i < pc2dex_entries; i++) {
593 struct PcInfo pc_info = pc_infos_.Get(i);
594 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
595 CHECK_EQ(pc_info.dex_pc, it.DexPc());
596 ++it;
597 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000598 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
599 HBasicBlock* block = graph_->GetBlocks().Get(i);
600 if (block->IsCatchBlock()) {
601 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
602 CHECK_EQ(block->GetDexPc(), it2.DexPc());
603 ++it2;
604 }
605 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000606 CHECK(it == table.PcToDexEnd());
607 CHECK(it2 == table.DexToPcEnd());
608 }
609}
610
611void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
612 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100613 // We currently don't use callee-saved registers.
614 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000615 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
616 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000617 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
618
619 *data = vmap_encoder.GetData();
620}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000621
Nicolas Geoffray39468442014-09-02 15:17:15 +0100622void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100623 uint32_t size = stack_map_stream_.PrepareForFillIn();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100624 data->resize(size);
625 MemoryRegion region(data->data(), size);
626 stack_map_stream_.FillIn(region);
627}
628
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000629void CodeGenerator::RecordPcInfo(HInstruction* instruction,
630 uint32_t dex_pc,
631 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000632 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000633 // The code generated for some type conversions may call the
634 // runtime, thus normally requiring a subsequent call to this
635 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000636 // information for certain instructions, which are considered "atomic"
637 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000638 // Therefore we do not currently record PC information for such
639 // instructions. As this may change later, we added this special
640 // case so that code generators may nevertheless call
641 // CodeGenerator::RecordPcInfo without triggering an error in
642 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
643 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000644 if (instruction->IsTypeConversion()) {
645 return;
646 }
647 if (instruction->IsRem()) {
648 Primitive::Type type = instruction->AsRem()->GetResultType();
649 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
650 return;
651 }
652 }
Roland Levillain624279f2014-12-04 11:54:28 +0000653 }
654
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100655 uint32_t outer_dex_pc = dex_pc;
656 uint32_t outer_environment_size = 0;
657 uint32_t inlining_depth = 0;
658 if (instruction != nullptr) {
659 for (HEnvironment* environment = instruction->GetEnvironment();
660 environment != nullptr;
661 environment = environment->GetParent()) {
662 outer_dex_pc = environment->GetDexPc();
663 outer_environment_size = environment->Size();
664 if (environment != instruction->GetEnvironment()) {
665 inlining_depth++;
666 }
667 }
668 }
669
Nicolas Geoffray39468442014-09-02 15:17:15 +0100670 // Collect PC infos for the mapping table.
671 struct PcInfo pc_info;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100672 pc_info.dex_pc = outer_dex_pc;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100673 pc_info.native_pc = GetAssembler()->CodeSize();
674 pc_infos_.Add(pc_info);
675
Nicolas Geoffray39468442014-09-02 15:17:15 +0100676 if (instruction == nullptr) {
677 // For stack overflow checks.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100678 stack_map_stream_.BeginStackMapEntry(pc_info.dex_pc, pc_info.native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100679 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000680 return;
681 }
682 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100683
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000684 uint32_t register_mask = locations->GetRegisterMask();
685 if (locations->OnlyCallsOnSlowPath()) {
686 // In case of slow path, we currently set the location of caller-save registers
687 // to register (instead of their stack location when pushed before the slow-path
688 // call). Therefore register_mask contains both callee-save and caller-save
689 // registers that hold objects. We must remove the caller-save from the mask, since
690 // they will be overwritten by the callee.
691 register_mask &= core_callee_save_mask_;
692 }
693 // The register mask must be a subset of callee-save registers.
694 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100695 stack_map_stream_.BeginStackMapEntry(pc_info.dex_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100696 pc_info.native_pc,
697 register_mask,
698 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100699 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100700 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100701
702 EmitEnvironment(instruction->GetEnvironment(), slow_path);
703 stack_map_stream_.EndStackMapEntry();
704}
705
706void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
707 if (environment == nullptr) return;
708
709 if (environment->GetParent() != nullptr) {
710 // We emit the parent environment first.
711 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100712 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
713 environment->GetDexPc(),
714 environment->GetInvokeType(),
715 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100716 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000717
718 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100719 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000720 HInstruction* current = environment->GetInstructionAt(i);
721 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100722 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000723 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100724 }
725
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100726 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000727 switch (location.GetKind()) {
728 case Location::kConstant: {
729 DCHECK_EQ(current, location.GetConstant());
730 if (current->IsLongConstant()) {
731 int64_t value = current->AsLongConstant()->GetValue();
732 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100733 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000734 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100735 DexRegisterLocation::Kind::kConstant, High32Bits(value));
736 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000737 DCHECK_LT(i, environment_size);
738 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000739 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000740 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100741 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000742 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100743 DexRegisterLocation::Kind::kConstant, High32Bits(value));
744 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000745 DCHECK_LT(i, environment_size);
746 } else if (current->IsIntConstant()) {
747 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100748 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000749 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100750 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000751 } else {
752 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000753 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100754 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000755 }
756 break;
757 }
758
759 case Location::kStackSlot: {
760 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100761 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000762 break;
763 }
764
765 case Location::kDoubleStackSlot: {
766 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100767 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000768 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100769 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
770 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000771 DCHECK_LT(i, environment_size);
772 break;
773 }
774
775 case Location::kRegister : {
776 int id = location.reg();
777 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
778 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100779 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000780 if (current->GetType() == Primitive::kPrimLong) {
781 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100782 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
783 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000784 DCHECK_LT(i, environment_size);
785 }
786 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100787 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000788 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100789 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
790 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000791 DCHECK_LT(i, environment_size);
792 }
793 }
794 break;
795 }
796
797 case Location::kFpuRegister : {
798 int id = location.reg();
799 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
800 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100801 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000802 if (current->GetType() == Primitive::kPrimDouble) {
803 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100804 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
805 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000806 DCHECK_LT(i, environment_size);
807 }
808 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100809 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000810 if (current->GetType() == Primitive::kPrimDouble) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100811 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
812 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000813 DCHECK_LT(i, environment_size);
814 }
815 }
816 break;
817 }
818
819 case Location::kFpuRegisterPair : {
820 int low = location.low();
821 int high = location.high();
822 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
823 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100824 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000825 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100826 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000827 }
828 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
829 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100830 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
831 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000832 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100833 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
834 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000835 }
836 DCHECK_LT(i, environment_size);
837 break;
838 }
839
840 case Location::kRegisterPair : {
841 int low = location.low();
842 int high = location.high();
843 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
844 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100845 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000846 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100847 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000848 }
849 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
850 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100851 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000852 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100853 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000854 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100855 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000856 DCHECK_LT(i, environment_size);
857 break;
858 }
859
860 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100861 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000862 break;
863 }
864
865 default:
866 LOG(FATAL) << "Unexpected kind " << location.GetKind();
867 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100868 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100869
870 if (environment->GetParent() != nullptr) {
871 stack_map_stream_.EndInlineInfoEntry();
872 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100873}
874
Calin Juravle77520bc2015-01-12 18:45:46 +0000875bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
876 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +0100877
878 return (first_next_not_move != nullptr)
879 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +0000880}
881
882void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
883 // If we are from a static path don't record the pc as we can't throw NPE.
884 // NB: having the checks here makes the code much less verbose in the arch
885 // specific code generators.
886 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
887 return;
888 }
889
890 if (!compiler_options_.GetImplicitNullChecks()) {
891 return;
892 }
893
Calin Juravle641547a2015-04-21 22:08:51 +0100894 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +0000895 return;
896 }
897
898 // Find the first previous instruction which is not a move.
899 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
900
901 // If the instruction is a null check it means that `instr` is the first user
902 // and needs to record the pc.
903 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
904 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
905 // TODO: The parallel moves modify the environment. Their changes need to be reverted
906 // otherwise the stack maps at the throw point will not be correct.
907 RecordPcInfo(null_check, null_check->GetDexPc());
908 }
909}
910
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100911void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
912 LocationSummary* locations = suspend_check->GetLocations();
913 HBasicBlock* block = suspend_check->GetBlock();
914 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
915 DCHECK(block->IsLoopHeader());
916
917 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
918 HInstruction* current = it.Current();
919 LiveInterval* interval = current->GetLiveInterval();
920 // We only need to clear bits of loop phis containing objects and allocated in register.
921 // Loop phis allocated on stack already have the object in the stack.
922 if (current->GetType() == Primitive::kPrimNot
923 && interval->HasRegister()
924 && interval->HasSpillSlot()) {
925 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
926 }
927 }
928}
929
Nicolas Geoffray90218252015-04-15 11:56:51 +0100930void CodeGenerator::EmitParallelMoves(Location from1,
931 Location to1,
932 Primitive::Type type1,
933 Location from2,
934 Location to2,
935 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000936 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +0100937 parallel_move.AddMove(from1, to1, type1, nullptr);
938 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000939 GetMoveResolver()->EmitNativeCode(&parallel_move);
940}
941
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000942void SlowPathCode::RecordPcInfo(CodeGenerator* codegen, HInstruction* instruction, uint32_t dex_pc) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000943 codegen->RecordPcInfo(instruction, dex_pc, this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000944}
945
946void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
947 RegisterSet* register_set = locations->GetLiveRegisters();
948 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
949 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
950 if (!codegen->IsCoreCalleeSaveRegister(i)) {
951 if (register_set->ContainsCoreRegister(i)) {
952 // If the register holds an object, update the stack mask.
953 if (locations->RegisterContainsObject(i)) {
954 locations->SetStackBit(stack_offset / kVRegSize);
955 }
956 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000957 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
958 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000959 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
960 }
961 }
962 }
963
964 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
965 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
966 if (register_set->ContainsFloatingPointRegister(i)) {
967 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000968 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
969 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000970 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
971 }
972 }
973 }
974}
975
976void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
977 RegisterSet* register_set = locations->GetLiveRegisters();
978 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
979 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
980 if (!codegen->IsCoreCalleeSaveRegister(i)) {
981 if (register_set->ContainsCoreRegister(i)) {
982 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
983 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
984 }
985 }
986 }
987
988 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
989 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
990 if (register_set->ContainsFloatingPointRegister(i)) {
991 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
992 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
993 }
994 }
995 }
996}
997
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000998} // namespace art