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/common-operator.h"
14 // A pattern matcher for nodes.
16 explicit NodeMatcher(Node* node) : node_(node) {}
18 Node* node() const { return node_; }
19 Operator* op() const { return node()->op(); }
20 IrOpcode::Value opcode() const { return node()->opcode(); }
22 bool HasProperty(Operator::Property property) const {
23 return op()->HasProperty(property);
25 Node* InputAt(int index) const { return node()->InputAt(index); }
27 #define DEFINE_IS_OPCODE(Opcode) \
28 bool Is##Opcode() const { return opcode() == IrOpcode::k##Opcode; }
29 ALL_OP_LIST(DEFINE_IS_OPCODE)
30 #undef DEFINE_IS_OPCODE
37 // A pattern matcher for abitrary value constants.
39 struct ValueMatcher : public NodeMatcher {
40 explicit ValueMatcher(Node* node)
43 has_value_(CommonOperatorTraits<T>::HasValue(node->op())) {
44 if (has_value_) value_ = CommonOperatorTraits<T>::ValueOf(node->op());
47 bool HasValue() const { return has_value_; }
53 bool Is(T value) const {
54 return HasValue() && CommonOperatorTraits<T>::Equals(Value(), value);
57 bool IsInRange(T low, T high) const {
58 return HasValue() && low <= value_ && value_ <= high;
67 // A pattern matcher for integer constants.
69 struct IntMatcher V8_FINAL : public ValueMatcher<T> {
70 explicit IntMatcher(Node* node) : ValueMatcher<T>(node) {}
72 bool IsPowerOf2() const {
73 return this->HasValue() && this->Value() > 0 &&
74 (this->Value() & (this->Value() - 1)) == 0;
78 typedef IntMatcher<int32_t> Int32Matcher;
79 typedef IntMatcher<uint32_t> Uint32Matcher;
80 typedef IntMatcher<int64_t> Int64Matcher;
81 typedef IntMatcher<uint64_t> Uint64Matcher;
84 // A pattern matcher for floating point constants.
86 struct FloatMatcher V8_FINAL : public ValueMatcher<T> {
87 explicit FloatMatcher(Node* node) : ValueMatcher<T>(node) {}
89 bool IsNaN() const { return this->HasValue() && std::isnan(this->Value()); }
92 typedef FloatMatcher<double> Float64Matcher;
95 // For shorter pattern matching code, this struct matches both the left and
96 // right hand sides of a binary operation and can put constants on the right
97 // if they appear on the left hand side of a commutative operation.
98 template <typename Left, typename Right>
99 struct BinopMatcher V8_FINAL : public NodeMatcher {
100 explicit BinopMatcher(Node* node)
101 : NodeMatcher(node), left_(InputAt(0)), right_(InputAt(1)) {
102 if (HasProperty(Operator::kCommutative)) PutConstantOnRight();
105 const Left& left() const { return left_; }
106 const Right& right() const { return right_; }
108 bool IsFoldable() const { return left().HasValue() && right().HasValue(); }
109 bool LeftEqualsRight() const { return left().node() == right().node(); }
112 void PutConstantOnRight() {
113 if (left().HasValue() && !right().HasValue()) {
114 std::swap(left_, right_);
115 node()->ReplaceInput(0, left().node());
116 node()->ReplaceInput(1, right().node());
124 typedef BinopMatcher<Int32Matcher, Int32Matcher> Int32BinopMatcher;
125 typedef BinopMatcher<Uint32Matcher, Uint32Matcher> Uint32BinopMatcher;
126 typedef BinopMatcher<Int64Matcher, Int64Matcher> Int64BinopMatcher;
127 typedef BinopMatcher<Uint64Matcher, Uint64Matcher> Uint64BinopMatcher;
128 typedef BinopMatcher<Float64Matcher, Float64Matcher> Float64BinopMatcher;
131 } // namespace v8::internal::compiler
133 #endif // V8_COMPILER_NODE_MATCHERS_H_