dead-code-elimination.h 3.26 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_COMPILER_DEAD_CODE_ELIMINATION_H_
#define V8_COMPILER_DEAD_CODE_ELIMINATION_H_

8
#include "src/base/compiler-specific.h"
9
#include "src/codegen/machine-type.h"
10
#include "src/common/globals.h"
11 12 13 14 15 16 17 18 19
#include "src/compiler/graph-reducer.h"

namespace v8 {
namespace internal {
namespace compiler {

// Forward declarations.
class CommonOperatorBuilder;

20
// Propagates {Dead} control and {DeadValue} values through the graph and
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
// thereby removes dead code.
// We detect dead values based on types, replacing uses of nodes with
// {Type::None()} with {DeadValue}. A pure node (other than a phi) using
// {DeadValue} is replaced by {DeadValue}. When {DeadValue} hits the effect
// chain, a crashing {Unreachable} node is inserted and the rest of the effect
// chain is collapsed. We wait for the {EffectControlLinearizer} to connect
// {Unreachable} nodes to the graph end, since this is much easier if there is
// no floating control.
// {DeadValue} has an input, which has to have {Type::None()}. This input is
// important to maintain the dependency on the cause of the unreachable code.
// {Unreachable} has a value output and {Type::None()} so it can be used by
// {DeadValue}.
// {DeadValue} nodes track a {MachineRepresentation} so they can be lowered to a
// value-producing node. {DeadValue} has the runtime semantics of crashing and
// behaves like a constant of its representation so it can be used in gap moves.
// Since phi nodes are the only remaining use of {DeadValue}, this
// representation is only adjusted for uses by phi nodes.
38
// In contrast to {DeadValue}, {Dead} can never remain in the graph.
39 40
class V8_EXPORT_PRIVATE DeadCodeElimination final
    : public NON_EXPORTED_BASE(AdvancedReducer) {
41 42
 public:
  DeadCodeElimination(Editor* editor, Graph* graph,
43
                      CommonOperatorBuilder* common, Zone* temp_zone);
44
  ~DeadCodeElimination() final = default;
45

46 47
  const char* reducer_name() const override { return "DeadCodeElimination"; }

48 49 50 51 52
  Reduction Reduce(Node* node) final;

 private:
  Reduction ReduceEnd(Node* node);
  Reduction ReduceLoopOrMerge(Node* node);
53
  Reduction ReduceLoopExit(Node* node);
54
  Reduction ReduceNode(Node* node);
55
  Reduction ReducePhi(Node* node);
56
  Reduction ReduceEffectPhi(Node* node);
57 58 59
  Reduction ReducePureNode(Node* node);
  Reduction ReduceUnreachableOrIfException(Node* node);
  Reduction ReduceEffectNode(Node* node);
60
  Reduction ReduceDeoptimizeOrReturnOrTerminateOrTailCall(Node* node);
61
  Reduction ReduceBranchOrSwitch(Node* node);
62

63
  Reduction RemoveLoopExit(Node* node);
64
  Reduction PropagateDeadControl(Node* node);
65

66 67
  void TrimMergeOrPhi(Node* node, int size);

68 69 70
  Node* DeadValue(Node* none_node,
                  MachineRepresentation rep = MachineRepresentation::kNone);

71 72 73 74 75 76 77
  Graph* graph() const { return graph_; }
  CommonOperatorBuilder* common() const { return common_; }
  Node* dead() const { return dead_; }

  Graph* const graph_;
  CommonOperatorBuilder* const common_;
  Node* const dead_;
78
  Zone* zone_;
79 80 81 82 83 84 85 86 87

  DISALLOW_COPY_AND_ASSIGN(DeadCodeElimination);
};

}  // namespace compiler
}  // namespace internal
}  // namespace v8

#endif  // V8_COMPILER_DEAD_CODE_ELIMINATION_H_