2014-08-08 07:04:07 +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.
|
|
|
|
|
2014-10-01 08:34:25 +00:00
|
|
|
#ifndef V8_UNITTESTS_COMPILER_INSTRUCTION_SELECTOR_UNITTEST_H_
|
|
|
|
#define V8_UNITTESTS_COMPILER_INSTRUCTION_SELECTOR_UNITTEST_H_
|
2014-08-08 07:04:07 +00:00
|
|
|
|
|
|
|
#include <deque>
|
2014-08-20 09:16:30 +00:00
|
|
|
#include <set>
|
2014-08-08 07:04:07 +00:00
|
|
|
|
2014-08-14 06:33:50 +00:00
|
|
|
#include "src/base/utils/random-number-generator.h"
|
2014-08-08 07:04:07 +00:00
|
|
|
#include "src/compiler/instruction-selector.h"
|
|
|
|
#include "src/compiler/raw-machine-assembler.h"
|
2014-10-30 09:00:58 +00:00
|
|
|
#include "src/macro-assembler.h"
|
2014-10-01 08:34:25 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
2014-08-08 07:04:07 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
2014-09-04 08:44:03 +00:00
|
|
|
class InstructionSelectorTest : public TestWithContext, public TestWithZone {
|
2014-08-08 07:04:07 +00:00
|
|
|
public:
|
2014-08-14 06:33:50 +00:00
|
|
|
InstructionSelectorTest();
|
2014-12-22 13:47:54 +00:00
|
|
|
~InstructionSelectorTest() OVERRIDE;
|
2014-08-08 07:04:07 +00:00
|
|
|
|
2014-08-14 06:33:50 +00:00
|
|
|
base::RandomNumberGenerator* rng() { return &rng_; }
|
|
|
|
|
2014-08-08 07:04:07 +00:00
|
|
|
class Stream;
|
|
|
|
|
2014-08-27 15:56:11 +00:00
|
|
|
enum StreamBuilderMode {
|
|
|
|
kAllInstructions,
|
|
|
|
kTargetInstructions,
|
|
|
|
kAllExceptNopInstructions
|
|
|
|
};
|
2014-08-08 07:04:07 +00:00
|
|
|
|
2014-09-02 07:07:52 +00:00
|
|
|
class StreamBuilder FINAL : public RawMachineAssembler {
|
2014-08-08 07:04:07 +00:00
|
|
|
public:
|
2014-08-11 15:55:28 +00:00
|
|
|
StreamBuilder(InstructionSelectorTest* test, MachineType return_type)
|
2015-01-23 15:19:34 +00:00
|
|
|
: RawMachineAssembler(test->isolate(),
|
|
|
|
new (test->zone()) Graph(test->zone()),
|
2014-09-03 10:13:21 +00:00
|
|
|
MakeMachineSignature(test->zone(), return_type)),
|
2014-08-08 07:04:07 +00:00
|
|
|
test_(test) {}
|
2014-08-11 15:55:28 +00:00
|
|
|
StreamBuilder(InstructionSelectorTest* test, MachineType return_type,
|
|
|
|
MachineType parameter0_type)
|
2014-09-03 10:13:21 +00:00
|
|
|
: RawMachineAssembler(
|
2015-01-23 15:19:34 +00:00
|
|
|
test->isolate(), new (test->zone()) Graph(test->zone()),
|
2014-09-03 10:13:21 +00:00
|
|
|
MakeMachineSignature(test->zone(), return_type, parameter0_type)),
|
2014-08-08 07:04:07 +00:00
|
|
|
test_(test) {}
|
2014-08-11 15:55:28 +00:00
|
|
|
StreamBuilder(InstructionSelectorTest* test, MachineType return_type,
|
|
|
|
MachineType parameter0_type, MachineType parameter1_type)
|
2014-08-08 07:04:07 +00:00
|
|
|
: RawMachineAssembler(
|
2015-01-23 15:19:34 +00:00
|
|
|
test->isolate(), new (test->zone()) Graph(test->zone()),
|
2014-09-03 10:13:21 +00:00
|
|
|
MakeMachineSignature(test->zone(), return_type, parameter0_type,
|
|
|
|
parameter1_type)),
|
2014-08-08 07:04:07 +00:00
|
|
|
test_(test) {}
|
2014-08-14 06:33:50 +00:00
|
|
|
StreamBuilder(InstructionSelectorTest* test, MachineType return_type,
|
|
|
|
MachineType parameter0_type, MachineType parameter1_type,
|
|
|
|
MachineType parameter2_type)
|
|
|
|
: RawMachineAssembler(
|
2015-01-23 15:19:34 +00:00
|
|
|
test->isolate(), new (test->zone()) Graph(test->zone()),
|
2014-09-03 10:13:21 +00:00
|
|
|
MakeMachineSignature(test->zone(), return_type, parameter0_type,
|
|
|
|
parameter1_type, parameter2_type)),
|
2014-08-14 06:33:50 +00:00
|
|
|
test_(test) {}
|
2014-08-08 07:04:07 +00:00
|
|
|
|
|
|
|
Stream Build(CpuFeature feature) {
|
|
|
|
return Build(InstructionSelector::Features(feature));
|
|
|
|
}
|
|
|
|
Stream Build(CpuFeature feature1, CpuFeature feature2) {
|
|
|
|
return Build(InstructionSelector::Features(feature1, feature2));
|
|
|
|
}
|
|
|
|
Stream Build(StreamBuilderMode mode = kTargetInstructions) {
|
|
|
|
return Build(InstructionSelector::Features(), mode);
|
|
|
|
}
|
|
|
|
Stream Build(InstructionSelector::Features features,
|
|
|
|
StreamBuilderMode mode = kTargetInstructions);
|
|
|
|
|
|
|
|
private:
|
2014-09-03 10:13:21 +00:00
|
|
|
MachineSignature* MakeMachineSignature(Zone* zone,
|
|
|
|
MachineType return_type) {
|
|
|
|
MachineSignature::Builder builder(zone, 1, 0);
|
|
|
|
builder.AddReturn(return_type);
|
|
|
|
return builder.Build();
|
2014-08-08 07:04:07 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 10:13:21 +00:00
|
|
|
MachineSignature* MakeMachineSignature(Zone* zone, MachineType return_type,
|
|
|
|
MachineType parameter0_type) {
|
|
|
|
MachineSignature::Builder builder(zone, 1, 1);
|
|
|
|
builder.AddReturn(return_type);
|
|
|
|
builder.AddParam(parameter0_type);
|
|
|
|
return builder.Build();
|
2014-08-08 07:04:07 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 10:13:21 +00:00
|
|
|
MachineSignature* MakeMachineSignature(Zone* zone, MachineType return_type,
|
|
|
|
MachineType parameter0_type,
|
|
|
|
MachineType parameter1_type) {
|
|
|
|
MachineSignature::Builder builder(zone, 1, 2);
|
|
|
|
builder.AddReturn(return_type);
|
|
|
|
builder.AddParam(parameter0_type);
|
|
|
|
builder.AddParam(parameter1_type);
|
|
|
|
return builder.Build();
|
2014-08-08 07:04:07 +00:00
|
|
|
}
|
|
|
|
|
2014-09-03 10:13:21 +00:00
|
|
|
MachineSignature* MakeMachineSignature(Zone* zone, MachineType return_type,
|
|
|
|
MachineType parameter0_type,
|
|
|
|
MachineType parameter1_type,
|
|
|
|
MachineType parameter2_type) {
|
|
|
|
MachineSignature::Builder builder(zone, 1, 3);
|
|
|
|
builder.AddReturn(return_type);
|
|
|
|
builder.AddParam(parameter0_type);
|
|
|
|
builder.AddParam(parameter1_type);
|
|
|
|
builder.AddParam(parameter2_type);
|
|
|
|
return builder.Build();
|
2014-08-14 06:33:50 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 07:04:07 +00:00
|
|
|
private:
|
|
|
|
InstructionSelectorTest* test_;
|
|
|
|
};
|
|
|
|
|
2014-09-02 07:07:52 +00:00
|
|
|
class Stream FINAL {
|
2014-08-08 07:04:07 +00:00
|
|
|
public:
|
|
|
|
size_t size() const { return instructions_.size(); }
|
|
|
|
const Instruction* operator[](size_t index) const {
|
|
|
|
EXPECT_LT(index, size());
|
|
|
|
return instructions_[index];
|
|
|
|
}
|
|
|
|
|
2014-08-26 08:29:12 +00:00
|
|
|
bool IsDouble(const InstructionOperand* operand) const {
|
|
|
|
return IsDouble(ToVreg(operand));
|
|
|
|
}
|
2014-10-06 14:30:55 +00:00
|
|
|
|
|
|
|
bool IsDouble(const Node* node) const { return IsDouble(ToVreg(node)); }
|
2014-08-20 09:16:30 +00:00
|
|
|
|
2014-08-26 08:29:12 +00:00
|
|
|
bool IsInteger(const InstructionOperand* operand) const {
|
|
|
|
return IsInteger(ToVreg(operand));
|
|
|
|
}
|
2014-10-06 14:30:55 +00:00
|
|
|
|
|
|
|
bool IsInteger(const Node* node) const { return IsInteger(ToVreg(node)); }
|
2014-08-26 08:29:12 +00:00
|
|
|
|
|
|
|
bool IsReference(const InstructionOperand* operand) const {
|
|
|
|
return IsReference(ToVreg(operand));
|
|
|
|
}
|
2014-10-06 14:30:55 +00:00
|
|
|
|
|
|
|
bool IsReference(const Node* node) const {
|
|
|
|
return IsReference(ToVreg(node));
|
2014-08-20 09:16:30 +00:00
|
|
|
}
|
|
|
|
|
2014-09-25 08:56:02 +00:00
|
|
|
float ToFloat32(const InstructionOperand* operand) const {
|
|
|
|
return ToConstant(operand).ToFloat32();
|
|
|
|
}
|
|
|
|
|
2014-10-08 08:47:29 +00:00
|
|
|
double ToFloat64(const InstructionOperand* operand) const {
|
|
|
|
return ToConstant(operand).ToFloat64();
|
|
|
|
}
|
|
|
|
|
2014-08-08 07:04:07 +00:00
|
|
|
int32_t ToInt32(const InstructionOperand* operand) const {
|
|
|
|
return ToConstant(operand).ToInt32();
|
|
|
|
}
|
|
|
|
|
2014-09-22 12:32:23 +00:00
|
|
|
int64_t ToInt64(const InstructionOperand* operand) const {
|
|
|
|
return ToConstant(operand).ToInt64();
|
|
|
|
}
|
|
|
|
|
2014-10-08 08:47:29 +00:00
|
|
|
Handle<HeapObject> ToHeapObject(const InstructionOperand* operand) const {
|
|
|
|
return ToConstant(operand).ToHeapObject();
|
|
|
|
}
|
|
|
|
|
2014-08-14 06:33:50 +00:00
|
|
|
int ToVreg(const InstructionOperand* operand) const {
|
2014-08-26 08:29:12 +00:00
|
|
|
if (operand->IsConstant()) return operand->index();
|
2014-08-14 06:33:50 +00:00
|
|
|
EXPECT_EQ(InstructionOperand::UNALLOCATED, operand->kind());
|
|
|
|
return UnallocatedOperand::cast(operand)->virtual_register();
|
|
|
|
}
|
|
|
|
|
2014-10-06 14:30:55 +00:00
|
|
|
int ToVreg(const Node* node) const;
|
|
|
|
|
2014-10-24 09:36:40 +00:00
|
|
|
bool IsFixed(const InstructionOperand* operand, Register reg) const;
|
2014-10-31 06:41:07 +00:00
|
|
|
bool IsSameAsFirst(const InstructionOperand* operand) const;
|
2014-10-24 09:36:40 +00:00
|
|
|
bool IsUsedAtStart(const InstructionOperand* operand) const;
|
|
|
|
|
2014-09-01 09:31:14 +00:00
|
|
|
FrameStateDescriptor* GetFrameStateDescriptor(int deoptimization_id) {
|
|
|
|
EXPECT_LT(deoptimization_id, GetFrameStateDescriptorCount());
|
2014-08-27 15:56:11 +00:00
|
|
|
return deoptimization_entries_[deoptimization_id];
|
|
|
|
}
|
|
|
|
|
2014-09-01 09:31:14 +00:00
|
|
|
int GetFrameStateDescriptorCount() {
|
2014-08-27 15:56:11 +00:00
|
|
|
return static_cast<int>(deoptimization_entries_.size());
|
|
|
|
}
|
|
|
|
|
2014-08-08 07:04:07 +00:00
|
|
|
private:
|
2014-10-06 14:30:55 +00:00
|
|
|
bool IsDouble(int virtual_register) const {
|
|
|
|
return doubles_.find(virtual_register) != doubles_.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsInteger(int virtual_register) const {
|
|
|
|
return !IsDouble(virtual_register) && !IsReference(virtual_register);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsReference(int virtual_register) const {
|
|
|
|
return references_.find(virtual_register) != references_.end();
|
|
|
|
}
|
|
|
|
|
2014-08-08 07:04:07 +00:00
|
|
|
Constant ToConstant(const InstructionOperand* operand) const {
|
|
|
|
ConstantMap::const_iterator i;
|
|
|
|
if (operand->IsConstant()) {
|
|
|
|
i = constants_.find(operand->index());
|
2014-08-19 08:27:33 +00:00
|
|
|
EXPECT_FALSE(constants_.end() == i);
|
2014-08-08 07:04:07 +00:00
|
|
|
} else {
|
|
|
|
EXPECT_EQ(InstructionOperand::IMMEDIATE, operand->kind());
|
|
|
|
i = immediates_.find(operand->index());
|
2014-08-19 08:27:33 +00:00
|
|
|
EXPECT_FALSE(immediates_.end() == i);
|
2014-08-08 07:04:07 +00:00
|
|
|
}
|
|
|
|
EXPECT_EQ(operand->index(), i->first);
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
friend class StreamBuilder;
|
|
|
|
|
|
|
|
typedef std::map<int, Constant> ConstantMap;
|
2014-10-06 14:30:55 +00:00
|
|
|
typedef std::map<NodeId, int> VirtualRegisters;
|
2014-08-08 07:04:07 +00:00
|
|
|
|
|
|
|
ConstantMap constants_;
|
|
|
|
ConstantMap immediates_;
|
|
|
|
std::deque<Instruction*> instructions_;
|
2014-08-20 09:16:30 +00:00
|
|
|
std::set<int> doubles_;
|
|
|
|
std::set<int> references_;
|
2014-10-06 14:30:55 +00:00
|
|
|
VirtualRegisters virtual_registers_;
|
2014-08-27 15:56:11 +00:00
|
|
|
std::deque<FrameStateDescriptor*> deoptimization_entries_;
|
2014-08-08 07:04:07 +00:00
|
|
|
};
|
2014-08-14 06:33:50 +00:00
|
|
|
|
|
|
|
base::RandomNumberGenerator rng_;
|
2014-08-08 07:04:07 +00:00
|
|
|
};
|
|
|
|
|
2014-08-14 06:33:50 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class InstructionSelectorTestWithParam
|
|
|
|
: public InstructionSelectorTest,
|
|
|
|
public ::testing::WithParamInterface<T> {};
|
|
|
|
|
2014-08-08 07:04:07 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
2014-10-01 08:34:25 +00:00
|
|
|
#endif // V8_UNITTESTS_COMPILER_INSTRUCTION_SELECTOR_UNITTEST_H_
|