Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / v8 / src / compiler / node-matchers.h
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.
4
5 #ifndef V8_COMPILER_NODE_MATCHERS_H_
6 #define V8_COMPILER_NODE_MATCHERS_H_
7
8 #include "src/compiler/common-operator.h"
9
10 namespace v8 {
11 namespace internal {
12 namespace compiler {
13
14 // A pattern matcher for nodes.
15 struct NodeMatcher {
16   explicit NodeMatcher(Node* node) : node_(node) {}
17
18   Node* node() const { return node_; }
19   Operator* op() const { return node()->op(); }
20   IrOpcode::Value opcode() const { return node()->opcode(); }
21
22   bool HasProperty(Operator::Property property) const {
23     return op()->HasProperty(property);
24   }
25   Node* InputAt(int index) const { return node()->InputAt(index); }
26
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
31
32  private:
33   Node* node_;
34 };
35
36
37 // A pattern matcher for abitrary value constants.
38 template <typename T>
39 struct ValueMatcher : public NodeMatcher {
40   explicit ValueMatcher(Node* node)
41       : NodeMatcher(node),
42         value_(),
43         has_value_(CommonOperatorTraits<T>::HasValue(node->op())) {
44     if (has_value_) value_ = CommonOperatorTraits<T>::ValueOf(node->op());
45   }
46
47   bool HasValue() const { return has_value_; }
48   T Value() const {
49     DCHECK(HasValue());
50     return value_;
51   }
52
53   bool Is(T value) const {
54     return HasValue() && CommonOperatorTraits<T>::Equals(Value(), value);
55   }
56
57   bool IsInRange(T low, T high) const {
58     return HasValue() && low <= value_ && value_ <= high;
59   }
60
61  private:
62   T value_;
63   bool has_value_;
64 };
65
66
67 // A pattern matcher for integer constants.
68 template <typename T>
69 struct IntMatcher V8_FINAL : public ValueMatcher<T> {
70   explicit IntMatcher(Node* node) : ValueMatcher<T>(node) {}
71
72   bool IsPowerOf2() const {
73     return this->HasValue() && this->Value() > 0 &&
74            (this->Value() & (this->Value() - 1)) == 0;
75   }
76 };
77
78 typedef IntMatcher<int32_t> Int32Matcher;
79 typedef IntMatcher<uint32_t> Uint32Matcher;
80 typedef IntMatcher<int64_t> Int64Matcher;
81 typedef IntMatcher<uint64_t> Uint64Matcher;
82
83
84 // A pattern matcher for floating point constants.
85 template <typename T>
86 struct FloatMatcher V8_FINAL : public ValueMatcher<T> {
87   explicit FloatMatcher(Node* node) : ValueMatcher<T>(node) {}
88
89   bool IsNaN() const { return this->HasValue() && std::isnan(this->Value()); }
90 };
91
92 typedef FloatMatcher<double> Float64Matcher;
93
94
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();
103   }
104
105   const Left& left() const { return left_; }
106   const Right& right() const { return right_; }
107
108   bool IsFoldable() const { return left().HasValue() && right().HasValue(); }
109   bool LeftEqualsRight() const { return left().node() == right().node(); }
110
111  private:
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());
117     }
118   }
119
120   Left left_;
121   Right right_;
122 };
123
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;
129 }
130 }
131 }  // namespace v8::internal::compiler
132
133 #endif  // V8_COMPILER_NODE_MATCHERS_H_