code_generator.cc revision 579885a26d761f5ba9550f2a1cd7f0f598c2e1e3
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
43static 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
53void CodeGenerator::CompileBaseline(CodeAllocator* allocator, bool is_leaf) {
54  Initialize();
55  if (!is_leaf) {
56    MarkNotLeaf();
57  }
58  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}
68
69bool 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
91void CodeGenerator::CompileInternal(CodeAllocator* allocator, bool is_baseline) {
92  HGraphVisitor* instruction_visitor = GetInstructionVisitor();
93  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_);
97    // 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;
101    Bind(block);
102    for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
103      HInstruction* current = it.Current();
104      if (is_baseline) {
105        InitLocationsBaseline(current);
106      }
107      current->Accept(instruction_visitor);
108    }
109  }
110
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;
117  Finalize(allocator);
118}
119
120void CodeGenerator::CompileOptimized(CodeAllocator* allocator) {
121  // The register allocator already called `InitializeCodeGeneration`,
122  // where the frame size has been computed.
123  DCHECK(block_order_ != nullptr);
124  Initialize();
125  CompileInternal(allocator, /* is_baseline */ false);
126}
127
128void CodeGenerator::Finalize(CodeAllocator* allocator) {
129  size_t code_size = GetAssembler()->CodeSize();
130  uint8_t* buffer = allocator->Allocate(code_size);
131
132  MemoryRegion code(buffer, code_size);
133  GetAssembler()->FinalizeInstructions(code);
134}
135
136size_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;
141    }
142  }
143  LOG(FATAL) << "Could not find a register in baseline register allocator";
144  UNREACHABLE();
145  return -1;
146}
147
148size_t CodeGenerator::FindTwoFreeConsecutiveAlignedEntries(bool* array, size_t length) {
149  for (size_t i = 0; i < length - 1; i += 2) {
150    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();
158  return -1;
159}
160
161void 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)));
169  ComputeSpillMask();
170  first_register_slot_in_slow_path_ = (number_of_out_slots + number_of_spill_slots) * kVRegSize;
171
172  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  }
188}
189
190Location CodeGenerator::GetTemporaryLocation(HTemporary* temp) const {
191  uint16_t number_of_locals = GetGraph()->GetNumberOfLocalVRegs();
192  // 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;
195  // Use the temporary region (right below the dex registers).
196  int32_t slot = GetFrameSize() - FrameEntrySpillSize()
197                                - kVRegSize  // filler
198                                - (number_of_locals * kVRegSize)
199                                - ((temp_size + temp->GetIndex()) * kVRegSize);
200  return temp_size == 2 ? Location::DoubleStackSlot(slot) : Location::StackSlot(slot);
201}
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}
218
219void CodeGenerator::AllocateRegistersLocally(HInstruction* instruction) const {
220  LocationSummary* locations = instruction->GetLocations();
221  if (locations == nullptr) return;
222
223  for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
224    blocked_core_registers_[i] = false;
225  }
226
227  for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
228    blocked_fpu_registers_[i] = false;
229  }
230
231  for (size_t i = 0, e = number_of_register_pairs_; i < e; ++i) {
232    blocked_register_pairs_[i] = false;
233  }
234
235  // Mark all fixed input, temp and output registers as used.
236  for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
237    Location loc = locations->InAt(i);
238    // The DCHECKS below check that a register is not specified twice in
239    // the summary.
240    if (loc.IsRegister()) {
241      DCHECK(!blocked_core_registers_[loc.reg()]);
242      blocked_core_registers_[loc.reg()] = true;
243    } else if (loc.IsFpuRegister()) {
244      DCHECK(!blocked_fpu_registers_[loc.reg()]);
245      blocked_fpu_registers_[loc.reg()] = true;
246    } else if (loc.IsFpuRegisterPair()) {
247      DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()]);
248      blocked_fpu_registers_[loc.AsFpuRegisterPairLow<int>()] = true;
249      DCHECK(!blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()]);
250      blocked_fpu_registers_[loc.AsFpuRegisterPairHigh<int>()] = true;
251    } else if (loc.IsRegisterPair()) {
252      DCHECK(!blocked_core_registers_[loc.AsRegisterPairLow<int>()]);
253      blocked_core_registers_[loc.AsRegisterPairLow<int>()] = true;
254      DCHECK(!blocked_core_registers_[loc.AsRegisterPairHigh<int>()]);
255      blocked_core_registers_[loc.AsRegisterPairHigh<int>()] = true;
256    }
257  }
258
259  for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
260    Location loc = locations->GetTemp(i);
261    // The DCHECKS below check that a register is not specified twice in
262    // the summary.
263    if (loc.IsRegister()) {
264      DCHECK(!blocked_core_registers_[loc.reg()]);
265      blocked_core_registers_[loc.reg()] = true;
266    } else if (loc.IsFpuRegister()) {
267      DCHECK(!blocked_fpu_registers_[loc.reg()]);
268      blocked_fpu_registers_[loc.reg()] = true;
269    } else {
270      DCHECK(loc.GetPolicy() == Location::kRequiresRegister
271             || loc.GetPolicy() == Location::kRequiresFpuRegister);
272    }
273  }
274
275  static constexpr bool kBaseline = true;
276  SetupBlockedRegisters(kBaseline);
277
278  // Allocate all unallocated input locations.
279  for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
280    Location loc = locations->InAt(i);
281    HInstruction* input = instruction->InputAt(i);
282    if (loc.IsUnallocated()) {
283      if ((loc.GetPolicy() == Location::kRequiresRegister)
284          || (loc.GetPolicy() == Location::kRequiresFpuRegister)) {
285        loc = AllocateFreeRegister(input->GetType());
286      } else {
287        DCHECK_EQ(loc.GetPolicy(), Location::kAny);
288        HLoadLocal* load = input->AsLoadLocal();
289        if (load != nullptr) {
290          loc = GetStackLocation(load);
291        } else {
292          loc = AllocateFreeRegister(input->GetType());
293        }
294      }
295      locations->SetInAt(i, loc);
296    }
297  }
298
299  // Allocate all unallocated temp locations.
300  for (size_t i = 0, e = locations->GetTempCount(); i < e; ++i) {
301    Location loc = locations->GetTemp(i);
302    if (loc.IsUnallocated()) {
303      switch (loc.GetPolicy()) {
304        case Location::kRequiresRegister:
305          // Allocate a core register (large enough to fit a 32-bit integer).
306          loc = AllocateFreeRegister(Primitive::kPrimInt);
307          break;
308
309        case Location::kRequiresFpuRegister:
310          // Allocate a core register (large enough to fit a 64-bit double).
311          loc = AllocateFreeRegister(Primitive::kPrimDouble);
312          break;
313
314        default:
315          LOG(FATAL) << "Unexpected policy for temporary location "
316                     << loc.GetPolicy();
317      }
318      locations->SetTempAt(i, loc);
319    }
320  }
321  Location result_location = locations->Out();
322  if (result_location.IsUnallocated()) {
323    switch (result_location.GetPolicy()) {
324      case Location::kAny:
325      case Location::kRequiresRegister:
326      case Location::kRequiresFpuRegister:
327        result_location = AllocateFreeRegister(instruction->GetType());
328        break;
329      case Location::kSameAsFirstInput:
330        result_location = locations->InAt(0);
331        break;
332    }
333    locations->UpdateOut(result_location);
334  }
335}
336
337void CodeGenerator::InitLocationsBaseline(HInstruction* instruction) {
338  AllocateLocations(instruction);
339  if (instruction->GetLocations() == nullptr) {
340    if (instruction->IsTemporary()) {
341      HInstruction* previous = instruction->GetPrevious();
342      Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
343      Move(previous, temp_location, instruction);
344    }
345    return;
346  }
347  AllocateRegistersLocally(instruction);
348  for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
349    Location location = instruction->GetLocations()->InAt(i);
350    HInstruction* input = instruction->InputAt(i);
351    if (location.IsValid()) {
352      // Move the input to the desired location.
353      if (input->GetNext()->IsTemporary()) {
354        // If the input was stored in a temporary, use that temporary to
355        // perform the move.
356        Move(input->GetNext(), location, instruction);
357      } else {
358        Move(input, location, instruction);
359      }
360    }
361  }
362}
363
364void CodeGenerator::AllocateLocations(HInstruction* instruction) {
365  instruction->Accept(GetLocationBuilder());
366  LocationSummary* locations = instruction->GetLocations();
367  if (!instruction->IsSuspendCheckEntry()) {
368    if (locations != nullptr && locations->CanCall()) {
369      MarkNotLeaf();
370    }
371    if (instruction->NeedsCurrentMethod()) {
372      SetRequiresCurrentMethod();
373    }
374  }
375}
376
377CodeGenerator* CodeGenerator::Create(HGraph* graph,
378                                     InstructionSet instruction_set,
379                                     const InstructionSetFeatures& isa_features,
380                                     const CompilerOptions& compiler_options) {
381  switch (instruction_set) {
382    case kArm:
383    case kThumb2: {
384      return new arm::CodeGeneratorARM(graph,
385          *isa_features.AsArmInstructionSetFeatures(),
386          compiler_options);
387    }
388    case kArm64: {
389      return new arm64::CodeGeneratorARM64(graph,
390          *isa_features.AsArm64InstructionSetFeatures(),
391          compiler_options);
392    }
393    case kMips:
394      return nullptr;
395    case kX86: {
396      return new x86::CodeGeneratorX86(graph, compiler_options);
397    }
398    case kX86_64: {
399      return new x86_64::CodeGeneratorX86_64(graph, compiler_options);
400    }
401    default:
402      return nullptr;
403  }
404}
405
406void CodeGenerator::BuildNativeGCMap(
407    std::vector<uint8_t>* data, const DexCompilationUnit& dex_compilation_unit) const {
408  const std::vector<uint8_t>& gc_map_raw =
409      dex_compilation_unit.GetVerifiedMethod()->GetDexGcMap();
410  verifier::DexPcToReferenceMap dex_gc_map(&(gc_map_raw)[0]);
411
412  uint32_t max_native_offset = 0;
413  for (size_t i = 0; i < pc_infos_.Size(); i++) {
414    uint32_t native_offset = pc_infos_.Get(i).native_pc;
415    if (native_offset > max_native_offset) {
416      max_native_offset = native_offset;
417    }
418  }
419
420  GcMapBuilder builder(data, pc_infos_.Size(), max_native_offset, dex_gc_map.RegWidth());
421  for (size_t i = 0; i < pc_infos_.Size(); i++) {
422    struct PcInfo pc_info = pc_infos_.Get(i);
423    uint32_t native_offset = pc_info.native_pc;
424    uint32_t dex_pc = pc_info.dex_pc;
425    const uint8_t* references = dex_gc_map.FindBitMap(dex_pc, false);
426    CHECK(references != nullptr) << "Missing ref for dex pc 0x" << std::hex << dex_pc;
427    builder.AddEntry(native_offset, references);
428  }
429}
430
431void CodeGenerator::BuildMappingTable(std::vector<uint8_t>* data, DefaultSrcMap* src_map) const {
432  uint32_t pc2dex_data_size = 0u;
433  uint32_t pc2dex_entries = pc_infos_.Size();
434  uint32_t pc2dex_offset = 0u;
435  int32_t pc2dex_dalvik_offset = 0;
436  uint32_t dex2pc_data_size = 0u;
437  uint32_t dex2pc_entries = 0u;
438  uint32_t dex2pc_offset = 0u;
439  int32_t dex2pc_dalvik_offset = 0;
440
441  if (src_map != nullptr) {
442    src_map->reserve(pc2dex_entries);
443  }
444
445  for (size_t i = 0; i < pc2dex_entries; i++) {
446    struct PcInfo pc_info = pc_infos_.Get(i);
447    pc2dex_data_size += UnsignedLeb128Size(pc_info.native_pc - pc2dex_offset);
448    pc2dex_data_size += SignedLeb128Size(pc_info.dex_pc - pc2dex_dalvik_offset);
449    pc2dex_offset = pc_info.native_pc;
450    pc2dex_dalvik_offset = pc_info.dex_pc;
451    if (src_map != nullptr) {
452      src_map->push_back(SrcMapElem({pc2dex_offset, pc2dex_dalvik_offset}));
453    }
454  }
455
456  // Walk over the blocks and find which ones correspond to catch block entries.
457  for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
458    HBasicBlock* block = graph_->GetBlocks().Get(i);
459    if (block->IsCatchBlock()) {
460      intptr_t native_pc = GetAddressOf(block);
461      ++dex2pc_entries;
462      dex2pc_data_size += UnsignedLeb128Size(native_pc - dex2pc_offset);
463      dex2pc_data_size += SignedLeb128Size(block->GetDexPc() - dex2pc_dalvik_offset);
464      dex2pc_offset = native_pc;
465      dex2pc_dalvik_offset = block->GetDexPc();
466    }
467  }
468
469  uint32_t total_entries = pc2dex_entries + dex2pc_entries;
470  uint32_t hdr_data_size = UnsignedLeb128Size(total_entries) + UnsignedLeb128Size(pc2dex_entries);
471  uint32_t data_size = hdr_data_size + pc2dex_data_size + dex2pc_data_size;
472  data->resize(data_size);
473
474  uint8_t* data_ptr = &(*data)[0];
475  uint8_t* write_pos = data_ptr;
476
477  write_pos = EncodeUnsignedLeb128(write_pos, total_entries);
478  write_pos = EncodeUnsignedLeb128(write_pos, pc2dex_entries);
479  DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size);
480  uint8_t* write_pos2 = write_pos + pc2dex_data_size;
481
482  pc2dex_offset = 0u;
483  pc2dex_dalvik_offset = 0u;
484  dex2pc_offset = 0u;
485  dex2pc_dalvik_offset = 0u;
486
487  for (size_t i = 0; i < pc2dex_entries; i++) {
488    struct PcInfo pc_info = pc_infos_.Get(i);
489    DCHECK(pc2dex_offset <= pc_info.native_pc);
490    write_pos = EncodeUnsignedLeb128(write_pos, pc_info.native_pc - pc2dex_offset);
491    write_pos = EncodeSignedLeb128(write_pos, pc_info.dex_pc - pc2dex_dalvik_offset);
492    pc2dex_offset = pc_info.native_pc;
493    pc2dex_dalvik_offset = pc_info.dex_pc;
494  }
495
496  for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
497    HBasicBlock* block = graph_->GetBlocks().Get(i);
498    if (block->IsCatchBlock()) {
499      intptr_t native_pc = GetAddressOf(block);
500      write_pos2 = EncodeUnsignedLeb128(write_pos2, native_pc - dex2pc_offset);
501      write_pos2 = EncodeSignedLeb128(write_pos2, block->GetDexPc() - dex2pc_dalvik_offset);
502      dex2pc_offset = native_pc;
503      dex2pc_dalvik_offset = block->GetDexPc();
504    }
505  }
506
507
508  DCHECK_EQ(static_cast<size_t>(write_pos - data_ptr), hdr_data_size + pc2dex_data_size);
509  DCHECK_EQ(static_cast<size_t>(write_pos2 - data_ptr), data_size);
510
511  if (kIsDebugBuild) {
512    // Verify the encoded table holds the expected data.
513    MappingTable table(data_ptr);
514    CHECK_EQ(table.TotalSize(), total_entries);
515    CHECK_EQ(table.PcToDexSize(), pc2dex_entries);
516    auto it = table.PcToDexBegin();
517    auto it2 = table.DexToPcBegin();
518    for (size_t i = 0; i < pc2dex_entries; i++) {
519      struct PcInfo pc_info = pc_infos_.Get(i);
520      CHECK_EQ(pc_info.native_pc, it.NativePcOffset());
521      CHECK_EQ(pc_info.dex_pc, it.DexPc());
522      ++it;
523    }
524    for (size_t i = 0; i < graph_->GetBlocks().Size(); ++i) {
525      HBasicBlock* block = graph_->GetBlocks().Get(i);
526      if (block->IsCatchBlock()) {
527        CHECK_EQ(GetAddressOf(block), it2.NativePcOffset());
528        CHECK_EQ(block->GetDexPc(), it2.DexPc());
529        ++it2;
530      }
531    }
532    CHECK(it == table.PcToDexEnd());
533    CHECK(it2 == table.DexToPcEnd());
534  }
535}
536
537void CodeGenerator::BuildVMapTable(std::vector<uint8_t>* data) const {
538  Leb128EncodingVector vmap_encoder;
539  // We currently don't use callee-saved registers.
540  size_t size = 0 + 1 /* marker */ + 0;
541  vmap_encoder.Reserve(size + 1u);  // All values are likely to be one byte in ULEB128 (<128).
542  vmap_encoder.PushBackUnsigned(size);
543  vmap_encoder.PushBackUnsigned(VmapTable::kAdjustedFpMarker);
544
545  *data = vmap_encoder.GetData();
546}
547
548void CodeGenerator::BuildStackMaps(std::vector<uint8_t>* data) {
549  uint32_t size = stack_map_stream_.ComputeNeededSize();
550  data->resize(size);
551  MemoryRegion region(data->data(), size);
552  stack_map_stream_.FillIn(region);
553}
554
555void CodeGenerator::RecordPcInfo(HInstruction* instruction, uint32_t dex_pc) {
556  if (instruction != nullptr) {
557    // The code generated for some type conversions may call the
558    // runtime, thus normally requiring a subsequent call to this
559    // method.  However, the method verifier does not produce PC
560    // information for certain instructions, which are considered "atomic"
561    // (they cannot join a GC).
562    // Therefore we do not currently record PC information for such
563    // instructions.  As this may change later, we added this special
564    // case so that code generators may nevertheless call
565    // CodeGenerator::RecordPcInfo without triggering an error in
566    // CodeGenerator::BuildNativeGCMap ("Missing ref for dex pc 0x")
567    // thereafter.
568    if (instruction->IsTypeConversion()) {
569      return;
570    }
571    if (instruction->IsRem()) {
572      Primitive::Type type = instruction->AsRem()->GetResultType();
573      if ((type == Primitive::kPrimFloat) || (type == Primitive::kPrimDouble)) {
574        return;
575      }
576    }
577  }
578
579  // Collect PC infos for the mapping table.
580  struct PcInfo pc_info;
581  pc_info.dex_pc = dex_pc;
582  pc_info.native_pc = GetAssembler()->CodeSize();
583  pc_infos_.Add(pc_info);
584
585  // Populate stack map information.
586
587  if (instruction == nullptr) {
588    // For stack overflow checks.
589    stack_map_stream_.AddStackMapEntry(dex_pc, pc_info.native_pc, 0, 0, 0, 0);
590    return;
591  }
592
593  LocationSummary* locations = instruction->GetLocations();
594  HEnvironment* environment = instruction->GetEnvironment();
595
596  size_t environment_size = instruction->EnvironmentSize();
597
598  size_t inlining_depth = 0;
599  uint32_t register_mask = locations->GetRegisterMask();
600  if (locations->OnlyCallsOnSlowPath()) {
601    // In case of slow path, we currently set the location of caller-save registers
602    // to register (instead of their stack location when pushed before the slow-path
603    // call). Therefore register_mask contains both callee-save and caller-save
604    // registers that hold objects. We must remove the caller-save from the mask, since
605    // they will be overwritten by the callee.
606    register_mask &= core_callee_save_mask_;
607  }
608  // The register mask must be a subset of callee-save registers.
609  DCHECK_EQ(register_mask & core_callee_save_mask_, register_mask);
610  stack_map_stream_.AddStackMapEntry(
611      dex_pc, pc_info.native_pc, register_mask,
612      locations->GetStackMask(), environment_size, inlining_depth);
613
614  // Walk over the environment, and record the location of dex registers.
615  for (size_t i = 0; i < environment_size; ++i) {
616    HInstruction* current = environment->GetInstructionAt(i);
617    if (current == nullptr) {
618      stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kNone, 0);
619      continue;
620    }
621
622    Location location = locations->GetEnvironmentAt(i);
623    switch (location.GetKind()) {
624      case Location::kConstant: {
625        DCHECK_EQ(current, location.GetConstant());
626        if (current->IsLongConstant()) {
627          int64_t value = current->AsLongConstant()->GetValue();
628          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
629          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
630          ++i;
631          DCHECK_LT(i, environment_size);
632        } else if (current->IsDoubleConstant()) {
633          int64_t value = bit_cast<double, int64_t>(current->AsDoubleConstant()->GetValue());
634          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, Low32Bits(value));
635          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, High32Bits(value));
636          ++i;
637          DCHECK_LT(i, environment_size);
638        } else if (current->IsIntConstant()) {
639          int32_t value = current->AsIntConstant()->GetValue();
640          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
641        } else if (current->IsNullConstant()) {
642          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, 0);
643        } else {
644          DCHECK(current->IsFloatConstant());
645          int32_t value = bit_cast<float, int32_t>(current->AsFloatConstant()->GetValue());
646          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kConstant, value);
647        }
648        break;
649      }
650
651      case Location::kStackSlot: {
652        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
653        break;
654      }
655
656      case Location::kDoubleStackSlot: {
657        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack, location.GetStackIndex());
658        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInStack,
659                                              location.GetHighStackIndex(kVRegSize));
660        ++i;
661        DCHECK_LT(i, environment_size);
662        break;
663      }
664
665      case Location::kRegister : {
666        int id = location.reg();
667        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
668        if (current->GetType() == Primitive::kPrimLong) {
669          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, id);
670          ++i;
671          DCHECK_LT(i, environment_size);
672        }
673        break;
674      }
675
676      case Location::kFpuRegister : {
677        int id = location.reg();
678        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
679        if (current->GetType() == Primitive::kPrimDouble) {
680          stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, id);
681          ++i;
682          DCHECK_LT(i, environment_size);
683        }
684        break;
685      }
686
687      case Location::kFpuRegisterPair : {
688        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.low());
689        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInFpuRegister, location.high());
690        ++i;
691        DCHECK_LT(i, environment_size);
692        break;
693      }
694
695      case Location::kRegisterPair : {
696        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, location.low());
697        stack_map_stream_.AddDexRegisterEntry(DexRegisterMap::kInRegister, location.high());
698        ++i;
699        DCHECK_LT(i, environment_size);
700        break;
701      }
702
703      default:
704        LOG(FATAL) << "Unexpected kind " << location.GetKind();
705    }
706  }
707}
708
709bool CodeGenerator::CanMoveNullCheckToUser(HNullCheck* null_check) {
710  HInstruction* first_next_not_move = null_check->GetNextDisregardingMoves();
711  return (first_next_not_move != nullptr) && first_next_not_move->CanDoImplicitNullCheck();
712}
713
714void CodeGenerator::MaybeRecordImplicitNullCheck(HInstruction* instr) {
715  // If we are from a static path don't record the pc as we can't throw NPE.
716  // NB: having the checks here makes the code much less verbose in the arch
717  // specific code generators.
718  if (instr->IsStaticFieldSet() || instr->IsStaticFieldGet()) {
719    return;
720  }
721
722  if (!compiler_options_.GetImplicitNullChecks()) {
723    return;
724  }
725
726  if (!instr->CanDoImplicitNullCheck()) {
727    return;
728  }
729
730  // Find the first previous instruction which is not a move.
731  HInstruction* first_prev_not_move = instr->GetPreviousDisregardingMoves();
732
733  // If the instruction is a null check it means that `instr` is the first user
734  // and needs to record the pc.
735  if (first_prev_not_move != nullptr && first_prev_not_move->IsNullCheck()) {
736    HNullCheck* null_check = first_prev_not_move->AsNullCheck();
737    // TODO: The parallel moves modify the environment. Their changes need to be reverted
738    // otherwise the stack maps at the throw point will not be correct.
739    RecordPcInfo(null_check, null_check->GetDexPc());
740  }
741}
742
743void CodeGenerator::SaveLiveRegisters(LocationSummary* locations) {
744  RegisterSet* register_set = locations->GetLiveRegisters();
745  size_t stack_offset = first_register_slot_in_slow_path_;
746  for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
747    if (!IsCoreCalleeSaveRegister(i)) {
748      if (register_set->ContainsCoreRegister(i)) {
749        // If the register holds an object, update the stack mask.
750        if (locations->RegisterContainsObject(i)) {
751          locations->SetStackBit(stack_offset / kVRegSize);
752        }
753        DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
754        stack_offset += SaveCoreRegister(stack_offset, i);
755      }
756    }
757  }
758
759  for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
760    if (!IsFloatingPointCalleeSaveRegister(i)) {
761      if (register_set->ContainsFloatingPointRegister(i)) {
762        DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
763        stack_offset += SaveFloatingPointRegister(stack_offset, i);
764      }
765    }
766  }
767}
768
769void CodeGenerator::RestoreLiveRegisters(LocationSummary* locations) {
770  RegisterSet* register_set = locations->GetLiveRegisters();
771  size_t stack_offset = first_register_slot_in_slow_path_;
772  for (size_t i = 0, e = GetNumberOfCoreRegisters(); i < e; ++i) {
773    if (!IsCoreCalleeSaveRegister(i)) {
774      if (register_set->ContainsCoreRegister(i)) {
775        DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
776        stack_offset += RestoreCoreRegister(stack_offset, i);
777      }
778    }
779  }
780
781  for (size_t i = 0, e = GetNumberOfFloatingPointRegisters(); i < e; ++i) {
782    if (!IsFloatingPointCalleeSaveRegister(i)) {
783      if (register_set->ContainsFloatingPointRegister(i)) {
784        DCHECK_LT(stack_offset, GetFrameSize() - FrameEntrySpillSize());
785        stack_offset += RestoreFloatingPointRegister(stack_offset, i);
786      }
787    }
788  }
789}
790
791void 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
810void CodeGenerator::EmitParallelMoves(Location from1, Location to1, Location from2, Location to2) {
811  HParallelMove parallel_move(GetGraph()->GetArena());
812  parallel_move.AddMove(from1, to1, nullptr);
813  parallel_move.AddMove(from2, to2, nullptr);
814  GetMoveResolver()->EmitNativeCode(&parallel_move);
815}
816
817}  // namespace art
818