blob: 1c62dfa859bd67a6cab48cec6a793ceed212c9cf [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
Alex Light50fa9932015-08-10 15:30:07 -070019#ifdef ART_ENABLE_CODEGEN_arm
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000020#include "code_generator_arm.h"
Alex Light50fa9932015-08-10 15:30:07 -070021#endif
22
23#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "code_generator_arm64.h"
Alex Light50fa9932015-08-10 15:30:07 -070025#endif
26
27#ifdef ART_ENABLE_CODEGEN_x86
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028#include "code_generator_x86.h"
Alex Light50fa9932015-08-10 15:30:07 -070029#endif
30
31#ifdef ART_ENABLE_CODEGEN_x86_64
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "code_generator_x86_64.h"
Alex Light50fa9932015-08-10 15:30:07 -070033#endif
34
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020035#ifdef ART_ENABLE_CODEGEN_mips
36#include "code_generator_mips.h"
37#endif
38
Alex Light50fa9932015-08-10 15:30:07 -070039#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -070040#include "code_generator_mips64.h"
Alex Light50fa9932015-08-10 15:30:07 -070041#endif
42
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070043#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000044#include "dex/verified_method.h"
45#include "driver/dex_compilation_unit.h"
46#include "gc_map_builder.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010047#include "graph_visualizer.h"
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +010048#include "intrinsics.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000049#include "leb128.h"
50#include "mapping_table.h"
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010051#include "mirror/array-inl.h"
52#include "mirror/object_array-inl.h"
53#include "mirror/object_reference.h"
Alex Light50fa9932015-08-10 15:30:07 -070054#include "parallel_move_resolver.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010055#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000056#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000057#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000058#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000059
60namespace art {
61
Alexandre Rames88c13cd2015-04-14 17:35:39 +010062// Return whether a location is consistent with a type.
63static bool CheckType(Primitive::Type type, Location location) {
64 if (location.IsFpuRegister()
65 || (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresFpuRegister))) {
66 return (type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble);
67 } else if (location.IsRegister() ||
68 (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresRegister))) {
69 return Primitive::IsIntegralType(type) || (type == Primitive::kPrimNot);
70 } else if (location.IsRegisterPair()) {
71 return type == Primitive::kPrimLong;
72 } else if (location.IsFpuRegisterPair()) {
73 return type == Primitive::kPrimDouble;
74 } else if (location.IsStackSlot()) {
75 return (Primitive::IsIntegralType(type) && type != Primitive::kPrimLong)
76 || (type == Primitive::kPrimFloat)
77 || (type == Primitive::kPrimNot);
78 } else if (location.IsDoubleStackSlot()) {
79 return (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
80 } else if (location.IsConstant()) {
81 if (location.GetConstant()->IsIntConstant()) {
82 return Primitive::IsIntegralType(type) && (type != Primitive::kPrimLong);
83 } else if (location.GetConstant()->IsNullConstant()) {
84 return type == Primitive::kPrimNot;
85 } else if (location.GetConstant()->IsLongConstant()) {
86 return type == Primitive::kPrimLong;
87 } else if (location.GetConstant()->IsFloatConstant()) {
88 return type == Primitive::kPrimFloat;
89 } else {
90 return location.GetConstant()->IsDoubleConstant()
91 && (type == Primitive::kPrimDouble);
92 }
93 } else {
94 return location.IsInvalid() || (location.GetPolicy() == Location::kAny);
95 }
96}
97
98// Check that a location summary is consistent with an instruction.
99static bool CheckTypeConsistency(HInstruction* instruction) {
100 LocationSummary* locations = instruction->GetLocations();
101 if (locations == nullptr) {
102 return true;
103 }
104
105 if (locations->Out().IsUnallocated()
106 && (locations->Out().GetPolicy() == Location::kSameAsFirstInput)) {
107 DCHECK(CheckType(instruction->GetType(), locations->InAt(0)))
108 << instruction->GetType()
109 << " " << locations->InAt(0);
110 } else {
111 DCHECK(CheckType(instruction->GetType(), locations->Out()))
112 << instruction->GetType()
113 << " " << locations->Out();
114 }
115
116 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
117 DCHECK(CheckType(instruction->InputAt(i)->GetType(), locations->InAt(i)))
118 << instruction->InputAt(i)->GetType()
119 << " " << locations->InAt(i);
120 }
121
122 HEnvironment* environment = instruction->GetEnvironment();
123 for (size_t i = 0; i < instruction->EnvironmentSize(); ++i) {
124 if (environment->GetInstructionAt(i) != nullptr) {
125 Primitive::Type type = environment->GetInstructionAt(i)->GetType();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100126 DCHECK(CheckType(type, environment->GetLocationAt(i)))
127 << type << " " << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100128 } else {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100129 DCHECK(environment->GetLocationAt(i).IsInvalid())
130 << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100131 }
132 }
133 return true;
134}
135
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100136size_t CodeGenerator::GetCacheOffset(uint32_t index) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100137 return sizeof(GcRoot<mirror::Object>) * index;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100138}
139
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140size_t CodeGenerator::GetCachePointerOffset(uint32_t index) {
141 auto pointer_size = InstructionSetPointerSize(GetInstructionSet());
Vladimir Marko05792b92015-08-03 11:56:49 +0100142 return pointer_size * index;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700143}
144
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100145void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000146 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100147 if (!is_leaf) {
148 MarkNotLeaf();
149 }
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700150 const bool is_64_bit = Is64BitInstructionSet(GetInstructionSet());
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000151 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
152 + GetGraph()->GetTemporariesVRegSlots()
153 + 1 /* filler */,
154 0, /* the baseline compiler does not have live registers at slow path */
155 0, /* the baseline compiler does not have live registers at slow path */
156 GetGraph()->GetMaximumNumberOfOutVRegs()
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700157 + (is_64_bit ? 2 : 1) /* current method */,
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000158 GetGraph()->GetBlocks());
159 CompileInternal(allocator, /* is_baseline */ true);
160}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100161
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000162bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100163 DCHECK_EQ((*block_order_)[current_block_index_], current);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000164 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
165}
166
167HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100168 for (size_t i = current_block_index_ + 1; i < block_order_->size(); ++i) {
169 HBasicBlock* block = (*block_order_)[i];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000170 if (!block->IsSingleJump()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000171 return block;
172 }
173 }
174 return nullptr;
175}
176
177HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000178 while (block->IsSingleJump()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100179 block = block->GetSuccessors()[0];
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000180 }
181 return block;
182}
183
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100184class DisassemblyScope {
185 public:
186 DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen)
187 : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) {
188 if (codegen_.GetDisassemblyInformation() != nullptr) {
189 start_offset_ = codegen_.GetAssembler().CodeSize();
190 }
191 }
192
193 ~DisassemblyScope() {
194 // We avoid building this data when we know it will not be used.
195 if (codegen_.GetDisassemblyInformation() != nullptr) {
196 codegen_.GetDisassemblyInformation()->AddInstructionInterval(
197 instruction_, start_offset_, codegen_.GetAssembler().CodeSize());
198 }
199 }
200
201 private:
202 const CodeGenerator& codegen_;
203 HInstruction* instruction_;
204 size_t start_offset_;
205};
206
207
208void CodeGenerator::GenerateSlowPaths() {
209 size_t code_start = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100210 for (SlowPathCode* slow_path : slow_paths_) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100211 if (disasm_info_ != nullptr) {
212 code_start = GetAssembler()->CodeSize();
213 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100214 slow_path->EmitNativeCode(this);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100215 if (disasm_info_ != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100216 disasm_info_->AddSlowPathInterval(slow_path, code_start, GetAssembler()->CodeSize());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100217 }
218 }
219}
220
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000221void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Roland Levillain3e3d7332015-04-28 11:00:54 +0100222 is_baseline_ = is_baseline;
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100223 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000224 DCHECK_EQ(current_block_index_, 0u);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100225
226 size_t frame_start = GetAssembler()->CodeSize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000227 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100228 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100229 if (disasm_info_ != nullptr) {
230 disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize());
231 }
232
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100233 for (size_t e = block_order_->size(); current_block_index_ < e; ++current_block_index_) {
234 HBasicBlock* block = (*block_order_)[current_block_index_];
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000235 // Don't generate code for an empty block. Its predecessors will branch to its successor
236 // directly. Also, the label of that block will not be emitted, so this helps catch
237 // errors where we reference that label.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000238 if (block->IsSingleJump()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100239 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100240 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
241 HInstruction* current = it.Current();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100242 DisassemblyScope disassembly_scope(current, *this);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000243 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000244 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000245 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100246 DCHECK(CheckTypeConsistency(current));
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600247 uintptr_t native_pc_begin = GetAssembler()->CodeSize();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100248 current->Accept(instruction_visitor);
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600249 uintptr_t native_pc_end = GetAssembler()->CodeSize();
250 RecordNativeDebugInfo(current->GetDexPc(), native_pc_begin, native_pc_end);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100251 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000252 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000253
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100254 GenerateSlowPaths();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000255
David Brazdil77a48ae2015-09-15 12:34:04 +0000256 // Emit catch stack maps at the end of the stack map stream as expected by the
257 // runtime exception handler.
258 if (!is_baseline && graph_->HasTryCatch()) {
259 RecordCatchBlockInfo();
260 }
261
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000262 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000263 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000264}
265
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100266void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000267 // The register allocator already called `InitializeCodeGeneration`,
268 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000269 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100270 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000271 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000272}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100273
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000274void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100275 size_t code_size = GetAssembler()->CodeSize();
276 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000277
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100278 MemoryRegion code(buffer, code_size);
279 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000280}
281
Vladimir Marko58155012015-08-19 12:49:41 +0000282void CodeGenerator::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches ATTRIBUTE_UNUSED) {
283 // No linker patches by default.
284}
285
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100286size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
287 for (size_t i = 0; i < length; ++i) {
288 if (!array[i]) {
289 array[i] = true;
290 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100291 }
292 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100293 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000294 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000295}
296
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000297size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
298 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000299 if (!array[i] && !array[i + 1]) {
300 array[i] = true;
301 array[i + 1] = true;
302 return i;
303 }
304 }
305 LOG(FATAL) << "Could not find a register in baseline register allocator";
306 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100307}
308
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000309void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
310 size_t maximum_number_of_live_core_registers,
311 size_t maximum_number_of_live_fp_registers,
312 size_t number_of_out_slots,
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100313 const ArenaVector<HBasicBlock*>& block_order) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000314 block_order_ = &block_order;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100315 DCHECK(!block_order.empty());
316 DCHECK(block_order[0] == GetGraph()->GetEntryBlock());
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000317 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100318 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
319
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000320 if (number_of_spill_slots == 0
321 && !HasAllocatedCalleeSaveRegisters()
322 && IsLeafMethod()
323 && !RequiresCurrentMethod()) {
324 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
325 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
326 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
327 } else {
328 SetFrameSize(RoundUp(
329 number_of_spill_slots * kVRegSize
330 + number_of_out_slots * kVRegSize
331 + maximum_number_of_live_core_registers * GetWordSize()
332 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
333 + FrameEntrySpillSize(),
334 kStackAlignment));
335 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100336}
337
338Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
339 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000340 // The type of the previous instruction tells us if we need a single or double stack slot.
341 Primitive::Type type = temp->GetType();
342 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100343 // Use the temporary region (right below the dex registers).
344 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
345 - kVRegSize // filler
346 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000347 - ((temp_size + temp->GetIndex()) * kVRegSize);
348 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100349}
350
351int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
352 uint16_t reg_number = local->GetRegNumber();
353 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
354 if (reg_number >= number_of_locals) {
355 // Local is a parameter of the method. It is stored in the caller's frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700356 // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode.
357 return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100358 + (reg_number - number_of_locals) * kVRegSize;
359 } else {
360 // Local is a temporary in this method. It is stored in this method's frame.
361 return GetFrameSize() - FrameEntrySpillSize()
362 - kVRegSize // filler.
363 - (number_of_locals * kVRegSize)
364 + (reg_number * kVRegSize);
365 }
366}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100367
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100368void CodeGenerator::CreateCommonInvokeLocationSummary(
Nicolas Geoffray4e40c262015-06-03 12:02:38 +0100369 HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100370 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
371 LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100372
373 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
374 HInstruction* input = invoke->InputAt(i);
375 locations->SetInAt(i, visitor->GetNextLocation(input->GetType()));
376 }
377
378 locations->SetOut(visitor->GetReturnLocation(invoke->GetType()));
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100379
380 if (invoke->IsInvokeStaticOrDirect()) {
381 HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect();
382 if (call->IsStringInit()) {
383 locations->AddTemp(visitor->GetMethodLocation());
384 } else if (call->IsRecursive()) {
385 locations->SetInAt(call->GetCurrentMethodInputIndex(), visitor->GetMethodLocation());
386 } else {
387 locations->AddTemp(visitor->GetMethodLocation());
388 locations->SetInAt(call->GetCurrentMethodInputIndex(), Location::RequiresRegister());
389 }
390 } else {
391 locations->AddTemp(visitor->GetMethodLocation());
392 }
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100393}
394
Calin Juravle175dc732015-08-25 15:42:32 +0100395void CodeGenerator::GenerateInvokeUnresolvedRuntimeCall(HInvokeUnresolved* invoke) {
396 MoveConstant(invoke->GetLocations()->GetTemp(0), invoke->GetDexMethodIndex());
397
398 // Initialize to anything to silent compiler warnings.
399 QuickEntrypointEnum entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
400 switch (invoke->GetOriginalInvokeType()) {
401 case kStatic:
402 entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
403 break;
404 case kDirect:
405 entrypoint = kQuickInvokeDirectTrampolineWithAccessCheck;
406 break;
407 case kVirtual:
408 entrypoint = kQuickInvokeVirtualTrampolineWithAccessCheck;
409 break;
410 case kSuper:
411 entrypoint = kQuickInvokeSuperTrampolineWithAccessCheck;
412 break;
413 case kInterface:
414 entrypoint = kQuickInvokeInterfaceTrampolineWithAccessCheck;
415 break;
416 }
417 InvokeRuntime(entrypoint, invoke, invoke->GetDexPc(), nullptr);
418}
419
Calin Juravlee460d1d2015-09-29 04:52:17 +0100420void CodeGenerator::CreateUnresolvedFieldLocationSummary(
421 HInstruction* field_access,
422 Primitive::Type field_type,
423 const FieldAccessCallingConvention& calling_convention) {
424 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
425 || field_access->IsUnresolvedInstanceFieldSet();
426 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
427 || field_access->IsUnresolvedStaticFieldGet();
428
429 ArenaAllocator* allocator = field_access->GetBlock()->GetGraph()->GetArena();
430 LocationSummary* locations =
431 new (allocator) LocationSummary(field_access, LocationSummary::kCall);
432
433 locations->AddTemp(calling_convention.GetFieldIndexLocation());
434
435 if (is_instance) {
436 // Add the `this` object for instance field accesses.
437 locations->SetInAt(0, calling_convention.GetObjectLocation());
438 }
439
440 // Note that pSetXXStatic/pGetXXStatic always takes/returns an int or int64
441 // regardless of the the type. Because of that we forced to special case
442 // the access to floating point values.
443 if (is_get) {
444 if (Primitive::IsFloatingPointType(field_type)) {
445 // The return value will be stored in regular registers while register
446 // allocator expects it in a floating point register.
447 // Note We don't need to request additional temps because the return
448 // register(s) are already blocked due the call and they may overlap with
449 // the input or field index.
450 // The transfer between the two will be done at codegen level.
451 locations->SetOut(calling_convention.GetFpuLocation(field_type));
452 } else {
453 locations->SetOut(calling_convention.GetReturnLocation(field_type));
454 }
455 } else {
456 size_t set_index = is_instance ? 1 : 0;
457 if (Primitive::IsFloatingPointType(field_type)) {
458 // The set value comes from a float location while the calling convention
459 // expects it in a regular register location. Allocate a temp for it and
460 // make the transfer at codegen.
461 AddLocationAsTemp(calling_convention.GetSetValueLocation(field_type, is_instance), locations);
462 locations->SetInAt(set_index, calling_convention.GetFpuLocation(field_type));
463 } else {
464 locations->SetInAt(set_index,
465 calling_convention.GetSetValueLocation(field_type, is_instance));
466 }
467 }
468}
469
470void CodeGenerator::GenerateUnresolvedFieldAccess(
471 HInstruction* field_access,
472 Primitive::Type field_type,
473 uint32_t field_index,
474 uint32_t dex_pc,
475 const FieldAccessCallingConvention& calling_convention) {
476 LocationSummary* locations = field_access->GetLocations();
477
478 MoveConstant(locations->GetTemp(0), field_index);
479
480 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
481 || field_access->IsUnresolvedInstanceFieldSet();
482 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
483 || field_access->IsUnresolvedStaticFieldGet();
484
485 if (!is_get && Primitive::IsFloatingPointType(field_type)) {
486 // Copy the float value to be set into the calling convention register.
487 // Note that using directly the temp location is problematic as we don't
488 // support temp register pairs. To avoid boilerplate conversion code, use
489 // the location from the calling convention.
490 MoveLocation(calling_convention.GetSetValueLocation(field_type, is_instance),
491 locations->InAt(is_instance ? 1 : 0),
492 (Primitive::Is64BitType(field_type) ? Primitive::kPrimLong : Primitive::kPrimInt));
493 }
494
495 QuickEntrypointEnum entrypoint = kQuickSet8Static; // Initialize to anything to avoid warnings.
496 switch (field_type) {
497 case Primitive::kPrimBoolean:
498 entrypoint = is_instance
499 ? (is_get ? kQuickGetBooleanInstance : kQuickSet8Instance)
500 : (is_get ? kQuickGetBooleanStatic : kQuickSet8Static);
501 break;
502 case Primitive::kPrimByte:
503 entrypoint = is_instance
504 ? (is_get ? kQuickGetByteInstance : kQuickSet8Instance)
505 : (is_get ? kQuickGetByteStatic : kQuickSet8Static);
506 break;
507 case Primitive::kPrimShort:
508 entrypoint = is_instance
509 ? (is_get ? kQuickGetShortInstance : kQuickSet16Instance)
510 : (is_get ? kQuickGetShortStatic : kQuickSet16Static);
511 break;
512 case Primitive::kPrimChar:
513 entrypoint = is_instance
514 ? (is_get ? kQuickGetCharInstance : kQuickSet16Instance)
515 : (is_get ? kQuickGetCharStatic : kQuickSet16Static);
516 break;
517 case Primitive::kPrimInt:
518 case Primitive::kPrimFloat:
519 entrypoint = is_instance
520 ? (is_get ? kQuickGet32Instance : kQuickSet32Instance)
521 : (is_get ? kQuickGet32Static : kQuickSet32Static);
522 break;
523 case Primitive::kPrimNot:
524 entrypoint = is_instance
525 ? (is_get ? kQuickGetObjInstance : kQuickSetObjInstance)
526 : (is_get ? kQuickGetObjStatic : kQuickSetObjStatic);
527 break;
528 case Primitive::kPrimLong:
529 case Primitive::kPrimDouble:
530 entrypoint = is_instance
531 ? (is_get ? kQuickGet64Instance : kQuickSet64Instance)
532 : (is_get ? kQuickGet64Static : kQuickSet64Static);
533 break;
534 default:
535 LOG(FATAL) << "Invalid type " << field_type;
536 }
537 InvokeRuntime(entrypoint, field_access, dex_pc, nullptr);
538
539 if (is_get && Primitive::IsFloatingPointType(field_type)) {
540 MoveLocation(locations->Out(), calling_convention.GetReturnLocation(field_type), field_type);
541 }
542}
543
Calin Juravle98893e12015-10-02 21:05:03 +0100544void CodeGenerator::CreateLoadClassLocationSummary(HLoadClass* cls,
545 Location runtime_type_index_location,
546 Location runtime_return_location) {
547 ArenaAllocator* allocator = cls->GetBlock()->GetGraph()->GetArena();
548 LocationSummary::CallKind call_kind = cls->NeedsAccessCheck()
549 ? LocationSummary::kCall
550 : (cls->CanCallRuntime()
551 ? LocationSummary::kCallOnSlowPath
552 : LocationSummary::kNoCall);
553 LocationSummary* locations = new (allocator) LocationSummary(cls, call_kind);
Calin Juravle98893e12015-10-02 21:05:03 +0100554 if (cls->NeedsAccessCheck()) {
Calin Juravle580b6092015-10-06 17:35:58 +0100555 locations->SetInAt(0, Location::NoLocation());
Calin Juravle98893e12015-10-02 21:05:03 +0100556 locations->AddTemp(runtime_type_index_location);
557 locations->SetOut(runtime_return_location);
558 } else {
Calin Juravle580b6092015-10-06 17:35:58 +0100559 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle98893e12015-10-02 21:05:03 +0100560 locations->SetOut(Location::RequiresRegister());
561 }
562}
563
564
Mark Mendell5f874182015-03-04 15:42:45 -0500565void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
566 // The DCHECKS below check that a register is not specified twice in
567 // the summary. The out location can overlap with an input, so we need
568 // to special case it.
569 if (location.IsRegister()) {
570 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
571 blocked_core_registers_[location.reg()] = true;
572 } else if (location.IsFpuRegister()) {
573 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
574 blocked_fpu_registers_[location.reg()] = true;
575 } else if (location.IsFpuRegisterPair()) {
576 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
577 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
578 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
579 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
580 } else if (location.IsRegisterPair()) {
581 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
582 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
583 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
584 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
585 }
586}
587
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100588void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
589 LocationSummary* locations = instruction->GetLocations();
590 if (locations == nullptr) return;
591
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100592 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
593 blocked_core_registers_[i] = false;
594 }
595
596 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
597 blocked_fpu_registers_[i] = false;
598 }
599
600 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
601 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100602 }
603
604 // Mark all fixed input, temp and output registers as used.
605 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500606 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100607 }
608
609 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
610 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500611 BlockIfInRegister(loc);
612 }
613 Location result_location = locations->Out();
614 if (locations->OutputCanOverlapWithInputs()) {
615 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100616 }
617
Mark Mendell5f874182015-03-04 15:42:45 -0500618 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100619
620 // Allocate all unallocated input locations.
621 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
622 Location loc = locations->InAt(i);
623 HInstruction* input = instruction->InputAt(i);
624 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100625 if ((loc.GetPolicy() == Location::kRequiresRegister)
626 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100627 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100628 } else {
629 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
630 HLoadLocal* load = input->AsLoadLocal();
631 if (load != nullptr) {
632 loc = GetStackLocation(load);
633 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100634 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100635 }
636 }
637 locations->SetInAt(i, loc);
638 }
639 }
640
641 // Allocate all unallocated temp locations.
642 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
643 Location loc = locations->GetTemp(i);
644 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000645 switch (loc.GetPolicy()) {
646 case Location::kRequiresRegister:
647 // Allocate a core register (large enough to fit a 32-bit integer).
648 loc = AllocateFreeRegister(Primitive::kPrimInt);
649 break;
650
651 case Location::kRequiresFpuRegister:
652 // Allocate a core register (large enough to fit a 64-bit double).
653 loc = AllocateFreeRegister(Primitive::kPrimDouble);
654 break;
655
656 default:
657 LOG(FATAL) << "Unexpected policy for temporary location "
658 << loc.GetPolicy();
659 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100660 locations->SetTempAt(i, loc);
661 }
662 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100663 if (result_location.IsUnallocated()) {
664 switch (result_location.GetPolicy()) {
665 case Location::kAny:
666 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100667 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100668 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100669 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100670 case Location::kSameAsFirstInput:
671 result_location = locations->InAt(0);
672 break;
673 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000674 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100675 }
676}
677
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000678void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
679 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100680 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100681 if (instruction->IsTemporary()) {
682 HInstruction* previous = instruction->GetPrevious();
683 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
684 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100685 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100686 return;
687 }
688 AllocateRegistersLocally(instruction);
689 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000690 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000691 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000692 if (location.IsValid()) {
693 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000694 if (input->GetNext()->IsTemporary()) {
695 // If the input was stored in a temporary, use that temporary to
696 // perform the move.
697 Move(input->GetNext(), location, instruction);
698 } else {
699 Move(input, location, instruction);
700 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000701 }
702 }
703}
704
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000705void CodeGenerator::AllocateLocations(HInstruction* instruction) {
706 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100707 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000708 LocationSummary* locations = instruction->GetLocations();
709 if (!instruction->IsSuspendCheckEntry()) {
710 if (locations != nullptr && locations->CanCall()) {
711 MarkNotLeaf();
712 }
713 if (instruction->NeedsCurrentMethod()) {
714 SetRequiresCurrentMethod();
715 }
716 }
717}
718
Serban Constantinescuecc43662015-08-13 13:33:12 +0100719void CodeGenerator::MaybeRecordStat(MethodCompilationStat compilation_stat, size_t count) const {
720 if (stats_ != nullptr) {
721 stats_->RecordStat(compilation_stat, count);
722 }
723}
724
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000725CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000726 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000727 const InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100728 const CompilerOptions& compiler_options,
729 OptimizingCompilerStats* stats) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000730 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -0700731#ifdef ART_ENABLE_CODEGEN_arm
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000732 case kArm:
733 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000734 return new arm::CodeGeneratorARM(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100735 *isa_features.AsArmInstructionSetFeatures(),
736 compiler_options,
737 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000738 }
Alex Light50fa9932015-08-10 15:30:07 -0700739#endif
740#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames5319def2014-10-23 10:03:10 +0100741 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000742 return new arm64::CodeGeneratorARM64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100743 *isa_features.AsArm64InstructionSetFeatures(),
744 compiler_options,
745 stats);
Alexandre Rames5319def2014-10-23 10:03:10 +0100746 }
Alex Light50fa9932015-08-10 15:30:07 -0700747#endif
748#ifdef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200749 case kMips: {
750 return new mips::CodeGeneratorMIPS(graph,
751 *isa_features.AsMipsInstructionSetFeatures(),
752 compiler_options,
753 stats);
754 }
Alex Light50fa9932015-08-10 15:30:07 -0700755#endif
756#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700757 case kMips64: {
758 return new mips64::CodeGeneratorMIPS64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100759 *isa_features.AsMips64InstructionSetFeatures(),
760 compiler_options,
761 stats);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700762 }
Alex Light50fa9932015-08-10 15:30:07 -0700763#endif
764#ifdef ART_ENABLE_CODEGEN_x86
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000765 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400766 return new x86::CodeGeneratorX86(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100767 *isa_features.AsX86InstructionSetFeatures(),
768 compiler_options,
769 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000770 }
Alex Light50fa9932015-08-10 15:30:07 -0700771#endif
772#ifdef ART_ENABLE_CODEGEN_x86_64
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700773 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400774 return new x86_64::CodeGeneratorX86_64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100775 *isa_features.AsX86_64InstructionSetFeatures(),
776 compiler_options,
777 stats);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700778 }
Alex Light50fa9932015-08-10 15:30:07 -0700779#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000780 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000781 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000782 }
783}
784
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000785void CodeGenerator::BuildNativeGCMap(
Vladimir Markof9f64412015-09-02 14:05:49 +0100786 ArenaVector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000787 const std::vector<uint8_t>& gc_map_raw =
788 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
789 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
790
Vladimir Markobd8c7252015-06-12 10:06:32 +0100791 uint32_t max_native_offset = stack_map_stream_.ComputeMaxNativePcOffset();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000792
Vladimir Markobd8c7252015-06-12 10:06:32 +0100793 size_t num_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
794 GcMapBuilder builder(data, num_stack_maps, max_native_offset, dex_gc_map.RegWidth());
795 for (size_t i = 0; i != num_stack_maps; ++i) {
796 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
797 uint32_t native_offset = stack_map_entry.native_pc_offset;
798 uint32_t dex_pc = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000799 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800800 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000801 builder.AddEntry(native_offset, references);
802 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000803}
804
Vladimir Markof9f64412015-09-02 14:05:49 +0100805void CodeGenerator::BuildMappingTable(ArenaVector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000806 uint32_t pc2dex_data_size = 0u;
Vladimir Markobd8c7252015-06-12 10:06:32 +0100807 uint32_t pc2dex_entries = stack_map_stream_.GetNumberOfStackMaps();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000808 uint32_t pc2dex_offset = 0u;
809 int32_t pc2dex_dalvik_offset = 0;
810 uint32_t dex2pc_data_size = 0u;
811 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000812 uint32_t dex2pc_offset = 0u;
813 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000814
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000815 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100816 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
817 pc2dex_data_size += UnsignedLeb128Size(stack_map_entry.native_pc_offset - pc2dex_offset);
818 pc2dex_data_size += SignedLeb128Size(stack_map_entry.dex_pc - pc2dex_dalvik_offset);
819 pc2dex_offset = stack_map_entry.native_pc_offset;
820 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000821 }
822
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000823 // Walk over the blocks and find which ones correspond to catch block entries.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100824 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000825 if (block->IsCatchBlock()) {
826 intptr_t native_pc = GetAddressOf(block);
827 ++dex2pc_entries;
828 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
829 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
830 dex2pc_offset = native_pc;
831 dex2pc_dalvik_offset = block->GetDexPc();
832 }
833 }
834
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000835 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
836 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
837 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
838 data->resize(data_size);
839
840 uint8_t* data_ptr = &(*data)[0];
841 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000842
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000843 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
844 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
845 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
846 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
847
848 pc2dex_offset = 0u;
849 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000850 dex2pc_offset = 0u;
851 dex2pc_dalvik_offset = 0u;
852
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000853 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100854 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
855 DCHECK(pc2dex_offset <= stack_map_entry.native_pc_offset);
856 write_pos = EncodeUnsignedLeb128(write_pos, stack_map_entry.native_pc_offset - pc2dex_offset);
857 write_pos = EncodeSignedLeb128(write_pos, stack_map_entry.dex_pc - pc2dex_dalvik_offset);
858 pc2dex_offset = stack_map_entry.native_pc_offset;
859 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000860 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000861
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100862 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000863 if (block->IsCatchBlock()) {
864 intptr_t native_pc = GetAddressOf(block);
865 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
866 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
867 dex2pc_offset = native_pc;
868 dex2pc_dalvik_offset = block->GetDexPc();
869 }
870 }
871
872
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000873 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
874 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
875
876 if (kIsDebugBuild) {
877 // Verify the encoded table holds the expected data.
878 MappingTable table(data_ptr);
879 CHECK_EQ(table.TotalSize(), total_entries);
880 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
881 auto it = table.PcToDexBegin();
882 auto it2 = table.DexToPcBegin();
883 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100884 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
885 CHECK_EQ(stack_map_entry.native_pc_offset, it.NativePcOffset());
886 CHECK_EQ(stack_map_entry.dex_pc, it.DexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000887 ++it;
888 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100889 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000890 if (block->IsCatchBlock()) {
891 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
892 CHECK_EQ(block->GetDexPc(), it2.DexPc());
893 ++it2;
894 }
895 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000896 CHECK(it == table.PcToDexEnd());
897 CHECK(it2 == table.DexToPcEnd());
898 }
899}
900
Vladimir Markof9f64412015-09-02 14:05:49 +0100901void CodeGenerator::BuildVMapTable(ArenaVector<uint8_t>* data) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100902 Leb128Encoder<ArenaVector<uint8_t>> vmap_encoder(data);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100903 // We currently don't use callee-saved registers.
904 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000905 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
906 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000907 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000908}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000909
Vladimir Markof9f64412015-09-02 14:05:49 +0100910void CodeGenerator::BuildStackMaps(ArenaVector<uint8_t>* data) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100911 uint32_t size = stack_map_stream_.PrepareForFillIn();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100912 data->resize(size);
913 MemoryRegion region(data->data(), size);
914 stack_map_stream_.FillIn(region);
915}
916
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600917void CodeGenerator::RecordNativeDebugInfo(uint32_t dex_pc,
918 uintptr_t native_pc_begin,
919 uintptr_t native_pc_end) {
920 if (src_map_ != nullptr && dex_pc != kNoDexPc && native_pc_begin != native_pc_end) {
921 src_map_->push_back(SrcMapElem({static_cast<uint32_t>(native_pc_begin),
922 static_cast<int32_t>(dex_pc)}));
923 }
924}
925
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000926void CodeGenerator::RecordPcInfo(HInstruction* instruction,
927 uint32_t dex_pc,
928 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000929 if (instruction != nullptr) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700930 // The code generated for some type conversions and comparisons
931 // may call the runtime, thus normally requiring a subsequent
932 // call to this method. However, the method verifier does not
933 // produce PC information for certain instructions, which are
934 // considered "atomic" (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000935 // Therefore we do not currently record PC information for such
936 // instructions. As this may change later, we added this special
937 // case so that code generators may nevertheless call
938 // CodeGenerator::RecordPcInfo without triggering an error in
939 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
940 // thereafter.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700941 if (instruction->IsTypeConversion() || instruction->IsCompare()) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000942 return;
943 }
944 if (instruction->IsRem()) {
945 Primitive::Type type = instruction->AsRem()->GetResultType();
946 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
947 return;
948 }
949 }
Roland Levillain624279f2014-12-04 11:54:28 +0000950 }
951
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100952 uint32_t outer_dex_pc = dex_pc;
953 uint32_t outer_environment_size = 0;
954 uint32_t inlining_depth = 0;
955 if (instruction != nullptr) {
956 for (HEnvironment* environment = instruction->GetEnvironment();
957 environment != nullptr;
958 environment = environment->GetParent()) {
959 outer_dex_pc = environment->GetDexPc();
960 outer_environment_size = environment->Size();
961 if (environment != instruction->GetEnvironment()) {
962 inlining_depth++;
963 }
964 }
965 }
966
Nicolas Geoffray39468442014-09-02 15:17:15 +0100967 // Collect PC infos for the mapping table.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100968 uint32_t native_pc = GetAssembler()->CodeSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100969
Nicolas Geoffray39468442014-09-02 15:17:15 +0100970 if (instruction == nullptr) {
971 // For stack overflow checks.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100972 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100973 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000974 return;
975 }
976 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100977
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000978 uint32_t register_mask = locations->GetRegisterMask();
979 if (locations->OnlyCallsOnSlowPath()) {
980 // In case of slow path, we currently set the location of caller-save registers
981 // to register (instead of their stack location when pushed before the slow-path
982 // call). Therefore register_mask contains both callee-save and caller-save
983 // registers that hold objects. We must remove the caller-save from the mask, since
984 // they will be overwritten by the callee.
985 register_mask &= core_callee_save_mask_;
986 }
987 // The register mask must be a subset of callee-save registers.
988 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100989 stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
990 native_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100991 register_mask,
992 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100993 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100994 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100995
996 EmitEnvironment(instruction->GetEnvironment(), slow_path);
997 stack_map_stream_.EndStackMapEntry();
998}
999
David Brazdil77a48ae2015-09-15 12:34:04 +00001000void CodeGenerator::RecordCatchBlockInfo() {
1001 ArenaAllocator* arena = graph_->GetArena();
1002
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001003 for (HBasicBlock* block : *block_order_) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001004 if (!block->IsCatchBlock()) {
1005 continue;
1006 }
1007
1008 uint32_t dex_pc = block->GetDexPc();
1009 uint32_t num_vregs = graph_->GetNumberOfVRegs();
1010 uint32_t inlining_depth = 0; // Inlining of catch blocks is not supported at the moment.
1011 uint32_t native_pc = GetAddressOf(block);
1012 uint32_t register_mask = 0; // Not used.
1013
1014 // The stack mask is not used, so we leave it empty.
1015 ArenaBitVector* stack_mask = new (arena) ArenaBitVector(arena, 0, /* expandable */ true);
1016
1017 stack_map_stream_.BeginStackMapEntry(dex_pc,
1018 native_pc,
1019 register_mask,
1020 stack_mask,
1021 num_vregs,
1022 inlining_depth);
1023
1024 HInstruction* current_phi = block->GetFirstPhi();
1025 for (size_t vreg = 0; vreg < num_vregs; ++vreg) {
1026 while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) {
1027 HInstruction* next_phi = current_phi->GetNext();
1028 DCHECK(next_phi == nullptr ||
1029 current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber())
1030 << "Phis need to be sorted by vreg number to keep this a linear-time loop.";
1031 current_phi = next_phi;
1032 }
1033
1034 if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) {
1035 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
1036 } else {
1037 Location location = current_phi->GetLiveInterval()->ToLocation();
1038 switch (location.GetKind()) {
1039 case Location::kStackSlot: {
1040 stack_map_stream_.AddDexRegisterEntry(
1041 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
1042 break;
1043 }
1044 case Location::kDoubleStackSlot: {
1045 stack_map_stream_.AddDexRegisterEntry(
1046 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
1047 stack_map_stream_.AddDexRegisterEntry(
1048 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
1049 ++vreg;
1050 DCHECK_LT(vreg, num_vregs);
1051 break;
1052 }
1053 default: {
1054 // All catch phis must be allocated to a stack slot.
1055 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1056 UNREACHABLE();
1057 }
1058 }
1059 }
1060 }
1061
1062 stack_map_stream_.EndStackMapEntry();
1063 }
1064}
1065
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001066void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
1067 if (environment == nullptr) return;
1068
1069 if (environment->GetParent() != nullptr) {
1070 // We emit the parent environment first.
1071 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001072 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
1073 environment->GetDexPc(),
1074 environment->GetInvokeType(),
1075 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001076 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001077
1078 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001079 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001080 HInstruction* current = environment->GetInstructionAt(i);
1081 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001082 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001083 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001084 }
1085
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001086 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001087 switch (location.GetKind()) {
1088 case Location::kConstant: {
1089 DCHECK_EQ(current, location.GetConstant());
1090 if (current->IsLongConstant()) {
1091 int64_t value = current->AsLongConstant()->GetValue();
1092 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001093 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001094 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001095 DexRegisterLocation::Kind::kConstant, High32Bits(value));
1096 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001097 DCHECK_LT(i, environment_size);
1098 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001099 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001100 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001101 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001102 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001103 DexRegisterLocation::Kind::kConstant, High32Bits(value));
1104 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001105 DCHECK_LT(i, environment_size);
1106 } else if (current->IsIntConstant()) {
1107 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001108 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001109 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001110 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001111 } else {
1112 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +00001113 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001114 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001115 }
1116 break;
1117 }
1118
1119 case Location::kStackSlot: {
1120 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001121 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001122 break;
1123 }
1124
1125 case Location::kDoubleStackSlot: {
1126 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001127 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001128 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001129 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
1130 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001131 DCHECK_LT(i, environment_size);
1132 break;
1133 }
1134
1135 case Location::kRegister : {
1136 int id = location.reg();
1137 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
1138 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001139 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001140 if (current->GetType() == Primitive::kPrimLong) {
1141 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001142 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
1143 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001144 DCHECK_LT(i, environment_size);
1145 }
1146 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001147 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001148 if (current->GetType() == Primitive::kPrimLong) {
David Brazdild9cb68e2015-08-25 13:52:43 +01001149 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001150 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001151 DCHECK_LT(i, environment_size);
1152 }
1153 }
1154 break;
1155 }
1156
1157 case Location::kFpuRegister : {
1158 int id = location.reg();
1159 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
1160 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001161 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001162 if (current->GetType() == Primitive::kPrimDouble) {
1163 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001164 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
1165 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001166 DCHECK_LT(i, environment_size);
1167 }
1168 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001169 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001170 if (current->GetType() == Primitive::kPrimDouble) {
David Brazdild9cb68e2015-08-25 13:52:43 +01001171 stack_map_stream_.AddDexRegisterEntry(
1172 DexRegisterLocation::Kind::kInFpuRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001173 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001174 DCHECK_LT(i, environment_size);
1175 }
1176 }
1177 break;
1178 }
1179
1180 case Location::kFpuRegisterPair : {
1181 int low = location.low();
1182 int high = location.high();
1183 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
1184 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001185 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001186 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001187 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001188 }
1189 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
1190 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001191 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1192 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001193 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001194 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
1195 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001196 }
1197 DCHECK_LT(i, environment_size);
1198 break;
1199 }
1200
1201 case Location::kRegisterPair : {
1202 int low = location.low();
1203 int high = location.high();
1204 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
1205 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001206 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001207 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001208 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001209 }
1210 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
1211 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001212 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001213 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001214 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001215 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001216 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001217 DCHECK_LT(i, environment_size);
1218 break;
1219 }
1220
1221 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001222 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001223 break;
1224 }
1225
1226 default:
1227 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1228 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001229 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001230
1231 if (environment->GetParent() != nullptr) {
1232 stack_map_stream_.EndInlineInfoEntry();
1233 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001234}
1235
David Brazdil77a48ae2015-09-15 12:34:04 +00001236bool CodeGenerator::IsImplicitNullCheckAllowed(HNullCheck* null_check) const {
1237 return compiler_options_.GetImplicitNullChecks() &&
1238 // Null checks which might throw into a catch block need to save live
1239 // registers and therefore cannot be done implicitly.
1240 !null_check->CanThrowIntoCatchBlock();
1241}
1242
Calin Juravle77520bc2015-01-12 18:45:46 +00001243bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
1244 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +01001245
1246 return (first_next_not_move != nullptr)
1247 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00001248}
1249
1250void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
1251 // If we are from a static path don't record the pc as we can't throw NPE.
1252 // NB: having the checks here makes the code much less verbose in the arch
1253 // specific code generators.
1254 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
1255 return;
1256 }
1257
Calin Juravle641547a2015-04-21 22:08:51 +01001258 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001259 return;
1260 }
1261
1262 // Find the first previous instruction which is not a move.
1263 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
1264
1265 // If the instruction is a null check it means that `instr` is the first user
1266 // and needs to record the pc.
1267 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
1268 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
David Brazdil77a48ae2015-09-15 12:34:04 +00001269 if (IsImplicitNullCheckAllowed(null_check)) {
1270 // TODO: The parallel moves modify the environment. Their changes need to be
1271 // reverted otherwise the stack maps at the throw point will not be correct.
1272 RecordPcInfo(null_check, null_check->GetDexPc());
1273 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001274 }
1275}
1276
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001277void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
1278 LocationSummary* locations = suspend_check->GetLocations();
1279 HBasicBlock* block = suspend_check->GetBlock();
1280 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
1281 DCHECK(block->IsLoopHeader());
1282
1283 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1284 HInstruction* current = it.Current();
1285 LiveInterval* interval = current->GetLiveInterval();
1286 // We only need to clear bits of loop phis containing objects and allocated in register.
1287 // Loop phis allocated on stack already have the object in the stack.
1288 if (current->GetType() == Primitive::kPrimNot
1289 && interval->HasRegister()
1290 && interval->HasSpillSlot()) {
1291 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
1292 }
1293 }
1294}
1295
Nicolas Geoffray90218252015-04-15 11:56:51 +01001296void CodeGenerator::EmitParallelMoves(Location from1,
1297 Location to1,
1298 Primitive::Type type1,
1299 Location from2,
1300 Location to2,
1301 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001302 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +01001303 parallel_move.AddMove(from1, to1, type1, nullptr);
1304 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001305 GetMoveResolver()->EmitNativeCode(&parallel_move);
1306}
1307
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001308void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) {
1309 // Ensure that the call kind indication given to the register allocator is
1310 // coherent with the runtime call generated, and that the GC side effect is
1311 // set when required.
1312 if (slow_path == nullptr) {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001313 DCHECK(instruction->GetLocations()->WillCall()) << instruction->DebugName();
1314 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()))
1315 << instruction->DebugName() << instruction->GetSideEffects().ToString();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001316 } else {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001317 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal())
1318 << instruction->DebugName() << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001319 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) ||
1320 // Control flow would not come back into the code if a fatal slow
1321 // path is taken, so we do not care if it triggers GC.
1322 slow_path->IsFatal() ||
1323 // HDeoptimize is a special case: we know we are not coming back from
1324 // it into the code.
Roland Levillaindf3f8222015-08-13 12:31:44 +01001325 instruction->IsDeoptimize())
1326 << instruction->DebugName() << instruction->GetSideEffects().ToString()
1327 << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001328 }
1329
1330 // Check the coherency of leaf information.
1331 DCHECK(instruction->IsSuspendCheck()
1332 || ((slow_path != nullptr) && slow_path->IsFatal())
1333 || instruction->GetLocations()->CanCall()
Roland Levillaindf3f8222015-08-13 12:31:44 +01001334 || !IsLeafMethod())
1335 << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : "");
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001336}
1337
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001338void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1339 RegisterSet* register_set = locations->GetLiveRegisters();
1340 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1341 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1342 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1343 if (register_set->ContainsCoreRegister(i)) {
1344 // If the register holds an object, update the stack mask.
1345 if (locations->RegisterContainsObject(i)) {
1346 locations->SetStackBit(stack_offset / kVRegSize);
1347 }
1348 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001349 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1350 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001351 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
1352 }
1353 }
1354 }
1355
1356 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1357 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1358 if (register_set->ContainsFloatingPointRegister(i)) {
1359 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001360 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1361 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001362 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
1363 }
1364 }
1365 }
1366}
1367
1368void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1369 RegisterSet* register_set = locations->GetLiveRegisters();
1370 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1371 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1372 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1373 if (register_set->ContainsCoreRegister(i)) {
1374 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1375 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1376 }
1377 }
1378 }
1379
1380 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1381 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1382 if (register_set->ContainsFloatingPointRegister(i)) {
1383 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1384 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1385 }
1386 }
1387 }
1388}
1389
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001390void CodeGenerator::CreateSystemArrayCopyLocationSummary(HInvoke* invoke) {
1391 // Check to see if we have known failures that will cause us to have to bail out
1392 // to the runtime, and just generate the runtime call directly.
1393 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1394 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
1395
1396 // The positions must be non-negative.
1397 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
1398 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
1399 // We will have to fail anyways.
1400 return;
1401 }
1402
1403 // The length must be >= 0.
1404 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1405 if (length != nullptr) {
1406 int32_t len = length->GetValue();
1407 if (len < 0) {
1408 // Just call as normal.
1409 return;
1410 }
1411 }
1412
1413 SystemArrayCopyOptimizations optimizations(invoke);
1414
1415 if (optimizations.GetDestinationIsSource()) {
1416 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
1417 // We only support backward copying if source and destination are the same.
1418 return;
1419 }
1420 }
1421
1422 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
1423 // We currently don't intrinsify primitive copying.
1424 return;
1425 }
1426
1427 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
1428 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1429 LocationSummary::kCallOnSlowPath,
1430 kIntrinsified);
1431 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
1432 locations->SetInAt(0, Location::RequiresRegister());
1433 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1434 locations->SetInAt(2, Location::RequiresRegister());
1435 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
1436 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
1437
1438 locations->AddTemp(Location::RequiresRegister());
1439 locations->AddTemp(Location::RequiresRegister());
1440 locations->AddTemp(Location::RequiresRegister());
1441}
1442
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001443} // namespace art