blob: 5163395cac54f55833afa91ca623ee7d8db86e27 [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();
103 DCHECK(CheckType(type, locations->GetEnvironmentAt(i)))
104 << type << " " << locations->GetEnvironmentAt(i);
105 } else {
106 DCHECK(locations->GetEnvironmentAt(i).IsInvalid())
107 << locations->GetEnvironmentAt(i);
108 }
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) {
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100156 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000157 DCHECK_EQ(current_block_index_, 0u);
158 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100159 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000160 for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) {
161 HBasicBlock* block = block_order_->Get(current_block_index_);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000162 // Don't generate code for an empty block. Its predecessors will branch to its successor
163 // directly. Also, the label of that block will not be emitted, so this helps catch
164 // errors where we reference that label.
David Brazdil46e2a392015-03-16 17:31:52 +0000165 if (block->IsSingleGoto()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100166 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100167 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
168 HInstruction* current = it.Current();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000169 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000170 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000171 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100172 DCHECK(CheckTypeConsistency(current));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100173 current->Accept(instruction_visitor);
174 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000175 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000176
177 // Generate the slow paths.
178 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
179 slow_paths_.Get(i)->EmitNativeCode(this);
180 }
181
182 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000183 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000184}
185
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100186void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000187 // The register allocator already called `InitializeCodeGeneration`,
188 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000189 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100190 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000191 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000192}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100193
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000194void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100195 size_t code_size = GetAssembler()->CodeSize();
196 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000197
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100198 MemoryRegion code(buffer, code_size);
199 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000200}
201
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100202size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
203 for (size_t i = 0; i < length; ++i) {
204 if (!array[i]) {
205 array[i] = true;
206 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100207 }
208 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100209 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000210 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000211}
212
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000213size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
214 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000215 if (!array[i] && !array[i + 1]) {
216 array[i] = true;
217 array[i + 1] = true;
218 return i;
219 }
220 }
221 LOG(FATAL) << "Could not find a register in baseline register allocator";
222 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100223}
224
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000225void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
226 size_t maximum_number_of_live_core_registers,
227 size_t maximum_number_of_live_fp_registers,
228 size_t number_of_out_slots,
229 const GrowableArray<HBasicBlock*>& block_order) {
230 block_order_ = &block_order;
231 DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock());
232 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), block_order_->Get(1)));
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000233 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100234 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
235
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000236 if (number_of_spill_slots == 0
237 && !HasAllocatedCalleeSaveRegisters()
238 && IsLeafMethod()
239 && !RequiresCurrentMethod()) {
240 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
241 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
242 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
243 } else {
244 SetFrameSize(RoundUp(
245 number_of_spill_slots * kVRegSize
246 + number_of_out_slots * kVRegSize
247 + maximum_number_of_live_core_registers * GetWordSize()
248 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
249 + FrameEntrySpillSize(),
250 kStackAlignment));
251 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100252}
253
254Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
255 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000256 // The type of the previous instruction tells us if we need a single or double stack slot.
257 Primitive::Type type = temp->GetType();
258 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100259 // Use the temporary region (right below the dex registers).
260 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
261 - kVRegSize // filler
262 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000263 - ((temp_size + temp->GetIndex()) * kVRegSize);
264 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100265}
266
267int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
268 uint16_t reg_number = local->GetRegNumber();
269 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
270 if (reg_number >= number_of_locals) {
271 // Local is a parameter of the method. It is stored in the caller's frame.
272 return GetFrameSize() + kVRegSize // ART method
273 + (reg_number - number_of_locals) * kVRegSize;
274 } else {
275 // Local is a temporary in this method. It is stored in this method's frame.
276 return GetFrameSize() - FrameEntrySpillSize()
277 - kVRegSize // filler.
278 - (number_of_locals * kVRegSize)
279 + (reg_number * kVRegSize);
280 }
281}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100282
Mark Mendell5f874182015-03-04 15:42:45 -0500283void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
284 // The DCHECKS below check that a register is not specified twice in
285 // the summary. The out location can overlap with an input, so we need
286 // to special case it.
287 if (location.IsRegister()) {
288 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
289 blocked_core_registers_[location.reg()] = true;
290 } else if (location.IsFpuRegister()) {
291 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
292 blocked_fpu_registers_[location.reg()] = true;
293 } else if (location.IsFpuRegisterPair()) {
294 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
295 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
296 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
297 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
298 } else if (location.IsRegisterPair()) {
299 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
300 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
301 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
302 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
303 }
304}
305
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100306void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
307 LocationSummary* locations = instruction->GetLocations();
308 if (locations == nullptr) return;
309
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100310 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
311 blocked_core_registers_[i] = false;
312 }
313
314 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
315 blocked_fpu_registers_[i] = false;
316 }
317
318 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
319 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100320 }
321
322 // Mark all fixed input, temp and output registers as used.
323 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500324 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100325 }
326
327 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
328 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500329 BlockIfInRegister(loc);
330 }
331 Location result_location = locations->Out();
332 if (locations->OutputCanOverlapWithInputs()) {
333 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100334 }
335
Mark Mendell5f874182015-03-04 15:42:45 -0500336 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100337
338 // Allocate all unallocated input locations.
339 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
340 Location loc = locations->InAt(i);
341 HInstruction* input = instruction->InputAt(i);
342 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100343 if ((loc.GetPolicy() == Location::kRequiresRegister)
344 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100345 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100346 } else {
347 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
348 HLoadLocal* load = input->AsLoadLocal();
349 if (load != nullptr) {
350 loc = GetStackLocation(load);
351 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100352 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100353 }
354 }
355 locations->SetInAt(i, loc);
356 }
357 }
358
359 // Allocate all unallocated temp locations.
360 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
361 Location loc = locations->GetTemp(i);
362 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000363 switch (loc.GetPolicy()) {
364 case Location::kRequiresRegister:
365 // Allocate a core register (large enough to fit a 32-bit integer).
366 loc = AllocateFreeRegister(Primitive::kPrimInt);
367 break;
368
369 case Location::kRequiresFpuRegister:
370 // Allocate a core register (large enough to fit a 64-bit double).
371 loc = AllocateFreeRegister(Primitive::kPrimDouble);
372 break;
373
374 default:
375 LOG(FATAL) << "Unexpected policy for temporary location "
376 << loc.GetPolicy();
377 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100378 locations->SetTempAt(i, loc);
379 }
380 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100381 if (result_location.IsUnallocated()) {
382 switch (result_location.GetPolicy()) {
383 case Location::kAny:
384 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100385 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100386 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100387 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 case Location::kSameAsFirstInput:
389 result_location = locations->InAt(0);
390 break;
391 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000392 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100393 }
394}
395
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000396void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
397 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100398 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100399 if (instruction->IsTemporary()) {
400 HInstruction* previous = instruction->GetPrevious();
401 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
402 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100403 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100404 return;
405 }
406 AllocateRegistersLocally(instruction);
407 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000408 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000409 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000410 if (location.IsValid()) {
411 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000412 if (input->GetNext()->IsTemporary()) {
413 // If the input was stored in a temporary, use that temporary to
414 // perform the move.
415 Move(input->GetNext(), location, instruction);
416 } else {
417 Move(input, location, instruction);
418 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000419 }
420 }
421}
422
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000423void CodeGenerator::AllocateLocations(HInstruction* instruction) {
424 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100425 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000426 LocationSummary* locations = instruction->GetLocations();
427 if (!instruction->IsSuspendCheckEntry()) {
428 if (locations != nullptr && locations->CanCall()) {
429 MarkNotLeaf();
430 }
431 if (instruction->NeedsCurrentMethod()) {
432 SetRequiresCurrentMethod();
433 }
434 }
435}
436
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000437CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000438 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000439 const InstructionSetFeatures& isa_features,
440 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000441 switch (instruction_set) {
442 case kArm:
443 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000444 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000445 *isa_features.AsArmInstructionSetFeatures(),
446 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000447 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100448 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000449 return new arm64::CodeGeneratorARM64(graph,
450 *isa_features.AsArm64InstructionSetFeatures(),
451 compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100452 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000453 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000454 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000455 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400456 return new x86::CodeGeneratorX86(graph,
457 *isa_features.AsX86InstructionSetFeatures(),
458 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000459 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700460 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400461 return new x86_64::CodeGeneratorX86_64(graph,
462 *isa_features.AsX86_64InstructionSetFeatures(),
463 compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700464 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000465 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000466 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000467 }
468}
469
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000470void CodeGenerator::BuildNativeGCMap(
471 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
472 const std::vector<uint8_t>& gc_map_raw =
473 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
474 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
475
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000476 uint32_t max_native_offset = 0;
477 for (size_t i = 0; i < pc_infos_.Size(); i++) {
478 uint32_t native_offset = pc_infos_.Get(i).native_pc;
479 if (native_offset > max_native_offset) {
480 max_native_offset = native_offset;
481 }
482 }
483
484 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
485 for (size_t i = 0; i < pc_infos_.Size(); i++) {
486 struct PcInfo pc_info = pc_infos_.Get(i);
487 uint32_t native_offset = pc_info.native_pc;
488 uint32_t dex_pc = pc_info.dex_pc;
489 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800490 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000491 builder.AddEntry(native_offset, references);
492 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000493}
494
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100495void CodeGenerator::BuildSourceMap(DefaultSrcMap* src_map) const {
496 for (size_t i = 0; i < pc_infos_.Size(); i++) {
497 struct PcInfo pc_info = pc_infos_.Get(i);
498 uint32_t pc2dex_offset = pc_info.native_pc;
499 int32_t pc2dex_dalvik_offset = pc_info.dex_pc;
500 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
501 }
502}
503
504void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000505 uint32_t pc2dex_data_size = 0u;
506 uint32_t pc2dex_entries = pc_infos_.Size();
507 uint32_t pc2dex_offset = 0u;
508 int32_t pc2dex_dalvik_offset = 0;
509 uint32_t dex2pc_data_size = 0u;
510 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000511 uint32_t dex2pc_offset = 0u;
512 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000513
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000514 for (size_t i = 0; i < pc2dex_entries; i++) {
515 struct PcInfo pc_info = pc_infos_.Get(i);
516 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
517 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
518 pc2dex_offset = pc_info.native_pc;
519 pc2dex_dalvik_offset = pc_info.dex_pc;
520 }
521
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000522 // Walk over the blocks and find which ones correspond to catch block entries.
523 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
524 HBasicBlock* block = graph_->GetBlocks().Get(i);
525 if (block->IsCatchBlock()) {
526 intptr_t native_pc = GetAddressOf(block);
527 ++dex2pc_entries;
528 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
529 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
530 dex2pc_offset = native_pc;
531 dex2pc_dalvik_offset = block->GetDexPc();
532 }
533 }
534
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000535 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
536 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
537 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
538 data->resize(data_size);
539
540 uint8_t* data_ptr = &(*data)[0];
541 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000542
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000543 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
544 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
545 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
546 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
547
548 pc2dex_offset = 0u;
549 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000550 dex2pc_offset = 0u;
551 dex2pc_dalvik_offset = 0u;
552
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000553 for (size_t i = 0; i < pc2dex_entries; i++) {
554 struct PcInfo pc_info = pc_infos_.Get(i);
555 DCHECK(pc2dex_offset <= pc_info.native_pc);
556 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
557 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
558 pc2dex_offset = pc_info.native_pc;
559 pc2dex_dalvik_offset = pc_info.dex_pc;
560 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000561
562 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
563 HBasicBlock* block = graph_->GetBlocks().Get(i);
564 if (block->IsCatchBlock()) {
565 intptr_t native_pc = GetAddressOf(block);
566 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
567 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
568 dex2pc_offset = native_pc;
569 dex2pc_dalvik_offset = block->GetDexPc();
570 }
571 }
572
573
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000574 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
575 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
576
577 if (kIsDebugBuild) {
578 // Verify the encoded table holds the expected data.
579 MappingTable table(data_ptr);
580 CHECK_EQ(table.TotalSize(), total_entries);
581 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
582 auto it = table.PcToDexBegin();
583 auto it2 = table.DexToPcBegin();
584 for (size_t i = 0; i < pc2dex_entries; i++) {
585 struct PcInfo pc_info = pc_infos_.Get(i);
586 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
587 CHECK_EQ(pc_info.dex_pc, it.DexPc());
588 ++it;
589 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000590 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
591 HBasicBlock* block = graph_->GetBlocks().Get(i);
592 if (block->IsCatchBlock()) {
593 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
594 CHECK_EQ(block->GetDexPc(), it2.DexPc());
595 ++it2;
596 }
597 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000598 CHECK(it == table.PcToDexEnd());
599 CHECK(it2 == table.DexToPcEnd());
600 }
601}
602
603void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
604 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100605 // We currently don't use callee-saved registers.
606 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000607 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
608 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000609 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
610
611 *data = vmap_encoder.GetData();
612}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000613
Nicolas Geoffray39468442014-09-02 15:17:15 +0100614void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100615 uint32_t size = stack_map_stream_.PrepareForFillIn();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100616 data->resize(size);
617 MemoryRegion region(data->data(), size);
618 stack_map_stream_.FillIn(region);
619}
620
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000621void CodeGenerator::RecordPcInfo(HInstruction* instruction,
622 uint32_t dex_pc,
623 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000624 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000625 // The code generated for some type conversions may call the
626 // runtime, thus normally requiring a subsequent call to this
627 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000628 // information for certain instructions, which are considered "atomic"
629 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000630 // Therefore we do not currently record PC information for such
631 // instructions. As this may change later, we added this special
632 // case so that code generators may nevertheless call
633 // CodeGenerator::RecordPcInfo without triggering an error in
634 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
635 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000636 if (instruction->IsTypeConversion()) {
637 return;
638 }
639 if (instruction->IsRem()) {
640 Primitive::Type type = instruction->AsRem()->GetResultType();
641 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
642 return;
643 }
644 }
Roland Levillain624279f2014-12-04 11:54:28 +0000645 }
646
Nicolas Geoffray39468442014-09-02 15:17:15 +0100647 // Collect PC infos for the mapping table.
648 struct PcInfo pc_info;
649 pc_info.dex_pc = dex_pc;
650 pc_info.native_pc = GetAssembler()->CodeSize();
651 pc_infos_.Add(pc_info);
652
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000653 uint32_t inlining_depth = 0;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000654
Nicolas Geoffray39468442014-09-02 15:17:15 +0100655 if (instruction == nullptr) {
656 // For stack overflow checks.
Calin Juravle4f46ac52015-04-23 18:47:21 +0100657 stack_map_stream_.BeginStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, inlining_depth);
658 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000659 return;
660 }
661 LocationSummary* locations = instruction->GetLocations();
662 HEnvironment* environment = instruction->GetEnvironment();
663 size_t environment_size = instruction->EnvironmentSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100664
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000665 uint32_t register_mask = locations->GetRegisterMask();
666 if (locations->OnlyCallsOnSlowPath()) {
667 // In case of slow path, we currently set the location of caller-save registers
668 // to register (instead of their stack location when pushed before the slow-path
669 // call). Therefore register_mask contains both callee-save and caller-save
670 // registers that hold objects. We must remove the caller-save from the mask, since
671 // they will be overwritten by the callee.
672 register_mask &= core_callee_save_mask_;
673 }
674 // The register mask must be a subset of callee-save registers.
675 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100676 stack_map_stream_.BeginStackMapEntry(dex_pc,
677 pc_info.native_pc,
678 register_mask,
679 locations->GetStackMask(),
680 environment_size,
681 inlining_depth);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000682
683 // Walk over the environment, and record the location of dex registers.
684 for (size_t i = 0; i < environment_size; ++i) {
685 HInstruction* current = environment->GetInstructionAt(i);
686 if (current == nullptr) {
687 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kNone, 0);
688 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100689 }
690
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000691 Location location = locations->GetEnvironmentAt(i);
692 switch (location.GetKind()) {
693 case Location::kConstant: {
694 DCHECK_EQ(current, location.GetConstant());
695 if (current->IsLongConstant()) {
696 int64_t value = current->AsLongConstant()->GetValue();
697 stack_map_stream_.AddDexRegisterEntry(
698 i, DexRegisterLocation::Kind::kConstant, Low32Bits(value));
699 stack_map_stream_.AddDexRegisterEntry(
700 ++i, DexRegisterLocation::Kind::kConstant, High32Bits(value));
701 DCHECK_LT(i, environment_size);
702 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000703 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000704 stack_map_stream_.AddDexRegisterEntry(
705 i, DexRegisterLocation::Kind::kConstant, Low32Bits(value));
706 stack_map_stream_.AddDexRegisterEntry(
707 ++i, DexRegisterLocation::Kind::kConstant, High32Bits(value));
708 DCHECK_LT(i, environment_size);
709 } else if (current->IsIntConstant()) {
710 int32_t value = current->AsIntConstant()->GetValue();
711 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, value);
712 } else if (current->IsNullConstant()) {
713 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, 0);
714 } else {
715 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000716 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000717 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, value);
718 }
719 break;
720 }
721
722 case Location::kStackSlot: {
723 stack_map_stream_.AddDexRegisterEntry(
724 i, DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
725 break;
726 }
727
728 case Location::kDoubleStackSlot: {
729 stack_map_stream_.AddDexRegisterEntry(
730 i, DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
731 stack_map_stream_.AddDexRegisterEntry(
732 ++i, DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
733 DCHECK_LT(i, environment_size);
734 break;
735 }
736
737 case Location::kRegister : {
738 int id = location.reg();
739 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
740 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
741 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
742 if (current->GetType() == Primitive::kPrimLong) {
743 stack_map_stream_.AddDexRegisterEntry(
744 ++i, DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
745 DCHECK_LT(i, environment_size);
746 }
747 } else {
748 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInRegister, id);
749 if (current->GetType() == Primitive::kPrimLong) {
750 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInRegister, id);
751 DCHECK_LT(i, environment_size);
752 }
753 }
754 break;
755 }
756
757 case Location::kFpuRegister : {
758 int id = location.reg();
759 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
760 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
761 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
762 if (current->GetType() == Primitive::kPrimDouble) {
763 stack_map_stream_.AddDexRegisterEntry(
764 ++i, DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
765 DCHECK_LT(i, environment_size);
766 }
767 } else {
768 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInFpuRegister, id);
769 if (current->GetType() == Primitive::kPrimDouble) {
770 stack_map_stream_.AddDexRegisterEntry(
771 ++i, DexRegisterLocation::Kind::kInFpuRegister, id);
772 DCHECK_LT(i, environment_size);
773 }
774 }
775 break;
776 }
777
778 case Location::kFpuRegisterPair : {
779 int low = location.low();
780 int high = location.high();
781 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
782 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
783 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
784 } else {
785 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInFpuRegister, low);
786 }
787 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
788 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
789 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInStack, offset);
790 } else {
791 stack_map_stream_.AddDexRegisterEntry(
792 ++i, DexRegisterLocation::Kind::kInFpuRegister, high);
793 }
794 DCHECK_LT(i, environment_size);
795 break;
796 }
797
798 case Location::kRegisterPair : {
799 int low = location.low();
800 int high = location.high();
801 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
802 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
803 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
804 } else {
805 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInRegister, low);
806 }
807 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
808 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
809 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInStack, offset);
810 } else {
811 stack_map_stream_.AddDexRegisterEntry(
812 ++i, DexRegisterLocation::Kind::kInRegister, high);
813 }
814 DCHECK_LT(i, environment_size);
815 break;
816 }
817
818 case Location::kInvalid: {
819 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kNone, 0);
820 break;
821 }
822
823 default:
824 LOG(FATAL) << "Unexpected kind " << location.GetKind();
825 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100826 }
Calin Juravle4f46ac52015-04-23 18:47:21 +0100827 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100828}
829
Calin Juravle77520bc2015-01-12 18:45:46 +0000830bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
831 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +0100832
833 return (first_next_not_move != nullptr)
834 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +0000835}
836
837void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
838 // If we are from a static path don't record the pc as we can't throw NPE.
839 // NB: having the checks here makes the code much less verbose in the arch
840 // specific code generators.
841 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
842 return;
843 }
844
845 if (!compiler_options_.GetImplicitNullChecks()) {
846 return;
847 }
848
Calin Juravle641547a2015-04-21 22:08:51 +0100849 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +0000850 return;
851 }
852
853 // Find the first previous instruction which is not a move.
854 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
855
856 // If the instruction is a null check it means that `instr` is the first user
857 // and needs to record the pc.
858 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
859 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
860 // TODO: The parallel moves modify the environment. Their changes need to be reverted
861 // otherwise the stack maps at the throw point will not be correct.
862 RecordPcInfo(null_check, null_check->GetDexPc());
863 }
864}
865
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100866void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
867 LocationSummary* locations = suspend_check->GetLocations();
868 HBasicBlock* block = suspend_check->GetBlock();
869 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
870 DCHECK(block->IsLoopHeader());
871
872 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
873 HInstruction* current = it.Current();
874 LiveInterval* interval = current->GetLiveInterval();
875 // We only need to clear bits of loop phis containing objects and allocated in register.
876 // Loop phis allocated on stack already have the object in the stack.
877 if (current->GetType() == Primitive::kPrimNot
878 && interval->HasRegister()
879 && interval->HasSpillSlot()) {
880 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
881 }
882 }
883}
884
Nicolas Geoffray90218252015-04-15 11:56:51 +0100885void CodeGenerator::EmitParallelMoves(Location from1,
886 Location to1,
887 Primitive::Type type1,
888 Location from2,
889 Location to2,
890 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000891 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +0100892 parallel_move.AddMove(from1, to1, type1, nullptr);
893 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000894 GetMoveResolver()->EmitNativeCode(&parallel_move);
895}
896
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000897void SlowPathCode::RecordPcInfo(CodeGenerator* codegen, HInstruction* instruction, uint32_t dex_pc) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000898 codegen->RecordPcInfo(instruction, dex_pc, this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000899}
900
901void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
902 RegisterSet* register_set = locations->GetLiveRegisters();
903 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
904 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
905 if (!codegen->IsCoreCalleeSaveRegister(i)) {
906 if (register_set->ContainsCoreRegister(i)) {
907 // If the register holds an object, update the stack mask.
908 if (locations->RegisterContainsObject(i)) {
909 locations->SetStackBit(stack_offset / kVRegSize);
910 }
911 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000912 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
913 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000914 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
915 }
916 }
917 }
918
919 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
920 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
921 if (register_set->ContainsFloatingPointRegister(i)) {
922 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000923 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
924 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000925 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
926 }
927 }
928 }
929}
930
931void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
932 RegisterSet* register_set = locations->GetLiveRegisters();
933 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
934 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
935 if (!codegen->IsCoreCalleeSaveRegister(i)) {
936 if (register_set->ContainsCoreRegister(i)) {
937 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
938 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
939 }
940 }
941 }
942
943 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
944 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
945 if (register_set->ContainsFloatingPointRegister(i)) {
946 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
947 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
948 }
949 }
950 }
951}
952
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000953} // namespace art