Home | History | Annotate | Download | only in src
      1 // Copyright 2011 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #include "v8.h"
     29 
     30 #include "safepoint-table.h"
     31 
     32 #include "deoptimizer.h"
     33 #include "disasm.h"
     34 #include "macro-assembler.h"
     35 #include "zone-inl.h"
     36 
     37 namespace v8 {
     38 namespace internal {
     39 
     40 
     41 bool SafepointEntry::HasRegisters() const {
     42   ASSERT(is_valid());
     43   ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte));
     44   const int num_reg_bytes = kNumSafepointRegisters >> kBitsPerByteLog2;
     45   for (int i = 0; i < num_reg_bytes; i++) {
     46     if (bits_[i] != SafepointTable::kNoRegisters) return true;
     47   }
     48   return false;
     49 }
     50 
     51 
     52 bool SafepointEntry::HasRegisterAt(int reg_index) const {
     53   ASSERT(is_valid());
     54   ASSERT(reg_index >= 0 && reg_index < kNumSafepointRegisters);
     55   int byte_index = reg_index >> kBitsPerByteLog2;
     56   int bit_index = reg_index & (kBitsPerByte - 1);
     57   return (bits_[byte_index] & (1 << bit_index)) != 0;
     58 }
     59 
     60 
     61 SafepointTable::SafepointTable(Code* code) {
     62   ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION);
     63   code_ = code;
     64   Address header = code->instruction_start() + code->safepoint_table_offset();
     65   length_ = Memory::uint32_at(header + kLengthOffset);
     66   entry_size_ = Memory::uint32_at(header + kEntrySizeOffset);
     67   pc_and_deoptimization_indexes_ = header + kHeaderSize;
     68   entries_ = pc_and_deoptimization_indexes_ +
     69             (length_ * kPcAndDeoptimizationIndexSize);
     70   ASSERT(entry_size_ > 0);
     71   ASSERT_EQ(SafepointEntry::DeoptimizationIndexField::max(),
     72             Safepoint::kNoDeoptimizationIndex);
     73 }
     74 
     75 
     76 SafepointEntry SafepointTable::FindEntry(Address pc) const {
     77   unsigned pc_offset = static_cast<unsigned>(pc - code_->instruction_start());
     78   for (unsigned i = 0; i < length(); i++) {
     79     // TODO(kasperl): Replace the linear search with binary search.
     80     if (GetPcOffset(i) == pc_offset) return GetEntry(i);
     81   }
     82   return SafepointEntry();
     83 }
     84 
     85 
     86 void SafepointTable::PrintEntry(unsigned index) const {
     87   disasm::NameConverter converter;
     88   SafepointEntry entry = GetEntry(index);
     89   uint8_t* bits = entry.bits();
     90 
     91   // Print the stack slot bits.
     92   if (entry_size_ > 0) {
     93     ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte));
     94     const int first = kNumSafepointRegisters >> kBitsPerByteLog2;
     95     int last = entry_size_ - 1;
     96     for (int i = first; i < last; i++) PrintBits(bits[i], kBitsPerByte);
     97     int last_bits = code_->stack_slots() - ((last - first) * kBitsPerByte);
     98     PrintBits(bits[last], last_bits);
     99 
    100     // Print the registers (if any).
    101     if (!entry.HasRegisters()) return;
    102     for (int j = 0; j < kNumSafepointRegisters; j++) {
    103       if (entry.HasRegisterAt(j)) {
    104         PrintF(" | %s", converter.NameOfCPURegister(j));
    105       }
    106     }
    107   }
    108 }
    109 
    110 
    111 void SafepointTable::PrintBits(uint8_t byte, int digits) {
    112   ASSERT(digits >= 0 && digits <= kBitsPerByte);
    113   for (int i = 0; i < digits; i++) {
    114     PrintF("%c", ((byte & (1 << i)) == 0) ? '0' : '1');
    115   }
    116 }
    117 
    118 
    119 void Safepoint::DefinePointerRegister(Register reg) {
    120   registers_->Add(reg.code());
    121 }
    122 
    123 
    124 Safepoint SafepointTableBuilder::DefineSafepoint(
    125     Assembler* assembler, Safepoint::Kind kind, int arguments,
    126     int deoptimization_index) {
    127   ASSERT(deoptimization_index != -1);
    128   ASSERT(arguments >= 0);
    129   DeoptimizationInfo pc_and_deoptimization_index;
    130   pc_and_deoptimization_index.pc = assembler->pc_offset();
    131   pc_and_deoptimization_index.deoptimization_index = deoptimization_index;
    132   pc_and_deoptimization_index.pc_after_gap = assembler->pc_offset();
    133   pc_and_deoptimization_index.arguments = arguments;
    134   pc_and_deoptimization_index.has_doubles = (kind & Safepoint::kWithDoubles);
    135   deoptimization_info_.Add(pc_and_deoptimization_index);
    136   indexes_.Add(new ZoneList<int>(8));
    137   registers_.Add((kind & Safepoint::kWithRegisters)
    138       ? new ZoneList<int>(4)
    139       : NULL);
    140   return Safepoint(indexes_.last(), registers_.last());
    141 }
    142 
    143 
    144 unsigned SafepointTableBuilder::GetCodeOffset() const {
    145   ASSERT(emitted_);
    146   return offset_;
    147 }
    148 
    149 
    150 void SafepointTableBuilder::Emit(Assembler* assembler, int bits_per_entry) {
    151   // For lazy deoptimization we need space to patch a call after every call.
    152   // Ensure there is always space for such patching, even if the code ends
    153   // in a call.
    154   int target_offset = assembler->pc_offset() + Deoptimizer::patch_size();
    155   while (assembler->pc_offset() < target_offset) {
    156     assembler->nop();
    157   }
    158 
    159   // Make sure the safepoint table is properly aligned. Pad with nops.
    160   assembler->Align(kIntSize);
    161   assembler->RecordComment(";;; Safepoint table.");
    162   offset_ = assembler->pc_offset();
    163 
    164   // Take the register bits into account.
    165   bits_per_entry += kNumSafepointRegisters;
    166 
    167   // Compute the number of bytes per safepoint entry.
    168   int bytes_per_entry =
    169       RoundUp(bits_per_entry, kBitsPerByte) >> kBitsPerByteLog2;
    170 
    171   // Emit the table header.
    172   int length = deoptimization_info_.length();
    173   assembler->dd(length);
    174   assembler->dd(bytes_per_entry);
    175 
    176   // Emit sorted table of pc offsets together with deoptimization indexes and
    177   // pc after gap information.
    178   for (int i = 0; i < length; i++) {
    179     assembler->dd(deoptimization_info_[i].pc);
    180     assembler->dd(EncodeExceptPC(deoptimization_info_[i]));
    181   }
    182 
    183   // Emit table of bitmaps.
    184   ZoneList<uint8_t> bits(bytes_per_entry);
    185   for (int i = 0; i < length; i++) {
    186     ZoneList<int>* indexes = indexes_[i];
    187     ZoneList<int>* registers = registers_[i];
    188     bits.Clear();
    189     bits.AddBlock(0, bytes_per_entry);
    190 
    191     // Run through the registers (if any).
    192     ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte));
    193     if (registers == NULL) {
    194       const int num_reg_bytes = kNumSafepointRegisters >> kBitsPerByteLog2;
    195       for (int j = 0; j < num_reg_bytes; j++) {
    196         bits[j] = SafepointTable::kNoRegisters;
    197       }
    198     } else {
    199       for (int j = 0; j < registers->length(); j++) {
    200         int index = registers->at(j);
    201         ASSERT(index >= 0 && index < kNumSafepointRegisters);
    202         int byte_index = index >> kBitsPerByteLog2;
    203         int bit_index = index & (kBitsPerByte - 1);
    204         bits[byte_index] |= (1 << bit_index);
    205       }
    206     }
    207 
    208     // Run through the indexes and build a bitmap.
    209     for (int j = 0; j < indexes->length(); j++) {
    210       int index = bits_per_entry - 1 - indexes->at(j);
    211       int byte_index = index >> kBitsPerByteLog2;
    212       int bit_index = index & (kBitsPerByte - 1);
    213       bits[byte_index] |= (1U << bit_index);
    214     }
    215 
    216     // Emit the bitmap for the current entry.
    217     for (int k = 0; k < bytes_per_entry; k++) {
    218       assembler->db(bits[k]);
    219     }
    220   }
    221   emitted_ = true;
    222 }
    223 
    224 
    225 uint32_t SafepointTableBuilder::EncodeExceptPC(const DeoptimizationInfo& info) {
    226   unsigned index = info.deoptimization_index;
    227   unsigned gap_size = info.pc_after_gap - info.pc;
    228   uint32_t encoding = SafepointEntry::DeoptimizationIndexField::encode(index);
    229   encoding |= SafepointEntry::GapCodeSizeField::encode(gap_size);
    230   encoding |= SafepointEntry::ArgumentsField::encode(info.arguments);
    231   encoding |= SafepointEntry::SaveDoublesField::encode(info.has_doubles);
    232   return encoding;
    233 }
    234 
    235 
    236 int SafepointTableBuilder::CountShortDeoptimizationIntervals(unsigned limit) {
    237   int result = 0;
    238   if (!deoptimization_info_.is_empty()) {
    239     unsigned previous_gap_end = deoptimization_info_[0].pc_after_gap;
    240     for (int i = 1, n = deoptimization_info_.length(); i < n; i++) {
    241       DeoptimizationInfo info = deoptimization_info_[i];
    242       if (static_cast<int>(info.deoptimization_index) !=
    243           Safepoint::kNoDeoptimizationIndex) {
    244         if (previous_gap_end + limit > info.pc) {
    245           result++;
    246         }
    247         previous_gap_end = info.pc_after_gap;
    248       }
    249     }
    250   }
    251   return result;
    252 }
    253 
    254 
    255 
    256 } }  // namespace v8::internal
    257