| OLD | NEW |
| (Empty) |
| 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_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_ | |
| 6 #define V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_ | |
| 7 | |
| 8 #include <deque> | |
| 9 #include <set> | |
| 10 | |
| 11 #include "src/base/utils/random-number-generator.h" | |
| 12 #include "src/compiler/instruction-selector.h" | |
| 13 #include "src/compiler/raw-machine-assembler.h" | |
| 14 #include "test/compiler-unittests/compiler-unittests.h" | |
| 15 | |
| 16 namespace v8 { | |
| 17 namespace internal { | |
| 18 namespace compiler { | |
| 19 | |
| 20 class InstructionSelectorTest : public CompilerTest { | |
| 21 public: | |
| 22 InstructionSelectorTest(); | |
| 23 virtual ~InstructionSelectorTest() {} | |
| 24 | |
| 25 base::RandomNumberGenerator* rng() { return &rng_; } | |
| 26 | |
| 27 class Stream; | |
| 28 | |
| 29 enum StreamBuilderMode { | |
| 30 kAllInstructions, | |
| 31 kTargetInstructions, | |
| 32 kAllExceptNopInstructions | |
| 33 }; | |
| 34 | |
| 35 class StreamBuilder V8_FINAL : public RawMachineAssembler { | |
| 36 public: | |
| 37 StreamBuilder(InstructionSelectorTest* test, MachineType return_type) | |
| 38 : RawMachineAssembler(new (test->zone()) Graph(test->zone()), | |
| 39 CallDescriptorBuilder(test->zone(), return_type)), | |
| 40 test_(test) {} | |
| 41 StreamBuilder(InstructionSelectorTest* test, MachineType return_type, | |
| 42 MachineType parameter0_type) | |
| 43 : RawMachineAssembler(new (test->zone()) Graph(test->zone()), | |
| 44 CallDescriptorBuilder(test->zone(), return_type, | |
| 45 parameter0_type)), | |
| 46 test_(test) {} | |
| 47 StreamBuilder(InstructionSelectorTest* test, MachineType return_type, | |
| 48 MachineType parameter0_type, MachineType parameter1_type) | |
| 49 : RawMachineAssembler( | |
| 50 new (test->zone()) Graph(test->zone()), | |
| 51 CallDescriptorBuilder(test->zone(), return_type, parameter0_type, | |
| 52 parameter1_type)), | |
| 53 test_(test) {} | |
| 54 StreamBuilder(InstructionSelectorTest* test, MachineType return_type, | |
| 55 MachineType parameter0_type, MachineType parameter1_type, | |
| 56 MachineType parameter2_type) | |
| 57 : RawMachineAssembler( | |
| 58 new (test->zone()) Graph(test->zone()), | |
| 59 CallDescriptorBuilder(test->zone(), return_type, parameter0_type, | |
| 60 parameter1_type, parameter2_type)), | |
| 61 test_(test) {} | |
| 62 | |
| 63 Stream Build(CpuFeature feature) { | |
| 64 return Build(InstructionSelector::Features(feature)); | |
| 65 } | |
| 66 Stream Build(CpuFeature feature1, CpuFeature feature2) { | |
| 67 return Build(InstructionSelector::Features(feature1, feature2)); | |
| 68 } | |
| 69 Stream Build(StreamBuilderMode mode = kTargetInstructions) { | |
| 70 return Build(InstructionSelector::Features(), mode); | |
| 71 } | |
| 72 Stream Build(InstructionSelector::Features features, | |
| 73 StreamBuilderMode mode = kTargetInstructions); | |
| 74 | |
| 75 private: | |
| 76 MachineCallDescriptorBuilder* CallDescriptorBuilder( | |
| 77 Zone* zone, MachineType return_type) { | |
| 78 return new (zone) MachineCallDescriptorBuilder(return_type, 0, NULL); | |
| 79 } | |
| 80 | |
| 81 MachineCallDescriptorBuilder* CallDescriptorBuilder( | |
| 82 Zone* zone, MachineType return_type, MachineType parameter0_type) { | |
| 83 MachineType* parameter_types = zone->NewArray<MachineType>(1); | |
| 84 parameter_types[0] = parameter0_type; | |
| 85 return new (zone) | |
| 86 MachineCallDescriptorBuilder(return_type, 1, parameter_types); | |
| 87 } | |
| 88 | |
| 89 MachineCallDescriptorBuilder* CallDescriptorBuilder( | |
| 90 Zone* zone, MachineType return_type, MachineType parameter0_type, | |
| 91 MachineType parameter1_type) { | |
| 92 MachineType* parameter_types = zone->NewArray<MachineType>(2); | |
| 93 parameter_types[0] = parameter0_type; | |
| 94 parameter_types[1] = parameter1_type; | |
| 95 return new (zone) | |
| 96 MachineCallDescriptorBuilder(return_type, 2, parameter_types); | |
| 97 } | |
| 98 | |
| 99 MachineCallDescriptorBuilder* CallDescriptorBuilder( | |
| 100 Zone* zone, MachineType return_type, MachineType parameter0_type, | |
| 101 MachineType parameter1_type, MachineType parameter2_type) { | |
| 102 MachineType* parameter_types = zone->NewArray<MachineType>(3); | |
| 103 parameter_types[0] = parameter0_type; | |
| 104 parameter_types[1] = parameter1_type; | |
| 105 parameter_types[2] = parameter2_type; | |
| 106 return new (zone) | |
| 107 MachineCallDescriptorBuilder(return_type, 3, parameter_types); | |
| 108 } | |
| 109 | |
| 110 private: | |
| 111 InstructionSelectorTest* test_; | |
| 112 }; | |
| 113 | |
| 114 class Stream V8_FINAL { | |
| 115 public: | |
| 116 size_t size() const { return instructions_.size(); } | |
| 117 const Instruction* operator[](size_t index) const { | |
| 118 EXPECT_LT(index, size()); | |
| 119 return instructions_[index]; | |
| 120 } | |
| 121 | |
| 122 bool IsDouble(const InstructionOperand* operand) const { | |
| 123 return IsDouble(ToVreg(operand)); | |
| 124 } | |
| 125 bool IsDouble(int virtual_register) const { | |
| 126 return doubles_.find(virtual_register) != doubles_.end(); | |
| 127 } | |
| 128 | |
| 129 bool IsInteger(const InstructionOperand* operand) const { | |
| 130 return IsInteger(ToVreg(operand)); | |
| 131 } | |
| 132 bool IsInteger(int virtual_register) const { | |
| 133 return !IsDouble(virtual_register) && !IsReference(virtual_register); | |
| 134 } | |
| 135 | |
| 136 bool IsReference(const InstructionOperand* operand) const { | |
| 137 return IsReference(ToVreg(operand)); | |
| 138 } | |
| 139 bool IsReference(int virtual_register) const { | |
| 140 return references_.find(virtual_register) != references_.end(); | |
| 141 } | |
| 142 | |
| 143 int32_t ToInt32(const InstructionOperand* operand) const { | |
| 144 return ToConstant(operand).ToInt32(); | |
| 145 } | |
| 146 | |
| 147 int ToVreg(const InstructionOperand* operand) const { | |
| 148 if (operand->IsConstant()) return operand->index(); | |
| 149 EXPECT_EQ(InstructionOperand::UNALLOCATED, operand->kind()); | |
| 150 return UnallocatedOperand::cast(operand)->virtual_register(); | |
| 151 } | |
| 152 | |
| 153 FrameStateDescriptor* GetFrameStateDescriptor(int deoptimization_id) { | |
| 154 EXPECT_LT(deoptimization_id, GetFrameStateDescriptorCount()); | |
| 155 return deoptimization_entries_[deoptimization_id]; | |
| 156 } | |
| 157 | |
| 158 int GetFrameStateDescriptorCount() { | |
| 159 return static_cast<int>(deoptimization_entries_.size()); | |
| 160 } | |
| 161 | |
| 162 private: | |
| 163 Constant ToConstant(const InstructionOperand* operand) const { | |
| 164 ConstantMap::const_iterator i; | |
| 165 if (operand->IsConstant()) { | |
| 166 i = constants_.find(operand->index()); | |
| 167 EXPECT_FALSE(constants_.end() == i); | |
| 168 } else { | |
| 169 EXPECT_EQ(InstructionOperand::IMMEDIATE, operand->kind()); | |
| 170 i = immediates_.find(operand->index()); | |
| 171 EXPECT_FALSE(immediates_.end() == i); | |
| 172 } | |
| 173 EXPECT_EQ(operand->index(), i->first); | |
| 174 return i->second; | |
| 175 } | |
| 176 | |
| 177 friend class StreamBuilder; | |
| 178 | |
| 179 typedef std::map<int, Constant> ConstantMap; | |
| 180 | |
| 181 ConstantMap constants_; | |
| 182 ConstantMap immediates_; | |
| 183 std::deque<Instruction*> instructions_; | |
| 184 std::set<int> doubles_; | |
| 185 std::set<int> references_; | |
| 186 std::deque<FrameStateDescriptor*> deoptimization_entries_; | |
| 187 }; | |
| 188 | |
| 189 base::RandomNumberGenerator rng_; | |
| 190 }; | |
| 191 | |
| 192 | |
| 193 template <typename T> | |
| 194 class InstructionSelectorTestWithParam | |
| 195 : public InstructionSelectorTest, | |
| 196 public ::testing::WithParamInterface<T> {}; | |
| 197 | |
| 198 } // namespace compiler | |
| 199 } // namespace internal | |
| 200 } // namespace v8 | |
| 201 | |
| 202 #endif // V8_COMPILER_UNITTESTS_INSTRUCTION_SELECTOR_UNITTEST_H_ | |
| OLD | NEW |