blob: 3c6a41df34a6e871b6c6d28431cbddca8f35e260 [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;
206 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
207 if (disasm_info_ != nullptr) {
208 code_start = GetAssembler()->CodeSize();
209 }
210 slow_paths_.Get(i)->EmitNativeCode(this);
211 if (disasm_info_ != nullptr) {
212 disasm_info_->AddSlowPathInterval(slow_paths_.Get(i), code_start, GetAssembler()->CodeSize());
213 }
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
Mark Mendell5f874182015-03-04 15:42:45 -0500416void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
417 // The DCHECKS below check that a register is not specified twice in
418 // the summary. The out location can overlap with an input, so we need
419 // to special case it.
420 if (location.IsRegister()) {
421 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
422 blocked_core_registers_[location.reg()] = true;
423 } else if (location.IsFpuRegister()) {
424 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
425 blocked_fpu_registers_[location.reg()] = true;
426 } else if (location.IsFpuRegisterPair()) {
427 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
428 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
429 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
430 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
431 } else if (location.IsRegisterPair()) {
432 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
433 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
434 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
435 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
436 }
437}
438
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100439void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
440 LocationSummary* locations = instruction->GetLocations();
441 if (locations == nullptr) return;
442
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100443 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
444 blocked_core_registers_[i] = false;
445 }
446
447 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
448 blocked_fpu_registers_[i] = false;
449 }
450
451 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
452 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100453 }
454
455 // Mark all fixed input, temp and output registers as used.
456 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500457 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100458 }
459
460 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
461 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500462 BlockIfInRegister(loc);
463 }
464 Location result_location = locations->Out();
465 if (locations->OutputCanOverlapWithInputs()) {
466 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100467 }
468
Mark Mendell5f874182015-03-04 15:42:45 -0500469 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100470
471 // Allocate all unallocated input locations.
472 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
473 Location loc = locations->InAt(i);
474 HInstruction* input = instruction->InputAt(i);
475 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100476 if ((loc.GetPolicy() == Location::kRequiresRegister)
477 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100478 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100479 } else {
480 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
481 HLoadLocal* load = input->AsLoadLocal();
482 if (load != nullptr) {
483 loc = GetStackLocation(load);
484 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100485 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100486 }
487 }
488 locations->SetInAt(i, loc);
489 }
490 }
491
492 // Allocate all unallocated temp locations.
493 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
494 Location loc = locations->GetTemp(i);
495 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000496 switch (loc.GetPolicy()) {
497 case Location::kRequiresRegister:
498 // Allocate a core register (large enough to fit a 32-bit integer).
499 loc = AllocateFreeRegister(Primitive::kPrimInt);
500 break;
501
502 case Location::kRequiresFpuRegister:
503 // Allocate a core register (large enough to fit a 64-bit double).
504 loc = AllocateFreeRegister(Primitive::kPrimDouble);
505 break;
506
507 default:
508 LOG(FATAL) << "Unexpected policy for temporary location "
509 << loc.GetPolicy();
510 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100511 locations->SetTempAt(i, loc);
512 }
513 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100514 if (result_location.IsUnallocated()) {
515 switch (result_location.GetPolicy()) {
516 case Location::kAny:
517 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100518 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100519 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100520 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100521 case Location::kSameAsFirstInput:
522 result_location = locations->InAt(0);
523 break;
524 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000525 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100526 }
527}
528
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000529void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
530 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100531 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100532 if (instruction->IsTemporary()) {
533 HInstruction* previous = instruction->GetPrevious();
534 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
535 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100536 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100537 return;
538 }
539 AllocateRegistersLocally(instruction);
540 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000541 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000542 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000543 if (location.IsValid()) {
544 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000545 if (input->GetNext()->IsTemporary()) {
546 // If the input was stored in a temporary, use that temporary to
547 // perform the move.
548 Move(input->GetNext(), location, instruction);
549 } else {
550 Move(input, location, instruction);
551 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000552 }
553 }
554}
555
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000556void CodeGenerator::AllocateLocations(HInstruction* instruction) {
557 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100558 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000559 LocationSummary* locations = instruction->GetLocations();
560 if (!instruction->IsSuspendCheckEntry()) {
561 if (locations != nullptr && locations->CanCall()) {
562 MarkNotLeaf();
563 }
564 if (instruction->NeedsCurrentMethod()) {
565 SetRequiresCurrentMethod();
566 }
567 }
568}
569
Serban Constantinescuecc43662015-08-13 13:33:12 +0100570void CodeGenerator::MaybeRecordStat(MethodCompilationStat compilation_stat, size_t count) const {
571 if (stats_ != nullptr) {
572 stats_->RecordStat(compilation_stat, count);
573 }
574}
575
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000576CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000577 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000578 const InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100579 const CompilerOptions& compiler_options,
580 OptimizingCompilerStats* stats) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000581 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -0700582#ifdef ART_ENABLE_CODEGEN_arm
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000583 case kArm:
584 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000585 return new arm::CodeGeneratorARM(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100586 *isa_features.AsArmInstructionSetFeatures(),
587 compiler_options,
588 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000589 }
Alex Light50fa9932015-08-10 15:30:07 -0700590#endif
591#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames5319def2014-10-23 10:03:10 +0100592 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000593 return new arm64::CodeGeneratorARM64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100594 *isa_features.AsArm64InstructionSetFeatures(),
595 compiler_options,
596 stats);
Alexandre Rames5319def2014-10-23 10:03:10 +0100597 }
Alex Light50fa9932015-08-10 15:30:07 -0700598#endif
599#ifdef ART_ENABLE_CODEGEN_mips
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000600 case kMips:
Alex Light50fa9932015-08-10 15:30:07 -0700601 UNUSED(compiler_options);
602 UNUSED(graph);
603 UNUSED(isa_features);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000604 return nullptr;
Alex Light50fa9932015-08-10 15:30:07 -0700605#endif
606#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700607 case kMips64: {
608 return new mips64::CodeGeneratorMIPS64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100609 *isa_features.AsMips64InstructionSetFeatures(),
610 compiler_options,
611 stats);
Alexey Frunze4dda3372015-06-01 18:31:49 -0700612 }
Alex Light50fa9932015-08-10 15:30:07 -0700613#endif
614#ifdef ART_ENABLE_CODEGEN_x86
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000615 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400616 return new x86::CodeGeneratorX86(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100617 *isa_features.AsX86InstructionSetFeatures(),
618 compiler_options,
619 stats);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000620 }
Alex Light50fa9932015-08-10 15:30:07 -0700621#endif
622#ifdef ART_ENABLE_CODEGEN_x86_64
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700623 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400624 return new x86_64::CodeGeneratorX86_64(graph,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100625 *isa_features.AsX86_64InstructionSetFeatures(),
626 compiler_options,
627 stats);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700628 }
Alex Light50fa9932015-08-10 15:30:07 -0700629#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000630 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000631 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000632 }
633}
634
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000635void CodeGenerator::BuildNativeGCMap(
Vladimir Markof9f64412015-09-02 14:05:49 +0100636 ArenaVector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000637 const std::vector<uint8_t>& gc_map_raw =
638 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
639 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
640
Vladimir Markobd8c7252015-06-12 10:06:32 +0100641 uint32_t max_native_offset = stack_map_stream_.ComputeMaxNativePcOffset();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000642
Vladimir Markobd8c7252015-06-12 10:06:32 +0100643 size_t num_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
644 GcMapBuilder builder(data, num_stack_maps, max_native_offset, dex_gc_map.RegWidth());
645 for (size_t i = 0; i != num_stack_maps; ++i) {
646 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
647 uint32_t native_offset = stack_map_entry.native_pc_offset;
648 uint32_t dex_pc = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000649 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800650 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000651 builder.AddEntry(native_offset, references);
652 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000653}
654
Vladimir Markof9f64412015-09-02 14:05:49 +0100655void CodeGenerator::BuildMappingTable(ArenaVector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000656 uint32_t pc2dex_data_size = 0u;
Vladimir Markobd8c7252015-06-12 10:06:32 +0100657 uint32_t pc2dex_entries = stack_map_stream_.GetNumberOfStackMaps();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000658 uint32_t pc2dex_offset = 0u;
659 int32_t pc2dex_dalvik_offset = 0;
660 uint32_t dex2pc_data_size = 0u;
661 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000662 uint32_t dex2pc_offset = 0u;
663 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000664
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000665 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100666 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
667 pc2dex_data_size += UnsignedLeb128Size(stack_map_entry.native_pc_offset - pc2dex_offset);
668 pc2dex_data_size += SignedLeb128Size(stack_map_entry.dex_pc - pc2dex_dalvik_offset);
669 pc2dex_offset = stack_map_entry.native_pc_offset;
670 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000671 }
672
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000673 // Walk over the blocks and find which ones correspond to catch block entries.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100674 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000675 if (block->IsCatchBlock()) {
676 intptr_t native_pc = GetAddressOf(block);
677 ++dex2pc_entries;
678 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
679 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
680 dex2pc_offset = native_pc;
681 dex2pc_dalvik_offset = block->GetDexPc();
682 }
683 }
684
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000685 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
686 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
687 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
688 data->resize(data_size);
689
690 uint8_t* data_ptr = &(*data)[0];
691 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000692
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000693 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
694 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
695 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
696 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
697
698 pc2dex_offset = 0u;
699 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000700 dex2pc_offset = 0u;
701 dex2pc_dalvik_offset = 0u;
702
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000703 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100704 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
705 DCHECK(pc2dex_offset <= stack_map_entry.native_pc_offset);
706 write_pos = EncodeUnsignedLeb128(write_pos, stack_map_entry.native_pc_offset - pc2dex_offset);
707 write_pos = EncodeSignedLeb128(write_pos, stack_map_entry.dex_pc - pc2dex_dalvik_offset);
708 pc2dex_offset = stack_map_entry.native_pc_offset;
709 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000710 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000711
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100712 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000713 if (block->IsCatchBlock()) {
714 intptr_t native_pc = GetAddressOf(block);
715 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
716 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
717 dex2pc_offset = native_pc;
718 dex2pc_dalvik_offset = block->GetDexPc();
719 }
720 }
721
722
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000723 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
724 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
725
726 if (kIsDebugBuild) {
727 // Verify the encoded table holds the expected data.
728 MappingTable table(data_ptr);
729 CHECK_EQ(table.TotalSize(), total_entries);
730 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
731 auto it = table.PcToDexBegin();
732 auto it2 = table.DexToPcBegin();
733 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100734 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
735 CHECK_EQ(stack_map_entry.native_pc_offset, it.NativePcOffset());
736 CHECK_EQ(stack_map_entry.dex_pc, it.DexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000737 ++it;
738 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100739 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000740 if (block->IsCatchBlock()) {
741 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
742 CHECK_EQ(block->GetDexPc(), it2.DexPc());
743 ++it2;
744 }
745 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000746 CHECK(it == table.PcToDexEnd());
747 CHECK(it2 == table.DexToPcEnd());
748 }
749}
750
Vladimir Markof9f64412015-09-02 14:05:49 +0100751void CodeGenerator::BuildVMapTable(ArenaVector<uint8_t>* data) const {
752 Leb128Encoder<ArenaAllocatorAdapter<uint8_t>> vmap_encoder(data);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100753 // We currently don't use callee-saved registers.
754 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000755 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
756 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000757 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000758}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000759
Vladimir Markof9f64412015-09-02 14:05:49 +0100760void CodeGenerator::BuildStackMaps(ArenaVector<uint8_t>* data) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100761 uint32_t size = stack_map_stream_.PrepareForFillIn();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100762 data->resize(size);
763 MemoryRegion region(data->data(), size);
764 stack_map_stream_.FillIn(region);
765}
766
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600767void CodeGenerator::RecordNativeDebugInfo(uint32_t dex_pc,
768 uintptr_t native_pc_begin,
769 uintptr_t native_pc_end) {
770 if (src_map_ != nullptr && dex_pc != kNoDexPc && native_pc_begin != native_pc_end) {
771 src_map_->push_back(SrcMapElem({static_cast<uint32_t>(native_pc_begin),
772 static_cast<int32_t>(dex_pc)}));
773 }
774}
775
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000776void CodeGenerator::RecordPcInfo(HInstruction* instruction,
777 uint32_t dex_pc,
778 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000779 if (instruction != nullptr) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700780 // The code generated for some type conversions and comparisons
781 // may call the runtime, thus normally requiring a subsequent
782 // call to this method. However, the method verifier does not
783 // produce PC information for certain instructions, which are
784 // considered "atomic" (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000785 // Therefore we do not currently record PC information for such
786 // instructions. As this may change later, we added this special
787 // case so that code generators may nevertheless call
788 // CodeGenerator::RecordPcInfo without triggering an error in
789 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
790 // thereafter.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700791 if (instruction->IsTypeConversion() || instruction->IsCompare()) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000792 return;
793 }
794 if (instruction->IsRem()) {
795 Primitive::Type type = instruction->AsRem()->GetResultType();
796 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
797 return;
798 }
799 }
Roland Levillain624279f2014-12-04 11:54:28 +0000800 }
801
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100802 uint32_t outer_dex_pc = dex_pc;
803 uint32_t outer_environment_size = 0;
804 uint32_t inlining_depth = 0;
805 if (instruction != nullptr) {
806 for (HEnvironment* environment = instruction->GetEnvironment();
807 environment != nullptr;
808 environment = environment->GetParent()) {
809 outer_dex_pc = environment->GetDexPc();
810 outer_environment_size = environment->Size();
811 if (environment != instruction->GetEnvironment()) {
812 inlining_depth++;
813 }
814 }
815 }
816
Nicolas Geoffray39468442014-09-02 15:17:15 +0100817 // Collect PC infos for the mapping table.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100818 uint32_t native_pc = GetAssembler()->CodeSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100819
Nicolas Geoffray39468442014-09-02 15:17:15 +0100820 if (instruction == nullptr) {
821 // For stack overflow checks.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100822 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100823 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000824 return;
825 }
826 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100827
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000828 uint32_t register_mask = locations->GetRegisterMask();
829 if (locations->OnlyCallsOnSlowPath()) {
830 // In case of slow path, we currently set the location of caller-save registers
831 // to register (instead of their stack location when pushed before the slow-path
832 // call). Therefore register_mask contains both callee-save and caller-save
833 // registers that hold objects. We must remove the caller-save from the mask, since
834 // they will be overwritten by the callee.
835 register_mask &= core_callee_save_mask_;
836 }
837 // The register mask must be a subset of callee-save registers.
838 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100839 stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
840 native_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100841 register_mask,
842 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100843 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100844 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100845
846 EmitEnvironment(instruction->GetEnvironment(), slow_path);
847 stack_map_stream_.EndStackMapEntry();
848}
849
David Brazdil77a48ae2015-09-15 12:34:04 +0000850void CodeGenerator::RecordCatchBlockInfo() {
851 ArenaAllocator* arena = graph_->GetArena();
852
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100853 for (HBasicBlock* block : *block_order_) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000854 if (!block->IsCatchBlock()) {
855 continue;
856 }
857
858 uint32_t dex_pc = block->GetDexPc();
859 uint32_t num_vregs = graph_->GetNumberOfVRegs();
860 uint32_t inlining_depth = 0; // Inlining of catch blocks is not supported at the moment.
861 uint32_t native_pc = GetAddressOf(block);
862 uint32_t register_mask = 0; // Not used.
863
864 // The stack mask is not used, so we leave it empty.
865 ArenaBitVector* stack_mask = new (arena) ArenaBitVector(arena, 0, /* expandable */ true);
866
867 stack_map_stream_.BeginStackMapEntry(dex_pc,
868 native_pc,
869 register_mask,
870 stack_mask,
871 num_vregs,
872 inlining_depth);
873
874 HInstruction* current_phi = block->GetFirstPhi();
875 for (size_t vreg = 0; vreg < num_vregs; ++vreg) {
876 while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) {
877 HInstruction* next_phi = current_phi->GetNext();
878 DCHECK(next_phi == nullptr ||
879 current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber())
880 << "Phis need to be sorted by vreg number to keep this a linear-time loop.";
881 current_phi = next_phi;
882 }
883
884 if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) {
885 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
886 } else {
887 Location location = current_phi->GetLiveInterval()->ToLocation();
888 switch (location.GetKind()) {
889 case Location::kStackSlot: {
890 stack_map_stream_.AddDexRegisterEntry(
891 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
892 break;
893 }
894 case Location::kDoubleStackSlot: {
895 stack_map_stream_.AddDexRegisterEntry(
896 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
897 stack_map_stream_.AddDexRegisterEntry(
898 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
899 ++vreg;
900 DCHECK_LT(vreg, num_vregs);
901 break;
902 }
903 default: {
904 // All catch phis must be allocated to a stack slot.
905 LOG(FATAL) << "Unexpected kind " << location.GetKind();
906 UNREACHABLE();
907 }
908 }
909 }
910 }
911
912 stack_map_stream_.EndStackMapEntry();
913 }
914}
915
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100916void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
917 if (environment == nullptr) return;
918
919 if (environment->GetParent() != nullptr) {
920 // We emit the parent environment first.
921 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100922 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
923 environment->GetDexPc(),
924 environment->GetInvokeType(),
925 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100926 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000927
928 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100929 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000930 HInstruction* current = environment->GetInstructionAt(i);
931 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100932 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000933 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100934 }
935
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100936 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000937 switch (location.GetKind()) {
938 case Location::kConstant: {
939 DCHECK_EQ(current, location.GetConstant());
940 if (current->IsLongConstant()) {
941 int64_t value = current->AsLongConstant()->GetValue();
942 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100943 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000944 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100945 DexRegisterLocation::Kind::kConstant, High32Bits(value));
946 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000947 DCHECK_LT(i, environment_size);
948 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000949 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000950 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100951 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000952 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100953 DexRegisterLocation::Kind::kConstant, High32Bits(value));
954 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000955 DCHECK_LT(i, environment_size);
956 } else if (current->IsIntConstant()) {
957 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100958 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000959 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100960 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000961 } else {
962 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000963 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100964 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000965 }
966 break;
967 }
968
969 case Location::kStackSlot: {
970 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100971 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000972 break;
973 }
974
975 case Location::kDoubleStackSlot: {
976 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100977 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000978 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100979 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
980 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000981 DCHECK_LT(i, environment_size);
982 break;
983 }
984
985 case Location::kRegister : {
986 int id = location.reg();
987 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
988 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100989 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000990 if (current->GetType() == Primitive::kPrimLong) {
991 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100992 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
993 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000994 DCHECK_LT(i, environment_size);
995 }
996 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100997 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000998 if (current->GetType() == Primitive::kPrimLong) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100999 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001000 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001001 DCHECK_LT(i, environment_size);
1002 }
1003 }
1004 break;
1005 }
1006
1007 case Location::kFpuRegister : {
1008 int id = location.reg();
1009 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
1010 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001011 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001012 if (current->GetType() == Primitive::kPrimDouble) {
1013 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001014 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
1015 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001016 DCHECK_LT(i, environment_size);
1017 }
1018 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001019 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001020 if (current->GetType() == Primitive::kPrimDouble) {
David Brazdild9cb68e2015-08-25 13:52:43 +01001021 stack_map_stream_.AddDexRegisterEntry(
1022 DexRegisterLocation::Kind::kInFpuRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001023 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001024 DCHECK_LT(i, environment_size);
1025 }
1026 }
1027 break;
1028 }
1029
1030 case Location::kFpuRegisterPair : {
1031 int low = location.low();
1032 int high = location.high();
1033 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
1034 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001035 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001036 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001037 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001038 }
1039 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
1040 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001041 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1042 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001043 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001044 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
1045 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001046 }
1047 DCHECK_LT(i, environment_size);
1048 break;
1049 }
1050
1051 case Location::kRegisterPair : {
1052 int low = location.low();
1053 int high = location.high();
1054 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
1055 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001056 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001057 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001058 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001059 }
1060 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
1061 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001062 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001063 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001064 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001065 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001066 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001067 DCHECK_LT(i, environment_size);
1068 break;
1069 }
1070
1071 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001072 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001073 break;
1074 }
1075
1076 default:
1077 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1078 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001079 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001080
1081 if (environment->GetParent() != nullptr) {
1082 stack_map_stream_.EndInlineInfoEntry();
1083 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001084}
1085
David Brazdil77a48ae2015-09-15 12:34:04 +00001086bool CodeGenerator::IsImplicitNullCheckAllowed(HNullCheck* null_check) const {
1087 return compiler_options_.GetImplicitNullChecks() &&
1088 // Null checks which might throw into a catch block need to save live
1089 // registers and therefore cannot be done implicitly.
1090 !null_check->CanThrowIntoCatchBlock();
1091}
1092
Calin Juravle77520bc2015-01-12 18:45:46 +00001093bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
1094 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +01001095
1096 return (first_next_not_move != nullptr)
1097 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00001098}
1099
1100void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
1101 // If we are from a static path don't record the pc as we can't throw NPE.
1102 // NB: having the checks here makes the code much less verbose in the arch
1103 // specific code generators.
1104 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
1105 return;
1106 }
1107
Calin Juravle641547a2015-04-21 22:08:51 +01001108 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001109 return;
1110 }
1111
1112 // Find the first previous instruction which is not a move.
1113 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
1114
1115 // If the instruction is a null check it means that `instr` is the first user
1116 // and needs to record the pc.
1117 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
1118 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
David Brazdil77a48ae2015-09-15 12:34:04 +00001119 if (IsImplicitNullCheckAllowed(null_check)) {
1120 // TODO: The parallel moves modify the environment. Their changes need to be
1121 // reverted otherwise the stack maps at the throw point will not be correct.
1122 RecordPcInfo(null_check, null_check->GetDexPc());
1123 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001124 }
1125}
1126
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001127void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
1128 LocationSummary* locations = suspend_check->GetLocations();
1129 HBasicBlock* block = suspend_check->GetBlock();
1130 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
1131 DCHECK(block->IsLoopHeader());
1132
1133 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1134 HInstruction* current = it.Current();
1135 LiveInterval* interval = current->GetLiveInterval();
1136 // We only need to clear bits of loop phis containing objects and allocated in register.
1137 // Loop phis allocated on stack already have the object in the stack.
1138 if (current->GetType() == Primitive::kPrimNot
1139 && interval->HasRegister()
1140 && interval->HasSpillSlot()) {
1141 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
1142 }
1143 }
1144}
1145
Nicolas Geoffray90218252015-04-15 11:56:51 +01001146void CodeGenerator::EmitParallelMoves(Location from1,
1147 Location to1,
1148 Primitive::Type type1,
1149 Location from2,
1150 Location to2,
1151 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001152 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +01001153 parallel_move.AddMove(from1, to1, type1, nullptr);
1154 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001155 GetMoveResolver()->EmitNativeCode(&parallel_move);
1156}
1157
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001158void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) {
1159 // Ensure that the call kind indication given to the register allocator is
1160 // coherent with the runtime call generated, and that the GC side effect is
1161 // set when required.
1162 if (slow_path == nullptr) {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001163 DCHECK(instruction->GetLocations()->WillCall()) << instruction->DebugName();
1164 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()))
1165 << instruction->DebugName() << instruction->GetSideEffects().ToString();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001166 } else {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001167 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal())
1168 << instruction->DebugName() << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001169 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) ||
1170 // Control flow would not come back into the code if a fatal slow
1171 // path is taken, so we do not care if it triggers GC.
1172 slow_path->IsFatal() ||
1173 // HDeoptimize is a special case: we know we are not coming back from
1174 // it into the code.
Roland Levillaindf3f8222015-08-13 12:31:44 +01001175 instruction->IsDeoptimize())
1176 << instruction->DebugName() << instruction->GetSideEffects().ToString()
1177 << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001178 }
1179
1180 // Check the coherency of leaf information.
1181 DCHECK(instruction->IsSuspendCheck()
1182 || ((slow_path != nullptr) && slow_path->IsFatal())
1183 || instruction->GetLocations()->CanCall()
Roland Levillaindf3f8222015-08-13 12:31:44 +01001184 || !IsLeafMethod())
1185 << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : "");
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001186}
1187
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001188void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1189 RegisterSet* register_set = locations->GetLiveRegisters();
1190 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1191 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1192 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1193 if (register_set->ContainsCoreRegister(i)) {
1194 // If the register holds an object, update the stack mask.
1195 if (locations->RegisterContainsObject(i)) {
1196 locations->SetStackBit(stack_offset / kVRegSize);
1197 }
1198 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001199 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1200 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001201 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
1202 }
1203 }
1204 }
1205
1206 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1207 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1208 if (register_set->ContainsFloatingPointRegister(i)) {
1209 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001210 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1211 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001212 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
1213 }
1214 }
1215 }
1216}
1217
1218void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1219 RegisterSet* register_set = locations->GetLiveRegisters();
1220 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1221 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1222 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1223 if (register_set->ContainsCoreRegister(i)) {
1224 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1225 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1226 }
1227 }
1228 }
1229
1230 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1231 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1232 if (register_set->ContainsFloatingPointRegister(i)) {
1233 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1234 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1235 }
1236 }
1237 }
1238}
1239
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001240} // namespace art