machine-operator-reducer.h 5.64 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 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_MACHINE_OPERATOR_REDUCER_H_
#define V8_COMPILER_MACHINE_OPERATOR_REDUCER_H_

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

namespace v8 {
namespace internal {
namespace compiler {

// Forward declarations.
18
class CommonOperatorBuilder;
19
class MachineGraph;
20 21
class Word32Adapter;
class Word64Adapter;
22 23 24

// Performs constant folding and strength reduction on nodes that have
// machine operators.
25
class V8_EXPORT_PRIVATE MachineOperatorReducer final
26
    : public NON_EXPORTED_BASE(AdvancedReducer) {
27
 public:
28
  explicit MachineOperatorReducer(Editor* editor, MachineGraph* mcgraph,
29
                                  bool allow_signalling_nan = true);
30
  ~MachineOperatorReducer() override;
31

32 33
  const char* reducer_name() const override { return "MachineOperatorReducer"; }

34
  Reduction Reduce(Node* node) override;
35 36

 private:
37 38 39
  friend class Word32Adapter;
  friend class Word64Adapter;

40 41
  Node* Float32Constant(float value);
  Node* Float64Constant(double value);
42 43
  Node* Int32Constant(int32_t value);
  Node* Int64Constant(int64_t value);
44
  Node* Uint32Constant(uint32_t value) {
45
    return Int32Constant(base::bit_cast<int32_t>(value));
46 47
  }
  Node* Uint64Constant(uint64_t value) {
48
    return Int64Constant(base::bit_cast<int64_t>(value));
49
  }
50 51
  Node* Float64Mul(Node* lhs, Node* rhs);
  Node* Float64PowHalf(Node* value);
52 53 54 55
  Node* Word32And(Node* lhs, Node* rhs);
  Node* Word32And(Node* lhs, uint32_t rhs) {
    return Word32And(lhs, Uint32Constant(rhs));
  }
56 57
  Node* Word32Sar(Node* lhs, uint32_t rhs);
  Node* Word32Shr(Node* lhs, uint32_t rhs);
58
  Node* Word32Equal(Node* lhs, Node* rhs);
59
  Node* Word64And(Node* lhs, Node* rhs);
60 61 62
  Node* Int32Add(Node* lhs, Node* rhs);
  Node* Int32Sub(Node* lhs, Node* rhs);
  Node* Int32Mul(Node* lhs, Node* rhs);
63 64
  Node* Int32Div(Node* dividend, int32_t divisor);
  Node* Uint32Div(Node* dividend, uint32_t divisor);
65
  Node* TruncateInt64ToInt32(Node* value);
66 67

  Reduction ReplaceBool(bool value) { return ReplaceInt32(value ? 1 : 0); }
68
  Reduction ReplaceFloat32(float value) {
69 70
    return Replace(Float32Constant(value));
  }
71
  Reduction ReplaceFloat64(double value) {
72 73
    return Replace(Float64Constant(value));
  }
74 75 76
  Reduction ReplaceInt32(int32_t value) {
    return Replace(Int32Constant(value));
  }
77 78 79
  Reduction ReplaceUint32(uint32_t value) {
    return Replace(Uint32Constant(value));
  }
80 81
  Reduction ReplaceInt64(int64_t value) {
    return Replace(Int64Constant(value));
82
  }
83

84
  Reduction ReduceInt32Add(Node* node);
85
  Reduction ReduceInt64Add(Node* node);
86
  Reduction ReduceInt32Sub(Node* node);
87
  Reduction ReduceInt64Sub(Node* node);
88
  Reduction ReduceInt64Mul(Node* node);
89
  Reduction ReduceInt32Div(Node* node);
90
  Reduction ReduceUint32Div(Node* node);
91
  Reduction ReduceInt32Mod(Node* node);
92
  Reduction ReduceUint32Mod(Node* node);
93
  Reduction ReduceStore(Node* node);
94
  Reduction ReduceProjection(size_t index, Node* node);
95 96 97
  const Operator* Map64To32Comparison(const Operator* op, bool sign_extended);
  Reduction ReduceWord32Comparisons(Node* node);
  Reduction ReduceWord64Comparisons(Node* node);
98
  Reduction ReduceWord32Shifts(Node* node);
99
  Reduction ReduceWord32Shl(Node* node);
100
  Reduction ReduceWord64Shl(Node* node);
101
  Reduction ReduceWord32Shr(Node* node);
102
  Reduction ReduceWord64Shr(Node* node);
103
  Reduction ReduceWord32Sar(Node* node);
104
  Reduction ReduceWord64Sar(Node* node);
105
  Reduction ReduceWord32And(Node* node);
106
  Reduction ReduceWord64And(Node* node);
107
  Reduction TryMatchWord32Ror(Node* node);
108
  Reduction ReduceWord32Or(Node* node);
109
  Reduction ReduceWord64Or(Node* node);
110
  Reduction ReduceWord32Xor(Node* node);
111
  Reduction ReduceWord64Xor(Node* node);
112
  Reduction ReduceWord32Equal(Node* node);
113 114
  Reduction ReduceFloat64InsertLowWord32(Node* node);
  Reduction ReduceFloat64InsertHighWord32(Node* node);
115
  Reduction ReduceFloat64Compare(Node* node);
116
  Reduction ReduceFloat64RoundDown(Node* node);
117
  Reduction ReduceTruncateInt64ToInt32(Node* node);
118
  Reduction ReduceConditional(Node* node);
119

120
  Graph* graph() const;
121
  MachineGraph* mcgraph() const { return mcgraph_; }
122
  CommonOperatorBuilder* common() const;
123
  MachineOperatorBuilder* machine() const;
124

125 126 127 128 129 130 131 132 133
  // These reductions can be applied to operations of different word sizes.
  // Use Word32Adapter or Word64Adapter to specialize for a particular one.
  template <typename WordNAdapter>
  Reduction ReduceWordNAnd(Node* node);
  template <typename WordNAdapter>
  Reduction ReduceWordNOr(Node* node);
  template <typename WordNAdapter>
  Reduction ReduceWordNXor(Node* node);

134 135 136 137 138 139
  // Tries to simplify "if(x == 0)" by removing the "== 0" and inverting
  // branches.
  Reduction SimplifyBranch(Node* node);
  // Helper for SimplifyBranch; swaps the if/else of a branch.
  void SwapBranches(Node* node);

140 141 142 143 144 145 146 147 148 149 150 151
  // Helper for ReduceConditional. Does not perform the actual reduction; just
  // returns a new Node that could be used as the input to the condition.
  template <typename WordNAdapter>
  base::Optional<Node*> ReduceConditionalN(Node* node);

  // Helper for finding a reduced equality condition. Does not perform the
  // actual reduction; just returns a new pair that could be compared for the
  // same outcome.
  template <typename WordNAdapter>
  base::Optional<std::pair<Node*, uint32_t>> ReduceWord32EqualForConstantRhs(
      Node* lhs, uint32_t rhs);

152
  MachineGraph* mcgraph_;
153
  bool allow_signalling_nan_;
154
};
155 156 157 158

}  // namespace compiler
}  // namespace internal
}  // namespace v8
159 160

#endif  // V8_COMPILER_MACHINE_OPERATOR_REDUCER_H_