1 // Copyright 2015 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 #ifndef V8_COMPILER_DEAD_CODE_ELIMINATION_H_ 6 #define V8_COMPILER_DEAD_CODE_ELIMINATION_H_ 7 8 #include "src/base/compiler-specific.h" 9 #include "src/codegen/machine-type.h" 10 #include "src/common/globals.h" 11 #include "src/compiler/graph-reducer.h" 12 13 namespace v8 { 14 namespace internal { 15 namespace compiler { 16 17 // Forward declarations. 18 class CommonOperatorBuilder; 19 20 // Propagates {Dead} control and {DeadValue} values through the graph and 21 // thereby removes dead code. 22 // We detect dead values based on types, replacing uses of nodes with 23 // {Type::None()} with {DeadValue}. A pure node (other than a phi) using 24 // {DeadValue} is replaced by {DeadValue}. When {DeadValue} hits the effect 25 // chain, a crashing {Unreachable} node is inserted and the rest of the effect 26 // chain is collapsed. We wait for the {EffectControlLinearizer} to connect 27 // {Unreachable} nodes to the graph end, since this is much easier if there is 28 // no floating control. 29 // {DeadValue} has an input, which has to have {Type::None()}. This input is 30 // important to maintain the dependency on the cause of the unreachable code. 31 // {Unreachable} has a value output and {Type::None()} so it can be used by 32 // {DeadValue}. 33 // {DeadValue} nodes track a {MachineRepresentation} so they can be lowered to a 34 // value-producing node. {DeadValue} has the runtime semantics of crashing and 35 // behaves like a constant of its representation so it can be used in gap moves. 36 // Since phi nodes are the only remaining use of {DeadValue}, this 37 // representation is only adjusted for uses by phi nodes. 38 // In contrast to {DeadValue}, {Dead} can never remain in the graph. 39 class V8_EXPORT_PRIVATE DeadCodeElimination final NON_EXPORTED_BASE(AdvancedReducer)40 : public NON_EXPORTED_BASE(AdvancedReducer) { 41 public: 42 DeadCodeElimination(Editor* editor, Graph* graph, 43 CommonOperatorBuilder* common, Zone* temp_zone); 44 ~DeadCodeElimination() final = default; 45 DeadCodeElimination(const DeadCodeElimination&) = delete; 46 DeadCodeElimination& operator=(const DeadCodeElimination&) = delete; 47 48 const char* reducer_name() const override { return "DeadCodeElimination"; } 49 50 Reduction Reduce(Node* node) final; 51 52 private: 53 Reduction ReduceEnd(Node* node); 54 Reduction ReduceLoopOrMerge(Node* node); 55 Reduction ReduceLoopExit(Node* node); 56 Reduction ReduceNode(Node* node); 57 Reduction ReducePhi(Node* node); 58 Reduction ReduceEffectPhi(Node* node); 59 Reduction ReducePureNode(Node* node); 60 Reduction ReduceUnreachableOrIfException(Node* node); 61 Reduction ReduceEffectNode(Node* node); 62 Reduction ReduceDeoptimizeOrReturnOrTerminateOrTailCall(Node* node); 63 Reduction ReduceBranchOrSwitch(Node* node); 64 65 Reduction RemoveLoopExit(Node* node); 66 Reduction PropagateDeadControl(Node* node); 67 68 void TrimMergeOrPhi(Node* node, int size); 69 70 Node* DeadValue(Node* none_node, 71 MachineRepresentation rep = MachineRepresentation::kNone); 72 73 Graph* graph() const { return graph_; } 74 CommonOperatorBuilder* common() const { return common_; } 75 Node* dead() const { return dead_; } 76 77 Graph* const graph_; 78 CommonOperatorBuilder* const common_; 79 Node* const dead_; 80 Zone* zone_; 81 }; 82 83 } // namespace compiler 84 } // namespace internal 85 } // namespace v8 86 87 #endif // V8_COMPILER_DEAD_CODE_ELIMINATION_H_ 88