blob: 561dcb731549b21ee48d7134cc3aa3cbc417e481 [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 Geoffraydc23d832015-02-16 11:15:43 +000043static bool IsSingleGoto(HBasicBlock* block) {
44 HLoopInformation* loop_info = block->GetLoopInformation();
45 // TODO: Remove the null check b/19084197.
46 return (block->GetFirstInstruction() != nullptr)
47 && (block->GetFirstInstruction() == block->GetLastInstruction())
48 && block->GetLastInstruction()->IsGoto()
49 // Back edges generate the suspend check.
50 && (loop_info == nullptr || !loop_info->IsBackEdge(block));
51}
52
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010053void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000054 Initialize();
Nicolas Geoffray73e80c32014-07-22 17:47:56 +010055 if (!is_leaf) {
56 MarkNotLeaf();
57 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000058 InitializeCodeGeneration(GetGraph()->GetNumberOfLocalVRegs()
59 + GetGraph()->GetTemporariesVRegSlots()
60 + 1 /* filler */,
61 0, /* the baseline compiler does not have live registers at slow path */
62 0, /* the baseline compiler does not have live registers at slow path */
63 GetGraph()->GetMaximumNumberOfOutVRegs()
64 + 1 /* current method */,
65 GetGraph()->GetBlocks());
66 CompileInternal(allocator, /* is_baseline */ true);
67}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +010068
Nicolas Geoffraydc23d832015-02-16 11:15:43 +000069bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
70 DCHECK_EQ(block_order_->Get(current_block_index_), current);
71 return GetNextBlockToEmit() == FirstNonEmptyBlock(next);
72}
73
74HBasicBlock* CodeGenerator::GetNextBlockToEmit() const {
75 for (size_t i = current_block_index_ + 1; i < block_order_->Size(); ++i) {
76 HBasicBlock* block = block_order_->Get(i);
77 if (!IsSingleGoto(block)) {
78 return block;
79 }
80 }
81 return nullptr;
82}
83
84HBasicBlock* CodeGenerator::FirstNonEmptyBlock(HBasicBlock* block) const {
85 while (IsSingleGoto(block)) {
86 block = block->GetSuccessors().Get(0);
87 }
88 return block;
89}
90
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000091void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
Nicolas Geoffray8a16d972014-09-11 10:30:02 +010092 HGraphVisitor* instruction_visitor = GetInstructionVisitor();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +000093 DCHECK_EQ(current_block_index_, 0u);
94 GenerateFrameEntry();
95 for (size_t e = block_order_->Size(); current_block_index_ < e; ++current_block_index_) {
96 HBasicBlock* block = block_order_->Get(current_block_index_);
Nicolas Geoffraydc23d832015-02-16 11:15:43 +000097 // Don't generate code for an empty block. Its predecessors will branch to its successor
98 // directly. Also, the label of that block will not be emitted, so this helps catch
99 // errors where we reference that label.
100 if (IsSingleGoto(block)) continue;
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100101 Bind(block);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100102 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
103 HInstruction* current = it.Current();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000104 if (is_baseline) {
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000105 InitLocationsBaseline(current);
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000106 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100107 current->Accept(instruction_visitor);
108 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000109 }
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000110
111 // Generate the slow paths.
112 for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
113 slow_paths_.Get(i)->EmitNativeCode(this);
114 }
115
116 // Finalize instructions in assember;
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000117 Finalize(allocator);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000118}
119
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100120void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000121 // The register allocator already called `InitializeCodeGeneration`,
122 // where the frame size has been computed.
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000123 DCHECK(block_order_ != nullptr);
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100124 Initialize();
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000125 CompileInternal(allocator, /* is_baseline */ false);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000126}
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100127
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000128void CodeGenerator::Finalize(CodeAllocator* allocator) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100129 size_t code_size = GetAssembler()->CodeSize();
130 uint8_t* buffer = allocator->Allocate(code_size);
Serban Constantinescu32f5b4d2014-11-25 20:05:46 +0000131
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100132 MemoryRegion code(buffer, code_size);
133 GetAssembler()->FinalizeInstructions(code);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000134}
135
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100136size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
137 for (size_t i = 0; i < length; ++i) {
138 if (!array[i]) {
139 array[i] = true;
140 return i;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100141 }
142 }
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100143 LOG(FATAL) << "Could not find a register in baseline register allocator";
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000144 UNREACHABLE();
145 return -1;
146}
147
Nicolas Geoffray3c035032014-10-28 10:46:40 +0000148size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
149 for (size_t i = 0; i < length - 1; i += 2) {
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000150 if (!array[i] && !array[i + 1]) {
151 array[i] = true;
152 array[i + 1] = true;
153 return i;
154 }
155 }
156 LOG(FATAL) << "Could not find a register in baseline register allocator";
157 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100158 return -1;
159}
160
Nicolas Geoffray4c204ba2015-02-03 15:12:35 +0000161void CodeGenerator::InitializeCodeGeneration(size_t number_of_spill_slots,
162 size_t maximum_number_of_live_core_registers,
163 size_t maximum_number_of_live_fp_registers,
164 size_t number_of_out_slots,
165 const GrowableArray<HBasicBlock*>& block_order) {
166 block_order_ = &block_order;
167 DCHECK(block_order_->Get(0) == GetGraph()->GetEntryBlock());
168 DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), block_order_->Get(1)));
Nicolas Geoffray4dee6362015-01-23 18:23:14 +0000169 ComputeSpillMask();
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100170 first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
171
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000172 if (number_of_spill_slots == 0
173 && !HasAllocatedCalleeSaveRegisters()
174 && IsLeafMethod()
175 && !RequiresCurrentMethod()) {
176 DCHECK_EQ(maximum_number_of_live_core_registers, 0u);
177 DCHECK_EQ(maximum_number_of_live_fp_registers, 0u);
178 SetFrameSize(CallPushesPC() ? GetWordSize() : 0);
179 } else {
180 SetFrameSize(RoundUp(
181 number_of_spill_slots * kVRegSize
182 + number_of_out_slots * kVRegSize
183 + maximum_number_of_live_core_registers * GetWordSize()
184 + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
185 + FrameEntrySpillSize(),
186 kStackAlignment));
187 }
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100188}
189
190Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
191 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000192 // The type of the previous instruction tells us if we need a single or double stack slot.
193 Primitive::Type type = temp->GetType();
194 int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100195 // Use the temporary region (right below the dex registers).
196 int32_t slot = GetFrameSize() - FrameEntrySpillSize()
197 - kVRegSize // filler
198 - (number_of_locals * kVRegSize)
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000199 - ((temp_size + temp->GetIndex()) * kVRegSize);
200 return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
Nicolas Geoffrayab032bc2014-07-15 12:55:21 +0100201}
202
203int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
204 uint16_t reg_number = local->GetRegNumber();
205 uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
206 if (reg_number >= number_of_locals) {
207 // Local is a parameter of the method. It is stored in the caller's frame.
208 return GetFrameSize() + kVRegSize // ART method
209 + (reg_number - number_of_locals) * kVRegSize;
210 } else {
211 // Local is a temporary in this method. It is stored in this method's frame.
212 return GetFrameSize() - FrameEntrySpillSize()
213 - kVRegSize // filler.
214 - (number_of_locals * kVRegSize)
215 + (reg_number * kVRegSize);
216 }
217}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100218
Mark Mendell5f874182015-03-04 15:42:45 -0500219void CodeGenerator::BlockIfInRegister(Location location, bool is_out) const {
220 // The DCHECKS below check that a register is not specified twice in
221 // the summary. The out location can overlap with an input, so we need
222 // to special case it.
223 if (location.IsRegister()) {
224 DCHECK(is_out || !blocked_core_registers_[location.reg()]);
225 blocked_core_registers_[location.reg()] = true;
226 } else if (location.IsFpuRegister()) {
227 DCHECK(is_out || !blocked_fpu_registers_[location.reg()]);
228 blocked_fpu_registers_[location.reg()] = true;
229 } else if (location.IsFpuRegisterPair()) {
230 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()]);
231 blocked_fpu_registers_[location.AsFpuRegisterPairLow<int>()] = true;
232 DCHECK(is_out || !blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()]);
233 blocked_fpu_registers_[location.AsFpuRegisterPairHigh<int>()] = true;
234 } else if (location.IsRegisterPair()) {
235 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairLow<int>()]);
236 blocked_core_registers_[location.AsRegisterPairLow<int>()] = true;
237 DCHECK(is_out || !blocked_core_registers_[location.AsRegisterPairHigh<int>()]);
238 blocked_core_registers_[location.AsRegisterPairHigh<int>()] = true;
239 }
240}
241
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100242void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
243 LocationSummary* locations = instruction->GetLocations();
244 if (locations == nullptr) return;
245
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100246 for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
247 blocked_core_registers_[i] = false;
248 }
249
250 for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
251 blocked_fpu_registers_[i] = false;
252 }
253
254 for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
255 blocked_register_pairs_[i] = false;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100256 }
257
258 // Mark all fixed input, temp and output registers as used.
259 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Mark Mendell5f874182015-03-04 15:42:45 -0500260 BlockIfInRegister(locations->InAt(i));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100261 }
262
263 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
264 Location loc = locations->GetTemp(i);
Mark Mendell5f874182015-03-04 15:42:45 -0500265 BlockIfInRegister(loc);
266 }
267 Location result_location = locations->Out();
268 if (locations->OutputCanOverlapWithInputs()) {
269 BlockIfInRegister(result_location, /* is_out */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100270 }
271
Mark Mendell5f874182015-03-04 15:42:45 -0500272 SetupBlockedRegisters(/* is_baseline */ true);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100273
274 // Allocate all unallocated input locations.
275 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
276 Location loc = locations->InAt(i);
277 HInstruction* input = instruction->InputAt(i);
278 if (loc.IsUnallocated()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100279 if ((loc.GetPolicy() == Location::kRequiresRegister)
280 || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100281 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100282 } else {
283 DCHECK_EQ(loc.GetPolicy(), Location::kAny);
284 HLoadLocal* load = input->AsLoadLocal();
285 if (load != nullptr) {
286 loc = GetStackLocation(load);
287 } else {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100288 loc = AllocateFreeRegister(input->GetType());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100289 }
290 }
291 locations->SetInAt(i, loc);
292 }
293 }
294
295 // Allocate all unallocated temp locations.
296 for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
297 Location loc = locations->GetTemp(i);
298 if (loc.IsUnallocated()) {
Roland Levillain647b9ed2014-11-27 12:06:00 +0000299 switch (loc.GetPolicy()) {
300 case Location::kRequiresRegister:
301 // Allocate a core register (large enough to fit a 32-bit integer).
302 loc = AllocateFreeRegister(Primitive::kPrimInt);
303 break;
304
305 case Location::kRequiresFpuRegister:
306 // Allocate a core register (large enough to fit a 64-bit double).
307 loc = AllocateFreeRegister(Primitive::kPrimDouble);
308 break;
309
310 default:
311 LOG(FATAL) << "Unexpected policy for temporary location "
312 << loc.GetPolicy();
313 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100314 locations->SetTempAt(i, loc);
315 }
316 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100317 if (result_location.IsUnallocated()) {
318 switch (result_location.GetPolicy()) {
319 case Location::kAny:
320 case Location::kRequiresRegister:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100321 case Location::kRequiresFpuRegister:
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100322 result_location = AllocateFreeRegister(instruction->GetType());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100323 break;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100324 case Location::kSameAsFirstInput:
325 result_location = locations->InAt(0);
326 break;
327 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000328 locations->UpdateOut(result_location);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100329 }
330}
331
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000332void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
333 AllocateLocations(instruction);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100334 if (instruction->GetLocations() == nullptr) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100335 if (instruction->IsTemporary()) {
336 HInstruction* previous = instruction->GetPrevious();
337 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
338 Move(previous, temp_location, instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100339 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100340 return;
341 }
342 AllocateRegistersLocally(instruction);
343 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000344 Location location = instruction->GetLocations()->InAt(i);
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000345 HInstruction* input = instruction->InputAt(i);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000346 if (location.IsValid()) {
347 // Move the input to the desired location.
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000348 if (input->GetNext()->IsTemporary()) {
349 // If the input was stored in a temporary, use that temporary to
350 // perform the move.
351 Move(input->GetNext(), location, instruction);
352 } else {
353 Move(input, location, instruction);
354 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000355 }
356 }
357}
358
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000359void CodeGenerator::AllocateLocations(HInstruction* instruction) {
360 instruction->Accept(GetLocationBuilder());
361 LocationSummary* locations = instruction->GetLocations();
362 if (!instruction->IsSuspendCheckEntry()) {
363 if (locations != nullptr && locations->CanCall()) {
364 MarkNotLeaf();
365 }
366 if (instruction->NeedsCurrentMethod()) {
367 SetRequiresCurrentMethod();
368 }
369 }
370}
371
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000372CodeGenerator* CodeGenerator::Create(HGraph* graph,
Calin Juravle34166012014-12-19 17:22:29 +0000373 InstructionSet instruction_set,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000374 const InstructionSetFeatures& isa_features,
375 const CompilerOptions& compiler_options) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000376 switch (instruction_set) {
377 case kArm:
378 case kThumb2: {
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000379 return new arm::CodeGeneratorARM(graph,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000380 *isa_features.AsArmInstructionSetFeatures(),
381 compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000382 }
Alexandre Rames5319def2014-10-23 10:03:10 +0100383 case kArm64: {
Serban Constantinescu579885a2015-02-22 20:51:33 +0000384 return new arm64::CodeGeneratorARM64(graph,
385 *isa_features.AsArm64InstructionSetFeatures(),
386 compiler_options);
Alexandre Rames5319def2014-10-23 10:03:10 +0100387 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000388 case kMips:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000389 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000390 case kX86: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000391 return new x86::CodeGeneratorX86(graph, compiler_options);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000392 }
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700393 case kX86_64: {
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000394 return new x86_64::CodeGeneratorX86_64(graph, compiler_options);
Dmitry Petrochenko6a58cb12014-04-02 17:27:59 +0700395 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000396 default:
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000397 return nullptr;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000398 }
399}
400
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000401void CodeGenerator::BuildNativeGCMap(
402 std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
403 const std::vector<uint8_t>& gc_map_raw =
404 dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
405 verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
406
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000407 uint32_t max_native_offset = 0;
408 for (size_t i = 0; i < pc_infos_.Size(); i++) {
409 uint32_t native_offset = pc_infos_.Get(i).native_pc;
410 if (native_offset > max_native_offset) {
411 max_native_offset = native_offset;
412 }
413 }
414
415 GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
416 for (size_t i = 0; i < pc_infos_.Size(); i++) {
417 struct PcInfo pc_info = pc_infos_.Get(i);
418 uint32_t native_offset = pc_info.native_pc;
419 uint32_t dex_pc = pc_info.dex_pc;
420 const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
Jean Christophe Beyler0ada95d2014-12-04 11:20:20 -0800421 CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000422 builder.AddEntry(native_offset, references);
423 }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000424}
425
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800426void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, DefaultSrcMap* src_map) const {
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000427 uint32_t pc2dex_data_size = 0u;
428 uint32_t pc2dex_entries = pc_infos_.Size();
429 uint32_t pc2dex_offset = 0u;
430 int32_t pc2dex_dalvik_offset = 0;
431 uint32_t dex2pc_data_size = 0u;
432 uint32_t dex2pc_entries = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000433 uint32_t dex2pc_offset = 0u;
434 int32_t dex2pc_dalvik_offset = 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000435
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700436 if (src_map != nullptr) {
437 src_map->reserve(pc2dex_entries);
438 }
439
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000440 for (size_t i = 0; i < pc2dex_entries; i++) {
441 struct PcInfo pc_info = pc_infos_.Get(i);
442 pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
443 pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
444 pc2dex_offset = pc_info.native_pc;
445 pc2dex_dalvik_offset = pc_info.dex_pc;
Yevgeny Roubane3ea8382014-08-08 16:29:38 +0700446 if (src_map != nullptr) {
447 src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
448 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000449 }
450
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000451 // Walk over the blocks and find which ones correspond to catch block entries.
452 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
453 HBasicBlock* block = graph_->GetBlocks().Get(i);
454 if (block->IsCatchBlock()) {
455 intptr_t native_pc = GetAddressOf(block);
456 ++dex2pc_entries;
457 dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
458 dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
459 dex2pc_offset = native_pc;
460 dex2pc_dalvik_offset = block->GetDexPc();
461 }
462 }
463
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000464 uint32_t total_entries = pc2dex_entries + dex2pc_entries;
465 uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
466 uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
467 data->resize(data_size);
468
469 uint8_t* data_ptr = &(*data)[0];
470 uint8_t* write_pos = data_ptr;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000471
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000472 write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
473 write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
474 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
475 uint8_t* write_pos2 = write_pos + pc2dex_data_size;
476
477 pc2dex_offset = 0u;
478 pc2dex_dalvik_offset = 0u;
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000479 dex2pc_offset = 0u;
480 dex2pc_dalvik_offset = 0u;
481
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000482 for (size_t i = 0; i < pc2dex_entries; i++) {
483 struct PcInfo pc_info = pc_infos_.Get(i);
484 DCHECK(pc2dex_offset <= pc_info.native_pc);
485 write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
486 write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
487 pc2dex_offset = pc_info.native_pc;
488 pc2dex_dalvik_offset = pc_info.dex_pc;
489 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000490
491 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
492 HBasicBlock* block = graph_->GetBlocks().Get(i);
493 if (block->IsCatchBlock()) {
494 intptr_t native_pc = GetAddressOf(block);
495 write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
496 write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
497 dex2pc_offset = native_pc;
498 dex2pc_dalvik_offset = block->GetDexPc();
499 }
500 }
501
502
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000503 DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
504 DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
505
506 if (kIsDebugBuild) {
507 // Verify the encoded table holds the expected data.
508 MappingTable table(data_ptr);
509 CHECK_EQ(table.TotalSize(), total_entries);
510 CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
511 auto it = table.PcToDexBegin();
512 auto it2 = table.DexToPcBegin();
513 for (size_t i = 0; i < pc2dex_entries; i++) {
514 struct PcInfo pc_info = pc_infos_.Get(i);
515 CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
516 CHECK_EQ(pc_info.dex_pc, it.DexPc());
517 ++it;
518 }
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000519 for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
520 HBasicBlock* block = graph_->GetBlocks().Get(i);
521 if (block->IsCatchBlock()) {
522 CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
523 CHECK_EQ(block->GetDexPc(), it2.DexPc());
524 ++it2;
525 }
526 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000527 CHECK(it == table.PcToDexEnd());
528 CHECK(it2 == table.DexToPcEnd());
529 }
530}
531
532void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
533 Leb128EncodingVector vmap_encoder;
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100534 // We currently don't use callee-saved registers.
535 size_t size = 0 + 1 /* marker */ + 0;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000536 vmap_encoder.Reserve(size + 1u); // All values are likely to be one byte in ULEB128 (<128).
537 vmap_encoder.PushBackUnsigned(size);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000538 vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
539
540 *data = vmap_encoder.GetData();
541}
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000542
Nicolas Geoffray39468442014-09-02 15:17:15 +0100543void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
544 uint32_t size = stack_map_stream_.ComputeNeededSize();
545 data->resize(size);
546 MemoryRegion region(data->data(), size);
547 stack_map_stream_.FillIn(region);
548}
549
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000550void CodeGenerator::RecordPcInfo(HInstruction* instruction,
551 uint32_t dex_pc,
552 SlowPathCode* slow_path) {
Calin Juravled2ec87d2014-12-08 14:24:46 +0000553 if (instruction != nullptr) {
Roland Levillain624279f2014-12-04 11:54:28 +0000554 // The code generated for some type conversions may call the
555 // runtime, thus normally requiring a subsequent call to this
556 // method. However, the method verifier does not produce PC
Calin Juravled2ec87d2014-12-08 14:24:46 +0000557 // information for certain instructions, which are considered "atomic"
558 // (they cannot join a GC).
Roland Levillain624279f2014-12-04 11:54:28 +0000559 // Therefore we do not currently record PC information for such
560 // instructions. As this may change later, we added this special
561 // case so that code generators may nevertheless call
562 // CodeGenerator::RecordPcInfo without triggering an error in
563 // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
564 // thereafter.
Calin Juravled2ec87d2014-12-08 14:24:46 +0000565 if (instruction->IsTypeConversion()) {
566 return;
567 }
568 if (instruction->IsRem()) {
569 Primitive::Type type = instruction->AsRem()->GetResultType();
570 if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
571 return;
572 }
573 }
Roland Levillain624279f2014-12-04 11:54:28 +0000574 }
575
Nicolas Geoffray39468442014-09-02 15:17:15 +0100576 // Collect PC infos for the mapping table.
577 struct PcInfo pc_info;
578 pc_info.dex_pc = dex_pc;
579 pc_info.native_pc = GetAssembler()->CodeSize();
580 pc_infos_.Add(pc_info);
581
Nicolas Geoffrayfead4e42015-03-13 14:39:40 +0000582 uint32_t inlining_depth = 0;
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000583
Nicolas Geoffray39468442014-09-02 15:17:15 +0100584 if (instruction == nullptr) {
585 // For stack overflow checks.
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000586 stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, inlining_depth);
587 return;
588 }
589 LocationSummary* locations = instruction->GetLocations();
590 HEnvironment* environment = instruction->GetEnvironment();
591 size_t environment_size = instruction->EnvironmentSize();
Nicolas Geoffray39468442014-09-02 15:17:15 +0100592
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000593 uint32_t register_mask = locations->GetRegisterMask();
594 if (locations->OnlyCallsOnSlowPath()) {
595 // In case of slow path, we currently set the location of caller-save registers
596 // to register (instead of their stack location when pushed before the slow-path
597 // call). Therefore register_mask contains both callee-save and caller-save
598 // registers that hold objects. We must remove the caller-save from the mask, since
599 // they will be overwritten by the callee.
600 register_mask &= core_callee_save_mask_;
601 }
602 // The register mask must be a subset of callee-save registers.
603 DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
604 stack_map_stream_.AddStackMapEntry(dex_pc,
605 pc_info.native_pc,
606 register_mask,
607 locations->GetStackMask(),
608 environment_size,
609 inlining_depth);
610
611 // Walk over the environment, and record the location of dex registers.
612 for (size_t i = 0; i < environment_size; ++i) {
613 HInstruction* current = environment->GetInstructionAt(i);
614 if (current == nullptr) {
615 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kNone, 0);
616 continue;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100617 }
618
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000619 Location location = locations->GetEnvironmentAt(i);
620 switch (location.GetKind()) {
621 case Location::kConstant: {
622 DCHECK_EQ(current, location.GetConstant());
623 if (current->IsLongConstant()) {
624 int64_t value = current->AsLongConstant()->GetValue();
625 stack_map_stream_.AddDexRegisterEntry(
626 i, DexRegisterLocation::Kind::kConstant, Low32Bits(value));
627 stack_map_stream_.AddDexRegisterEntry(
628 ++i, DexRegisterLocation::Kind::kConstant, High32Bits(value));
629 DCHECK_LT(i, environment_size);
630 } else if (current->IsDoubleConstant()) {
631 int64_t value = bit_cast<double, int64_t>(current->AsDoubleConstant()->GetValue());
632 stack_map_stream_.AddDexRegisterEntry(
633 i, DexRegisterLocation::Kind::kConstant, Low32Bits(value));
634 stack_map_stream_.AddDexRegisterEntry(
635 ++i, DexRegisterLocation::Kind::kConstant, High32Bits(value));
636 DCHECK_LT(i, environment_size);
637 } else if (current->IsIntConstant()) {
638 int32_t value = current->AsIntConstant()->GetValue();
639 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, value);
640 } else if (current->IsNullConstant()) {
641 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, 0);
642 } else {
643 DCHECK(current->IsFloatConstant()) << current->DebugName();
644 int32_t value = bit_cast<float, int32_t>(current->AsFloatConstant()->GetValue());
645 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kConstant, value);
646 }
647 break;
648 }
649
650 case Location::kStackSlot: {
651 stack_map_stream_.AddDexRegisterEntry(
652 i, DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
653 break;
654 }
655
656 case Location::kDoubleStackSlot: {
657 stack_map_stream_.AddDexRegisterEntry(
658 i, DexRegisterLocation::Kind::kInStack, location.GetStackIndex());
659 stack_map_stream_.AddDexRegisterEntry(
660 ++i, DexRegisterLocation::Kind::kInStack, location.GetHighStackIndex(kVRegSize));
661 DCHECK_LT(i, environment_size);
662 break;
663 }
664
665 case Location::kRegister : {
666 int id = location.reg();
667 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(id)) {
668 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(id);
669 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
670 if (current->GetType() == Primitive::kPrimLong) {
671 stack_map_stream_.AddDexRegisterEntry(
672 ++i, DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
673 DCHECK_LT(i, environment_size);
674 }
675 } else {
676 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInRegister, id);
677 if (current->GetType() == Primitive::kPrimLong) {
678 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInRegister, id);
679 DCHECK_LT(i, environment_size);
680 }
681 }
682 break;
683 }
684
685 case Location::kFpuRegister : {
686 int id = location.reg();
687 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(id)) {
688 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(id);
689 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
690 if (current->GetType() == Primitive::kPrimDouble) {
691 stack_map_stream_.AddDexRegisterEntry(
692 ++i, DexRegisterLocation::Kind::kInStack, offset + kVRegSize);
693 DCHECK_LT(i, environment_size);
694 }
695 } else {
696 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInFpuRegister, id);
697 if (current->GetType() == Primitive::kPrimDouble) {
698 stack_map_stream_.AddDexRegisterEntry(
699 ++i, DexRegisterLocation::Kind::kInFpuRegister, id);
700 DCHECK_LT(i, environment_size);
701 }
702 }
703 break;
704 }
705
706 case Location::kFpuRegisterPair : {
707 int low = location.low();
708 int high = location.high();
709 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(low)) {
710 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(low);
711 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
712 } else {
713 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInFpuRegister, low);
714 }
715 if (slow_path != nullptr && slow_path->IsFpuRegisterSaved(high)) {
716 uint32_t offset = slow_path->GetStackOffsetOfFpuRegister(high);
717 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInStack, offset);
718 } else {
719 stack_map_stream_.AddDexRegisterEntry(
720 ++i, DexRegisterLocation::Kind::kInFpuRegister, high);
721 }
722 DCHECK_LT(i, environment_size);
723 break;
724 }
725
726 case Location::kRegisterPair : {
727 int low = location.low();
728 int high = location.high();
729 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(low)) {
730 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(low);
731 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInStack, offset);
732 } else {
733 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kInRegister, low);
734 }
735 if (slow_path != nullptr && slow_path->IsCoreRegisterSaved(high)) {
736 uint32_t offset = slow_path->GetStackOffsetOfCoreRegister(high);
737 stack_map_stream_.AddDexRegisterEntry(++i, DexRegisterLocation::Kind::kInStack, offset);
738 } else {
739 stack_map_stream_.AddDexRegisterEntry(
740 ++i, DexRegisterLocation::Kind::kInRegister, high);
741 }
742 DCHECK_LT(i, environment_size);
743 break;
744 }
745
746 case Location::kInvalid: {
747 stack_map_stream_.AddDexRegisterEntry(i, DexRegisterLocation::Kind::kNone, 0);
748 break;
749 }
750
751 default:
752 LOG(FATAL) << "Unexpected kind " << location.GetKind();
753 }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100754 }
755}
756
Calin Juravle77520bc2015-01-12 18:45:46 +0000757bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
758 HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
759 return (first_next_not_move != nullptr) && first_next_not_move->CanDoImplicitNullCheck();
760}
761
762void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
763 // If we are from a static path don't record the pc as we can't throw NPE.
764 // NB: having the checks here makes the code much less verbose in the arch
765 // specific code generators.
766 if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
767 return;
768 }
769
770 if (!compiler_options_.GetImplicitNullChecks()) {
771 return;
772 }
773
774 if (!instr->CanDoImplicitNullCheck()) {
775 return;
776 }
777
778 // Find the first previous instruction which is not a move.
779 HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
780
781 // If the instruction is a null check it means that `instr` is the first user
782 // and needs to record the pc.
783 if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
784 HNullCheck* null_check = first_prev_not_move->AsNullCheck();
785 // TODO: The parallel moves modify the environment. Their changes need to be reverted
786 // otherwise the stack maps at the throw point will not be correct.
787 RecordPcInfo(null_check, null_check->GetDexPc());
788 }
789}
790
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100791void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
792 LocationSummary* locations = suspend_check->GetLocations();
793 HBasicBlock* block = suspend_check->GetBlock();
794 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
795 DCHECK(block->IsLoopHeader());
796
797 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
798 HInstruction* current = it.Current();
799 LiveInterval* interval = current->GetLiveInterval();
800 // We only need to clear bits of loop phis containing objects and allocated in register.
801 // Loop phis allocated on stack already have the object in the stack.
802 if (current->GetType() == Primitive::kPrimNot
803 && interval->HasRegister()
804 && interval->HasSpillSlot()) {
805 locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
806 }
807 }
808}
809
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000810void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) {
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000811 HParallelMove parallel_move(GetGraph()->GetArena());
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +0000812 parallel_move.AddMove(from1, to1, nullptr);
813 parallel_move.AddMove(from2, to2, nullptr);
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000814 GetMoveResolver()->EmitNativeCode(&parallel_move);
815}
816
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000817void SlowPathCode::RecordPcInfo(CodeGenerator* codegen, HInstruction* instruction, uint32_t dex_pc) {
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000818 codegen->RecordPcInfo(instruction, dex_pc, this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000819}
820
821void SlowPathCode::SaveLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
822 RegisterSet* register_set = locations->GetLiveRegisters();
823 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
824 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
825 if (!codegen->IsCoreCalleeSaveRegister(i)) {
826 if (register_set->ContainsCoreRegister(i)) {
827 // If the register holds an object, update the stack mask.
828 if (locations->RegisterContainsObject(i)) {
829 locations->SetStackBit(stack_offset / kVRegSize);
830 }
831 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000832 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
833 saved_core_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000834 stack_offset += codegen->SaveCoreRegister(stack_offset, i);
835 }
836 }
837 }
838
839 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
840 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
841 if (register_set->ContainsFloatingPointRegister(i)) {
842 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
Nicolas Geoffrayeeefa122015-03-13 18:52:59 +0000843 DCHECK_LT(i, kMaximumNumberOfExpectedRegisters);
844 saved_fpu_stack_offsets_[i] = stack_offset;
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000845 stack_offset += codegen->SaveFloatingPointRegister(stack_offset, i);
846 }
847 }
848 }
849}
850
851void SlowPathCode::RestoreLiveRegisters(CodeGenerator* codegen, LocationSummary* locations) {
852 RegisterSet* register_set = locations->GetLiveRegisters();
853 size_t stack_offset = codegen->GetFirstRegisterSlotInSlowPath();
854 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
855 if (!codegen->IsCoreCalleeSaveRegister(i)) {
856 if (register_set->ContainsCoreRegister(i)) {
857 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
858 stack_offset += codegen->RestoreCoreRegister(stack_offset, i);
859 }
860 }
861 }
862
863 for (size_t i = 0, e = codegen->GetNumberOfFloatingPointRegisters(); i < e; ++i) {
864 if (!codegen->IsFloatingPointCalleeSaveRegister(i)) {
865 if (register_set->ContainsFloatingPointRegister(i)) {
866 DCHECK_LT(stack_offset, codegen->GetFrameSize() - codegen->FrameEntrySpillSize());
867 stack_offset += codegen->RestoreFloatingPointRegister(stack_offset, i);
868 }
869 }
870 }
871}
872
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000873} // namespace art