blob: 4607ebe54818884ed825504bab2cd722f573cf97 [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
19#include "code_generator_arm.h"
Alexandre Rames5319def2014-10-23 10:03:10 +010020#include "code_generator_arm64.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000021#include "code_generator_x86.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010022#include "code_generator_x86_64.h"
Alexey Frunze4dda3372015-06-01 18:31:49 -070023#include "code_generator_mips64.h"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070024#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000025#include "dex/verified_method.h"
26#include "driver/dex_compilation_unit.h"
27#include "gc_map_builder.h"
Alexandre Rameseb7b7392015-06-19 14:47:01 +010028#include "graph_visualizer.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000029#include "leb128.h"
30#include "mapping_table.h"
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010031#include "mirror/array-inl.h"
32#include "mirror/object_array-inl.h"
33#include "mirror/object_reference.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010034#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000036#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000037#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038
39namespace art {
40
Alexandre Rames88c13cd2015-04-14 17:35:39 +010041// Return whether a location is consistent with a type.
42static bool CheckType(Primitive::Type type, Location location) {
43 if (location.IsFpuRegister()
44 || (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresFpuRegister))) {
45 return (type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble);
46 } else if (location.IsRegister() ||
47 (location.IsUnallocated() && (location.GetPolicy() == Location::kRequiresRegister))) {
48 return Primitive::IsIntegralType(type) || (type == Primitive::kPrimNot);
49 } else if (location.IsRegisterPair()) {
50 return type == Primitive::kPrimLong;
51 } else if (location.IsFpuRegisterPair()) {
52 return type == Primitive::kPrimDouble;
53 } else if (location.IsStackSlot()) {
54 return (Primitive::IsIntegralType(type) && type != Primitive::kPrimLong)
55 || (type == Primitive::kPrimFloat)
56 || (type == Primitive::kPrimNot);
57 } else if (location.IsDoubleStackSlot()) {
58 return (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble);
59 } else if (location.IsConstant()) {
60 if (location.GetConstant()->IsIntConstant()) {
61 return Primitive::IsIntegralType(type) && (type != Primitive::kPrimLong);
62 } else if (location.GetConstant()->IsNullConstant()) {
63 return type == Primitive::kPrimNot;
64 } else if (location.GetConstant()->IsLongConstant()) {
65 return type == Primitive::kPrimLong;
66 } else if (location.GetConstant()->IsFloatConstant()) {
67 return type == Primitive::kPrimFloat;
68 } else {
69 return location.GetConstant()->IsDoubleConstant()
70 && (type == Primitive::kPrimDouble);
71 }
72 } else {
73 return location.IsInvalid() || (location.GetPolicy() == Location::kAny);
74 }
75}
76
77// Check that a location summary is consistent with an instruction.
78static bool CheckTypeConsistency(HInstruction* instruction) {
79 LocationSummary* locations = instruction->GetLocations();
80 if (locations == nullptr) {
81 return true;
82 }
83
84 if (locations->Out().IsUnallocated()
85 && (locations->Out().GetPolicy() == Location::kSameAsFirstInput)) {
86 DCHECK(CheckType(instruction->GetType(), locations->InAt(0)))
87 << instruction->GetType()
88 << " " << locations->InAt(0);
89 } else {
90 DCHECK(CheckType(instruction->GetType(), locations->Out()))
91 << instruction->GetType()
92 << " " << locations->Out();
93 }
94
95 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
96 DCHECK(CheckType(instruction->InputAt(i)->GetType(), locations->InAt(i)))
97 << instruction->InputAt(i)->GetType()
98 << " " << locations->InAt(i);
99 }
100
101 HEnvironment* environment = instruction->GetEnvironment();
102 for (size_t i = 0; i < instruction->EnvironmentSize(); ++i) {
103 if (environment->GetInstructionAt(i) != nullptr) {
104 Primitive::Type type = environment->GetInstructionAt(i)->GetType();
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100105 DCHECK(CheckType(type, environment->GetLocationAt(i)))
106 << type << " " << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100107 } else {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100108 DCHECK(environment->GetLocationAt(i).IsInvalid())
109 << environment->GetLocationAt(i);
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100110 }
111 }
112 return true;
113}
114
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +0100115size_t CodeGenerator::GetCacheOffset(uint32_t index) {
116 return mirror::ObjectArray<mirror::Object>::OffsetOfElement(index).SizeValue();
117}
118
Mathieu Chartiere401d142015-04-22 13:56:20 -0700119size_t CodeGenerator::GetCachePointerOffset(uint32_t index) {
120 auto pointer_size = InstructionSetPointerSize(GetInstructionSet());
121 return mirror::Array::DataOffset(pointer_size).Uint32Value() + pointer_size * index;
122}
123
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100124void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000125 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +0100126 if (!is_leaf) {
127 MarkNotLeaf();
128 }
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700129 const bool is_64_bit = Is64BitInstructionSet(GetInstructionSet());
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000130 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
131 + GetGraph()->GetTemporariesVRegSlots()
132 + 1 /* filler */,
133 0, /* the baseline compiler does not have live registers at slow path */
134 0, /* the baseline compiler does not have live registers at slow path */
135 GetGraph()->GetMaximumNumberOfOutVRegs()
Mathieu Chartiere3b034a2015-05-31 14:29:23 -0700136 + (is_64_bit ? 2 : 1) /* current method */,
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000137 GetGraph()->GetBlocks());
138 CompileInternal(allocator, /* is_baseline */ true);
139}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100140
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000141bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
142 DCHECK_EQ(block_order_->Get(current_block_index_), current);
143 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
144}
145
146HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
147 for (size_t i = current_block_index_ + 1; i < block_order_->Size(); ++i) {
148 HBasicBlock* block = block_order_->Get(i);
David Brazdilfc6a86a2015-06-26 10:33:45 +0000149 if (!block->IsSingleJump()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000150 return block;
151 }
152 }
153 return nullptr;
154}
155
156HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdilfc6a86a2015-06-26 10:33:45 +0000157 while (block->IsSingleJump()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000158 block = block->GetSuccessors().Get(0);
159 }
160 return block;
161}
162
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100163class DisassemblyScope {
164 public:
165 DisassemblyScope(HInstruction* instruction, const CodeGenerator& codegen)
166 : codegen_(codegen), instruction_(instruction), start_offset_(static_cast<size_t>(-1)) {
167 if (codegen_.GetDisassemblyInformation() != nullptr) {
168 start_offset_ = codegen_.GetAssembler().CodeSize();
169 }
170 }
171
172 ~DisassemblyScope() {
173 // We avoid building this data when we know it will not be used.
174 if (codegen_.GetDisassemblyInformation() != nullptr) {
175 codegen_.GetDisassemblyInformation()->AddInstructionInterval(
176 instruction_, start_offset_, codegen_.GetAssembler().CodeSize());
177 }
178 }
179
180 private:
181 const CodeGenerator& codegen_;
182 HInstruction* instruction_;
183 size_t start_offset_;
184};
185
186
187void CodeGenerator::GenerateSlowPaths() {
188 size_t code_start = 0;
189 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
190 if (disasm_info_ != nullptr) {
191 code_start = GetAssembler()->CodeSize();
192 }
193 slow_paths_.Get(i)->EmitNativeCode(this);
194 if (disasm_info_ != nullptr) {
195 disasm_info_->AddSlowPathInterval(slow_paths_.Get(i), code_start, GetAssembler()->CodeSize());
196 }
197 }
198}
199
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000200void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Roland Levillain3e3d7332015-04-28 11:00:54 +0100201 is_baseline_ = is_baseline;
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100202 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000203 DCHECK_EQ(current_block_index_, 0u);
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100204
205 size_t frame_start = GetAssembler()->CodeSize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000206 GenerateFrameEntry();
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100207 DCHECK_EQ(GetAssembler()->cfi().GetCurrentCFAOffset(), static_cast<int>(frame_size_));
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100208 if (disasm_info_ != nullptr) {
209 disasm_info_->SetFrameEntryInterval(frame_start, GetAssembler()->CodeSize());
210 }
211
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000212 for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) {
213 HBasicBlock* block = block_order_->Get(current_block_index_);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +0000214 // Don't generate code for an empty block. Its predecessors will branch to its successor
215 // directly. Also, the label of that block will not be emitted, so this helps catch
216 // errors where we reference that label.
David Brazdilfc6a86a2015-06-26 10:33:45 +0000217 if (block->IsSingleJump()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100218 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100219 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
220 HInstruction* current = it.Current();
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100221 DisassemblyScope disassembly_scope(current, *this);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000222 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000223 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000224 }
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100225 DCHECK(CheckTypeConsistency(current));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100226 current->Accept(instruction_visitor);
227 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000228 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000229
Alexandre Rameseb7b7392015-06-19 14:47:01 +0100230 GenerateSlowPaths();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000231
232 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000233 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000234}
235
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100236void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000237 // The register allocator already called `InitializeCodeGeneration`,
238 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000239 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100240 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000241 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000242}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100243
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000244void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100245 size_t code_size = GetAssembler()->CodeSize();
246 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000247
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100248 MemoryRegion code(buffer, code_size);
249 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000250}
251
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100252size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
253 for (size_t i = 0; i < length; ++i) {
254 if (!array[i]) {
255 array[i] = true;
256 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100257 }
258 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100259 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000260 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000261}
262
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000263size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
264 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000265 if (!array[i] && !array[i + 1]) {
266 array[i] = true;
267 array[i + 1] = true;
268 return i;
269 }
270 }
271 LOG(FATAL) << "Could not find a register in baseline register allocator";
272 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100273}
274
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000275void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
276 size_t maximum_number_of_live_core_registers,
277 size_t maximum_number_of_live_fp_registers,
278 size_t number_of_out_slots,
279 const GrowableArray<HBasicBlock*>& block_order) {
280 block_order_ = &block_order;
281 DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock());
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000282 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100283 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
284
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000285 if (number_of_spill_slots == 0
286 && !HasAllocatedCalleeSaveRegisters()
287 && IsLeafMethod()
288 && !RequiresCurrentMethod()) {
289 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
290 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
291 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
292 } else {
293 SetFrameSize(RoundUp(
294 number_of_spill_slots * kVRegSize
295 + number_of_out_slots * kVRegSize
296 + maximum_number_of_live_core_registers * GetWordSize()
297 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
298 + FrameEntrySpillSize(),
299 kStackAlignment));
300 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100301}
302
303Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
304 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000305 // The type of the previous instruction tells us if we need a single or double stack slot.
306 Primitive::Type type = temp->GetType();
307 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100308 // Use the temporary region (right below the dex registers).
309 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
310 - kVRegSize // filler
311 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000312 - ((temp_size + temp->GetIndex()) * kVRegSize);
313 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100314}
315
316int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
317 uint16_t reg_number = local->GetRegNumber();
318 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
319 if (reg_number >= number_of_locals) {
320 // Local is a parameter of the method. It is stored in the caller's frame.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700321 // TODO: Share this logic with StackVisitor::GetVRegOffsetFromQuickCode.
322 return GetFrameSize() + InstructionSetPointerSize(GetInstructionSet()) // ART method
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100323 + (reg_number - number_of_locals) * kVRegSize;
324 } else {
325 // Local is a temporary in this method. It is stored in this method's frame.
326 return GetFrameSize() - FrameEntrySpillSize()
327 - kVRegSize // filler.
328 - (number_of_locals * kVRegSize)
329 + (reg_number * kVRegSize);
330 }
331}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100332
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100333void CodeGenerator::CreateCommonInvokeLocationSummary(
Nicolas Geoffray4e40c262015-06-03 12:02:38 +0100334 HInvoke* invoke, InvokeDexCallingConventionVisitor* visitor) {
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100335 ArenaAllocator* allocator = invoke->GetBlock()->GetGraph()->GetArena();
336 LocationSummary* locations = new (allocator) LocationSummary(invoke, LocationSummary::kCall);
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100337
338 for (size_t i = 0; i < invoke->GetNumberOfArguments(); i++) {
339 HInstruction* input = invoke->InputAt(i);
340 locations->SetInAt(i, visitor->GetNextLocation(input->GetType()));
341 }
342
343 locations->SetOut(visitor->GetReturnLocation(invoke->GetType()));
Nicolas Geoffray94015b92015-06-04 18:21:04 +0100344
345 if (invoke->IsInvokeStaticOrDirect()) {
346 HInvokeStaticOrDirect* call = invoke->AsInvokeStaticOrDirect();
347 if (call->IsStringInit()) {
348 locations->AddTemp(visitor->GetMethodLocation());
349 } else if (call->IsRecursive()) {
350 locations->SetInAt(call->GetCurrentMethodInputIndex(), visitor->GetMethodLocation());
351 } else {
352 locations->AddTemp(visitor->GetMethodLocation());
353 locations->SetInAt(call->GetCurrentMethodInputIndex(), Location::RequiresRegister());
354 }
355 } else {
356 locations->AddTemp(visitor->GetMethodLocation());
357 }
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100358}
359
Mark Mendell5f874182015-03-04 15:42:45 -0500360void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
361 // The DCHECKS below check that a register is not specified twice in
362 // the summary. The out location can overlap with an input, so we need
363 // to special case it.
364 if (location.IsRegister()) {
365 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
366 blocked_core_registers_[location.reg()] = true;
367 } else if (location.IsFpuRegister()) {
368 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
369 blocked_fpu_registers_[location.reg()] = true;
370 } else if (location.IsFpuRegisterPair()) {
371 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
372 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
373 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
374 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
375 } else if (location.IsRegisterPair()) {
376 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
377 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
378 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
379 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
380 }
381}
382
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100383void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
384 LocationSummary* locations = instruction->GetLocations();
385 if (locations == nullptr) return;
386
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100387 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
388 blocked_core_registers_[i] = false;
389 }
390
391 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
392 blocked_fpu_registers_[i] = false;
393 }
394
395 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
396 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100397 }
398
399 // Mark all fixed input, temp and output registers as used.
400 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500401 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100402 }
403
404 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
405 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500406 BlockIfInRegister(loc);
407 }
408 Location result_location = locations->Out();
409 if (locations->OutputCanOverlapWithInputs()) {
410 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100411 }
412
Mark Mendell5f874182015-03-04 15:42:45 -0500413 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100414
415 // Allocate all unallocated input locations.
416 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
417 Location loc = locations->InAt(i);
418 HInstruction* input = instruction->InputAt(i);
419 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100420 if ((loc.GetPolicy() == Location::kRequiresRegister)
421 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100422 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100423 } else {
424 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
425 HLoadLocal* load = input->AsLoadLocal();
426 if (load != nullptr) {
427 loc = GetStackLocation(load);
428 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100429 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100430 }
431 }
432 locations->SetInAt(i, loc);
433 }
434 }
435
436 // Allocate all unallocated temp locations.
437 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
438 Location loc = locations->GetTemp(i);
439 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000440 switch (loc.GetPolicy()) {
441 case Location::kRequiresRegister:
442 // Allocate a core register (large enough to fit a 32-bit integer).
443 loc = AllocateFreeRegister(Primitive::kPrimInt);
444 break;
445
446 case Location::kRequiresFpuRegister:
447 // Allocate a core register (large enough to fit a 64-bit double).
448 loc = AllocateFreeRegister(Primitive::kPrimDouble);
449 break;
450
451 default:
452 LOG(FATAL) << "Unexpected policy for temporary location "
453 << loc.GetPolicy();
454 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100455 locations->SetTempAt(i, loc);
456 }
457 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100458 if (result_location.IsUnallocated()) {
459 switch (result_location.GetPolicy()) {
460 case Location::kAny:
461 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100462 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100463 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100464 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100465 case Location::kSameAsFirstInput:
466 result_location = locations->InAt(0);
467 break;
468 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000469 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100470 }
471}
472
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000473void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
474 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100475 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100476 if (instruction->IsTemporary()) {
477 HInstruction* previous = instruction->GetPrevious();
478 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
479 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100480 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100481 return;
482 }
483 AllocateRegistersLocally(instruction);
484 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000485 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000486 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000487 if (location.IsValid()) {
488 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000489 if (input->GetNext()->IsTemporary()) {
490 // If the input was stored in a temporary, use that temporary to
491 // perform the move.
492 Move(input->GetNext(), location, instruction);
493 } else {
494 Move(input, location, instruction);
495 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000496 }
497 }
498}
499
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000500void CodeGenerator::AllocateLocations(HInstruction* instruction) {
501 instruction->Accept(GetLocationBuilder());
Alexandre Rames88c13cd2015-04-14 17:35:39 +0100502 DCHECK(CheckTypeConsistency(instruction));
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000503 LocationSummary* locations = instruction->GetLocations();
504 if (!instruction->IsSuspendCheckEntry()) {
505 if (locations != nullptr && locations->CanCall()) {
506 MarkNotLeaf();
507 }
508 if (instruction->NeedsCurrentMethod()) {
509 SetRequiresCurrentMethod();
510 }
511 }
512}
513
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000514CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000515 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000516 const InstructionSetFeatures& isa_features,
517 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000518 switch (instruction_set) {
519 case kArm:
520 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000521 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000522 *isa_features.AsArmInstructionSetFeatures(),
523 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000524 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100525 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000526 return new arm64::CodeGeneratorARM64(graph,
527 *isa_features.AsArm64InstructionSetFeatures(),
528 compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100529 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000530 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000531 return nullptr;
Alexey Frunze4dda3372015-06-01 18:31:49 -0700532 case kMips64: {
533 return new mips64::CodeGeneratorMIPS64(graph,
534 *isa_features.AsMips64InstructionSetFeatures(),
535 compiler_options);
536 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000537 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400538 return new x86::CodeGeneratorX86(graph,
539 *isa_features.AsX86InstructionSetFeatures(),
540 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000541 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700542 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400543 return new x86_64::CodeGeneratorX86_64(graph,
544 *isa_features.AsX86_64InstructionSetFeatures(),
545 compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700546 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000547 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000548 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000549 }
550}
551
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000552void CodeGenerator::BuildNativeGCMap(
553 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
554 const std::vector<uint8_t>& gc_map_raw =
555 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
556 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
557
Vladimir Markobd8c7252015-06-12 10:06:32 +0100558 uint32_t max_native_offset = stack_map_stream_.ComputeMaxNativePcOffset();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000559
Vladimir Markobd8c7252015-06-12 10:06:32 +0100560 size_t num_stack_maps = stack_map_stream_.GetNumberOfStackMaps();
561 GcMapBuilder builder(data, num_stack_maps, max_native_offset, dex_gc_map.RegWidth());
562 for (size_t i = 0; i != num_stack_maps; ++i) {
563 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
564 uint32_t native_offset = stack_map_entry.native_pc_offset;
565 uint32_t dex_pc = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000566 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800567 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000568 builder.AddEntry(native_offset, references);
569 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000570}
571
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100572void CodeGenerator::BuildSourceMap(DefaultSrcMap* src_map) const {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100573 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
574 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
575 uint32_t pc2dex_offset = stack_map_entry.native_pc_offset;
576 int32_t pc2dex_dalvik_offset = stack_map_entry.dex_pc;
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100577 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
578 }
579}
580
581void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000582 uint32_t pc2dex_data_size = 0u;
Vladimir Markobd8c7252015-06-12 10:06:32 +0100583 uint32_t pc2dex_entries = stack_map_stream_.GetNumberOfStackMaps();
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000584 uint32_t pc2dex_offset = 0u;
585 int32_t pc2dex_dalvik_offset = 0;
586 uint32_t dex2pc_data_size = 0u;
587 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000588 uint32_t dex2pc_offset = 0u;
589 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000590
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000591 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100592 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
593 pc2dex_data_size += UnsignedLeb128Size(stack_map_entry.native_pc_offset - pc2dex_offset);
594 pc2dex_data_size += SignedLeb128Size(stack_map_entry.dex_pc - pc2dex_dalvik_offset);
595 pc2dex_offset = stack_map_entry.native_pc_offset;
596 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000597 }
598
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000599 // Walk over the blocks and find which ones correspond to catch block entries.
600 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
601 HBasicBlock* block = graph_->GetBlocks().Get(i);
602 if (block->IsCatchBlock()) {
603 intptr_t native_pc = GetAddressOf(block);
604 ++dex2pc_entries;
605 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
606 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
607 dex2pc_offset = native_pc;
608 dex2pc_dalvik_offset = block->GetDexPc();
609 }
610 }
611
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000612 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
613 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
614 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
615 data->resize(data_size);
616
617 uint8_t* data_ptr = &(*data)[0];
618 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000619
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000620 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
621 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
622 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
623 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
624
625 pc2dex_offset = 0u;
626 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000627 dex2pc_offset = 0u;
628 dex2pc_dalvik_offset = 0u;
629
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000630 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100631 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
632 DCHECK(pc2dex_offset <= stack_map_entry.native_pc_offset);
633 write_pos = EncodeUnsignedLeb128(write_pos, stack_map_entry.native_pc_offset - pc2dex_offset);
634 write_pos = EncodeSignedLeb128(write_pos, stack_map_entry.dex_pc - pc2dex_dalvik_offset);
635 pc2dex_offset = stack_map_entry.native_pc_offset;
636 pc2dex_dalvik_offset = stack_map_entry.dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000637 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000638
639 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
640 HBasicBlock* block = graph_->GetBlocks().Get(i);
641 if (block->IsCatchBlock()) {
642 intptr_t native_pc = GetAddressOf(block);
643 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
644 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
645 dex2pc_offset = native_pc;
646 dex2pc_dalvik_offset = block->GetDexPc();
647 }
648 }
649
650
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000651 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
652 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
653
654 if (kIsDebugBuild) {
655 // Verify the encoded table holds the expected data.
656 MappingTable table(data_ptr);
657 CHECK_EQ(table.TotalSize(), total_entries);
658 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
659 auto it = table.PcToDexBegin();
660 auto it2 = table.DexToPcBegin();
661 for (size_t i = 0; i < pc2dex_entries; i++) {
Vladimir Markobd8c7252015-06-12 10:06:32 +0100662 const StackMapStream::StackMapEntry& stack_map_entry = stack_map_stream_.GetStackMap(i);
663 CHECK_EQ(stack_map_entry.native_pc_offset, it.NativePcOffset());
664 CHECK_EQ(stack_map_entry.dex_pc, it.DexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000665 ++it;
666 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000667 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
668 HBasicBlock* block = graph_->GetBlocks().Get(i);
669 if (block->IsCatchBlock()) {
670 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
671 CHECK_EQ(block->GetDexPc(), it2.DexPc());
672 ++it2;
673 }
674 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000675 CHECK(it == table.PcToDexEnd());
676 CHECK(it2 == table.DexToPcEnd());
677 }
678}
679
680void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
681 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100682 // We currently don't use callee-saved registers.
683 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000684 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
685 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000686 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
687
688 *data = vmap_encoder.GetData();
689}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000690
Nicolas Geoffray39468442014-09-02 15:17:15 +0100691void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
Calin Juravle4f46ac52015-04-23 18:47:21 +0100692 uint32_t size = stack_map_stream_.PrepareForFillIn();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100693 data->resize(size);
694 MemoryRegion region(data->data(), size);
695 stack_map_stream_.FillIn(region);
696}
697
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000698void CodeGenerator::RecordPcInfo(HInstruction* instruction,
699 uint32_t dex_pc,
700 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000701 if (instruction != nullptr) {
Alexey Frunze4dda3372015-06-01 18:31:49 -0700702 // The code generated for some type conversions and comparisons
703 // may call the runtime, thus normally requiring a subsequent
704 // call to this method. However, the method verifier does not
705 // produce PC information for certain instructions, which are
706 // considered "atomic" (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000707 // Therefore we do not currently record PC information for such
708 // instructions. As this may change later, we added this special
709 // case so that code generators may nevertheless call
710 // CodeGenerator::RecordPcInfo without triggering an error in
711 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
712 // thereafter.
Alexey Frunze4dda3372015-06-01 18:31:49 -0700713 if (instruction->IsTypeConversion() || instruction->IsCompare()) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000714 return;
715 }
716 if (instruction->IsRem()) {
717 Primitive::Type type = instruction->AsRem()->GetResultType();
718 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
719 return;
720 }
721 }
Roland Levillain624279f2014-12-04 11:54:28 +0000722 }
723
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100724 uint32_t outer_dex_pc = dex_pc;
725 uint32_t outer_environment_size = 0;
726 uint32_t inlining_depth = 0;
727 if (instruction != nullptr) {
728 for (HEnvironment* environment = instruction->GetEnvironment();
729 environment != nullptr;
730 environment = environment->GetParent()) {
731 outer_dex_pc = environment->GetDexPc();
732 outer_environment_size = environment->Size();
733 if (environment != instruction->GetEnvironment()) {
734 inlining_depth++;
735 }
736 }
737 }
738
Nicolas Geoffray39468442014-09-02 15:17:15 +0100739 // Collect PC infos for the mapping table.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100740 uint32_t native_pc = GetAssembler()->CodeSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100741
Nicolas Geoffray39468442014-09-02 15:17:15 +0100742 if (instruction == nullptr) {
743 // For stack overflow checks.
Vladimir Markobd8c7252015-06-12 10:06:32 +0100744 stack_map_stream_.BeginStackMapEntry(outer_dex_pc, native_pc, 0, 0, 0, 0);
Calin Juravle4f46ac52015-04-23 18:47:21 +0100745 stack_map_stream_.EndStackMapEntry();
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000746 return;
747 }
748 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100749
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000750 uint32_t register_mask = locations->GetRegisterMask();
751 if (locations->OnlyCallsOnSlowPath()) {
752 // In case of slow path, we currently set the location of caller-save registers
753 // to register (instead of their stack location when pushed before the slow-path
754 // call). Therefore register_mask contains both callee-save and caller-save
755 // registers that hold objects. We must remove the caller-save from the mask, since
756 // they will be overwritten by the callee.
757 register_mask &= core_callee_save_mask_;
758 }
759 // The register mask must be a subset of callee-save registers.
760 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
Vladimir Markobd8c7252015-06-12 10:06:32 +0100761 stack_map_stream_.BeginStackMapEntry(outer_dex_pc,
762 native_pc,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100763 register_mask,
764 locations->GetStackMask(),
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100765 outer_environment_size,
Calin Juravle4f46ac52015-04-23 18:47:21 +0100766 inlining_depth);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100767
768 EmitEnvironment(instruction->GetEnvironment(), slow_path);
769 stack_map_stream_.EndStackMapEntry();
770}
771
772void CodeGenerator::EmitEnvironment(HEnvironment* environment, SlowPathCode* slow_path) {
773 if (environment == nullptr) return;
774
775 if (environment->GetParent() != nullptr) {
776 // We emit the parent environment first.
777 EmitEnvironment(environment->GetParent(), slow_path);
Nicolas Geoffrayb176d7c2015-05-20 18:48:31 +0100778 stack_map_stream_.BeginInlineInfoEntry(environment->GetMethodIdx(),
779 environment->GetDexPc(),
780 environment->GetInvokeType(),
781 environment->Size());
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100782 }
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000783
784 // Walk over the environment, and record the location of dex registers.
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100785 for (size_t i = 0, environment_size = environment->Size(); i < environment_size; ++i) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000786 HInstruction* current = environment->GetInstructionAt(i);
787 if (current == nullptr) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100788 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000789 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100790 }
791
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100792 Location location = environment->GetLocationAt(i);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000793 switch (location.GetKind()) {
794 case Location::kConstant: {
795 DCHECK_EQ(current, location.GetConstant());
796 if (current->IsLongConstant()) {
797 int64_t value = current->AsLongConstant()->GetValue();
798 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100799 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000800 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100801 DexRegisterLocation::Kind::kConstant, High32Bits(value));
802 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000803 DCHECK_LT(i, environment_size);
804 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000805 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000806 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100807 DexRegisterLocation::Kind::kConstant, Low32Bits(value));
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000808 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100809 DexRegisterLocation::Kind::kConstant, High32Bits(value));
810 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000811 DCHECK_LT(i, environment_size);
812 } else if (current->IsIntConstant()) {
813 int32_t value = current->AsIntConstant()->GetValue();
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100814 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000815 } else if (current->IsNullConstant()) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100816 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000817 } else {
818 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000819 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100820 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kConstant, value);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000821 }
822 break;
823 }
824
825 case Location::kStackSlot: {
826 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100827 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000828 break;
829 }
830
831 case Location::kDoubleStackSlot: {
832 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100833 DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000834 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100835 DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
836 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000837 DCHECK_LT(i, environment_size);
838 break;
839 }
840
841 case Location::kRegister : {
842 int id = location.reg();
843 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
844 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100845 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000846 if (current->GetType() == Primitive::kPrimLong) {
847 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100848 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
849 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000850 DCHECK_LT(i, environment_size);
851 }
852 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100853 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000854 if (current->GetType() == Primitive::kPrimLong) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100855 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, id);
856 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000857 DCHECK_LT(i, environment_size);
858 }
859 }
860 break;
861 }
862
863 case Location::kFpuRegister : {
864 int id = location.reg();
865 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
866 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100867 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000868 if (current->GetType() == Primitive::kPrimDouble) {
869 stack_map_stream_.AddDexRegisterEntry(
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100870 DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
871 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000872 DCHECK_LT(i, environment_size);
873 }
874 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100875 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000876 if (current->GetType() == Primitive::kPrimDouble) {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100877 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, id);
878 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000879 DCHECK_LT(i, environment_size);
880 }
881 }
882 break;
883 }
884
885 case Location::kFpuRegisterPair : {
886 int low = location.low();
887 int high = location.high();
888 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
889 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100890 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000891 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100892 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000893 }
894 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
895 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100896 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
897 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000898 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100899 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInFpuRegister, high);
900 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000901 }
902 DCHECK_LT(i, environment_size);
903 break;
904 }
905
906 case Location::kRegisterPair : {
907 int low = location.low();
908 int high = location.high();
909 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
910 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100911 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000912 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100913 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, low);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000914 }
915 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
916 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100917 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInStack, offset);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000918 } else {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100919 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kInRegister, high);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000920 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100921 ++i;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000922 DCHECK_LT(i, environment_size);
923 break;
924 }
925
926 case Location::kInvalid: {
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100927 stack_map_stream_.AddDexRegisterEntry(DexRegisterLocation::Kind::kNone, 0);
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000928 break;
929 }
930
931 default:
932 LOG(FATAL) << "Unexpected kind " << location.GetKind();
933 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100934 }
Nicolas Geoffrayb1d0f3f2015-05-14 12:41:51 +0100935
936 if (environment->GetParent() != nullptr) {
937 stack_map_stream_.EndInlineInfoEntry();
938 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100939}
940
Calin Juravle77520bc2015-01-12 18:45:46 +0000941bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
942 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
Calin Juravle641547a2015-04-21 22:08:51 +0100943
944 return (first_next_not_move != nullptr)
945 && first_next_not_move->CanDoImplicitNullCheckOn(null_check->InputAt(0));
Calin Juravle77520bc2015-01-12 18:45:46 +0000946}
947
948void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
949 // If we are from a static path don't record the pc as we can't throw NPE.
950 // NB: having the checks here makes the code much less verbose in the arch
951 // specific code generators.
952 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
953 return;
954 }
955
956 if (!compiler_options_.GetImplicitNullChecks()) {
957 return;
958 }
959
Calin Juravle641547a2015-04-21 22:08:51 +0100960 if (!instr->CanDoImplicitNullCheckOn(instr->InputAt(0))) {
Calin Juravle77520bc2015-01-12 18:45:46 +0000961 return;
962 }
963
964 // Find the first previous instruction which is not a move.
965 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
966
967 // If the instruction is a null check it means that `instr` is the first user
968 // and needs to record the pc.
969 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
970 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
971 // TODO: The parallel moves modify the environment. Their changes need to be reverted
972 // otherwise the stack maps at the throw point will not be correct.
973 RecordPcInfo(null_check, null_check->GetDexPc());
974 }
975}
976
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100977void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
978 LocationSummary* locations = suspend_check->GetLocations();
979 HBasicBlock* block = suspend_check->GetBlock();
980 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
981 DCHECK(block->IsLoopHeader());
982
983 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
984 HInstruction* current = it.Current();
985 LiveInterval* interval = current->GetLiveInterval();
986 // We only need to clear bits of loop phis containing objects and allocated in register.
987 // Loop phis allocated on stack already have the object in the stack.
988 if (current->GetType() == Primitive::kPrimNot
989 && interval->HasRegister()
990 && interval->HasSpillSlot()) {
991 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
992 }
993 }
994}
995
Nicolas Geoffray90218252015-04-15 11:56:51 +0100996void CodeGenerator::EmitParallelMoves(Location from1,
997 Location to1,
998 Primitive::Type type1,
999 Location from2,
1000 Location to2,
1001 Primitive::Type type2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001002 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray90218252015-04-15 11:56:51 +01001003 parallel_move.AddMove(from1, to1, type1, nullptr);
1004 parallel_move.AddMove(from2, to2, type2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +00001005 GetMoveResolver()->EmitNativeCode(&parallel_move);
1006}
1007
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001008void SlowPathCode::RecordPcInfo(CodeGenerator* codegen, HInstruction* instruction, uint32_t dex_pc) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001009 codegen->RecordPcInfo(instruction, dex_pc, this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001010}
1011
1012void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1013 RegisterSet* register_set = locations->GetLiveRegisters();
1014 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1015 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1016 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1017 if (register_set->ContainsCoreRegister(i)) {
1018 // If the register holds an object, update the stack mask.
1019 if (locations->RegisterContainsObject(i)) {
1020 locations->SetStackBit(stack_offset / kVRegSize);
1021 }
1022 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001023 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1024 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001025 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
1026 }
1027 }
1028 }
1029
1030 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1031 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1032 if (register_set->ContainsFloatingPointRegister(i)) {
1033 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +00001034 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
1035 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +00001036 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
1037 }
1038 }
1039 }
1040}
1041
1042void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
1043 RegisterSet* register_set = locations->GetLiveRegisters();
1044 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
1045 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1046 if (!codegen->IsCoreCalleeSaveRegister(i)) {
1047 if (register_set->ContainsCoreRegister(i)) {
1048 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1049 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
1050 }
1051 }
1052 }
1053
1054 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
1055 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
1056 if (register_set->ContainsFloatingPointRegister(i)) {
1057 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
1058 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
1059 }
1060 }
1061 }
1062}
1063
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001064} // namespace art