safepoint-table.cc 10.6 KB
Newer Older
1
// Copyright 2011 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/safepoint-table.h"
6

7
#include "src/assembler-inl.h"
8 9
#include "src/deoptimizer.h"
#include "src/disasm.h"
10
#include "src/frames-inl.h"
11
#include "src/macro-assembler.h"
12
#include "src/ostreams.h"
13 14 15 16

namespace v8 {
namespace internal {

17 18

bool SafepointEntry::HasRegisters() const {
19 20
  DCHECK(is_valid());
  DCHECK(IsAligned(kNumSafepointRegisters, kBitsPerByte));
21 22 23 24 25 26 27 28 29
  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 {
30 31
  DCHECK(is_valid());
  DCHECK(reg_index >= 0 && reg_index < kNumSafepointRegisters);
32 33 34 35 36
  int byte_index = reg_index >> kBitsPerByteLog2;
  int bit_index = reg_index & (kBitsPerByte - 1);
  return (bits_[byte_index] & (1 << bit_index)) != 0;
}

37 38 39 40 41 42 43
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;
44 45 46
  length_ = Memory::uint32_at(header + kLengthOffset);
  entry_size_ = Memory::uint32_at(header + kEntrySizeOffset);
  pc_and_deoptimization_indexes_ = header + kHeaderSize;
47
  entries_ = pc_and_deoptimization_indexes_ + (length_ * kFixedEntrySize);
48
  DCHECK_GT(entry_size_, 0);
49 50
  STATIC_ASSERT(SafepointEntry::DeoptimizationIndexField::kMax ==
                Safepoint::kNoDeoptimizationIndex);
51 52
}

53 54 55 56 57 58
SafepointTable::SafepointTable(Code* code)
    : SafepointTable(code->instruction_start(), code->safepoint_table_offset(),
                     code->stack_slots(), true) {
  DCHECK(code->is_turbofanned());
}

59 60 61 62 63 64 65 66 67 68 69
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;
}
70

71
SafepointEntry SafepointTable::FindEntry(Address pc) const {
72
  unsigned pc_offset = static_cast<unsigned>(pc - instruction_start_);
73 74 75 76 77 78
  // 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++) {
79
    // TODO(kasperl): Replace the linear search with binary search.
80
    if (GetPcOffset(i) == pc_offset ||
81 82
        (has_deopt_ &&
         GetTrampolinePcOffset(i) == static_cast<int>(pc_offset))) {
83 84
      return GetEntry(i);
    }
85
  }
86
  UNREACHABLE();
87
  return SafepointEntry();
88 89 90
}


91 92
void SafepointTable::PrintEntry(unsigned index,
                                std::ostream& os) const {  // NOLINT
93
  disasm::NameConverter converter;
94 95
  SafepointEntry entry = GetEntry(index);
  uint8_t* bits = entry.bits();
96 97 98

  // Print the stack slot bits.
  if (entry_size_ > 0) {
99
    DCHECK(IsAligned(kNumSafepointRegisters, kBitsPerByte));
100 101
    const int first = kNumSafepointRegisters >> kBitsPerByteLog2;
    int last = entry_size_ - 1;
102
    for (int i = first; i < last; i++) PrintBits(os, bits[i], kBitsPerByte);
103
    int last_bits = stack_slots_ - ((last - first) * kBitsPerByte);
104
    PrintBits(os, bits[last], last_bits);
105 106

    // Print the registers (if any).
107
    if (!entry.HasRegisters()) return;
108
    for (int j = 0; j < kNumSafepointRegisters; j++) {
109
      if (entry.HasRegisterAt(j)) {
110
        os << " | " << converter.NameOfCPURegister(j);
111 112 113 114 115 116
      }
    }
  }
}


117
void SafepointTable::PrintBits(std::ostream& os,  // NOLINT
118
                               uint8_t byte, int digits) {
119
  DCHECK(digits >= 0 && digits <= kBitsPerByte);
120
  for (int i = 0; i < digits; i++) {
121
    os << (((byte & (1 << i)) == 0) ? "0" : "1");
122 123 124 125
  }
}


126 127
void Safepoint::DefinePointerRegister(Register reg, Zone* zone) {
  registers_->Add(reg.code(), zone);
128 129 130
}


131
Safepoint SafepointTableBuilder::DefineSafepoint(
132 133 134 135
    Assembler* assembler,
    Safepoint::Kind kind,
    int arguments,
    Safepoint::DeoptMode deopt_mode) {
136
  DCHECK_GE(arguments, 0);
137 138 139 140
  DeoptimizationInfo info;
  info.pc = assembler->pc_offset();
  info.arguments = arguments;
  info.has_doubles = (kind & Safepoint::kWithDoubles);
141
  info.trampoline = -1;
142 143
  deoptimization_info_.Add(info, zone_);
  deopt_index_list_.Add(Safepoint::kNoDeoptimizationIndex, zone_);
144 145 146
  if (deopt_mode == Safepoint::kNoLazyDeopt) {
    last_lazy_safepoint_ = deopt_index_list_.length();
  }
147
  indexes_.Add(new(zone_) ZoneList<int>(8, zone_), zone_);
148
  registers_.Add((kind & Safepoint::kWithRegisters)
149 150 151
                     ? new (zone_) ZoneList<int>(4, zone_)
                     : nullptr,
                 zone_);
152
  return Safepoint(indexes_.last(), registers_.last());
153 154 155
}


156 157 158 159 160 161
void SafepointTableBuilder::RecordLazyDeoptimizationIndex(int index) {
  while (last_lazy_safepoint_ < deopt_index_list_.length()) {
    deopt_index_list_[last_lazy_safepoint_++] = index;
  }
}

162
unsigned SafepointTableBuilder::GetCodeOffset() const {
163
  DCHECK(emitted_);
164 165 166
  return offset_;
}

167 168
int SafepointTableBuilder::UpdateDeoptimizationInfo(int pc, int trampoline,
                                                    int start) {
169
  int index = -1;
170
  for (int i = start; i < deoptimization_info_.length(); i++) {
171 172 173 174 175
    if (static_cast<int>(deoptimization_info_[i].pc) == pc) {
      index = i;
      break;
    }
  }
176
  CHECK_GE(index, 0);
177
  DCHECK(index < deoptimization_info_.length());
178 179
  deoptimization_info_[index].trampoline = trampoline;
  return index;
180
}
181 182

void SafepointTableBuilder::Emit(Assembler* assembler, int bits_per_entry) {
183 184
  RemoveDuplicates();

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  // 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);

202
  // Emit sorted table of pc offsets together with deoptimization indexes.
203 204
  for (int i = 0; i < length; i++) {
    assembler->dd(deoptimization_info_[i].pc);
205 206
    assembler->dd(EncodeExceptPC(deoptimization_info_[i],
                                 deopt_index_list_[i]));
207
    assembler->dd(deoptimization_info_[i].trampoline);
208 209 210
  }

  // Emit table of bitmaps.
211
  ZoneList<uint8_t> bits(bytes_per_entry, zone_);
212 213 214 215
  for (int i = 0; i < length; i++) {
    ZoneList<int>* indexes = indexes_[i];
    ZoneList<int>* registers = registers_[i];
    bits.Clear();
216
    bits.AddBlock(0, bytes_per_entry, zone_);
217 218

    // Run through the registers (if any).
219
    DCHECK(IsAligned(kNumSafepointRegisters, kBitsPerByte));
220
    if (registers == nullptr) {
221 222 223 224 225 226 227
      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);
228
        DCHECK(index >= 0 && index < kNumSafepointRegisters);
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        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;
}


252 253
uint32_t SafepointTableBuilder::EncodeExceptPC(const DeoptimizationInfo& info,
                                               unsigned index) {
254 255
  uint32_t encoding = SafepointEntry::DeoptimizationIndexField::encode(index);
  encoding |= SafepointEntry::ArgumentsField::encode(info.arguments);
256
  encoding |= SafepointEntry::SaveDoublesField::encode(info.has_doubles);
257 258 259
  return encoding;
}

260
void SafepointTableBuilder::RemoveDuplicates() {
261 262 263 264 265
  // 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.

266 267 268 269 270 271 272
  int length = deoptimization_info_.length();
  DCHECK_EQ(length, deopt_index_list_.length());
  DCHECK_EQ(length, indexes_.length());
  DCHECK_EQ(length, registers_.length());

  if (length < 2) return;

273
  // Check that all entries (1, length] are identical to entry 0.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  for (int i = 1; i < length; ++i) {
    if (!IsIdenticalExceptForPc(0, i)) return;
  }

  // If we get here, all entries were identical. Rewind all lists to just one
  // entry, and set the pc to kMaxUInt32.
  deoptimization_info_.Rewind(1);
  deopt_index_list_.Rewind(1);
  indexes_.Rewind(1);
  registers_.Rewind(1);
  deoptimization_info_[0].pc = kMaxUInt32;
}

bool SafepointTableBuilder::IsIdenticalExceptForPc(int index1,
                                                   int index2) const {
  DeoptimizationInfo& deopt_info_1 = deoptimization_info_[index1];
  DeoptimizationInfo& deopt_info_2 = deoptimization_info_[index2];
  if (deopt_info_1.arguments != deopt_info_2.arguments) return false;
  if (deopt_info_1.has_doubles != deopt_info_2.has_doubles) return false;

  if (deopt_index_list_[index1] != deopt_index_list_[index2]) return false;

  ZoneList<int>* indexes1 = indexes_[index1];
  ZoneList<int>* indexes2 = indexes_[index2];
  if (indexes1->length() != indexes2->length()) return false;
  for (int i = 0; i < indexes1->length(); ++i) {
    if (indexes1->at(i) != indexes2->at(i)) return false;
  }

  ZoneList<int>* registers1 = registers_[index1];
  ZoneList<int>* registers2 = registers_[index2];
  if (registers1) {
306
    if (!registers2) return false;
307 308 309 310 311 312 313 314 315 316
    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;
}
317

318 319
}  // namespace internal
}  // namespace v8