code_generator.cc revision 42d1f5f006c8bdbcbf855c53036cd50f9c69753e
1/*
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"
20#include "code_generator_arm64.h"
21#include "code_generator_x86.h"
22#include "code_generator_x86_64.h"
23#include "compiled_method.h"
24#include "dex/verified_method.h"
25#include "driver/dex_compilation_unit.h"
26#include "gc_map_builder.h"
27#include "leb128.h"
28#include "mapping_table.h"
29#include "mirror/array-inl.h"
30#include "mirror/object_array-inl.h"
31#include "mirror/object_reference.h"
32#include "ssa_liveness_analysis.h"
33#include "utils/assembler.h"
34#include "verifier/dex_gc_map.h"
35#include "vmap_table.h"
36
37namespace art {
38
39size_t CodeGenerator::GetCacheOffset(uint32_t index) {
40  return mirror::ObjectArray<mirror::Object>::OffsetOfElement(index).SizeValue();
41}
42
43void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
44  const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
45  DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
46  DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
47  Initialize();
48
49  DCHECK_EQ(frame_size_, kUninitializedFrameSize);
50  if (!is_leaf) {
51    MarkNotLeaf();
52  }
53  ComputeFrameSize(GetGraph()->GetNumberOfLocalVRegs()
54                     + GetGraph()->GetTemporariesVRegSlots()
55                     + 1 /* filler */,
56                   0, /* the baseline compiler does not have live registers at slow path */
57                   0, /* the baseline compiler does not have live registers at slow path */
58                   GetGraph()->GetMaximumNumberOfOutVRegs()
59                     + 1 /* current method */);
60  GenerateFrameEntry();
61
62  HGraphVisitor* location_builder = GetLocationBuilder();
63  HGraphVisitor* instruction_visitor = GetInstructionVisitor();
64  for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
65    HBasicBlock* block = blocks.Get(i);
66    Bind(block);
67    for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
68      HInstruction* current = it.Current();
69      current->Accept(location_builder);
70      InitLocations(current);
71      current->Accept(instruction_visitor);
72    }
73  }
74  GenerateSlowPaths();
75  Finalize(allocator);
76}
77
78void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
79  // The frame size has already been computed during register allocation.
80  DCHECK_NE(frame_size_, kUninitializedFrameSize);
81  const GrowableArray<HBasicBlock*>& blocks = GetGraph()->GetBlocks();
82  DCHECK(blocks.Get(0) == GetGraph()->GetEntryBlock());
83  DCHECK(GoesToNextBlock(GetGraph()->GetEntryBlock(), blocks.Get(1)));
84  Initialize();
85
86  GenerateFrameEntry();
87  HGraphVisitor* instruction_visitor = GetInstructionVisitor();
88  for (size_t i = 0, e = blocks.Size(); i < e; ++i) {
89    HBasicBlock* block = blocks.Get(i);
90    Bind(block);
91    for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
92      HInstruction* current = it.Current();
93      current->Accept(instruction_visitor);
94    }
95  }
96  GenerateSlowPaths();
97  Finalize(allocator);
98}
99
100void CodeGenerator::Finalize(CodeAllocator* allocator) {
101  size_t code_size = GetAssembler()->CodeSize();
102  uint8_t* buffer = allocator->Allocate(code_size);
103
104  MemoryRegion code(buffer, code_size);
105  GetAssembler()->FinalizeInstructions(code);
106}
107
108void CodeGenerator::GenerateSlowPaths() {
109  for (size_t i = 0, e = slow_paths_.Size(); i < e; ++i) {
110    slow_paths_.Get(i)->EmitNativeCode(this);
111  }
112}
113
114size_t CodeGenerator::FindFreeEntry(bool* array, size_t length) {
115  for (size_t i = 0; i < length; ++i) {
116    if (!array[i]) {
117      array[i] = true;
118      return i;
119    }
120  }
121  LOG(FATAL) << "Could not find a register in baseline register allocator";
122  UNREACHABLE();
123  return -1;
124}
125
126size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
127  for (size_t i = 0; i < length - 1; i += 2) {
128    if (!array[i] && !array[i + 1]) {
129      array[i] = true;
130      array[i + 1] = true;
131      return i;
132    }
133  }
134  LOG(FATAL) << "Could not find a register in baseline register allocator";
135  UNREACHABLE();
136  return -1;
137}
138
139void CodeGenerator::ComputeFrameSize(size_t number_of_spill_slots,
140                                     size_t maximum_number_of_live_core_registers,
141                                     size_t maximum_number_of_live_fp_registers,
142                                     size_t number_of_out_slots) {
143  first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
144
145  SetFrameSize(RoundUp(
146      number_of_spill_slots * kVRegSize
147      + number_of_out_slots * kVRegSize
148      + maximum_number_of_live_core_registers * GetWordSize()
149      + maximum_number_of_live_fp_registers * GetFloatingPointSpillSlotSize()
150      + FrameEntrySpillSize(),
151      kStackAlignment));
152}
153
154Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
155  uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
156  // The type of the previous instruction tells us if we need a single or double stack slot.
157  Primitive::Type type = temp->GetType();
158  int32_t temp_size = (type == Primitive::kPrimLong) || (type == Primitive::kPrimDouble) ? 2 : 1;
159  // Use the temporary region (right below the dex registers).
160  int32_t slot = GetFrameSize() - FrameEntrySpillSize()
161                                - kVRegSize  // filler
162                                - (number_of_locals * kVRegSize)
163                                - ((temp_size + temp->GetIndex()) * kVRegSize);
164  return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
165}
166
167int32_t CodeGenerator::GetStackSlot(HLocal* local) const {
168  uint16_t reg_number = local->GetRegNumber();
169  uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
170  if (reg_number >= number_of_locals) {
171    // Local is a parameter of the method. It is stored in the caller's frame.
172    return GetFrameSize() + kVRegSize  // ART method
173                          + (reg_number - number_of_locals) * kVRegSize;
174  } else {
175    // Local is a temporary in this method. It is stored in this method's frame.
176    return GetFrameSize() - FrameEntrySpillSize()
177                          - kVRegSize  // filler.
178                          - (number_of_locals * kVRegSize)
179                          + (reg_number * kVRegSize);
180  }
181}
182
183void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
184  LocationSummary* locations = instruction->GetLocations();
185  if (locations == nullptr) return;
186
187  for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
188    blocked_core_registers_[i] = false;
189  }
190
191  for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
192    blocked_fpu_registers_[i] = false;
193  }
194
195  for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
196    blocked_register_pairs_[i] = false;
197  }
198
199  // Mark all fixed input, temp and output registers as used.
200  for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
201    Location loc = locations->InAt(i);
202    // The DCHECKS below check that a register is not specified twice in
203    // the summary.
204    if (loc.IsRegister()) {
205      DCHECK(!blocked_core_registers_[loc.reg()]);
206      blocked_core_registers_[loc.reg()] = true;
207    } else if (loc.IsFpuRegister()) {
208      DCHECK(!blocked_fpu_registers_[loc.reg()]);
209      blocked_fpu_registers_[loc.reg()] = true;
210    } else if (loc.IsFpuRegisterPair()) {
211      DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()]);
212      blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()] = true;
213      DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()]);
214      blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()] = true;
215    } else if (loc.IsRegisterPair()) {
216      DCHECK(!blocked_core_registers_[loc.AsRegisterPairLow<int>()]);
217      blocked_core_registers_[loc.AsRegisterPairLow<int>()] = true;
218      DCHECK(!blocked_core_registers_[loc.AsRegisterPairHigh<int>()]);
219      blocked_core_registers_[loc.AsRegisterPairHigh<int>()] = true;
220    }
221  }
222
223  for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
224    Location loc = locations->GetTemp(i);
225    // The DCHECKS below check that a register is not specified twice in
226    // the summary.
227    if (loc.IsRegister()) {
228      DCHECK(!blocked_core_registers_[loc.reg()]);
229      blocked_core_registers_[loc.reg()] = true;
230    } else if (loc.IsFpuRegister()) {
231      DCHECK(!blocked_fpu_registers_[loc.reg()]);
232      blocked_fpu_registers_[loc.reg()] = true;
233    } else {
234      DCHECK(loc.GetPolicy() == Location::kRequiresRegister
235             || loc.GetPolicy() == Location::kRequiresFpuRegister);
236    }
237  }
238
239  SetupBlockedRegisters();
240
241  // Allocate all unallocated input locations.
242  for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
243    Location loc = locations->InAt(i);
244    HInstruction* input = instruction->InputAt(i);
245    if (loc.IsUnallocated()) {
246      if ((loc.GetPolicy() == Location::kRequiresRegister)
247          || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
248        loc = AllocateFreeRegister(input->GetType());
249      } else {
250        DCHECK_EQ(loc.GetPolicy(), Location::kAny);
251        HLoadLocal* load = input->AsLoadLocal();
252        if (load != nullptr) {
253          loc = GetStackLocation(load);
254        } else {
255          loc = AllocateFreeRegister(input->GetType());
256        }
257      }
258      locations->SetInAt(i, loc);
259    }
260  }
261
262  // Allocate all unallocated temp locations.
263  for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
264    Location loc = locations->GetTemp(i);
265    if (loc.IsUnallocated()) {
266      switch (loc.GetPolicy()) {
267        case Location::kRequiresRegister:
268          // Allocate a core register (large enough to fit a 32-bit integer).
269          loc = AllocateFreeRegister(Primitive::kPrimInt);
270          break;
271
272        case Location::kRequiresFpuRegister:
273          // Allocate a core register (large enough to fit a 64-bit double).
274          loc = AllocateFreeRegister(Primitive::kPrimDouble);
275          break;
276
277        default:
278          LOG(FATAL) << "Unexpected policy for temporary location "
279                     << loc.GetPolicy();
280      }
281      locations->SetTempAt(i, loc);
282    }
283  }
284  Location result_location = locations->Out();
285  if (result_location.IsUnallocated()) {
286    switch (result_location.GetPolicy()) {
287      case Location::kAny:
288      case Location::kRequiresRegister:
289      case Location::kRequiresFpuRegister:
290        result_location = AllocateFreeRegister(instruction->GetType());
291        break;
292      case Location::kSameAsFirstInput:
293        result_location = locations->InAt(0);
294        break;
295    }
296    locations->SetOut(result_location);
297  }
298}
299
300void CodeGenerator::InitLocations(HInstruction* instruction) {
301  if (instruction->GetLocations() == nullptr) {
302    if (instruction->IsTemporary()) {
303      HInstruction* previous = instruction->GetPrevious();
304      Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
305      Move(previous, temp_location, instruction);
306    }
307    return;
308  }
309  AllocateRegistersLocally(instruction);
310  for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
311    Location location = instruction->GetLocations()->InAt(i);
312    HInstruction* input = instruction->InputAt(i);
313    if (location.IsValid()) {
314      // Move the input to the desired location.
315      if (input->GetNext()->IsTemporary()) {
316        // If the input was stored in a temporary, use that temporary to
317        // perform the move.
318        Move(input->GetNext(), location, instruction);
319      } else {
320        Move(input, location, instruction);
321      }
322    }
323  }
324}
325
326bool CodeGenerator::GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const {
327  // We currently iterate over the block in insertion order.
328  return current->GetBlockId() + 1 == next->GetBlockId();
329}
330
331CodeGenerator* CodeGenerator::Create(HGraph* graph,
332                                     InstructionSet instruction_set,
333                                     const InstructionSetFeatures& isa_features) {
334  switch (instruction_set) {
335    case kArm:
336    case kThumb2: {
337      return new arm::CodeGeneratorARM(graph,
338          isa_features.AsArmInstructionSetFeatures());
339    }
340    case kArm64: {
341      return new arm64::CodeGeneratorARM64(graph);
342    }
343    case kMips:
344      return nullptr;
345    case kX86: {
346      return new x86::CodeGeneratorX86(graph);
347    }
348    case kX86_64: {
349      return new x86_64::CodeGeneratorX86_64(graph);
350    }
351    default:
352      return nullptr;
353  }
354}
355
356void CodeGenerator::BuildNativeGCMap(
357    std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
358  const std::vector<uint8_t>& gc_map_raw =
359      dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
360  verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
361
362  uint32_t max_native_offset = 0;
363  for (size_t i = 0; i < pc_infos_.Size(); i++) {
364    uint32_t native_offset = pc_infos_.Get(i).native_pc;
365    if (native_offset > max_native_offset) {
366      max_native_offset = native_offset;
367    }
368  }
369
370  GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
371  for (size_t i = 0; i < pc_infos_.Size(); i++) {
372    struct PcInfo pc_info = pc_infos_.Get(i);
373    uint32_t native_offset = pc_info.native_pc;
374    uint32_t dex_pc = pc_info.dex_pc;
375    const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
376    CHECK(references != NULL) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
377    builder.AddEntry(native_offset, references);
378  }
379}
380
381void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, DefaultSrcMap* src_map) const {
382  uint32_t pc2dex_data_size = 0u;
383  uint32_t pc2dex_entries = pc_infos_.Size();
384  uint32_t pc2dex_offset = 0u;
385  int32_t pc2dex_dalvik_offset = 0;
386  uint32_t dex2pc_data_size = 0u;
387  uint32_t dex2pc_entries = 0u;
388  uint32_t dex2pc_offset = 0u;
389  int32_t dex2pc_dalvik_offset = 0;
390
391  if (src_map != nullptr) {
392    src_map->reserve(pc2dex_entries);
393  }
394
395  for (size_t i = 0; i < pc2dex_entries; i++) {
396    struct PcInfo pc_info = pc_infos_.Get(i);
397    pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
398    pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
399    pc2dex_offset = pc_info.native_pc;
400    pc2dex_dalvik_offset = pc_info.dex_pc;
401    if (src_map != nullptr) {
402      src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
403    }
404  }
405
406  // Walk over the blocks and find which ones correspond to catch block entries.
407  for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
408    HBasicBlock* block = graph_->GetBlocks().Get(i);
409    if (block->IsCatchBlock()) {
410      intptr_t native_pc = GetAddressOf(block);
411      ++dex2pc_entries;
412      dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
413      dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
414      dex2pc_offset = native_pc;
415      dex2pc_dalvik_offset = block->GetDexPc();
416    }
417  }
418
419  uint32_t total_entries = pc2dex_entries + dex2pc_entries;
420  uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
421  uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
422  data->resize(data_size);
423
424  uint8_t* data_ptr = &(*data)[0];
425  uint8_t* write_pos = data_ptr;
426
427  write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
428  write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
429  DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
430  uint8_t* write_pos2 = write_pos + pc2dex_data_size;
431
432  pc2dex_offset = 0u;
433  pc2dex_dalvik_offset = 0u;
434  dex2pc_offset = 0u;
435  dex2pc_dalvik_offset = 0u;
436
437  for (size_t i = 0; i < pc2dex_entries; i++) {
438    struct PcInfo pc_info = pc_infos_.Get(i);
439    DCHECK(pc2dex_offset <= pc_info.native_pc);
440    write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
441    write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
442    pc2dex_offset = pc_info.native_pc;
443    pc2dex_dalvik_offset = pc_info.dex_pc;
444  }
445
446  for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
447    HBasicBlock* block = graph_->GetBlocks().Get(i);
448    if (block->IsCatchBlock()) {
449      intptr_t native_pc = GetAddressOf(block);
450      write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
451      write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
452      dex2pc_offset = native_pc;
453      dex2pc_dalvik_offset = block->GetDexPc();
454    }
455  }
456
457
458  DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
459  DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
460
461  if (kIsDebugBuild) {
462    // Verify the encoded table holds the expected data.
463    MappingTable table(data_ptr);
464    CHECK_EQ(table.TotalSize(), total_entries);
465    CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
466    auto it = table.PcToDexBegin();
467    auto it2 = table.DexToPcBegin();
468    for (size_t i = 0; i < pc2dex_entries; i++) {
469      struct PcInfo pc_info = pc_infos_.Get(i);
470      CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
471      CHECK_EQ(pc_info.dex_pc, it.DexPc());
472      ++it;
473    }
474    for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
475      HBasicBlock* block = graph_->GetBlocks().Get(i);
476      if (block->IsCatchBlock()) {
477        CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
478        CHECK_EQ(block->GetDexPc(), it2.DexPc());
479        ++it2;
480      }
481    }
482    CHECK(it == table.PcToDexEnd());
483    CHECK(it2 == table.DexToPcEnd());
484  }
485}
486
487void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
488  Leb128EncodingVector vmap_encoder;
489  // We currently don't use callee-saved registers.
490  size_t size = 0 + 1 /* marker */ + 0;
491  vmap_encoder.Reserve(size + 1u);  // All values are likely to be one byte in ULEB128 (<128).
492  vmap_encoder.PushBackUnsigned(size);
493  vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
494
495  *data = vmap_encoder.GetData();
496}
497
498void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
499  uint32_t size = stack_map_stream_.ComputeNeededSize();
500  data->resize(size);
501  MemoryRegion region(data->data(), size);
502  stack_map_stream_.FillIn(region);
503}
504
505void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) {
506  if (instruction != nullptr) {
507    // The code generated for some type conversions may call the
508    // runtime, thus normally requiring a subsequent call to this
509    // method.  However, the method verifier does not produce PC
510    // information for certain instructions, which are considered "atomic"
511    // (they cannot join a GC).
512    // Therefore we do not currently record PC information for such
513    // instructions.  As this may change later, we added this special
514    // case so that code generators may nevertheless call
515    // CodeGenerator::RecordPcInfo without triggering an error in
516    // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
517    // thereafter.
518    if (instruction->IsTypeConversion()) {
519      return;
520    }
521    if (instruction->IsRem()) {
522      Primitive::Type type = instruction->AsRem()->GetResultType();
523      if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
524        return;
525      }
526    }
527  }
528
529  // Collect PC infos for the mapping table.
530  struct PcInfo pc_info;
531  pc_info.dex_pc = dex_pc;
532  pc_info.native_pc = GetAssembler()->CodeSize();
533  pc_infos_.Add(pc_info);
534
535  // Populate stack map information.
536
537  if (instruction == nullptr) {
538    // For stack overflow checks.
539    stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0);
540    return;
541  }
542
543  LocationSummary* locations = instruction->GetLocations();
544  HEnvironment* environment = instruction->GetEnvironment();
545
546  size_t environment_size = instruction->EnvironmentSize();
547
548  size_t register_mask = 0;
549  size_t inlining_depth = 0;
550  stack_map_stream_.AddStackMapEntry(
551      dex_pc, pc_info.native_pc, register_mask,
552      locations->GetStackMask(), environment_size, inlining_depth);
553
554  // Walk over the environment, and record the location of dex registers.
555  for (size_t i = 0; i < environment_size; ++i) {
556    HInstruction* current = environment->GetInstructionAt(i);
557    if (current == nullptr) {
558      stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0);
559      continue;
560    }
561
562    Location location = locations->GetEnvironmentAt(i);
563    switch (location.GetKind()) {
564      case Location::kConstant: {
565        DCHECK(current == location.GetConstant());
566        if (current->IsLongConstant()) {
567          int64_t value = current->AsLongConstant()->GetValue();
568          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
569          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
570          ++i;
571          DCHECK_LT(i, environment_size);
572        } else if (current->IsDoubleConstant()) {
573          int64_t value = bit_cast<double, int64_t>(current->AsDoubleConstant()->GetValue());
574          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
575          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
576          ++i;
577          DCHECK_LT(i, environment_size);
578        } else if (current->IsIntConstant()) {
579          int32_t value = current->AsIntConstant()->GetValue();
580          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
581        } else {
582          DCHECK(current->IsFloatConstant());
583          int32_t value = bit_cast<float, int32_t>(current->AsFloatConstant()->GetValue());
584          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
585        }
586        break;
587      }
588
589      case Location::kStackSlot: {
590        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
591        break;
592      }
593
594      case Location::kDoubleStackSlot: {
595        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
596        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack,
597                                              location.GetHighStackIndex(kVRegSize));
598        ++i;
599        DCHECK_LT(i, environment_size);
600        break;
601      }
602
603      case Location::kRegister : {
604        int id = location.reg();
605        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
606        if (current->GetType() == Primitive::kPrimLong) {
607          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
608          ++i;
609          DCHECK_LT(i, environment_size);
610        }
611        break;
612      }
613
614      case Location::kFpuRegister : {
615        int id = location.reg();
616        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
617        if (current->GetType() == Primitive::kPrimDouble) {
618          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
619          ++i;
620          DCHECK_LT(i, environment_size);
621        }
622        break;
623      }
624
625      case Location::kFpuRegisterPair : {
626        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.low());
627        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.high());
628        ++i;
629        DCHECK_LT(i, environment_size);
630        break;
631      }
632
633      default:
634        LOG(FATAL) << "Unexpected kind " << location.GetKind();
635    }
636  }
637}
638
639void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) {
640  RegisterSet* register_set = locations->GetLiveRegisters();
641  size_t stack_offset = first_register_slot_in_slow_path_;
642  for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
643    if (register_set->ContainsCoreRegister(i)) {
644      // If the register holds an object, update the stack mask.
645      if (locations->RegisterContainsObject(i)) {
646        locations->SetStackBit(stack_offset / kVRegSize);
647      }
648      DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
649      stack_offset += SaveCoreRegister(stack_offset, i);
650    }
651  }
652
653  for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
654    if (register_set->ContainsFloatingPointRegister(i)) {
655      DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
656      stack_offset += SaveFloatingPointRegister(stack_offset, i);
657    }
658  }
659}
660
661void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) {
662  RegisterSet* register_set = locations->GetLiveRegisters();
663  size_t stack_offset = first_register_slot_in_slow_path_;
664  for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
665    if (register_set->ContainsCoreRegister(i)) {
666      DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
667      stack_offset += RestoreCoreRegister(stack_offset, i);
668    }
669  }
670
671  for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
672    if (register_set->ContainsFloatingPointRegister(i)) {
673      DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
674      stack_offset += RestoreFloatingPointRegister(stack_offset, i);
675    }
676  }
677}
678
679void CodeGenerator::ClearSpillSlotsFromLoopPhisInStackMap(HSuspendCheck* suspend_check) const {
680  LocationSummary* locations = suspend_check->GetLocations();
681  HBasicBlock* block = suspend_check->GetBlock();
682  DCHECK(block->GetLoopInformation()->GetSuspendCheck() == suspend_check);
683  DCHECK(block->IsLoopHeader());
684
685  for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
686    HInstruction* current = it.Current();
687    LiveInterval* interval = current->GetLiveInterval();
688    // We only need to clear bits of loop phis containing objects and allocated in register.
689    // Loop phis allocated on stack already have the object in the stack.
690    if (current->GetType() == Primitive::kPrimNot
691        && interval->HasRegister()
692        && interval->HasSpillSlot()) {
693      locations->ClearStackBit(interval->GetSpillSlot() / kVRegSize);
694    }
695  }
696}
697
698void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) {
699  HParallelMove parallel_move(GetGraph()->GetArena());
700  parallel_move.AddMove(from1, to1, nullptr);
701  parallel_move.AddMove(from2, to2, nullptr);
702  GetMoveResolver()->EmitNativeCode(&parallel_move);
703}
704
705}  // namespace art
706