1 // Copyright 2014 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.
5 #ifndef V8_COMPILER_NODE_MATCHERS_H_
6 #define V8_COMPILER_NODE_MATCHERS_H_
8 #include "src/compiler/node.h"
9 #include "src/compiler/operator.h"
10 #include "src/unique.h"
16 // A pattern matcher for nodes.
18 explicit NodeMatcher(Node* node) : node_(node) {}
20 Node* node() const { return node_; }
21 const Operator* op() const { return node()->op(); }
22 IrOpcode::Value opcode() const { return node()->opcode(); }
24 bool HasProperty(Operator::Property property) const {
25 return op()->HasProperty(property);
27 Node* InputAt(int index) const { return node()->InputAt(index); }
29 #define DEFINE_IS_OPCODE(Opcode) \
30 bool Is##Opcode() const { return opcode() == IrOpcode::k##Opcode; }
31 ALL_OP_LIST(DEFINE_IS_OPCODE)
32 #undef DEFINE_IS_OPCODE
39 // A pattern matcher for abitrary value constants.
40 template <typename T, IrOpcode::Value kOpcode>
41 struct ValueMatcher : public NodeMatcher {
42 explicit ValueMatcher(Node* node)
43 : NodeMatcher(node), value_(), has_value_(opcode() == kOpcode) {
45 value_ = OpParameter<T>(node);
49 bool HasValue() const { return has_value_; }
50 const T& Value() const {
55 bool Is(const T& value) const {
56 return this->HasValue() && this->Value() == value;
59 bool IsInRange(const T& low, const T& high) const {
60 return this->HasValue() && low <= this->Value() && this->Value() <= high;
69 // A pattern matcher for integer constants.
70 template <typename T, IrOpcode::Value kOpcode>
71 struct IntMatcher FINAL : public ValueMatcher<T, kOpcode> {
72 explicit IntMatcher(Node* node) : ValueMatcher<T, kOpcode>(node) {}
74 bool IsPowerOf2() const {
75 return this->HasValue() && this->Value() > 0 &&
76 (this->Value() & (this->Value() - 1)) == 0;
80 typedef IntMatcher<int32_t, IrOpcode::kInt32Constant> Int32Matcher;
81 typedef IntMatcher<uint32_t, IrOpcode::kInt32Constant> Uint32Matcher;
82 typedef IntMatcher<int64_t, IrOpcode::kInt64Constant> Int64Matcher;
83 typedef IntMatcher<uint64_t, IrOpcode::kInt64Constant> Uint64Matcher;
84 #if V8_HOST_ARCH_32_BIT
85 typedef Int32Matcher IntPtrMatcher;
86 typedef Uint32Matcher UintPtrMatcher;
88 typedef Int64Matcher IntPtrMatcher;
89 typedef Uint64Matcher UintPtrMatcher;
93 // A pattern matcher for floating point constants.
94 template <typename T, IrOpcode::Value kOpcode>
95 struct FloatMatcher FINAL : public ValueMatcher<T, kOpcode> {
96 explicit FloatMatcher(Node* node) : ValueMatcher<T, kOpcode>(node) {}
98 bool IsNaN() const { return this->HasValue() && std::isnan(this->Value()); }
101 typedef FloatMatcher<float, IrOpcode::kFloat32Constant> Float32Matcher;
102 typedef FloatMatcher<double, IrOpcode::kFloat64Constant> Float64Matcher;
103 typedef FloatMatcher<double, IrOpcode::kNumberConstant> NumberMatcher;
106 // A pattern matcher for heap object constants.
107 template <typename T>
108 struct HeapObjectMatcher FINAL
109 : public ValueMatcher<Unique<T>, IrOpcode::kHeapConstant> {
110 explicit HeapObjectMatcher(Node* node)
111 : ValueMatcher<Unique<T>, IrOpcode::kHeapConstant>(node) {}
115 // For shorter pattern matching code, this struct matches both the left and
116 // right hand sides of a binary operation and can put constants on the right
117 // if they appear on the left hand side of a commutative operation.
118 template <typename Left, typename Right>
119 struct BinopMatcher FINAL : public NodeMatcher {
120 explicit BinopMatcher(Node* node)
121 : NodeMatcher(node), left_(InputAt(0)), right_(InputAt(1)) {
122 if (HasProperty(Operator::kCommutative)) PutConstantOnRight();
125 const Left& left() const { return left_; }
126 const Right& right() const { return right_; }
128 bool IsFoldable() const { return left().HasValue() && right().HasValue(); }
129 bool LeftEqualsRight() const { return left().node() == right().node(); }
132 void PutConstantOnRight() {
133 if (left().HasValue() && !right().HasValue()) {
134 std::swap(left_, right_);
135 node()->ReplaceInput(0, left().node());
136 node()->ReplaceInput(1, right().node());
144 typedef BinopMatcher<Int32Matcher, Int32Matcher> Int32BinopMatcher;
145 typedef BinopMatcher<Uint32Matcher, Uint32Matcher> Uint32BinopMatcher;
146 typedef BinopMatcher<Int64Matcher, Int64Matcher> Int64BinopMatcher;
147 typedef BinopMatcher<Uint64Matcher, Uint64Matcher> Uint64BinopMatcher;
148 typedef BinopMatcher<IntPtrMatcher, IntPtrMatcher> IntPtrBinopMatcher;
149 typedef BinopMatcher<UintPtrMatcher, UintPtrMatcher> UintPtrBinopMatcher;
150 typedef BinopMatcher<Float64Matcher, Float64Matcher> Float64BinopMatcher;
151 typedef BinopMatcher<NumberMatcher, NumberMatcher> NumberBinopMatcher;
153 } // namespace compiler
154 } // namespace internal
157 #endif // V8_COMPILER_NODE_MATCHERS_H_