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