OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/interpreter/bytecode-peephole-optimizer.h" | 5 #include "src/interpreter/bytecode-peephole-optimizer.h" |
6 | 6 |
7 #include "src/interpreter/constant-array-builder.h" | 7 #include "src/interpreter/constant-array-builder.h" |
8 #include "src/objects-inl.h" | 8 #include "src/objects-inl.h" |
9 #include "src/objects.h" | 9 #include "src/objects.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
(...skipping 10 matching lines...) Expand all Loading... |
22 // override | 22 // override |
23 Handle<BytecodeArray> BytecodePeepholeOptimizer::ToBytecodeArray( | 23 Handle<BytecodeArray> BytecodePeepholeOptimizer::ToBytecodeArray( |
24 int fixed_register_count, int parameter_count, | 24 int fixed_register_count, int parameter_count, |
25 Handle<FixedArray> handler_table) { | 25 Handle<FixedArray> handler_table) { |
26 Flush(); | 26 Flush(); |
27 return next_stage_->ToBytecodeArray(fixed_register_count, parameter_count, | 27 return next_stage_->ToBytecodeArray(fixed_register_count, parameter_count, |
28 handler_table); | 28 handler_table); |
29 } | 29 } |
30 | 30 |
31 // override | 31 // override |
32 void BytecodePeepholeOptimizer::Write(BytecodeNode* node) { | |
33 node = OptimizeAndEmitLast(node); | |
34 if (node != nullptr) { | |
35 SetLast(node); | |
36 } | |
37 } | |
38 | |
39 // override | |
40 void BytecodePeepholeOptimizer::WriteJump(BytecodeNode* node, | |
41 BytecodeLabel* label) { | |
42 node = OptimizeAndEmitLast(node); | |
43 next_stage_->WriteJump(node, label); | |
44 } | |
45 | |
46 // override | |
47 void BytecodePeepholeOptimizer::BindLabel(BytecodeLabel* label) { | 32 void BytecodePeepholeOptimizer::BindLabel(BytecodeLabel* label) { |
48 Flush(); | 33 Flush(); |
49 next_stage_->BindLabel(label); | 34 next_stage_->BindLabel(label); |
50 } | 35 } |
51 | 36 |
52 // override | 37 // override |
53 void BytecodePeepholeOptimizer::BindLabel(const BytecodeLabel& target, | 38 void BytecodePeepholeOptimizer::BindLabel(const BytecodeLabel& target, |
54 BytecodeLabel* label) { | 39 BytecodeLabel* label) { |
55 // There is no need to flush here, it will have been flushed when |target| | 40 // There is no need to flush here, it will have been flushed when |
56 // was bound. | 41 // |target| was bound. |
57 next_stage_->BindLabel(target, label); | 42 next_stage_->BindLabel(target, label); |
58 } | 43 } |
59 | 44 |
| 45 // override |
| 46 void BytecodePeepholeOptimizer::WriteJump(BytecodeNode* node, |
| 47 BytecodeLabel* label) { |
| 48 // Handlers for jump bytecodes do not emit |node| as WriteJump() |
| 49 // requires the |label| and having a label argument in all action |
| 50 // handlers results in dead work in the non-jump case. |
| 51 ApplyPeepholeAction(node); |
| 52 next_stage()->WriteJump(node, label); |
| 53 } |
| 54 |
| 55 // override |
| 56 void BytecodePeepholeOptimizer::Write(BytecodeNode* node) { |
| 57 // Handlers for non-jump bytecodes run to completion emitting |
| 58 // bytecode to next stage as appropriate. |
| 59 ApplyPeepholeAction(node); |
| 60 } |
| 61 |
60 void BytecodePeepholeOptimizer::Flush() { | 62 void BytecodePeepholeOptimizer::Flush() { |
61 // TODO(oth/rmcilroy): We could check CanElideLast() here to potentially | |
62 // eliminate last rather than writing it. | |
63 if (LastIsValid()) { | 63 if (LastIsValid()) { |
64 next_stage_->Write(&last_); | 64 next_stage_->Write(&last_); |
65 InvalidateLast(); | 65 InvalidateLast(); |
66 } | 66 } |
67 } | 67 } |
68 | 68 |
69 void BytecodePeepholeOptimizer::InvalidateLast() { | 69 void BytecodePeepholeOptimizer::InvalidateLast() { |
70 last_.set_bytecode(Bytecode::kIllegal); | 70 last_.set_bytecode(Bytecode::kIllegal); |
71 } | 71 } |
72 | 72 |
73 bool BytecodePeepholeOptimizer::LastIsValid() const { | 73 bool BytecodePeepholeOptimizer::LastIsValid() const { |
74 return last_.bytecode() != Bytecode::kIllegal; | 74 return last_.bytecode() != Bytecode::kIllegal; |
75 } | 75 } |
76 | 76 |
77 void BytecodePeepholeOptimizer::SetLast(const BytecodeNode* const node) { | 77 void BytecodePeepholeOptimizer::SetLast(const BytecodeNode* const node) { |
| 78 // An action shouldn't leave a NOP as last bytecode unless it has |
| 79 // source position information. NOP without source information can |
| 80 // always be elided. |
| 81 DCHECK(node->bytecode() != Bytecode::kNop || node->source_info().is_valid()); |
| 82 |
78 last_.Clone(node); | 83 last_.Clone(node); |
79 } | 84 } |
80 | 85 |
81 Handle<Object> BytecodePeepholeOptimizer::GetConstantForIndexOperand( | 86 Handle<Object> BytecodePeepholeOptimizer::GetConstantForIndexOperand( |
82 const BytecodeNode* const node, int index) const { | 87 const BytecodeNode* const node, int index) const { |
83 DCHECK_LE(index, node->operand_count()); | 88 DCHECK_LE(index, node->operand_count()); |
84 DCHECK_EQ(Bytecodes::GetOperandType(node->bytecode(), 0), OperandType::kIdx); | 89 DCHECK_EQ(Bytecodes::GetOperandType(node->bytecode(), 0), OperandType::kIdx); |
85 uint32_t index_operand = node->operand(0); | 90 uint32_t index_operand = node->operand(0); |
86 return constant_array_builder_->At(index_operand); | 91 return constant_array_builder_->At(index_operand); |
87 } | 92 } |
88 | 93 |
89 bool BytecodePeepholeOptimizer::LastBytecodePutsNameInAccumulator() const { | |
90 DCHECK(LastIsValid()); | |
91 return (last_.bytecode() == Bytecode::kTypeOf || | |
92 last_.bytecode() == Bytecode::kToName || | |
93 (last_.bytecode() == Bytecode::kLdaConstant && | |
94 GetConstantForIndexOperand(&last_, 0)->IsName())); | |
95 } | |
96 | |
97 void BytecodePeepholeOptimizer::TryToRemoveLastExpressionPosition( | |
98 const BytecodeNode* const current) { | |
99 if (current->source_info().is_valid() && | |
100 last_.source_info().is_expression() && | |
101 Bytecodes::IsWithoutExternalSideEffects(last_.bytecode())) { | |
102 // The last bytecode has been marked as expression. It has no | |
103 // external effects so can't throw and the current bytecode is a | |
104 // source position. Remove the expression position on the last | |
105 // bytecode to open up potential peephole optimizations and to | |
106 // save the memory and perf cost of storing the unneeded | |
107 // expression position. | |
108 last_.source_info().set_invalid(); | |
109 } | |
110 } | |
111 | |
112 bool BytecodePeepholeOptimizer::CanElideCurrent( | |
113 const BytecodeNode* const current) const { | |
114 if (Bytecodes::IsLdarOrStar(last_.bytecode()) && | |
115 Bytecodes::IsLdarOrStar(current->bytecode()) && | |
116 current->operand(0) == last_.operand(0)) { | |
117 // Ldar and Star make the accumulator and register hold equivalent | |
118 // values. Only the first bytecode is needed if there's a sequence | |
119 // of back-to-back Ldar and Star bytecodes with the same operand. | |
120 return true; | |
121 } else if (current->bytecode() == Bytecode::kToName && | |
122 LastBytecodePutsNameInAccumulator()) { | |
123 // If the previous bytecode ensured a name was in the accumulator, | |
124 // the type coercion ToName() can be elided. | |
125 return true; | |
126 } else { | |
127 // Additional candidates for eliding current: | |
128 // (i) current is Nop. | |
129 // (ii) ToNumber if the last puts a number in the accumulator. | |
130 return false; | |
131 } | |
132 } | |
133 | |
134 bool BytecodePeepholeOptimizer::CanElideLastBasedOnSourcePosition( | 94 bool BytecodePeepholeOptimizer::CanElideLastBasedOnSourcePosition( |
135 const BytecodeNode* const current) const { | 95 const BytecodeNode* const current) const { |
136 // | 96 // |
137 // The rules for allowing the elision of the last bytecode based | 97 // The rules for allowing the elision of the last bytecode based |
138 // on source position are: | 98 // on source position are: |
139 // | 99 // |
140 // C U R R E N T | 100 // C U R R E N T |
141 // +--------+--------+--------+ | 101 // +--------+--------+--------+ |
142 // | None | Expr | Stmt | | 102 // | None | Expr | Stmt | |
143 // L +--------+--------+--------+--------+ | 103 // L +--------+--------+--------+--------+ |
144 // | None | YES | YES | YES | | 104 // | None | YES | YES | YES | |
145 // A +--------+--------+--------+--------+ | 105 // A +--------+--------+--------+--------+ |
146 // | Expr | YES | MAYBE | MAYBE | | 106 // | Expr | YES | MAYBE | MAYBE | |
147 // S +--------+--------+--------+--------+ | 107 // S +--------+--------+--------+--------+ |
148 // | Stmt | YES | NO | NO | | 108 // | Stmt | YES | NO | NO | |
149 // T +--------+--------+--------+--------+ | 109 // T +--------+--------+--------+--------+ |
150 // | 110 // |
151 // The goal is not lose any statement positions and not lose useful | 111 // The goal is not lose any statement positions and not lose useful |
152 // expression positions. Whenever the last bytecode is elided it's | 112 // expression positions. Whenever the last bytecode is elided it's |
153 // source position information is applied to the current node | 113 // source position information is applied to the current node |
154 // updating it if necessary. | 114 // updating it if necessary. |
155 // | 115 // |
156 // The last bytecode can be elided for the MAYBE cases if the last | 116 // The last bytecode could be elided for the MAYBE cases if the last |
157 // bytecode is known not to throw. If it throws, the system would | 117 // bytecode is known not to throw. If it throws, the system would |
158 // not have correct stack trace information. The appropriate check | 118 // not have correct stack trace information. The appropriate check |
159 // for this would be Bytecodes::IsWithoutExternalSideEffects(), | 119 // for this would be Bytecodes::IsWithoutExternalSideEffects(). By |
160 // which is checked in | 120 // default, the upstream bytecode generator filters out unneeded |
161 // BytecodePeepholeOptimizer::TransformLastAndCurrentBytecodes() to | 121 // expression position information so there is neglible benefit to |
162 // keep the check here simple. | 122 // handling MAYBE specially. Hence MAYBE is treated the same as NO. |
163 // | |
164 // In rare cases, bytecode generation produces consecutive bytecodes | |
165 // with the same expression positions. In principle, the latter of | |
166 // these can be elided, but would make this function more expensive. | |
167 // | 123 // |
168 return (!last_.source_info().is_valid() || | 124 return (!last_.source_info().is_valid() || |
169 !current->source_info().is_valid()); | 125 !current->source_info().is_valid()); |
170 } | 126 } |
171 | 127 |
172 namespace { | 128 namespace { |
173 | 129 |
174 void TransformLdaStarToLdrLdar(Bytecode new_bytecode, BytecodeNode* const last, | 130 void TransformLdaStarToLdrLdar(Bytecode new_bytecode, BytecodeNode* const last, |
175 BytecodeNode* const current) { | 131 BytecodeNode* const current) { |
176 DCHECK_EQ(current->bytecode(), Bytecode::kStar); | 132 DCHECK_EQ(current->bytecode(), Bytecode::kStar); |
177 | 133 |
178 // | 134 // |
179 // An example transformation here would be: | 135 // An example transformation here would be: |
180 // | 136 // |
181 // LdaGlobal i0, i1 ____\ LdrGlobal i0, i1, R | 137 // LdaGlobal i0, i1 ____\ LdrGlobal i0, i1, R |
182 // Star R ====/ Ldar R | 138 // Star R ====/ Ldar R |
183 // | 139 // |
184 // which loads a global value into both a register and the | 140 // which loads a global value into both a register and the |
185 // accumulator. However, in the second form the Ldar can often be | 141 // accumulator. However, in the second form the Ldar can often be |
186 // peephole optimized away unlike the Star in the first form. | 142 // peephole optimized away unlike the Star in the first form. |
187 // | 143 // |
188 last->Transform(new_bytecode, current->operand(0)); | 144 last->Transform(new_bytecode, current->operand(0)); |
189 current->set_bytecode(Bytecode::kLdar, current->operand(0)); | 145 current->set_bytecode(Bytecode::kLdar, current->operand(0)); |
190 } | 146 } |
191 | 147 |
192 void TransformToBinaryOpWithSmiOnRhs(Bytecode new_bytecode, | 148 void TransformLdaSmiBinaryOpToBinaryOpWithSmi(Bytecode new_bytecode, |
193 BytecodeNode* const last, | 149 BytecodeNode* const last, |
194 BytecodeNode* const current) { | 150 BytecodeNode* const current) { |
195 DCHECK(Bytecodes::IsLdaSmiOrLdaZero(last->bytecode())); | 151 DCHECK_EQ(last->bytecode(), Bytecode::kLdaSmi); |
196 uint32_t imm_operand = | 152 current->set_bytecode(new_bytecode, last->operand(0), current->operand(0)); |
197 last->bytecode() == Bytecode::kLdaSmi ? last->operand(0) : 0; | |
198 current->set_bytecode(new_bytecode, imm_operand, current->operand(0)); | |
199 if (last->source_info().is_valid()) { | 153 if (last->source_info().is_valid()) { |
200 current->source_info().Clone(last->source_info()); | 154 current->source_info().Clone(last->source_info()); |
201 } | 155 } |
| 156 } |
| 157 |
| 158 void TransformLdaZeroBinaryOpToBinaryOpWithZero(Bytecode new_bytecode, |
| 159 BytecodeNode* const last, |
| 160 BytecodeNode* const current) { |
| 161 DCHECK_EQ(last->bytecode(), Bytecode::kLdaZero); |
| 162 current->set_bytecode(new_bytecode, 0, current->operand(0)); |
| 163 if (last->source_info().is_valid()) { |
| 164 current->source_info().Clone(last->source_info()); |
| 165 } |
202 } | 166 } |
203 | 167 |
204 } // namespace | 168 } // namespace |
205 | 169 |
206 bool BytecodePeepholeOptimizer::TransformLastAndCurrentBytecodes( | 170 void BytecodePeepholeOptimizer::DefaultAction( |
207 BytecodeNode* const current) { | 171 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
208 if (current->bytecode() == Bytecode::kStar && | 172 DCHECK(LastIsValid()); |
209 !current->source_info().is_statement()) { | 173 DCHECK(!Bytecodes::IsJump(node->bytecode())); |
210 // Note: If the Star is tagged with a statement position, we can't | |
211 // perform this transform as the store to the register will | |
212 // have the wrong ordering for stepping in the debugger. | |
213 switch (last_.bytecode()) { | |
214 case Bytecode::kLdaNamedProperty: | |
215 TransformLdaStarToLdrLdar(Bytecode::kLdrNamedProperty, &last_, current); | |
216 return true; | |
217 case Bytecode::kLdaKeyedProperty: | |
218 TransformLdaStarToLdrLdar(Bytecode::kLdrKeyedProperty, &last_, current); | |
219 return true; | |
220 case Bytecode::kLdaGlobal: | |
221 TransformLdaStarToLdrLdar(Bytecode::kLdrGlobal, &last_, current); | |
222 return true; | |
223 case Bytecode::kLdaContextSlot: | |
224 TransformLdaStarToLdrLdar(Bytecode::kLdrContextSlot, &last_, current); | |
225 return true; | |
226 case Bytecode::kLdaUndefined: | |
227 TransformLdaStarToLdrLdar(Bytecode::kLdrUndefined, &last_, current); | |
228 return true; | |
229 default: | |
230 break; | |
231 } | |
232 } else if (Bytecodes::IsLdaSmiOrLdaZero(last_.bytecode()) && | |
233 (!last_.source_info().is_valid() || | |
234 !current->source_info().is_valid())) { | |
235 switch (current->bytecode()) { | |
236 case Bytecode::kAdd: | |
237 TransformToBinaryOpWithSmiOnRhs(Bytecode::kAddSmi, &last_, current); | |
238 InvalidateLast(); | |
239 return true; | |
240 case Bytecode::kSub: | |
241 TransformToBinaryOpWithSmiOnRhs(Bytecode::kSubSmi, &last_, current); | |
242 InvalidateLast(); | |
243 return true; | |
244 case Bytecode::kBitwiseOr: | |
245 TransformToBinaryOpWithSmiOnRhs(Bytecode::kBitwiseOrSmi, &last_, | |
246 current); | |
247 InvalidateLast(); | |
248 return true; | |
249 case Bytecode::kBitwiseAnd: | |
250 TransformToBinaryOpWithSmiOnRhs(Bytecode::kBitwiseAndSmi, &last_, | |
251 current); | |
252 InvalidateLast(); | |
253 return true; | |
254 case Bytecode::kShiftLeft: | |
255 TransformToBinaryOpWithSmiOnRhs(Bytecode::kShiftLeftSmi, &last_, | |
256 current); | |
257 InvalidateLast(); | |
258 return true; | |
259 case Bytecode::kShiftRight: | |
260 TransformToBinaryOpWithSmiOnRhs(Bytecode::kShiftRightSmi, &last_, | |
261 current); | |
262 InvalidateLast(); | |
263 return true; | |
264 default: | |
265 break; | |
266 } | |
267 } | |
268 | 174 |
269 return false; | 175 next_stage()->Write(last()); |
| 176 SetLast(node); |
270 } | 177 } |
271 | 178 |
272 bool BytecodePeepholeOptimizer::RemoveToBooleanFromJump( | 179 void BytecodePeepholeOptimizer::UpdateLastAction( |
273 BytecodeNode* const current) { | 180 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
274 bool can_remove = Bytecodes::IsJumpIfToBoolean(current->bytecode()) && | 181 DCHECK(!LastIsValid()); |
275 Bytecodes::WritesBooleanToAccumulator(last_.bytecode()); | 182 DCHECK(!Bytecodes::IsJump(node->bytecode())); |
276 if (can_remove) { | 183 |
277 // Conditional jumps with boolean conditions are emiitted in | 184 SetLast(node); |
278 // ToBoolean form by the bytecode array builder, | |
279 // i.e. JumpIfToBooleanTrue rather JumpIfTrue. The ToBoolean | |
280 // element can be removed if the previous bytecode put a boolean | |
281 // value in the accumulator. | |
282 Bytecode jump = Bytecodes::GetJumpWithoutToBoolean(current->bytecode()); | |
283 current->set_bytecode(jump, current->operand(0)); | |
284 } | |
285 return can_remove; | |
286 } | 185 } |
287 | 186 |
288 bool BytecodePeepholeOptimizer::RemoveToBooleanFromLogicalNot( | 187 void BytecodePeepholeOptimizer::UpdateLastIfSourceInfoPresentAction( |
289 BytecodeNode* const current) { | 188 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
290 bool can_remove = current->bytecode() == Bytecode::kToBooleanLogicalNot && | 189 DCHECK(!LastIsValid()); |
291 Bytecodes::WritesBooleanToAccumulator(last_.bytecode()); | 190 DCHECK(!Bytecodes::IsJump(node->bytecode())); |
292 if (can_remove) { | |
293 // Logical-nots are emitted in ToBoolean form by the bytecode array | |
294 // builder, The ToBoolean element can be removed if the previous bytecode | |
295 // put a boolean value in the accumulator. | |
296 current->set_bytecode(Bytecode::kLogicalNot); | |
297 } | |
298 return can_remove; | |
299 } | |
300 | 191 |
301 bool BytecodePeepholeOptimizer::TransformCurrentBytecode( | 192 if (node->source_info().is_valid()) { |
302 BytecodeNode* const current) { | 193 SetLast(node); |
303 return RemoveToBooleanFromJump(current) || | |
304 RemoveToBooleanFromLogicalNot(current); | |
305 } | |
306 | |
307 bool BytecodePeepholeOptimizer::CanElideLast( | |
308 const BytecodeNode* const current) const { | |
309 if (last_.bytecode() == Bytecode::kNop) { | |
310 // Nop are placeholders for holding source position information. | |
311 return true; | |
312 } else if (Bytecodes::IsAccumulatorLoadWithoutEffects(current->bytecode()) && | |
313 Bytecodes::IsAccumulatorLoadWithoutEffects(last_.bytecode())) { | |
314 // The accumulator is invisible to the debugger. If there is a sequence of | |
315 // consecutive accumulator loads (that don't have side effects) then only | |
316 // the final load is potentially visible. | |
317 return true; | |
318 } else if (Bytecodes::GetAccumulatorUse(current->bytecode()) == | |
319 AccumulatorUse::kWrite && | |
320 Bytecodes::IsAccumulatorLoadWithoutEffects(last_.bytecode())) { | |
321 // The current instruction clobbers the accumulator without reading it. The | |
322 // load in the last instruction can be elided as it has no effect. | |
323 return true; | |
324 } else { | |
325 return false; | |
326 } | 194 } |
327 } | 195 } |
328 | 196 |
329 BytecodeNode* BytecodePeepholeOptimizer::Optimize(BytecodeNode* current) { | 197 void BytecodePeepholeOptimizer::ElideCurrentAction( |
330 TryToRemoveLastExpressionPosition(current); | 198 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
331 if (TransformCurrentBytecode(current) || | 199 DCHECK(LastIsValid()); |
332 TransformLastAndCurrentBytecodes(current)) { | 200 DCHECK(!Bytecodes::IsJump(node->bytecode())); |
333 return current; | 201 |
| 202 if (node->source_info().is_valid()) { |
| 203 // Preserve the source information by replacing the node bytecode |
| 204 // with a no op bytecode. |
| 205 node->set_bytecode(Bytecode::kNop); |
| 206 DefaultAction(node); |
| 207 } else { |
| 208 // Nothing to do, keep last and wait for next bytecode to pair with it. |
334 } | 209 } |
335 | |
336 if (CanElideCurrent(current)) { | |
337 if (current->source_info().is_valid()) { | |
338 // Preserve the source information by replacing the current bytecode | |
339 // with a no op bytecode. | |
340 current->set_bytecode(Bytecode::kNop); | |
341 } else { | |
342 current = nullptr; | |
343 } | |
344 return current; | |
345 } | |
346 | |
347 if (CanElideLast(current) && CanElideLastBasedOnSourcePosition(current)) { | |
348 if (last_.source_info().is_valid()) { | |
349 // Current can not be valid per CanElideLastBasedOnSourcePosition(). | |
350 current->source_info().Clone(last_.source_info()); | |
351 } | |
352 InvalidateLast(); | |
353 return current; | |
354 } | |
355 | |
356 return current; | |
357 } | 210 } |
358 | 211 |
359 BytecodeNode* BytecodePeepholeOptimizer::OptimizeAndEmitLast( | 212 void BytecodePeepholeOptimizer::ElideCurrentIfOperand0MatchesAction( |
360 BytecodeNode* current) { | 213 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
361 // Attempt optimization if there is an earlier node to optimize with. | 214 DCHECK(LastIsValid()); |
362 if (LastIsValid()) { | 215 DCHECK(!Bytecodes::IsJump(node->bytecode())); |
363 current = Optimize(current); | 216 |
364 // Only output the last node if it wasn't invalidated by the optimization. | 217 if (last()->operand(0) == node->operand(0)) { |
365 if (LastIsValid()) { | 218 ElideCurrentAction(node); |
366 next_stage_->Write(&last_); | 219 } else { |
367 InvalidateLast(); | 220 DefaultAction(node); |
| 221 } |
| 222 } |
| 223 |
| 224 void BytecodePeepholeOptimizer::ElideCurrentIfLoadingNameConstantAction( |
| 225 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
| 226 DCHECK_EQ(last()->bytecode(), Bytecode::kLdaConstant); |
| 227 DCHECK(!Bytecodes::IsJump(node->bytecode())); |
| 228 |
| 229 if (GetConstantForIndexOperand(last(), 0)->IsName()) { |
| 230 ElideCurrentAction(node); |
| 231 } else { |
| 232 DefaultAction(node); |
| 233 } |
| 234 } |
| 235 |
| 236 void BytecodePeepholeOptimizer::ElideLastAction( |
| 237 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
| 238 DCHECK(LastIsValid()); |
| 239 DCHECK(!Bytecodes::IsJump(node->bytecode())); |
| 240 |
| 241 if (CanElideLastBasedOnSourcePosition(node)) { |
| 242 if (last()->source_info().is_valid()) { |
| 243 // |node| can not have a valid source position if the source |
| 244 // position of last() is valid (per rules in |
| 245 // CanElideLastBasedOnSourcePosition()). |
| 246 node->source_info().Clone(last()->source_info()); |
368 } | 247 } |
| 248 SetLast(node); |
| 249 } else { |
| 250 DefaultAction(node); |
369 } | 251 } |
370 return current; | 252 } |
| 253 |
| 254 void BytecodePeepholeOptimizer::ChangeBytecodeAction( |
| 255 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
| 256 DCHECK(LastIsValid()); |
| 257 DCHECK(!Bytecodes::IsJump(node->bytecode())); |
| 258 |
| 259 node->set_bytecode(action_data->bytecode); |
| 260 DefaultAction(node); |
| 261 } |
| 262 |
| 263 void BytecodePeepholeOptimizer::TransformLdaStarToLdrLdarAction( |
| 264 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
| 265 DCHECK(LastIsValid()); |
| 266 DCHECK(!Bytecodes::IsJump(node->bytecode())); |
| 267 |
| 268 if (!node->source_info().is_statement()) { |
| 269 TransformLdaStarToLdrLdar(action_data->bytecode, last(), node); |
| 270 } |
| 271 DefaultAction(node); |
| 272 } |
| 273 |
| 274 void BytecodePeepholeOptimizer::TransformLdaSmiBinaryOpToBinaryOpWithSmiAction( |
| 275 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
| 276 DCHECK(LastIsValid()); |
| 277 DCHECK(!Bytecodes::IsJump(node->bytecode())); |
| 278 |
| 279 if (!node->source_info().is_valid() || !last()->source_info().is_valid()) { |
| 280 // Fused last and current into current. |
| 281 TransformLdaSmiBinaryOpToBinaryOpWithSmi(action_data->bytecode, last(), |
| 282 node); |
| 283 SetLast(node); |
| 284 } else { |
| 285 DefaultAction(node); |
| 286 } |
| 287 } |
| 288 |
| 289 void BytecodePeepholeOptimizer:: |
| 290 TransformLdaZeroBinaryOpToBinaryOpWithZeroAction( |
| 291 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
| 292 DCHECK(LastIsValid()); |
| 293 DCHECK(!Bytecodes::IsJump(node->bytecode())); |
| 294 if (!node->source_info().is_valid() || !last()->source_info().is_valid()) { |
| 295 // Fused last and current into current. |
| 296 TransformLdaZeroBinaryOpToBinaryOpWithZero(action_data->bytecode, last(), |
| 297 node); |
| 298 SetLast(node); |
| 299 } else { |
| 300 DefaultAction(node); |
| 301 } |
| 302 } |
| 303 |
| 304 void BytecodePeepholeOptimizer::DefaultJumpAction( |
| 305 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
| 306 DCHECK(LastIsValid()); |
| 307 DCHECK(Bytecodes::IsJump(node->bytecode())); |
| 308 |
| 309 next_stage()->Write(last()); |
| 310 InvalidateLast(); |
| 311 } |
| 312 |
| 313 void BytecodePeepholeOptimizer::UpdateLastJumpAction( |
| 314 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
| 315 DCHECK(!LastIsValid()); |
| 316 DCHECK(Bytecodes::IsJump(node->bytecode())); |
| 317 } |
| 318 |
| 319 void BytecodePeepholeOptimizer::ChangeJumpBytecodeAction( |
| 320 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
| 321 DCHECK(LastIsValid()); |
| 322 DCHECK(Bytecodes::IsJump(node->bytecode())); |
| 323 |
| 324 next_stage()->Write(last()); |
| 325 InvalidateLast(); |
| 326 node->set_bytecode(action_data->bytecode, node->operand(0)); |
| 327 } |
| 328 |
| 329 void BytecodePeepholeOptimizer::ElideLastBeforeJumpAction( |
| 330 BytecodeNode* const node, const PeepholeActionAndData* action_data) { |
| 331 DCHECK(LastIsValid()); |
| 332 DCHECK(Bytecodes::IsJump(node->bytecode())); |
| 333 DCHECK(CanElideLastBasedOnSourcePosition(node)); |
| 334 |
| 335 if (!node->source_info().is_valid()) { |
| 336 node->source_info().Clone(last()->source_info()); |
| 337 } else { |
| 338 next_stage()->Write(last()); |
| 339 } |
| 340 InvalidateLast(); |
| 341 } |
| 342 |
| 343 void BytecodePeepholeOptimizer::ApplyPeepholeAction(BytecodeNode* const node) { |
| 344 // A single table is used for looking up peephole optimization |
| 345 // matches as it is observed to have better performance. This is |
| 346 // inspite of the fact that jump bytecodes and non-jump bytecodes |
| 347 // have different processing logic, in particular a jump bytecode |
| 348 // always needs to emit the jump via WriteJump(). |
| 349 const PeepholeActionAndData* const action_data = |
| 350 PeepholeActionTable::Lookup(last()->bytecode(), node->bytecode()); |
| 351 switch (action_data->action) { |
| 352 #define CASE(Action) \ |
| 353 case PeepholeAction::k##Action: \ |
| 354 Action(node, action_data); \ |
| 355 break; |
| 356 PEEPHOLE_ACTION_LIST(CASE) |
| 357 #undef CASE |
| 358 default: |
| 359 UNREACHABLE(); |
| 360 break; |
| 361 } |
371 } | 362 } |
372 | 363 |
373 } // namespace interpreter | 364 } // namespace interpreter |
374 } // namespace internal | 365 } // namespace internal |
375 } // namespace v8 | 366 } // namespace v8 |
OLD | NEW |