Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: src/interpreter/mkpeephole.cc

Issue 2118183002: [interpeter] Move to table based peephole optimizer. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase and back out patch set 21. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 <array>
6 #include <fstream>
7 #include <string>
8
9 #include "src/interpreter/bytecode-peephole-optimizer.h"
10 #include "src/interpreter/bytecodes.h"
11
12 namespace v8 {
13 namespace internal {
14
15 // These flag definitions are used in bytecodes.cc, but not available
16 // to the linker.
17 bool FLAG_prepare_always_opt = false;
18 bool FLAG_always_opt = false;
19
20 namespace interpreter {
21
22 const char* ActionName(PeepholeAction action) {
23 switch (action) {
24 #define CASE(Name) \
25 case PeepholeAction::k##Name: \
26 return "PeepholeAction::k" #Name;
27 PEEPHOLE_ACTION_LIST(CASE)
28 #undef CASE
29 default:
30 UNREACHABLE();
31 return "";
32 }
33 }
34
35 std::string BytecodeName(Bytecode bytecode) {
36 return "Bytecode::k" + std::string(Bytecodes::ToString(bytecode));
37 }
38
39 class PeepholeActionTableWriter final {
40 public:
41 static const size_t kNumberOfBytecodes =
42 static_cast<size_t>(Bytecode::kLast) + 1;
43 typedef std::array<PeepholeActionAndData, kNumberOfBytecodes> Row;
44
45 void BuildTable();
46 void Write(std::ostream& os);
47
48 private:
49 static const char* kIndent;
50 static const char* kNamespaceElements[];
51
52 void WriteHeader(std::ostream& os);
53 void WriteIncludeFiles(std::ostream& os);
54 void WriteClassMethods(std::ostream& os);
55 void WriteUniqueRows(std::ostream& os);
56 void WriteRowMap(std::ostream& os);
57 void WriteRow(std::ostream& os, size_t row_index);
58 void WriteOpenNamespace(std::ostream& os);
59 void WriteCloseNamespace(std::ostream& os);
60
61 PeepholeActionAndData LookupActionAndData(Bytecode last, Bytecode current);
62 void BuildRow(Bytecode last, Row* row);
63 size_t HashRow(const Row* row);
64 void InsertRow(size_t row_index, const Row* const row, size_t row_hash,
65 std::map<size_t, size_t>* hash_to_row_map);
66 bool RowsEqual(const Row* const first, const Row* const second);
67
68 std::vector<Row>* table() { return &table_; }
69
70 // Table of unique rows.
71 std::vector<Row> table_;
72
73 // Mapping of row index to unique row index.
74 std::array<size_t, kNumberOfBytecodes> row_map_;
75 };
76
77 const char* PeepholeActionTableWriter::kIndent = " ";
78 const char* PeepholeActionTableWriter::kNamespaceElements[] = {"v8", "internal",
79 "interpreter"};
80
81 // static
82 PeepholeActionAndData PeepholeActionTableWriter::LookupActionAndData(
83 Bytecode last, Bytecode current) {
84 // Optimize various accumulator loads followed by store accumulator
85 // to an equivalent register load and loading the accumulator with
86 // the register. The latter accumulator load can often be elided as
87 // it is side-effect free and often followed by another accumulator
88 // load so can be elided.
89 if (current == Bytecode::kStar) {
90 switch (last) {
91 case Bytecode::kLdaNamedProperty:
92 return {PeepholeAction::kTransformLdaStarToLdrLdarAction,
93 Bytecode::kLdrNamedProperty};
94 case Bytecode::kLdaKeyedProperty:
95 return {PeepholeAction::kTransformLdaStarToLdrLdarAction,
96 Bytecode::kLdrKeyedProperty};
97 case Bytecode::kLdaGlobal:
98 return {PeepholeAction::kTransformLdaStarToLdrLdarAction,
99 Bytecode::kLdrGlobal};
100 case Bytecode::kLdaContextSlot:
101 return {PeepholeAction::kTransformLdaStarToLdrLdarAction,
102 Bytecode::kLdrContextSlot};
103 case Bytecode::kLdaUndefined:
104 return {PeepholeAction::kTransformLdaStarToLdrLdarAction,
105 Bytecode::kLdrUndefined};
106 default:
107 break;
108 }
109 }
110
111 // ToName optimizations: remove unnecessary ToName bytecodes.
112 if (current == Bytecode::kToName) {
113 if (last == Bytecode::kLdaConstant) {
114 return {PeepholeAction::kElideCurrentIfLoadingNameConstantAction,
115 Bytecode::kIllegal};
116 } else if (Bytecodes::PutsNameInAccumulator(last)) {
117 return {PeepholeAction::kElideCurrentAction, Bytecode::kIllegal};
118 }
119 }
120
121 // Nop are placeholders for holding source position information and can be
122 // elided if there is no source information.
123 if (last == Bytecode::kNop) {
124 if (Bytecodes::IsJump(current)) {
125 return {PeepholeAction::kElideLastBeforeJumpAction, Bytecode::kIllegal};
126 } else {
127 return {PeepholeAction::kElideLastAction, Bytecode::kIllegal};
128 }
129 }
130
131 // The accumulator is invisible to the debugger. If there is a sequence
132 // of consecutive accumulator loads (that don't have side effects) then
133 // only the final load is potentially visible.
134 if (Bytecodes::IsAccumulatorLoadWithoutEffects(last) &&
135 Bytecodes::IsAccumulatorLoadWithoutEffects(current)) {
136 return {PeepholeAction::kElideLastAction, Bytecode::kIllegal};
137 }
138
139 // The current instruction clobbers the accumulator without reading
140 // it. The load in the last instruction can be elided as it has no
141 // effect.
142 if (Bytecodes::IsAccumulatorLoadWithoutEffects(last) &&
143 Bytecodes::GetAccumulatorUse(current) == AccumulatorUse::kWrite) {
144 return {PeepholeAction::kElideLastAction, Bytecode::kIllegal};
145 }
146
147 // Ldar and Star make the accumulator and register hold equivalent
148 // values. Only the first bytecode is needed if there's a sequence
149 // of back-to-back Ldar and Star bytecodes with the same operand.
150 if (Bytecodes::IsLdarOrStar(last) && Bytecodes::IsLdarOrStar(current)) {
151 return {PeepholeAction::kElideCurrentIfOperand0MatchesAction,
152 Bytecode::kIllegal};
153 }
154
155 // Remove ToBoolean coercion from conditional jumps where possible.
156 if (Bytecodes::WritesBooleanToAccumulator(last)) {
157 if (Bytecodes::IsJumpIfToBoolean(current)) {
158 return {PeepholeAction::kChangeJumpBytecodeAction,
159 Bytecodes::GetJumpWithoutToBoolean(current)};
160 } else if (current == Bytecode::kToBooleanLogicalNot) {
161 return {PeepholeAction::kChangeBytecodeAction, Bytecode::kLogicalNot};
162 }
163 }
164
165 // Fuse LdaSmi followed by binary op to produce binary op with a
166 // zero immediate argument. This saves dispatches, but not size.
167 if (last == Bytecode::kLdaSmi) {
168 switch (current) {
169 case Bytecode::kAdd:
170 return {PeepholeAction::kTransformLdaSmiBinaryOpToBinaryOpWithSmiAction,
171 Bytecode::kAddSmi};
172 case Bytecode::kSub:
173 return {PeepholeAction::kTransformLdaSmiBinaryOpToBinaryOpWithSmiAction,
174 Bytecode::kSubSmi};
175 case Bytecode::kBitwiseAnd:
176 return {PeepholeAction::kTransformLdaSmiBinaryOpToBinaryOpWithSmiAction,
177 Bytecode::kBitwiseAndSmi};
178 case Bytecode::kBitwiseOr:
179 return {PeepholeAction::kTransformLdaSmiBinaryOpToBinaryOpWithSmiAction,
180 Bytecode::kBitwiseOrSmi};
181 case Bytecode::kShiftLeft:
182 return {PeepholeAction::kTransformLdaSmiBinaryOpToBinaryOpWithSmiAction,
183 Bytecode::kShiftLeftSmi};
184 case Bytecode::kShiftRight:
185 return {PeepholeAction::kTransformLdaSmiBinaryOpToBinaryOpWithSmiAction,
186 Bytecode::kShiftRightSmi};
187 default:
188 break;
189 }
190 }
191
192 // Fuse LdaZero followed by binary op to produce binary op with a
193 // zero immediate argument. This saves dispatches, but not size.
194 if (last == Bytecode::kLdaZero) {
195 switch (current) {
196 case Bytecode::kAdd:
197 return {
198 PeepholeAction::kTransformLdaZeroBinaryOpToBinaryOpWithZeroAction,
199 Bytecode::kAddSmi};
200 case Bytecode::kSub:
201 return {
202 PeepholeAction::kTransformLdaZeroBinaryOpToBinaryOpWithZeroAction,
203 Bytecode::kSubSmi};
204 case Bytecode::kBitwiseAnd:
205 return {
206 PeepholeAction::kTransformLdaZeroBinaryOpToBinaryOpWithZeroAction,
207 Bytecode::kBitwiseAndSmi};
208 case Bytecode::kBitwiseOr:
209 return {
210 PeepholeAction::kTransformLdaZeroBinaryOpToBinaryOpWithZeroAction,
211 Bytecode::kBitwiseOrSmi};
212 case Bytecode::kShiftLeft:
213 return {
214 PeepholeAction::kTransformLdaZeroBinaryOpToBinaryOpWithZeroAction,
215 Bytecode::kShiftLeftSmi};
216 case Bytecode::kShiftRight:
217 return {
218 PeepholeAction::kTransformLdaZeroBinaryOpToBinaryOpWithZeroAction,
219 Bytecode::kShiftRightSmi};
220 default:
221 break;
222 }
223 }
224
225 // If there is no last bytecode to optimize against, store the incoming
226 // bytecode or for jumps emit incoming bytecode immediately.
227 if (last == Bytecode::kIllegal) {
228 if (Bytecodes::IsJump(current)) {
229 return {PeepholeAction::kUpdateLastJumpAction, Bytecode::kIllegal};
230 } else {
231 return {PeepholeAction::kUpdateLastAction, Bytecode::kIllegal};
232 }
233 }
234
235 // No matches, take the default action.
236 if (Bytecodes::IsJump(current)) {
237 return {PeepholeAction::kDefaultJumpAction, Bytecode::kIllegal};
238 } else {
239 return {PeepholeAction::kDefaultAction, Bytecode::kIllegal};
240 }
241 }
242
243 void PeepholeActionTableWriter::Write(std::ostream& os) {
244 WriteHeader(os);
245 WriteIncludeFiles(os);
246 WriteOpenNamespace(os);
247 WriteUniqueRows(os);
248 WriteRowMap(os);
249 WriteClassMethods(os);
250 WriteCloseNamespace(os);
251 }
252
253 void PeepholeActionTableWriter::WriteHeader(std::ostream& os) {
254 os << "// Copyright 2016 the V8 project authors. All rights reserved.\n"
255 << "// Use of this source code is governed by a BSD-style license that\n"
256 << "// can be found in the LICENSE file.\n\n"
257 << "// Autogenerated by " __FILE__ ". Do not edit.\n\n";
258 }
259
260 void PeepholeActionTableWriter::WriteIncludeFiles(std::ostream& os) {
261 os << "#include \"src/interpreter/bytecode-peephole-table.h\"\n\n";
262 }
263
264 void PeepholeActionTableWriter::WriteUniqueRows(std::ostream& os) {
265 os << "const PeepholeActionAndData PeepholeActionTable::row_data_["
266 << table_.size() << "][" << kNumberOfBytecodes << "] = {\n";
267 for (size_t i = 0; i < table_.size(); ++i) {
268 os << "{\n";
269 WriteRow(os, i);
270 os << "},\n";
271 }
272 os << "};\n\n";
273 }
274
275 void PeepholeActionTableWriter::WriteRowMap(std::ostream& os) {
276 os << "const PeepholeActionAndData* const PeepholeActionTable::row_["
277 << kNumberOfBytecodes << "] = {\n";
278 for (size_t i = 0; i < kNumberOfBytecodes; ++i) {
279 os << kIndent << " PeepholeActionTable::row_data_[" << row_map_[i]
280 << "], \n";
281 }
282 os << "};\n\n";
283 }
284
285 void PeepholeActionTableWriter::WriteRow(std::ostream& os, size_t row_index) {
286 const Row row = table_.at(row_index);
287 for (PeepholeActionAndData action_data : row) {
288 os << kIndent << "{" << ActionName(action_data.action) << ","
289 << BytecodeName(action_data.bytecode) << "},\n";
290 }
291 }
292
293 void PeepholeActionTableWriter::WriteOpenNamespace(std::ostream& os) {
294 for (auto element : kNamespaceElements) {
295 os << "namespace " << element << " {\n";
296 }
297 os << "\n";
298 }
299
300 void PeepholeActionTableWriter::WriteCloseNamespace(std::ostream& os) {
301 for (auto element : kNamespaceElements) {
302 os << "} // namespace " << element << "\n";
303 }
304 }
305
306 void PeepholeActionTableWriter::WriteClassMethods(std::ostream& os) {
307 os << "// static\n"
308 << "const PeepholeActionAndData*\n"
309 << "PeepholeActionTable::Lookup(Bytecode last, Bytecode current) {\n"
310 << kIndent
311 << "return &row_[Bytecodes::ToByte(last)][Bytecodes::ToByte(current)];\n"
312 << "}\n\n";
313 }
314
315 void PeepholeActionTableWriter::BuildTable() {
316 std::map<size_t, size_t> hash_to_row_map;
317 Row row;
318 for (size_t i = 0; i < kNumberOfBytecodes; ++i) {
319 uint8_t byte_value = static_cast<uint8_t>(i);
320 Bytecode last = Bytecodes::FromByte(byte_value);
321 BuildRow(last, &row);
322 size_t row_hash = HashRow(&row);
323 InsertRow(i, &row, row_hash, &hash_to_row_map);
324 }
325 }
326
327 void PeepholeActionTableWriter::BuildRow(Bytecode last, Row* row) {
328 for (size_t i = 0; i < kNumberOfBytecodes; ++i) {
329 uint8_t byte_value = static_cast<uint8_t>(i);
330 Bytecode current = Bytecodes::FromByte(byte_value);
331 PeepholeActionAndData action_data = LookupActionAndData(last, current);
332 row->at(i) = action_data;
333 }
334 }
335
336 // static
337 bool PeepholeActionTableWriter::RowsEqual(const Row* const first,
338 const Row* const second) {
339 return memcmp(first, second, sizeof(*first)) == 0;
340 }
341
342 // static
343 void PeepholeActionTableWriter::InsertRow(
344 size_t row_index, const Row* const row, size_t row_hash,
345 std::map<size_t, size_t>* hash_to_row_map) {
346 // Insert row if no existing row matches, otherwise use existing row.
347 auto iter = hash_to_row_map->find(row_hash);
348 if (iter == hash_to_row_map->end()) {
349 row_map_[row_index] = table()->size();
350 hash_to_row_map->insert({row_hash, table()->size()});
351 table()->push_back(*row);
352 } else {
353 row_map_[row_index] = iter->second;
354 DCHECK(RowsEqual(&table()->at(iter->second), row));
355 }
356 }
357
358 // static
359 size_t PeepholeActionTableWriter::HashRow(const Row* row) {
360 static const size_t kHashShift = 3;
361 std::size_t result = (1u << 31) - 1u;
362 const uint8_t* raw_data = reinterpret_cast<const uint8_t*>(row);
363 for (size_t i = 0; i < sizeof(*row); ++i) {
364 size_t top_bits = result >> (kBitsPerByte * sizeof(size_t) - kHashShift);
365 result = (result << kHashShift) ^ top_bits ^ raw_data[i];
366 }
367 return result;
368 }
369
370 } // namespace interpreter
371 } // namespace internal
372 } // namespace v8
373
374 int main(int argc, const char* argv[]) {
375 CHECK_EQ(argc, 2);
376
377 std::ofstream ofs(argv[1], std::ofstream::trunc);
378 v8::internal::interpreter::PeepholeActionTableWriter writer;
379 writer.BuildTable();
380 writer.Write(ofs);
381 ofs.flush();
382 ofs.close();
383
384 return 0;
385 }
OLDNEW
« BUILD.gn ('K') | « src/interpreter/bytecodes.cc ('k') | src/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698