2014-07-30 13:54:45 +00:00
|
|
|
// 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_NODE_MATCHERS_H_
|
|
|
|
#define V8_COMPILER_NODE_MATCHERS_H_
|
|
|
|
|
2014-08-22 04:47:55 +00:00
|
|
|
#include "src/compiler/node.h"
|
2014-09-08 09:16:11 +00:00
|
|
|
#include "src/compiler/operator.h"
|
2014-10-07 13:30:28 +00:00
|
|
|
#include "src/unique.h"
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
|
|
|
// A pattern matcher for nodes.
|
|
|
|
struct NodeMatcher {
|
|
|
|
explicit NodeMatcher(Node* node) : node_(node) {}
|
|
|
|
|
|
|
|
Node* node() const { return node_; }
|
2014-09-04 09:37:25 +00:00
|
|
|
const Operator* op() const { return node()->op(); }
|
2014-07-30 13:54:45 +00:00
|
|
|
IrOpcode::Value opcode() const { return node()->opcode(); }
|
|
|
|
|
|
|
|
bool HasProperty(Operator::Property property) const {
|
|
|
|
return op()->HasProperty(property);
|
|
|
|
}
|
|
|
|
Node* InputAt(int index) const { return node()->InputAt(index); }
|
|
|
|
|
|
|
|
#define DEFINE_IS_OPCODE(Opcode) \
|
|
|
|
bool Is##Opcode() const { return opcode() == IrOpcode::k##Opcode; }
|
|
|
|
ALL_OP_LIST(DEFINE_IS_OPCODE)
|
|
|
|
#undef DEFINE_IS_OPCODE
|
|
|
|
|
|
|
|
private:
|
|
|
|
Node* node_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// A pattern matcher for abitrary value constants.
|
2014-09-08 09:16:11 +00:00
|
|
|
template <typename T, IrOpcode::Value kOpcode>
|
2014-07-30 13:54:45 +00:00
|
|
|
struct ValueMatcher : public NodeMatcher {
|
|
|
|
explicit ValueMatcher(Node* node)
|
2014-09-08 09:16:11 +00:00
|
|
|
: NodeMatcher(node), value_(), has_value_(opcode() == kOpcode) {
|
|
|
|
if (has_value_) {
|
|
|
|
value_ = OpParameter<T>(node);
|
|
|
|
}
|
2014-07-30 13:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HasValue() const { return has_value_; }
|
2014-09-08 09:16:11 +00:00
|
|
|
const T& Value() const {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(HasValue());
|
2014-07-30 13:54:45 +00:00
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
2014-09-08 09:16:11 +00:00
|
|
|
bool Is(const T& value) const {
|
|
|
|
return this->HasValue() && this->Value() == value;
|
2014-07-30 13:54:45 +00:00
|
|
|
}
|
|
|
|
|
2014-09-08 09:16:11 +00:00
|
|
|
bool IsInRange(const T& low, const T& high) const {
|
|
|
|
return this->HasValue() && low <= this->Value() && this->Value() <= high;
|
2014-07-30 13:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T value_;
|
|
|
|
bool has_value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// A pattern matcher for integer constants.
|
2014-09-08 09:16:11 +00:00
|
|
|
template <typename T, IrOpcode::Value kOpcode>
|
|
|
|
struct IntMatcher FINAL : public ValueMatcher<T, kOpcode> {
|
|
|
|
explicit IntMatcher(Node* node) : ValueMatcher<T, kOpcode>(node) {}
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
bool IsPowerOf2() const {
|
|
|
|
return this->HasValue() && this->Value() > 0 &&
|
|
|
|
(this->Value() & (this->Value() - 1)) == 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-08 09:16:11 +00:00
|
|
|
typedef IntMatcher<int32_t, IrOpcode::kInt32Constant> Int32Matcher;
|
|
|
|
typedef IntMatcher<uint32_t, IrOpcode::kInt32Constant> Uint32Matcher;
|
|
|
|
typedef IntMatcher<int64_t, IrOpcode::kInt64Constant> Int64Matcher;
|
|
|
|
typedef IntMatcher<uint64_t, IrOpcode::kInt64Constant> Uint64Matcher;
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
// A pattern matcher for floating point constants.
|
2014-09-08 09:16:11 +00:00
|
|
|
template <typename T, IrOpcode::Value kOpcode>
|
|
|
|
struct FloatMatcher FINAL : public ValueMatcher<T, kOpcode> {
|
|
|
|
explicit FloatMatcher(Node* node) : ValueMatcher<T, kOpcode>(node) {}
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
bool IsNaN() const { return this->HasValue() && std::isnan(this->Value()); }
|
|
|
|
};
|
|
|
|
|
2014-09-22 11:42:10 +00:00
|
|
|
typedef FloatMatcher<float, IrOpcode::kFloat32Constant> Float32Matcher;
|
2014-09-08 09:16:11 +00:00
|
|
|
typedef FloatMatcher<double, IrOpcode::kFloat64Constant> Float64Matcher;
|
|
|
|
typedef FloatMatcher<double, IrOpcode::kNumberConstant> NumberMatcher;
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
|
2014-08-22 04:47:55 +00:00
|
|
|
// A pattern matcher for heap object constants.
|
2014-09-08 09:16:11 +00:00
|
|
|
template <typename T>
|
|
|
|
struct HeapObjectMatcher FINAL
|
|
|
|
: public ValueMatcher<Unique<T>, IrOpcode::kHeapConstant> {
|
2014-08-22 04:47:55 +00:00
|
|
|
explicit HeapObjectMatcher(Node* node)
|
2014-09-08 09:16:11 +00:00
|
|
|
: ValueMatcher<Unique<T>, IrOpcode::kHeapConstant>(node) {}
|
2014-08-22 04:47:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
// For shorter pattern matching code, this struct matches both the left and
|
|
|
|
// right hand sides of a binary operation and can put constants on the right
|
|
|
|
// if they appear on the left hand side of a commutative operation.
|
|
|
|
template <typename Left, typename Right>
|
2014-09-02 07:07:52 +00:00
|
|
|
struct BinopMatcher FINAL : public NodeMatcher {
|
2014-07-30 13:54:45 +00:00
|
|
|
explicit BinopMatcher(Node* node)
|
|
|
|
: NodeMatcher(node), left_(InputAt(0)), right_(InputAt(1)) {
|
|
|
|
if (HasProperty(Operator::kCommutative)) PutConstantOnRight();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Left& left() const { return left_; }
|
|
|
|
const Right& right() const { return right_; }
|
|
|
|
|
|
|
|
bool IsFoldable() const { return left().HasValue() && right().HasValue(); }
|
|
|
|
bool LeftEqualsRight() const { return left().node() == right().node(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void PutConstantOnRight() {
|
|
|
|
if (left().HasValue() && !right().HasValue()) {
|
|
|
|
std::swap(left_, right_);
|
|
|
|
node()->ReplaceInput(0, left().node());
|
|
|
|
node()->ReplaceInput(1, right().node());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Left left_;
|
|
|
|
Right right_;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef BinopMatcher<Int32Matcher, Int32Matcher> Int32BinopMatcher;
|
|
|
|
typedef BinopMatcher<Uint32Matcher, Uint32Matcher> Uint32BinopMatcher;
|
|
|
|
typedef BinopMatcher<Int64Matcher, Int64Matcher> Int64BinopMatcher;
|
|
|
|
typedef BinopMatcher<Uint64Matcher, Uint64Matcher> Uint64BinopMatcher;
|
|
|
|
typedef BinopMatcher<Float64Matcher, Float64Matcher> Float64BinopMatcher;
|
2014-09-08 09:16:11 +00:00
|
|
|
|
2014-09-29 10:15:55 +00:00
|
|
|
|
|
|
|
// Fairly intel-specify node matcher used for matching scale factors in
|
|
|
|
// addressing modes.
|
|
|
|
// Matches nodes of form [x * N] for N in {1,2,4,8}
|
|
|
|
class ScaleFactorMatcher : public NodeMatcher {
|
|
|
|
public:
|
2014-09-30 09:46:30 +00:00
|
|
|
static const int kMatchedFactors[4];
|
|
|
|
|
|
|
|
explicit ScaleFactorMatcher(Node* node);
|
2014-09-29 10:15:55 +00:00
|
|
|
|
2014-09-30 09:46:30 +00:00
|
|
|
bool Matches() const { return left_ != NULL; }
|
|
|
|
int Power() const {
|
2014-09-29 10:15:55 +00:00
|
|
|
DCHECK(Matches());
|
|
|
|
return power_;
|
|
|
|
}
|
2014-09-30 09:46:30 +00:00
|
|
|
Node* Left() const {
|
2014-09-29 10:15:55 +00:00
|
|
|
DCHECK(Matches());
|
|
|
|
return left_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Node* left_;
|
|
|
|
int power_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Fairly intel-specify node matcher used for matching index and displacement
|
|
|
|
// operands in addressing modes.
|
|
|
|
// Matches nodes of form:
|
|
|
|
// [x * N]
|
|
|
|
// [x * N + K]
|
|
|
|
// [x + K]
|
|
|
|
// [x] -- fallback case
|
|
|
|
// for N in {1,2,4,8} and K int32_t
|
|
|
|
class IndexAndDisplacementMatcher : public NodeMatcher {
|
|
|
|
public:
|
2014-09-30 09:46:30 +00:00
|
|
|
explicit IndexAndDisplacementMatcher(Node* node);
|
2014-09-29 10:15:55 +00:00
|
|
|
|
2014-09-30 09:46:30 +00:00
|
|
|
Node* index_node() const { return index_node_; }
|
|
|
|
int displacement() const { return displacement_; }
|
|
|
|
int power() const { return power_; }
|
2014-09-29 10:15:55 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
Node* index_node_;
|
|
|
|
int displacement_;
|
|
|
|
int power_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-09-30 09:46:30 +00:00
|
|
|
// Fairly intel-specify node matcher used for matching multiplies that can be
|
|
|
|
// transformed to lea instructions.
|
|
|
|
// Matches nodes of form:
|
|
|
|
// [x * N]
|
|
|
|
// for N in {1,2,3,4,5,8,9}
|
|
|
|
class LeaMultiplyMatcher : public NodeMatcher {
|
|
|
|
public:
|
|
|
|
static const int kMatchedFactors[7];
|
|
|
|
|
|
|
|
explicit LeaMultiplyMatcher(Node* node);
|
|
|
|
|
|
|
|
bool Matches() const { return left_ != NULL; }
|
|
|
|
int Power() const {
|
|
|
|
DCHECK(Matches());
|
|
|
|
return power_;
|
|
|
|
}
|
|
|
|
Node* Left() const {
|
|
|
|
DCHECK(Matches());
|
|
|
|
return left_;
|
|
|
|
}
|
|
|
|
// Displacement will be either 0 or 1.
|
|
|
|
int32_t Displacement() const {
|
|
|
|
DCHECK(Matches());
|
|
|
|
return displacement_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Node* left_;
|
|
|
|
int power_;
|
|
|
|
int displacement_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-09-08 09:16:11 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
2014-07-30 13:54:45 +00:00
|
|
|
|
|
|
|
#endif // V8_COMPILER_NODE_MATCHERS_H_
|