blob: 28970062cc514683ba2b3e4c6b8056907bd414f8 [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
35#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -070036#include "code_generator_mips64.h"
Alex Light50fa9932015-08-10 15:30:07 -070037#endif
38
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070039#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000040#include "dex/verified_method.h"
41#include "driver/dex_compilation_unit.h"
42#include "gc_map_builder.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010043#include "graph_visualizer.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000044#include "leb128.h"
45#include "mapping_table.h"
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010046#include "mirror/array-inl.h"
47#include "mirror/object_array-inl.h"
48#include "mirror/object_reference.h"
Alex Light50fa9932015-08-10 15:30:07 -070049#include "parallel_move_resolver.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010050#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000051#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000052#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000053#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000054
55namespace art {
56
Alexandre Rames88c13cd2015-04-14 17:35:39 +010057// Return whether a location is consistent with a type.
58static bool CheckType(Primitive::Type type, Location location) {
59 if (location.IsFpuRegister()
60 || (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresFpuRegister))) {
61 return (type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble);
62 } else if (location.IsRegister() ||
63 (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresRegister))) {
64 return Primitive::IsIntegralType(type) || (type == Primitive::kPrimNot);
65 } else if (location.IsRegisterPair()) {
66 return type == Primitive::kPrimLong;
67 } else if (location.IsFpuRegisterPair()) {
68 return type == Primitive::kPrimDouble;
69 } else if (location.IsStackSlot()) {
70 return (Primitive::IsIntegralType(type) && type != Primitive::kPrimLong)
71 || (type == Primitive::kPrimFloat)
72 || (type == Primitive::kPrimNot);
73 } else if (location.IsDoubleStackSlot()) {
74 return (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
75 } else if (location.IsConstant()) {
76 if (location.GetConstant()->IsIntConstant()) {
77 return Primitive::IsIntegralType(type) && (type != Primitive::kPrimLong);
78 } else if (location.GetConstant()->IsNullConstant()) {
79 return type == Primitive::kPrimNot;
80 } else if (location.GetConstant()->IsLongConstant()) {
81 return type == Primitive::kPrimLong;
82 } else if (location.GetConstant()->IsFloatConstant()) {
83 return type == Primitive::kPrimFloat;
84 } else {
85 return location.GetConstant()->IsDoubleConstant()
86 && (type == Primitive::kPrimDouble);
87 }
88 } else {
89 return location.IsInvalid() || (location.GetPolicy() == Location::kAny);
90 }
91}
92
93// Check that a location summary is consistent with an instruction.
94static bool CheckTypeConsistency(HInstruction* instruction) {
95 LocationSummary* locations = instruction->GetLocations();
96 if (locations == nullptr) {
97 return true;
98 }
99
100 if (locations->Out().IsUnallocated()
101 && (locations->Out().GetPolicy() == Location::kSameAsFirstInput)) {
102 DCHECK(CheckType(instruction->GetType(), locations->InAt(0)))
103 << instruction->GetType()
104 << " " << locations->InAt(0);
105 } else {
106 DCHECK(CheckType(instruction->GetType(), locations->Out()))
107 << instruction->GetType()
108 << " " << locations->Out();
109 }
110
111 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
112 DCHECK(CheckType(instruction->InputAt(i)->GetType(), locations->InAt(i)))
113 << instruction->InputAt(i)->GetType()
114 << " " << locations->InAt(i);
115 }
116
117 HEnvironment* environment = instruction->GetEnvironment();
118 for (size_t i = 0; i < instruction->EnvironmentSize(); ++i) {
119 if (environment->GetInstructionAt(i) != nullptr) {
120 Primitive::Type type = environment->GetInstructionAt(i)->GetType();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100121 DCHECK(CheckType(type, environment->GetLocationAt(i)))
122 << type << " " << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100123 } else {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100124 DCHECK(environment->GetLocationAt(i).IsInvalid())
125 << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100126 }
127 }
128 return true;
129}
130
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100131size_t CodeGenerator::GetCacheOffset(uint32_t index) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100132 return sizeof(GcRoot<mirror::Object>) * index;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100133}
134
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135size_t CodeGenerator::GetCachePointerOffset(uint32_t index) {
136 auto pointer_size = InstructionSetPointerSize(GetInstructionSet());
Vladimir Marko05792b92015-08-03 11:56:49 +0100137 return pointer_size * index;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700138}
139
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100140void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000141 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100142 if (!is_leaf) {
143 MarkNotLeaf();
144 }
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700145 const bool is_64_bit = Is64BitInstructionSet(GetInstructionSet());
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000146 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
147 + GetGraph()->GetTemporariesVRegSlots()
148 + 1 /* filler */,
149 0, /* the baseline compiler does not have live registers at slow path */
150 0, /* the baseline compiler does not have live registers at slow path */
151 GetGraph()->GetMaximumNumberOfOutVRegs()
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700152 + (is_64_bit ? 2 : 1) /* current method */,
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000153 GetGraph()->GetBlocks());
154 CompileInternal(allocator, /* is_baseline */ true);
155}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100156
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000157bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100158 DCHECK_EQ((*block_order_)[current_block_index_], current);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000159 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
160}
161
162HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100163 for (size_t i = current_block_index_ + 1; i < block_order_->size(); ++i) {
164 HBasicBlock* block = (*block_order_)[i];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000165 if (!block->IsSingleJump()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000166 return block;
167 }
168 }
169 return nullptr;
170}
171
172HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000173 while (block->IsSingleJump()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100174 block = block->GetSuccessors()[0];
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000175 }
176 return block;
177}
178
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100179class DisassemblyScope {
180 public:
181 DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen)
182 : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) {
183 if (codegen_.GetDisassemblyInformation() != nullptr) {
184 start_offset_ = codegen_.GetAssembler().CodeSize();
185 }
186 }
187
188 ~DisassemblyScope() {
189 // We avoid building this data when we know it will not be used.
190 if (codegen_.GetDisassemblyInformation() != nullptr) {
191 codegen_.GetDisassemblyInformation()->AddInstructionInterval(
192 instruction_, start_offset_, codegen_.GetAssembler().CodeSize());
193 }
194 }
195
196 private:
197 const CodeGenerator& codegen_;
198 HInstruction* instruction_;
199 size_t start_offset_;
200};
201
202
203void CodeGenerator::GenerateSlowPaths() {
204 size_t code_start = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100205 for (SlowPathCode* slow_path : slow_paths_) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100206 if (disasm_info_ != nullptr) {
207 code_start = GetAssembler()->CodeSize();
208 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100209 slow_path->EmitNativeCode(this);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100210 if (disasm_info_ != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100211 disasm_info_->AddSlowPathInterval(slow_path, code_start, GetAssembler()->CodeSize());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100212 }
213 }
214}
215
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000216void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Roland Levillain3e3d7332015-04-28 11:00:54 +0100217 is_baseline_ = is_baseline;
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100218 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000219 DCHECK_EQ(current_block_index_, 0u);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100220
221 size_t frame_start = GetAssembler()->CodeSize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000222 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100223 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100224 if (disasm_info_ != nullptr) {
225 disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize());
226 }
227
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100228 for (size_t e = block_order_->size(); current_block_index_ < e; ++current_block_index_) {
229 HBasicBlock* block = (*block_order_)[current_block_index_];
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000230 // Don't generate code for an empty block. Its predecessors will branch to its successor
231 // directly. Also, the label of that block will not be emitted, so this helps catch
232 // errors where we reference that label.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000233 if (block->IsSingleJump()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100234 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100235 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
236 HInstruction* current = it.Current();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100237 DisassemblyScope disassembly_scope(current, *this);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000238 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000239 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000240 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100241 DCHECK(CheckTypeConsistency(current));
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600242 uintptr_t native_pc_begin = GetAssembler()->CodeSize();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100243 current->Accept(instruction_visitor);
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600244 uintptr_t native_pc_end = GetAssembler()->CodeSize();
245 RecordNativeDebugInfo(current->GetDexPc(), native_pc_begin, native_pc_end);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100246 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000247 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000248
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100249 GenerateSlowPaths();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000250
David Brazdil77a48ae2015-09-15 12:34:04 +0000251 // Emit catch stack maps at the end of the stack map stream as expected by the
252 // runtime exception handler.
253 if (!is_baseline && graph_->HasTryCatch()) {
254 RecordCatchBlockInfo();
255 }
256
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000257 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000258 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000259}
260
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100261void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000262 // The register allocator already called `InitializeCodeGeneration`,
263 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000264 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100265 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000266 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000267}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100268
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000269void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100270 size_t code_size = GetAssembler()->CodeSize();
271 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000272
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100273 MemoryRegion code(buffer, code_size);
274 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000275}
276
Vladimir Marko58155012015-08-19 12:49:41 +0000277void CodeGenerator::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches ATTRIBUTE_UNUSED) {
278 // No linker patches by default.
279}
280
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100281size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
282 for (size_t i = 0; i < length; ++i) {
283 if (!array[i]) {
284 array[i] = true;
285 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100286 }
287 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100288 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000289 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000290}
291
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000292size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
293 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000294 if (!array[i] && !array[i + 1]) {
295 array[i] = true;
296 array[i + 1] = true;
297 return i;
298 }
299 }
300 LOG(FATAL) << "Could not find a register in baseline register allocator";
301 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100302}
303
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000304void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
305 size_t maximum_number_of_live_core_registers,
306 size_t maximum_number_of_live_fp_registers,
307 size_t number_of_out_slots,
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100308 const ArenaVector<HBasicBlock*>& block_order) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000309 block_order_ = &block_order;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100310 DCHECK(!block_order.empty());
311 DCHECK(block_order[0] == GetGraph()->GetEntryBlock());
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000312 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100313 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
314
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000315 if (number_of_spill_slots == 0
316 && !HasAllocatedCalleeSaveRegisters()
317 && IsLeafMethod()
318 && !RequiresCurrentMethod()) {
319 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
320 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
321 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
322 } else {
323 SetFrameSize(RoundUp(
324 number_of_spill_slots * kVRegSize
325 + number_of_out_slots * kVRegSize
326 + maximum_number_of_live_core_registers * GetWordSize()
327 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
328 + FrameEntrySpillSize(),
329 kStackAlignment));
330 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100331}
332
333Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
334 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000335 // The type of the previous instruction tells us if we need a single or double stack slot.
336 Primitive::Type type = temp->GetType();
337 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100338 // Use the temporary region (right below the dex registers).
339 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
340 - kVRegSize // filler
341 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000342 - ((temp_size + temp->GetIndex()) * kVRegSize);
343 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100344}
345
346int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
347 uint16_t reg_number = local->GetRegNumber();
348 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
349 if (reg_number >= number_of_locals) {
350 // Local is a parameter of the method. It is stored in the caller's frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700351 // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode.
352 return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100353 + (reg_number - number_of_locals) * kVRegSize;
354 } else {
355 // Local is a temporary in this method. It is stored in this method's frame.
356 return GetFrameSize() - FrameEntrySpillSize()
357 - kVRegSize // filler.
358 - (number_of_locals * kVRegSize)
359 + (reg_number * kVRegSize);
360 }
361}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100362
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100363void CodeGenerator::CreateCommonInvokeLocationSummary(
Nicolas Geoffray4e40c262015-06-03 12:02:38 +0100364 HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100365 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
366 LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100367
368 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
369 HInstruction* input = invoke->InputAt(i);
370 locations->SetInAt(i, visitor->GetNextLocation(input->GetType()));
371 }
372
373 locations->SetOut(visitor->GetReturnLocation(invoke->GetType()));
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100374
375 if (invoke->IsInvokeStaticOrDirect()) {
376 HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect();
377 if (call->IsStringInit()) {
378 locations->AddTemp(visitor->GetMethodLocation());
379 } else if (call->IsRecursive()) {
380 locations->SetInAt(call->GetCurrentMethodInputIndex(), visitor->GetMethodLocation());
381 } else {
382 locations->AddTemp(visitor->GetMethodLocation());
383 locations->SetInAt(call->GetCurrentMethodInputIndex(), Location::RequiresRegister());
384 }
385 } else {
386 locations->AddTemp(visitor->GetMethodLocation());
387 }
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100388}
389
Calin Juravle175dc732015-08-25 15:42:32 +0100390void CodeGenerator::GenerateInvokeUnresolvedRuntimeCall(HInvokeUnresolved* invoke) {
391 MoveConstant(invoke->GetLocations()->GetTemp(0), invoke->GetDexMethodIndex());
392
393 // Initialize to anything to silent compiler warnings.
394 QuickEntrypointEnum entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
395 switch (invoke->GetOriginalInvokeType()) {
396 case kStatic:
397 entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
398 break;
399 case kDirect:
400 entrypoint = kQuickInvokeDirectTrampolineWithAccessCheck;
401 break;
402 case kVirtual:
403 entrypoint = kQuickInvokeVirtualTrampolineWithAccessCheck;
404 break;
405 case kSuper:
406 entrypoint = kQuickInvokeSuperTrampolineWithAccessCheck;
407 break;
408 case kInterface:
409 entrypoint = kQuickInvokeInterfaceTrampolineWithAccessCheck;
410 break;
411 }
412 InvokeRuntime(entrypoint, invoke, invoke->GetDexPc(), nullptr);
413}
414
Calin Juravlee460d1d2015-09-29 04:52:17 +0100415void CodeGenerator::CreateUnresolvedFieldLocationSummary(
416 HInstruction* field_access,
417 Primitive::Type field_type,
418 const FieldAccessCallingConvention& calling_convention) {
419 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
420 || field_access->IsUnresolvedInstanceFieldSet();
421 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
422 || field_access->IsUnresolvedStaticFieldGet();
423
424 ArenaAllocator* allocator = field_access->GetBlock()->GetGraph()->GetArena();
425 LocationSummary* locations =
426 new (allocator) LocationSummary(field_access, LocationSummary::kCall);
427
428 locations->AddTemp(calling_convention.GetFieldIndexLocation());
429
430 if (is_instance) {
431 // Add the `this` object for instance field accesses.
432 locations->SetInAt(0, calling_convention.GetObjectLocation());
433 }
434
435 // Note that pSetXXStatic/pGetXXStatic always takes/returns an int or int64
436 // regardless of the the type. Because of that we forced to special case
437 // the access to floating point values.
438 if (is_get) {
439 if (Primitive::IsFloatingPointType(field_type)) {
440 // The return value will be stored in regular registers while register
441 // allocator expects it in a floating point register.
442 // Note We don't need to request additional temps because the return
443 // register(s) are already blocked due the call and they may overlap with
444 // the input or field index.
445 // The transfer between the two will be done at codegen level.
446 locations->SetOut(calling_convention.GetFpuLocation(field_type));
447 } else {
448 locations->SetOut(calling_convention.GetReturnLocation(field_type));
449 }
450 } else {
451 size_t set_index = is_instance ? 1 : 0;
452 if (Primitive::IsFloatingPointType(field_type)) {
453 // The set value comes from a float location while the calling convention
454 // expects it in a regular register location. Allocate a temp for it and
455 // make the transfer at codegen.
456 AddLocationAsTemp(calling_convention.GetSetValueLocation(field_type, is_instance), locations);
457 locations->SetInAt(set_index, calling_convention.GetFpuLocation(field_type));
458 } else {
459 locations->SetInAt(set_index,
460 calling_convention.GetSetValueLocation(field_type, is_instance));
461 }
462 }
463}
464
465void CodeGenerator::GenerateUnresolvedFieldAccess(
466 HInstruction* field_access,
467 Primitive::Type field_type,
468 uint32_t field_index,
469 uint32_t dex_pc,
470 const FieldAccessCallingConvention& calling_convention) {
471 LocationSummary* locations = field_access->GetLocations();
472
473 MoveConstant(locations->GetTemp(0), field_index);
474
475 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
476 || field_access->IsUnresolvedInstanceFieldSet();
477 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
478 || field_access->IsUnresolvedStaticFieldGet();
479
480 if (!is_get && Primitive::IsFloatingPointType(field_type)) {
481 // Copy the float value to be set into the calling convention register.
482 // Note that using directly the temp location is problematic as we don't
483 // support temp register pairs. To avoid boilerplate conversion code, use
484 // the location from the calling convention.
485 MoveLocation(calling_convention.GetSetValueLocation(field_type, is_instance),
486 locations->InAt(is_instance ? 1 : 0),
487 (Primitive::Is64BitType(field_type) ? Primitive::kPrimLong : Primitive::kPrimInt));
488 }
489
490 QuickEntrypointEnum entrypoint = kQuickSet8Static; // Initialize to anything to avoid warnings.
491 switch (field_type) {
492 case Primitive::kPrimBoolean:
493 entrypoint = is_instance
494 ? (is_get ? kQuickGetBooleanInstance : kQuickSet8Instance)
495 : (is_get ? kQuickGetBooleanStatic : kQuickSet8Static);
496 break;
497 case Primitive::kPrimByte:
498 entrypoint = is_instance
499 ? (is_get ? kQuickGetByteInstance : kQuickSet8Instance)
500 : (is_get ? kQuickGetByteStatic : kQuickSet8Static);
501 break;
502 case Primitive::kPrimShort:
503 entrypoint = is_instance
504 ? (is_get ? kQuickGetShortInstance : kQuickSet16Instance)
505 : (is_get ? kQuickGetShortStatic : kQuickSet16Static);
506 break;
507 case Primitive::kPrimChar:
508 entrypoint = is_instance
509 ? (is_get ? kQuickGetCharInstance : kQuickSet16Instance)
510 : (is_get ? kQuickGetCharStatic : kQuickSet16Static);
511 break;
512 case Primitive::kPrimInt:
513 case Primitive::kPrimFloat:
514 entrypoint = is_instance
515 ? (is_get ? kQuickGet32Instance : kQuickSet32Instance)
516 : (is_get ? kQuickGet32Static : kQuickSet32Static);
517 break;
518 case Primitive::kPrimNot:
519 entrypoint = is_instance
520 ? (is_get ? kQuickGetObjInstance : kQuickSetObjInstance)
521 : (is_get ? kQuickGetObjStatic : kQuickSetObjStatic);
522 break;
523 case Primitive::kPrimLong:
524 case Primitive::kPrimDouble:
525 entrypoint = is_instance
526 ? (is_get ? kQuickGet64Instance : kQuickSet64Instance)
527 : (is_get ? kQuickGet64Static : kQuickSet64Static);
528 break;
529 default:
530 LOG(FATAL) << "Invalid type " << field_type;
531 }
532 InvokeRuntime(entrypoint, field_access, dex_pc, nullptr);
533
534 if (is_get && Primitive::IsFloatingPointType(field_type)) {
535 MoveLocation(locations->Out(), calling_convention.GetReturnLocation(field_type), field_type);
536 }
537}
538
Calin Juravle98893e12015-10-02 21:05:03 +0100539void CodeGenerator::CreateLoadClassLocationSummary(HLoadClass* cls,
540 Location runtime_type_index_location,
541 Location runtime_return_location) {
542 ArenaAllocator* allocator = cls->GetBlock()->GetGraph()->GetArena();
543 LocationSummary::CallKind call_kind = cls->NeedsAccessCheck()
544 ? LocationSummary::kCall
545 : (cls->CanCallRuntime()
546 ? LocationSummary::kCallOnSlowPath
547 : LocationSummary::kNoCall);
548 LocationSummary* locations = new (allocator) LocationSummary(cls, call_kind);
Calin Juravle98893e12015-10-02 21:05:03 +0100549 if (cls->NeedsAccessCheck()) {
Calin Juravle580b6092015-10-06 17:35:58 +0100550 locations->SetInAt(0, Location::NoLocation());
Calin Juravle98893e12015-10-02 21:05:03 +0100551 locations->AddTemp(runtime_type_index_location);
552 locations->SetOut(runtime_return_location);
553 } else {
Calin Juravle580b6092015-10-06 17:35:58 +0100554 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle98893e12015-10-02 21:05:03 +0100555 locations->SetOut(Location::RequiresRegister());
556 }
557}
558
559
Mark Mendell5f874182015-03-04 15:42:45 -0500560void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
561 // The DCHECKS below check that a register is not specified twice in
562 // the summary. The out location can overlap with an input, so we need
563 // to special case it.
564 if (location.IsRegister()) {
565 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
566 blocked_core_registers_[location.reg()] = true;
567 } else if (location.IsFpuRegister()) {
568 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
569 blocked_fpu_registers_[location.reg()] = true;
570 } else if (location.IsFpuRegisterPair()) {
571 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
572 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
573 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
574 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
575 } else if (location.IsRegisterPair()) {
576 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
577 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
578 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
579 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
580 }
581}
582
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100583void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
584 LocationSummary* locations = instruction->GetLocations();
585 if (locations == nullptr) return;
586
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100587 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
588 blocked_core_registers_[i] = false;
589 }
590
591 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
592 blocked_fpu_registers_[i] = false;
593 }
594
595 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
596 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100597 }
598
599 // Mark all fixed input, temp and output registers as used.
600 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500601 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100602 }
603
604 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
605 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500606 BlockIfInRegister(loc);
607 }
608 Location result_location = locations->Out();
609 if (locations->OutputCanOverlapWithInputs()) {
610 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100611 }
612
Mark Mendell5f874182015-03-04 15:42:45 -0500613 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100614
615 // Allocate all unallocated input locations.
616 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
617 Location loc = locations->InAt(i);
618 HInstruction* input = instruction->InputAt(i);
619 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100620 if ((loc.GetPolicy() == Location::kRequiresRegister)
621 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100622 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100623 } else {
624 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
625 HLoadLocal* load = input->AsLoadLocal();
626 if (load != nullptr) {
627 loc = GetStackLocation(load);
628 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100629 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100630 }
631 }
632 locations->SetInAt(i, loc);
633 }
634 }
635
636 // Allocate all unallocated temp locations.
637 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
638 Location loc = locations->GetTemp(i);
639 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000640 switch (loc.GetPolicy()) {
641 case Location::kRequiresRegister:
642 // Allocate a core register (large enough to fit a 32-bit integer).
643 loc = AllocateFreeRegister(Primitive::kPrimInt);
644 break;
645
646 case Location::kRequiresFpuRegister:
647 // Allocate a core register (large enough to fit a 64-bit double).
648 loc = AllocateFreeRegister(Primitive::kPrimDouble);
649 break;
650
651 default:
652 LOG(FATAL) << "Unexpected policy for temporary location "
653 << loc.GetPolicy();
654 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100655 locations->SetTempAt(i, loc);
656 }
657 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100658 if (result_location.IsUnallocated()) {
659 switch (result_location.GetPolicy()) {
660 case Location::kAny:
661 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100662 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100663 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100664 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100665 case Location::kSameAsFirstInput:
666 result_location = locations->InAt(0);
667 break;
668 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000669 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100670 }
671}
672
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000673void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
674 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100675 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100676 if (instruction->IsTemporary()) {
677 HInstruction* previous = instruction->GetPrevious();
678 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
679 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100680 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100681 return;
682 }
683 AllocateRegistersLocally(instruction);
684 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000685 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000686 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000687 if (location.IsValid()) {
688 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000689 if (input->GetNext()->IsTemporary()) {
690 // If the input was stored in a temporary, use that temporary to
691 // perform the move.
692 Move(input->GetNext(), location, instruction);
693 } else {
694 Move(input, location, instruction);
695 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000696 }
697 }
698}
699
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000700void CodeGenerator::AllocateLocations(HInstruction* instruction) {
701 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100702 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000703 LocationSummary* locations = instruction->GetLocations();
704 if (!instruction->IsSuspendCheckEntry()) {
705 if (locations != nullptr && locations->CanCall()) {
706 MarkNotLeaf();
707 }
708 if (instruction->NeedsCurrentMethod()) {
709 SetRequiresCurrentMethod();
710 }
711 }
712}
713
Serban Constantinescuecc43662015-08-13 13:33:12 +0100714void CodeGenerator::MaybeRecordStat(MethodCompilationStat compilation_stat, size_t count) const {
715 if (stats_ != nullptr) {
716 stats_->RecordStat(compilation_stat, count);
717 }
718}
719
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000720CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000721 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000722 const InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100723 const CompilerOptions& compiler_options,
724 OptimizingCompilerStats* stats) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000725 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -0700726#ifdef ART_ENABLE_CODEGEN_arm
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000727 case kArm:
728 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000729 return new arm::CodeGeneratorARM(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100730 *isa_features.AsArmInstructionSetFeatures(),
731 compiler_options,
732 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000733 }
Alex Light50fa9932015-08-10 15:30:07 -0700734#endif
735#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames5319def2014-10-23 10:03:10 +0100736 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000737 return new arm64::CodeGeneratorARM64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100738 *isa_features.AsArm64InstructionSetFeatures(),
739 compiler_options,
740 stats);
Alexandre Rames5319def2014-10-23 10:03:10 +0100741 }
Alex Light50fa9932015-08-10 15:30:07 -0700742#endif
743#ifdef ART_ENABLE_CODEGEN_mips
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000744 case kMips:
Alex Light50fa9932015-08-10 15:30:07 -0700745 UNUSED(compiler_options);
746 UNUSED(graph);
747 UNUSED(isa_features);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000748 return nullptr;
Alex Light50fa9932015-08-10 15:30:07 -0700749#endif
750#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700751 case kMips64: {
752 return new mips64::CodeGeneratorMIPS64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100753 *isa_features.AsMips64InstructionSetFeatures(),
754 compiler_options,
755 stats);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700756 }
Alex Light50fa9932015-08-10 15:30:07 -0700757#endif
758#ifdef ART_ENABLE_CODEGEN_x86
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000759 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400760 return new x86::CodeGeneratorX86(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100761 *isa_features.AsX86InstructionSetFeatures(),
762 compiler_options,
763 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000764 }
Alex Light50fa9932015-08-10 15:30:07 -0700765#endif
766#ifdef ART_ENABLE_CODEGEN_x86_64
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700767 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400768 return new x86_64::CodeGeneratorX86_64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100769 *isa_features.AsX86_64InstructionSetFeatures(),
770 compiler_options,
771 stats);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700772 }
Alex Light50fa9932015-08-10 15:30:07 -0700773#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000774 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000775 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000776 }
777}
778
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000779void CodeGenerator::BuildNativeGCMap(
Vladimir Markof9f64412015-09-02 14:05:49 +0100780 ArenaVector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000781 const std::vector<uint8_t>& gc_map_raw =
782 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
783 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
784
Vladimir Markobd8c7252015-06-12 10:06:32 +0100785 uint32_t max_native_offset = stack_map_stream_.ComputeMaxNativePcOffset();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000786
Vladimir Markobd8c7252015-06-12 10:06:32 +0100787 size_t num_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
788 GcMapBuilder builder(data, num_stack_maps, max_native_offset, dex_gc_map.RegWidth());
789 for (size_t i = 0; i != num_stack_maps; ++i) {
790 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
791 uint32_t native_offset = stack_map_entry.native_pc_offset;
792 uint32_t dex_pc = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000793 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800794 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000795 builder.AddEntry(native_offset, references);
796 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000797}
798
Vladimir Markof9f64412015-09-02 14:05:49 +0100799void CodeGenerator::BuildMappingTable(ArenaVector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000800 uint32_t pc2dex_data_size = 0u;
Vladimir Markobd8c7252015-06-12 10:06:32 +0100801 uint32_t pc2dex_entries = stack_map_stream_.GetNumberOfStackMaps();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000802 uint32_t pc2dex_offset = 0u;
803 int32_t pc2dex_dalvik_offset = 0;
804 uint32_t dex2pc_data_size = 0u;
805 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000806 uint32_t dex2pc_offset = 0u;
807 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000808
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000809 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100810 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
811 pc2dex_data_size += UnsignedLeb128Size(stack_map_entry.native_pc_offset - pc2dex_offset);
812 pc2dex_data_size += SignedLeb128Size(stack_map_entry.dex_pc - pc2dex_dalvik_offset);
813 pc2dex_offset = stack_map_entry.native_pc_offset;
814 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000815 }
816
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000817 // Walk over the blocks and find which ones correspond to catch block entries.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100818 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000819 if (block->IsCatchBlock()) {
820 intptr_t native_pc = GetAddressOf(block);
821 ++dex2pc_entries;
822 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
823 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
824 dex2pc_offset = native_pc;
825 dex2pc_dalvik_offset = block->GetDexPc();
826 }
827 }
828
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000829 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
830 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
831 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
832 data->resize(data_size);
833
834 uint8_t* data_ptr = &(*data)[0];
835 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000836
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000837 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
838 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
839 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
840 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
841
842 pc2dex_offset = 0u;
843 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000844 dex2pc_offset = 0u;
845 dex2pc_dalvik_offset = 0u;
846
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000847 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100848 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
849 DCHECK(pc2dex_offset <= stack_map_entry.native_pc_offset);
850 write_pos = EncodeUnsignedLeb128(write_pos, stack_map_entry.native_pc_offset - pc2dex_offset);
851 write_pos = EncodeSignedLeb128(write_pos, stack_map_entry.dex_pc - pc2dex_dalvik_offset);
852 pc2dex_offset = stack_map_entry.native_pc_offset;
853 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000854 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000855
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100856 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000857 if (block->IsCatchBlock()) {
858 intptr_t native_pc = GetAddressOf(block);
859 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
860 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
861 dex2pc_offset = native_pc;
862 dex2pc_dalvik_offset = block->GetDexPc();
863 }
864 }
865
866
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000867 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
868 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
869
870 if (kIsDebugBuild) {
871 // Verify the encoded table holds the expected data.
872 MappingTable table(data_ptr);
873 CHECK_EQ(table.TotalSize(), total_entries);
874 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
875 auto it = table.PcToDexBegin();
876 auto it2 = table.DexToPcBegin();
877 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100878 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
879 CHECK_EQ(stack_map_entry.native_pc_offset, it.NativePcOffset());
880 CHECK_EQ(stack_map_entry.dex_pc, it.DexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000881 ++it;
882 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100883 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000884 if (block->IsCatchBlock()) {
885 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
886 CHECK_EQ(block->GetDexPc(), it2.DexPc());
887 ++it2;
888 }
889 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000890 CHECK(it == table.PcToDexEnd());
891 CHECK(it2 == table.DexToPcEnd());
892 }
893}
894
Vladimir Markof9f64412015-09-02 14:05:49 +0100895void CodeGenerator::BuildVMapTable(ArenaVector<uint8_t>* data) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100896 Leb128Encoder<ArenaVector<uint8_t>> vmap_encoder(data);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100897 // We currently don't use callee-saved registers.
898 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000899 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
900 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000901 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000902}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000903
Vladimir Markof9f64412015-09-02 14:05:49 +0100904void CodeGenerator::BuildStackMaps(ArenaVector<uint8_t>* data) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100905 uint32_t size = stack_map_stream_.PrepareForFillIn();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100906 data->resize(size);
907 MemoryRegion region(data->data(), size);
908 stack_map_stream_.FillIn(region);
909}
910
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600911void CodeGenerator::RecordNativeDebugInfo(uint32_t dex_pc,
912 uintptr_t native_pc_begin,
913 uintptr_t native_pc_end) {
914 if (src_map_ != nullptr && dex_pc != kNoDexPc && native_pc_begin != native_pc_end) {
915 src_map_->push_back(SrcMapElem({static_cast<uint32_t>(native_pc_begin),
916 static_cast<int32_t>(dex_pc)}));
917 }
918}
919
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000920void CodeGenerator::RecordPcInfo(HInstruction* instruction,
921 uint32_t dex_pc,
922 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000923 if (instruction != nullptr) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700924 // The code generated for some type conversions and comparisons
925 // may call the runtime, thus normally requiring a subsequent
926 // call to this method. However, the method verifier does not
927 // produce PC information for certain instructions, which are
928 // considered "atomic" (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000929 // Therefore we do not currently record PC information for such
930 // instructions. As this may change later, we added this special
931 // case so that code generators may nevertheless call
932 // CodeGenerator::RecordPcInfo without triggering an error in
933 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
934 // thereafter.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700935 if (instruction->IsTypeConversion() || instruction->IsCompare()) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000936 return;
937 }
938 if (instruction->IsRem()) {
939 Primitive::Type type = instruction->AsRem()->GetResultType();
940 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
941 return;
942 }
943 }
Roland Levillain624279f2014-12-04 11:54:28 +0000944 }
945
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100946 uint32_t outer_dex_pc = dex_pc;
947 uint32_t outer_environment_size = 0;
948 uint32_t inlining_depth = 0;
949 if (instruction != nullptr) {
950 for (HEnvironment* environment = instruction->GetEnvironment();
951 environment != nullptr;
952 environment = environment->GetParent()) {
953 outer_dex_pc = environment->GetDexPc();
954 outer_environment_size = environment->Size();
955 if (environment != instruction->GetEnvironment()) {
956 inlining_depth++;
957 }
958 }
959 }
960
Nicolas Geoffray39468442014-09-02 15:17:15 +0100961 // Collect PC infos for the mapping table.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100962 uint32_t native_pc = GetAssembler()->CodeSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100963
Nicolas Geoffray39468442014-09-02 15:17:15 +0100964 if (instruction == nullptr) {
965 // For stack overflow checks.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100966 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100967 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000968 return;
969 }
970 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100971
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000972 uint32_t register_mask = locations->GetRegisterMask();
973 if (locations->OnlyCallsOnSlowPath()) {
974 // In case of slow path, we currently set the location of caller-save registers
975 // to register (instead of their stack location when pushed before the slow-path
976 // call). Therefore register_mask contains both callee-save and caller-save
977 // registers that hold objects. We must remove the caller-save from the mask, since
978 // they will be overwritten by the callee.
979 register_mask &= core_callee_save_mask_;
980 }
981 // The register mask must be a subset of callee-save registers.
982 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100983 stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
984 native_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100985 register_mask,
986 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100987 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100988 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100989
990 EmitEnvironment(instruction->GetEnvironment(), slow_path);
991 stack_map_stream_.EndStackMapEntry();
992}
993
David Brazdil77a48ae2015-09-15 12:34:04 +0000994void CodeGenerator::RecordCatchBlockInfo() {
995 ArenaAllocator* arena = graph_->GetArena();
996
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100997 for (HBasicBlock* block : *block_order_) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000998 if (!block->IsCatchBlock()) {
999 continue;
1000 }
1001
1002 uint32_t dex_pc = block->GetDexPc();
1003 uint32_t num_vregs = graph_->GetNumberOfVRegs();
1004 uint32_t inlining_depth = 0; // Inlining of catch blocks is not supported at the moment.
1005 uint32_t native_pc = GetAddressOf(block);
1006 uint32_t register_mask = 0; // Not used.
1007
1008 // The stack mask is not used, so we leave it empty.
1009 ArenaBitVector* stack_mask = new (arena) ArenaBitVector(arena, 0, /* expandable */ true);
1010
1011 stack_map_stream_.BeginStackMapEntry(dex_pc,
1012 native_pc,
1013 register_mask,
1014 stack_mask,
1015 num_vregs,
1016 inlining_depth);
1017
1018 HInstruction* current_phi = block->GetFirstPhi();
1019 for (size_t vreg = 0; vreg < num_vregs; ++vreg) {
1020 while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) {
1021 HInstruction* next_phi = current_phi->GetNext();
1022 DCHECK(next_phi == nullptr ||
1023 current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber())
1024 << "Phis need to be sorted by vreg number to keep this a linear-time loop.";
1025 current_phi = next_phi;
1026 }
1027
1028 if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) {
1029 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
1030 } else {
1031 Location location = current_phi->GetLiveInterval()->ToLocation();
1032 switch (location.GetKind()) {
1033 case Location::kStackSlot: {
1034 stack_map_stream_.AddDexRegisterEntry(
1035 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
1036 break;
1037 }
1038 case Location::kDoubleStackSlot: {
1039 stack_map_stream_.AddDexRegisterEntry(
1040 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
1041 stack_map_stream_.AddDexRegisterEntry(
1042 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
1043 ++vreg;
1044 DCHECK_LT(vreg, num_vregs);
1045 break;
1046 }
1047 default: {
1048 // All catch phis must be allocated to a stack slot.
1049 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1050 UNREACHABLE();
1051 }
1052 }
1053 }
1054 }
1055
1056 stack_map_stream_.EndStackMapEntry();
1057 }
1058}
1059
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001060void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
1061 if (environment == nullptr) return;
1062
1063 if (environment->GetParent() != nullptr) {
1064 // We emit the parent environment first.
1065 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001066 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
1067 environment->GetDexPc(),
1068 environment->GetInvokeType(),
1069 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001070 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001071
1072 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001073 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001074 HInstruction* current = environment->GetInstructionAt(i);
1075 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001076 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001077 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001078 }
1079
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001080 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001081 switch (location.GetKind()) {
1082 case Location::kConstant: {
1083 DCHECK_EQ(current, location.GetConstant());
1084 if (current->IsLongConstant()) {
1085 int64_t value = current->AsLongConstant()->GetValue();
1086 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001087 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001088 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001089 DexRegisterLocation::Kind::kConstant, High32Bits(value));
1090 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001091 DCHECK_LT(i, environment_size);
1092 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001093 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001094 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001095 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001096 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001097 DexRegisterLocation::Kind::kConstant, High32Bits(value));
1098 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001099 DCHECK_LT(i, environment_size);
1100 } else if (current->IsIntConstant()) {
1101 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001102 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001103 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001104 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001105 } else {
1106 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +00001107 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->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 }
1110 break;
1111 }
1112
1113 case Location::kStackSlot: {
1114 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001115 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001116 break;
1117 }
1118
1119 case Location::kDoubleStackSlot: {
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 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001123 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
1124 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001125 DCHECK_LT(i, environment_size);
1126 break;
1127 }
1128
1129 case Location::kRegister : {
1130 int id = location.reg();
1131 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
1132 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001133 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001134 if (current->GetType() == Primitive::kPrimLong) {
1135 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001136 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
1137 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001138 DCHECK_LT(i, environment_size);
1139 }
1140 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001141 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001142 if (current->GetType() == Primitive::kPrimLong) {
David Brazdild9cb68e2015-08-25 13:52:43 +01001143 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001144 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001145 DCHECK_LT(i, environment_size);
1146 }
1147 }
1148 break;
1149 }
1150
1151 case Location::kFpuRegister : {
1152 int id = location.reg();
1153 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
1154 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001155 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001156 if (current->GetType() == Primitive::kPrimDouble) {
1157 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001158 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
1159 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001160 DCHECK_LT(i, environment_size);
1161 }
1162 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001163 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001164 if (current->GetType() == Primitive::kPrimDouble) {
David Brazdild9cb68e2015-08-25 13:52:43 +01001165 stack_map_stream_.AddDexRegisterEntry(
1166 DexRegisterLocation::Kind::kInFpuRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001167 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001168 DCHECK_LT(i, environment_size);
1169 }
1170 }
1171 break;
1172 }
1173
1174 case Location::kFpuRegisterPair : {
1175 int low = location.low();
1176 int high = location.high();
1177 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
1178 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001179 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001180 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001181 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001182 }
1183 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
1184 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001185 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1186 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001187 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001188 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
1189 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001190 }
1191 DCHECK_LT(i, environment_size);
1192 break;
1193 }
1194
1195 case Location::kRegisterPair : {
1196 int low = location.low();
1197 int high = location.high();
1198 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
1199 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001200 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001201 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001202 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001203 }
1204 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
1205 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
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, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001209 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001210 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001211 DCHECK_LT(i, environment_size);
1212 break;
1213 }
1214
1215 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001216 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001217 break;
1218 }
1219
1220 default:
1221 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1222 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001223 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001224
1225 if (environment->GetParent() != nullptr) {
1226 stack_map_stream_.EndInlineInfoEntry();
1227 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001228}
1229
David Brazdil77a48ae2015-09-15 12:34:04 +00001230bool CodeGenerator::IsImplicitNullCheckAllowed(HNullCheck* null_check) const {
1231 return compiler_options_.GetImplicitNullChecks() &&
1232 // Null checks which might throw into a catch block need to save live
1233 // registers and therefore cannot be done implicitly.
1234 !null_check->CanThrowIntoCatchBlock();
1235}
1236
Calin Juravle77520bc2015-01-12 18:45:46 +00001237bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
1238 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +01001239
1240 return (first_next_not_move != nullptr)
1241 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00001242}
1243
1244void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
1245 // If we are from a static path don't record the pc as we can't throw NPE.
1246 // NB: having the checks here makes the code much less verbose in the arch
1247 // specific code generators.
1248 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
1249 return;
1250 }
1251
Calin Juravle641547a2015-04-21 22:08:51 +01001252 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001253 return;
1254 }
1255
1256 // Find the first previous instruction which is not a move.
1257 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
1258
1259 // If the instruction is a null check it means that `instr` is the first user
1260 // and needs to record the pc.
1261 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
1262 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
David Brazdil77a48ae2015-09-15 12:34:04 +00001263 if (IsImplicitNullCheckAllowed(null_check)) {
1264 // TODO: The parallel moves modify the environment. Their changes need to be
1265 // reverted otherwise the stack maps at the throw point will not be correct.
1266 RecordPcInfo(null_check, null_check->GetDexPc());
1267 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001268 }
1269}
1270
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001271void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
1272 LocationSummary* locations = suspend_check->GetLocations();
1273 HBasicBlock* block = suspend_check->GetBlock();
1274 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
1275 DCHECK(block->IsLoopHeader());
1276
1277 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1278 HInstruction* current = it.Current();
1279 LiveInterval* interval = current->GetLiveInterval();
1280 // We only need to clear bits of loop phis containing objects and allocated in register.
1281 // Loop phis allocated on stack already have the object in the stack.
1282 if (current->GetType() == Primitive::kPrimNot
1283 && interval->HasRegister()
1284 && interval->HasSpillSlot()) {
1285 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
1286 }
1287 }
1288}
1289
Nicolas Geoffray90218252015-04-15 11:56:51 +01001290void CodeGenerator::EmitParallelMoves(Location from1,
1291 Location to1,
1292 Primitive::Type type1,
1293 Location from2,
1294 Location to2,
1295 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001296 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +01001297 parallel_move.AddMove(from1, to1, type1, nullptr);
1298 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001299 GetMoveResolver()->EmitNativeCode(&parallel_move);
1300}
1301
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001302void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) {
1303 // Ensure that the call kind indication given to the register allocator is
1304 // coherent with the runtime call generated, and that the GC side effect is
1305 // set when required.
1306 if (slow_path == nullptr) {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001307 DCHECK(instruction->GetLocations()->WillCall()) << instruction->DebugName();
1308 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()))
1309 << instruction->DebugName() << instruction->GetSideEffects().ToString();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001310 } else {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001311 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal())
1312 << instruction->DebugName() << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001313 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) ||
1314 // Control flow would not come back into the code if a fatal slow
1315 // path is taken, so we do not care if it triggers GC.
1316 slow_path->IsFatal() ||
1317 // HDeoptimize is a special case: we know we are not coming back from
1318 // it into the code.
Roland Levillaindf3f8222015-08-13 12:31:44 +01001319 instruction->IsDeoptimize())
1320 << instruction->DebugName() << instruction->GetSideEffects().ToString()
1321 << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001322 }
1323
1324 // Check the coherency of leaf information.
1325 DCHECK(instruction->IsSuspendCheck()
1326 || ((slow_path != nullptr) && slow_path->IsFatal())
1327 || instruction->GetLocations()->CanCall()
Roland Levillaindf3f8222015-08-13 12:31:44 +01001328 || !IsLeafMethod())
1329 << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : "");
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001330}
1331
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001332void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1333 RegisterSet* register_set = locations->GetLiveRegisters();
1334 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1335 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1336 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1337 if (register_set->ContainsCoreRegister(i)) {
1338 // If the register holds an object, update the stack mask.
1339 if (locations->RegisterContainsObject(i)) {
1340 locations->SetStackBit(stack_offset / kVRegSize);
1341 }
1342 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001343 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1344 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001345 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
1346 }
1347 }
1348 }
1349
1350 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1351 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1352 if (register_set->ContainsFloatingPointRegister(i)) {
1353 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001354 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1355 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001356 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
1357 }
1358 }
1359 }
1360}
1361
1362void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1363 RegisterSet* register_set = locations->GetLiveRegisters();
1364 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1365 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1366 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1367 if (register_set->ContainsCoreRegister(i)) {
1368 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1369 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1370 }
1371 }
1372 }
1373
1374 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1375 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1376 if (register_set->ContainsFloatingPointRegister(i)) {
1377 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1378 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1379 }
1380 }
1381 }
1382}
1383
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001384} // namespace art