blob: da28dc7ecbdf18b0c8518e29675e7d4fd4cdc543 [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"
Yevgeny Roubane3ea8382014-08-08 16:29:38 +070023#include "compiled_method.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000024#include "dex/verified_method.h"
25#include "driver/dex_compilation_unit.h"
26#include "gc_map_builder.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000027#include "leb128.h"
28#include "mapping_table.h"
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010029#include "mirror/array-inl.h"
30#include "mirror/object_array-inl.h"
31#include "mirror/object_reference.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010032#include "ssa_liveness_analysis.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/assembler.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000034#include "verifier/dex_gc_map.h"
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +000035#include "vmap_table.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036
37namespace art {
38
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +010039size_t CodeGenerator::GetCacheOffset(uint32_t index) {
40 return mirror::ObjectArray<mirror::Object>::OffsetOfElement(index).SizeValue();
41}
42
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010043void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000044 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010045 if (!is_leaf) {
46 MarkNotLeaf();
47 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000048 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
49 + GetGraph()->GetTemporariesVRegSlots()
50 + 1 /* filler */,
51 0, /* the baseline compiler does not have live registers at slow path */
52 0, /* the baseline compiler does not have live registers at slow path */
53 GetGraph()->GetMaximumNumberOfOutVRegs()
54 + 1 /* current method */,
55 GetGraph()->GetBlocks());
56 CompileInternal(allocator, /* is_baseline */ true);
57}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010058
Nicolas Geoffraydc23d832015-02-16 11:15:43 +000059bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
60 DCHECK_EQ(block_order_->Get(current_block_index_), current);
61 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
62}
63
64HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
65 for (size_t i = current_block_index_ + 1; i < block_order_->Size(); ++i) {
66 HBasicBlock* block = block_order_->Get(i);
David Brazdil46e2a392015-03-16 17:31:52 +000067 if (!block->IsSingleGoto()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +000068 return block;
69 }
70 }
71 return nullptr;
72}
73
74HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
David Brazdil46e2a392015-03-16 17:31:52 +000075 while (block->IsSingleGoto()) {
Nicolas Geoffraydc23d832015-02-16 11:15:43 +000076 block = block->GetSuccessors().Get(0);
77 }
78 return block;
79}
80
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000081void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010082 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000083 DCHECK_EQ(current_block_index_, 0u);
84 GenerateFrameEntry();
85 for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) {
86 HBasicBlock* block = block_order_->Get(current_block_index_);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +000087 // Don't generate code for an empty block. Its predecessors will branch to its successor
88 // directly. Also, the label of that block will not be emitted, so this helps catch
89 // errors where we reference that label.
David Brazdil46e2a392015-03-16 17:31:52 +000090 if (block->IsSingleGoto()) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010091 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010092 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
93 HInstruction* current = it.Current();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000094 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +000095 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000096 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010097 current->Accept(instruction_visitor);
98 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000099 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000100
101 // Generate the slow paths.
102 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
103 slow_paths_.Get(i)->EmitNativeCode(this);
104 }
105
106 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000107 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000108}
109
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100110void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000111 // The register allocator already called `InitializeCodeGeneration`,
112 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000113 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100114 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000115 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000116}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100117
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000118void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100119 size_t code_size = GetAssembler()->CodeSize();
120 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000121
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100122 MemoryRegion code(buffer, code_size);
123 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000124}
125
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100126size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
127 for (size_t i = 0; i < length; ++i) {
128 if (!array[i]) {
129 array[i] = true;
130 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100131 }
132 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100133 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000134 UNREACHABLE();
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000135}
136
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000137size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
138 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000139 if (!array[i] && !array[i + 1]) {
140 array[i] = true;
141 array[i + 1] = true;
142 return i;
143 }
144 }
145 LOG(FATAL) << "Could not find a register in baseline register allocator";
146 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100147}
148
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000149void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
150 size_t maximum_number_of_live_core_registers,
151 size_t maximum_number_of_live_fp_registers,
152 size_t number_of_out_slots,
153 const GrowableArray<HBasicBlock*>& block_order) {
154 block_order_ = &block_order;
155 DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock());
156 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), block_order_->Get(1)));
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000157 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100158 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
159
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000160 if (number_of_spill_slots == 0
161 && !HasAllocatedCalleeSaveRegisters()
162 && IsLeafMethod()
163 && !RequiresCurrentMethod()) {
164 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
165 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
166 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
167 } else {
168 SetFrameSize(RoundUp(
169 number_of_spill_slots * kVRegSize
170 + number_of_out_slots * kVRegSize
171 + maximum_number_of_live_core_registers * GetWordSize()
172 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
173 + FrameEntrySpillSize(),
174 kStackAlignment));
175 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100176}
177
178Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
179 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000180 // The type of the previous instruction tells us if we need a single or double stack slot.
181 Primitive::Type type = temp->GetType();
182 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100183 // Use the temporary region (right below the dex registers).
184 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
185 - kVRegSize // filler
186 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000187 - ((temp_size + temp->GetIndex()) * kVRegSize);
188 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100189}
190
191int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
192 uint16_t reg_number = local->GetRegNumber();
193 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
194 if (reg_number >= number_of_locals) {
195 // Local is a parameter of the method. It is stored in the caller's frame.
196 return GetFrameSize() + kVRegSize // ART method
197 + (reg_number - number_of_locals) * kVRegSize;
198 } else {
199 // Local is a temporary in this method. It is stored in this method's frame.
200 return GetFrameSize() - FrameEntrySpillSize()
201 - kVRegSize // filler.
202 - (number_of_locals * kVRegSize)
203 + (reg_number * kVRegSize);
204 }
205}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100206
Mark Mendell5f874182015-03-04 15:42:45 -0500207void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
208 // The DCHECKS below check that a register is not specified twice in
209 // the summary. The out location can overlap with an input, so we need
210 // to special case it.
211 if (location.IsRegister()) {
212 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
213 blocked_core_registers_[location.reg()] = true;
214 } else if (location.IsFpuRegister()) {
215 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
216 blocked_fpu_registers_[location.reg()] = true;
217 } else if (location.IsFpuRegisterPair()) {
218 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
219 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
220 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
221 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
222 } else if (location.IsRegisterPair()) {
223 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
224 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
225 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
226 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
227 }
228}
229
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100230void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
231 LocationSummary* locations = instruction->GetLocations();
232 if (locations == nullptr) return;
233
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100234 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
235 blocked_core_registers_[i] = false;
236 }
237
238 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
239 blocked_fpu_registers_[i] = false;
240 }
241
242 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
243 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100244 }
245
246 // Mark all fixed input, temp and output registers as used.
247 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500248 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100249 }
250
251 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
252 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500253 BlockIfInRegister(loc);
254 }
255 Location result_location = locations->Out();
256 if (locations->OutputCanOverlapWithInputs()) {
257 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100258 }
259
Mark Mendell5f874182015-03-04 15:42:45 -0500260 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100261
262 // Allocate all unallocated input locations.
263 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
264 Location loc = locations->InAt(i);
265 HInstruction* input = instruction->InputAt(i);
266 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100267 if ((loc.GetPolicy() == Location::kRequiresRegister)
268 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100269 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100270 } else {
271 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
272 HLoadLocal* load = input->AsLoadLocal();
273 if (load != nullptr) {
274 loc = GetStackLocation(load);
275 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100276 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100277 }
278 }
279 locations->SetInAt(i, loc);
280 }
281 }
282
283 // Allocate all unallocated temp locations.
284 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
285 Location loc = locations->GetTemp(i);
286 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000287 switch (loc.GetPolicy()) {
288 case Location::kRequiresRegister:
289 // Allocate a core register (large enough to fit a 32-bit integer).
290 loc = AllocateFreeRegister(Primitive::kPrimInt);
291 break;
292
293 case Location::kRequiresFpuRegister:
294 // Allocate a core register (large enough to fit a 64-bit double).
295 loc = AllocateFreeRegister(Primitive::kPrimDouble);
296 break;
297
298 default:
299 LOG(FATAL) << "Unexpected policy for temporary location "
300 << loc.GetPolicy();
301 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100302 locations->SetTempAt(i, loc);
303 }
304 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100305 if (result_location.IsUnallocated()) {
306 switch (result_location.GetPolicy()) {
307 case Location::kAny:
308 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100309 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100310 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100311 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100312 case Location::kSameAsFirstInput:
313 result_location = locations->InAt(0);
314 break;
315 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000316 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100317 }
318}
319
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000320void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
321 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100322 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100323 if (instruction->IsTemporary()) {
324 HInstruction* previous = instruction->GetPrevious();
325 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
326 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100327 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100328 return;
329 }
330 AllocateRegistersLocally(instruction);
331 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000332 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000333 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000334 if (location.IsValid()) {
335 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000336 if (input->GetNext()->IsTemporary()) {
337 // If the input was stored in a temporary, use that temporary to
338 // perform the move.
339 Move(input->GetNext(), location, instruction);
340 } else {
341 Move(input, location, instruction);
342 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000343 }
344 }
345}
346
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000347void CodeGenerator::AllocateLocations(HInstruction* instruction) {
348 instruction->Accept(GetLocationBuilder());
349 LocationSummary* locations = instruction->GetLocations();
350 if (!instruction->IsSuspendCheckEntry()) {
351 if (locations != nullptr && locations->CanCall()) {
352 MarkNotLeaf();
353 }
354 if (instruction->NeedsCurrentMethod()) {
355 SetRequiresCurrentMethod();
356 }
357 }
358}
359
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000360CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000361 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000362 const InstructionSetFeatures& isa_features,
363 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000364 switch (instruction_set) {
365 case kArm:
366 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000367 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000368 *isa_features.AsArmInstructionSetFeatures(),
369 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000370 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100371 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000372 return new arm64::CodeGeneratorARM64(graph,
373 *isa_features.AsArm64InstructionSetFeatures(),
374 compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100375 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000376 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000377 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000378 case kX86: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400379 return new x86::CodeGeneratorX86(graph,
380 *isa_features.AsX86InstructionSetFeatures(),
381 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000382 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700383 case kX86_64: {
Mark Mendellfb8d2792015-03-31 22:16:59 -0400384 return new x86_64::CodeGeneratorX86_64(graph,
385 *isa_features.AsX86_64InstructionSetFeatures(),
386 compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700387 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000388 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000389 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000390 }
391}
392
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000393void CodeGenerator::BuildNativeGCMap(
394 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
395 const std::vector<uint8_t>& gc_map_raw =
396 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
397 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
398
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000399 uint32_t max_native_offset = 0;
400 for (size_t i = 0; i < pc_infos_.Size(); i++) {
401 uint32_t native_offset = pc_infos_.Get(i).native_pc;
402 if (native_offset > max_native_offset) {
403 max_native_offset = native_offset;
404 }
405 }
406
407 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
408 for (size_t i = 0; i < pc_infos_.Size(); i++) {
409 struct PcInfo pc_info = pc_infos_.Get(i);
410 uint32_t native_offset = pc_info.native_pc;
411 uint32_t dex_pc = pc_info.dex_pc;
412 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800413 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000414 builder.AddEntry(native_offset, references);
415 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000416}
417
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800418void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, DefaultSrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000419 uint32_t pc2dex_data_size = 0u;
420 uint32_t pc2dex_entries = pc_infos_.Size();
421 uint32_t pc2dex_offset = 0u;
422 int32_t pc2dex_dalvik_offset = 0;
423 uint32_t dex2pc_data_size = 0u;
424 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000425 uint32_t dex2pc_offset = 0u;
426 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000427
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700428 if (src_map != nullptr) {
429 src_map->reserve(pc2dex_entries);
430 }
431
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000432 for (size_t i = 0; i < pc2dex_entries; i++) {
433 struct PcInfo pc_info = pc_infos_.Get(i);
434 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
435 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
436 pc2dex_offset = pc_info.native_pc;
437 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700438 if (src_map != nullptr) {
439 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
440 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000441 }
442
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000443 // Walk over the blocks and find which ones correspond to catch block entries.
444 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
445 HBasicBlock* block = graph_->GetBlocks().Get(i);
446 if (block->IsCatchBlock()) {
447 intptr_t native_pc = GetAddressOf(block);
448 ++dex2pc_entries;
449 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
450 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
451 dex2pc_offset = native_pc;
452 dex2pc_dalvik_offset = block->GetDexPc();
453 }
454 }
455
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000456 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
457 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
458 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
459 data->resize(data_size);
460
461 uint8_t* data_ptr = &(*data)[0];
462 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000463
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000464 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
465 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
466 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
467 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
468
469 pc2dex_offset = 0u;
470 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000471 dex2pc_offset = 0u;
472 dex2pc_dalvik_offset = 0u;
473
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000474 for (size_t i = 0; i < pc2dex_entries; i++) {
475 struct PcInfo pc_info = pc_infos_.Get(i);
476 DCHECK(pc2dex_offset <= pc_info.native_pc);
477 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
478 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
479 pc2dex_offset = pc_info.native_pc;
480 pc2dex_dalvik_offset = pc_info.dex_pc;
481 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000482
483 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
484 HBasicBlock* block = graph_->GetBlocks().Get(i);
485 if (block->IsCatchBlock()) {
486 intptr_t native_pc = GetAddressOf(block);
487 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
488 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
489 dex2pc_offset = native_pc;
490 dex2pc_dalvik_offset = block->GetDexPc();
491 }
492 }
493
494
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000495 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
496 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
497
498 if (kIsDebugBuild) {
499 // Verify the encoded table holds the expected data.
500 MappingTable table(data_ptr);
501 CHECK_EQ(table.TotalSize(), total_entries);
502 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
503 auto it = table.PcToDexBegin();
504 auto it2 = table.DexToPcBegin();
505 for (size_t i = 0; i < pc2dex_entries; i++) {
506 struct PcInfo pc_info = pc_infos_.Get(i);
507 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
508 CHECK_EQ(pc_info.dex_pc, it.DexPc());
509 ++it;
510 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000511 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
512 HBasicBlock* block = graph_->GetBlocks().Get(i);
513 if (block->IsCatchBlock()) {
514 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
515 CHECK_EQ(block->GetDexPc(), it2.DexPc());
516 ++it2;
517 }
518 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000519 CHECK(it == table.PcToDexEnd());
520 CHECK(it2 == table.DexToPcEnd());
521 }
522}
523
524void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
525 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100526 // We currently don't use callee-saved registers.
527 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000528 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
529 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000530 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
531
532 *data = vmap_encoder.GetData();
533}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000534
Nicolas Geoffray39468442014-09-02 15:17:15 +0100535void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
536 uint32_t size = stack_map_stream_.ComputeNeededSize();
537 data->resize(size);
538 MemoryRegion region(data->data(), size);
539 stack_map_stream_.FillIn(region);
540}
541
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000542void CodeGenerator::RecordPcInfo(HInstruction* instruction,
543 uint32_t dex_pc,
544 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000545 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000546 // The code generated for some type conversions may call the
547 // runtime, thus normally requiring a subsequent call to this
548 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000549 // information for certain instructions, which are considered "atomic"
550 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000551 // Therefore we do not currently record PC information for such
552 // instructions. As this may change later, we added this special
553 // case so that code generators may nevertheless call
554 // CodeGenerator::RecordPcInfo without triggering an error in
555 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
556 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000557 if (instruction->IsTypeConversion()) {
558 return;
559 }
560 if (instruction->IsRem()) {
561 Primitive::Type type = instruction->AsRem()->GetResultType();
562 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
563 return;
564 }
565 }
Roland Levillain624279f2014-12-04 11:54:28 +0000566 }
567
Nicolas Geoffray39468442014-09-02 15:17:15 +0100568 // Collect PC infos for the mapping table.
569 struct PcInfo pc_info;
570 pc_info.dex_pc = dex_pc;
571 pc_info.native_pc = GetAssembler()->CodeSize();
572 pc_infos_.Add(pc_info);
573
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000574 uint32_t inlining_depth = 0;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000575
Nicolas Geoffray39468442014-09-02 15:17:15 +0100576 if (instruction == nullptr) {
577 // For stack overflow checks.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000578 stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, inlining_depth);
579 return;
580 }
581 LocationSummary* locations = instruction->GetLocations();
582 HEnvironment* environment = instruction->GetEnvironment();
583 size_t environment_size = instruction->EnvironmentSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100584
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000585 uint32_t register_mask = locations->GetRegisterMask();
586 if (locations->OnlyCallsOnSlowPath()) {
587 // In case of slow path, we currently set the location of caller-save registers
588 // to register (instead of their stack location when pushed before the slow-path
589 // call). Therefore register_mask contains both callee-save and caller-save
590 // registers that hold objects. We must remove the caller-save from the mask, since
591 // they will be overwritten by the callee.
592 register_mask &= core_callee_save_mask_;
593 }
594 // The register mask must be a subset of callee-save registers.
595 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
596 stack_map_stream_.AddStackMapEntry(dex_pc,
597 pc_info.native_pc,
598 register_mask,
599 locations->GetStackMask(),
600 environment_size,
601 inlining_depth);
602
603 // Walk over the environment, and record the location of dex registers.
604 for (size_t i = 0; i < environment_size; ++i) {
605 HInstruction* current = environment->GetInstructionAt(i);
606 if (current == nullptr) {
607 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kNone, 0);
608 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100609 }
610
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000611 Location location = locations->GetEnvironmentAt(i);
612 switch (location.GetKind()) {
613 case Location::kConstant: {
614 DCHECK_EQ(current, location.GetConstant());
615 if (current->IsLongConstant()) {
616 int64_t value = current->AsLongConstant()->GetValue();
617 stack_map_stream_.AddDexRegisterEntry(
618 i, DexRegisterLocation::Kind::kConstant, Low32Bits(value));
619 stack_map_stream_.AddDexRegisterEntry(
620 ++i, DexRegisterLocation::Kind::kConstant, High32Bits(value));
621 DCHECK_LT(i, environment_size);
622 } else if (current->IsDoubleConstant()) {
Roland Levillainda4d79b2015-03-24 14:36:11 +0000623 int64_t value = bit_cast<int64_t, double>(current->AsDoubleConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000624 stack_map_stream_.AddDexRegisterEntry(
625 i, DexRegisterLocation::Kind::kConstant, Low32Bits(value));
626 stack_map_stream_.AddDexRegisterEntry(
627 ++i, DexRegisterLocation::Kind::kConstant, High32Bits(value));
628 DCHECK_LT(i, environment_size);
629 } else if (current->IsIntConstant()) {
630 int32_t value = current->AsIntConstant()->GetValue();
631 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, value);
632 } else if (current->IsNullConstant()) {
633 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, 0);
634 } else {
635 DCHECK(current->IsFloatConstant()) << current->DebugName();
Roland Levillainda4d79b2015-03-24 14:36:11 +0000636 int32_t value = bit_cast<int32_t, float>(current->AsFloatConstant()->GetValue());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000637 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, value);
638 }
639 break;
640 }
641
642 case Location::kStackSlot: {
643 stack_map_stream_.AddDexRegisterEntry(
644 i, DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
645 break;
646 }
647
648 case Location::kDoubleStackSlot: {
649 stack_map_stream_.AddDexRegisterEntry(
650 i, DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
651 stack_map_stream_.AddDexRegisterEntry(
652 ++i, DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
653 DCHECK_LT(i, environment_size);
654 break;
655 }
656
657 case Location::kRegister : {
658 int id = location.reg();
659 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
660 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
661 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
662 if (current->GetType() == Primitive::kPrimLong) {
663 stack_map_stream_.AddDexRegisterEntry(
664 ++i, DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
665 DCHECK_LT(i, environment_size);
666 }
667 } else {
668 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInRegister, id);
669 if (current->GetType() == Primitive::kPrimLong) {
670 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInRegister, id);
671 DCHECK_LT(i, environment_size);
672 }
673 }
674 break;
675 }
676
677 case Location::kFpuRegister : {
678 int id = location.reg();
679 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
680 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
681 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
682 if (current->GetType() == Primitive::kPrimDouble) {
683 stack_map_stream_.AddDexRegisterEntry(
684 ++i, DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
685 DCHECK_LT(i, environment_size);
686 }
687 } else {
688 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInFpuRegister, id);
689 if (current->GetType() == Primitive::kPrimDouble) {
690 stack_map_stream_.AddDexRegisterEntry(
691 ++i, DexRegisterLocation::Kind::kInFpuRegister, id);
692 DCHECK_LT(i, environment_size);
693 }
694 }
695 break;
696 }
697
698 case Location::kFpuRegisterPair : {
699 int low = location.low();
700 int high = location.high();
701 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
702 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
703 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
704 } else {
705 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInFpuRegister, low);
706 }
707 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
708 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
709 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInStack, offset);
710 } else {
711 stack_map_stream_.AddDexRegisterEntry(
712 ++i, DexRegisterLocation::Kind::kInFpuRegister, high);
713 }
714 DCHECK_LT(i, environment_size);
715 break;
716 }
717
718 case Location::kRegisterPair : {
719 int low = location.low();
720 int high = location.high();
721 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
722 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
723 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
724 } else {
725 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInRegister, low);
726 }
727 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
728 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
729 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInStack, offset);
730 } else {
731 stack_map_stream_.AddDexRegisterEntry(
732 ++i, DexRegisterLocation::Kind::kInRegister, high);
733 }
734 DCHECK_LT(i, environment_size);
735 break;
736 }
737
738 case Location::kInvalid: {
739 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kNone, 0);
740 break;
741 }
742
743 default:
744 LOG(FATAL) << "Unexpected kind " << location.GetKind();
745 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100746 }
747}
748
Calin Juravle77520bc2015-01-12 18:45:46 +0000749bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
750 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
751 return (first_next_not_move != nullptr) && first_next_not_move->CanDoImplicitNullCheck();
752}
753
754void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
755 // If we are from a static path don't record the pc as we can't throw NPE.
756 // NB: having the checks here makes the code much less verbose in the arch
757 // specific code generators.
758 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
759 return;
760 }
761
762 if (!compiler_options_.GetImplicitNullChecks()) {
763 return;
764 }
765
766 if (!instr->CanDoImplicitNullCheck()) {
767 return;
768 }
769
770 // Find the first previous instruction which is not a move.
771 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
772
773 // If the instruction is a null check it means that `instr` is the first user
774 // and needs to record the pc.
775 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
776 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
777 // TODO: The parallel moves modify the environment. Their changes need to be reverted
778 // otherwise the stack maps at the throw point will not be correct.
779 RecordPcInfo(null_check, null_check->GetDexPc());
780 }
781}
782
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100783void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
784 LocationSummary* locations = suspend_check->GetLocations();
785 HBasicBlock* block = suspend_check->GetBlock();
786 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
787 DCHECK(block->IsLoopHeader());
788
789 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
790 HInstruction* current = it.Current();
791 LiveInterval* interval = current->GetLiveInterval();
792 // We only need to clear bits of loop phis containing objects and allocated in register.
793 // Loop phis allocated on stack already have the object in the stack.
794 if (current->GetType() == Primitive::kPrimNot
795 && interval->HasRegister()
796 && interval->HasSpillSlot()) {
797 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
798 }
799 }
800}
801
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000802void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000803 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +0000804 parallel_move.AddMove(from1, to1, nullptr);
805 parallel_move.AddMove(from2, to2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000806 GetMoveResolver()->EmitNativeCode(&parallel_move);
807}
808
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000809void SlowPathCode::RecordPcInfo(CodeGenerator* codegen, HInstruction* instruction, uint32_t dex_pc) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000810 codegen->RecordPcInfo(instruction, dex_pc, this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000811}
812
813void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
814 RegisterSet* register_set = locations->GetLiveRegisters();
815 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
816 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
817 if (!codegen->IsCoreCalleeSaveRegister(i)) {
818 if (register_set->ContainsCoreRegister(i)) {
819 // If the register holds an object, update the stack mask.
820 if (locations->RegisterContainsObject(i)) {
821 locations->SetStackBit(stack_offset / kVRegSize);
822 }
823 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000824 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
825 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000826 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
827 }
828 }
829 }
830
831 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
832 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
833 if (register_set->ContainsFloatingPointRegister(i)) {
834 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000835 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
836 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000837 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
838 }
839 }
840 }
841}
842
843void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
844 RegisterSet* register_set = locations->GetLiveRegisters();
845 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
846 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
847 if (!codegen->IsCoreCalleeSaveRegister(i)) {
848 if (register_set->ContainsCoreRegister(i)) {
849 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
850 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
851 }
852 }
853 }
854
855 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
856 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
857 if (register_set->ContainsFloatingPointRegister(i)) {
858 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
859 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
860 }
861 }
862 }
863}
864
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000865} // namespace art