branch-elimination.h 2.94 KB
Newer Older
1 2 3 4
// 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.

5 6
#ifndef V8_COMPILER_BRANCH_ELIMINATION_H_
#define V8_COMPILER_BRANCH_ELIMINATION_H_
7

8
#include "src/base/compiler-specific.h"
9
#include "src/compiler/functional-list.h"
10
#include "src/compiler/graph-reducer.h"
11
#include "src/compiler/node-aux-data.h"
12
#include "src/globals.h"
13 14 15 16 17

namespace v8 {
namespace internal {
namespace compiler {

18 19
// Forward declarations.
class CommonOperatorBuilder;
20 21
class JSGraph;

22 23
class V8_EXPORT_PRIVATE BranchElimination final
    : public NON_EXPORTED_BASE(AdvancedReducer) {
24 25 26 27
 public:
  BranchElimination(Editor* editor, JSGraph* js_graph, Zone* zone);
  ~BranchElimination() final;

28 29
  const char* reducer_name() const override { return "BranchElimination"; }

30 31 32 33 34
  Reduction Reduce(Node* node) final;

 private:
  struct BranchCondition {
    Node* condition;
35
    Node* branch;
36 37
    bool is_true;

38
    bool operator==(BranchCondition other) const {
39 40
      return condition == other.condition && branch == other.branch &&
             is_true == other.is_true;
41 42
    }
    bool operator!=(BranchCondition other) const { return !(*this == other); }
43 44 45 46 47
  };

  // Class for tracking information about branch conditions.
  // At the moment it is a linked list of conditions and their values
  // (true or false).
48
  class ControlPathConditions : public FunctionalList<BranchCondition> {
49
   public:
50 51
    bool LookupCondition(Node* condition, Node** branch, bool* is_true) const;
    void AddCondition(Zone* zone, Node* condition, Node* branch, bool is_true,
52
                      ControlPathConditions hint);
53 54

   private:
55
    using FunctionalList<BranchCondition>::PushFront;
56 57 58
  };

  Reduction ReduceBranch(Node* node);
59
  Reduction ReduceDeoptimizeConditional(Node* node);
60 61 62 63 64 65 66
  Reduction ReduceIf(Node* node, bool is_true_branch);
  Reduction ReduceLoop(Node* node);
  Reduction ReduceMerge(Node* node);
  Reduction ReduceStart(Node* node);
  Reduction ReduceOtherControl(Node* node);

  Reduction TakeConditionsFromFirstControl(Node* node);
67 68
  Reduction UpdateConditions(Node* node, ControlPathConditions conditions);
  Reduction UpdateConditions(Node* node, ControlPathConditions prev_conditions,
69 70
                             Node* current_condition, Node* current_branch,
                             bool is_true_branch);
71 72

  Node* dead() const { return dead_; }
73 74
  Graph* graph() const;
  JSGraph* jsgraph() const { return jsgraph_; }
75
  Isolate* isolate() const;
76
  CommonOperatorBuilder* common() const;
77

78
  JSGraph* const jsgraph_;
79 80 81 82 83 84

  // Maps each control node to the condition information known about the node.
  // If the information is nullptr, then we have not calculated the information
  // yet.
  NodeAuxData<ControlPathConditions> node_conditions_;
  NodeAuxData<bool> reduced_;
85 86 87 88 89 90 91 92
  Zone* zone_;
  Node* dead_;
};

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

93
#endif  // V8_COMPILER_BRANCH_ELIMINATION_H_