blob: c106d3064c2163da53c2b2a2d3d8883625f478a5 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator.h"
18
19#include "code_generator_arm.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010020#include "code_generator_arm64.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000021#include "code_generator_x86.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "code_generator_x86_64.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070023#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000024#include "dex/verified_method.h"
25#include "driver/dex_compilation_unit.h"
26#include "gc_map_builder.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000027#include "leb128.h"
28#include "mapping_table.h"
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010029#include "mirror/array-inl.h"
30#include "mirror/object_array-inl.h"
31#include "mirror/object_reference.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010032#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000034#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000035#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036
37namespace art {
38
Alexandre Rames88c13cd2015-04-14 17:35:39 +010039// Return whether a location is consistent with a type.
40static bool CheckType(Primitive::Type type, Location location) {
41 if (location.IsFpuRegister()
42 || (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresFpuRegister))) {
43 return (type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble);
44 } else if (location.IsRegister() ||
45 (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresRegister))) {
46 return Primitive::IsIntegralType(type) || (type == Primitive::kPrimNot);
47 } else if (location.IsRegisterPair()) {
48 return type == Primitive::kPrimLong;
49 } else if (location.IsFpuRegisterPair()) {
50 return type == Primitive::kPrimDouble;
51 } else if (location.IsStackSlot()) {
52 return (Primitive::IsIntegralType(type) && type != Primitive::kPrimLong)
53 || (type == Primitive::kPrimFloat)
54 || (type == Primitive::kPrimNot);
55 } else if (location.IsDoubleStackSlot()) {
56 return (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
57 } else if (location.IsConstant()) {
58 if (location.GetConstant()->IsIntConstant()) {
59 return Primitive::IsIntegralType(type) && (type != Primitive::kPrimLong);
60 } else if (location.GetConstant()->IsNullConstant()) {
61 return type == Primitive::kPrimNot;
62 } else if (location.GetConstant()->IsLongConstant()) {
63 return type == Primitive::kPrimLong;
64 } else if (location.GetConstant()->IsFloatConstant()) {
65 return type == Primitive::kPrimFloat;
66 } else {
67 return location.GetConstant()->IsDoubleConstant()
68 && (type == Primitive::kPrimDouble);
69 }
70 } else {
71 return location.IsInvalid() || (location.GetPolicy() == Location::kAny);
72 }
73}
74
75// Check that a location summary is consistent with an instruction.
76static bool CheckTypeConsistency(HInstruction* instruction) {
77 LocationSummary* locations = instruction->GetLocations();
78 if (locations == nullptr) {
79 return true;
80 }
81
82 if (locations->Out().IsUnallocated()
83 && (locations->Out().GetPolicy() == Location::kSameAsFirstInput)) {
84 DCHECK(CheckType(instruction->GetType(), locations->InAt(0)))
85 << instruction->GetType()
86 << " " << locations->InAt(0);
87 } else {
88 DCHECK(CheckType(instruction->GetType(), locations->Out()))
89 << instruction->GetType()
90 << " " << locations->Out();
91 }
92
93 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
94 DCHECK(CheckType(instruction->InputAt(i)->GetType(), locations->InAt(i)))
95 << instruction->InputAt(i)->GetType()
96 << " " << locations->InAt(i);
97 }
98
99 HEnvironment* environment = instruction->GetEnvironment();
100 for (size_t i = 0; i < instruction->EnvironmentSize(); ++i) {
101 if (environment->GetInstructionAt(i) != nullptr) {
102 Primitive::Type type = environment->GetInstructionAt(i)->GetType();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100103 DCHECK(CheckType(type, environment->GetLocationAt(i)))
104 << type << " " << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100105 } else {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100106 DCHECK(environment->GetLocationAt(i).IsInvalid())
107 << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100108 }
109 }
110 return true;
111}
112
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100113size_t CodeGenerator::GetCacheOffset(uint32_t index) {
114 return mirror::ObjectArray<mirror::Object>::OffsetOfElement(index).SizeValue();
115}
116
Mathieu Chartiere401d142015-04-22 13:56:20 -0700117size_t CodeGenerator::GetCachePointerOffset(uint32_t index) {
118 auto pointer_size = InstructionSetPointerSize(GetInstructionSet());
119 return mirror::Array::DataOffset(pointer_size).Uint32Value() + pointer_size * index;
120}
121
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100122void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000123 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100124 if (!is_leaf) {
125 MarkNotLeaf();
126 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000127 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
128 + GetGraph()->GetTemporariesVRegSlots()
129 + 1 /* filler */,
130 0, /* the baseline compiler does not have live registers at slow path */
131 0, /* the baseline compiler does not have live registers at slow path */
132 GetGraph()->GetMaximumNumberOfOutVRegs()
133 + 1 /* current method */,
134 GetGraph()->GetBlocks());
135 CompileInternal(allocator, /* is_baseline */ true);
136}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100137
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000138bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
139 DCHECK_EQ(block_order_->Get(current_block_index_), current);
140 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
141}
142
143HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
144 for (size_t i = current_block_index_ + 1; i < block_order_->Size(); ++i) {
145 HBasicBlock* block = block_order_->Get(i);
David Brazdil46e2a392015-03-16 17:31:52 +0000146 if (!block->IsSingleGoto()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000147 return block;
148 }
149 }
150 return nullptr;
151}
152
153HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdil46e2a392015-03-16 17:31:52 +0000154 while (block->IsSingleGoto()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000155 block = block->GetSuccessors().Get(0);
156 }
157 return block;
158}
159
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000160void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Roland Levillain3e3d7332015-04-28 11:00:54 +0100161 is_baseline_ = is_baseline;
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100162 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000163 DCHECK_EQ(current_block_index_, 0u);
164 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100165 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000166 for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) {
167 HBasicBlock* block = block_order_->Get(current_block_index_);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000168 // Don't generate code for an empty block. Its predecessors will branch to its successor
169 // directly. Also, the label of that block will not be emitted, so this helps catch
170 // errors where we reference that label.
David Brazdil46e2a392015-03-16 17:31:52 +0000171 if (block->IsSingleGoto()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100172 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100173 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
174 HInstruction* current = it.Current();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000175 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000176 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000177 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100178 DCHECK(CheckTypeConsistency(current));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100179 current->Accept(instruction_visitor);
180 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000181 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000182
183 // Generate the slow paths.
184 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
185 slow_paths_.Get(i)->EmitNativeCode(this);
186 }
187
188 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000189 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000190}
191
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100192void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000193 // The register allocator already called `InitializeCodeGeneration`,
194 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000195 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100196 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000197 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000198}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100199
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000200void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100201 size_t code_size = GetAssembler()->CodeSize();
202 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000203
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100204 MemoryRegion code(buffer, code_size);
205 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000206}
207
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100208size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
209 for (size_t i = 0; i < length; ++i) {
210 if (!array[i]) {
211 array[i] = true;
212 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100213 }
214 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100215 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000216 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000217}
218
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000219size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
220 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000221 if (!array[i] && !array[i + 1]) {
222 array[i] = true;
223 array[i + 1] = true;
224 return i;
225 }
226 }
227 LOG(FATAL) << "Could not find a register in baseline register allocator";
228 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100229}
230
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000231void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
232 size_t maximum_number_of_live_core_registers,
233 size_t maximum_number_of_live_fp_registers,
234 size_t number_of_out_slots,
235 const GrowableArray<HBasicBlock*>& block_order) {
236 block_order_ = &block_order;
237 DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock());
238 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), block_order_->Get(1)));
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000239 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100240 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
241
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000242 if (number_of_spill_slots == 0
243 && !HasAllocatedCalleeSaveRegisters()
244 && IsLeafMethod()
245 && !RequiresCurrentMethod()) {
246 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
247 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
248 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
249 } else {
250 SetFrameSize(RoundUp(
251 number_of_spill_slots * kVRegSize
252 + number_of_out_slots * kVRegSize
253 + maximum_number_of_live_core_registers * GetWordSize()
254 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
255 + FrameEntrySpillSize(),
256 kStackAlignment));
257 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100258}
259
260Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
261 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000262 // The type of the previous instruction tells us if we need a single or double stack slot.
263 Primitive::Type type = temp->GetType();
264 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100265 // Use the temporary region (right below the dex registers).
266 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
267 - kVRegSize // filler
268 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000269 - ((temp_size + temp->GetIndex()) * kVRegSize);
270 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100271}
272
273int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
274 uint16_t reg_number = local->GetRegNumber();
275 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
276 if (reg_number >= number_of_locals) {
277 // Local is a parameter of the method. It is stored in the caller's frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode.
279 return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100280 + (reg_number - number_of_locals) * kVRegSize;
281 } else {
282 // Local is a temporary in this method. It is stored in this method's frame.
283 return GetFrameSize() - FrameEntrySpillSize()
284 - kVRegSize // filler.
285 - (number_of_locals * kVRegSize)
286 + (reg_number * kVRegSize);
287 }
288}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100289
Mark Mendell5f874182015-03-04 15:42:45 -0500290void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
291 // The DCHECKS below check that a register is not specified twice in
292 // the summary. The out location can overlap with an input, so we need
293 // to special case it.
294 if (location.IsRegister()) {
295 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
296 blocked_core_registers_[location.reg()] = true;
297 } else if (location.IsFpuRegister()) {
298 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
299 blocked_fpu_registers_[location.reg()] = true;
300 } else if (location.IsFpuRegisterPair()) {
301 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
302 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
303 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
304 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
305 } else if (location.IsRegisterPair()) {
306 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
307 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
308 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
309 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
310 }
311}
312
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100313void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
314 LocationSummary* locations = instruction->GetLocations();
315 if (locations == nullptr) return;
316
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100317 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
318 blocked_core_registers_[i] = false;
319 }
320
321 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
322 blocked_fpu_registers_[i] = false;
323 }
324
325 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
326 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100327 }
328
329 // Mark all fixed input, temp and output registers as used.
330 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500331 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100332 }
333
334 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
335 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500336 BlockIfInRegister(loc);
337 }
338 Location result_location = locations->Out();
339 if (locations->OutputCanOverlapWithInputs()) {
340 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100341 }
342
Mark Mendell5f874182015-03-04 15:42:45 -0500343 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100344
345 // Allocate all unallocated input locations.
346 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
347 Location loc = locations->InAt(i);
348 HInstruction* input = instruction->InputAt(i);
349 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100350 if ((loc.GetPolicy() == Location::kRequiresRegister)
351 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100352 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100353 } else {
354 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
355 HLoadLocal* load = input->AsLoadLocal();
356 if (load != nullptr) {
357 loc = GetStackLocation(load);
358 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100359 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100360 }
361 }
362 locations->SetInAt(i, loc);
363 }
364 }
365
366 // Allocate all unallocated temp locations.
367 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
368 Location loc = locations->GetTemp(i);
369 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000370 switch (loc.GetPolicy()) {
371 case Location::kRequiresRegister:
372 // Allocate a core register (large enough to fit a 32-bit integer).
373 loc = AllocateFreeRegister(Primitive::kPrimInt);
374 break;
375
376 case Location::kRequiresFpuRegister:
377 // Allocate a core register (large enough to fit a 64-bit double).
378 loc = AllocateFreeRegister(Primitive::kPrimDouble);
379 break;
380
381 default:
382 LOG(FATAL) << "Unexpected policy for temporary location "
383 << loc.GetPolicy();
384 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100385 locations->SetTempAt(i, loc);
386 }
387 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100388 if (result_location.IsUnallocated()) {
389 switch (result_location.GetPolicy()) {
390 case Location::kAny:
391 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100392 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100393 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100394 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100395 case Location::kSameAsFirstInput:
396 result_location = locations->InAt(0);
397 break;
398 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000399 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100400 }
401}
402
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000403void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
404 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100405 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100406 if (instruction->IsTemporary()) {
407 HInstruction* previous = instruction->GetPrevious();
408 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
409 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100410 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100411 return;
412 }
413 AllocateRegistersLocally(instruction);
414 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000415 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000416 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000417 if (location.IsValid()) {
418 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000419 if (input->GetNext()->IsTemporary()) {
420 // If the input was stored in a temporary, use that temporary to
421 // perform the move.
422 Move(input->GetNext(), location, instruction);
423 } else {
424 Move(input, location, instruction);
425 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000426 }
427 }
428}
429
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000430void CodeGenerator::AllocateLocations(HInstruction* instruction) {
431 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100432 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000433 LocationSummary* locations = instruction->GetLocations();
434 if (!instruction->IsSuspendCheckEntry()) {
435 if (locations != nullptr && locations->CanCall()) {
436 MarkNotLeaf();
437 }
438 if (instruction->NeedsCurrentMethod()) {
439 SetRequiresCurrentMethod();
440 }
441 }
442}
443
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000444CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000445 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000446 const InstructionSetFeatures& isa_features,
447 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000448 switch (instruction_set) {
449 case kArm:
450 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000451 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000452 *isa_features.AsArmInstructionSetFeatures(),
453 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000454 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100455 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000456 return new arm64::CodeGeneratorARM64(graph,
457 *isa_features.AsArm64InstructionSetFeatures(),
458 compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100459 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000460 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000461 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000462 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400463 return new x86::CodeGeneratorX86(graph,
464 *isa_features.AsX86InstructionSetFeatures(),
465 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000466 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700467 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400468 return new x86_64::CodeGeneratorX86_64(graph,
469 *isa_features.AsX86_64InstructionSetFeatures(),
470 compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700471 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000472 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000473 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000474 }
475}
476
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000477void CodeGenerator::BuildNativeGCMap(
478 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
479 const std::vector<uint8_t>& gc_map_raw =
480 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
481 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
482
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000483 uint32_t max_native_offset = 0;
484 for (size_t i = 0; i < pc_infos_.Size(); i++) {
485 uint32_t native_offset = pc_infos_.Get(i).native_pc;
486 if (native_offset > max_native_offset) {
487 max_native_offset = native_offset;
488 }
489 }
490
491 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
492 for (size_t i = 0; i < pc_infos_.Size(); i++) {
493 struct PcInfo pc_info = pc_infos_.Get(i);
494 uint32_t native_offset = pc_info.native_pc;
495 uint32_t dex_pc = pc_info.dex_pc;
496 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800497 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000498 builder.AddEntry(native_offset, references);
499 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000500}
501
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100502void CodeGenerator::BuildSourceMap(DefaultSrcMap* src_map) const {
503 for (size_t i = 0; i < pc_infos_.Size(); i++) {
504 struct PcInfo pc_info = pc_infos_.Get(i);
505 uint32_t pc2dex_offset = pc_info.native_pc;
506 int32_t pc2dex_dalvik_offset = pc_info.dex_pc;
507 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
508 }
509}
510
511void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000512 uint32_t pc2dex_data_size = 0u;
513 uint32_t pc2dex_entries = pc_infos_.Size();
514 uint32_t pc2dex_offset = 0u;
515 int32_t pc2dex_dalvik_offset = 0;
516 uint32_t dex2pc_data_size = 0u;
517 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000518 uint32_t dex2pc_offset = 0u;
519 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000520
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000521 for (size_t i = 0; i < pc2dex_entries; i++) {
522 struct PcInfo pc_info = pc_infos_.Get(i);
523 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
524 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
525 pc2dex_offset = pc_info.native_pc;
526 pc2dex_dalvik_offset = pc_info.dex_pc;
527 }
528
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000529 // Walk over the blocks and find which ones correspond to catch block entries.
530 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
531 HBasicBlock* block = graph_->GetBlocks().Get(i);
532 if (block->IsCatchBlock()) {
533 intptr_t native_pc = GetAddressOf(block);
534 ++dex2pc_entries;
535 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
536 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
537 dex2pc_offset = native_pc;
538 dex2pc_dalvik_offset = block->GetDexPc();
539 }
540 }
541
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000542 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
543 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
544 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
545 data->resize(data_size);
546
547 uint8_t* data_ptr = &(*data)[0];
548 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000549
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000550 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
551 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
552 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
553 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
554
555 pc2dex_offset = 0u;
556 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000557 dex2pc_offset = 0u;
558 dex2pc_dalvik_offset = 0u;
559
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000560 for (size_t i = 0; i < pc2dex_entries; i++) {
561 struct PcInfo pc_info = pc_infos_.Get(i);
562 DCHECK(pc2dex_offset <= pc_info.native_pc);
563 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
564 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
565 pc2dex_offset = pc_info.native_pc;
566 pc2dex_dalvik_offset = pc_info.dex_pc;
567 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000568
569 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
570 HBasicBlock* block = graph_->GetBlocks().Get(i);
571 if (block->IsCatchBlock()) {
572 intptr_t native_pc = GetAddressOf(block);
573 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
574 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
575 dex2pc_offset = native_pc;
576 dex2pc_dalvik_offset = block->GetDexPc();
577 }
578 }
579
580
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000581 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
582 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
583
584 if (kIsDebugBuild) {
585 // Verify the encoded table holds the expected data.
586 MappingTable table(data_ptr);
587 CHECK_EQ(table.TotalSize(), total_entries);
588 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
589 auto it = table.PcToDexBegin();
590 auto it2 = table.DexToPcBegin();
591 for (size_t i = 0; i < pc2dex_entries; i++) {
592 struct PcInfo pc_info = pc_infos_.Get(i);
593 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
594 CHECK_EQ(pc_info.dex_pc, it.DexPc());
595 ++it;
596 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000597 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
598 HBasicBlock* block = graph_->GetBlocks().Get(i);
599 if (block->IsCatchBlock()) {
600 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
601 CHECK_EQ(block->GetDexPc(), it2.DexPc());
602 ++it2;
603 }
604 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000605 CHECK(it == table.PcToDexEnd());
606 CHECK(it2 == table.DexToPcEnd());
607 }
608}
609
610void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
611 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100612 // We currently don't use callee-saved registers.
613 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000614 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
615 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000616 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
617
618 *data = vmap_encoder.GetData();
619}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000620
Nicolas Geoffray39468442014-09-02 15:17:15 +0100621void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100622 uint32_t size = stack_map_stream_.PrepareForFillIn();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100623 data->resize(size);
624 MemoryRegion region(data->data(), size);
625 stack_map_stream_.FillIn(region);
626}
627
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000628void CodeGenerator::RecordPcInfo(HInstruction* instruction,
629 uint32_t dex_pc,
630 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000631 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000632 // The code generated for some type conversions may call the
633 // runtime, thus normally requiring a subsequent call to this
634 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000635 // information for certain instructions, which are considered "atomic"
636 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000637 // Therefore we do not currently record PC information for such
638 // instructions. As this may change later, we added this special
639 // case so that code generators may nevertheless call
640 // CodeGenerator::RecordPcInfo without triggering an error in
641 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
642 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000643 if (instruction->IsTypeConversion()) {
644 return;
645 }
646 if (instruction->IsRem()) {
647 Primitive::Type type = instruction->AsRem()->GetResultType();
648 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
649 return;
650 }
651 }
Roland Levillain624279f2014-12-04 11:54:28 +0000652 }
653
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100654 uint32_t outer_dex_pc = dex_pc;
655 uint32_t outer_environment_size = 0;
656 uint32_t inlining_depth = 0;
657 if (instruction != nullptr) {
658 for (HEnvironment* environment = instruction->GetEnvironment();
659 environment != nullptr;
660 environment = environment->GetParent()) {
661 outer_dex_pc = environment->GetDexPc();
662 outer_environment_size = environment->Size();
663 if (environment != instruction->GetEnvironment()) {
664 inlining_depth++;
665 }
666 }
667 }
668
Nicolas Geoffray39468442014-09-02 15:17:15 +0100669 // Collect PC infos for the mapping table.
670 struct PcInfo pc_info;
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100671 pc_info.dex_pc = outer_dex_pc;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100672 pc_info.native_pc = GetAssembler()->CodeSize();
673 pc_infos_.Add(pc_info);
674
Nicolas Geoffray39468442014-09-02 15:17:15 +0100675 if (instruction == nullptr) {
676 // For stack overflow checks.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100677 stack_map_stream_.BeginStackMapEntry(pc_info.dex_pc, pc_info.native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100678 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000679 return;
680 }
681 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100682
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000683 uint32_t register_mask = locations->GetRegisterMask();
684 if (locations->OnlyCallsOnSlowPath()) {
685 // In case of slow path, we currently set the location of caller-save registers
686 // to register (instead of their stack location when pushed before the slow-path
687 // call). Therefore register_mask contains both callee-save and caller-save
688 // registers that hold objects. We must remove the caller-save from the mask, since
689 // they will be overwritten by the callee.
690 register_mask &= core_callee_save_mask_;
691 }
692 // The register mask must be a subset of callee-save registers.
693 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100694 stack_map_stream_.BeginStackMapEntry(pc_info.dex_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100695 pc_info.native_pc,
696 register_mask,
697 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100698 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100699 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100700
701 EmitEnvironment(instruction->GetEnvironment(), slow_path);
702 stack_map_stream_.EndStackMapEntry();
703}
704
705void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
706 if (environment == nullptr) return;
707
708 if (environment->GetParent() != nullptr) {
709 // We emit the parent environment first.
710 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100711 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
712 environment->GetDexPc(),
713 environment->GetInvokeType(),
714 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100715 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000716
717 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100718 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000719 HInstruction* current = environment->GetInstructionAt(i);
720 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100721 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000722 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100723 }
724
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100725 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000726 switch (location.GetKind()) {
727 case Location::kConstant: {
728 DCHECK_EQ(current, location.GetConstant());
729 if (current->IsLongConstant()) {
730 int64_t value = current->AsLongConstant()->GetValue();
731 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100732 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000733 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100734 DexRegisterLocation::Kind::kConstant, High32Bits(value));
735 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000736 DCHECK_LT(i, environment_size);
737 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000738 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000739 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100740 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000741 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100742 DexRegisterLocation::Kind::kConstant, High32Bits(value));
743 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000744 DCHECK_LT(i, environment_size);
745 } else if (current->IsIntConstant()) {
746 int32_t value = current->AsIntConstant()->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 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100749 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000750 } else {
751 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000752 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100753 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000754 }
755 break;
756 }
757
758 case Location::kStackSlot: {
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 break;
762 }
763
764 case Location::kDoubleStackSlot: {
765 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100766 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000767 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100768 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
769 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000770 DCHECK_LT(i, environment_size);
771 break;
772 }
773
774 case Location::kRegister : {
775 int id = location.reg();
776 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
777 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100778 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000779 if (current->GetType() == Primitive::kPrimLong) {
780 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100781 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
782 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000783 DCHECK_LT(i, environment_size);
784 }
785 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100786 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000787 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100788 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
789 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000790 DCHECK_LT(i, environment_size);
791 }
792 }
793 break;
794 }
795
796 case Location::kFpuRegister : {
797 int id = location.reg();
798 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
799 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100800 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000801 if (current->GetType() == Primitive::kPrimDouble) {
802 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100803 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
804 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000805 DCHECK_LT(i, environment_size);
806 }
807 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100808 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000809 if (current->GetType() == Primitive::kPrimDouble) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100810 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
811 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000812 DCHECK_LT(i, environment_size);
813 }
814 }
815 break;
816 }
817
818 case Location::kFpuRegisterPair : {
819 int low = location.low();
820 int high = location.high();
821 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
822 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100823 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000824 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100825 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000826 }
827 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
828 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100829 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
830 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000831 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100832 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
833 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000834 }
835 DCHECK_LT(i, environment_size);
836 break;
837 }
838
839 case Location::kRegisterPair : {
840 int low = location.low();
841 int high = location.high();
842 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
843 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
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, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000847 }
848 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
849 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100850 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000851 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100852 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000853 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100854 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000855 DCHECK_LT(i, environment_size);
856 break;
857 }
858
859 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100860 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000861 break;
862 }
863
864 default:
865 LOG(FATAL) << "Unexpected kind " << location.GetKind();
866 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100867 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100868
869 if (environment->GetParent() != nullptr) {
870 stack_map_stream_.EndInlineInfoEntry();
871 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100872}
873
Calin Juravle77520bc2015-01-12 18:45:46 +0000874bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
875 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +0100876
877 return (first_next_not_move != nullptr)
878 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +0000879}
880
881void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
882 // If we are from a static path don't record the pc as we can't throw NPE.
883 // NB: having the checks here makes the code much less verbose in the arch
884 // specific code generators.
885 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
886 return;
887 }
888
889 if (!compiler_options_.GetImplicitNullChecks()) {
890 return;
891 }
892
Calin Juravle641547a2015-04-21 22:08:51 +0100893 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +0000894 return;
895 }
896
897 // Find the first previous instruction which is not a move.
898 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
899
900 // If the instruction is a null check it means that `instr` is the first user
901 // and needs to record the pc.
902 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
903 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
904 // TODO: The parallel moves modify the environment. Their changes need to be reverted
905 // otherwise the stack maps at the throw point will not be correct.
906 RecordPcInfo(null_check, null_check->GetDexPc());
907 }
908}
909
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100910void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
911 LocationSummary* locations = suspend_check->GetLocations();
912 HBasicBlock* block = suspend_check->GetBlock();
913 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
914 DCHECK(block->IsLoopHeader());
915
916 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
917 HInstruction* current = it.Current();
918 LiveInterval* interval = current->GetLiveInterval();
919 // We only need to clear bits of loop phis containing objects and allocated in register.
920 // Loop phis allocated on stack already have the object in the stack.
921 if (current->GetType() == Primitive::kPrimNot
922 && interval->HasRegister()
923 && interval->HasSpillSlot()) {
924 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
925 }
926 }
927}
928
Nicolas Geoffray90218252015-04-15 11:56:51 +0100929void CodeGenerator::EmitParallelMoves(Location from1,
930 Location to1,
931 Primitive::Type type1,
932 Location from2,
933 Location to2,
934 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000935 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +0100936 parallel_move.AddMove(from1, to1, type1, nullptr);
937 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000938 GetMoveResolver()->EmitNativeCode(&parallel_move);
939}
940
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000941void SlowPathCode::RecordPcInfo(CodeGenerator* codegen, HInstruction* instruction, uint32_t dex_pc) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000942 codegen->RecordPcInfo(instruction, dex_pc, this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000943}
944
945void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
946 RegisterSet* register_set = locations->GetLiveRegisters();
947 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
948 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
949 if (!codegen->IsCoreCalleeSaveRegister(i)) {
950 if (register_set->ContainsCoreRegister(i)) {
951 // If the register holds an object, update the stack mask.
952 if (locations->RegisterContainsObject(i)) {
953 locations->SetStackBit(stack_offset / kVRegSize);
954 }
955 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000956 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
957 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000958 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
959 }
960 }
961 }
962
963 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
964 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
965 if (register_set->ContainsFloatingPointRegister(i)) {
966 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000967 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
968 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000969 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
970 }
971 }
972 }
973}
974
975void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
976 RegisterSet* register_set = locations->GetLiveRegisters();
977 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
978 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
979 if (!codegen->IsCoreCalleeSaveRegister(i)) {
980 if (register_set->ContainsCoreRegister(i)) {
981 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
982 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
983 }
984 }
985 }
986
987 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
988 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
989 if (register_set->ContainsFloatingPointRegister(i)) {
990 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
991 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
992 }
993 }
994 }
995}
996
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000997} // namespace art