kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 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 "safepoint-table.h" |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 29 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 30 | #include "deoptimizer.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 31 | #include "disasm.h" |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 32 | #include "macro-assembler.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 33 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 37 | |
| 38 | bool SafepointEntry::HasRegisters() const { |
| 39 | ASSERT(is_valid()); |
| 40 | ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte)); |
| 41 | const int num_reg_bytes = kNumSafepointRegisters >> kBitsPerByteLog2; |
| 42 | for (int i = 0; i < num_reg_bytes; i++) { |
| 43 | if (bits_[i] != SafepointTable::kNoRegisters) return true; |
| 44 | } |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | bool SafepointEntry::HasRegisterAt(int reg_index) const { |
| 50 | ASSERT(is_valid()); |
| 51 | ASSERT(reg_index >= 0 && reg_index < kNumSafepointRegisters); |
| 52 | int byte_index = reg_index >> kBitsPerByteLog2; |
| 53 | int bit_index = reg_index & (kBitsPerByte - 1); |
| 54 | return (bits_[byte_index] & (1 << bit_index)) != 0; |
| 55 | } |
| 56 | |
| 57 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 58 | SafepointTable::SafepointTable(Code* code) { |
| 59 | ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION); |
| 60 | code_ = code; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 61 | Address header = code->instruction_start() + code->safepoint_table_offset(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 62 | length_ = Memory::uint32_at(header + kLengthOffset); |
| 63 | entry_size_ = Memory::uint32_at(header + kEntrySizeOffset); |
| 64 | pc_and_deoptimization_indexes_ = header + kHeaderSize; |
| 65 | entries_ = pc_and_deoptimization_indexes_ + |
| 66 | (length_ * kPcAndDeoptimizationIndexSize); |
| 67 | ASSERT(entry_size_ > 0); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 68 | ASSERT_EQ(SafepointEntry::DeoptimizationIndexField::max(), |
| 69 | Safepoint::kNoDeoptimizationIndex); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 73 | SafepointEntry SafepointTable::FindEntry(Address pc) const { |
| 74 | unsigned pc_offset = static_cast<unsigned>(pc - code_->instruction_start()); |
| 75 | for (unsigned i = 0; i < length(); i++) { |
| 76 | // TODO(kasperl): Replace the linear search with binary search. |
| 77 | if (GetPcOffset(i) == pc_offset) return GetEntry(i); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 78 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 79 | return SafepointEntry(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | |
| 83 | void SafepointTable::PrintEntry(unsigned index) const { |
| 84 | disasm::NameConverter converter; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 85 | SafepointEntry entry = GetEntry(index); |
| 86 | uint8_t* bits = entry.bits(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 87 | |
| 88 | // Print the stack slot bits. |
| 89 | if (entry_size_ > 0) { |
| 90 | ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte)); |
| 91 | const int first = kNumSafepointRegisters >> kBitsPerByteLog2; |
| 92 | int last = entry_size_ - 1; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 93 | for (int i = first; i < last; i++) PrintBits(bits[i], kBitsPerByte); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 94 | int last_bits = code_->stack_slots() - ((last - first) * kBitsPerByte); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 95 | PrintBits(bits[last], last_bits); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 96 | |
| 97 | // Print the registers (if any). |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 98 | if (!entry.HasRegisters()) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 99 | for (int j = 0; j < kNumSafepointRegisters; j++) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 100 | if (entry.HasRegisterAt(j)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 101 | PrintF(" | %s", converter.NameOfCPURegister(j)); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void SafepointTable::PrintBits(uint8_t byte, int digits) { |
| 109 | ASSERT(digits >= 0 && digits <= kBitsPerByte); |
| 110 | for (int i = 0; i < digits; i++) { |
| 111 | PrintF("%c", ((byte & (1 << i)) == 0) ? '0' : '1'); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 116 | void Safepoint::DefinePointerRegister(Register reg) { |
| 117 | registers_->Add(reg.code()); |
| 118 | } |
| 119 | |
| 120 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 121 | Safepoint SafepointTableBuilder::DefineSafepoint( |
| 122 | Assembler* assembler, Safepoint::Kind kind, int arguments, |
| 123 | int deoptimization_index) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 124 | ASSERT(deoptimization_index != -1); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 125 | ASSERT(arguments >= 0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 126 | DeoptimizationInfo pc_and_deoptimization_index; |
| 127 | pc_and_deoptimization_index.pc = assembler->pc_offset(); |
| 128 | pc_and_deoptimization_index.deoptimization_index = deoptimization_index; |
| 129 | pc_and_deoptimization_index.pc_after_gap = assembler->pc_offset(); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 130 | pc_and_deoptimization_index.arguments = arguments; |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 131 | pc_and_deoptimization_index.has_doubles = (kind & Safepoint::kWithDoubles); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 132 | deoptimization_info_.Add(pc_and_deoptimization_index); |
| 133 | indexes_.Add(new ZoneList<int>(8)); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 134 | registers_.Add((kind & Safepoint::kWithRegisters) |
| 135 | ? new ZoneList<int>(4) |
| 136 | : NULL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 137 | return Safepoint(indexes_.last(), registers_.last()); |
| 138 | } |
| 139 | |
| 140 | |
| 141 | unsigned SafepointTableBuilder::GetCodeOffset() const { |
| 142 | ASSERT(emitted_); |
| 143 | return offset_; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void SafepointTableBuilder::Emit(Assembler* assembler, int bits_per_entry) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 148 | // For lazy deoptimization we need space to patch a call after every call. |
| 149 | // Ensure there is always space for such patching, even if the code ends |
| 150 | // in a call. |
| 151 | int target_offset = assembler->pc_offset() + Deoptimizer::patch_size(); |
| 152 | while (assembler->pc_offset() < target_offset) { |
| 153 | assembler->nop(); |
| 154 | } |
| 155 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 156 | // Make sure the safepoint table is properly aligned. Pad with nops. |
| 157 | assembler->Align(kIntSize); |
| 158 | assembler->RecordComment(";;; Safepoint table."); |
| 159 | offset_ = assembler->pc_offset(); |
| 160 | |
| 161 | // Take the register bits into account. |
| 162 | bits_per_entry += kNumSafepointRegisters; |
| 163 | |
| 164 | // Compute the number of bytes per safepoint entry. |
| 165 | int bytes_per_entry = |
| 166 | RoundUp(bits_per_entry, kBitsPerByte) >> kBitsPerByteLog2; |
| 167 | |
| 168 | // Emit the table header. |
| 169 | int length = deoptimization_info_.length(); |
| 170 | assembler->dd(length); |
| 171 | assembler->dd(bytes_per_entry); |
| 172 | |
| 173 | // Emit sorted table of pc offsets together with deoptimization indexes and |
| 174 | // pc after gap information. |
| 175 | for (int i = 0; i < length; i++) { |
| 176 | assembler->dd(deoptimization_info_[i].pc); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 177 | assembler->dd(EncodeExceptPC(deoptimization_info_[i])); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Emit table of bitmaps. |
| 181 | ZoneList<uint8_t> bits(bytes_per_entry); |
| 182 | for (int i = 0; i < length; i++) { |
| 183 | ZoneList<int>* indexes = indexes_[i]; |
| 184 | ZoneList<int>* registers = registers_[i]; |
| 185 | bits.Clear(); |
| 186 | bits.AddBlock(0, bytes_per_entry); |
| 187 | |
| 188 | // Run through the registers (if any). |
| 189 | ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte)); |
| 190 | if (registers == NULL) { |
| 191 | const int num_reg_bytes = kNumSafepointRegisters >> kBitsPerByteLog2; |
| 192 | for (int j = 0; j < num_reg_bytes; j++) { |
| 193 | bits[j] = SafepointTable::kNoRegisters; |
| 194 | } |
| 195 | } else { |
| 196 | for (int j = 0; j < registers->length(); j++) { |
| 197 | int index = registers->at(j); |
| 198 | ASSERT(index >= 0 && index < kNumSafepointRegisters); |
| 199 | int byte_index = index >> kBitsPerByteLog2; |
| 200 | int bit_index = index & (kBitsPerByte - 1); |
| 201 | bits[byte_index] |= (1 << bit_index); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Run through the indexes and build a bitmap. |
| 206 | for (int j = 0; j < indexes->length(); j++) { |
| 207 | int index = bits_per_entry - 1 - indexes->at(j); |
| 208 | int byte_index = index >> kBitsPerByteLog2; |
| 209 | int bit_index = index & (kBitsPerByte - 1); |
| 210 | bits[byte_index] |= (1U << bit_index); |
| 211 | } |
| 212 | |
| 213 | // Emit the bitmap for the current entry. |
| 214 | for (int k = 0; k < bytes_per_entry; k++) { |
| 215 | assembler->db(bits[k]); |
| 216 | } |
| 217 | } |
| 218 | emitted_ = true; |
| 219 | } |
| 220 | |
| 221 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 222 | uint32_t SafepointTableBuilder::EncodeExceptPC(const DeoptimizationInfo& info) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 223 | unsigned index = info.deoptimization_index; |
| 224 | unsigned gap_size = info.pc_after_gap - info.pc; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 225 | uint32_t encoding = SafepointEntry::DeoptimizationIndexField::encode(index); |
| 226 | encoding |= SafepointEntry::GapCodeSizeField::encode(gap_size); |
| 227 | encoding |= SafepointEntry::ArgumentsField::encode(info.arguments); |
| 228 | encoding |= SafepointEntry::SaveDoublesField::encode(info.has_doubles); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 229 | return encoding; |
| 230 | } |
| 231 | |
| 232 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 233 | int SafepointTableBuilder::CountShortDeoptimizationIntervals(unsigned limit) { |
| 234 | int result = 0; |
| 235 | if (!deoptimization_info_.is_empty()) { |
| 236 | unsigned previous_gap_end = deoptimization_info_[0].pc_after_gap; |
| 237 | for (int i = 1, n = deoptimization_info_.length(); i < n; i++) { |
| 238 | DeoptimizationInfo info = deoptimization_info_[i]; |
| 239 | if (static_cast<int>(info.deoptimization_index) != |
| 240 | Safepoint::kNoDeoptimizationIndex) { |
| 241 | if (previous_gap_end + limit > info.pc) { |
| 242 | result++; |
| 243 | } |
| 244 | previous_gap_end = info.pc_after_gap; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | return result; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 253 | } } // namespace v8::internal |