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 "src/interpreter/bytecode-dead-code-optimizer.h"
6
7 namespace v8 {
8 namespace internal {
9 namespace interpreter {
10
BytecodeDeadCodeOptimizer(BytecodePipelineStage * next_stage)11 BytecodeDeadCodeOptimizer::BytecodeDeadCodeOptimizer(
12 BytecodePipelineStage* next_stage)
13 : next_stage_(next_stage), exit_seen_in_block_(false) {}
14
15 // override
ToBytecodeArray(Isolate * isolate,int register_count,int parameter_count,Handle<FixedArray> handler_table)16 Handle<BytecodeArray> BytecodeDeadCodeOptimizer::ToBytecodeArray(
17 Isolate* isolate, int register_count, int parameter_count,
18 Handle<FixedArray> handler_table) {
19 return next_stage_->ToBytecodeArray(isolate, register_count, parameter_count,
20 handler_table);
21 }
22
23 // override
Write(BytecodeNode * node)24 void BytecodeDeadCodeOptimizer::Write(BytecodeNode* node) {
25 // Don't emit dead code.
26 if (exit_seen_in_block_) return;
27
28 switch (node->bytecode()) {
29 case Bytecode::kReturn:
30 case Bytecode::kThrow:
31 case Bytecode::kReThrow:
32 exit_seen_in_block_ = true;
33 break;
34 default:
35 break;
36 }
37
38 next_stage_->Write(node);
39 }
40
41 // override
WriteJump(BytecodeNode * node,BytecodeLabel * label)42 void BytecodeDeadCodeOptimizer::WriteJump(BytecodeNode* node,
43 BytecodeLabel* label) {
44 // Don't emit dead code.
45 // TODO(rmcilroy): For forward jumps we could mark the label as dead, thereby
46 // avoiding emitting dead code when we bind the label.
47 if (exit_seen_in_block_) return;
48
49 switch (node->bytecode()) {
50 case Bytecode::kJump:
51 case Bytecode::kJumpConstant:
52 exit_seen_in_block_ = true;
53 break;
54 default:
55 break;
56 }
57
58 next_stage_->WriteJump(node, label);
59 }
60
61 // override
BindLabel(BytecodeLabel * label)62 void BytecodeDeadCodeOptimizer::BindLabel(BytecodeLabel* label) {
63 next_stage_->BindLabel(label);
64 exit_seen_in_block_ = false;
65 }
66
67 // override
BindLabel(const BytecodeLabel & target,BytecodeLabel * label)68 void BytecodeDeadCodeOptimizer::BindLabel(const BytecodeLabel& target,
69 BytecodeLabel* label) {
70 next_stage_->BindLabel(target, label);
71 // exit_seen_in_block_ was reset when target was bound, so shouldn't be
72 // changed here.
73 }
74
75 } // namespace interpreter
76 } // namespace internal
77 } // namespace v8
78