blob: 77d53fcd8fe6fbaaab16b910e38f9b1cd4eb4ee9 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator.h"
18
Alex Light50fa9932015-08-10 15:30:07 -070019#ifdef ART_ENABLE_CODEGEN_arm
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000020#include "code_generator_arm.h"
Alex Light50fa9932015-08-10 15:30:07 -070021#endif
22
23#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames5319def2014-10-23 10:03:10 +010024#include "code_generator_arm64.h"
Alex Light50fa9932015-08-10 15:30:07 -070025#endif
26
27#ifdef ART_ENABLE_CODEGEN_x86
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000028#include "code_generator_x86.h"
Alex Light50fa9932015-08-10 15:30:07 -070029#endif
30
31#ifdef ART_ENABLE_CODEGEN_x86_64
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010032#include "code_generator_x86_64.h"
Alex Light50fa9932015-08-10 15:30:07 -070033#endif
34
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020035#ifdef ART_ENABLE_CODEGEN_mips
36#include "code_generator_mips.h"
37#endif
38
Alex Light50fa9932015-08-10 15:30:07 -070039#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -070040#include "code_generator_mips64.h"
Alex Light50fa9932015-08-10 15:30:07 -070041#endif
42
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070043#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000044#include "dex/verified_method.h"
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +000045#include "driver/compiler_driver.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000046#include "gc_map_builder.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010047#include "graph_visualizer.h"
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +010048#include "intrinsics.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000049#include "leb128.h"
50#include "mapping_table.h"
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010051#include "mirror/array-inl.h"
52#include "mirror/object_array-inl.h"
53#include "mirror/object_reference.h"
Alex Light50fa9932015-08-10 15:30:07 -070054#include "parallel_move_resolver.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010055#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000056#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000057#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000058#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000059
60namespace art {
61
Alexandre Rames88c13cd2015-04-14 17:35:39 +010062// Return whether a location is consistent with a type.
63static bool CheckType(Primitive::Type type, Location location) {
64 if (location.IsFpuRegister()
65 || (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresFpuRegister))) {
66 return (type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble);
67 } else if (location.IsRegister() ||
68 (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresRegister))) {
69 return Primitive::IsIntegralType(type) || (type == Primitive::kPrimNot);
70 } else if (location.IsRegisterPair()) {
71 return type == Primitive::kPrimLong;
72 } else if (location.IsFpuRegisterPair()) {
73 return type == Primitive::kPrimDouble;
74 } else if (location.IsStackSlot()) {
75 return (Primitive::IsIntegralType(type) && type != Primitive::kPrimLong)
76 || (type == Primitive::kPrimFloat)
77 || (type == Primitive::kPrimNot);
78 } else if (location.IsDoubleStackSlot()) {
79 return (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
80 } else if (location.IsConstant()) {
81 if (location.GetConstant()->IsIntConstant()) {
82 return Primitive::IsIntegralType(type) && (type != Primitive::kPrimLong);
83 } else if (location.GetConstant()->IsNullConstant()) {
84 return type == Primitive::kPrimNot;
85 } else if (location.GetConstant()->IsLongConstant()) {
86 return type == Primitive::kPrimLong;
87 } else if (location.GetConstant()->IsFloatConstant()) {
88 return type == Primitive::kPrimFloat;
89 } else {
90 return location.GetConstant()->IsDoubleConstant()
91 && (type == Primitive::kPrimDouble);
92 }
93 } else {
94 return location.IsInvalid() || (location.GetPolicy() == Location::kAny);
95 }
96}
97
98// Check that a location summary is consistent with an instruction.
99static bool CheckTypeConsistency(HInstruction* instruction) {
100 LocationSummary* locations = instruction->GetLocations();
101 if (locations == nullptr) {
102 return true;
103 }
104
105 if (locations->Out().IsUnallocated()
106 && (locations->Out().GetPolicy() == Location::kSameAsFirstInput)) {
107 DCHECK(CheckType(instruction->GetType(), locations->InAt(0)))
108 << instruction->GetType()
109 << " " << locations->InAt(0);
110 } else {
111 DCHECK(CheckType(instruction->GetType(), locations->Out()))
112 << instruction->GetType()
113 << " " << locations->Out();
114 }
115
116 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
117 DCHECK(CheckType(instruction->InputAt(i)->GetType(), locations->InAt(i)))
118 << instruction->InputAt(i)->GetType()
119 << " " << locations->InAt(i);
120 }
121
122 HEnvironment* environment = instruction->GetEnvironment();
123 for (size_t i = 0; i < instruction->EnvironmentSize(); ++i) {
124 if (environment->GetInstructionAt(i) != nullptr) {
125 Primitive::Type type = environment->GetInstructionAt(i)->GetType();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100126 DCHECK(CheckType(type, environment->GetLocationAt(i)))
127 << type << " " << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100128 } else {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100129 DCHECK(environment->GetLocationAt(i).IsInvalid())
130 << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100131 }
132 }
133 return true;
134}
135
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100136size_t CodeGenerator::GetCacheOffset(uint32_t index) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100137 return sizeof(GcRoot<mirror::Object>) * index;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100138}
139
Mathieu Chartiere401d142015-04-22 13:56:20 -0700140size_t CodeGenerator::GetCachePointerOffset(uint32_t index) {
141 auto pointer_size = InstructionSetPointerSize(GetInstructionSet());
Vladimir Marko05792b92015-08-03 11:56:49 +0100142 return pointer_size * index;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700143}
144
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100145void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000146 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100147 if (!is_leaf) {
148 MarkNotLeaf();
149 }
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700150 const bool is_64_bit = Is64BitInstructionSet(GetInstructionSet());
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000151 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
152 + GetGraph()->GetTemporariesVRegSlots()
153 + 1 /* filler */,
154 0, /* the baseline compiler does not have live registers at slow path */
155 0, /* the baseline compiler does not have live registers at slow path */
156 GetGraph()->GetMaximumNumberOfOutVRegs()
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700157 + (is_64_bit ? 2 : 1) /* current method */,
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000158 GetGraph()->GetBlocks());
159 CompileInternal(allocator, /* is_baseline */ true);
160}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100161
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000162bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100163 DCHECK_EQ((*block_order_)[current_block_index_], current);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000164 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
165}
166
167HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100168 for (size_t i = current_block_index_ + 1; i < block_order_->size(); ++i) {
169 HBasicBlock* block = (*block_order_)[i];
David Brazdilfc6a86a2015-06-26 10:33:45 +0000170 if (!block->IsSingleJump()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000171 return block;
172 }
173 }
174 return nullptr;
175}
176
177HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000178 while (block->IsSingleJump()) {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100179 block = block->GetSuccessors()[0];
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000180 }
181 return block;
182}
183
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100184class DisassemblyScope {
185 public:
186 DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen)
187 : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) {
188 if (codegen_.GetDisassemblyInformation() != nullptr) {
189 start_offset_ = codegen_.GetAssembler().CodeSize();
190 }
191 }
192
193 ~DisassemblyScope() {
194 // We avoid building this data when we know it will not be used.
195 if (codegen_.GetDisassemblyInformation() != nullptr) {
196 codegen_.GetDisassemblyInformation()->AddInstructionInterval(
197 instruction_, start_offset_, codegen_.GetAssembler().CodeSize());
198 }
199 }
200
201 private:
202 const CodeGenerator& codegen_;
203 HInstruction* instruction_;
204 size_t start_offset_;
205};
206
207
208void CodeGenerator::GenerateSlowPaths() {
209 size_t code_start = 0;
Vladimir Marko225b6462015-09-28 12:17:40 +0100210 for (SlowPathCode* slow_path : slow_paths_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000211 current_slow_path_ = slow_path;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100212 if (disasm_info_ != nullptr) {
213 code_start = GetAssembler()->CodeSize();
214 }
Vladimir Marko225b6462015-09-28 12:17:40 +0100215 slow_path->EmitNativeCode(this);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100216 if (disasm_info_ != nullptr) {
Vladimir Marko225b6462015-09-28 12:17:40 +0100217 disasm_info_->AddSlowPathInterval(slow_path, code_start, GetAssembler()->CodeSize());
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100218 }
219 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000220 current_slow_path_ = nullptr;
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100221}
222
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000223void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Roland Levillain3e3d7332015-04-28 11:00:54 +0100224 is_baseline_ = is_baseline;
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100225 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000226 DCHECK_EQ(current_block_index_, 0u);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100227
228 size_t frame_start = GetAssembler()->CodeSize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000229 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100230 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100231 if (disasm_info_ != nullptr) {
232 disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize());
233 }
234
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100235 for (size_t e = block_order_->size(); current_block_index_ < e; ++current_block_index_) {
236 HBasicBlock* block = (*block_order_)[current_block_index_];
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000237 // Don't generate code for an empty block. Its predecessors will branch to its successor
238 // directly. Also, the label of that block will not be emitted, so this helps catch
239 // errors where we reference that label.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000240 if (block->IsSingleJump()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100241 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100242 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
243 HInstruction* current = it.Current();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100244 DisassemblyScope disassembly_scope(current, *this);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000245 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000246 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000247 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100248 DCHECK(CheckTypeConsistency(current));
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600249 uintptr_t native_pc_begin = GetAssembler()->CodeSize();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100250 current->Accept(instruction_visitor);
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600251 uintptr_t native_pc_end = GetAssembler()->CodeSize();
252 RecordNativeDebugInfo(current->GetDexPc(), native_pc_begin, native_pc_end);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100253 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000254 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000255
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100256 GenerateSlowPaths();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000257
David Brazdil77a48ae2015-09-15 12:34:04 +0000258 // Emit catch stack maps at the end of the stack map stream as expected by the
259 // runtime exception handler.
260 if (!is_baseline && graph_->HasTryCatch()) {
261 RecordCatchBlockInfo();
262 }
263
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000264 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000265 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000266}
267
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100268void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000269 // The register allocator already called `InitializeCodeGeneration`,
270 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000271 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100272 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000273 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000274}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100275
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000276void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100277 size_t code_size = GetAssembler()->CodeSize();
278 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000279
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100280 MemoryRegion code(buffer, code_size);
281 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000282}
283
Vladimir Marko58155012015-08-19 12:49:41 +0000284void CodeGenerator::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches ATTRIBUTE_UNUSED) {
285 // No linker patches by default.
286}
287
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100288size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
289 for (size_t i = 0; i < length; ++i) {
290 if (!array[i]) {
291 array[i] = true;
292 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100293 }
294 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100295 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000296 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000297}
298
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000299size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
300 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000301 if (!array[i] && !array[i + 1]) {
302 array[i] = true;
303 array[i + 1] = true;
304 return i;
305 }
306 }
307 LOG(FATAL) << "Could not find a register in baseline register allocator";
308 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100309}
310
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000311void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
312 size_t maximum_number_of_live_core_registers,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000313 size_t maximum_number_of_live_fpu_registers,
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000314 size_t number_of_out_slots,
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100315 const ArenaVector<HBasicBlock*>& block_order) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000316 block_order_ = &block_order;
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100317 DCHECK(!block_order.empty());
318 DCHECK(block_order[0] == GetGraph()->GetEntryBlock());
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000319 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100320 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
321
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000322 if (number_of_spill_slots == 0
323 && !HasAllocatedCalleeSaveRegisters()
324 && IsLeafMethod()
325 && !RequiresCurrentMethod()) {
326 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000327 DCHECK_EQ(maximum_number_of_live_fpu_registers, 0u);
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000328 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
329 } else {
330 SetFrameSize(RoundUp(
331 number_of_spill_slots * kVRegSize
332 + number_of_out_slots * kVRegSize
333 + maximum_number_of_live_core_registers * GetWordSize()
Roland Levillain0d5a2812015-11-13 10:07:31 +0000334 + maximum_number_of_live_fpu_registers * GetFloatingPointSpillSlotSize()
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000335 + FrameEntrySpillSize(),
336 kStackAlignment));
337 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100338}
339
340Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
341 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000342 // The type of the previous instruction tells us if we need a single or double stack slot.
343 Primitive::Type type = temp->GetType();
344 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100345 // Use the temporary region (right below the dex registers).
346 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
347 - kVRegSize // filler
348 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000349 - ((temp_size + temp->GetIndex()) * kVRegSize);
350 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100351}
352
353int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
354 uint16_t reg_number = local->GetRegNumber();
355 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
356 if (reg_number >= number_of_locals) {
357 // Local is a parameter of the method. It is stored in the caller's frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700358 // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode.
359 return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100360 + (reg_number - number_of_locals) * kVRegSize;
361 } else {
362 // Local is a temporary in this method. It is stored in this method's frame.
363 return GetFrameSize() - FrameEntrySpillSize()
364 - kVRegSize // filler.
365 - (number_of_locals * kVRegSize)
366 + (reg_number * kVRegSize);
367 }
368}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100369
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100370void CodeGenerator::CreateCommonInvokeLocationSummary(
Nicolas Geoffray4e40c262015-06-03 12:02:38 +0100371 HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100372 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
373 LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100374
375 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
376 HInstruction* input = invoke->InputAt(i);
377 locations->SetInAt(i, visitor->GetNextLocation(input->GetType()));
378 }
379
380 locations->SetOut(visitor->GetReturnLocation(invoke->GetType()));
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100381
382 if (invoke->IsInvokeStaticOrDirect()) {
383 HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect();
Vladimir Markodc151b22015-10-15 18:02:30 +0100384 switch (call->GetMethodLoadKind()) {
385 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
386 locations->SetInAt(call->GetCurrentMethodInputIndex(), visitor->GetMethodLocation());
387 break;
388 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod:
389 locations->AddTemp(visitor->GetMethodLocation());
390 locations->SetInAt(call->GetCurrentMethodInputIndex(), Location::RequiresRegister());
391 break;
392 default:
393 locations->AddTemp(visitor->GetMethodLocation());
394 break;
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100395 }
396 } else {
397 locations->AddTemp(visitor->GetMethodLocation());
398 }
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100399}
400
Calin Juravle175dc732015-08-25 15:42:32 +0100401void CodeGenerator::GenerateInvokeUnresolvedRuntimeCall(HInvokeUnresolved* invoke) {
402 MoveConstant(invoke->GetLocations()->GetTemp(0), invoke->GetDexMethodIndex());
403
404 // Initialize to anything to silent compiler warnings.
405 QuickEntrypointEnum entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
406 switch (invoke->GetOriginalInvokeType()) {
407 case kStatic:
408 entrypoint = kQuickInvokeStaticTrampolineWithAccessCheck;
409 break;
410 case kDirect:
411 entrypoint = kQuickInvokeDirectTrampolineWithAccessCheck;
412 break;
413 case kVirtual:
414 entrypoint = kQuickInvokeVirtualTrampolineWithAccessCheck;
415 break;
416 case kSuper:
417 entrypoint = kQuickInvokeSuperTrampolineWithAccessCheck;
418 break;
419 case kInterface:
420 entrypoint = kQuickInvokeInterfaceTrampolineWithAccessCheck;
421 break;
422 }
423 InvokeRuntime(entrypoint, invoke, invoke->GetDexPc(), nullptr);
424}
425
Calin Juravlee460d1d2015-09-29 04:52:17 +0100426void CodeGenerator::CreateUnresolvedFieldLocationSummary(
427 HInstruction* field_access,
428 Primitive::Type field_type,
429 const FieldAccessCallingConvention& calling_convention) {
430 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
431 || field_access->IsUnresolvedInstanceFieldSet();
432 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
433 || field_access->IsUnresolvedStaticFieldGet();
434
435 ArenaAllocator* allocator = field_access->GetBlock()->GetGraph()->GetArena();
436 LocationSummary* locations =
437 new (allocator) LocationSummary(field_access, LocationSummary::kCall);
438
439 locations->AddTemp(calling_convention.GetFieldIndexLocation());
440
441 if (is_instance) {
442 // Add the `this` object for instance field accesses.
443 locations->SetInAt(0, calling_convention.GetObjectLocation());
444 }
445
446 // Note that pSetXXStatic/pGetXXStatic always takes/returns an int or int64
447 // regardless of the the type. Because of that we forced to special case
448 // the access to floating point values.
449 if (is_get) {
450 if (Primitive::IsFloatingPointType(field_type)) {
451 // The return value will be stored in regular registers while register
452 // allocator expects it in a floating point register.
453 // Note We don't need to request additional temps because the return
454 // register(s) are already blocked due the call and they may overlap with
455 // the input or field index.
456 // The transfer between the two will be done at codegen level.
457 locations->SetOut(calling_convention.GetFpuLocation(field_type));
458 } else {
459 locations->SetOut(calling_convention.GetReturnLocation(field_type));
460 }
461 } else {
462 size_t set_index = is_instance ? 1 : 0;
463 if (Primitive::IsFloatingPointType(field_type)) {
464 // The set value comes from a float location while the calling convention
465 // expects it in a regular register location. Allocate a temp for it and
466 // make the transfer at codegen.
467 AddLocationAsTemp(calling_convention.GetSetValueLocation(field_type, is_instance), locations);
468 locations->SetInAt(set_index, calling_convention.GetFpuLocation(field_type));
469 } else {
470 locations->SetInAt(set_index,
471 calling_convention.GetSetValueLocation(field_type, is_instance));
472 }
473 }
474}
475
476void CodeGenerator::GenerateUnresolvedFieldAccess(
477 HInstruction* field_access,
478 Primitive::Type field_type,
479 uint32_t field_index,
480 uint32_t dex_pc,
481 const FieldAccessCallingConvention& calling_convention) {
482 LocationSummary* locations = field_access->GetLocations();
483
484 MoveConstant(locations->GetTemp(0), field_index);
485
486 bool is_instance = field_access->IsUnresolvedInstanceFieldGet()
487 || field_access->IsUnresolvedInstanceFieldSet();
488 bool is_get = field_access->IsUnresolvedInstanceFieldGet()
489 || field_access->IsUnresolvedStaticFieldGet();
490
491 if (!is_get && Primitive::IsFloatingPointType(field_type)) {
492 // Copy the float value to be set into the calling convention register.
493 // Note that using directly the temp location is problematic as we don't
494 // support temp register pairs. To avoid boilerplate conversion code, use
495 // the location from the calling convention.
496 MoveLocation(calling_convention.GetSetValueLocation(field_type, is_instance),
497 locations->InAt(is_instance ? 1 : 0),
498 (Primitive::Is64BitType(field_type) ? Primitive::kPrimLong : Primitive::kPrimInt));
499 }
500
501 QuickEntrypointEnum entrypoint = kQuickSet8Static; // Initialize to anything to avoid warnings.
502 switch (field_type) {
503 case Primitive::kPrimBoolean:
504 entrypoint = is_instance
505 ? (is_get ? kQuickGetBooleanInstance : kQuickSet8Instance)
506 : (is_get ? kQuickGetBooleanStatic : kQuickSet8Static);
507 break;
508 case Primitive::kPrimByte:
509 entrypoint = is_instance
510 ? (is_get ? kQuickGetByteInstance : kQuickSet8Instance)
511 : (is_get ? kQuickGetByteStatic : kQuickSet8Static);
512 break;
513 case Primitive::kPrimShort:
514 entrypoint = is_instance
515 ? (is_get ? kQuickGetShortInstance : kQuickSet16Instance)
516 : (is_get ? kQuickGetShortStatic : kQuickSet16Static);
517 break;
518 case Primitive::kPrimChar:
519 entrypoint = is_instance
520 ? (is_get ? kQuickGetCharInstance : kQuickSet16Instance)
521 : (is_get ? kQuickGetCharStatic : kQuickSet16Static);
522 break;
523 case Primitive::kPrimInt:
524 case Primitive::kPrimFloat:
525 entrypoint = is_instance
526 ? (is_get ? kQuickGet32Instance : kQuickSet32Instance)
527 : (is_get ? kQuickGet32Static : kQuickSet32Static);
528 break;
529 case Primitive::kPrimNot:
530 entrypoint = is_instance
531 ? (is_get ? kQuickGetObjInstance : kQuickSetObjInstance)
532 : (is_get ? kQuickGetObjStatic : kQuickSetObjStatic);
533 break;
534 case Primitive::kPrimLong:
535 case Primitive::kPrimDouble:
536 entrypoint = is_instance
537 ? (is_get ? kQuickGet64Instance : kQuickSet64Instance)
538 : (is_get ? kQuickGet64Static : kQuickSet64Static);
539 break;
540 default:
541 LOG(FATAL) << "Invalid type " << field_type;
542 }
543 InvokeRuntime(entrypoint, field_access, dex_pc, nullptr);
544
545 if (is_get && Primitive::IsFloatingPointType(field_type)) {
546 MoveLocation(locations->Out(), calling_convention.GetReturnLocation(field_type), field_type);
547 }
548}
549
Roland Levillain0d5a2812015-11-13 10:07:31 +0000550// TODO: Remove argument `code_generator_supports_read_barrier` when
551// all code generators have read barrier support.
Calin Juravle98893e12015-10-02 21:05:03 +0100552void CodeGenerator::CreateLoadClassLocationSummary(HLoadClass* cls,
553 Location runtime_type_index_location,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000554 Location runtime_return_location,
555 bool code_generator_supports_read_barrier) {
Calin Juravle98893e12015-10-02 21:05:03 +0100556 ArenaAllocator* allocator = cls->GetBlock()->GetGraph()->GetArena();
557 LocationSummary::CallKind call_kind = cls->NeedsAccessCheck()
558 ? LocationSummary::kCall
Roland Levillain0d5a2812015-11-13 10:07:31 +0000559 : (((code_generator_supports_read_barrier && kEmitCompilerReadBarrier) ||
560 cls->CanCallRuntime())
561 ? LocationSummary::kCallOnSlowPath
562 : LocationSummary::kNoCall);
Calin Juravle98893e12015-10-02 21:05:03 +0100563 LocationSummary* locations = new (allocator) LocationSummary(cls, call_kind);
Calin Juravle98893e12015-10-02 21:05:03 +0100564 if (cls->NeedsAccessCheck()) {
Calin Juravle580b6092015-10-06 17:35:58 +0100565 locations->SetInAt(0, Location::NoLocation());
Calin Juravle98893e12015-10-02 21:05:03 +0100566 locations->AddTemp(runtime_type_index_location);
567 locations->SetOut(runtime_return_location);
568 } else {
Calin Juravle580b6092015-10-06 17:35:58 +0100569 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle98893e12015-10-02 21:05:03 +0100570 locations->SetOut(Location::RequiresRegister());
571 }
572}
573
574
Mark Mendell5f874182015-03-04 15:42:45 -0500575void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
576 // The DCHECKS below check that a register is not specified twice in
577 // the summary. The out location can overlap with an input, so we need
578 // to special case it.
579 if (location.IsRegister()) {
580 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
581 blocked_core_registers_[location.reg()] = true;
582 } else if (location.IsFpuRegister()) {
583 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
584 blocked_fpu_registers_[location.reg()] = true;
585 } else if (location.IsFpuRegisterPair()) {
586 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
587 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
588 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
589 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
590 } else if (location.IsRegisterPair()) {
591 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
592 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
593 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
594 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
595 }
596}
597
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100598void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
599 LocationSummary* locations = instruction->GetLocations();
600 if (locations == nullptr) return;
601
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100602 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
603 blocked_core_registers_[i] = false;
604 }
605
606 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
607 blocked_fpu_registers_[i] = false;
608 }
609
610 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
611 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100612 }
613
614 // Mark all fixed input, temp and output registers as used.
615 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500616 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100617 }
618
619 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
620 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500621 BlockIfInRegister(loc);
622 }
623 Location result_location = locations->Out();
624 if (locations->OutputCanOverlapWithInputs()) {
625 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100626 }
627
Mark Mendell5f874182015-03-04 15:42:45 -0500628 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100629
630 // Allocate all unallocated input locations.
631 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
632 Location loc = locations->InAt(i);
633 HInstruction* input = instruction->InputAt(i);
634 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100635 if ((loc.GetPolicy() == Location::kRequiresRegister)
636 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100637 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100638 } else {
639 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
640 HLoadLocal* load = input->AsLoadLocal();
641 if (load != nullptr) {
642 loc = GetStackLocation(load);
643 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100644 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100645 }
646 }
647 locations->SetInAt(i, loc);
648 }
649 }
650
651 // Allocate all unallocated temp locations.
652 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
653 Location loc = locations->GetTemp(i);
654 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000655 switch (loc.GetPolicy()) {
656 case Location::kRequiresRegister:
657 // Allocate a core register (large enough to fit a 32-bit integer).
658 loc = AllocateFreeRegister(Primitive::kPrimInt);
659 break;
660
661 case Location::kRequiresFpuRegister:
662 // Allocate a core register (large enough to fit a 64-bit double).
663 loc = AllocateFreeRegister(Primitive::kPrimDouble);
664 break;
665
666 default:
667 LOG(FATAL) << "Unexpected policy for temporary location "
668 << loc.GetPolicy();
669 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100670 locations->SetTempAt(i, loc);
671 }
672 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100673 if (result_location.IsUnallocated()) {
674 switch (result_location.GetPolicy()) {
675 case Location::kAny:
676 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100677 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100678 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100679 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100680 case Location::kSameAsFirstInput:
681 result_location = locations->InAt(0);
682 break;
683 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000684 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100685 }
686}
687
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000688void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
689 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100690 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100691 if (instruction->IsTemporary()) {
692 HInstruction* previous = instruction->GetPrevious();
693 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
694 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100695 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100696 return;
697 }
698 AllocateRegistersLocally(instruction);
699 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000700 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000701 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000702 if (location.IsValid()) {
703 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000704 if (input->GetNext()->IsTemporary()) {
705 // If the input was stored in a temporary, use that temporary to
706 // perform the move.
707 Move(input->GetNext(), location, instruction);
708 } else {
709 Move(input, location, instruction);
710 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000711 }
712 }
713}
714
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000715void CodeGenerator::AllocateLocations(HInstruction* instruction) {
716 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100717 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000718 LocationSummary* locations = instruction->GetLocations();
719 if (!instruction->IsSuspendCheckEntry()) {
720 if (locations != nullptr && locations->CanCall()) {
721 MarkNotLeaf();
722 }
723 if (instruction->NeedsCurrentMethod()) {
724 SetRequiresCurrentMethod();
725 }
726 }
727}
728
Serban Constantinescuecc43662015-08-13 13:33:12 +0100729void CodeGenerator::MaybeRecordStat(MethodCompilationStat compilation_stat, size_t count) const {
730 if (stats_ != nullptr) {
731 stats_->RecordStat(compilation_stat, count);
732 }
733}
734
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000735CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000736 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000737 const InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100738 const CompilerOptions& compiler_options,
739 OptimizingCompilerStats* stats) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000740 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -0700741#ifdef ART_ENABLE_CODEGEN_arm
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000742 case kArm:
743 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000744 return new arm::CodeGeneratorARM(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100745 *isa_features.AsArmInstructionSetFeatures(),
746 compiler_options,
747 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000748 }
Alex Light50fa9932015-08-10 15:30:07 -0700749#endif
750#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames5319def2014-10-23 10:03:10 +0100751 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000752 return new arm64::CodeGeneratorARM64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100753 *isa_features.AsArm64InstructionSetFeatures(),
754 compiler_options,
755 stats);
Alexandre Rames5319def2014-10-23 10:03:10 +0100756 }
Alex Light50fa9932015-08-10 15:30:07 -0700757#endif
758#ifdef ART_ENABLE_CODEGEN_mips
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200759 case kMips: {
760 return new mips::CodeGeneratorMIPS(graph,
761 *isa_features.AsMipsInstructionSetFeatures(),
762 compiler_options,
763 stats);
764 }
Alex Light50fa9932015-08-10 15:30:07 -0700765#endif
766#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700767 case kMips64: {
768 return new mips64::CodeGeneratorMIPS64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100769 *isa_features.AsMips64InstructionSetFeatures(),
770 compiler_options,
771 stats);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700772 }
Alex Light50fa9932015-08-10 15:30:07 -0700773#endif
774#ifdef ART_ENABLE_CODEGEN_x86
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000775 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400776 return new x86::CodeGeneratorX86(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100777 *isa_features.AsX86InstructionSetFeatures(),
778 compiler_options,
779 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000780 }
Alex Light50fa9932015-08-10 15:30:07 -0700781#endif
782#ifdef ART_ENABLE_CODEGEN_x86_64
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700783 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400784 return new x86_64::CodeGeneratorX86_64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100785 *isa_features.AsX86_64InstructionSetFeatures(),
786 compiler_options,
787 stats);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700788 }
Alex Light50fa9932015-08-10 15:30:07 -0700789#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000790 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000791 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000792 }
793}
794
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000795void CodeGenerator::BuildNativeGCMap(
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000796 ArenaVector<uint8_t>* data, const CompilerDriver& compiler_driver) const {
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000797 const std::vector<uint8_t>& gc_map_raw =
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000798 compiler_driver.GetVerifiedMethod(&GetGraph()->GetDexFile(), GetGraph()->GetMethodIdx())
799 ->GetDexGcMap();
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000800 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
801
Vladimir Markobd8c7252015-06-12 10:06:32 +0100802 uint32_t max_native_offset = stack_map_stream_.ComputeMaxNativePcOffset();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000803
Vladimir Markobd8c7252015-06-12 10:06:32 +0100804 size_t num_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
805 GcMapBuilder builder(data, num_stack_maps, max_native_offset, dex_gc_map.RegWidth());
806 for (size_t i = 0; i != num_stack_maps; ++i) {
807 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
808 uint32_t native_offset = stack_map_entry.native_pc_offset;
809 uint32_t dex_pc = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000810 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800811 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000812 builder.AddEntry(native_offset, references);
813 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000814}
815
Vladimir Markof9f64412015-09-02 14:05:49 +0100816void CodeGenerator::BuildMappingTable(ArenaVector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000817 uint32_t pc2dex_data_size = 0u;
Vladimir Markobd8c7252015-06-12 10:06:32 +0100818 uint32_t pc2dex_entries = stack_map_stream_.GetNumberOfStackMaps();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000819 uint32_t pc2dex_offset = 0u;
820 int32_t pc2dex_dalvik_offset = 0;
821 uint32_t dex2pc_data_size = 0u;
822 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000823 uint32_t dex2pc_offset = 0u;
824 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000825
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000826 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100827 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
828 pc2dex_data_size += UnsignedLeb128Size(stack_map_entry.native_pc_offset - pc2dex_offset);
829 pc2dex_data_size += SignedLeb128Size(stack_map_entry.dex_pc - pc2dex_dalvik_offset);
830 pc2dex_offset = stack_map_entry.native_pc_offset;
831 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000832 }
833
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000834 // Walk over the blocks and find which ones correspond to catch block entries.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100835 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000836 if (block->IsCatchBlock()) {
837 intptr_t native_pc = GetAddressOf(block);
838 ++dex2pc_entries;
839 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
840 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
841 dex2pc_offset = native_pc;
842 dex2pc_dalvik_offset = block->GetDexPc();
843 }
844 }
845
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000846 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
847 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
848 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
849 data->resize(data_size);
850
851 uint8_t* data_ptr = &(*data)[0];
852 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000853
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000854 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
855 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
856 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
857 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
858
859 pc2dex_offset = 0u;
860 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000861 dex2pc_offset = 0u;
862 dex2pc_dalvik_offset = 0u;
863
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000864 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100865 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
866 DCHECK(pc2dex_offset <= stack_map_entry.native_pc_offset);
867 write_pos = EncodeUnsignedLeb128(write_pos, stack_map_entry.native_pc_offset - pc2dex_offset);
868 write_pos = EncodeSignedLeb128(write_pos, stack_map_entry.dex_pc - pc2dex_dalvik_offset);
869 pc2dex_offset = stack_map_entry.native_pc_offset;
870 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000871 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000872
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100873 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000874 if (block->IsCatchBlock()) {
875 intptr_t native_pc = GetAddressOf(block);
876 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
877 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
878 dex2pc_offset = native_pc;
879 dex2pc_dalvik_offset = block->GetDexPc();
880 }
881 }
882
883
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000884 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
885 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
886
887 if (kIsDebugBuild) {
888 // Verify the encoded table holds the expected data.
889 MappingTable table(data_ptr);
890 CHECK_EQ(table.TotalSize(), total_entries);
891 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
892 auto it = table.PcToDexBegin();
893 auto it2 = table.DexToPcBegin();
894 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100895 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
896 CHECK_EQ(stack_map_entry.native_pc_offset, it.NativePcOffset());
897 CHECK_EQ(stack_map_entry.dex_pc, it.DexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000898 ++it;
899 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100900 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000901 if (block->IsCatchBlock()) {
902 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
903 CHECK_EQ(block->GetDexPc(), it2.DexPc());
904 ++it2;
905 }
906 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000907 CHECK(it == table.PcToDexEnd());
908 CHECK(it2 == table.DexToPcEnd());
909 }
910}
911
Vladimir Markof9f64412015-09-02 14:05:49 +0100912void CodeGenerator::BuildVMapTable(ArenaVector<uint8_t>* data) const {
Vladimir Markoec7802a2015-10-01 20:57:57 +0100913 Leb128Encoder<ArenaVector<uint8_t>> vmap_encoder(data);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100914 // We currently don't use callee-saved registers.
915 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000916 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
917 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000918 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000919}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000920
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000921size_t CodeGenerator::ComputeStackMapsSize() {
922 return stack_map_stream_.PrepareForFillIn();
923}
924
925void CodeGenerator::BuildStackMaps(MemoryRegion region) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100926 stack_map_stream_.FillIn(region);
927}
928
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600929void CodeGenerator::RecordNativeDebugInfo(uint32_t dex_pc,
930 uintptr_t native_pc_begin,
931 uintptr_t native_pc_end) {
Nicolas Geoffrayd28b9692015-11-04 14:36:55 +0000932 if (compiler_options_.GetGenerateDebugInfo() &&
933 dex_pc != kNoDexPc &&
934 native_pc_begin != native_pc_end) {
935 src_map_.push_back(SrcMapElem({static_cast<uint32_t>(native_pc_begin),
936 static_cast<int32_t>(dex_pc)}));
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600937 }
938}
939
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000940void CodeGenerator::RecordPcInfo(HInstruction* instruction,
941 uint32_t dex_pc,
942 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000943 if (instruction != nullptr) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700944 // The code generated for some type conversions and comparisons
945 // may call the runtime, thus normally requiring a subsequent
946 // call to this method. However, the method verifier does not
947 // produce PC information for certain instructions, which are
948 // considered "atomic" (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000949 // Therefore we do not currently record PC information for such
950 // instructions. As this may change later, we added this special
951 // case so that code generators may nevertheless call
952 // CodeGenerator::RecordPcInfo without triggering an error in
953 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
954 // thereafter.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700955 if (instruction->IsTypeConversion() || instruction->IsCompare()) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000956 return;
957 }
958 if (instruction->IsRem()) {
959 Primitive::Type type = instruction->AsRem()->GetResultType();
960 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
961 return;
962 }
963 }
Roland Levillain624279f2014-12-04 11:54:28 +0000964 }
965
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100966 uint32_t outer_dex_pc = dex_pc;
967 uint32_t outer_environment_size = 0;
968 uint32_t inlining_depth = 0;
969 if (instruction != nullptr) {
970 for (HEnvironment* environment = instruction->GetEnvironment();
971 environment != nullptr;
972 environment = environment->GetParent()) {
973 outer_dex_pc = environment->GetDexPc();
974 outer_environment_size = environment->Size();
975 if (environment != instruction->GetEnvironment()) {
976 inlining_depth++;
977 }
978 }
979 }
980
Nicolas Geoffray39468442014-09-02 15:17:15 +0100981 // Collect PC infos for the mapping table.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100982 uint32_t native_pc = GetAssembler()->CodeSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100983
Nicolas Geoffray39468442014-09-02 15:17:15 +0100984 if (instruction == nullptr) {
985 // For stack overflow checks.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100986 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100987 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000988 return;
989 }
990 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100991
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000992 uint32_t register_mask = locations->GetRegisterMask();
993 if (locations->OnlyCallsOnSlowPath()) {
994 // In case of slow path, we currently set the location of caller-save registers
995 // to register (instead of their stack location when pushed before the slow-path
996 // call). Therefore register_mask contains both callee-save and caller-save
997 // registers that hold objects. We must remove the caller-save from the mask, since
998 // they will be overwritten by the callee.
999 register_mask &= core_callee_save_mask_;
1000 }
1001 // The register mask must be a subset of callee-save registers.
1002 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Vladimir Markobd8c7252015-06-12 10:06:32 +01001003 stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
1004 native_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +01001005 register_mask,
1006 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001007 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +01001008 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001009
1010 EmitEnvironment(instruction->GetEnvironment(), slow_path);
1011 stack_map_stream_.EndStackMapEntry();
1012}
1013
David Brazdil77a48ae2015-09-15 12:34:04 +00001014void CodeGenerator::RecordCatchBlockInfo() {
1015 ArenaAllocator* arena = graph_->GetArena();
1016
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001017 for (HBasicBlock* block : *block_order_) {
David Brazdil77a48ae2015-09-15 12:34:04 +00001018 if (!block->IsCatchBlock()) {
1019 continue;
1020 }
1021
1022 uint32_t dex_pc = block->GetDexPc();
1023 uint32_t num_vregs = graph_->GetNumberOfVRegs();
1024 uint32_t inlining_depth = 0; // Inlining of catch blocks is not supported at the moment.
1025 uint32_t native_pc = GetAddressOf(block);
1026 uint32_t register_mask = 0; // Not used.
1027
1028 // The stack mask is not used, so we leave it empty.
1029 ArenaBitVector* stack_mask = new (arena) ArenaBitVector(arena, 0, /* expandable */ true);
1030
1031 stack_map_stream_.BeginStackMapEntry(dex_pc,
1032 native_pc,
1033 register_mask,
1034 stack_mask,
1035 num_vregs,
1036 inlining_depth);
1037
1038 HInstruction* current_phi = block->GetFirstPhi();
1039 for (size_t vreg = 0; vreg < num_vregs; ++vreg) {
1040 while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) {
1041 HInstruction* next_phi = current_phi->GetNext();
1042 DCHECK(next_phi == nullptr ||
1043 current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber())
1044 << "Phis need to be sorted by vreg number to keep this a linear-time loop.";
1045 current_phi = next_phi;
1046 }
1047
1048 if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) {
1049 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
1050 } else {
1051 Location location = current_phi->GetLiveInterval()->ToLocation();
1052 switch (location.GetKind()) {
1053 case Location::kStackSlot: {
1054 stack_map_stream_.AddDexRegisterEntry(
1055 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
1056 break;
1057 }
1058 case Location::kDoubleStackSlot: {
1059 stack_map_stream_.AddDexRegisterEntry(
1060 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
1061 stack_map_stream_.AddDexRegisterEntry(
1062 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
1063 ++vreg;
1064 DCHECK_LT(vreg, num_vregs);
1065 break;
1066 }
1067 default: {
1068 // All catch phis must be allocated to a stack slot.
1069 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1070 UNREACHABLE();
1071 }
1072 }
1073 }
1074 }
1075
1076 stack_map_stream_.EndStackMapEntry();
1077 }
1078}
1079
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001080void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
1081 if (environment == nullptr) return;
1082
1083 if (environment->GetParent() != nullptr) {
1084 // We emit the parent environment first.
1085 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +01001086 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
1087 environment->GetDexPc(),
1088 environment->GetInvokeType(),
1089 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001090 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001091
1092 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001093 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001094 HInstruction* current = environment->GetInstructionAt(i);
1095 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001096 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001097 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +01001098 }
1099
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001100 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001101 switch (location.GetKind()) {
1102 case Location::kConstant: {
1103 DCHECK_EQ(current, location.GetConstant());
1104 if (current->IsLongConstant()) {
1105 int64_t value = current->AsLongConstant()->GetValue();
1106 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001107 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001108 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001109 DexRegisterLocation::Kind::kConstant, High32Bits(value));
1110 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001111 DCHECK_LT(i, environment_size);
1112 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001113 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001114 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001115 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001116 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001117 DexRegisterLocation::Kind::kConstant, High32Bits(value));
1118 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001119 DCHECK_LT(i, environment_size);
1120 } else if (current->IsIntConstant()) {
1121 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001122 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001123 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001124 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001125 } else {
1126 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +00001127 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001128 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001129 }
1130 break;
1131 }
1132
1133 case Location::kStackSlot: {
1134 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001135 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001136 break;
1137 }
1138
1139 case Location::kDoubleStackSlot: {
1140 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001141 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001142 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001143 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
1144 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001145 DCHECK_LT(i, environment_size);
1146 break;
1147 }
1148
1149 case Location::kRegister : {
1150 int id = location.reg();
1151 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
1152 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001153 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001154 if (current->GetType() == Primitive::kPrimLong) {
1155 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001156 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
1157 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001158 DCHECK_LT(i, environment_size);
1159 }
1160 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001161 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001162 if (current->GetType() == Primitive::kPrimLong) {
David Brazdild9cb68e2015-08-25 13:52:43 +01001163 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001164 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001165 DCHECK_LT(i, environment_size);
1166 }
1167 }
1168 break;
1169 }
1170
1171 case Location::kFpuRegister : {
1172 int id = location.reg();
1173 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
1174 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001175 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001176 if (current->GetType() == Primitive::kPrimDouble) {
1177 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001178 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
1179 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001180 DCHECK_LT(i, environment_size);
1181 }
1182 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001183 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001184 if (current->GetType() == Primitive::kPrimDouble) {
David Brazdild9cb68e2015-08-25 13:52:43 +01001185 stack_map_stream_.AddDexRegisterEntry(
1186 DexRegisterLocation::Kind::kInFpuRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001187 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001188 DCHECK_LT(i, environment_size);
1189 }
1190 }
1191 break;
1192 }
1193
1194 case Location::kFpuRegisterPair : {
1195 int low = location.low();
1196 int high = location.high();
1197 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
1198 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001199 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001200 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001201 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001202 }
1203 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
1204 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001205 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1206 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001207 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001208 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
1209 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001210 }
1211 DCHECK_LT(i, environment_size);
1212 break;
1213 }
1214
1215 case Location::kRegisterPair : {
1216 int low = location.low();
1217 int high = location.high();
1218 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
1219 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001220 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001221 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001222 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001223 }
1224 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
1225 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001226 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001227 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001228 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001229 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001230 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001231 DCHECK_LT(i, environment_size);
1232 break;
1233 }
1234
1235 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001236 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001237 break;
1238 }
1239
1240 default:
1241 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1242 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001243 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001244
1245 if (environment->GetParent() != nullptr) {
1246 stack_map_stream_.EndInlineInfoEntry();
1247 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001248}
1249
David Brazdil77a48ae2015-09-15 12:34:04 +00001250bool CodeGenerator::IsImplicitNullCheckAllowed(HNullCheck* null_check) const {
1251 return compiler_options_.GetImplicitNullChecks() &&
1252 // Null checks which might throw into a catch block need to save live
1253 // registers and therefore cannot be done implicitly.
1254 !null_check->CanThrowIntoCatchBlock();
1255}
1256
Calin Juravle77520bc2015-01-12 18:45:46 +00001257bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
1258 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +01001259
1260 return (first_next_not_move != nullptr)
1261 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00001262}
1263
1264void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
1265 // If we are from a static path don't record the pc as we can't throw NPE.
1266 // NB: having the checks here makes the code much less verbose in the arch
1267 // specific code generators.
1268 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
1269 return;
1270 }
1271
Calin Juravle641547a2015-04-21 22:08:51 +01001272 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001273 return;
1274 }
1275
1276 // Find the first previous instruction which is not a move.
1277 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
1278
1279 // If the instruction is a null check it means that `instr` is the first user
1280 // and needs to record the pc.
1281 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
1282 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
David Brazdil77a48ae2015-09-15 12:34:04 +00001283 if (IsImplicitNullCheckAllowed(null_check)) {
1284 // TODO: The parallel moves modify the environment. Their changes need to be
1285 // reverted otherwise the stack maps at the throw point will not be correct.
1286 RecordPcInfo(null_check, null_check->GetDexPc());
1287 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001288 }
1289}
1290
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001291void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
1292 LocationSummary* locations = suspend_check->GetLocations();
1293 HBasicBlock* block = suspend_check->GetBlock();
1294 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
1295 DCHECK(block->IsLoopHeader());
1296
1297 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1298 HInstruction* current = it.Current();
1299 LiveInterval* interval = current->GetLiveInterval();
1300 // We only need to clear bits of loop phis containing objects and allocated in register.
1301 // Loop phis allocated on stack already have the object in the stack.
1302 if (current->GetType() == Primitive::kPrimNot
1303 && interval->HasRegister()
1304 && interval->HasSpillSlot()) {
1305 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
1306 }
1307 }
1308}
1309
Nicolas Geoffray90218252015-04-15 11:56:51 +01001310void CodeGenerator::EmitParallelMoves(Location from1,
1311 Location to1,
1312 Primitive::Type type1,
1313 Location from2,
1314 Location to2,
1315 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001316 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +01001317 parallel_move.AddMove(from1, to1, type1, nullptr);
1318 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001319 GetMoveResolver()->EmitNativeCode(&parallel_move);
1320}
1321
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001322void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) {
1323 // Ensure that the call kind indication given to the register allocator is
1324 // coherent with the runtime call generated, and that the GC side effect is
1325 // set when required.
1326 if (slow_path == nullptr) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001327 DCHECK(instruction->GetLocations()->WillCall())
1328 << "instruction->DebugName()=" << instruction->DebugName();
Roland Levillaindf3f8222015-08-13 12:31:44 +01001329 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()))
Roland Levillain0d5a2812015-11-13 10:07:31 +00001330 << "instruction->DebugName()=" << instruction->DebugName()
1331 << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001332 } else {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001333 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal())
Roland Levillain0d5a2812015-11-13 10:07:31 +00001334 << "instruction->DebugName()=" << instruction->DebugName()
1335 << " slow_path->GetDescription()=" << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001336 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) ||
1337 // Control flow would not come back into the code if a fatal slow
1338 // path is taken, so we do not care if it triggers GC.
1339 slow_path->IsFatal() ||
1340 // HDeoptimize is a special case: we know we are not coming back from
1341 // it into the code.
Roland Levillain0d5a2812015-11-13 10:07:31 +00001342 instruction->IsDeoptimize() ||
1343 // When read barriers are enabled, some instructions use a
1344 // slow path to emit a read barrier, which does not trigger
1345 // GC, is not fatal, nor is emitted by HDeoptimize
1346 // instructions.
1347 (kEmitCompilerReadBarrier &&
1348 (instruction->IsInstanceFieldGet() ||
1349 instruction->IsStaticFieldGet() ||
1350 instruction->IsArraySet() ||
1351 instruction->IsArrayGet() ||
1352 instruction->IsLoadClass() ||
1353 instruction->IsLoadString() ||
1354 instruction->IsInstanceOf() ||
1355 instruction->IsCheckCast())))
1356 << "instruction->DebugName()=" << instruction->DebugName()
1357 << " instruction->GetSideEffects().ToString()=" << instruction->GetSideEffects().ToString()
1358 << " slow_path->GetDescription()=" << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001359 }
1360
1361 // Check the coherency of leaf information.
1362 DCHECK(instruction->IsSuspendCheck()
1363 || ((slow_path != nullptr) && slow_path->IsFatal())
1364 || instruction->GetLocations()->CanCall()
Roland Levillaindf3f8222015-08-13 12:31:44 +01001365 || !IsLeafMethod())
1366 << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : "");
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001367}
1368
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001369void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001370 RegisterSet* live_registers = locations->GetLiveRegisters();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001371 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001372
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001373 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1374 if (!codegen->IsCoreCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001375 if (live_registers->ContainsCoreRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001376 // If the register holds an object, update the stack mask.
1377 if (locations->RegisterContainsObject(i)) {
1378 locations->SetStackBit(stack_offset / kVRegSize);
1379 }
1380 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001381 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1382 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001383 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
1384 }
1385 }
1386 }
1387
1388 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1389 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001390 if (live_registers->ContainsFloatingPointRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001391 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001392 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1393 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001394 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
1395 }
1396 }
1397 }
1398}
1399
1400void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001401 RegisterSet* live_registers = locations->GetLiveRegisters();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001402 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
Roland Levillain0d5a2812015-11-13 10:07:31 +00001403
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001404 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1405 if (!codegen->IsCoreCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001406 if (live_registers->ContainsCoreRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001407 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Roland Levillain0d5a2812015-11-13 10:07:31 +00001408 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001409 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1410 }
1411 }
1412 }
1413
1414 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1415 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001416 if (live_registers->ContainsFloatingPointRegister(i)) {
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001417 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Roland Levillain0d5a2812015-11-13 10:07:31 +00001418 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001419 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1420 }
1421 }
1422 }
1423}
1424
Nicolas Geoffray5bd05a52015-10-13 09:48:30 +01001425void CodeGenerator::CreateSystemArrayCopyLocationSummary(HInvoke* invoke) {
1426 // Check to see if we have known failures that will cause us to have to bail out
1427 // to the runtime, and just generate the runtime call directly.
1428 HIntConstant* src_pos = invoke->InputAt(1)->AsIntConstant();
1429 HIntConstant* dest_pos = invoke->InputAt(3)->AsIntConstant();
1430
1431 // The positions must be non-negative.
1432 if ((src_pos != nullptr && src_pos->GetValue() < 0) ||
1433 (dest_pos != nullptr && dest_pos->GetValue() < 0)) {
1434 // We will have to fail anyways.
1435 return;
1436 }
1437
1438 // The length must be >= 0.
1439 HIntConstant* length = invoke->InputAt(4)->AsIntConstant();
1440 if (length != nullptr) {
1441 int32_t len = length->GetValue();
1442 if (len < 0) {
1443 // Just call as normal.
1444 return;
1445 }
1446 }
1447
1448 SystemArrayCopyOptimizations optimizations(invoke);
1449
1450 if (optimizations.GetDestinationIsSource()) {
1451 if (src_pos != nullptr && dest_pos != nullptr && src_pos->GetValue() < dest_pos->GetValue()) {
1452 // We only support backward copying if source and destination are the same.
1453 return;
1454 }
1455 }
1456
1457 if (optimizations.GetDestinationIsPrimitiveArray() || optimizations.GetSourceIsPrimitiveArray()) {
1458 // We currently don't intrinsify primitive copying.
1459 return;
1460 }
1461
1462 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
1463 LocationSummary* locations = new (allocator) LocationSummary(invoke,
1464 LocationSummary::kCallOnSlowPath,
1465 kIntrinsified);
1466 // arraycopy(Object src, int src_pos, Object dest, int dest_pos, int length).
1467 locations->SetInAt(0, Location::RequiresRegister());
1468 locations->SetInAt(1, Location::RegisterOrConstant(invoke->InputAt(1)));
1469 locations->SetInAt(2, Location::RequiresRegister());
1470 locations->SetInAt(3, Location::RegisterOrConstant(invoke->InputAt(3)));
1471 locations->SetInAt(4, Location::RegisterOrConstant(invoke->InputAt(4)));
1472
1473 locations->AddTemp(Location::RequiresRegister());
1474 locations->AddTemp(Location::RequiresRegister());
1475 locations->AddTemp(Location::RequiresRegister());
1476}
1477
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001478} // namespace art