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-11-26 11:18:46 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2015-03-09 11:06:30 +00:00
|
|
|
// TODO(turbofan): Move ExternalReference out of assembler.h
|
|
|
|
#include "src/assembler.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); }
|
|
|
|
|
2015-04-08 11:54:53 +00:00
|
|
|
bool Equals(const Node* node) const { return node_ == node; }
|
|
|
|
|
2015-02-24 12:26:16 +00:00
|
|
|
bool IsComparison() const;
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
#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-12-12 18:16:51 +00:00
|
|
|
template <typename T, IrOpcode::Value kOpcode>
|
2014-07-30 13:54:45 +00:00
|
|
|
struct ValueMatcher : public NodeMatcher {
|
2014-11-17 10:50:56 +00:00
|
|
|
typedef T ValueType;
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
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_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-12-12 18:16:51 +00:00
|
|
|
template <>
|
|
|
|
inline ValueMatcher<int64_t, IrOpcode::kInt64Constant>::ValueMatcher(Node* node)
|
|
|
|
: NodeMatcher(node), value_(), has_value_(false) {
|
|
|
|
if (opcode() == IrOpcode::kInt32Constant) {
|
|
|
|
value_ = OpParameter<int32_t>(node);
|
|
|
|
has_value_ = true;
|
|
|
|
} else if (opcode() == IrOpcode::kInt64Constant) {
|
|
|
|
value_ = OpParameter<int64_t>(node);
|
|
|
|
has_value_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
inline ValueMatcher<uint64_t, IrOpcode::kInt64Constant>::ValueMatcher(
|
|
|
|
Node* node)
|
|
|
|
: NodeMatcher(node), value_(), has_value_(false) {
|
|
|
|
if (opcode() == IrOpcode::kInt32Constant) {
|
|
|
|
value_ = OpParameter<uint32_t>(node);
|
|
|
|
has_value_ = true;
|
|
|
|
} else if (opcode() == IrOpcode::kInt64Constant) {
|
|
|
|
value_ = OpParameter<uint64_t>(node);
|
|
|
|
has_value_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
// 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
|
|
|
|
2014-12-18 08:42:43 +00:00
|
|
|
bool IsMultipleOf(T n) const {
|
|
|
|
return this->HasValue() && (this->Value() % n) == 0;
|
|
|
|
}
|
2014-07-30 13:54:45 +00:00
|
|
|
bool IsPowerOf2() const {
|
|
|
|
return this->HasValue() && this->Value() > 0 &&
|
|
|
|
(this->Value() & (this->Value() - 1)) == 0;
|
|
|
|
}
|
2014-11-24 12:30:20 +00:00
|
|
|
bool IsNegativePowerOf2() const {
|
|
|
|
return this->HasValue() && this->Value() < 0 &&
|
|
|
|
(-this->Value() & (-this->Value() - 1)) == 0;
|
|
|
|
}
|
2014-07-30 13:54:45 +00:00
|
|
|
};
|
|
|
|
|
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-10-29 14:16:32 +00:00
|
|
|
#if V8_HOST_ARCH_32_BIT
|
|
|
|
typedef Int32Matcher IntPtrMatcher;
|
|
|
|
typedef Uint32Matcher UintPtrMatcher;
|
|
|
|
#else
|
|
|
|
typedef Int64Matcher IntPtrMatcher;
|
|
|
|
typedef Uint64Matcher UintPtrMatcher;
|
|
|
|
#endif
|
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
|
|
|
|
2014-11-26 11:18:46 +00:00
|
|
|
bool IsMinusZero() const {
|
|
|
|
return this->Is(0.0) && std::signbit(this->Value());
|
|
|
|
}
|
2014-07-30 13:54:45 +00:00
|
|
|
bool IsNaN() const { return this->HasValue() && std::isnan(this->Value()); }
|
2015-04-08 11:54:53 +00:00
|
|
|
bool IsZero() const { return this->Is(0.0) && !std::signbit(this->Value()); }
|
2014-07-30 13:54:45 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-09 11:06:30 +00:00
|
|
|
// A pattern matcher for external reference constants.
|
|
|
|
struct ExternalReferenceMatcher FINAL
|
|
|
|
: public ValueMatcher<ExternalReference, IrOpcode::kExternalConstant> {
|
|
|
|
explicit ExternalReferenceMatcher(Node* node)
|
|
|
|
: ValueMatcher<ExternalReference, IrOpcode::kExternalConstant>(node) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// For shorter pattern matching code, this struct matches the inputs to
|
|
|
|
// machine-level load operations.
|
|
|
|
template <typename Object>
|
|
|
|
struct LoadMatcher : public NodeMatcher {
|
|
|
|
explicit LoadMatcher(Node* node)
|
|
|
|
: NodeMatcher(node), object_(InputAt(0)), index_(InputAt(1)) {}
|
|
|
|
|
|
|
|
typedef Object ObjectMatcher;
|
|
|
|
|
|
|
|
Object const& object() const { return object_; }
|
|
|
|
IntPtrMatcher const& index() const { return index_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Object const object_;
|
|
|
|
IntPtrMatcher const index_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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-11-07 16:47:25 +00:00
|
|
|
struct BinopMatcher : 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();
|
|
|
|
}
|
2014-12-11 11:52:48 +00:00
|
|
|
BinopMatcher(Node* node, bool allow_input_swap)
|
|
|
|
: NodeMatcher(node), left_(InputAt(0)), right_(InputAt(1)) {
|
|
|
|
if (allow_input_swap) PutConstantOnRight();
|
|
|
|
}
|
2014-07-30 13:54:45 +00:00
|
|
|
|
2014-11-17 10:50:56 +00:00
|
|
|
typedef Left LeftMatcher;
|
|
|
|
typedef Right RightMatcher;
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
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(); }
|
|
|
|
|
2014-11-07 16:47:25 +00:00
|
|
|
protected:
|
|
|
|
void SwapInputs() {
|
|
|
|
std::swap(left_, right_);
|
|
|
|
node()->ReplaceInput(0, left().node());
|
|
|
|
node()->ReplaceInput(1, right().node());
|
|
|
|
}
|
|
|
|
|
2014-07-30 13:54:45 +00:00
|
|
|
private:
|
|
|
|
void PutConstantOnRight() {
|
|
|
|
if (left().HasValue() && !right().HasValue()) {
|
2014-11-07 16:47:25 +00:00
|
|
|
SwapInputs();
|
2014-07-30 13:54:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Left left_;
|
|
|
|
Right right_;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef BinopMatcher<Int32Matcher, Int32Matcher> Int32BinopMatcher;
|
|
|
|
typedef BinopMatcher<Uint32Matcher, Uint32Matcher> Uint32BinopMatcher;
|
|
|
|
typedef BinopMatcher<Int64Matcher, Int64Matcher> Int64BinopMatcher;
|
|
|
|
typedef BinopMatcher<Uint64Matcher, Uint64Matcher> Uint64BinopMatcher;
|
2014-10-29 14:16:32 +00:00
|
|
|
typedef BinopMatcher<IntPtrMatcher, IntPtrMatcher> IntPtrBinopMatcher;
|
|
|
|
typedef BinopMatcher<UintPtrMatcher, UintPtrMatcher> UintPtrBinopMatcher;
|
2015-03-30 07:33:46 +00:00
|
|
|
typedef BinopMatcher<Float32Matcher, Float32Matcher> Float32BinopMatcher;
|
2014-07-30 13:54:45 +00:00
|
|
|
typedef BinopMatcher<Float64Matcher, Float64Matcher> Float64BinopMatcher;
|
2014-10-28 13:56:26 +00:00
|
|
|
typedef BinopMatcher<NumberMatcher, NumberMatcher> NumberBinopMatcher;
|
2014-09-08 09:16:11 +00:00
|
|
|
|
2014-11-26 11:18:46 +00:00
|
|
|
|
2014-12-05 10:54:27 +00:00
|
|
|
template <class BinopMatcher, IrOpcode::Value kMulOpcode,
|
|
|
|
IrOpcode::Value kShiftOpcode>
|
|
|
|
struct ScaleMatcher {
|
|
|
|
explicit ScaleMatcher(Node* node, bool allow_power_of_two_plus_one = false)
|
|
|
|
: scale_(-1), power_of_two_plus_one_(false) {
|
|
|
|
if (node->InputCount() < 2) return;
|
|
|
|
BinopMatcher m(node);
|
2014-11-17 10:50:56 +00:00
|
|
|
if (node->opcode() == kShiftOpcode) {
|
2014-11-07 16:47:25 +00:00
|
|
|
if (m.right().HasValue()) {
|
2014-11-17 10:50:56 +00:00
|
|
|
typename BinopMatcher::RightMatcher::ValueType value =
|
|
|
|
m.right().Value();
|
2014-11-07 16:47:25 +00:00
|
|
|
if (value >= 0 && value <= 3) {
|
2014-12-05 10:54:27 +00:00
|
|
|
scale_ = static_cast<int>(value);
|
2014-11-07 16:47:25 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-17 10:50:56 +00:00
|
|
|
} else if (node->opcode() == kMulOpcode) {
|
2014-11-07 16:47:25 +00:00
|
|
|
if (m.right().HasValue()) {
|
2014-11-17 10:50:56 +00:00
|
|
|
typename BinopMatcher::RightMatcher::ValueType value =
|
|
|
|
m.right().Value();
|
2014-11-07 16:47:25 +00:00
|
|
|
if (value == 1) {
|
2014-12-05 10:54:27 +00:00
|
|
|
scale_ = 0;
|
2014-11-07 16:47:25 +00:00
|
|
|
} else if (value == 2) {
|
2014-12-05 10:54:27 +00:00
|
|
|
scale_ = 1;
|
2014-11-07 16:47:25 +00:00
|
|
|
} else if (value == 4) {
|
2014-12-05 10:54:27 +00:00
|
|
|
scale_ = 2;
|
2014-11-07 16:47:25 +00:00
|
|
|
} else if (value == 8) {
|
2014-12-05 10:54:27 +00:00
|
|
|
scale_ = 3;
|
|
|
|
} else if (allow_power_of_two_plus_one) {
|
|
|
|
if (value == 3) {
|
|
|
|
scale_ = 1;
|
|
|
|
power_of_two_plus_one_ = true;
|
|
|
|
} else if (value == 5) {
|
|
|
|
scale_ = 2;
|
|
|
|
power_of_two_plus_one_ = true;
|
|
|
|
} else if (value == 9) {
|
|
|
|
scale_ = 3;
|
|
|
|
power_of_two_plus_one_ = true;
|
|
|
|
}
|
2014-11-07 16:47:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-05 10:54:27 +00:00
|
|
|
bool matches() const { return scale_ != -1; }
|
|
|
|
int scale() const { return scale_; }
|
|
|
|
bool power_of_two_plus_one() const { return power_of_two_plus_one_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int scale_;
|
|
|
|
bool power_of_two_plus_one_;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef ScaleMatcher<Int32BinopMatcher, IrOpcode::kInt32Mul,
|
|
|
|
IrOpcode::kWord32Shl> Int32ScaleMatcher;
|
|
|
|
typedef ScaleMatcher<Int64BinopMatcher, IrOpcode::kInt64Mul,
|
|
|
|
IrOpcode::kWord64Shl> Int64ScaleMatcher;
|
|
|
|
|
|
|
|
|
|
|
|
template <class BinopMatcher, IrOpcode::Value kAddOpcode,
|
|
|
|
IrOpcode::Value kMulOpcode, IrOpcode::Value kShiftOpcode>
|
|
|
|
struct AddMatcher : public BinopMatcher {
|
|
|
|
static const IrOpcode::Value kOpcode = kAddOpcode;
|
|
|
|
typedef ScaleMatcher<BinopMatcher, kMulOpcode, kShiftOpcode> Matcher;
|
|
|
|
|
2014-12-11 11:52:48 +00:00
|
|
|
AddMatcher(Node* node, bool allow_input_swap)
|
|
|
|
: BinopMatcher(node, allow_input_swap),
|
|
|
|
scale_(-1),
|
|
|
|
power_of_two_plus_one_(false) {
|
|
|
|
Initialize(node, allow_input_swap);
|
|
|
|
}
|
2014-12-05 10:54:27 +00:00
|
|
|
explicit AddMatcher(Node* node)
|
2014-12-11 11:52:48 +00:00
|
|
|
: BinopMatcher(node, node->op()->HasProperty(Operator::kCommutative)),
|
|
|
|
scale_(-1),
|
|
|
|
power_of_two_plus_one_(false) {
|
|
|
|
Initialize(node, node->op()->HasProperty(Operator::kCommutative));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasIndexInput() const { return scale_ != -1; }
|
|
|
|
Node* IndexInput() const {
|
|
|
|
DCHECK(HasIndexInput());
|
|
|
|
return this->left().node()->InputAt(0);
|
|
|
|
}
|
|
|
|
int scale() const {
|
|
|
|
DCHECK(HasIndexInput());
|
|
|
|
return scale_;
|
|
|
|
}
|
|
|
|
bool power_of_two_plus_one() const { return power_of_two_plus_one_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Initialize(Node* node, bool allow_input_swap) {
|
2014-12-05 10:54:27 +00:00
|
|
|
Matcher left_matcher(this->left().node(), true);
|
|
|
|
if (left_matcher.matches()) {
|
|
|
|
scale_ = left_matcher.scale();
|
|
|
|
power_of_two_plus_one_ = left_matcher.power_of_two_plus_one();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-11 11:52:48 +00:00
|
|
|
if (!allow_input_swap) {
|
2014-12-05 10:54:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matcher right_matcher(this->right().node(), true);
|
|
|
|
if (right_matcher.matches()) {
|
|
|
|
scale_ = right_matcher.scale();
|
|
|
|
power_of_two_plus_one_ = right_matcher.power_of_two_plus_one();
|
|
|
|
this->SwapInputs();
|
|
|
|
return;
|
2014-11-07 16:47:25 +00:00
|
|
|
}
|
2014-12-05 10:54:27 +00:00
|
|
|
|
|
|
|
if (this->right().opcode() == kAddOpcode &&
|
|
|
|
this->left().opcode() != kAddOpcode) {
|
|
|
|
this->SwapInputs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int scale_;
|
|
|
|
bool power_of_two_plus_one_;
|
2014-11-07 16:47:25 +00:00
|
|
|
};
|
|
|
|
|
2014-11-17 10:50:56 +00:00
|
|
|
typedef AddMatcher<Int32BinopMatcher, IrOpcode::kInt32Add, IrOpcode::kInt32Mul,
|
|
|
|
IrOpcode::kWord32Shl> Int32AddMatcher;
|
|
|
|
typedef AddMatcher<Int64BinopMatcher, IrOpcode::kInt64Add, IrOpcode::kInt64Mul,
|
|
|
|
IrOpcode::kWord64Shl> Int64AddMatcher;
|
|
|
|
|
2014-12-05 10:54:27 +00:00
|
|
|
|
2014-11-17 10:50:56 +00:00
|
|
|
template <class AddMatcher>
|
2014-12-05 10:54:27 +00:00
|
|
|
struct BaseWithIndexAndDisplacementMatcher {
|
2014-12-11 11:52:48 +00:00
|
|
|
BaseWithIndexAndDisplacementMatcher(Node* node, bool allow_input_swap)
|
|
|
|
: matches_(false),
|
|
|
|
index_(NULL),
|
|
|
|
scale_(0),
|
|
|
|
base_(NULL),
|
|
|
|
displacement_(NULL) {
|
|
|
|
Initialize(node, allow_input_swap);
|
|
|
|
}
|
|
|
|
|
2014-12-05 10:54:27 +00:00
|
|
|
explicit BaseWithIndexAndDisplacementMatcher(Node* node)
|
2014-11-07 16:47:25 +00:00
|
|
|
: matches_(false),
|
2014-12-05 10:54:27 +00:00
|
|
|
index_(NULL),
|
|
|
|
scale_(0),
|
|
|
|
base_(NULL),
|
|
|
|
displacement_(NULL) {
|
2014-12-11 11:52:48 +00:00
|
|
|
Initialize(node, node->op()->HasProperty(Operator::kCommutative));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool matches() const { return matches_; }
|
|
|
|
Node* index() const { return index_; }
|
|
|
|
int scale() const { return scale_; }
|
|
|
|
Node* base() const { return base_; }
|
|
|
|
Node* displacement() const { return displacement_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool matches_;
|
|
|
|
Node* index_;
|
|
|
|
int scale_;
|
|
|
|
Node* base_;
|
|
|
|
Node* displacement_;
|
|
|
|
|
|
|
|
void Initialize(Node* node, bool allow_input_swap) {
|
2014-12-05 10:54:27 +00:00
|
|
|
// The BaseWithIndexAndDisplacementMatcher canonicalizes the order of
|
|
|
|
// displacements and scale factors that are used as inputs, so instead of
|
|
|
|
// enumerating all possible patterns by brute force, checking for node
|
|
|
|
// clusters using the following templates in the following order suffices to
|
|
|
|
// find all of the interesting cases (S = index * scale, B = base input, D =
|
|
|
|
// displacement input):
|
|
|
|
// (S + (B + D))
|
|
|
|
// (S + (B + B))
|
|
|
|
// (S + D)
|
|
|
|
// (S + B)
|
|
|
|
// ((S + D) + B)
|
|
|
|
// ((S + B) + D)
|
|
|
|
// ((B + D) + B)
|
|
|
|
// ((B + B) + D)
|
|
|
|
// (B + D)
|
|
|
|
// (B + B)
|
2014-11-17 10:50:56 +00:00
|
|
|
if (node->InputCount() < 2) return;
|
2014-12-11 11:52:48 +00:00
|
|
|
AddMatcher m(node, allow_input_swap);
|
2014-12-05 10:54:27 +00:00
|
|
|
Node* left = m.left().node();
|
|
|
|
Node* right = m.right().node();
|
|
|
|
Node* displacement = NULL;
|
|
|
|
Node* base = NULL;
|
|
|
|
Node* index = NULL;
|
|
|
|
Node* scale_expression = NULL;
|
|
|
|
bool power_of_two_plus_one = false;
|
|
|
|
int scale = 0;
|
|
|
|
if (m.HasIndexInput() && left->OwnedBy(node)) {
|
|
|
|
index = m.IndexInput();
|
|
|
|
scale = m.scale();
|
|
|
|
scale_expression = left;
|
|
|
|
power_of_two_plus_one = m.power_of_two_plus_one();
|
2014-11-17 10:50:56 +00:00
|
|
|
if (right->opcode() == AddMatcher::kOpcode && right->OwnedBy(node)) {
|
|
|
|
AddMatcher right_matcher(right);
|
2014-11-07 16:47:25 +00:00
|
|
|
if (right_matcher.right().HasValue()) {
|
2014-12-05 10:54:27 +00:00
|
|
|
// (S + (B + D))
|
|
|
|
base = right_matcher.left().node();
|
|
|
|
displacement = right_matcher.right().node();
|
2014-11-07 16:47:25 +00:00
|
|
|
} else {
|
2014-12-05 10:54:27 +00:00
|
|
|
// (S + (B + B))
|
|
|
|
base = right;
|
2014-11-07 16:47:25 +00:00
|
|
|
}
|
2014-12-05 10:54:27 +00:00
|
|
|
} else if (m.right().HasValue()) {
|
|
|
|
// (S + D)
|
|
|
|
displacement = right;
|
2014-11-07 16:47:25 +00:00
|
|
|
} else {
|
2014-12-05 10:54:27 +00:00
|
|
|
// (S + B)
|
|
|
|
base = right;
|
2014-11-07 16:47:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-11-17 10:50:56 +00:00
|
|
|
if (left->opcode() == AddMatcher::kOpcode && left->OwnedBy(node)) {
|
|
|
|
AddMatcher left_matcher(left);
|
2014-11-07 16:47:25 +00:00
|
|
|
Node* left_left = left_matcher.left().node();
|
|
|
|
Node* left_right = left_matcher.right().node();
|
2014-12-05 10:54:27 +00:00
|
|
|
if (left_matcher.HasIndexInput() && left_left->OwnedBy(left)) {
|
2014-11-07 16:47:25 +00:00
|
|
|
if (left_matcher.right().HasValue()) {
|
2014-12-05 10:54:27 +00:00
|
|
|
// ((S + D) + B)
|
|
|
|
index = left_matcher.IndexInput();
|
|
|
|
scale = left_matcher.scale();
|
|
|
|
scale_expression = left_left;
|
|
|
|
power_of_two_plus_one = left_matcher.power_of_two_plus_one();
|
|
|
|
displacement = left_right;
|
|
|
|
base = right;
|
|
|
|
} else if (m.right().HasValue()) {
|
|
|
|
// ((S + B) + D)
|
|
|
|
index = left_matcher.IndexInput();
|
|
|
|
scale = left_matcher.scale();
|
|
|
|
scale_expression = left_left;
|
|
|
|
power_of_two_plus_one = left_matcher.power_of_two_plus_one();
|
|
|
|
base = left_right;
|
|
|
|
displacement = right;
|
2014-11-07 16:47:25 +00:00
|
|
|
} else {
|
2014-12-05 10:54:27 +00:00
|
|
|
// (B + B)
|
|
|
|
index = left;
|
|
|
|
base = right;
|
2014-11-07 16:47:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (left_matcher.right().HasValue()) {
|
2014-12-05 10:54:27 +00:00
|
|
|
// ((B + D) + B)
|
|
|
|
index = left_left;
|
|
|
|
displacement = left_right;
|
|
|
|
base = right;
|
|
|
|
} else if (m.right().HasValue()) {
|
|
|
|
// ((B + B) + D)
|
|
|
|
index = left_left;
|
|
|
|
base = left_right;
|
|
|
|
displacement = right;
|
2014-11-07 16:47:25 +00:00
|
|
|
} else {
|
2014-12-05 10:54:27 +00:00
|
|
|
// (B + B)
|
|
|
|
index = left;
|
|
|
|
base = right;
|
2014-11-07 16:47:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-12-05 10:54:27 +00:00
|
|
|
if (m.right().HasValue()) {
|
|
|
|
// (B + D)
|
|
|
|
base = left;
|
|
|
|
displacement = right;
|
2014-11-07 16:47:25 +00:00
|
|
|
} else {
|
2014-12-05 10:54:27 +00:00
|
|
|
// (B + B)
|
|
|
|
base = left;
|
|
|
|
index = right;
|
2014-11-07 16:47:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-17 10:50:56 +00:00
|
|
|
int64_t value = 0;
|
2014-12-05 10:54:27 +00:00
|
|
|
if (displacement != NULL) {
|
|
|
|
switch (displacement->opcode()) {
|
2014-11-17 10:50:56 +00:00
|
|
|
case IrOpcode::kInt32Constant: {
|
2014-12-05 10:54:27 +00:00
|
|
|
value = OpParameter<int32_t>(displacement);
|
2014-11-17 10:50:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case IrOpcode::kInt64Constant: {
|
2014-12-05 10:54:27 +00:00
|
|
|
value = OpParameter<int64_t>(displacement);
|
2014-11-17 10:50:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (value == 0) {
|
2014-12-05 10:54:27 +00:00
|
|
|
displacement = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (power_of_two_plus_one) {
|
|
|
|
if (base != NULL) {
|
|
|
|
// If the scale requires explicitly using the index as the base, but a
|
|
|
|
// base is already part of the match, then the (1 << N + 1) scale factor
|
|
|
|
// can't be folded into the match and the entire index * scale
|
|
|
|
// calculation must be computed separately.
|
|
|
|
index = scale_expression;
|
|
|
|
scale = 0;
|
|
|
|
} else {
|
|
|
|
base = index;
|
2014-11-17 10:50:56 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-05 10:54:27 +00:00
|
|
|
base_ = base;
|
|
|
|
displacement_ = displacement;
|
|
|
|
index_ = index;
|
|
|
|
scale_ = scale;
|
2014-11-07 16:47:25 +00:00
|
|
|
matches_ = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-05 10:54:27 +00:00
|
|
|
typedef BaseWithIndexAndDisplacementMatcher<Int32AddMatcher>
|
|
|
|
BaseWithIndexAndDisplacement32Matcher;
|
|
|
|
typedef BaseWithIndexAndDisplacementMatcher<Int64AddMatcher>
|
|
|
|
BaseWithIndexAndDisplacement64Matcher;
|
2014-11-17 10:50:56 +00:00
|
|
|
|
2015-04-07 09:03:28 +00:00
|
|
|
struct BranchMatcher : public NodeMatcher {
|
|
|
|
explicit BranchMatcher(Node* branch);
|
|
|
|
|
|
|
|
bool Matched() const { return if_true_ && if_false_; }
|
|
|
|
|
|
|
|
Node* Branch() const { return node(); }
|
|
|
|
Node* IfTrue() const { return if_true_; }
|
|
|
|
Node* IfFalse() const { return if_false_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Node* if_true_;
|
|
|
|
Node* if_false_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct DiamondMatcher : public NodeMatcher {
|
|
|
|
explicit DiamondMatcher(Node* merge);
|
|
|
|
|
|
|
|
bool Matched() const { return branch_; }
|
|
|
|
bool IfProjectionsAreOwned() const {
|
|
|
|
return if_true_->OwnedBy(node()) && if_false_->OwnedBy(node());
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* Branch() const { return branch_; }
|
|
|
|
Node* IfTrue() const { return if_true_; }
|
|
|
|
Node* IfFalse() const { return if_false_; }
|
|
|
|
Node* Merge() const { return node(); }
|
|
|
|
|
2015-04-08 11:13:43 +00:00
|
|
|
Node* TrueInputOf(Node* phi) const {
|
|
|
|
DCHECK(IrOpcode::IsPhiOpcode(phi->opcode()));
|
|
|
|
DCHECK_EQ(3, phi->InputCount());
|
|
|
|
DCHECK_EQ(Merge(), phi->InputAt(2));
|
|
|
|
return phi->InputAt(if_true_ == Merge()->InputAt(0) ? 0 : 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* FalseInputOf(Node* phi) const {
|
|
|
|
DCHECK(IrOpcode::IsPhiOpcode(phi->opcode()));
|
|
|
|
DCHECK_EQ(3, phi->InputCount());
|
|
|
|
DCHECK_EQ(Merge(), phi->InputAt(2));
|
|
|
|
return phi->InputAt(if_true_ == Merge()->InputAt(0) ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
2015-04-07 09:03:28 +00:00
|
|
|
private:
|
|
|
|
Node* branch_;
|
|
|
|
Node* if_true_;
|
|
|
|
Node* if_false_;
|
|
|
|
};
|
|
|
|
|
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_
|