blob: aadf525f2615d25b82a30e134a84f4576a37c5f1 [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
Mark Mendell5f874182015-03-04 15:42:45 -0500391void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
392 // The DCHECKS below check that a register is not specified twice in
393 // the summary. The out location can overlap with an input, so we need
394 // to special case it.
395 if (location.IsRegister()) {
396 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
397 blocked_core_registers_[location.reg()] = true;
398 } else if (location.IsFpuRegister()) {
399 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
400 blocked_fpu_registers_[location.reg()] = true;
401 } else if (location.IsFpuRegisterPair()) {
402 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
403 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
404 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
405 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
406 } else if (location.IsRegisterPair()) {
407 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
408 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
409 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
410 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
411 }
412}
413
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100414void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
415 LocationSummary* locations = instruction->GetLocations();
416 if (locations == nullptr) return;
417
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100418 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
419 blocked_core_registers_[i] = false;
420 }
421
422 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
423 blocked_fpu_registers_[i] = false;
424 }
425
426 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
427 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100428 }
429
430 // Mark all fixed input, temp and output registers as used.
431 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500432 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100433 }
434
435 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
436 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500437 BlockIfInRegister(loc);
438 }
439 Location result_location = locations->Out();
440 if (locations->OutputCanOverlapWithInputs()) {
441 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100442 }
443
Mark Mendell5f874182015-03-04 15:42:45 -0500444 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100445
446 // Allocate all unallocated input locations.
447 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
448 Location loc = locations->InAt(i);
449 HInstruction* input = instruction->InputAt(i);
450 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100451 if ((loc.GetPolicy() == Location::kRequiresRegister)
452 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100453 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100454 } else {
455 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
456 HLoadLocal* load = input->AsLoadLocal();
457 if (load != nullptr) {
458 loc = GetStackLocation(load);
459 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100460 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100461 }
462 }
463 locations->SetInAt(i, loc);
464 }
465 }
466
467 // Allocate all unallocated temp locations.
468 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
469 Location loc = locations->GetTemp(i);
470 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000471 switch (loc.GetPolicy()) {
472 case Location::kRequiresRegister:
473 // Allocate a core register (large enough to fit a 32-bit integer).
474 loc = AllocateFreeRegister(Primitive::kPrimInt);
475 break;
476
477 case Location::kRequiresFpuRegister:
478 // Allocate a core register (large enough to fit a 64-bit double).
479 loc = AllocateFreeRegister(Primitive::kPrimDouble);
480 break;
481
482 default:
483 LOG(FATAL) << "Unexpected policy for temporary location "
484 << loc.GetPolicy();
485 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100486 locations->SetTempAt(i, loc);
487 }
488 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100489 if (result_location.IsUnallocated()) {
490 switch (result_location.GetPolicy()) {
491 case Location::kAny:
492 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100493 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100494 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100495 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100496 case Location::kSameAsFirstInput:
497 result_location = locations->InAt(0);
498 break;
499 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000500 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100501 }
502}
503
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000504void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
505 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100506 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100507 if (instruction->IsTemporary()) {
508 HInstruction* previous = instruction->GetPrevious();
509 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
510 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100511 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100512 return;
513 }
514 AllocateRegistersLocally(instruction);
515 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000516 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000517 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000518 if (location.IsValid()) {
519 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000520 if (input->GetNext()->IsTemporary()) {
521 // If the input was stored in a temporary, use that temporary to
522 // perform the move.
523 Move(input->GetNext(), location, instruction);
524 } else {
525 Move(input, location, instruction);
526 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000527 }
528 }
529}
530
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000531void CodeGenerator::AllocateLocations(HInstruction* instruction) {
532 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100533 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000534 LocationSummary* locations = instruction->GetLocations();
535 if (!instruction->IsSuspendCheckEntry()) {
536 if (locations != nullptr && locations->CanCall()) {
537 MarkNotLeaf();
538 }
539 if (instruction->NeedsCurrentMethod()) {
540 SetRequiresCurrentMethod();
541 }
542 }
543}
544
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000545CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000546 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000547 const InstructionSetFeatures& isa_features,
548 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000549 switch (instruction_set) {
Alex Light50fa9932015-08-10 15:30:07 -0700550#ifdef ART_ENABLE_CODEGEN_arm
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000551 case kArm:
552 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000553 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000554 *isa_features.AsArmInstructionSetFeatures(),
555 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000556 }
Alex Light50fa9932015-08-10 15:30:07 -0700557#endif
558#ifdef ART_ENABLE_CODEGEN_arm64
Alexandre Rames5319def2014-10-23 10:03:10 +0100559 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000560 return new arm64::CodeGeneratorARM64(graph,
561 *isa_features.AsArm64InstructionSetFeatures(),
562 compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100563 }
Alex Light50fa9932015-08-10 15:30:07 -0700564#endif
565#ifdef ART_ENABLE_CODEGEN_mips
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000566 case kMips:
Alex Light50fa9932015-08-10 15:30:07 -0700567 UNUSED(compiler_options);
568 UNUSED(graph);
569 UNUSED(isa_features);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000570 return nullptr;
Alex Light50fa9932015-08-10 15:30:07 -0700571#endif
572#ifdef ART_ENABLE_CODEGEN_mips64
Alexey Frunze4dda3372015-06-01 18:31:49 -0700573 case kMips64: {
574 return new mips64::CodeGeneratorMIPS64(graph,
575 *isa_features.AsMips64InstructionSetFeatures(),
576 compiler_options);
577 }
Alex Light50fa9932015-08-10 15:30:07 -0700578#endif
579#ifdef ART_ENABLE_CODEGEN_x86
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000580 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400581 return new x86::CodeGeneratorX86(graph,
582 *isa_features.AsX86InstructionSetFeatures(),
583 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000584 }
Alex Light50fa9932015-08-10 15:30:07 -0700585#endif
586#ifdef ART_ENABLE_CODEGEN_x86_64
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700587 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400588 return new x86_64::CodeGeneratorX86_64(graph,
589 *isa_features.AsX86_64InstructionSetFeatures(),
590 compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700591 }
Alex Light50fa9932015-08-10 15:30:07 -0700592#endif
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000593 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000594 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000595 }
596}
597
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000598void CodeGenerator::BuildNativeGCMap(
Vladimir Markof9f64412015-09-02 14:05:49 +0100599 ArenaVector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000600 const std::vector<uint8_t>& gc_map_raw =
601 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
602 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
603
Vladimir Markobd8c7252015-06-12 10:06:32 +0100604 uint32_t max_native_offset = stack_map_stream_.ComputeMaxNativePcOffset();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000605
Vladimir Markobd8c7252015-06-12 10:06:32 +0100606 size_t num_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
607 GcMapBuilder builder(data, num_stack_maps, max_native_offset, dex_gc_map.RegWidth());
608 for (size_t i = 0; i != num_stack_maps; ++i) {
609 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
610 uint32_t native_offset = stack_map_entry.native_pc_offset;
611 uint32_t dex_pc = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000612 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800613 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000614 builder.AddEntry(native_offset, references);
615 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000616}
617
Vladimir Markof9f64412015-09-02 14:05:49 +0100618void CodeGenerator::BuildMappingTable(ArenaVector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000619 uint32_t pc2dex_data_size = 0u;
Vladimir Markobd8c7252015-06-12 10:06:32 +0100620 uint32_t pc2dex_entries = stack_map_stream_.GetNumberOfStackMaps();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000621 uint32_t pc2dex_offset = 0u;
622 int32_t pc2dex_dalvik_offset = 0;
623 uint32_t dex2pc_data_size = 0u;
624 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000625 uint32_t dex2pc_offset = 0u;
626 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000627
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000628 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100629 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
630 pc2dex_data_size += UnsignedLeb128Size(stack_map_entry.native_pc_offset - pc2dex_offset);
631 pc2dex_data_size += SignedLeb128Size(stack_map_entry.dex_pc - pc2dex_dalvik_offset);
632 pc2dex_offset = stack_map_entry.native_pc_offset;
633 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000634 }
635
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000636 // Walk over the blocks and find which ones correspond to catch block entries.
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100637 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000638 if (block->IsCatchBlock()) {
639 intptr_t native_pc = GetAddressOf(block);
640 ++dex2pc_entries;
641 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
642 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
643 dex2pc_offset = native_pc;
644 dex2pc_dalvik_offset = block->GetDexPc();
645 }
646 }
647
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000648 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
649 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
650 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
651 data->resize(data_size);
652
653 uint8_t* data_ptr = &(*data)[0];
654 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000655
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000656 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
657 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
658 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
659 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
660
661 pc2dex_offset = 0u;
662 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000663 dex2pc_offset = 0u;
664 dex2pc_dalvik_offset = 0u;
665
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000666 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100667 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
668 DCHECK(pc2dex_offset <= stack_map_entry.native_pc_offset);
669 write_pos = EncodeUnsignedLeb128(write_pos, stack_map_entry.native_pc_offset - pc2dex_offset);
670 write_pos = EncodeSignedLeb128(write_pos, stack_map_entry.dex_pc - pc2dex_dalvik_offset);
671 pc2dex_offset = stack_map_entry.native_pc_offset;
672 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000673 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000674
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100675 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000676 if (block->IsCatchBlock()) {
677 intptr_t native_pc = GetAddressOf(block);
678 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
679 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
680 dex2pc_offset = native_pc;
681 dex2pc_dalvik_offset = block->GetDexPc();
682 }
683 }
684
685
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000686 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
687 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
688
689 if (kIsDebugBuild) {
690 // Verify the encoded table holds the expected data.
691 MappingTable table(data_ptr);
692 CHECK_EQ(table.TotalSize(), total_entries);
693 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
694 auto it = table.PcToDexBegin();
695 auto it2 = table.DexToPcBegin();
696 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100697 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
698 CHECK_EQ(stack_map_entry.native_pc_offset, it.NativePcOffset());
699 CHECK_EQ(stack_map_entry.dex_pc, it.DexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000700 ++it;
701 }
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100702 for (HBasicBlock* block : graph_->GetBlocks()) {
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000703 if (block->IsCatchBlock()) {
704 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
705 CHECK_EQ(block->GetDexPc(), it2.DexPc());
706 ++it2;
707 }
708 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000709 CHECK(it == table.PcToDexEnd());
710 CHECK(it2 == table.DexToPcEnd());
711 }
712}
713
Vladimir Markof9f64412015-09-02 14:05:49 +0100714void CodeGenerator::BuildVMapTable(ArenaVector<uint8_t>* data) const {
715 Leb128Encoder<ArenaAllocatorAdapter<uint8_t>> vmap_encoder(data);
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100716 // We currently don't use callee-saved registers.
717 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000718 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
719 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000720 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000721}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000722
Vladimir Markof9f64412015-09-02 14:05:49 +0100723void CodeGenerator::BuildStackMaps(ArenaVector<uint8_t>* data) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100724 uint32_t size = stack_map_stream_.PrepareForFillIn();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100725 data->resize(size);
726 MemoryRegion region(data->data(), size);
727 stack_map_stream_.FillIn(region);
728}
729
Yevgeny Rouban2a7c1ef2015-07-22 18:36:24 +0600730void CodeGenerator::RecordNativeDebugInfo(uint32_t dex_pc,
731 uintptr_t native_pc_begin,
732 uintptr_t native_pc_end) {
733 if (src_map_ != nullptr && dex_pc != kNoDexPc && native_pc_begin != native_pc_end) {
734 src_map_->push_back(SrcMapElem({static_cast<uint32_t>(native_pc_begin),
735 static_cast<int32_t>(dex_pc)}));
736 }
737}
738
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000739void CodeGenerator::RecordPcInfo(HInstruction* instruction,
740 uint32_t dex_pc,
741 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000742 if (instruction != nullptr) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700743 // The code generated for some type conversions and comparisons
744 // may call the runtime, thus normally requiring a subsequent
745 // call to this method. However, the method verifier does not
746 // produce PC information for certain instructions, which are
747 // considered "atomic" (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000748 // Therefore we do not currently record PC information for such
749 // instructions. As this may change later, we added this special
750 // case so that code generators may nevertheless call
751 // CodeGenerator::RecordPcInfo without triggering an error in
752 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
753 // thereafter.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700754 if (instruction->IsTypeConversion() || instruction->IsCompare()) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000755 return;
756 }
757 if (instruction->IsRem()) {
758 Primitive::Type type = instruction->AsRem()->GetResultType();
759 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
760 return;
761 }
762 }
Roland Levillain624279f2014-12-04 11:54:28 +0000763 }
764
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100765 uint32_t outer_dex_pc = dex_pc;
766 uint32_t outer_environment_size = 0;
767 uint32_t inlining_depth = 0;
768 if (instruction != nullptr) {
769 for (HEnvironment* environment = instruction->GetEnvironment();
770 environment != nullptr;
771 environment = environment->GetParent()) {
772 outer_dex_pc = environment->GetDexPc();
773 outer_environment_size = environment->Size();
774 if (environment != instruction->GetEnvironment()) {
775 inlining_depth++;
776 }
777 }
778 }
779
Nicolas Geoffray39468442014-09-02 15:17:15 +0100780 // Collect PC infos for the mapping table.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100781 uint32_t native_pc = GetAssembler()->CodeSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100782
Nicolas Geoffray39468442014-09-02 15:17:15 +0100783 if (instruction == nullptr) {
784 // For stack overflow checks.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100785 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100786 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000787 return;
788 }
789 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100790
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000791 uint32_t register_mask = locations->GetRegisterMask();
792 if (locations->OnlyCallsOnSlowPath()) {
793 // In case of slow path, we currently set the location of caller-save registers
794 // to register (instead of their stack location when pushed before the slow-path
795 // call). Therefore register_mask contains both callee-save and caller-save
796 // registers that hold objects. We must remove the caller-save from the mask, since
797 // they will be overwritten by the callee.
798 register_mask &= core_callee_save_mask_;
799 }
800 // The register mask must be a subset of callee-save registers.
801 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100802 stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
803 native_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100804 register_mask,
805 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100806 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100807 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100808
809 EmitEnvironment(instruction->GetEnvironment(), slow_path);
810 stack_map_stream_.EndStackMapEntry();
811}
812
David Brazdil77a48ae2015-09-15 12:34:04 +0000813void CodeGenerator::RecordCatchBlockInfo() {
814 ArenaAllocator* arena = graph_->GetArena();
815
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100816 for (HBasicBlock* block : *block_order_) {
David Brazdil77a48ae2015-09-15 12:34:04 +0000817 if (!block->IsCatchBlock()) {
818 continue;
819 }
820
821 uint32_t dex_pc = block->GetDexPc();
822 uint32_t num_vregs = graph_->GetNumberOfVRegs();
823 uint32_t inlining_depth = 0; // Inlining of catch blocks is not supported at the moment.
824 uint32_t native_pc = GetAddressOf(block);
825 uint32_t register_mask = 0; // Not used.
826
827 // The stack mask is not used, so we leave it empty.
828 ArenaBitVector* stack_mask = new (arena) ArenaBitVector(arena, 0, /* expandable */ true);
829
830 stack_map_stream_.BeginStackMapEntry(dex_pc,
831 native_pc,
832 register_mask,
833 stack_mask,
834 num_vregs,
835 inlining_depth);
836
837 HInstruction* current_phi = block->GetFirstPhi();
838 for (size_t vreg = 0; vreg < num_vregs; ++vreg) {
839 while (current_phi != nullptr && current_phi->AsPhi()->GetRegNumber() < vreg) {
840 HInstruction* next_phi = current_phi->GetNext();
841 DCHECK(next_phi == nullptr ||
842 current_phi->AsPhi()->GetRegNumber() <= next_phi->AsPhi()->GetRegNumber())
843 << "Phis need to be sorted by vreg number to keep this a linear-time loop.";
844 current_phi = next_phi;
845 }
846
847 if (current_phi == nullptr || current_phi->AsPhi()->GetRegNumber() != vreg) {
848 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
849 } else {
850 Location location = current_phi->GetLiveInterval()->ToLocation();
851 switch (location.GetKind()) {
852 case Location::kStackSlot: {
853 stack_map_stream_.AddDexRegisterEntry(
854 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
855 break;
856 }
857 case Location::kDoubleStackSlot: {
858 stack_map_stream_.AddDexRegisterEntry(
859 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
860 stack_map_stream_.AddDexRegisterEntry(
861 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
862 ++vreg;
863 DCHECK_LT(vreg, num_vregs);
864 break;
865 }
866 default: {
867 // All catch phis must be allocated to a stack slot.
868 LOG(FATAL) << "Unexpected kind " << location.GetKind();
869 UNREACHABLE();
870 }
871 }
872 }
873 }
874
875 stack_map_stream_.EndStackMapEntry();
876 }
877}
878
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100879void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
880 if (environment == nullptr) return;
881
882 if (environment->GetParent() != nullptr) {
883 // We emit the parent environment first.
884 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100885 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
886 environment->GetDexPc(),
887 environment->GetInvokeType(),
888 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100889 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000890
891 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100892 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000893 HInstruction* current = environment->GetInstructionAt(i);
894 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100895 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000896 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100897 }
898
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100899 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000900 switch (location.GetKind()) {
901 case Location::kConstant: {
902 DCHECK_EQ(current, location.GetConstant());
903 if (current->IsLongConstant()) {
904 int64_t value = current->AsLongConstant()->GetValue();
905 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100906 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000907 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100908 DexRegisterLocation::Kind::kConstant, High32Bits(value));
909 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000910 DCHECK_LT(i, environment_size);
911 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000912 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000913 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100914 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000915 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100916 DexRegisterLocation::Kind::kConstant, High32Bits(value));
917 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000918 DCHECK_LT(i, environment_size);
919 } else if (current->IsIntConstant()) {
920 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100921 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000922 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100923 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000924 } else {
925 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000926 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100927 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000928 }
929 break;
930 }
931
932 case Location::kStackSlot: {
933 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100934 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000935 break;
936 }
937
938 case Location::kDoubleStackSlot: {
939 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100940 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000941 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100942 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
943 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000944 DCHECK_LT(i, environment_size);
945 break;
946 }
947
948 case Location::kRegister : {
949 int id = location.reg();
950 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
951 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100952 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000953 if (current->GetType() == Primitive::kPrimLong) {
954 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100955 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
956 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000957 DCHECK_LT(i, environment_size);
958 }
959 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100960 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000961 if (current->GetType() == Primitive::kPrimLong) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100962 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100963 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000964 DCHECK_LT(i, environment_size);
965 }
966 }
967 break;
968 }
969
970 case Location::kFpuRegister : {
971 int id = location.reg();
972 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
973 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100974 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000975 if (current->GetType() == Primitive::kPrimDouble) {
976 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100977 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
978 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000979 DCHECK_LT(i, environment_size);
980 }
981 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100982 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000983 if (current->GetType() == Primitive::kPrimDouble) {
David Brazdild9cb68e2015-08-25 13:52:43 +0100984 stack_map_stream_.AddDexRegisterEntry(
985 DexRegisterLocation::Kind::kInFpuRegisterHigh, id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100986 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000987 DCHECK_LT(i, environment_size);
988 }
989 }
990 break;
991 }
992
993 case Location::kFpuRegisterPair : {
994 int low = location.low();
995 int high = location.high();
996 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
997 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100998 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000999 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001000 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001001 }
1002 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
1003 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001004 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
1005 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001006 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001007 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
1008 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001009 }
1010 DCHECK_LT(i, environment_size);
1011 break;
1012 }
1013
1014 case Location::kRegisterPair : {
1015 int low = location.low();
1016 int high = location.high();
1017 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
1018 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001019 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001020 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001021 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001022 }
1023 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
1024 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001025 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001026 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001027 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001028 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001029 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001030 DCHECK_LT(i, environment_size);
1031 break;
1032 }
1033
1034 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001035 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001036 break;
1037 }
1038
1039 default:
1040 LOG(FATAL) << "Unexpected kind " << location.GetKind();
1041 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001042 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +01001043
1044 if (environment->GetParent() != nullptr) {
1045 stack_map_stream_.EndInlineInfoEntry();
1046 }
Nicolas Geoffray39468442014-09-02 15:17:15 +01001047}
1048
David Brazdil77a48ae2015-09-15 12:34:04 +00001049bool CodeGenerator::IsImplicitNullCheckAllowed(HNullCheck* null_check) const {
1050 return compiler_options_.GetImplicitNullChecks() &&
1051 // Null checks which might throw into a catch block need to save live
1052 // registers and therefore cannot be done implicitly.
1053 !null_check->CanThrowIntoCatchBlock();
1054}
1055
Calin Juravle77520bc2015-01-12 18:45:46 +00001056bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
1057 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +01001058
1059 return (first_next_not_move != nullptr)
1060 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00001061}
1062
1063void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
1064 // If we are from a static path don't record the pc as we can't throw NPE.
1065 // NB: having the checks here makes the code much less verbose in the arch
1066 // specific code generators.
1067 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
1068 return;
1069 }
1070
Calin Juravle641547a2015-04-21 22:08:51 +01001071 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +00001072 return;
1073 }
1074
1075 // Find the first previous instruction which is not a move.
1076 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
1077
1078 // If the instruction is a null check it means that `instr` is the first user
1079 // and needs to record the pc.
1080 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
1081 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
David Brazdil77a48ae2015-09-15 12:34:04 +00001082 if (IsImplicitNullCheckAllowed(null_check)) {
1083 // TODO: The parallel moves modify the environment. Their changes need to be
1084 // reverted otherwise the stack maps at the throw point will not be correct.
1085 RecordPcInfo(null_check, null_check->GetDexPc());
1086 }
Calin Juravle77520bc2015-01-12 18:45:46 +00001087 }
1088}
1089
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001090void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
1091 LocationSummary* locations = suspend_check->GetLocations();
1092 HBasicBlock* block = suspend_check->GetBlock();
1093 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
1094 DCHECK(block->IsLoopHeader());
1095
1096 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
1097 HInstruction* current = it.Current();
1098 LiveInterval* interval = current->GetLiveInterval();
1099 // We only need to clear bits of loop phis containing objects and allocated in register.
1100 // Loop phis allocated on stack already have the object in the stack.
1101 if (current->GetType() == Primitive::kPrimNot
1102 && interval->HasRegister()
1103 && interval->HasSpillSlot()) {
1104 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
1105 }
1106 }
1107}
1108
Nicolas Geoffray90218252015-04-15 11:56:51 +01001109void CodeGenerator::EmitParallelMoves(Location from1,
1110 Location to1,
1111 Primitive::Type type1,
1112 Location from2,
1113 Location to2,
1114 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001115 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +01001116 parallel_move.AddMove(from1, to1, type1, nullptr);
1117 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001118 GetMoveResolver()->EmitNativeCode(&parallel_move);
1119}
1120
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001121void CodeGenerator::ValidateInvokeRuntime(HInstruction* instruction, SlowPathCode* slow_path) {
1122 // Ensure that the call kind indication given to the register allocator is
1123 // coherent with the runtime call generated, and that the GC side effect is
1124 // set when required.
1125 if (slow_path == nullptr) {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001126 DCHECK(instruction->GetLocations()->WillCall()) << instruction->DebugName();
1127 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()))
1128 << instruction->DebugName() << instruction->GetSideEffects().ToString();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001129 } else {
Roland Levillaindf3f8222015-08-13 12:31:44 +01001130 DCHECK(instruction->GetLocations()->OnlyCallsOnSlowPath() || slow_path->IsFatal())
1131 << instruction->DebugName() << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001132 DCHECK(instruction->GetSideEffects().Includes(SideEffects::CanTriggerGC()) ||
1133 // Control flow would not come back into the code if a fatal slow
1134 // path is taken, so we do not care if it triggers GC.
1135 slow_path->IsFatal() ||
1136 // HDeoptimize is a special case: we know we are not coming back from
1137 // it into the code.
Roland Levillaindf3f8222015-08-13 12:31:44 +01001138 instruction->IsDeoptimize())
1139 << instruction->DebugName() << instruction->GetSideEffects().ToString()
1140 << slow_path->GetDescription();
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001141 }
1142
1143 // Check the coherency of leaf information.
1144 DCHECK(instruction->IsSuspendCheck()
1145 || ((slow_path != nullptr) && slow_path->IsFatal())
1146 || instruction->GetLocations()->CanCall()
Roland Levillaindf3f8222015-08-13 12:31:44 +01001147 || !IsLeafMethod())
1148 << instruction->DebugName() << ((slow_path != nullptr) ? slow_path->GetDescription() : "");
Alexandre Rames78e3ef62015-08-12 13:43:29 +01001149}
1150
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001151void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1152 RegisterSet* register_set = locations->GetLiveRegisters();
1153 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1154 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1155 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1156 if (register_set->ContainsCoreRegister(i)) {
1157 // If the register holds an object, update the stack mask.
1158 if (locations->RegisterContainsObject(i)) {
1159 locations->SetStackBit(stack_offset / kVRegSize);
1160 }
1161 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001162 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1163 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001164 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
1165 }
1166 }
1167 }
1168
1169 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1170 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1171 if (register_set->ContainsFloatingPointRegister(i)) {
1172 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001173 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1174 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001175 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
1176 }
1177 }
1178 }
1179}
1180
1181void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1182 RegisterSet* register_set = locations->GetLiveRegisters();
1183 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1184 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1185 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1186 if (register_set->ContainsCoreRegister(i)) {
1187 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1188 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1189 }
1190 }
1191 }
1192
1193 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1194 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1195 if (register_set->ContainsFloatingPointRegister(i)) {
1196 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1197 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1198 }
1199 }
1200 }
1201}
1202
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001203} // namespace art