blob: 8fe3170b37f7b22526e894c009cee091e8ced64e [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"
Alexey Frunze4dda3372015-06-01 18:31:49 -070023#include "code_generator_mips64.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070024#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000025#include "dex/verified_method.h"
26#include "driver/dex_compilation_unit.h"
27#include "gc_map_builder.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010028#include "graph_visualizer.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000029#include "leb128.h"
30#include "mapping_table.h"
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010031#include "mirror/array-inl.h"
32#include "mirror/object_array-inl.h"
33#include "mirror/object_reference.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010034#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000036#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000037#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038
39namespace art {
40
Alexandre Rames88c13cd2015-04-14 17:35:39 +010041// Return whether a location is consistent with a type.
42static bool CheckType(Primitive::Type type, Location location) {
43 if (location.IsFpuRegister()
44 || (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresFpuRegister))) {
45 return (type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble);
46 } else if (location.IsRegister() ||
47 (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresRegister))) {
48 return Primitive::IsIntegralType(type) || (type == Primitive::kPrimNot);
49 } else if (location.IsRegisterPair()) {
50 return type == Primitive::kPrimLong;
51 } else if (location.IsFpuRegisterPair()) {
52 return type == Primitive::kPrimDouble;
53 } else if (location.IsStackSlot()) {
54 return (Primitive::IsIntegralType(type) && type != Primitive::kPrimLong)
55 || (type == Primitive::kPrimFloat)
56 || (type == Primitive::kPrimNot);
57 } else if (location.IsDoubleStackSlot()) {
58 return (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
59 } else if (location.IsConstant()) {
60 if (location.GetConstant()->IsIntConstant()) {
61 return Primitive::IsIntegralType(type) && (type != Primitive::kPrimLong);
62 } else if (location.GetConstant()->IsNullConstant()) {
63 return type == Primitive::kPrimNot;
64 } else if (location.GetConstant()->IsLongConstant()) {
65 return type == Primitive::kPrimLong;
66 } else if (location.GetConstant()->IsFloatConstant()) {
67 return type == Primitive::kPrimFloat;
68 } else {
69 return location.GetConstant()->IsDoubleConstant()
70 && (type == Primitive::kPrimDouble);
71 }
72 } else {
73 return location.IsInvalid() || (location.GetPolicy() == Location::kAny);
74 }
75}
76
77// Check that a location summary is consistent with an instruction.
78static bool CheckTypeConsistency(HInstruction* instruction) {
79 LocationSummary* locations = instruction->GetLocations();
80 if (locations == nullptr) {
81 return true;
82 }
83
84 if (locations->Out().IsUnallocated()
85 && (locations->Out().GetPolicy() == Location::kSameAsFirstInput)) {
86 DCHECK(CheckType(instruction->GetType(), locations->InAt(0)))
87 << instruction->GetType()
88 << " " << locations->InAt(0);
89 } else {
90 DCHECK(CheckType(instruction->GetType(), locations->Out()))
91 << instruction->GetType()
92 << " " << locations->Out();
93 }
94
95 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
96 DCHECK(CheckType(instruction->InputAt(i)->GetType(), locations->InAt(i)))
97 << instruction->InputAt(i)->GetType()
98 << " " << locations->InAt(i);
99 }
100
101 HEnvironment* environment = instruction->GetEnvironment();
102 for (size_t i = 0; i < instruction->EnvironmentSize(); ++i) {
103 if (environment->GetInstructionAt(i) != nullptr) {
104 Primitive::Type type = environment->GetInstructionAt(i)->GetType();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100105 DCHECK(CheckType(type, environment->GetLocationAt(i)))
106 << type << " " << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100107 } else {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100108 DCHECK(environment->GetLocationAt(i).IsInvalid())
109 << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100110 }
111 }
112 return true;
113}
114
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100115size_t CodeGenerator::GetCacheOffset(uint32_t index) {
116 return mirror::ObjectArray<mirror::Object>::OffsetOfElement(index).SizeValue();
117}
118
Mathieu Chartiere401d142015-04-22 13:56:20 -0700119size_t CodeGenerator::GetCachePointerOffset(uint32_t index) {
120 auto pointer_size = InstructionSetPointerSize(GetInstructionSet());
121 return mirror::Array::DataOffset(pointer_size).Uint32Value() + pointer_size * index;
122}
123
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100124void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000125 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100126 if (!is_leaf) {
127 MarkNotLeaf();
128 }
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700129 const bool is_64_bit = Is64BitInstructionSet(GetInstructionSet());
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000130 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
131 + GetGraph()->GetTemporariesVRegSlots()
132 + 1 /* filler */,
133 0, /* the baseline compiler does not have live registers at slow path */
134 0, /* the baseline compiler does not have live registers at slow path */
135 GetGraph()->GetMaximumNumberOfOutVRegs()
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700136 + (is_64_bit ? 2 : 1) /* current method */,
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000137 GetGraph()->GetBlocks());
138 CompileInternal(allocator, /* is_baseline */ true);
139}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100140
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000141bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
142 DCHECK_EQ(block_order_->Get(current_block_index_), current);
143 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
144}
145
146HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
147 for (size_t i = current_block_index_ + 1; i < block_order_->Size(); ++i) {
148 HBasicBlock* block = block_order_->Get(i);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000149 if (!block->IsSingleJump()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000150 return block;
151 }
152 }
153 return nullptr;
154}
155
156HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000157 while (block->IsSingleJump()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000158 block = block->GetSuccessors().Get(0);
159 }
160 return block;
161}
162
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100163class DisassemblyScope {
164 public:
165 DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen)
166 : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) {
167 if (codegen_.GetDisassemblyInformation() != nullptr) {
168 start_offset_ = codegen_.GetAssembler().CodeSize();
169 }
170 }
171
172 ~DisassemblyScope() {
173 // We avoid building this data when we know it will not be used.
174 if (codegen_.GetDisassemblyInformation() != nullptr) {
175 codegen_.GetDisassemblyInformation()->AddInstructionInterval(
176 instruction_, start_offset_, codegen_.GetAssembler().CodeSize());
177 }
178 }
179
180 private:
181 const CodeGenerator& codegen_;
182 HInstruction* instruction_;
183 size_t start_offset_;
184};
185
186
187void CodeGenerator::GenerateSlowPaths() {
188 size_t code_start = 0;
189 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
190 if (disasm_info_ != nullptr) {
191 code_start = GetAssembler()->CodeSize();
192 }
193 slow_paths_.Get(i)->EmitNativeCode(this);
194 if (disasm_info_ != nullptr) {
195 disasm_info_->AddSlowPathInterval(slow_paths_.Get(i), code_start, GetAssembler()->CodeSize());
196 }
197 }
198}
199
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000200void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Roland Levillain3e3d7332015-04-28 11:00:54 +0100201 is_baseline_ = is_baseline;
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100202 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000203 DCHECK_EQ(current_block_index_, 0u);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100204
205 size_t frame_start = GetAssembler()->CodeSize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000206 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100207 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100208 if (disasm_info_ != nullptr) {
209 disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize());
210 }
211
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000212 for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) {
213 HBasicBlock* block = block_order_->Get(current_block_index_);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000214 // Don't generate code for an empty block. Its predecessors will branch to its successor
215 // directly. Also, the label of that block will not be emitted, so this helps catch
216 // errors where we reference that label.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000217 if (block->IsSingleJump()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100218 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100219 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
220 HInstruction* current = it.Current();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100221 DisassemblyScope disassembly_scope(current, *this);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000222 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000223 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000224 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100225 DCHECK(CheckTypeConsistency(current));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100226 current->Accept(instruction_visitor);
227 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000228 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000229
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100230 GenerateSlowPaths();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000231
232 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000233 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000234}
235
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100236void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000237 // The register allocator already called `InitializeCodeGeneration`,
238 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000239 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100240 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000241 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000242}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100243
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000244void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100245 size_t code_size = GetAssembler()->CodeSize();
246 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000247
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100248 MemoryRegion code(buffer, code_size);
249 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000250}
251
Vladimir Marko9b688a02015-05-06 14:12:42 +0100252void CodeGenerator::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches ATTRIBUTE_UNUSED) {
253 // No linker patches by default.
254}
255
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100256size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
257 for (size_t i = 0; i < length; ++i) {
258 if (!array[i]) {
259 array[i] = true;
260 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100261 }
262 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100263 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000264 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000265}
266
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000267size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
268 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000269 if (!array[i] && !array[i + 1]) {
270 array[i] = true;
271 array[i + 1] = true;
272 return i;
273 }
274 }
275 LOG(FATAL) << "Could not find a register in baseline register allocator";
276 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100277}
278
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000279void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
280 size_t maximum_number_of_live_core_registers,
281 size_t maximum_number_of_live_fp_registers,
282 size_t number_of_out_slots,
283 const GrowableArray<HBasicBlock*>& block_order) {
284 block_order_ = &block_order;
285 DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock());
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000286 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100287 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
288
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000289 if (number_of_spill_slots == 0
290 && !HasAllocatedCalleeSaveRegisters()
291 && IsLeafMethod()
292 && !RequiresCurrentMethod()) {
293 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
294 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
295 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
296 } else {
297 SetFrameSize(RoundUp(
298 number_of_spill_slots * kVRegSize
299 + number_of_out_slots * kVRegSize
300 + maximum_number_of_live_core_registers * GetWordSize()
301 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
302 + FrameEntrySpillSize(),
303 kStackAlignment));
304 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100305}
306
307Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
308 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000309 // The type of the previous instruction tells us if we need a single or double stack slot.
310 Primitive::Type type = temp->GetType();
311 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100312 // Use the temporary region (right below the dex registers).
313 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
314 - kVRegSize // filler
315 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000316 - ((temp_size + temp->GetIndex()) * kVRegSize);
317 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100318}
319
320int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
321 uint16_t reg_number = local->GetRegNumber();
322 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
323 if (reg_number >= number_of_locals) {
324 // Local is a parameter of the method. It is stored in the caller's frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700325 // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode.
326 return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100327 + (reg_number - number_of_locals) * kVRegSize;
328 } else {
329 // Local is a temporary in this method. It is stored in this method's frame.
330 return GetFrameSize() - FrameEntrySpillSize()
331 - kVRegSize // filler.
332 - (number_of_locals * kVRegSize)
333 + (reg_number * kVRegSize);
334 }
335}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100336
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100337void CodeGenerator::CreateCommonInvokeLocationSummary(
Nicolas Geoffray4e40c262015-06-03 12:02:38 +0100338 HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100339 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
340 LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100341
342 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
343 HInstruction* input = invoke->InputAt(i);
344 locations->SetInAt(i, visitor->GetNextLocation(input->GetType()));
345 }
346
347 locations->SetOut(visitor->GetReturnLocation(invoke->GetType()));
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100348
349 if (invoke->IsInvokeStaticOrDirect()) {
350 HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect();
351 if (call->IsStringInit()) {
352 locations->AddTemp(visitor->GetMethodLocation());
353 } else if (call->IsRecursive()) {
354 locations->SetInAt(call->GetCurrentMethodInputIndex(), visitor->GetMethodLocation());
355 } else {
356 locations->AddTemp(visitor->GetMethodLocation());
357 locations->SetInAt(call->GetCurrentMethodInputIndex(), Location::RequiresRegister());
358 }
359 } else {
360 locations->AddTemp(visitor->GetMethodLocation());
361 }
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100362}
363
Mark Mendell5f874182015-03-04 15:42:45 -0500364void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
365 // The DCHECKS below check that a register is not specified twice in
366 // the summary. The out location can overlap with an input, so we need
367 // to special case it.
368 if (location.IsRegister()) {
369 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
370 blocked_core_registers_[location.reg()] = true;
371 } else if (location.IsFpuRegister()) {
372 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
373 blocked_fpu_registers_[location.reg()] = true;
374 } else if (location.IsFpuRegisterPair()) {
375 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
376 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
377 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
378 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
379 } else if (location.IsRegisterPair()) {
380 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
381 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
382 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
383 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
384 }
385}
386
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100387void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
388 LocationSummary* locations = instruction->GetLocations();
389 if (locations == nullptr) return;
390
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100391 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
392 blocked_core_registers_[i] = false;
393 }
394
395 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
396 blocked_fpu_registers_[i] = false;
397 }
398
399 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
400 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100401 }
402
403 // Mark all fixed input, temp and output registers as used.
404 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500405 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100406 }
407
408 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
409 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500410 BlockIfInRegister(loc);
411 }
412 Location result_location = locations->Out();
413 if (locations->OutputCanOverlapWithInputs()) {
414 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100415 }
416
Mark Mendell5f874182015-03-04 15:42:45 -0500417 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100418
419 // Allocate all unallocated input locations.
420 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
421 Location loc = locations->InAt(i);
422 HInstruction* input = instruction->InputAt(i);
423 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100424 if ((loc.GetPolicy() == Location::kRequiresRegister)
425 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100426 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100427 } else {
428 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
429 HLoadLocal* load = input->AsLoadLocal();
430 if (load != nullptr) {
431 loc = GetStackLocation(load);
432 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100433 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100434 }
435 }
436 locations->SetInAt(i, loc);
437 }
438 }
439
440 // Allocate all unallocated temp locations.
441 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
442 Location loc = locations->GetTemp(i);
443 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000444 switch (loc.GetPolicy()) {
445 case Location::kRequiresRegister:
446 // Allocate a core register (large enough to fit a 32-bit integer).
447 loc = AllocateFreeRegister(Primitive::kPrimInt);
448 break;
449
450 case Location::kRequiresFpuRegister:
451 // Allocate a core register (large enough to fit a 64-bit double).
452 loc = AllocateFreeRegister(Primitive::kPrimDouble);
453 break;
454
455 default:
456 LOG(FATAL) << "Unexpected policy for temporary location "
457 << loc.GetPolicy();
458 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100459 locations->SetTempAt(i, loc);
460 }
461 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100462 if (result_location.IsUnallocated()) {
463 switch (result_location.GetPolicy()) {
464 case Location::kAny:
465 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100466 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100467 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100468 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100469 case Location::kSameAsFirstInput:
470 result_location = locations->InAt(0);
471 break;
472 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000473 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100474 }
475}
476
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000477void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
478 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100479 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100480 if (instruction->IsTemporary()) {
481 HInstruction* previous = instruction->GetPrevious();
482 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
483 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100484 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100485 return;
486 }
487 AllocateRegistersLocally(instruction);
488 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000489 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000490 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000491 if (location.IsValid()) {
492 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000493 if (input->GetNext()->IsTemporary()) {
494 // If the input was stored in a temporary, use that temporary to
495 // perform the move.
496 Move(input->GetNext(), location, instruction);
497 } else {
498 Move(input, location, instruction);
499 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000500 }
501 }
502}
503
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000504void CodeGenerator::AllocateLocations(HInstruction* instruction) {
505 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100506 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000507 LocationSummary* locations = instruction->GetLocations();
508 if (!instruction->IsSuspendCheckEntry()) {
509 if (locations != nullptr && locations->CanCall()) {
510 MarkNotLeaf();
511 }
512 if (instruction->NeedsCurrentMethod()) {
513 SetRequiresCurrentMethod();
514 }
515 }
516}
517
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000518CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000519 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000520 const InstructionSetFeatures& isa_features,
521 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000522 switch (instruction_set) {
523 case kArm:
524 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000525 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000526 *isa_features.AsArmInstructionSetFeatures(),
527 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000528 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100529 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000530 return new arm64::CodeGeneratorARM64(graph,
531 *isa_features.AsArm64InstructionSetFeatures(),
532 compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100533 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000534 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000535 return nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700536 case kMips64: {
537 return new mips64::CodeGeneratorMIPS64(graph,
538 *isa_features.AsMips64InstructionSetFeatures(),
539 compiler_options);
540 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000541 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400542 return new x86::CodeGeneratorX86(graph,
543 *isa_features.AsX86InstructionSetFeatures(),
544 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000545 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700546 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400547 return new x86_64::CodeGeneratorX86_64(graph,
548 *isa_features.AsX86_64InstructionSetFeatures(),
549 compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700550 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000551 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000552 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000553 }
554}
555
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000556void CodeGenerator::BuildNativeGCMap(
557 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
558 const std::vector<uint8_t>& gc_map_raw =
559 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
560 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
561
Vladimir Markobd8c7252015-06-12 10:06:32 +0100562 uint32_t max_native_offset = stack_map_stream_.ComputeMaxNativePcOffset();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000563
Vladimir Markobd8c7252015-06-12 10:06:32 +0100564 size_t num_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
565 GcMapBuilder builder(data, num_stack_maps, max_native_offset, dex_gc_map.RegWidth());
566 for (size_t i = 0; i != num_stack_maps; ++i) {
567 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
568 uint32_t native_offset = stack_map_entry.native_pc_offset;
569 uint32_t dex_pc = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000570 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800571 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000572 builder.AddEntry(native_offset, references);
573 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000574}
575
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100576void CodeGenerator::BuildSourceMap(DefaultSrcMap* src_map) const {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100577 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
578 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
579 uint32_t pc2dex_offset = stack_map_entry.native_pc_offset;
580 int32_t pc2dex_dalvik_offset = stack_map_entry.dex_pc;
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100581 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
582 }
583}
584
585void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000586 uint32_t pc2dex_data_size = 0u;
Vladimir Markobd8c7252015-06-12 10:06:32 +0100587 uint32_t pc2dex_entries = stack_map_stream_.GetNumberOfStackMaps();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000588 uint32_t pc2dex_offset = 0u;
589 int32_t pc2dex_dalvik_offset = 0;
590 uint32_t dex2pc_data_size = 0u;
591 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000592 uint32_t dex2pc_offset = 0u;
593 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000594
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000595 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100596 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
597 pc2dex_data_size += UnsignedLeb128Size(stack_map_entry.native_pc_offset - pc2dex_offset);
598 pc2dex_data_size += SignedLeb128Size(stack_map_entry.dex_pc - pc2dex_dalvik_offset);
599 pc2dex_offset = stack_map_entry.native_pc_offset;
600 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000601 }
602
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000603 // Walk over the blocks and find which ones correspond to catch block entries.
604 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
605 HBasicBlock* block = graph_->GetBlocks().Get(i);
606 if (block->IsCatchBlock()) {
607 intptr_t native_pc = GetAddressOf(block);
608 ++dex2pc_entries;
609 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
610 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
611 dex2pc_offset = native_pc;
612 dex2pc_dalvik_offset = block->GetDexPc();
613 }
614 }
615
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000616 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
617 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
618 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
619 data->resize(data_size);
620
621 uint8_t* data_ptr = &(*data)[0];
622 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000623
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000624 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
625 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
626 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
627 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
628
629 pc2dex_offset = 0u;
630 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000631 dex2pc_offset = 0u;
632 dex2pc_dalvik_offset = 0u;
633
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000634 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100635 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
636 DCHECK(pc2dex_offset <= stack_map_entry.native_pc_offset);
637 write_pos = EncodeUnsignedLeb128(write_pos, stack_map_entry.native_pc_offset - pc2dex_offset);
638 write_pos = EncodeSignedLeb128(write_pos, stack_map_entry.dex_pc - pc2dex_dalvik_offset);
639 pc2dex_offset = stack_map_entry.native_pc_offset;
640 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000641 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000642
643 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
644 HBasicBlock* block = graph_->GetBlocks().Get(i);
645 if (block->IsCatchBlock()) {
646 intptr_t native_pc = GetAddressOf(block);
647 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
648 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
649 dex2pc_offset = native_pc;
650 dex2pc_dalvik_offset = block->GetDexPc();
651 }
652 }
653
654
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000655 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
656 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
657
658 if (kIsDebugBuild) {
659 // Verify the encoded table holds the expected data.
660 MappingTable table(data_ptr);
661 CHECK_EQ(table.TotalSize(), total_entries);
662 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
663 auto it = table.PcToDexBegin();
664 auto it2 = table.DexToPcBegin();
665 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100666 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
667 CHECK_EQ(stack_map_entry.native_pc_offset, it.NativePcOffset());
668 CHECK_EQ(stack_map_entry.dex_pc, it.DexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000669 ++it;
670 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000671 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
672 HBasicBlock* block = graph_->GetBlocks().Get(i);
673 if (block->IsCatchBlock()) {
674 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
675 CHECK_EQ(block->GetDexPc(), it2.DexPc());
676 ++it2;
677 }
678 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000679 CHECK(it == table.PcToDexEnd());
680 CHECK(it2 == table.DexToPcEnd());
681 }
682}
683
684void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
685 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100686 // We currently don't use callee-saved registers.
687 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000688 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
689 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000690 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
691
692 *data = vmap_encoder.GetData();
693}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000694
Nicolas Geoffray39468442014-09-02 15:17:15 +0100695void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100696 uint32_t size = stack_map_stream_.PrepareForFillIn();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100697 data->resize(size);
698 MemoryRegion region(data->data(), size);
699 stack_map_stream_.FillIn(region);
700}
701
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000702void CodeGenerator::RecordPcInfo(HInstruction* instruction,
703 uint32_t dex_pc,
704 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000705 if (instruction != nullptr) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700706 // The code generated for some type conversions and comparisons
707 // may call the runtime, thus normally requiring a subsequent
708 // call to this method. However, the method verifier does not
709 // produce PC information for certain instructions, which are
710 // considered "atomic" (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000711 // Therefore we do not currently record PC information for such
712 // instructions. As this may change later, we added this special
713 // case so that code generators may nevertheless call
714 // CodeGenerator::RecordPcInfo without triggering an error in
715 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
716 // thereafter.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700717 if (instruction->IsTypeConversion() || instruction->IsCompare()) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000718 return;
719 }
720 if (instruction->IsRem()) {
721 Primitive::Type type = instruction->AsRem()->GetResultType();
722 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
723 return;
724 }
725 }
Roland Levillain624279f2014-12-04 11:54:28 +0000726 }
727
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100728 uint32_t outer_dex_pc = dex_pc;
729 uint32_t outer_environment_size = 0;
730 uint32_t inlining_depth = 0;
731 if (instruction != nullptr) {
732 for (HEnvironment* environment = instruction->GetEnvironment();
733 environment != nullptr;
734 environment = environment->GetParent()) {
735 outer_dex_pc = environment->GetDexPc();
736 outer_environment_size = environment->Size();
737 if (environment != instruction->GetEnvironment()) {
738 inlining_depth++;
739 }
740 }
741 }
742
Nicolas Geoffray39468442014-09-02 15:17:15 +0100743 // Collect PC infos for the mapping table.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100744 uint32_t native_pc = GetAssembler()->CodeSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100745
Nicolas Geoffray39468442014-09-02 15:17:15 +0100746 if (instruction == nullptr) {
747 // For stack overflow checks.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100748 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100749 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000750 return;
751 }
752 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100753
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000754 uint32_t register_mask = locations->GetRegisterMask();
755 if (locations->OnlyCallsOnSlowPath()) {
756 // In case of slow path, we currently set the location of caller-save registers
757 // to register (instead of their stack location when pushed before the slow-path
758 // call). Therefore register_mask contains both callee-save and caller-save
759 // registers that hold objects. We must remove the caller-save from the mask, since
760 // they will be overwritten by the callee.
761 register_mask &= core_callee_save_mask_;
762 }
763 // The register mask must be a subset of callee-save registers.
764 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100765 stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
766 native_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100767 register_mask,
768 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100769 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100770 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100771
772 EmitEnvironment(instruction->GetEnvironment(), slow_path);
773 stack_map_stream_.EndStackMapEntry();
774}
775
776void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
777 if (environment == nullptr) return;
778
779 if (environment->GetParent() != nullptr) {
780 // We emit the parent environment first.
781 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100782 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
783 environment->GetDexPc(),
784 environment->GetInvokeType(),
785 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100786 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000787
788 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100789 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000790 HInstruction* current = environment->GetInstructionAt(i);
791 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100792 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000793 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100794 }
795
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100796 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000797 switch (location.GetKind()) {
798 case Location::kConstant: {
799 DCHECK_EQ(current, location.GetConstant());
800 if (current->IsLongConstant()) {
801 int64_t value = current->AsLongConstant()->GetValue();
802 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100803 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000804 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100805 DexRegisterLocation::Kind::kConstant, High32Bits(value));
806 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000807 DCHECK_LT(i, environment_size);
808 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000809 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000810 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100811 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000812 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100813 DexRegisterLocation::Kind::kConstant, High32Bits(value));
814 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000815 DCHECK_LT(i, environment_size);
816 } else if (current->IsIntConstant()) {
817 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100818 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000819 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100820 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000821 } else {
822 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000823 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100824 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000825 }
826 break;
827 }
828
829 case Location::kStackSlot: {
830 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100831 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000832 break;
833 }
834
835 case Location::kDoubleStackSlot: {
836 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100837 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000838 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100839 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
840 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000841 DCHECK_LT(i, environment_size);
842 break;
843 }
844
845 case Location::kRegister : {
846 int id = location.reg();
847 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
848 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100849 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000850 if (current->GetType() == Primitive::kPrimLong) {
851 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100852 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
853 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000854 DCHECK_LT(i, environment_size);
855 }
856 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100857 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000858 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100859 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
860 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000861 DCHECK_LT(i, environment_size);
862 }
863 }
864 break;
865 }
866
867 case Location::kFpuRegister : {
868 int id = location.reg();
869 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
870 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100871 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000872 if (current->GetType() == Primitive::kPrimDouble) {
873 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100874 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
875 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000876 DCHECK_LT(i, environment_size);
877 }
878 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100879 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000880 if (current->GetType() == Primitive::kPrimDouble) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100881 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
882 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000883 DCHECK_LT(i, environment_size);
884 }
885 }
886 break;
887 }
888
889 case Location::kFpuRegisterPair : {
890 int low = location.low();
891 int high = location.high();
892 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
893 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100894 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000895 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100896 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000897 }
898 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
899 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100900 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
901 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000902 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100903 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
904 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000905 }
906 DCHECK_LT(i, environment_size);
907 break;
908 }
909
910 case Location::kRegisterPair : {
911 int low = location.low();
912 int high = location.high();
913 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
914 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100915 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000916 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100917 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000918 }
919 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
920 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100921 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000922 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100923 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000924 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100925 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000926 DCHECK_LT(i, environment_size);
927 break;
928 }
929
930 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100931 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000932 break;
933 }
934
935 default:
936 LOG(FATAL) << "Unexpected kind " << location.GetKind();
937 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100938 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100939
940 if (environment->GetParent() != nullptr) {
941 stack_map_stream_.EndInlineInfoEntry();
942 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100943}
944
Calin Juravle77520bc2015-01-12 18:45:46 +0000945bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
946 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +0100947
948 return (first_next_not_move != nullptr)
949 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +0000950}
951
952void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
953 // If we are from a static path don't record the pc as we can't throw NPE.
954 // NB: having the checks here makes the code much less verbose in the arch
955 // specific code generators.
956 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
957 return;
958 }
959
960 if (!compiler_options_.GetImplicitNullChecks()) {
961 return;
962 }
963
Calin Juravle641547a2015-04-21 22:08:51 +0100964 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +0000965 return;
966 }
967
968 // Find the first previous instruction which is not a move.
969 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
970
971 // If the instruction is a null check it means that `instr` is the first user
972 // and needs to record the pc.
973 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
974 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
975 // TODO: The parallel moves modify the environment. Their changes need to be reverted
976 // otherwise the stack maps at the throw point will not be correct.
977 RecordPcInfo(null_check, null_check->GetDexPc());
978 }
979}
980
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100981void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
982 LocationSummary* locations = suspend_check->GetLocations();
983 HBasicBlock* block = suspend_check->GetBlock();
984 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
985 DCHECK(block->IsLoopHeader());
986
987 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
988 HInstruction* current = it.Current();
989 LiveInterval* interval = current->GetLiveInterval();
990 // We only need to clear bits of loop phis containing objects and allocated in register.
991 // Loop phis allocated on stack already have the object in the stack.
992 if (current->GetType() == Primitive::kPrimNot
993 && interval->HasRegister()
994 && interval->HasSpillSlot()) {
995 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
996 }
997 }
998}
999
Nicolas Geoffray90218252015-04-15 11:56:51 +01001000void CodeGenerator::EmitParallelMoves(Location from1,
1001 Location to1,
1002 Primitive::Type type1,
1003 Location from2,
1004 Location to2,
1005 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001006 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +01001007 parallel_move.AddMove(from1, to1, type1, nullptr);
1008 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001009 GetMoveResolver()->EmitNativeCode(&parallel_move);
1010}
1011
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001012void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) {
1013 // Ensure that the call kind indication given to the register allocator is
1014 // coherent with the runtime call generated, and that the GC side effect is
1015 // set when required.
1016 if (slow_path == nullptr) {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001017 DCHECK(instruction->GetLocations()->WillCall()) << instruction->DebugName();
1018 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()))
1019 << instruction->DebugName() << instruction->GetSideEffects().ToString();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001020 } else {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001021 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal())
1022 << instruction->DebugName() << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001023 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) ||
1024 // Control flow would not come back into the code if a fatal slow
1025 // path is taken, so we do not care if it triggers GC.
1026 slow_path->IsFatal() ||
1027 // HDeoptimize is a special case: we know we are not coming back from
1028 // it into the code.
Roland Levillaindf3f8222015-08-13 12:31:44 +01001029 instruction->IsDeoptimize())
1030 << instruction->DebugName() << instruction->GetSideEffects().ToString()
1031 << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001032 }
1033
1034 // Check the coherency of leaf information.
1035 DCHECK(instruction->IsSuspendCheck()
1036 || ((slow_path != nullptr) && slow_path->IsFatal())
1037 || instruction->GetLocations()->CanCall()
Roland Levillaindf3f8222015-08-13 12:31:44 +01001038 || !IsLeafMethod())
1039 << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : "");
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001040}
1041
Roland Levillaindf3f8222015-08-13 12:31:44 +01001042void SlowPathCode::RecordPcInfo(CodeGenerator* codegen,
1043 HInstruction* instruction,
1044 uint32_t dex_pc) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001045 codegen->RecordPcInfo(instruction, dex_pc, this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001046}
1047
1048void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1049 RegisterSet* register_set = locations->GetLiveRegisters();
1050 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1051 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1052 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1053 if (register_set->ContainsCoreRegister(i)) {
1054 // If the register holds an object, update the stack mask.
1055 if (locations->RegisterContainsObject(i)) {
1056 locations->SetStackBit(stack_offset / kVRegSize);
1057 }
1058 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001059 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1060 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001061 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
1062 }
1063 }
1064 }
1065
1066 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1067 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1068 if (register_set->ContainsFloatingPointRegister(i)) {
1069 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001070 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1071 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001072 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
1073 }
1074 }
1075 }
1076}
1077
1078void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1079 RegisterSet* register_set = locations->GetLiveRegisters();
1080 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1081 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1082 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1083 if (register_set->ContainsCoreRegister(i)) {
1084 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1085 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1086 }
1087 }
1088 }
1089
1090 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1091 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1092 if (register_set->ContainsFloatingPointRegister(i)) {
1093 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1094 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1095 }
1096 }
1097 }
1098}
1099
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001100} // namespace art