blob: d2ec54c38874c9782c35f0ca9663482024787318 [file] [log] [blame]
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001// Copyright 2011 the V8 project authors. All rights reserved.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002// 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"
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000029
kmillikin@chromium.org31b12772011-02-02 16:08:26 +000030#include "deoptimizer.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000031#include "disasm.h"
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000032#include "macro-assembler.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000033
34namespace v8 {
35namespace internal {
36
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000037
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
kasperl@chromium.orga5551262010-12-07 12:49:48 +000058SafepointTable::SafepointTable(Code* code) {
59 ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION);
60 code_ = code;
ricow@chromium.org83aa5492011-02-07 12:42:56 +000061 Address header = code->instruction_start() + code->safepoint_table_offset();
kasperl@chromium.orga5551262010-12-07 12:49:48 +000062 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);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000068 ASSERT_EQ(SafepointEntry::DeoptimizationIndexField::max(),
69 Safepoint::kNoDeoptimizationIndex);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000070}
71
72
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000073SafepointEntry 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);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000078 }
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000079 return SafepointEntry();
kasperl@chromium.orga5551262010-12-07 12:49:48 +000080}
81
82
83void SafepointTable::PrintEntry(unsigned index) const {
84 disasm::NameConverter converter;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000085 SafepointEntry entry = GetEntry(index);
86 uint8_t* bits = entry.bits();
kasperl@chromium.orga5551262010-12-07 12:49:48 +000087
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;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000093 for (int i = first; i < last; i++) PrintBits(bits[i], kBitsPerByte);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000094 int last_bits = code_->stack_slots() - ((last - first) * kBitsPerByte);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000095 PrintBits(bits[last], last_bits);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000096
97 // Print the registers (if any).
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000098 if (!entry.HasRegisters()) return;
kasperl@chromium.orga5551262010-12-07 12:49:48 +000099 for (int j = 0; j < kNumSafepointRegisters; j++) {
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000100 if (entry.HasRegisterAt(j)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000101 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
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000116void Safepoint::DefinePointerRegister(Register reg) {
117 registers_->Add(reg.code());
118}
119
120
ager@chromium.org378b34e2011-01-28 08:04:38 +0000121Safepoint SafepointTableBuilder::DefineSafepoint(
122 Assembler* assembler, Safepoint::Kind kind, int arguments,
123 int deoptimization_index) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000124 ASSERT(deoptimization_index != -1);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000125 ASSERT(arguments >= 0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000126 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();
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000130 pc_and_deoptimization_index.arguments = arguments;
ager@chromium.org378b34e2011-01-28 08:04:38 +0000131 pc_and_deoptimization_index.has_doubles = (kind & Safepoint::kWithDoubles);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000132 deoptimization_info_.Add(pc_and_deoptimization_index);
133 indexes_.Add(new ZoneList<int>(8));
ager@chromium.org378b34e2011-01-28 08:04:38 +0000134 registers_.Add((kind & Safepoint::kWithRegisters)
135 ? new ZoneList<int>(4)
136 : NULL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000137 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) {
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000148 // 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
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000156 // 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);
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000177 assembler->dd(EncodeExceptPC(deoptimization_info_[i]));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000178 }
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
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000222uint32_t SafepointTableBuilder::EncodeExceptPC(const DeoptimizationInfo& info) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000223 unsigned index = info.deoptimization_index;
224 unsigned gap_size = info.pc_after_gap - info.pc;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000225 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);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000229 return encoding;
230}
231
232
ricow@chromium.org83aa5492011-02-07 12:42:56 +0000233int 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
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000253} } // namespace v8::internal