blob: 1da2a07462c8711d93f978829d6c1669858295ba [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_LT(current_block_index_, block_order_->size());
159 DCHECK_EQ((*block_order_)[current_block_index_], current);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000160 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
161}
162
163HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100164 for (size_t i = current_block_index_ + 1; i < block_order_->size(); ++i) {
165 HBasicBlock* block = (*block_order_)[i];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000166 if (!block->IsSingleJump()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000167 return block;
168 }
169 }
170 return nullptr;
171}
172
173HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000174 while (block->IsSingleJump()) {
Vladimir Marko60584552015-09-03 13:35:12 +0000175 block = block->GetSuccessor(0);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000176 }
177 return block;
178}
179
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100180class DisassemblyScope {
181 public:
182 DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen)
183 : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) {
184 if (codegen_.GetDisassemblyInformation() != nullptr) {
185 start_offset_ = codegen_.GetAssembler().CodeSize();
186 }
187 }
188
189 ~DisassemblyScope() {
190 // We avoid building this data when we know it will not be used.
191 if (codegen_.GetDisassemblyInformation() != nullptr) {
192 codegen_.GetDisassemblyInformation()->AddInstructionInterval(
193 instruction_, start_offset_, codegen_.GetAssembler().CodeSize());
194 }
195 }
196
197 private:
198 const CodeGenerator& codegen_;
199 HInstruction* instruction_;
200 size_t start_offset_;
201};
202
203
204void CodeGenerator::GenerateSlowPaths() {
205 size_t code_start = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100206 for (SlowPathCode* slow_path : slow_paths_) {
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100207 if (disasm_info_ != nullptr) {
208 code_start = GetAssembler()->CodeSize();
209 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100210 slow_path->EmitNativeCode(this);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100211 if (disasm_info_ != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100212 disasm_info_->AddSlowPathInterval(slow_path, code_start, GetAssembler()->CodeSize());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100213 }
214 }
215}
216
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000217void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Roland Levillain3e3d7332015-04-28 11:00:54 +0100218 is_baseline_ = is_baseline;
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100219 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000220 DCHECK_EQ(current_block_index_, 0u);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100221
222 size_t frame_start = GetAssembler()->CodeSize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000223 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100224 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100225 if (disasm_info_ != nullptr) {
226 disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize());
227 }
228
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100229 for (size_t e = block_order_->size(); current_block_index_ < e; ++current_block_index_) {
230 HBasicBlock* block = (*block_order_)[current_block_index_];
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000231 // Don't generate code for an empty block. Its predecessors will branch to its successor
232 // directly. Also, the label of that block will not be emitted, so this helps catch
233 // errors where we reference that label.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000234 if (block->IsSingleJump()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100235 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100236 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
237 HInstruction* current = it.Current();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100238 DisassemblyScope disassembly_scope(current, *this);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000239 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000240 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000241 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100242 DCHECK(CheckTypeConsistency(current));
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600243 uintptr_t native_pc_begin = GetAssembler()->CodeSize();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100244 current->Accept(instruction_visitor);
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600245 uintptr_t native_pc_end = GetAssembler()->CodeSize();
246 RecordNativeDebugInfo(current->GetDexPc(), native_pc_begin, native_pc_end);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100247 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000248 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000249
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100250 GenerateSlowPaths();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000251
David Brazdil77a48ae2015-09-15 12:34:04 +0000252 // Emit catch stack maps at the end of the stack map stream as expected by the
253 // runtime exception handler.
254 if (!is_baseline && graph_->HasTryCatch()) {
255 RecordCatchBlockInfo();
256 }
257
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000258 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000259 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000260}
261
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100262void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000263 // The register allocator already called `InitializeCodeGeneration`,
264 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000265 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100266 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000267 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000268}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100269
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000270void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100271 size_t code_size = GetAssembler()->CodeSize();
272 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000273
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100274 MemoryRegion code(buffer, code_size);
275 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000276}
277
Vladimir Marko58155012015-08-19 12:49:41 +0000278void CodeGenerator::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches ATTRIBUTE_UNUSED) {
279 // No linker patches by default.
280}
281
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100282size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
283 for (size_t i = 0; i < length; ++i) {
284 if (!array[i]) {
285 array[i] = true;
286 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100287 }
288 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100289 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000290 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000291}
292
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000293size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
294 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000295 if (!array[i] && !array[i + 1]) {
296 array[i] = true;
297 array[i + 1] = true;
298 return i;
299 }
300 }
301 LOG(FATAL) << "Could not find a register in baseline register allocator";
302 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100303}
304
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000305void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
306 size_t maximum_number_of_live_core_registers,
307 size_t maximum_number_of_live_fp_registers,
308 size_t number_of_out_slots,
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100309 const ArenaVector<HBasicBlock*>& block_order) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000310 block_order_ = &block_order;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100311 DCHECK(!block_order.empty());
312 DCHECK(block_order[0] == GetGraph()->GetEntryBlock());
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000313 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100314 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
315
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000316 if (number_of_spill_slots == 0
317 && !HasAllocatedCalleeSaveRegisters()
318 && IsLeafMethod()
319 && !RequiresCurrentMethod()) {
320 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
321 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
322 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
323 } else {
324 SetFrameSize(RoundUp(
325 number_of_spill_slots * kVRegSize
326 + number_of_out_slots * kVRegSize
327 + maximum_number_of_live_core_registers * GetWordSize()
328 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
329 + FrameEntrySpillSize(),
330 kStackAlignment));
331 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100332}
333
334Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
335 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000336 // The type of the previous instruction tells us if we need a single or double stack slot.
337 Primitive::Type type = temp->GetType();
338 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100339 // Use the temporary region (right below the dex registers).
340 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
341 - kVRegSize // filler
342 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000343 - ((temp_size + temp->GetIndex()) * kVRegSize);
344 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100345}
346
347int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
348 uint16_t reg_number = local->GetRegNumber();
349 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
350 if (reg_number >= number_of_locals) {
351 // Local is a parameter of the method. It is stored in the caller's frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352 // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode.
353 return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100354 + (reg_number - number_of_locals) * kVRegSize;
355 } else {
356 // Local is a temporary in this method. It is stored in this method's frame.
357 return GetFrameSize() - FrameEntrySpillSize()
358 - kVRegSize // filler.
359 - (number_of_locals * kVRegSize)
360 + (reg_number * kVRegSize);
361 }
362}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100363
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100364void CodeGenerator::CreateCommonInvokeLocationSummary(
Nicolas Geoffray4e40c262015-06-03 12:02:38 +0100365 HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100366 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
367 LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100368
369 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
370 HInstruction* input = invoke->InputAt(i);
371 locations->SetInAt(i, visitor->GetNextLocation(input->GetType()));
372 }
373
374 locations->SetOut(visitor->GetReturnLocation(invoke->GetType()));
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100375
376 if (invoke->IsInvokeStaticOrDirect()) {
377 HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect();
378 if (call->IsStringInit()) {
379 locations->AddTemp(visitor->GetMethodLocation());
380 } else if (call->IsRecursive()) {
381 locations->SetInAt(call->GetCurrentMethodInputIndex(), visitor->GetMethodLocation());
382 } else {
383 locations->AddTemp(visitor->GetMethodLocation());
384 locations->SetInAt(call->GetCurrentMethodInputIndex(), Location::RequiresRegister());
385 }
386 } else {
387 locations->AddTemp(visitor->GetMethodLocation());
388 }
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100389}
390
Calin Juravle175dc732015-08-25 15:42:32 +0100391void CodeGenerator::GenerateInvokeUnresolvedRuntimeCall(HInvokeUnresolved* invoke) {
392 MoveConstant(invoke->GetLocations()->GetTemp(0), invoke->GetDexMethodIndex());
393
394 // Initialize to anything to silent compiler warnings.
395 QuickEntrypointEnum entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
396 switch (invoke->GetOriginalInvokeType()) {
397 case kStatic:
398 entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
399 break;
400 case kDirect:
401 entrypoint = kQuickInvokeDirectTrampolineWithAccessCheck;
402 break;
403 case kVirtual:
404 entrypoint = kQuickInvokeVirtualTrampolineWithAccessCheck;
405 break;
406 case kSuper:
407 entrypoint = kQuickInvokeSuperTrampolineWithAccessCheck;
408 break;
409 case kInterface:
410 entrypoint = kQuickInvokeInterfaceTrampolineWithAccessCheck;
411 break;
412 }
413 InvokeRuntime(entrypoint, invoke, invoke->GetDexPc(), nullptr);
414}
415
Calin Juravlee460d1d2015-09-29 04:52:17 +0100416void CodeGenerator::CreateUnresolvedFieldLocationSummary(
417 HInstruction* field_access,
418 Primitive::Type field_type,
419 const FieldAccessCallingConvention& calling_convention) {
420 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
421 || field_access->IsUnresolvedInstanceFieldSet();
422 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
423 || field_access->IsUnresolvedStaticFieldGet();
424
425 ArenaAllocator* allocator = field_access->GetBlock()->GetGraph()->GetArena();
426 LocationSummary* locations =
427 new (allocator) LocationSummary(field_access, LocationSummary::kCall);
428
429 locations->AddTemp(calling_convention.GetFieldIndexLocation());
430
431 if (is_instance) {
432 // Add the `this` object for instance field accesses.
433 locations->SetInAt(0, calling_convention.GetObjectLocation());
434 }
435
436 // Note that pSetXXStatic/pGetXXStatic always takes/returns an int or int64
437 // regardless of the the type. Because of that we forced to special case
438 // the access to floating point values.
439 if (is_get) {
440 if (Primitive::IsFloatingPointType(field_type)) {
441 // The return value will be stored in regular registers while register
442 // allocator expects it in a floating point register.
443 // Note We don't need to request additional temps because the return
444 // register(s) are already blocked due the call and they may overlap with
445 // the input or field index.
446 // The transfer between the two will be done at codegen level.
447 locations->SetOut(calling_convention.GetFpuLocation(field_type));
448 } else {
449 locations->SetOut(calling_convention.GetReturnLocation(field_type));
450 }
451 } else {
452 size_t set_index = is_instance ? 1 : 0;
453 if (Primitive::IsFloatingPointType(field_type)) {
454 // The set value comes from a float location while the calling convention
455 // expects it in a regular register location. Allocate a temp for it and
456 // make the transfer at codegen.
457 AddLocationAsTemp(calling_convention.GetSetValueLocation(field_type, is_instance), locations);
458 locations->SetInAt(set_index, calling_convention.GetFpuLocation(field_type));
459 } else {
460 locations->SetInAt(set_index,
461 calling_convention.GetSetValueLocation(field_type, is_instance));
462 }
463 }
464}
465
466void CodeGenerator::GenerateUnresolvedFieldAccess(
467 HInstruction* field_access,
468 Primitive::Type field_type,
469 uint32_t field_index,
470 uint32_t dex_pc,
471 const FieldAccessCallingConvention& calling_convention) {
472 LocationSummary* locations = field_access->GetLocations();
473
474 MoveConstant(locations->GetTemp(0), field_index);
475
476 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
477 || field_access->IsUnresolvedInstanceFieldSet();
478 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
479 || field_access->IsUnresolvedStaticFieldGet();
480
481 if (!is_get && Primitive::IsFloatingPointType(field_type)) {
482 // Copy the float value to be set into the calling convention register.
483 // Note that using directly the temp location is problematic as we don't
484 // support temp register pairs. To avoid boilerplate conversion code, use
485 // the location from the calling convention.
486 MoveLocation(calling_convention.GetSetValueLocation(field_type, is_instance),
487 locations->InAt(is_instance ? 1 : 0),
488 (Primitive::Is64BitType(field_type) ? Primitive::kPrimLong : Primitive::kPrimInt));
489 }
490
491 QuickEntrypointEnum entrypoint = kQuickSet8Static; // Initialize to anything to avoid warnings.
492 switch (field_type) {
493 case Primitive::kPrimBoolean:
494 entrypoint = is_instance
495 ? (is_get ? kQuickGetBooleanInstance : kQuickSet8Instance)
496 : (is_get ? kQuickGetBooleanStatic : kQuickSet8Static);
497 break;
498 case Primitive::kPrimByte:
499 entrypoint = is_instance
500 ? (is_get ? kQuickGetByteInstance : kQuickSet8Instance)
501 : (is_get ? kQuickGetByteStatic : kQuickSet8Static);
502 break;
503 case Primitive::kPrimShort:
504 entrypoint = is_instance
505 ? (is_get ? kQuickGetShortInstance : kQuickSet16Instance)
506 : (is_get ? kQuickGetShortStatic : kQuickSet16Static);
507 break;
508 case Primitive::kPrimChar:
509 entrypoint = is_instance
510 ? (is_get ? kQuickGetCharInstance : kQuickSet16Instance)
511 : (is_get ? kQuickGetCharStatic : kQuickSet16Static);
512 break;
513 case Primitive::kPrimInt:
514 case Primitive::kPrimFloat:
515 entrypoint = is_instance
516 ? (is_get ? kQuickGet32Instance : kQuickSet32Instance)
517 : (is_get ? kQuickGet32Static : kQuickSet32Static);
518 break;
519 case Primitive::kPrimNot:
520 entrypoint = is_instance
521 ? (is_get ? kQuickGetObjInstance : kQuickSetObjInstance)
522 : (is_get ? kQuickGetObjStatic : kQuickSetObjStatic);
523 break;
524 case Primitive::kPrimLong:
525 case Primitive::kPrimDouble:
526 entrypoint = is_instance
527 ? (is_get ? kQuickGet64Instance : kQuickSet64Instance)
528 : (is_get ? kQuickGet64Static : kQuickSet64Static);
529 break;
530 default:
531 LOG(FATAL) << "Invalid type " << field_type;
532 }
533 InvokeRuntime(entrypoint, field_access, dex_pc, nullptr);
534
535 if (is_get && Primitive::IsFloatingPointType(field_type)) {
536 MoveLocation(locations->Out(), calling_convention.GetReturnLocation(field_type), field_type);
537 }
538}
539
Calin Juravle98893e12015-10-02 21:05:03 +0100540void CodeGenerator::CreateLoadClassLocationSummary(HLoadClass* cls,
541 Location runtime_type_index_location,
542 Location runtime_return_location) {
543 ArenaAllocator* allocator = cls->GetBlock()->GetGraph()->GetArena();
544 LocationSummary::CallKind call_kind = cls->NeedsAccessCheck()
545 ? LocationSummary::kCall
546 : (cls->CanCallRuntime()
547 ? LocationSummary::kCallOnSlowPath
548 : LocationSummary::kNoCall);
549 LocationSummary* locations = new (allocator) LocationSummary(cls, call_kind);
Calin Juravle98893e12015-10-02 21:05:03 +0100550 if (cls->NeedsAccessCheck()) {
Calin Juravle580b6092015-10-06 17:35:58 +0100551 locations->SetInAt(0, Location::NoLocation());
Calin Juravle98893e12015-10-02 21:05:03 +0100552 locations->AddTemp(runtime_type_index_location);
553 locations->SetOut(runtime_return_location);
554 } else {
Calin Juravle580b6092015-10-06 17:35:58 +0100555 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle98893e12015-10-02 21:05:03 +0100556 locations->SetOut(Location::RequiresRegister());
557 }
558}
559
560
Mark Mendell5f874182015-03-04 15:42:45 -0500561void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
562 // The DCHECKS below check that a register is not specified twice in
563 // the summary. The out location can overlap with an input, so we need
564 // to special case it.
565 if (location.IsRegister()) {
566 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
567 blocked_core_registers_[location.reg()] = true;
568 } else if (location.IsFpuRegister()) {
569 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
570 blocked_fpu_registers_[location.reg()] = true;
571 } else if (location.IsFpuRegisterPair()) {
572 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
573 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
574 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
575 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
576 } else if (location.IsRegisterPair()) {
577 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
578 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
579 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
580 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
581 }
582}
583
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100584void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
585 LocationSummary* locations = instruction->GetLocations();
586 if (locations == nullptr) return;
587
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100588 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
589 blocked_core_registers_[i] = false;
590 }
591
592 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
593 blocked_fpu_registers_[i] = false;
594 }
595
596 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
597 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100598 }
599
600 // Mark all fixed input, temp and output registers as used.
601 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500602 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100603 }
604
605 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
606 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500607 BlockIfInRegister(loc);
608 }
609 Location result_location = locations->Out();
610 if (locations->OutputCanOverlapWithInputs()) {
611 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100612 }
613
Mark Mendell5f874182015-03-04 15:42:45 -0500614 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100615
616 // Allocate all unallocated input locations.
617 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
618 Location loc = locations->InAt(i);
619 HInstruction* input = instruction->InputAt(i);
620 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100621 if ((loc.GetPolicy() == Location::kRequiresRegister)
622 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100623 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100624 } else {
625 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
626 HLoadLocal* load = input->AsLoadLocal();
627 if (load != nullptr) {
628 loc = GetStackLocation(load);
629 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100630 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100631 }
632 }
633 locations->SetInAt(i, loc);
634 }
635 }
636
637 // Allocate all unallocated temp locations.
638 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
639 Location loc = locations->GetTemp(i);
640 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000641 switch (loc.GetPolicy()) {
642 case Location::kRequiresRegister:
643 // Allocate a core register (large enough to fit a 32-bit integer).
644 loc = AllocateFreeRegister(Primitive::kPrimInt);
645 break;
646
647 case Location::kRequiresFpuRegister:
648 // Allocate a core register (large enough to fit a 64-bit double).
649 loc = AllocateFreeRegister(Primitive::kPrimDouble);
650 break;
651
652 default:
653 LOG(FATAL) << "Unexpected policy for temporary location "
654 << loc.GetPolicy();
655 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100656 locations->SetTempAt(i, loc);
657 }
658 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100659 if (result_location.IsUnallocated()) {
660 switch (result_location.GetPolicy()) {
661 case Location::kAny:
662 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100663 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100664 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100665 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100666 case Location::kSameAsFirstInput:
667 result_location = locations->InAt(0);
668 break;
669 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000670 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100671 }
672}
673
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000674void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
675 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100676 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100677 if (instruction->IsTemporary()) {
678 HInstruction* previous = instruction->GetPrevious();
679 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
680 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100681 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100682 return;
683 }
684 AllocateRegistersLocally(instruction);
685 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000686 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000687 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000688 if (location.IsValid()) {
689 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000690 if (input->GetNext()->IsTemporary()) {
691 // If the input was stored in a temporary, use that temporary to
692 // perform the move.
693 Move(input->GetNext(), location, instruction);
694 } else {
695 Move(input, location, instruction);
696 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000697 }
698 }
699}
700
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000701void CodeGenerator::AllocateLocations(HInstruction* instruction) {
702 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100703 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000704 LocationSummary* locations = instruction->GetLocations();
705 if (!instruction->IsSuspendCheckEntry()) {
706 if (locations != nullptr && locations->CanCall()) {
707 MarkNotLeaf();
708 }
709 if (instruction->NeedsCurrentMethod()) {
710 SetRequiresCurrentMethod();
711 }
712 }
713}
714
Serban Constantinescuecc43662015-08-13 13:33:12 +0100715void CodeGenerator::MaybeRecordStat(MethodCompilationStat compilation_stat, size_t count) const {
716 if (stats_ != nullptr) {
717 stats_->RecordStat(compilation_stat, count);
718 }
719}
720
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000721CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000722 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000723 const InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100724 const CompilerOptions& compiler_options,
725 OptimizingCompilerStats* stats) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000726 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -0700727#ifdef ART_ENABLE_CODEGEN_arm
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000728 case kArm:
729 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000730 return new arm::CodeGeneratorARM(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100731 *isa_features.AsArmInstructionSetFeatures(),
732 compiler_options,
733 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000734 }
Alex Light50fa9932015-08-10 15:30:07 -0700735#endif
736#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames5319def2014-10-23 10:03:10 +0100737 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000738 return new arm64::CodeGeneratorARM64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100739 *isa_features.AsArm64InstructionSetFeatures(),
740 compiler_options,
741 stats);
Alexandre Rames5319def2014-10-23 10:03:10 +0100742 }
Alex Light50fa9932015-08-10 15:30:07 -0700743#endif
744#ifdef ART_ENABLE_CODEGEN_mips
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000745 case kMips:
Alex Light50fa9932015-08-10 15:30:07 -0700746 UNUSED(compiler_options);
747 UNUSED(graph);
748 UNUSED(isa_features);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000749 return nullptr;
Alex Light50fa9932015-08-10 15:30:07 -0700750#endif
751#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700752 case kMips64: {
753 return new mips64::CodeGeneratorMIPS64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100754 *isa_features.AsMips64InstructionSetFeatures(),
755 compiler_options,
756 stats);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700757 }
Alex Light50fa9932015-08-10 15:30:07 -0700758#endif
759#ifdef ART_ENABLE_CODEGEN_x86
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000760 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400761 return new x86::CodeGeneratorX86(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100762 *isa_features.AsX86InstructionSetFeatures(),
763 compiler_options,
764 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000765 }
Alex Light50fa9932015-08-10 15:30:07 -0700766#endif
767#ifdef ART_ENABLE_CODEGEN_x86_64
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700768 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400769 return new x86_64::CodeGeneratorX86_64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100770 *isa_features.AsX86_64InstructionSetFeatures(),
771 compiler_options,
772 stats);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700773 }
Alex Light50fa9932015-08-10 15:30:07 -0700774#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000775 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000776 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000777 }
778}
779
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000780void CodeGenerator::BuildNativeGCMap(
Vladimir Markof9f64412015-09-02 14:05:49 +0100781 ArenaVector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000782 const std::vector<uint8_t>& gc_map_raw =
783 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
784 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
785
Vladimir Markobd8c7252015-06-12 10:06:32 +0100786 uint32_t max_native_offset = stack_map_stream_.ComputeMaxNativePcOffset();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000787
Vladimir Markobd8c7252015-06-12 10:06:32 +0100788 size_t num_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
789 GcMapBuilder builder(data, num_stack_maps, max_native_offset, dex_gc_map.RegWidth());
790 for (size_t i = 0; i != num_stack_maps; ++i) {
791 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
792 uint32_t native_offset = stack_map_entry.native_pc_offset;
793 uint32_t dex_pc = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000794 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800795 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000796 builder.AddEntry(native_offset, references);
797 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000798}
799
Vladimir Markof9f64412015-09-02 14:05:49 +0100800void CodeGenerator::BuildMappingTable(ArenaVector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000801 uint32_t pc2dex_data_size = 0u;
Vladimir Markobd8c7252015-06-12 10:06:32 +0100802 uint32_t pc2dex_entries = stack_map_stream_.GetNumberOfStackMaps();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000803 uint32_t pc2dex_offset = 0u;
804 int32_t pc2dex_dalvik_offset = 0;
805 uint32_t dex2pc_data_size = 0u;
806 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000807 uint32_t dex2pc_offset = 0u;
808 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000809
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000810 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100811 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
812 pc2dex_data_size += UnsignedLeb128Size(stack_map_entry.native_pc_offset - pc2dex_offset);
813 pc2dex_data_size += SignedLeb128Size(stack_map_entry.dex_pc - pc2dex_dalvik_offset);
814 pc2dex_offset = stack_map_entry.native_pc_offset;
815 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000816 }
817
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000818 // Walk over the blocks and find which ones correspond to catch block entries.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100819 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000820 if (block->IsCatchBlock()) {
821 intptr_t native_pc = GetAddressOf(block);
822 ++dex2pc_entries;
823 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
824 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
825 dex2pc_offset = native_pc;
826 dex2pc_dalvik_offset = block->GetDexPc();
827 }
828 }
829
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000830 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
831 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
832 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
833 data->resize(data_size);
834
835 uint8_t* data_ptr = &(*data)[0];
836 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000837
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000838 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
839 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
840 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
841 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
842
843 pc2dex_offset = 0u;
844 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000845 dex2pc_offset = 0u;
846 dex2pc_dalvik_offset = 0u;
847
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000848 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100849 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
850 DCHECK(pc2dex_offset <= stack_map_entry.native_pc_offset);
851 write_pos = EncodeUnsignedLeb128(write_pos, stack_map_entry.native_pc_offset - pc2dex_offset);
852 write_pos = EncodeSignedLeb128(write_pos, stack_map_entry.dex_pc - pc2dex_dalvik_offset);
853 pc2dex_offset = stack_map_entry.native_pc_offset;
854 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000855 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000856
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100857 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000858 if (block->IsCatchBlock()) {
859 intptr_t native_pc = GetAddressOf(block);
860 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
861 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
862 dex2pc_offset = native_pc;
863 dex2pc_dalvik_offset = block->GetDexPc();
864 }
865 }
866
867
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000868 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
869 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
870
871 if (kIsDebugBuild) {
872 // Verify the encoded table holds the expected data.
873 MappingTable table(data_ptr);
874 CHECK_EQ(table.TotalSize(), total_entries);
875 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
876 auto it = table.PcToDexBegin();
877 auto it2 = table.DexToPcBegin();
878 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100879 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
880 CHECK_EQ(stack_map_entry.native_pc_offset, it.NativePcOffset());
881 CHECK_EQ(stack_map_entry.dex_pc, it.DexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000882 ++it;
883 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100884 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000885 if (block->IsCatchBlock()) {
886 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
887 CHECK_EQ(block->GetDexPc(), it2.DexPc());
888 ++it2;
889 }
890 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000891 CHECK(it == table.PcToDexEnd());
892 CHECK(it2 == table.DexToPcEnd());
893 }
894}
895
Vladimir Markof9f64412015-09-02 14:05:49 +0100896void CodeGenerator::BuildVMapTable(ArenaVector<uint8_t>* data) const {
897 Leb128Encoder<ArenaAllocatorAdapter<uint8_t>> vmap_encoder(data);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100898 // We currently don't use callee-saved registers.
899 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000900 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
901 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000902 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000903}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000904
Vladimir Markof9f64412015-09-02 14:05:49 +0100905void CodeGenerator::BuildStackMaps(ArenaVector<uint8_t>* data) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100906 uint32_t size = stack_map_stream_.PrepareForFillIn();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100907 data->resize(size);
908 MemoryRegion region(data->data(), size);
909 stack_map_stream_.FillIn(region);
910}
911
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600912void CodeGenerator::RecordNativeDebugInfo(uint32_t dex_pc,
913 uintptr_t native_pc_begin,
914 uintptr_t native_pc_end) {
915 if (src_map_ != nullptr && dex_pc != kNoDexPc && native_pc_begin != native_pc_end) {
916 src_map_->push_back(SrcMapElem({static_cast<uint32_t>(native_pc_begin),
917 static_cast<int32_t>(dex_pc)}));
918 }
919}
920
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000921void CodeGenerator::RecordPcInfo(HInstruction* instruction,
922 uint32_t dex_pc,
923 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000924 if (instruction != nullptr) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700925 // The code generated for some type conversions and comparisons
926 // may call the runtime, thus normally requiring a subsequent
927 // call to this method. However, the method verifier does not
928 // produce PC information for certain instructions, which are
929 // considered "atomic" (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000930 // Therefore we do not currently record PC information for such
931 // instructions. As this may change later, we added this special
932 // case so that code generators may nevertheless call
933 // CodeGenerator::RecordPcInfo without triggering an error in
934 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
935 // thereafter.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700936 if (instruction->IsTypeConversion() || instruction->IsCompare()) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000937 return;
938 }
939 if (instruction->IsRem()) {
940 Primitive::Type type = instruction->AsRem()->GetResultType();
941 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
942 return;
943 }
944 }
Roland Levillain624279f2014-12-04 11:54:28 +0000945 }
946
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100947 uint32_t outer_dex_pc = dex_pc;
948 uint32_t outer_environment_size = 0;
949 uint32_t inlining_depth = 0;
950 if (instruction != nullptr) {
951 for (HEnvironment* environment = instruction->GetEnvironment();
952 environment != nullptr;
953 environment = environment->GetParent()) {
954 outer_dex_pc = environment->GetDexPc();
955 outer_environment_size = environment->Size();
956 if (environment != instruction->GetEnvironment()) {
957 inlining_depth++;
958 }
959 }
960 }
961
Nicolas Geoffray39468442014-09-02 15:17:15 +0100962 // Collect PC infos for the mapping table.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100963 uint32_t native_pc = GetAssembler()->CodeSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100964
Nicolas Geoffray39468442014-09-02 15:17:15 +0100965 if (instruction == nullptr) {
966 // For stack overflow checks.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100967 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100968 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000969 return;
970 }
971 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100972
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000973 uint32_t register_mask = locations->GetRegisterMask();
974 if (locations->OnlyCallsOnSlowPath()) {
975 // In case of slow path, we currently set the location of caller-save registers
976 // to register (instead of their stack location when pushed before the slow-path
977 // call). Therefore register_mask contains both callee-save and caller-save
978 // registers that hold objects. We must remove the caller-save from the mask, since
979 // they will be overwritten by the callee.
980 register_mask &= core_callee_save_mask_;
981 }
982 // The register mask must be a subset of callee-save registers.
983 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100984 stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
985 native_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100986 register_mask,
987 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100988 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100989 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100990
991 EmitEnvironment(instruction->GetEnvironment(), slow_path);
992 stack_map_stream_.EndStackMapEntry();
993}
994
David Brazdil77a48ae2015-09-15 12:34:04 +0000995void CodeGenerator::RecordCatchBlockInfo() {
996 ArenaAllocator* arena = graph_->GetArena();
997
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100998 for (HBasicBlock* block : *block_order_) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000999 if (!block->IsCatchBlock()) {
1000 continue;
1001 }
1002
1003 uint32_t dex_pc = block->GetDexPc();
1004 uint32_t num_vregs = graph_->GetNumberOfVRegs();
1005 uint32_t inlining_depth = 0; // Inlining of catch blocks is not supported at the moment.
1006 uint32_t native_pc = GetAddressOf(block);
1007 uint32_t register_mask = 0; // Not used.
1008
1009 // The stack mask is not used, so we leave it empty.
1010 ArenaBitVector* stack_mask = new (arena) ArenaBitVector(arena, 0, /* expandable */ true);
1011
1012 stack_map_stream_.BeginStackMapEntry(dex_pc,
1013 native_pc,
1014 register_mask,
1015 stack_mask,
1016 num_vregs,
1017 inlining_depth);
1018
1019 HInstruction* current_phi = block->GetFirstPhi();
1020 for (size_t vreg = 0; vreg < num_vregs; ++vreg) {
1021 while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) {
1022 HInstruction* next_phi = current_phi->GetNext();
1023 DCHECK(next_phi == nullptr ||
1024 current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber())
1025 << "Phis need to be sorted by vreg number to keep this a linear-time loop.";
1026 current_phi = next_phi;
1027 }
1028
1029 if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) {
1030 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
1031 } else {
1032 Location location = current_phi->GetLiveInterval()->ToLocation();
1033 switch (location.GetKind()) {
1034 case Location::kStackSlot: {
1035 stack_map_stream_.AddDexRegisterEntry(
1036 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
1037 break;
1038 }
1039 case Location::kDoubleStackSlot: {
1040 stack_map_stream_.AddDexRegisterEntry(
1041 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
1042 stack_map_stream_.AddDexRegisterEntry(
1043 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
1044 ++vreg;
1045 DCHECK_LT(vreg, num_vregs);
1046 break;
1047 }
1048 default: {
1049 // All catch phis must be allocated to a stack slot.
1050 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1051 UNREACHABLE();
1052 }
1053 }
1054 }
1055 }
1056
1057 stack_map_stream_.EndStackMapEntry();
1058 }
1059}
1060
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001061void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
1062 if (environment == nullptr) return;
1063
1064 if (environment->GetParent() != nullptr) {
1065 // We emit the parent environment first.
1066 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001067 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
1068 environment->GetDexPc(),
1069 environment->GetInvokeType(),
1070 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001071 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001072
1073 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001074 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001075 HInstruction* current = environment->GetInstructionAt(i);
1076 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001077 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001078 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001079 }
1080
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001081 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001082 switch (location.GetKind()) {
1083 case Location::kConstant: {
1084 DCHECK_EQ(current, location.GetConstant());
1085 if (current->IsLongConstant()) {
1086 int64_t value = current->AsLongConstant()->GetValue();
1087 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001088 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001089 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001090 DexRegisterLocation::Kind::kConstant, High32Bits(value));
1091 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001092 DCHECK_LT(i, environment_size);
1093 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001094 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001095 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001096 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001097 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001098 DexRegisterLocation::Kind::kConstant, High32Bits(value));
1099 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001100 DCHECK_LT(i, environment_size);
1101 } else if (current->IsIntConstant()) {
1102 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001103 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001104 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001105 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001106 } else {
1107 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +00001108 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001109 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001110 }
1111 break;
1112 }
1113
1114 case Location::kStackSlot: {
1115 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001116 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001117 break;
1118 }
1119
1120 case Location::kDoubleStackSlot: {
1121 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001122 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001123 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001124 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
1125 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001126 DCHECK_LT(i, environment_size);
1127 break;
1128 }
1129
1130 case Location::kRegister : {
1131 int id = location.reg();
1132 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
1133 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001134 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001135 if (current->GetType() == Primitive::kPrimLong) {
1136 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001137 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
1138 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001139 DCHECK_LT(i, environment_size);
1140 }
1141 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001142 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001143 if (current->GetType() == Primitive::kPrimLong) {
David Brazdild9cb68e2015-08-25 13:52:43 +01001144 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001145 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001146 DCHECK_LT(i, environment_size);
1147 }
1148 }
1149 break;
1150 }
1151
1152 case Location::kFpuRegister : {
1153 int id = location.reg();
1154 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
1155 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001156 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001157 if (current->GetType() == Primitive::kPrimDouble) {
1158 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001159 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
1160 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001161 DCHECK_LT(i, environment_size);
1162 }
1163 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001164 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001165 if (current->GetType() == Primitive::kPrimDouble) {
David Brazdild9cb68e2015-08-25 13:52:43 +01001166 stack_map_stream_.AddDexRegisterEntry(
1167 DexRegisterLocation::Kind::kInFpuRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001168 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001169 DCHECK_LT(i, environment_size);
1170 }
1171 }
1172 break;
1173 }
1174
1175 case Location::kFpuRegisterPair : {
1176 int low = location.low();
1177 int high = location.high();
1178 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
1179 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001180 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001181 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001182 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001183 }
1184 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
1185 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001186 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1187 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001188 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001189 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
1190 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001191 }
1192 DCHECK_LT(i, environment_size);
1193 break;
1194 }
1195
1196 case Location::kRegisterPair : {
1197 int low = location.low();
1198 int high = location.high();
1199 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
1200 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001201 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001202 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001203 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001204 }
1205 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
1206 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001207 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001208 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001209 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001210 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001211 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001212 DCHECK_LT(i, environment_size);
1213 break;
1214 }
1215
1216 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001217 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001218 break;
1219 }
1220
1221 default:
1222 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1223 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001224 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001225
1226 if (environment->GetParent() != nullptr) {
1227 stack_map_stream_.EndInlineInfoEntry();
1228 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001229}
1230
David Brazdil77a48ae2015-09-15 12:34:04 +00001231bool CodeGenerator::IsImplicitNullCheckAllowed(HNullCheck* null_check) const {
1232 return compiler_options_.GetImplicitNullChecks() &&
1233 // Null checks which might throw into a catch block need to save live
1234 // registers and therefore cannot be done implicitly.
1235 !null_check->CanThrowIntoCatchBlock();
1236}
1237
Calin Juravle77520bc2015-01-12 18:45:46 +00001238bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
1239 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +01001240
1241 return (first_next_not_move != nullptr)
1242 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00001243}
1244
1245void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
1246 // If we are from a static path don't record the pc as we can't throw NPE.
1247 // NB: having the checks here makes the code much less verbose in the arch
1248 // specific code generators.
1249 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
1250 return;
1251 }
1252
Calin Juravle641547a2015-04-21 22:08:51 +01001253 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001254 return;
1255 }
1256
1257 // Find the first previous instruction which is not a move.
1258 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
1259
1260 // If the instruction is a null check it means that `instr` is the first user
1261 // and needs to record the pc.
1262 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
1263 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
David Brazdil77a48ae2015-09-15 12:34:04 +00001264 if (IsImplicitNullCheckAllowed(null_check)) {
1265 // TODO: The parallel moves modify the environment. Their changes need to be
1266 // reverted otherwise the stack maps at the throw point will not be correct.
1267 RecordPcInfo(null_check, null_check->GetDexPc());
1268 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001269 }
1270}
1271
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001272void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
1273 LocationSummary* locations = suspend_check->GetLocations();
1274 HBasicBlock* block = suspend_check->GetBlock();
1275 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
1276 DCHECK(block->IsLoopHeader());
1277
1278 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1279 HInstruction* current = it.Current();
1280 LiveInterval* interval = current->GetLiveInterval();
1281 // We only need to clear bits of loop phis containing objects and allocated in register.
1282 // Loop phis allocated on stack already have the object in the stack.
1283 if (current->GetType() == Primitive::kPrimNot
1284 && interval->HasRegister()
1285 && interval->HasSpillSlot()) {
1286 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
1287 }
1288 }
1289}
1290
Nicolas Geoffray90218252015-04-15 11:56:51 +01001291void CodeGenerator::EmitParallelMoves(Location from1,
1292 Location to1,
1293 Primitive::Type type1,
1294 Location from2,
1295 Location to2,
1296 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001297 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +01001298 parallel_move.AddMove(from1, to1, type1, nullptr);
1299 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001300 GetMoveResolver()->EmitNativeCode(&parallel_move);
1301}
1302
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001303void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) {
1304 // Ensure that the call kind indication given to the register allocator is
1305 // coherent with the runtime call generated, and that the GC side effect is
1306 // set when required.
1307 if (slow_path == nullptr) {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001308 DCHECK(instruction->GetLocations()->WillCall()) << instruction->DebugName();
1309 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()))
1310 << instruction->DebugName() << instruction->GetSideEffects().ToString();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001311 } else {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001312 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal())
1313 << instruction->DebugName() << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001314 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) ||
1315 // Control flow would not come back into the code if a fatal slow
1316 // path is taken, so we do not care if it triggers GC.
1317 slow_path->IsFatal() ||
1318 // HDeoptimize is a special case: we know we are not coming back from
1319 // it into the code.
Roland Levillaindf3f8222015-08-13 12:31:44 +01001320 instruction->IsDeoptimize())
1321 << instruction->DebugName() << instruction->GetSideEffects().ToString()
1322 << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001323 }
1324
1325 // Check the coherency of leaf information.
1326 DCHECK(instruction->IsSuspendCheck()
1327 || ((slow_path != nullptr) && slow_path->IsFatal())
1328 || instruction->GetLocations()->CanCall()
Roland Levillaindf3f8222015-08-13 12:31:44 +01001329 || !IsLeafMethod())
1330 << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : "");
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001331}
1332
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001333void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1334 RegisterSet* register_set = locations->GetLiveRegisters();
1335 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1336 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1337 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1338 if (register_set->ContainsCoreRegister(i)) {
1339 // If the register holds an object, update the stack mask.
1340 if (locations->RegisterContainsObject(i)) {
1341 locations->SetStackBit(stack_offset / kVRegSize);
1342 }
1343 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001344 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1345 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001346 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
1347 }
1348 }
1349 }
1350
1351 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1352 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1353 if (register_set->ContainsFloatingPointRegister(i)) {
1354 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001355 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1356 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001357 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
1358 }
1359 }
1360 }
1361}
1362
1363void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1364 RegisterSet* register_set = locations->GetLiveRegisters();
1365 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1366 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1367 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1368 if (register_set->ContainsCoreRegister(i)) {
1369 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1370 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1371 }
1372 }
1373 }
1374
1375 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1376 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1377 if (register_set->ContainsFloatingPointRegister(i)) {
1378 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1379 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1380 }
1381 }
1382 }
1383}
1384
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001385} // namespace art