blob: d2ec54c38874c9782c35f0ca9663482024787318 [file] [log] [blame]
Steve Block1e0659c2011-05-24 12:43:12 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002// 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"
Ben Murdochb8e0da22011-05-16 14:20:40 +010029
Steve Block1e0659c2011-05-24 12:43:12 +010030#include "deoptimizer.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010031#include "disasm.h"
Ben Murdochb8e0da22011-05-16 14:20:40 +010032#include "macro-assembler.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010033
34namespace v8 {
35namespace internal {
36
Ben Murdochb8e0da22011-05-16 14:20:40 +010037
38bool 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
49bool 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
Ben Murdochb0fe1622011-05-05 13:52:32 +010058SafepointTable::SafepointTable(Code* code) {
59 ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION);
60 code_ = code;
Steve Block1e0659c2011-05-24 12:43:12 +010061 Address header = code->instruction_start() + code->safepoint_table_offset();
Ben Murdochb0fe1622011-05-05 13:52:32 +010062 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);
Ben Murdochb8e0da22011-05-16 14:20:40 +010068 ASSERT_EQ(SafepointEntry::DeoptimizationIndexField::max(),
69 Safepoint::kNoDeoptimizationIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +010070}
71
72
Ben Murdochb8e0da22011-05-16 14:20:40 +010073SafepointEntry 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);
Ben Murdochb0fe1622011-05-05 13:52:32 +010078 }
Ben Murdochb8e0da22011-05-16 14:20:40 +010079 return SafepointEntry();
Ben Murdochb0fe1622011-05-05 13:52:32 +010080}
81
82
83void SafepointTable::PrintEntry(unsigned index) const {
84 disasm::NameConverter converter;
Ben Murdochb8e0da22011-05-16 14:20:40 +010085 SafepointEntry entry = GetEntry(index);
86 uint8_t* bits = entry.bits();
Ben Murdochb0fe1622011-05-05 13:52:32 +010087
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;
Ben Murdochb8e0da22011-05-16 14:20:40 +010093 for (int i = first; i < last; i++) PrintBits(bits[i], kBitsPerByte);
Ben Murdochb0fe1622011-05-05 13:52:32 +010094 int last_bits = code_->stack_slots() - ((last - first) * kBitsPerByte);
Ben Murdochb8e0da22011-05-16 14:20:40 +010095 PrintBits(bits[last], last_bits);
Ben Murdochb0fe1622011-05-05 13:52:32 +010096
97 // Print the registers (if any).
Ben Murdochb8e0da22011-05-16 14:20:40 +010098 if (!entry.HasRegisters()) return;
Ben Murdochb0fe1622011-05-05 13:52:32 +010099 for (int j = 0; j < kNumSafepointRegisters; j++) {
Ben Murdochb8e0da22011-05-16 14:20:40 +0100100 if (entry.HasRegisterAt(j)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100101 PrintF(" | %s", converter.NameOfCPURegister(j));
102 }
103 }
104 }
105}
106
107
108void 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
Ben Murdochb8e0da22011-05-16 14:20:40 +0100116void Safepoint::DefinePointerRegister(Register reg) {
117 registers_->Add(reg.code());
118}
119
120
Steve Block1e0659c2011-05-24 12:43:12 +0100121Safepoint SafepointTableBuilder::DefineSafepoint(
122 Assembler* assembler, Safepoint::Kind kind, int arguments,
123 int deoptimization_index) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100124 ASSERT(deoptimization_index != -1);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100125 ASSERT(arguments >= 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100126 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();
Ben Murdochb8e0da22011-05-16 14:20:40 +0100130 pc_and_deoptimization_index.arguments = arguments;
Steve Block1e0659c2011-05-24 12:43:12 +0100131 pc_and_deoptimization_index.has_doubles = (kind & Safepoint::kWithDoubles);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100132 deoptimization_info_.Add(pc_and_deoptimization_index);
133 indexes_.Add(new ZoneList<int>(8));
Steve Block1e0659c2011-05-24 12:43:12 +0100134 registers_.Add((kind & Safepoint::kWithRegisters)
135 ? new ZoneList<int>(4)
136 : NULL);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100137 return Safepoint(indexes_.last(), registers_.last());
138}
139
140
141unsigned SafepointTableBuilder::GetCodeOffset() const {
142 ASSERT(emitted_);
143 return offset_;
144}
145
146
147void SafepointTableBuilder::Emit(Assembler* assembler, int bits_per_entry) {
Steve Block1e0659c2011-05-24 12:43:12 +0100148 // 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
Ben Murdochb0fe1622011-05-05 13:52:32 +0100156 // 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);
Ben Murdochb8e0da22011-05-16 14:20:40 +0100177 assembler->dd(EncodeExceptPC(deoptimization_info_[i]));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100178 }
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
Ben Murdochb8e0da22011-05-16 14:20:40 +0100222uint32_t SafepointTableBuilder::EncodeExceptPC(const DeoptimizationInfo& info) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100223 unsigned index = info.deoptimization_index;
224 unsigned gap_size = info.pc_after_gap - info.pc;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100225 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);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100229 return encoding;
230}
231
232
Steve Block1e0659c2011-05-24 12:43:12 +0100233int 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
Ben Murdochb0fe1622011-05-05 13:52:32 +0100253} } // namespace v8::internal