Home | History | Annotate | Download | only in optimizing
      1 /*
      2  * Copyright (C) 2015 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 #include "stack_map_stream.h"
     17 
     18 namespace art {
     19 
     20 void StackMapStream::BeginStackMapEntry(uint32_t dex_pc,
     21                                         uint32_t native_pc_offset,
     22                                         uint32_t register_mask,
     23                                         BitVector* sp_mask,
     24                                         uint32_t num_dex_registers,
     25                                         uint8_t inlining_depth) {
     26   DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
     27   DCHECK_NE(dex_pc, static_cast<uint32_t>(-1)) << "invalid dex_pc";
     28   current_entry_.dex_pc = dex_pc;
     29   current_entry_.native_pc_offset = native_pc_offset;
     30   current_entry_.register_mask = register_mask;
     31   current_entry_.sp_mask = sp_mask;
     32   current_entry_.num_dex_registers = num_dex_registers;
     33   current_entry_.inlining_depth = inlining_depth;
     34   current_entry_.dex_register_locations_start_index = dex_register_locations_.size();
     35   current_entry_.inline_infos_start_index = inline_infos_.size();
     36   current_entry_.dex_register_map_hash = 0;
     37   current_entry_.same_dex_register_map_as_ = kNoSameDexMapFound;
     38   if (num_dex_registers != 0) {
     39     current_entry_.live_dex_registers_mask =
     40         ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream);
     41   } else {
     42     current_entry_.live_dex_registers_mask = nullptr;
     43   }
     44 
     45   if (sp_mask != nullptr) {
     46     stack_mask_max_ = std::max(stack_mask_max_, sp_mask->GetHighestBitSet());
     47   }
     48   if (inlining_depth > 0) {
     49     number_of_stack_maps_with_inline_info_++;
     50   }
     51 
     52   dex_pc_max_ = std::max(dex_pc_max_, dex_pc);
     53   register_mask_max_ = std::max(register_mask_max_, register_mask);
     54   current_dex_register_ = 0;
     55 }
     56 
     57 void StackMapStream::EndStackMapEntry() {
     58   current_entry_.same_dex_register_map_as_ = FindEntryWithTheSameDexMap();
     59   stack_maps_.push_back(current_entry_);
     60   current_entry_ = StackMapEntry();
     61 }
     62 
     63 void StackMapStream::AddDexRegisterEntry(DexRegisterLocation::Kind kind, int32_t value) {
     64   if (kind != DexRegisterLocation::Kind::kNone) {
     65     // Ensure we only use non-compressed location kind at this stage.
     66     DCHECK(DexRegisterLocation::IsShortLocationKind(kind)) << kind;
     67     DexRegisterLocation location(kind, value);
     68 
     69     // Look for Dex register `location` in the location catalog (using the
     70     // companion hash map of locations to indices).  Use its index if it
     71     // is already in the location catalog.  If not, insert it (in the
     72     // location catalog and the hash map) and use the newly created index.
     73     auto it = location_catalog_entries_indices_.Find(location);
     74     if (it != location_catalog_entries_indices_.end()) {
     75       // Retrieve the index from the hash map.
     76       dex_register_locations_.push_back(it->second);
     77     } else {
     78       // Create a new entry in the location catalog and the hash map.
     79       size_t index = location_catalog_entries_.size();
     80       location_catalog_entries_.push_back(location);
     81       dex_register_locations_.push_back(index);
     82       location_catalog_entries_indices_.Insert(std::make_pair(location, index));
     83     }
     84 
     85     if (in_inline_frame_) {
     86       // TODO: Support sharing DexRegisterMap across InlineInfo.
     87       DCHECK_LT(current_dex_register_, current_inline_info_.num_dex_registers);
     88       current_inline_info_.live_dex_registers_mask->SetBit(current_dex_register_);
     89     } else {
     90       DCHECK_LT(current_dex_register_, current_entry_.num_dex_registers);
     91       current_entry_.live_dex_registers_mask->SetBit(current_dex_register_);
     92       current_entry_.dex_register_map_hash += (1 <<
     93           (current_dex_register_ % (sizeof(current_entry_.dex_register_map_hash) * kBitsPerByte)));
     94       current_entry_.dex_register_map_hash += static_cast<uint32_t>(value);
     95       current_entry_.dex_register_map_hash += static_cast<uint32_t>(kind);
     96     }
     97   }
     98   current_dex_register_++;
     99 }
    100 
    101 void StackMapStream::BeginInlineInfoEntry(uint32_t method_index,
    102                                           uint32_t dex_pc,
    103                                           InvokeType invoke_type,
    104                                           uint32_t num_dex_registers) {
    105   DCHECK(!in_inline_frame_);
    106   in_inline_frame_ = true;
    107   current_inline_info_.method_index = method_index;
    108   current_inline_info_.dex_pc = dex_pc;
    109   current_inline_info_.invoke_type = invoke_type;
    110   current_inline_info_.num_dex_registers = num_dex_registers;
    111   current_inline_info_.dex_register_locations_start_index = dex_register_locations_.size();
    112   if (num_dex_registers != 0) {
    113     current_inline_info_.live_dex_registers_mask =
    114         ArenaBitVector::Create(allocator_, num_dex_registers, true, kArenaAllocStackMapStream);
    115   } else {
    116     current_inline_info_.live_dex_registers_mask = nullptr;
    117   }
    118   current_dex_register_ = 0;
    119 }
    120 
    121 void StackMapStream::EndInlineInfoEntry() {
    122   DCHECK(in_inline_frame_);
    123   DCHECK_EQ(current_dex_register_, current_inline_info_.num_dex_registers)
    124       << "Inline information contains less registers than expected";
    125   in_inline_frame_ = false;
    126   inline_infos_.push_back(current_inline_info_);
    127   current_inline_info_ = InlineInfoEntry();
    128 }
    129 
    130 uint32_t StackMapStream::ComputeMaxNativePcOffset() const {
    131   uint32_t max_native_pc_offset = 0u;
    132   for (const StackMapEntry& entry : stack_maps_) {
    133     max_native_pc_offset = std::max(max_native_pc_offset, entry.native_pc_offset);
    134   }
    135   return max_native_pc_offset;
    136 }
    137 
    138 size_t StackMapStream::PrepareForFillIn() {
    139   int stack_mask_number_of_bits = stack_mask_max_ + 1;  // Need room for max element too.
    140   dex_register_maps_size_ = ComputeDexRegisterMapsSize();
    141   ComputeInlineInfoEncoding();  // needs dex_register_maps_size_.
    142   inline_info_size_ = inline_infos_.size() * inline_info_encoding_.GetEntrySize();
    143   uint32_t max_native_pc_offset = ComputeMaxNativePcOffset();
    144   size_t stack_map_size = stack_map_encoding_.SetFromSizes(max_native_pc_offset,
    145                                                            dex_pc_max_,
    146                                                            dex_register_maps_size_,
    147                                                            inline_info_size_,
    148                                                            register_mask_max_,
    149                                                            stack_mask_number_of_bits);
    150   stack_maps_size_ = stack_maps_.size() * stack_map_size;
    151   dex_register_location_catalog_size_ = ComputeDexRegisterLocationCatalogSize();
    152 
    153   size_t non_header_size =
    154       stack_maps_size_ +
    155       dex_register_location_catalog_size_ +
    156       dex_register_maps_size_ +
    157       inline_info_size_;
    158 
    159   // Prepare the CodeInfo variable-sized encoding.
    160   CodeInfoEncoding code_info_encoding;
    161   code_info_encoding.non_header_size = non_header_size;
    162   code_info_encoding.number_of_stack_maps = stack_maps_.size();
    163   code_info_encoding.stack_map_size_in_bytes = stack_map_size;
    164   code_info_encoding.stack_map_encoding = stack_map_encoding_;
    165   code_info_encoding.inline_info_encoding = inline_info_encoding_;
    166   code_info_encoding.number_of_location_catalog_entries = location_catalog_entries_.size();
    167   code_info_encoding.Compress(&code_info_encoding_);
    168 
    169   // TODO: Move the catalog at the end. It is currently too expensive at runtime
    170   // to compute its size (note that we do not encode that size in the CodeInfo).
    171   dex_register_location_catalog_start_ = code_info_encoding_.size() + stack_maps_size_;
    172   dex_register_maps_start_ =
    173       dex_register_location_catalog_start_ + dex_register_location_catalog_size_;
    174   inline_infos_start_ = dex_register_maps_start_ + dex_register_maps_size_;
    175 
    176   needed_size_ = code_info_encoding_.size() + non_header_size;
    177   return needed_size_;
    178 }
    179 
    180 size_t StackMapStream::ComputeDexRegisterLocationCatalogSize() const {
    181   size_t size = DexRegisterLocationCatalog::kFixedSize;
    182   for (const DexRegisterLocation& dex_register_location : location_catalog_entries_) {
    183     size += DexRegisterLocationCatalog::EntrySize(dex_register_location);
    184   }
    185   return size;
    186 }
    187 
    188 size_t StackMapStream::ComputeDexRegisterMapSize(uint32_t num_dex_registers,
    189                                                  const BitVector* live_dex_registers_mask) const {
    190   // For num_dex_registers == 0u live_dex_registers_mask may be null.
    191   if (num_dex_registers == 0u) {
    192     return 0u;  // No register map will be emitted.
    193   }
    194   DCHECK(live_dex_registers_mask != nullptr);
    195 
    196   // Size of the map in bytes.
    197   size_t size = DexRegisterMap::kFixedSize;
    198   // Add the live bit mask for the Dex register liveness.
    199   size += DexRegisterMap::GetLiveBitMaskSize(num_dex_registers);
    200   // Compute the size of the set of live Dex register entries.
    201   size_t number_of_live_dex_registers = live_dex_registers_mask->NumSetBits();
    202   size_t map_entries_size_in_bits =
    203       DexRegisterMap::SingleEntrySizeInBits(location_catalog_entries_.size())
    204       * number_of_live_dex_registers;
    205   size_t map_entries_size_in_bytes =
    206       RoundUp(map_entries_size_in_bits, kBitsPerByte) / kBitsPerByte;
    207   size += map_entries_size_in_bytes;
    208   return size;
    209 }
    210 
    211 size_t StackMapStream::ComputeDexRegisterMapsSize() const {
    212   size_t size = 0;
    213   size_t inline_info_index = 0;
    214   for (const StackMapEntry& entry : stack_maps_) {
    215     if (entry.same_dex_register_map_as_ == kNoSameDexMapFound) {
    216       size += ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask);
    217     } else {
    218       // Entries with the same dex map will have the same offset.
    219     }
    220     for (size_t j = 0; j < entry.inlining_depth; ++j) {
    221       InlineInfoEntry inline_entry = inline_infos_[inline_info_index++];
    222       size += ComputeDexRegisterMapSize(inline_entry.num_dex_registers,
    223                                         inline_entry.live_dex_registers_mask);
    224     }
    225   }
    226   return size;
    227 }
    228 
    229 void StackMapStream::ComputeInlineInfoEncoding() {
    230   uint32_t method_index_max = 0;
    231   uint32_t dex_pc_max = 0;
    232   uint32_t invoke_type_max = 0;
    233 
    234   uint32_t inline_info_index = 0;
    235   for (const StackMapEntry& entry : stack_maps_) {
    236     for (size_t j = 0; j < entry.inlining_depth; ++j) {
    237       InlineInfoEntry inline_entry = inline_infos_[inline_info_index++];
    238       method_index_max = std::max(method_index_max, inline_entry.method_index);
    239       dex_pc_max = std::max(dex_pc_max, inline_entry.dex_pc);
    240       invoke_type_max = std::max(invoke_type_max, static_cast<uint32_t>(inline_entry.invoke_type));
    241     }
    242   }
    243   DCHECK_EQ(inline_info_index, inline_infos_.size());
    244 
    245   inline_info_encoding_.SetFromSizes(method_index_max,
    246                                      dex_pc_max,
    247                                      invoke_type_max,
    248                                      dex_register_maps_size_);
    249 }
    250 
    251 void StackMapStream::FillIn(MemoryRegion region) {
    252   DCHECK_EQ(0u, current_entry_.dex_pc) << "EndStackMapEntry not called after BeginStackMapEntry";
    253   DCHECK_NE(0u, needed_size_) << "PrepareForFillIn not called before FillIn";
    254 
    255   DCHECK_EQ(region.size(), needed_size_);
    256 
    257   // Note that the memory region does not have to be zeroed when we JIT code
    258   // because we do not use the arena allocator there.
    259 
    260   // Write the CodeInfo header.
    261   region.CopyFrom(0, MemoryRegion(code_info_encoding_.data(), code_info_encoding_.size()));
    262 
    263   MemoryRegion dex_register_locations_region = region.Subregion(
    264       dex_register_maps_start_, dex_register_maps_size_);
    265 
    266   MemoryRegion inline_infos_region = region.Subregion(
    267       inline_infos_start_, inline_info_size_);
    268 
    269   CodeInfo code_info(region);
    270   CodeInfoEncoding encoding = code_info.ExtractEncoding();
    271   DCHECK_EQ(code_info.GetStackMapsSize(encoding), stack_maps_size_);
    272 
    273   // Set the Dex register location catalog.
    274   MemoryRegion dex_register_location_catalog_region = region.Subregion(
    275       dex_register_location_catalog_start_, dex_register_location_catalog_size_);
    276   DexRegisterLocationCatalog dex_register_location_catalog(dex_register_location_catalog_region);
    277   // Offset in `dex_register_location_catalog` where to store the next
    278   // register location.
    279   size_t location_catalog_offset = DexRegisterLocationCatalog::kFixedSize;
    280   for (DexRegisterLocation dex_register_location : location_catalog_entries_) {
    281     dex_register_location_catalog.SetRegisterInfo(location_catalog_offset, dex_register_location);
    282     location_catalog_offset += DexRegisterLocationCatalog::EntrySize(dex_register_location);
    283   }
    284   // Ensure we reached the end of the Dex registers location_catalog.
    285   DCHECK_EQ(location_catalog_offset, dex_register_location_catalog_region.size());
    286 
    287   ArenaBitVector empty_bitmask(allocator_, 0, /* expandable */ false, kArenaAllocStackMapStream);
    288   uintptr_t next_dex_register_map_offset = 0;
    289   uintptr_t next_inline_info_offset = 0;
    290   for (size_t i = 0, e = stack_maps_.size(); i < e; ++i) {
    291     StackMap stack_map = code_info.GetStackMapAt(i, encoding);
    292     StackMapEntry entry = stack_maps_[i];
    293 
    294     stack_map.SetDexPc(stack_map_encoding_, entry.dex_pc);
    295     stack_map.SetNativePcOffset(stack_map_encoding_, entry.native_pc_offset);
    296     stack_map.SetRegisterMask(stack_map_encoding_, entry.register_mask);
    297     size_t number_of_stack_mask_bits = stack_map.GetNumberOfStackMaskBits(stack_map_encoding_);
    298     if (entry.sp_mask != nullptr) {
    299       for (size_t bit = 0; bit < number_of_stack_mask_bits; bit++) {
    300         stack_map.SetStackMaskBit(stack_map_encoding_, bit, entry.sp_mask->IsBitSet(bit));
    301       }
    302     } else {
    303       // The MemoryRegion does not have to be zeroed, so make sure we clear the bits.
    304       for (size_t bit = 0; bit < number_of_stack_mask_bits; bit++) {
    305         stack_map.SetStackMaskBit(stack_map_encoding_, bit, false);
    306       }
    307     }
    308 
    309     if (entry.num_dex_registers == 0 || (entry.live_dex_registers_mask->NumSetBits() == 0)) {
    310       // No dex map available.
    311       stack_map.SetDexRegisterMapOffset(stack_map_encoding_, StackMap::kNoDexRegisterMap);
    312     } else {
    313       // Search for an entry with the same dex map.
    314       if (entry.same_dex_register_map_as_ != kNoSameDexMapFound) {
    315         // If we have a hit reuse the offset.
    316         stack_map.SetDexRegisterMapOffset(
    317             stack_map_encoding_,
    318             code_info.GetStackMapAt(entry.same_dex_register_map_as_, encoding)
    319                 .GetDexRegisterMapOffset(stack_map_encoding_));
    320       } else {
    321         // New dex registers maps should be added to the stack map.
    322         MemoryRegion register_region = dex_register_locations_region.Subregion(
    323             next_dex_register_map_offset,
    324             ComputeDexRegisterMapSize(entry.num_dex_registers, entry.live_dex_registers_mask));
    325         next_dex_register_map_offset += register_region.size();
    326         DexRegisterMap dex_register_map(register_region);
    327         stack_map.SetDexRegisterMapOffset(
    328             stack_map_encoding_, register_region.start() - dex_register_locations_region.start());
    329 
    330         // Set the dex register location.
    331         FillInDexRegisterMap(dex_register_map,
    332                              entry.num_dex_registers,
    333                              *entry.live_dex_registers_mask,
    334                              entry.dex_register_locations_start_index);
    335       }
    336     }
    337 
    338     // Set the inlining info.
    339     if (entry.inlining_depth != 0) {
    340       MemoryRegion inline_region = inline_infos_region.Subregion(
    341           next_inline_info_offset,
    342           entry.inlining_depth * inline_info_encoding_.GetEntrySize());
    343       next_inline_info_offset += inline_region.size();
    344       InlineInfo inline_info(inline_region);
    345 
    346       // Currently relative to the dex register map.
    347       stack_map.SetInlineDescriptorOffset(
    348           stack_map_encoding_, inline_region.start() - dex_register_locations_region.start());
    349 
    350       inline_info.SetDepth(inline_info_encoding_, entry.inlining_depth);
    351       DCHECK_LE(entry.inline_infos_start_index + entry.inlining_depth, inline_infos_.size());
    352       for (size_t depth = 0; depth < entry.inlining_depth; ++depth) {
    353         InlineInfoEntry inline_entry = inline_infos_[depth + entry.inline_infos_start_index];
    354         inline_info.SetMethodIndexAtDepth(inline_info_encoding_, depth, inline_entry.method_index);
    355         inline_info.SetDexPcAtDepth(inline_info_encoding_, depth, inline_entry.dex_pc);
    356         inline_info.SetInvokeTypeAtDepth(inline_info_encoding_, depth, inline_entry.invoke_type);
    357         if (inline_entry.num_dex_registers == 0) {
    358           // No dex map available.
    359           inline_info.SetDexRegisterMapOffsetAtDepth(inline_info_encoding_,
    360                                                      depth,
    361                                                      StackMap::kNoDexRegisterMap);
    362           DCHECK(inline_entry.live_dex_registers_mask == nullptr);
    363         } else {
    364           MemoryRegion register_region = dex_register_locations_region.Subregion(
    365               next_dex_register_map_offset,
    366               ComputeDexRegisterMapSize(inline_entry.num_dex_registers,
    367                                         inline_entry.live_dex_registers_mask));
    368           next_dex_register_map_offset += register_region.size();
    369           DexRegisterMap dex_register_map(register_region);
    370           inline_info.SetDexRegisterMapOffsetAtDepth(
    371               inline_info_encoding_,
    372               depth, register_region.start() - dex_register_locations_region.start());
    373 
    374           FillInDexRegisterMap(dex_register_map,
    375                                inline_entry.num_dex_registers,
    376                                *inline_entry.live_dex_registers_mask,
    377                                inline_entry.dex_register_locations_start_index);
    378         }
    379       }
    380     } else {
    381       if (inline_info_size_ != 0) {
    382         stack_map.SetInlineDescriptorOffset(stack_map_encoding_, StackMap::kNoInlineInfo);
    383       }
    384     }
    385   }
    386 
    387   // Verify all written data in debug build.
    388   if (kIsDebugBuild) {
    389     CheckCodeInfo(region);
    390   }
    391 }
    392 
    393 void StackMapStream::FillInDexRegisterMap(DexRegisterMap dex_register_map,
    394                                           uint32_t num_dex_registers,
    395                                           const BitVector& live_dex_registers_mask,
    396                                           uint32_t start_index_in_dex_register_locations) const {
    397   dex_register_map.SetLiveBitMask(num_dex_registers, live_dex_registers_mask);
    398   // Set the dex register location mapping data.
    399   size_t number_of_live_dex_registers = live_dex_registers_mask.NumSetBits();
    400   DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size());
    401   DCHECK_LE(start_index_in_dex_register_locations,
    402             dex_register_locations_.size() - number_of_live_dex_registers);
    403   for (size_t index_in_dex_register_locations = 0;
    404       index_in_dex_register_locations != number_of_live_dex_registers;
    405        ++index_in_dex_register_locations) {
    406     size_t location_catalog_entry_index = dex_register_locations_[
    407         start_index_in_dex_register_locations + index_in_dex_register_locations];
    408     dex_register_map.SetLocationCatalogEntryIndex(
    409         index_in_dex_register_locations,
    410         location_catalog_entry_index,
    411         num_dex_registers,
    412         location_catalog_entries_.size());
    413   }
    414 }
    415 
    416 size_t StackMapStream::FindEntryWithTheSameDexMap() {
    417   size_t current_entry_index = stack_maps_.size();
    418   auto entries_it = dex_map_hash_to_stack_map_indices_.find(current_entry_.dex_register_map_hash);
    419   if (entries_it == dex_map_hash_to_stack_map_indices_.end()) {
    420     // We don't have a perfect hash functions so we need a list to collect all stack maps
    421     // which might have the same dex register map.
    422     ArenaVector<uint32_t> stack_map_indices(allocator_->Adapter(kArenaAllocStackMapStream));
    423     stack_map_indices.push_back(current_entry_index);
    424     dex_map_hash_to_stack_map_indices_.Put(current_entry_.dex_register_map_hash,
    425                                            std::move(stack_map_indices));
    426     return kNoSameDexMapFound;
    427   }
    428 
    429   // We might have collisions, so we need to check whether or not we really have a match.
    430   for (uint32_t test_entry_index : entries_it->second) {
    431     if (HaveTheSameDexMaps(GetStackMap(test_entry_index), current_entry_)) {
    432       return test_entry_index;
    433     }
    434   }
    435   entries_it->second.push_back(current_entry_index);
    436   return kNoSameDexMapFound;
    437 }
    438 
    439 bool StackMapStream::HaveTheSameDexMaps(const StackMapEntry& a, const StackMapEntry& b) const {
    440   if (a.live_dex_registers_mask == nullptr && b.live_dex_registers_mask == nullptr) {
    441     return true;
    442   }
    443   if (a.live_dex_registers_mask == nullptr || b.live_dex_registers_mask == nullptr) {
    444     return false;
    445   }
    446   if (a.num_dex_registers != b.num_dex_registers) {
    447     return false;
    448   }
    449   if (a.num_dex_registers != 0u) {
    450     DCHECK(a.live_dex_registers_mask != nullptr);
    451     DCHECK(b.live_dex_registers_mask != nullptr);
    452     if (!a.live_dex_registers_mask->Equal(b.live_dex_registers_mask)) {
    453       return false;
    454     }
    455     size_t number_of_live_dex_registers = a.live_dex_registers_mask->NumSetBits();
    456     DCHECK_LE(number_of_live_dex_registers, dex_register_locations_.size());
    457     DCHECK_LE(a.dex_register_locations_start_index,
    458               dex_register_locations_.size() - number_of_live_dex_registers);
    459     DCHECK_LE(b.dex_register_locations_start_index,
    460               dex_register_locations_.size() - number_of_live_dex_registers);
    461     auto a_begin = dex_register_locations_.begin() + a.dex_register_locations_start_index;
    462     auto b_begin = dex_register_locations_.begin() + b.dex_register_locations_start_index;
    463     if (!std::equal(a_begin, a_begin + number_of_live_dex_registers, b_begin)) {
    464       return false;
    465     }
    466   }
    467   return true;
    468 }
    469 
    470 // Helper for CheckCodeInfo - check that register map has the expected content.
    471 void StackMapStream::CheckDexRegisterMap(const CodeInfo& code_info,
    472                                          const DexRegisterMap& dex_register_map,
    473                                          size_t num_dex_registers,
    474                                          BitVector* live_dex_registers_mask,
    475                                          size_t dex_register_locations_index) const {
    476   CodeInfoEncoding encoding = code_info.ExtractEncoding();
    477   for (size_t reg = 0; reg < num_dex_registers; reg++) {
    478     // Find the location we tried to encode.
    479     DexRegisterLocation expected = DexRegisterLocation::None();
    480     if (live_dex_registers_mask->IsBitSet(reg)) {
    481       size_t catalog_index = dex_register_locations_[dex_register_locations_index++];
    482       expected = location_catalog_entries_[catalog_index];
    483     }
    484     // Compare to the seen location.
    485     if (expected.GetKind() == DexRegisterLocation::Kind::kNone) {
    486       DCHECK(!dex_register_map.IsValid() || !dex_register_map.IsDexRegisterLive(reg));
    487     } else {
    488       DCHECK(dex_register_map.IsDexRegisterLive(reg));
    489       DexRegisterLocation seen = dex_register_map.GetDexRegisterLocation(
    490           reg, num_dex_registers, code_info, encoding);
    491       DCHECK_EQ(expected.GetKind(), seen.GetKind());
    492       DCHECK_EQ(expected.GetValue(), seen.GetValue());
    493     }
    494   }
    495   if (num_dex_registers == 0) {
    496     DCHECK(!dex_register_map.IsValid());
    497   }
    498 }
    499 
    500 // Check that all StackMapStream inputs are correctly encoded by trying to read them back.
    501 void StackMapStream::CheckCodeInfo(MemoryRegion region) const {
    502   CodeInfo code_info(region);
    503   CodeInfoEncoding encoding = code_info.ExtractEncoding();
    504   DCHECK_EQ(code_info.GetNumberOfStackMaps(encoding), stack_maps_.size());
    505   for (size_t s = 0; s < stack_maps_.size(); ++s) {
    506     const StackMap stack_map = code_info.GetStackMapAt(s, encoding);
    507     const StackMapEncoding& stack_map_encoding = encoding.stack_map_encoding;
    508     StackMapEntry entry = stack_maps_[s];
    509 
    510     // Check main stack map fields.
    511     DCHECK_EQ(stack_map.GetNativePcOffset(stack_map_encoding), entry.native_pc_offset);
    512     DCHECK_EQ(stack_map.GetDexPc(stack_map_encoding), entry.dex_pc);
    513     DCHECK_EQ(stack_map.GetRegisterMask(stack_map_encoding), entry.register_mask);
    514     size_t num_stack_mask_bits = stack_map.GetNumberOfStackMaskBits(stack_map_encoding);
    515     if (entry.sp_mask != nullptr) {
    516       DCHECK_GE(num_stack_mask_bits, entry.sp_mask->GetNumberOfBits());
    517       for (size_t b = 0; b < num_stack_mask_bits; b++) {
    518         DCHECK_EQ(stack_map.GetStackMaskBit(stack_map_encoding, b), entry.sp_mask->IsBitSet(b));
    519       }
    520     } else {
    521       for (size_t b = 0; b < num_stack_mask_bits; b++) {
    522         DCHECK_EQ(stack_map.GetStackMaskBit(stack_map_encoding, b), 0u);
    523       }
    524     }
    525 
    526     CheckDexRegisterMap(code_info,
    527                         code_info.GetDexRegisterMapOf(
    528                             stack_map, encoding, entry.num_dex_registers),
    529                         entry.num_dex_registers,
    530                         entry.live_dex_registers_mask,
    531                         entry.dex_register_locations_start_index);
    532 
    533     // Check inline info.
    534     DCHECK_EQ(stack_map.HasInlineInfo(stack_map_encoding), (entry.inlining_depth != 0));
    535     if (entry.inlining_depth != 0) {
    536       InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
    537       DCHECK_EQ(inline_info.GetDepth(encoding.inline_info_encoding), entry.inlining_depth);
    538       for (size_t d = 0; d < entry.inlining_depth; ++d) {
    539         size_t inline_info_index = entry.inline_infos_start_index + d;
    540         DCHECK_LT(inline_info_index, inline_infos_.size());
    541         InlineInfoEntry inline_entry = inline_infos_[inline_info_index];
    542         DCHECK_EQ(inline_info.GetDexPcAtDepth(encoding.inline_info_encoding, d),
    543                   inline_entry.dex_pc);
    544         DCHECK_EQ(inline_info.GetMethodIndexAtDepth(encoding.inline_info_encoding, d),
    545                   inline_entry.method_index);
    546         DCHECK_EQ(inline_info.GetInvokeTypeAtDepth(encoding.inline_info_encoding, d),
    547                   inline_entry.invoke_type);
    548 
    549         CheckDexRegisterMap(code_info,
    550                             code_info.GetDexRegisterMapAtDepth(
    551                                 d, inline_info, encoding, inline_entry.num_dex_registers),
    552                             inline_entry.num_dex_registers,
    553                             inline_entry.live_dex_registers_mask,
    554                             inline_entry.dex_register_locations_start_index);
    555       }
    556     }
    557   }
    558 }
    559 
    560 }  // namespace art
    561