2011-02-02 13:55:29 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/safepoint-table.h"
|
2011-01-12 14:14:14 +00:00
|
|
|
|
2017-03-15 11:38:48 +00:00
|
|
|
#include "src/assembler-inl.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/deoptimizer.h"
|
|
|
|
#include "src/disasm.h"
|
2015-08-12 10:28:34 +00:00
|
|
|
#include "src/frames-inl.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/macro-assembler.h"
|
2014-07-30 13:54:45 +00:00
|
|
|
#include "src/ostreams.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2011-01-12 14:14:14 +00:00
|
|
|
|
|
|
|
bool SafepointEntry::HasRegisters() const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(is_valid());
|
|
|
|
DCHECK(IsAligned(kNumSafepointRegisters, kBitsPerByte));
|
2011-01-12 14:14:14 +00:00
|
|
|
const int num_reg_bytes = kNumSafepointRegisters >> kBitsPerByteLog2;
|
|
|
|
for (int i = 0; i < num_reg_bytes; i++) {
|
|
|
|
if (bits_[i] != SafepointTable::kNoRegisters) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SafepointEntry::HasRegisterAt(int reg_index) const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(is_valid());
|
|
|
|
DCHECK(reg_index >= 0 && reg_index < kNumSafepointRegisters);
|
2011-01-12 14:14:14 +00:00
|
|
|
int byte_index = reg_index >> kBitsPerByteLog2;
|
|
|
|
int bit_index = reg_index & (kBitsPerByte - 1);
|
|
|
|
return (bits_[byte_index] & (1 << bit_index)) != 0;
|
|
|
|
}
|
|
|
|
|
2017-11-10 19:26:47 +00:00
|
|
|
SafepointTable::SafepointTable(Address instruction_start,
|
|
|
|
size_t safepoint_table_offset,
|
|
|
|
uint32_t stack_slots, bool has_deopt)
|
|
|
|
: instruction_start_(instruction_start),
|
|
|
|
stack_slots_(stack_slots),
|
|
|
|
has_deopt_(has_deopt) {
|
|
|
|
Address header = instruction_start_ + safepoint_table_offset;
|
2010-12-07 11:31:57 +00:00
|
|
|
length_ = Memory::uint32_at(header + kLengthOffset);
|
|
|
|
entry_size_ = Memory::uint32_at(header + kEntrySizeOffset);
|
|
|
|
pc_and_deoptimization_indexes_ = header + kHeaderSize;
|
2017-08-02 07:55:46 +00:00
|
|
|
entries_ = pc_and_deoptimization_indexes_ + (length_ * kFixedEntrySize);
|
2017-10-18 09:06:55 +00:00
|
|
|
DCHECK_GT(entry_size_, 0);
|
2011-09-12 10:50:50 +00:00
|
|
|
STATIC_ASSERT(SafepointEntry::DeoptimizationIndexField::kMax ==
|
|
|
|
Safepoint::kNoDeoptimizationIndex);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 19:26:47 +00:00
|
|
|
SafepointTable::SafepointTable(Code* code)
|
2018-02-26 14:11:52 +00:00
|
|
|
: SafepointTable(code->InstructionStart(), code->safepoint_table_offset(),
|
|
|
|
code->stack_slots(), true) {}
|
2017-11-10 19:26:47 +00:00
|
|
|
|
2017-08-22 09:38:51 +00:00
|
|
|
unsigned SafepointTable::find_return_pc(unsigned pc_offset) {
|
|
|
|
for (unsigned i = 0; i < length(); i++) {
|
|
|
|
if (GetTrampolinePcOffset(i) == static_cast<int>(pc_offset)) {
|
|
|
|
return GetPcOffset(i);
|
|
|
|
} else if (GetPcOffset(i) == pc_offset) {
|
|
|
|
return pc_offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
return 0;
|
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2011-01-12 14:14:14 +00:00
|
|
|
SafepointEntry SafepointTable::FindEntry(Address pc) const {
|
2017-11-10 19:26:47 +00:00
|
|
|
unsigned pc_offset = static_cast<unsigned>(pc - instruction_start_);
|
2017-05-23 13:47:33 +00:00
|
|
|
// We use kMaxUInt32 as sentinel value, so check that we don't hit that.
|
|
|
|
DCHECK_NE(kMaxUInt32, pc_offset);
|
|
|
|
unsigned len = length();
|
|
|
|
// If pc == kMaxUInt32, then this entry covers all call sites in the function.
|
|
|
|
if (len == 1 && GetPcOffset(0) == kMaxUInt32) return GetEntry(0);
|
|
|
|
for (unsigned i = 0; i < len; i++) {
|
2011-01-12 14:14:14 +00:00
|
|
|
// TODO(kasperl): Replace the linear search with binary search.
|
2017-08-22 09:38:51 +00:00
|
|
|
if (GetPcOffset(i) == pc_offset ||
|
2017-11-10 19:26:47 +00:00
|
|
|
(has_deopt_ &&
|
|
|
|
GetTrampolinePcOffset(i) == static_cast<int>(pc_offset))) {
|
2017-08-02 07:55:46 +00:00
|
|
|
return GetEntry(i);
|
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
2017-08-02 07:55:46 +00:00
|
|
|
UNREACHABLE();
|
2011-01-12 14:14:14 +00:00
|
|
|
return SafepointEntry();
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
void SafepointTable::PrintEntry(unsigned index,
|
|
|
|
std::ostream& os) const { // NOLINT
|
2010-12-07 11:31:57 +00:00
|
|
|
disasm::NameConverter converter;
|
2011-01-12 14:14:14 +00:00
|
|
|
SafepointEntry entry = GetEntry(index);
|
|
|
|
uint8_t* bits = entry.bits();
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
// Print the stack slot bits.
|
|
|
|
if (entry_size_ > 0) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsAligned(kNumSafepointRegisters, kBitsPerByte));
|
2010-12-07 11:31:57 +00:00
|
|
|
const int first = kNumSafepointRegisters >> kBitsPerByteLog2;
|
|
|
|
int last = entry_size_ - 1;
|
2014-07-07 09:57:29 +00:00
|
|
|
for (int i = first; i < last; i++) PrintBits(os, bits[i], kBitsPerByte);
|
2017-11-10 19:26:47 +00:00
|
|
|
int last_bits = stack_slots_ - ((last - first) * kBitsPerByte);
|
2014-07-07 09:57:29 +00:00
|
|
|
PrintBits(os, bits[last], last_bits);
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
// Print the registers (if any).
|
2011-01-12 14:14:14 +00:00
|
|
|
if (!entry.HasRegisters()) return;
|
2010-12-07 11:31:57 +00:00
|
|
|
for (int j = 0; j < kNumSafepointRegisters; j++) {
|
2011-01-12 14:14:14 +00:00
|
|
|
if (entry.HasRegisterAt(j)) {
|
2014-07-07 09:57:29 +00:00
|
|
|
os << " | " << converter.NameOfCPURegister(j);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-30 10:29:32 +00:00
|
|
|
void SafepointTable::PrintBits(std::ostream& os, // NOLINT
|
2014-07-07 09:57:29 +00:00
|
|
|
uint8_t byte, int digits) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(digits >= 0 && digits <= kBitsPerByte);
|
2010-12-07 11:31:57 +00:00
|
|
|
for (int i = 0; i < digits; i++) {
|
2014-07-07 09:57:29 +00:00
|
|
|
os << (((byte & (1 << i)) == 0) ? "0" : "1");
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-11 12:42:31 +00:00
|
|
|
void Safepoint::DefinePointerRegister(Register reg, Zone* zone) {
|
|
|
|
registers_->Add(reg.code(), zone);
|
2011-01-12 14:14:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-26 20:48:48 +00:00
|
|
|
Safepoint SafepointTableBuilder::DefineSafepoint(
|
2011-11-16 08:44:30 +00:00
|
|
|
Assembler* assembler,
|
|
|
|
Safepoint::Kind kind,
|
|
|
|
int arguments,
|
|
|
|
Safepoint::DeoptMode deopt_mode) {
|
2017-10-18 09:06:55 +00:00
|
|
|
DCHECK_GE(arguments, 0);
|
2017-12-11 17:19:22 +00:00
|
|
|
deoptimization_info_.Add(
|
|
|
|
DeoptimizationInfo(zone_, assembler->pc_offset(), arguments, kind),
|
|
|
|
zone_);
|
2011-11-16 08:44:30 +00:00
|
|
|
if (deopt_mode == Safepoint::kNoLazyDeopt) {
|
2017-12-11 17:19:22 +00:00
|
|
|
last_lazy_safepoint_ = deoptimization_info_.length();
|
2011-11-16 08:44:30 +00:00
|
|
|
}
|
2017-12-11 17:19:22 +00:00
|
|
|
DeoptimizationInfo& new_info = deoptimization_info_.last();
|
|
|
|
return Safepoint(new_info.indexes, new_info.registers);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-16 08:44:30 +00:00
|
|
|
void SafepointTableBuilder::RecordLazyDeoptimizationIndex(int index) {
|
2017-12-11 17:19:22 +00:00
|
|
|
while (last_lazy_safepoint_ < deoptimization_info_.length()) {
|
|
|
|
deoptimization_info_[last_lazy_safepoint_++].deopt_index = index;
|
2011-11-16 08:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
unsigned SafepointTableBuilder::GetCodeOffset() const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(emitted_);
|
2010-12-07 11:31:57 +00:00
|
|
|
return offset_;
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:38:51 +00:00
|
|
|
int SafepointTableBuilder::UpdateDeoptimizationInfo(int pc, int trampoline,
|
|
|
|
int start) {
|
2017-08-02 07:55:46 +00:00
|
|
|
int index = -1;
|
2017-08-22 09:38:51 +00:00
|
|
|
for (int i = start; i < deoptimization_info_.length(); i++) {
|
2017-08-02 07:55:46 +00:00
|
|
|
if (static_cast<int>(deoptimization_info_[i].pc) == pc) {
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 09:06:55 +00:00
|
|
|
CHECK_GE(index, 0);
|
2017-08-02 07:55:46 +00:00
|
|
|
DCHECK(index < deoptimization_info_.length());
|
2017-08-22 09:38:51 +00:00
|
|
|
deoptimization_info_[index].trampoline = trampoline;
|
|
|
|
return index;
|
2017-08-02 07:55:46 +00:00
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
void SafepointTableBuilder::Emit(Assembler* assembler, int bits_per_entry) {
|
2017-05-23 13:47:33 +00:00
|
|
|
RemoveDuplicates();
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// Make sure the safepoint table is properly aligned. Pad with nops.
|
|
|
|
assembler->Align(kIntSize);
|
|
|
|
assembler->RecordComment(";;; Safepoint table.");
|
|
|
|
offset_ = assembler->pc_offset();
|
|
|
|
|
|
|
|
// Take the register bits into account.
|
|
|
|
bits_per_entry += kNumSafepointRegisters;
|
|
|
|
|
|
|
|
// Compute the number of bytes per safepoint entry.
|
|
|
|
int bytes_per_entry =
|
|
|
|
RoundUp(bits_per_entry, kBitsPerByte) >> kBitsPerByteLog2;
|
|
|
|
|
|
|
|
// Emit the table header.
|
|
|
|
int length = deoptimization_info_.length();
|
|
|
|
assembler->dd(length);
|
|
|
|
assembler->dd(bytes_per_entry);
|
|
|
|
|
2011-11-16 08:44:30 +00:00
|
|
|
// Emit sorted table of pc offsets together with deoptimization indexes.
|
2010-12-07 11:31:57 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
2017-12-11 17:19:22 +00:00
|
|
|
const DeoptimizationInfo& info = deoptimization_info_[i];
|
|
|
|
assembler->dd(info.pc);
|
|
|
|
assembler->dd(EncodeExceptPC(info));
|
|
|
|
assembler->dd(info.trampoline);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit table of bitmaps.
|
2012-06-11 16:47:08 +00:00
|
|
|
ZoneList<uint8_t> bits(bytes_per_entry, zone_);
|
2010-12-07 11:31:57 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
2017-12-11 17:19:22 +00:00
|
|
|
ZoneList<int>* indexes = deoptimization_info_[i].indexes;
|
|
|
|
ZoneList<int>* registers = deoptimization_info_[i].registers;
|
2010-12-07 11:31:57 +00:00
|
|
|
bits.Clear();
|
2012-06-11 16:47:08 +00:00
|
|
|
bits.AddBlock(0, bytes_per_entry, zone_);
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
// Run through the registers (if any).
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(IsAligned(kNumSafepointRegisters, kBitsPerByte));
|
2017-10-13 16:33:03 +00:00
|
|
|
if (registers == nullptr) {
|
2010-12-07 11:31:57 +00:00
|
|
|
const int num_reg_bytes = kNumSafepointRegisters >> kBitsPerByteLog2;
|
|
|
|
for (int j = 0; j < num_reg_bytes; j++) {
|
|
|
|
bits[j] = SafepointTable::kNoRegisters;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int j = 0; j < registers->length(); j++) {
|
|
|
|
int index = registers->at(j);
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(index >= 0 && index < kNumSafepointRegisters);
|
2010-12-07 11:31:57 +00:00
|
|
|
int byte_index = index >> kBitsPerByteLog2;
|
|
|
|
int bit_index = index & (kBitsPerByte - 1);
|
|
|
|
bits[byte_index] |= (1 << bit_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run through the indexes and build a bitmap.
|
|
|
|
for (int j = 0; j < indexes->length(); j++) {
|
|
|
|
int index = bits_per_entry - 1 - indexes->at(j);
|
|
|
|
int byte_index = index >> kBitsPerByteLog2;
|
|
|
|
int bit_index = index & (kBitsPerByte - 1);
|
|
|
|
bits[byte_index] |= (1U << bit_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the bitmap for the current entry.
|
|
|
|
for (int k = 0; k < bytes_per_entry; k++) {
|
|
|
|
assembler->db(bits[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emitted_ = true;
|
|
|
|
}
|
|
|
|
|
2017-12-11 17:19:22 +00:00
|
|
|
uint32_t SafepointTableBuilder::EncodeExceptPC(const DeoptimizationInfo& info) {
|
|
|
|
return SafepointEntry::DeoptimizationIndexField::encode(info.deopt_index) |
|
|
|
|
SafepointEntry::ArgumentsField::encode(info.arguments) |
|
|
|
|
SafepointEntry::SaveDoublesField::encode(info.has_doubles);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 13:47:33 +00:00
|
|
|
void SafepointTableBuilder::RemoveDuplicates() {
|
2017-05-23 18:53:43 +00:00
|
|
|
// If the table contains more than one entry, and all entries are identical
|
|
|
|
// (except for the pc), replace the whole table by a single entry with pc =
|
|
|
|
// kMaxUInt32. This especially compacts the table for wasm code without tagged
|
|
|
|
// pointers and without deoptimization info.
|
|
|
|
|
2017-05-23 13:47:33 +00:00
|
|
|
int length = deoptimization_info_.length();
|
|
|
|
if (length < 2) return;
|
|
|
|
|
2017-05-23 18:53:43 +00:00
|
|
|
// Check that all entries (1, length] are identical to entry 0.
|
2017-12-11 17:19:22 +00:00
|
|
|
const DeoptimizationInfo& first_info = deoptimization_info_[0];
|
2017-05-23 13:47:33 +00:00
|
|
|
for (int i = 1; i < length; ++i) {
|
2017-12-11 17:19:22 +00:00
|
|
|
if (!IsIdenticalExceptForPc(first_info, deoptimization_info_[i])) return;
|
2017-05-23 13:47:33 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 17:19:22 +00:00
|
|
|
// If we get here, all entries were identical. Rewind the list to just one
|
2017-05-23 13:47:33 +00:00
|
|
|
// entry, and set the pc to kMaxUInt32.
|
|
|
|
deoptimization_info_.Rewind(1);
|
|
|
|
deoptimization_info_[0].pc = kMaxUInt32;
|
|
|
|
}
|
|
|
|
|
2017-12-11 17:19:22 +00:00
|
|
|
bool SafepointTableBuilder::IsIdenticalExceptForPc(
|
|
|
|
const DeoptimizationInfo& info1, const DeoptimizationInfo& info2) const {
|
|
|
|
if (info1.arguments != info2.arguments) return false;
|
|
|
|
if (info1.has_doubles != info2.has_doubles) return false;
|
2017-05-23 13:47:33 +00:00
|
|
|
|
2017-12-11 17:19:22 +00:00
|
|
|
if (info1.deopt_index != info2.deopt_index) return false;
|
2017-05-23 13:47:33 +00:00
|
|
|
|
2017-12-11 17:19:22 +00:00
|
|
|
ZoneList<int>* indexes1 = info1.indexes;
|
|
|
|
ZoneList<int>* indexes2 = info2.indexes;
|
2017-05-23 13:47:33 +00:00
|
|
|
if (indexes1->length() != indexes2->length()) return false;
|
|
|
|
for (int i = 0; i < indexes1->length(); ++i) {
|
|
|
|
if (indexes1->at(i) != indexes2->at(i)) return false;
|
|
|
|
}
|
|
|
|
|
2017-12-11 17:19:22 +00:00
|
|
|
ZoneList<int>* registers1 = info1.registers;
|
|
|
|
ZoneList<int>* registers2 = info2.registers;
|
2017-05-23 13:47:33 +00:00
|
|
|
if (registers1) {
|
2017-05-23 18:53:43 +00:00
|
|
|
if (!registers2) return false;
|
2017-05-23 13:47:33 +00:00
|
|
|
if (registers1->length() != registers2->length()) return false;
|
|
|
|
for (int i = 0; i < registers1->length(); ++i) {
|
|
|
|
if (registers1->at(i) != registers2->at(i)) return false;
|
|
|
|
}
|
|
|
|
} else if (registers2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2015-06-01 22:46:54 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|