blob: d71266df8bd3a1c58176ecee942402652532be2b [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
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100117void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000118 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100119 if (!is_leaf) {
120 MarkNotLeaf();
121 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000122 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
123 + GetGraph()->GetTemporariesVRegSlots()
124 + 1 /* filler */,
125 0, /* the baseline compiler does not have live registers at slow path */
126 0, /* the baseline compiler does not have live registers at slow path */
127 GetGraph()->GetMaximumNumberOfOutVRegs()
128 + 1 /* current method */,
129 GetGraph()->GetBlocks());
130 CompileInternal(allocator, /* is_baseline */ true);
131}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100132
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000133bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
134 DCHECK_EQ(block_order_->Get(current_block_index_), current);
135 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
136}
137
138HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
139 for (size_t i = current_block_index_ + 1; i < block_order_->Size(); ++i) {
140 HBasicBlock* block = block_order_->Get(i);
David Brazdil46e2a392015-03-16 17:31:52 +0000141 if (!block->IsSingleGoto()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000142 return block;
143 }
144 }
145 return nullptr;
146}
147
148HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdil46e2a392015-03-16 17:31:52 +0000149 while (block->IsSingleGoto()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000150 block = block->GetSuccessors().Get(0);
151 }
152 return block;
153}
154
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000155void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Roland Levillain3e3d7332015-04-28 11:00:54 +0100156 is_baseline_ = is_baseline;
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100157 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000158 DCHECK_EQ(current_block_index_, 0u);
159 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100160 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000161 for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) {
162 HBasicBlock* block = block_order_->Get(current_block_index_);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000163 // Don't generate code for an empty block. Its predecessors will branch to its successor
164 // directly. Also, the label of that block will not be emitted, so this helps catch
165 // errors where we reference that label.
David Brazdil46e2a392015-03-16 17:31:52 +0000166 if (block->IsSingleGoto()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100167 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100168 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
169 HInstruction* current = it.Current();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000170 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000171 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000172 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100173 DCHECK(CheckTypeConsistency(current));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100174 current->Accept(instruction_visitor);
175 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000176 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000177
178 // Generate the slow paths.
179 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
180 slow_paths_.Get(i)->EmitNativeCode(this);
181 }
182
183 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000184 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000185}
186
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100187void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000188 // The register allocator already called `InitializeCodeGeneration`,
189 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000190 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100191 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000192 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000193}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100194
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000195void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100196 size_t code_size = GetAssembler()->CodeSize();
197 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000198
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100199 MemoryRegion code(buffer, code_size);
200 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000201}
202
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100203size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
204 for (size_t i = 0; i < length; ++i) {
205 if (!array[i]) {
206 array[i] = true;
207 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100208 }
209 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100210 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000211 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000212}
213
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000214size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
215 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000216 if (!array[i] && !array[i + 1]) {
217 array[i] = true;
218 array[i + 1] = true;
219 return i;
220 }
221 }
222 LOG(FATAL) << "Could not find a register in baseline register allocator";
223 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100224}
225
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000226void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
227 size_t maximum_number_of_live_core_registers,
228 size_t maximum_number_of_live_fp_registers,
229 size_t number_of_out_slots,
230 const GrowableArray<HBasicBlock*>& block_order) {
231 block_order_ = &block_order;
232 DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock());
233 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), block_order_->Get(1)));
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000234 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100235 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
236
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000237 if (number_of_spill_slots == 0
238 && !HasAllocatedCalleeSaveRegisters()
239 && IsLeafMethod()
240 && !RequiresCurrentMethod()) {
241 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
242 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
243 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
244 } else {
245 SetFrameSize(RoundUp(
246 number_of_spill_slots * kVRegSize
247 + number_of_out_slots * kVRegSize
248 + maximum_number_of_live_core_registers * GetWordSize()
249 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
250 + FrameEntrySpillSize(),
251 kStackAlignment));
252 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100253}
254
255Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
256 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000257 // The type of the previous instruction tells us if we need a single or double stack slot.
258 Primitive::Type type = temp->GetType();
259 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100260 // Use the temporary region (right below the dex registers).
261 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
262 - kVRegSize // filler
263 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000264 - ((temp_size + temp->GetIndex()) * kVRegSize);
265 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100266}
267
268int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
269 uint16_t reg_number = local->GetRegNumber();
270 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
271 if (reg_number >= number_of_locals) {
272 // Local is a parameter of the method. It is stored in the caller's frame.
273 return GetFrameSize() + kVRegSize // ART method
274 + (reg_number - number_of_locals) * kVRegSize;
275 } else {
276 // Local is a temporary in this method. It is stored in this method's frame.
277 return GetFrameSize() - FrameEntrySpillSize()
278 - kVRegSize // filler.
279 - (number_of_locals * kVRegSize)
280 + (reg_number * kVRegSize);
281 }
282}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100283
Mark Mendell5f874182015-03-04 15:42:45 -0500284void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
285 // The DCHECKS below check that a register is not specified twice in
286 // the summary. The out location can overlap with an input, so we need
287 // to special case it.
288 if (location.IsRegister()) {
289 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
290 blocked_core_registers_[location.reg()] = true;
291 } else if (location.IsFpuRegister()) {
292 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
293 blocked_fpu_registers_[location.reg()] = true;
294 } else if (location.IsFpuRegisterPair()) {
295 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
296 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
297 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
298 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
299 } else if (location.IsRegisterPair()) {
300 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
301 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
302 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
303 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
304 }
305}
306
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100307void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
308 LocationSummary* locations = instruction->GetLocations();
309 if (locations == nullptr) return;
310
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100311 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
312 blocked_core_registers_[i] = false;
313 }
314
315 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
316 blocked_fpu_registers_[i] = false;
317 }
318
319 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
320 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100321 }
322
323 // Mark all fixed input, temp and output registers as used.
324 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500325 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100326 }
327
328 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
329 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500330 BlockIfInRegister(loc);
331 }
332 Location result_location = locations->Out();
333 if (locations->OutputCanOverlapWithInputs()) {
334 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100335 }
336
Mark Mendell5f874182015-03-04 15:42:45 -0500337 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100338
339 // Allocate all unallocated input locations.
340 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
341 Location loc = locations->InAt(i);
342 HInstruction* input = instruction->InputAt(i);
343 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100344 if ((loc.GetPolicy() == Location::kRequiresRegister)
345 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100346 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100347 } else {
348 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
349 HLoadLocal* load = input->AsLoadLocal();
350 if (load != nullptr) {
351 loc = GetStackLocation(load);
352 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100353 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100354 }
355 }
356 locations->SetInAt(i, loc);
357 }
358 }
359
360 // Allocate all unallocated temp locations.
361 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
362 Location loc = locations->GetTemp(i);
363 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000364 switch (loc.GetPolicy()) {
365 case Location::kRequiresRegister:
366 // Allocate a core register (large enough to fit a 32-bit integer).
367 loc = AllocateFreeRegister(Primitive::kPrimInt);
368 break;
369
370 case Location::kRequiresFpuRegister:
371 // Allocate a core register (large enough to fit a 64-bit double).
372 loc = AllocateFreeRegister(Primitive::kPrimDouble);
373 break;
374
375 default:
376 LOG(FATAL) << "Unexpected policy for temporary location "
377 << loc.GetPolicy();
378 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100379 locations->SetTempAt(i, loc);
380 }
381 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100382 if (result_location.IsUnallocated()) {
383 switch (result_location.GetPolicy()) {
384 case Location::kAny:
385 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100386 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100388 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100389 case Location::kSameAsFirstInput:
390 result_location = locations->InAt(0);
391 break;
392 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000393 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100394 }
395}
396
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000397void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
398 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100399 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100400 if (instruction->IsTemporary()) {
401 HInstruction* previous = instruction->GetPrevious();
402 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
403 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100404 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100405 return;
406 }
407 AllocateRegistersLocally(instruction);
408 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000409 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000410 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000411 if (location.IsValid()) {
412 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000413 if (input->GetNext()->IsTemporary()) {
414 // If the input was stored in a temporary, use that temporary to
415 // perform the move.
416 Move(input->GetNext(), location, instruction);
417 } else {
418 Move(input, location, instruction);
419 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000420 }
421 }
422}
423
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000424void CodeGenerator::AllocateLocations(HInstruction* instruction) {
425 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100426 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000427 LocationSummary* locations = instruction->GetLocations();
428 if (!instruction->IsSuspendCheckEntry()) {
429 if (locations != nullptr && locations->CanCall()) {
430 MarkNotLeaf();
431 }
432 if (instruction->NeedsCurrentMethod()) {
433 SetRequiresCurrentMethod();
434 }
435 }
436}
437
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000438CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000439 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000440 const InstructionSetFeatures& isa_features,
441 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000442 switch (instruction_set) {
443 case kArm:
444 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000445 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000446 *isa_features.AsArmInstructionSetFeatures(),
447 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000448 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100449 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000450 return new arm64::CodeGeneratorARM64(graph,
451 *isa_features.AsArm64InstructionSetFeatures(),
452 compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100453 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000454 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000455 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000456 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400457 return new x86::CodeGeneratorX86(graph,
458 *isa_features.AsX86InstructionSetFeatures(),
459 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000460 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700461 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400462 return new x86_64::CodeGeneratorX86_64(graph,
463 *isa_features.AsX86_64InstructionSetFeatures(),
464 compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700465 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000466 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000467 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000468 }
469}
470
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000471void CodeGenerator::BuildNativeGCMap(
472 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
473 const std::vector<uint8_t>& gc_map_raw =
474 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
475 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
476
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000477 uint32_t max_native_offset = 0;
478 for (size_t i = 0; i < pc_infos_.Size(); i++) {
479 uint32_t native_offset = pc_infos_.Get(i).native_pc;
480 if (native_offset > max_native_offset) {
481 max_native_offset = native_offset;
482 }
483 }
484
485 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
486 for (size_t i = 0; i < pc_infos_.Size(); i++) {
487 struct PcInfo pc_info = pc_infos_.Get(i);
488 uint32_t native_offset = pc_info.native_pc;
489 uint32_t dex_pc = pc_info.dex_pc;
490 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800491 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000492 builder.AddEntry(native_offset, references);
493 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000494}
495
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100496void CodeGenerator::BuildSourceMap(DefaultSrcMap* src_map) const {
497 for (size_t i = 0; i < pc_infos_.Size(); i++) {
498 struct PcInfo pc_info = pc_infos_.Get(i);
499 uint32_t pc2dex_offset = pc_info.native_pc;
500 int32_t pc2dex_dalvik_offset = pc_info.dex_pc;
501 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
502 }
503}
504
505void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000506 uint32_t pc2dex_data_size = 0u;
507 uint32_t pc2dex_entries = pc_infos_.Size();
508 uint32_t pc2dex_offset = 0u;
509 int32_t pc2dex_dalvik_offset = 0;
510 uint32_t dex2pc_data_size = 0u;
511 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000512 uint32_t dex2pc_offset = 0u;
513 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000514
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000515 for (size_t i = 0; i < pc2dex_entries; i++) {
516 struct PcInfo pc_info = pc_infos_.Get(i);
517 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
518 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
519 pc2dex_offset = pc_info.native_pc;
520 pc2dex_dalvik_offset = pc_info.dex_pc;
521 }
522
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000523 // Walk over the blocks and find which ones correspond to catch block entries.
524 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
525 HBasicBlock* block = graph_->GetBlocks().Get(i);
526 if (block->IsCatchBlock()) {
527 intptr_t native_pc = GetAddressOf(block);
528 ++dex2pc_entries;
529 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
530 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
531 dex2pc_offset = native_pc;
532 dex2pc_dalvik_offset = block->GetDexPc();
533 }
534 }
535
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000536 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
537 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
538 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
539 data->resize(data_size);
540
541 uint8_t* data_ptr = &(*data)[0];
542 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000543
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000544 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
545 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
546 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
547 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
548
549 pc2dex_offset = 0u;
550 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000551 dex2pc_offset = 0u;
552 dex2pc_dalvik_offset = 0u;
553
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000554 for (size_t i = 0; i < pc2dex_entries; i++) {
555 struct PcInfo pc_info = pc_infos_.Get(i);
556 DCHECK(pc2dex_offset <= pc_info.native_pc);
557 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
558 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
559 pc2dex_offset = pc_info.native_pc;
560 pc2dex_dalvik_offset = pc_info.dex_pc;
561 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000562
563 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
564 HBasicBlock* block = graph_->GetBlocks().Get(i);
565 if (block->IsCatchBlock()) {
566 intptr_t native_pc = GetAddressOf(block);
567 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
568 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
569 dex2pc_offset = native_pc;
570 dex2pc_dalvik_offset = block->GetDexPc();
571 }
572 }
573
574
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000575 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
576 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
577
578 if (kIsDebugBuild) {
579 // Verify the encoded table holds the expected data.
580 MappingTable table(data_ptr);
581 CHECK_EQ(table.TotalSize(), total_entries);
582 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
583 auto it = table.PcToDexBegin();
584 auto it2 = table.DexToPcBegin();
585 for (size_t i = 0; i < pc2dex_entries; i++) {
586 struct PcInfo pc_info = pc_infos_.Get(i);
587 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
588 CHECK_EQ(pc_info.dex_pc, it.DexPc());
589 ++it;
590 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000591 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
592 HBasicBlock* block = graph_->GetBlocks().Get(i);
593 if (block->IsCatchBlock()) {
594 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
595 CHECK_EQ(block->GetDexPc(), it2.DexPc());
596 ++it2;
597 }
598 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000599 CHECK(it == table.PcToDexEnd());
600 CHECK(it2 == table.DexToPcEnd());
601 }
602}
603
604void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
605 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100606 // We currently don't use callee-saved registers.
607 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000608 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
609 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000610 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
611
612 *data = vmap_encoder.GetData();
613}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000614
Nicolas Geoffray39468442014-09-02 15:17:15 +0100615void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100616 uint32_t size = stack_map_stream_.PrepareForFillIn();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100617 data->resize(size);
618 MemoryRegion region(data->data(), size);
619 stack_map_stream_.FillIn(region);
620}
621
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000622void CodeGenerator::RecordPcInfo(HInstruction* instruction,
623 uint32_t dex_pc,
624 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000625 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000626 // The code generated for some type conversions may call the
627 // runtime, thus normally requiring a subsequent call to this
628 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000629 // information for certain instructions, which are considered "atomic"
630 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000631 // Therefore we do not currently record PC information for such
632 // instructions. As this may change later, we added this special
633 // case so that code generators may nevertheless call
634 // CodeGenerator::RecordPcInfo without triggering an error in
635 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
636 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000637 if (instruction->IsTypeConversion()) {
638 return;
639 }
640 if (instruction->IsRem()) {
641 Primitive::Type type = instruction->AsRem()->GetResultType();
642 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
643 return;
644 }
645 }
Roland Levillain624279f2014-12-04 11:54:28 +0000646 }
647
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100648 uint32_t outer_dex_pc = dex_pc;
649 uint32_t outer_environment_size = 0;
650 uint32_t inlining_depth = 0;
651 if (instruction != nullptr) {
652 for (HEnvironment* environment = instruction->GetEnvironment();
653 environment != nullptr;
654 environment = environment->GetParent()) {
655 outer_dex_pc = environment->GetDexPc();
656 outer_environment_size = environment->Size();
657 if (environment != instruction->GetEnvironment()) {
658 inlining_depth++;
659 }
660 }
661 }
662
Nicolas Geoffray39468442014-09-02 15:17:15 +0100663 // Collect PC infos for the mapping table.
664 struct PcInfo pc_info;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100665 pc_info.dex_pc = outer_dex_pc;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100666 pc_info.native_pc = GetAssembler()->CodeSize();
667 pc_infos_.Add(pc_info);
668
Nicolas Geoffray39468442014-09-02 15:17:15 +0100669 if (instruction == nullptr) {
670 // For stack overflow checks.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100671 stack_map_stream_.BeginStackMapEntry(pc_info.dex_pc, pc_info.native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100672 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000673 return;
674 }
675 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100676
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000677 uint32_t register_mask = locations->GetRegisterMask();
678 if (locations->OnlyCallsOnSlowPath()) {
679 // In case of slow path, we currently set the location of caller-save registers
680 // to register (instead of their stack location when pushed before the slow-path
681 // call). Therefore register_mask contains both callee-save and caller-save
682 // registers that hold objects. We must remove the caller-save from the mask, since
683 // they will be overwritten by the callee.
684 register_mask &= core_callee_save_mask_;
685 }
686 // The register mask must be a subset of callee-save registers.
687 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100688 stack_map_stream_.BeginStackMapEntry(pc_info.dex_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100689 pc_info.native_pc,
690 register_mask,
691 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100692 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100693 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100694
695 EmitEnvironment(instruction->GetEnvironment(), slow_path);
696 stack_map_stream_.EndStackMapEntry();
697}
698
699void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
700 if (environment == nullptr) return;
701
702 if (environment->GetParent() != nullptr) {
703 // We emit the parent environment first.
704 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100705 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
706 environment->GetDexPc(),
707 environment->GetInvokeType(),
708 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100709 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000710
711 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100712 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000713 HInstruction* current = environment->GetInstructionAt(i);
714 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100715 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000716 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100717 }
718
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100719 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000720 switch (location.GetKind()) {
721 case Location::kConstant: {
722 DCHECK_EQ(current, location.GetConstant());
723 if (current->IsLongConstant()) {
724 int64_t value = current->AsLongConstant()->GetValue();
725 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100726 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000727 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100728 DexRegisterLocation::Kind::kConstant, High32Bits(value));
729 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000730 DCHECK_LT(i, environment_size);
731 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000732 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000733 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100734 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000735 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100736 DexRegisterLocation::Kind::kConstant, High32Bits(value));
737 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000738 DCHECK_LT(i, environment_size);
739 } else if (current->IsIntConstant()) {
740 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100741 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000742 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100743 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000744 } else {
745 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000746 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100747 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000748 }
749 break;
750 }
751
752 case Location::kStackSlot: {
753 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100754 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000755 break;
756 }
757
758 case Location::kDoubleStackSlot: {
759 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100760 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000761 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100762 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
763 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000764 DCHECK_LT(i, environment_size);
765 break;
766 }
767
768 case Location::kRegister : {
769 int id = location.reg();
770 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
771 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100772 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000773 if (current->GetType() == Primitive::kPrimLong) {
774 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100775 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
776 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000777 DCHECK_LT(i, environment_size);
778 }
779 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100780 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000781 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100782 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
783 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000784 DCHECK_LT(i, environment_size);
785 }
786 }
787 break;
788 }
789
790 case Location::kFpuRegister : {
791 int id = location.reg();
792 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
793 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100794 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000795 if (current->GetType() == Primitive::kPrimDouble) {
796 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100797 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
798 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000799 DCHECK_LT(i, environment_size);
800 }
801 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100802 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000803 if (current->GetType() == Primitive::kPrimDouble) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100804 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
805 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000806 DCHECK_LT(i, environment_size);
807 }
808 }
809 break;
810 }
811
812 case Location::kFpuRegisterPair : {
813 int low = location.low();
814 int high = location.high();
815 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
816 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100817 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000818 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100819 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000820 }
821 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
822 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100823 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
824 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000825 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100826 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
827 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000828 }
829 DCHECK_LT(i, environment_size);
830 break;
831 }
832
833 case Location::kRegisterPair : {
834 int low = location.low();
835 int high = location.high();
836 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
837 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100838 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000839 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100840 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000841 }
842 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
843 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100844 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000845 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100846 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000847 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100848 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000849 DCHECK_LT(i, environment_size);
850 break;
851 }
852
853 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100854 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000855 break;
856 }
857
858 default:
859 LOG(FATAL) << "Unexpected kind " << location.GetKind();
860 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100861 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100862
863 if (environment->GetParent() != nullptr) {
864 stack_map_stream_.EndInlineInfoEntry();
865 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100866}
867
Calin Juravle77520bc2015-01-12 18:45:46 +0000868bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
869 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +0100870
871 return (first_next_not_move != nullptr)
872 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +0000873}
874
875void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
876 // If we are from a static path don't record the pc as we can't throw NPE.
877 // NB: having the checks here makes the code much less verbose in the arch
878 // specific code generators.
879 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
880 return;
881 }
882
883 if (!compiler_options_.GetImplicitNullChecks()) {
884 return;
885 }
886
Calin Juravle641547a2015-04-21 22:08:51 +0100887 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +0000888 return;
889 }
890
891 // Find the first previous instruction which is not a move.
892 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
893
894 // If the instruction is a null check it means that `instr` is the first user
895 // and needs to record the pc.
896 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
897 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
898 // TODO: The parallel moves modify the environment. Their changes need to be reverted
899 // otherwise the stack maps at the throw point will not be correct.
900 RecordPcInfo(null_check, null_check->GetDexPc());
901 }
902}
903
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100904void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
905 LocationSummary* locations = suspend_check->GetLocations();
906 HBasicBlock* block = suspend_check->GetBlock();
907 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
908 DCHECK(block->IsLoopHeader());
909
910 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
911 HInstruction* current = it.Current();
912 LiveInterval* interval = current->GetLiveInterval();
913 // We only need to clear bits of loop phis containing objects and allocated in register.
914 // Loop phis allocated on stack already have the object in the stack.
915 if (current->GetType() == Primitive::kPrimNot
916 && interval->HasRegister()
917 && interval->HasSpillSlot()) {
918 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
919 }
920 }
921}
922
Nicolas Geoffray90218252015-04-15 11:56:51 +0100923void CodeGenerator::EmitParallelMoves(Location from1,
924 Location to1,
925 Primitive::Type type1,
926 Location from2,
927 Location to2,
928 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000929 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +0100930 parallel_move.AddMove(from1, to1, type1, nullptr);
931 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000932 GetMoveResolver()->EmitNativeCode(&parallel_move);
933}
934
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000935void SlowPathCode::RecordPcInfo(CodeGenerator* codegen, HInstruction* instruction, uint32_t dex_pc) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000936 codegen->RecordPcInfo(instruction, dex_pc, this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000937}
938
939void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
940 RegisterSet* register_set = locations->GetLiveRegisters();
941 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
942 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
943 if (!codegen->IsCoreCalleeSaveRegister(i)) {
944 if (register_set->ContainsCoreRegister(i)) {
945 // If the register holds an object, update the stack mask.
946 if (locations->RegisterContainsObject(i)) {
947 locations->SetStackBit(stack_offset / kVRegSize);
948 }
949 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000950 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
951 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000952 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
953 }
954 }
955 }
956
957 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
958 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
959 if (register_set->ContainsFloatingPointRegister(i)) {
960 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000961 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
962 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000963 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
964 }
965 }
966 }
967}
968
969void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
970 RegisterSet* register_set = locations->GetLiveRegisters();
971 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
972 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
973 if (!codegen->IsCoreCalleeSaveRegister(i)) {
974 if (register_set->ContainsCoreRegister(i)) {
975 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
976 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
977 }
978 }
979 }
980
981 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
982 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
983 if (register_set->ContainsFloatingPointRegister(i)) {
984 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
985 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
986 }
987 }
988 }
989}
990
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000991} // namespace art