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"
|
2015-07-07 15:02:39 +00:00
|
|
|
#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 {
|
|
|
|
|
2015-01-23 16:29:50 +00:00
|
|
|
class InstructionSelectorTest : public TestWithContext,
|
|
|
|
public TestWithIsolateAndZone {
|
2014-08-08 07:04:07 +00:00
|
|
|
public:
|
2014-08-14 06:33:50 +00:00
|
|
|
InstructionSelectorTest();
|
2015-04-20 13:08:11 +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
|
|
|
|
2015-04-20 13:08:11 +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-12-10 09:03:30 +00:00
|
|
|
: RawMachineAssembler(test->isolate(),
|
|
|
|
new (test->zone()) Graph(test->zone()),
|
|
|
|
MakeCallDescriptor(test->zone(), return_type),
|
|
|
|
MachineType::PointerRepresentation(),
|
|
|
|
MachineOperatorBuilder::kAllOptionalOps),
|
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()),
|
2015-07-21 15:54:16 +00:00
|
|
|
MakeCallDescriptor(test->zone(), return_type, parameter0_type),
|
2015-12-10 09:03:30 +00:00
|
|
|
MachineType::PointerRepresentation(),
|
|
|
|
MachineOperatorBuilder::kAllOptionalOps),
|
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()),
|
2015-07-21 15:54:16 +00:00
|
|
|
MakeCallDescriptor(test->zone(), return_type, parameter0_type,
|
|
|
|
parameter1_type),
|
2015-12-10 09:03:30 +00:00
|
|
|
MachineType::PointerRepresentation(),
|
|
|
|
MachineOperatorBuilder::kAllOptionalOps),
|
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()),
|
2015-07-21 15:54:16 +00:00
|
|
|
MakeCallDescriptor(test->zone(), return_type, parameter0_type,
|
|
|
|
parameter1_type, parameter2_type),
|
2015-12-10 09:03:30 +00:00
|
|
|
MachineType::PointerRepresentation(),
|
|
|
|
MachineOperatorBuilder::kAllOptionalOps),
|
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,
|
2015-04-30 09:56:24 +00:00
|
|
|
StreamBuilderMode mode = kTargetInstructions,
|
|
|
|
InstructionSelector::SourcePositionMode source_position_mode =
|
|
|
|
InstructionSelector::kAllSourcePositions);
|
2014-08-08 07:04:07 +00:00
|
|
|
|
2015-06-23 07:17:07 +00:00
|
|
|
const FrameStateFunctionInfo* GetFrameStateFunctionInfo(int parameter_count,
|
|
|
|
int local_count);
|
|
|
|
|
2014-08-08 07:04:07 +00:00
|
|
|
private:
|
2015-07-21 15:54:16 +00:00
|
|
|
CallDescriptor* MakeCallDescriptor(Zone* zone, MachineType return_type) {
|
2014-09-03 10:13:21 +00:00
|
|
|
MachineSignature::Builder builder(zone, 1, 0);
|
|
|
|
builder.AddReturn(return_type);
|
2016-02-15 13:25:12 +00:00
|
|
|
return MakeSimpleCallDescriptor(zone, builder.Build());
|
2014-08-08 07:04:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 15:54:16 +00:00
|
|
|
CallDescriptor* MakeCallDescriptor(Zone* zone, MachineType return_type,
|
|
|
|
MachineType parameter0_type) {
|
2014-09-03 10:13:21 +00:00
|
|
|
MachineSignature::Builder builder(zone, 1, 1);
|
|
|
|
builder.AddReturn(return_type);
|
|
|
|
builder.AddParam(parameter0_type);
|
2016-02-15 13:25:12 +00:00
|
|
|
return MakeSimpleCallDescriptor(zone, builder.Build());
|
2014-08-08 07:04:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 15:54:16 +00:00
|
|
|
CallDescriptor* MakeCallDescriptor(Zone* zone, MachineType return_type,
|
|
|
|
MachineType parameter0_type,
|
|
|
|
MachineType parameter1_type) {
|
2014-09-03 10:13:21 +00:00
|
|
|
MachineSignature::Builder builder(zone, 1, 2);
|
|
|
|
builder.AddReturn(return_type);
|
|
|
|
builder.AddParam(parameter0_type);
|
|
|
|
builder.AddParam(parameter1_type);
|
2016-02-15 13:25:12 +00:00
|
|
|
return MakeSimpleCallDescriptor(zone, builder.Build());
|
2014-08-08 07:04:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 15:54:16 +00:00
|
|
|
CallDescriptor* MakeCallDescriptor(Zone* zone, MachineType return_type,
|
|
|
|
MachineType parameter0_type,
|
|
|
|
MachineType parameter1_type,
|
|
|
|
MachineType parameter2_type) {
|
2014-09-03 10:13:21 +00:00
|
|
|
MachineSignature::Builder builder(zone, 1, 3);
|
|
|
|
builder.AddReturn(return_type);
|
|
|
|
builder.AddParam(parameter0_type);
|
|
|
|
builder.AddParam(parameter1_type);
|
|
|
|
builder.AddParam(parameter2_type);
|
2016-02-15 13:25:12 +00:00
|
|
|
return MakeSimpleCallDescriptor(zone, builder.Build());
|
2014-08-14 06:33:50 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 07:04:07 +00:00
|
|
|
private:
|
|
|
|
InstructionSelectorTest* test_;
|
2016-02-15 13:25:12 +00:00
|
|
|
|
|
|
|
// Create a simple call descriptor for testing.
|
|
|
|
CallDescriptor* MakeSimpleCallDescriptor(Zone* zone,
|
|
|
|
MachineSignature* msig) {
|
|
|
|
LocationSignature::Builder locations(zone, msig->return_count(),
|
|
|
|
msig->parameter_count());
|
|
|
|
|
|
|
|
// Add return location(s).
|
|
|
|
const int return_count = static_cast<int>(msig->return_count());
|
|
|
|
for (int i = 0; i < return_count; i++) {
|
|
|
|
locations.AddReturn(LinkageLocation::ForCallerFrameSlot(-1 - i));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just put all parameters on the stack.
|
|
|
|
const int parameter_count = static_cast<int>(msig->parameter_count());
|
|
|
|
for (int i = 0; i < parameter_count; i++) {
|
|
|
|
locations.AddParam(LinkageLocation::ForCallerFrameSlot(-1 - i));
|
|
|
|
}
|
|
|
|
|
|
|
|
const RegList kCalleeSaveRegisters = 0;
|
|
|
|
const RegList kCalleeSaveFPRegisters = 0;
|
|
|
|
|
|
|
|
MachineType target_type = MachineType::Pointer();
|
|
|
|
LinkageLocation target_loc = LinkageLocation::ForAnyRegister();
|
|
|
|
return new (zone) CallDescriptor( // --
|
|
|
|
CallDescriptor::kCallAddress, // kind
|
|
|
|
target_type, // target MachineType
|
|
|
|
target_loc, // target location
|
|
|
|
msig, // machine_sig
|
|
|
|
locations.Build(), // location_sig
|
|
|
|
0, // stack_parameter_count
|
|
|
|
Operator::kNoProperties, // properties
|
|
|
|
kCalleeSaveRegisters, // callee-saved registers
|
|
|
|
kCalleeSaveFPRegisters, // callee-saved fp regs
|
|
|
|
CallDescriptor::kNoFlags, // flags
|
|
|
|
"iselect-test-call");
|
|
|
|
}
|
2014-08-08 07:04:07 +00:00
|
|
|
};
|
|
|
|
|
2015-04-20 13:08:11 +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 {
|
2015-04-09 09:15:28 +00:00
|
|
|
if (operand->IsConstant()) {
|
|
|
|
return ConstantOperand::cast(operand)->virtual_register();
|
|
|
|
}
|
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()) {
|
2015-04-09 09:15:28 +00:00
|
|
|
i = constants_.find(ConstantOperand::cast(operand)->virtual_register());
|
|
|
|
EXPECT_EQ(ConstantOperand::cast(operand)->virtual_register(), i->first);
|
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());
|
2015-04-09 14:06:19 +00:00
|
|
|
auto imm = ImmediateOperand::cast(operand);
|
|
|
|
if (imm->type() == ImmediateOperand::INLINE) {
|
|
|
|
return Constant(imm->inline_value());
|
|
|
|
}
|
|
|
|
i = immediates_.find(imm->indexed_value());
|
|
|
|
EXPECT_EQ(imm->indexed_value(), i->first);
|
2014-08-19 08:27:33 +00:00
|
|
|
EXPECT_FALSE(immediates_.end() == i);
|
2014-08-08 07:04:07 +00:00
|
|
|
}
|
|
|
|
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_
|