Update To 11.40.268.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/node.h"
9 #include "src/compiler/operator.h"
10 #include "src/unique.h"
11
12 namespace v8 {
13 namespace internal {
14 namespace compiler {
15
16 // A pattern matcher for nodes.
17 struct NodeMatcher {
18   explicit NodeMatcher(Node* node) : node_(node) {}
19
20   Node* node() const { return node_; }
21   const Operator* op() const { return node()->op(); }
22   IrOpcode::Value opcode() const { return node()->opcode(); }
23
24   bool HasProperty(Operator::Property property) const {
25     return op()->HasProperty(property);
26   }
27   Node* InputAt(int index) const { return node()->InputAt(index); }
28
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
33
34  private:
35   Node* node_;
36 };
37
38
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) {
44     if (has_value_) {
45       value_ = OpParameter<T>(node);
46     }
47   }
48
49   bool HasValue() const { return has_value_; }
50   const T& Value() const {
51     DCHECK(HasValue());
52     return value_;
53   }
54
55   bool Is(const T& value) const {
56     return this->HasValue() && this->Value() == value;
57   }
58
59   bool IsInRange(const T& low, const T& high) const {
60     return this->HasValue() && low <= this->Value() && this->Value() <= high;
61   }
62
63  private:
64   T value_;
65   bool has_value_;
66 };
67
68
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) {}
73
74   bool IsPowerOf2() const {
75     return this->HasValue() && this->Value() > 0 &&
76            (this->Value() & (this->Value() - 1)) == 0;
77   }
78 };
79
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;
87 #else
88 typedef Int64Matcher IntPtrMatcher;
89 typedef Uint64Matcher UintPtrMatcher;
90 #endif
91
92
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) {}
97
98   bool IsNaN() const { return this->HasValue() && std::isnan(this->Value()); }
99 };
100
101 typedef FloatMatcher<float, IrOpcode::kFloat32Constant> Float32Matcher;
102 typedef FloatMatcher<double, IrOpcode::kFloat64Constant> Float64Matcher;
103 typedef FloatMatcher<double, IrOpcode::kNumberConstant> NumberMatcher;
104
105
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) {}
112 };
113
114
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();
123   }
124
125   const Left& left() const { return left_; }
126   const Right& right() const { return right_; }
127
128   bool IsFoldable() const { return left().HasValue() && right().HasValue(); }
129   bool LeftEqualsRight() const { return left().node() == right().node(); }
130
131  private:
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());
137     }
138   }
139
140   Left left_;
141   Right right_;
142 };
143
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;
152
153 }  // namespace compiler
154 }  // namespace internal
155 }  // namespace v8
156
157 #endif  // V8_COMPILER_NODE_MATCHERS_H_