1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "src/codegen/safepoint-table.h"
6
7 #include "src/codegen/assembler-inl.h"
8 #include "src/codegen/macro-assembler.h"
9 #include "src/deoptimizer/deoptimizer.h"
10 #include "src/diagnostics/disasm.h"
11 #include "src/execution/frames-inl.h"
12 #include "src/utils/ostreams.h"
13 #include "src/wasm/wasm-code-manager.h"
14
15 namespace v8 {
16 namespace internal {
17
SafepointTable(Code code)18 SafepointTable::SafepointTable(Code code)
19 : SafepointTable(code.InstructionStart(), code.SafepointTableAddress(),
20 code.stack_slots(), true) {}
21
SafepointTable(const wasm::WasmCode * code)22 SafepointTable::SafepointTable(const wasm::WasmCode* code)
23 : SafepointTable(code->instruction_start(),
24 code->instruction_start() + code->safepoint_table_offset(),
25 code->stack_slots(), false) {}
26
SafepointTable(Address instruction_start,Address safepoint_table_address,uint32_t stack_slots,bool has_deopt)27 SafepointTable::SafepointTable(Address instruction_start,
28 Address safepoint_table_address,
29 uint32_t stack_slots, bool has_deopt)
30 : instruction_start_(instruction_start),
31 stack_slots_(stack_slots),
32 has_deopt_(has_deopt),
33 safepoint_table_address_(safepoint_table_address),
34 length_(ReadLength(safepoint_table_address)),
35 entry_size_(ReadEntrySize(safepoint_table_address)) {}
36
find_return_pc(unsigned pc_offset)37 unsigned SafepointTable::find_return_pc(unsigned pc_offset) {
38 for (unsigned i = 0; i < length(); i++) {
39 if (GetTrampolinePcOffset(i) == static_cast<int>(pc_offset)) {
40 return GetPcOffset(i);
41 } else if (GetPcOffset(i) == pc_offset) {
42 return pc_offset;
43 }
44 }
45 UNREACHABLE();
46 }
47
FindEntry(Address pc) const48 SafepointEntry SafepointTable::FindEntry(Address pc) const {
49 unsigned pc_offset = static_cast<unsigned>(pc - instruction_start_);
50 // We use kMaxUInt32 as sentinel value, so check that we don't hit that.
51 DCHECK_NE(kMaxUInt32, pc_offset);
52 unsigned len = length();
53 CHECK_GT(len, 0);
54 // If pc == kMaxUInt32, then this entry covers all call sites in the function.
55 if (len == 1 && GetPcOffset(0) == kMaxUInt32) return GetEntry(0);
56 for (unsigned i = 0; i < len; i++) {
57 // TODO(kasperl): Replace the linear search with binary search.
58 if (GetPcOffset(i) == pc_offset ||
59 (has_deopt_ &&
60 GetTrampolinePcOffset(i) == static_cast<int>(pc_offset))) {
61 return GetEntry(i);
62 }
63 }
64 UNREACHABLE();
65 }
66
PrintEntry(unsigned index,std::ostream & os) const67 void SafepointTable::PrintEntry(unsigned index,
68 std::ostream& os) const { // NOLINT
69 disasm::NameConverter converter;
70 SafepointEntry entry = GetEntry(index);
71 uint8_t* bits = entry.bits();
72
73 // Print the stack slot bits.
74 if (entry_size_ > 0) {
75 const int first = 0;
76 int last = entry_size_ - 1;
77 for (int i = first; i < last; i++) PrintBits(os, bits[i], kBitsPerByte);
78 int last_bits = stack_slots_ - ((last - first) * kBitsPerByte);
79 PrintBits(os, bits[last], last_bits);
80 }
81 }
82
PrintBits(std::ostream & os,uint8_t byte,int digits)83 void SafepointTable::PrintBits(std::ostream& os, // NOLINT
84 uint8_t byte, int digits) {
85 DCHECK(digits >= 0 && digits <= kBitsPerByte);
86 for (int i = 0; i < digits; i++) {
87 os << (((byte & (1 << i)) == 0) ? "0" : "1");
88 }
89 }
90
DefineSafepoint(Assembler * assembler,Safepoint::DeoptMode deopt_mode)91 Safepoint SafepointTableBuilder::DefineSafepoint(
92 Assembler* assembler, Safepoint::DeoptMode deopt_mode) {
93 deoptimization_info_.push_back(
94 DeoptimizationInfo(zone_, assembler->pc_offset_for_safepoint()));
95 DeoptimizationInfo& new_info = deoptimization_info_.back();
96 return Safepoint(new_info.indexes);
97 }
98
GetCodeOffset() const99 unsigned SafepointTableBuilder::GetCodeOffset() const {
100 DCHECK(emitted_);
101 return offset_;
102 }
103
UpdateDeoptimizationInfo(int pc,int trampoline,int start,unsigned deopt_index)104 int SafepointTableBuilder::UpdateDeoptimizationInfo(int pc, int trampoline,
105 int start,
106 unsigned deopt_index) {
107 int index = start;
108 for (auto it = deoptimization_info_.Find(start);
109 it != deoptimization_info_.end(); it++, index++) {
110 if (static_cast<int>(it->pc) == pc) {
111 it->trampoline = trampoline;
112 it->deopt_index = deopt_index;
113 return index;
114 }
115 }
116 UNREACHABLE();
117 }
118
Emit(Assembler * assembler,int bits_per_entry)119 void SafepointTableBuilder::Emit(Assembler* assembler, int bits_per_entry) {
120 RemoveDuplicates();
121
122 // Make sure the safepoint table is properly aligned. Pad with nops.
123 assembler->Align(Code::kMetadataAlignment);
124 assembler->RecordComment(";;; Safepoint table.");
125 offset_ = assembler->pc_offset();
126
127 // Compute the number of bytes per safepoint entry.
128 int bytes_per_entry =
129 RoundUp(bits_per_entry, kBitsPerByte) >> kBitsPerByteLog2;
130
131 // Emit the table header.
132 STATIC_ASSERT(SafepointTable::kLengthOffset == 0 * kIntSize);
133 STATIC_ASSERT(SafepointTable::kEntrySizeOffset == 1 * kIntSize);
134 STATIC_ASSERT(SafepointTable::kHeaderSize == 2 * kIntSize);
135 int length = static_cast<int>(deoptimization_info_.size());
136 assembler->dd(length);
137 assembler->dd(bytes_per_entry);
138
139 // Emit sorted table of pc offsets together with additional info (i.e. the
140 // deoptimization index or arguments count) and trampoline offsets.
141 STATIC_ASSERT(SafepointTable::kPcOffset == 0 * kIntSize);
142 STATIC_ASSERT(SafepointTable::kEncodedInfoOffset == 1 * kIntSize);
143 STATIC_ASSERT(SafepointTable::kTrampolinePcOffset == 2 * kIntSize);
144 STATIC_ASSERT(SafepointTable::kFixedEntrySize == 3 * kIntSize);
145 for (const DeoptimizationInfo& info : deoptimization_info_) {
146 assembler->dd(info.pc);
147 assembler->dd(info.deopt_index);
148 assembler->dd(info.trampoline);
149 }
150
151 // Emit table of bitmaps.
152 ZoneVector<uint8_t> bits(bytes_per_entry, 0, zone_);
153 for (const DeoptimizationInfo& info : deoptimization_info_) {
154 ZoneChunkList<int>* indexes = info.indexes;
155 std::fill(bits.begin(), bits.end(), 0);
156
157 // Run through the indexes and build a bitmap.
158 for (int idx : *indexes) {
159 int index = bits_per_entry - 1 - idx;
160 int byte_index = index >> kBitsPerByteLog2;
161 int bit_index = index & (kBitsPerByte - 1);
162 bits[byte_index] |= (1U << bit_index);
163 }
164
165 // Emit the bitmap for the current entry.
166 for (int k = 0; k < bytes_per_entry; k++) {
167 assembler->db(bits[k]);
168 }
169 }
170 emitted_ = true;
171 }
172
RemoveDuplicates()173 void SafepointTableBuilder::RemoveDuplicates() {
174 // If the table contains more than one entry, and all entries are identical
175 // (except for the pc), replace the whole table by a single entry with pc =
176 // kMaxUInt32. This especially compacts the table for wasm code without tagged
177 // pointers and without deoptimization info.
178
179 if (deoptimization_info_.size() < 2) return;
180
181 // Check that all entries (1, size] are identical to entry 0.
182 const DeoptimizationInfo& first_info = deoptimization_info_.front();
183 for (auto it = deoptimization_info_.Find(1); it != deoptimization_info_.end();
184 it++) {
185 if (!IsIdenticalExceptForPc(first_info, *it)) return;
186 }
187
188 // If we get here, all entries were identical. Rewind the list to just one
189 // entry, and set the pc to kMaxUInt32.
190 deoptimization_info_.Rewind(1);
191 deoptimization_info_.front().pc = kMaxUInt32;
192 }
193
IsIdenticalExceptForPc(const DeoptimizationInfo & info1,const DeoptimizationInfo & info2) const194 bool SafepointTableBuilder::IsIdenticalExceptForPc(
195 const DeoptimizationInfo& info1, const DeoptimizationInfo& info2) const {
196 if (info1.deopt_index != info2.deopt_index) return false;
197
198 ZoneChunkList<int>* indexes1 = info1.indexes;
199 ZoneChunkList<int>* indexes2 = info2.indexes;
200 if (indexes1->size() != indexes2->size()) return false;
201 if (!std::equal(indexes1->begin(), indexes1->end(), indexes2->begin())) {
202 return false;
203 }
204
205 return true;
206 }
207
208 } // namespace internal
209 } // namespace v8
210