instruction-selector-unittest.h 11.2 KB
Newer Older
1 2 3 4
// 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.

5 6
#ifndef V8_UNITTESTS_COMPILER_INSTRUCTION_SELECTOR_UNITTEST_H_
#define V8_UNITTESTS_COMPILER_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 "src/macro-assembler.h"
15
#include "test/unittests/test-utils.h"
16 17 18 19 20

namespace v8 {
namespace internal {
namespace compiler {

21 22
class InstructionSelectorTest : public TestWithContext,
                                public TestWithIsolateAndZone {
23
 public:
24
  InstructionSelectorTest();
25
  ~InstructionSelectorTest() override;
26

27 28
  base::RandomNumberGenerator* rng() { return &rng_; }

29 30
  class Stream;

31 32 33 34 35
  enum StreamBuilderMode {
    kAllInstructions,
    kTargetInstructions,
    kAllExceptNopInstructions
  };
36

37
  class StreamBuilder final : public RawMachineAssembler {
38
   public:
39
    StreamBuilder(InstructionSelectorTest* test, MachineType return_type)
40 41 42 43 44
        : RawMachineAssembler(test->isolate(),
                              new (test->zone()) Graph(test->zone()),
                              MakeCallDescriptor(test->zone(), return_type),
                              MachineType::PointerRepresentation(),
                              MachineOperatorBuilder::kAllOptionalOps),
45
          test_(test) {}
46 47
    StreamBuilder(InstructionSelectorTest* test, MachineType return_type,
                  MachineType parameter0_type)
48
        : RawMachineAssembler(
49
              test->isolate(), new (test->zone()) Graph(test->zone()),
50
              MakeCallDescriptor(test->zone(), return_type, parameter0_type),
51
              MachineType::PointerRepresentation(),
52 53
              MachineOperatorBuilder::kAllOptionalOps,
              InstructionSelector::AlignmentRequirements()),
54
          test_(test) {}
55 56
    StreamBuilder(InstructionSelectorTest* test, MachineType return_type,
                  MachineType parameter0_type, MachineType parameter1_type)
57
        : RawMachineAssembler(
58
              test->isolate(), new (test->zone()) Graph(test->zone()),
59 60
              MakeCallDescriptor(test->zone(), return_type, parameter0_type,
                                 parameter1_type),
61 62
              MachineType::PointerRepresentation(),
              MachineOperatorBuilder::kAllOptionalOps),
63
          test_(test) {}
64 65 66 67
    StreamBuilder(InstructionSelectorTest* test, MachineType return_type,
                  MachineType parameter0_type, MachineType parameter1_type,
                  MachineType parameter2_type)
        : RawMachineAssembler(
68
              test->isolate(), new (test->zone()) Graph(test->zone()),
69 70
              MakeCallDescriptor(test->zone(), return_type, parameter0_type,
                                 parameter1_type, parameter2_type),
71 72
              MachineType::PointerRepresentation(),
              MachineOperatorBuilder::kAllOptionalOps),
73
          test_(test) {}
74 75 76 77 78 79 80 81 82 83 84

    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,
85 86 87
                 StreamBuilderMode mode = kTargetInstructions,
                 InstructionSelector::SourcePositionMode source_position_mode =
                     InstructionSelector::kAllSourcePositions);
88

89 90 91
    const FrameStateFunctionInfo* GetFrameStateFunctionInfo(int parameter_count,
                                                            int local_count);

92
   private:
93
    CallDescriptor* MakeCallDescriptor(Zone* zone, MachineType return_type) {
94 95
      MachineSignature::Builder builder(zone, 1, 0);
      builder.AddReturn(return_type);
96
      return MakeSimpleCallDescriptor(zone, builder.Build());
97 98
    }

99 100
    CallDescriptor* MakeCallDescriptor(Zone* zone, MachineType return_type,
                                       MachineType parameter0_type) {
101 102 103
      MachineSignature::Builder builder(zone, 1, 1);
      builder.AddReturn(return_type);
      builder.AddParam(parameter0_type);
104
      return MakeSimpleCallDescriptor(zone, builder.Build());
105 106
    }

107 108 109
    CallDescriptor* MakeCallDescriptor(Zone* zone, MachineType return_type,
                                       MachineType parameter0_type,
                                       MachineType parameter1_type) {
110 111 112 113
      MachineSignature::Builder builder(zone, 1, 2);
      builder.AddReturn(return_type);
      builder.AddParam(parameter0_type);
      builder.AddParam(parameter1_type);
114
      return MakeSimpleCallDescriptor(zone, builder.Build());
115 116
    }

117 118 119 120
    CallDescriptor* MakeCallDescriptor(Zone* zone, MachineType return_type,
                                       MachineType parameter0_type,
                                       MachineType parameter1_type,
                                       MachineType parameter2_type) {
121 122 123 124 125
      MachineSignature::Builder builder(zone, 1, 3);
      builder.AddReturn(return_type);
      builder.AddParam(parameter0_type);
      builder.AddParam(parameter1_type);
      builder.AddParam(parameter2_type);
126
      return MakeSimpleCallDescriptor(zone, builder.Build());
127 128
    }

129 130
   private:
    InstructionSelectorTest* test_;
131 132 133 134 135 136 137 138 139 140

    // 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++) {
141 142
        locations.AddReturn(
            LinkageLocation::ForCallerFrameSlot(-1 - i, msig->GetReturn(i)));
143 144 145 146 147
      }

      // 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++) {
148 149
        locations.AddParam(
            LinkageLocation::ForCallerFrameSlot(-1 - i, msig->GetParam(i)));
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
      }

      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
          locations.Build(),             // location_sig
          0,                             // stack_parameter_count
          Operator::kNoProperties,       // properties
          kCalleeSaveRegisters,          // callee-saved registers
          kCalleeSaveFPRegisters,        // callee-saved fp regs
166
          CallDescriptor::kCanUseRoots,  // flags
167 168
          "iselect-test-call");
    }
169 170
  };

171
  class Stream final {
172 173 174 175 176 177 178
   public:
    size_t size() const { return instructions_.size(); }
    const Instruction* operator[](size_t index) const {
      EXPECT_LT(index, size());
      return instructions_[index];
    }

179 180 181
    bool IsDouble(const InstructionOperand* operand) const {
      return IsDouble(ToVreg(operand));
    }
182 183

    bool IsDouble(const Node* node) const { return IsDouble(ToVreg(node)); }
184

185 186 187
    bool IsInteger(const InstructionOperand* operand) const {
      return IsInteger(ToVreg(operand));
    }
188 189

    bool IsInteger(const Node* node) const { return IsInteger(ToVreg(node)); }
190 191 192 193

    bool IsReference(const InstructionOperand* operand) const {
      return IsReference(ToVreg(operand));
    }
194 195 196

    bool IsReference(const Node* node) const {
      return IsReference(ToVreg(node));
197 198
    }

199 200 201 202
    float ToFloat32(const InstructionOperand* operand) const {
      return ToConstant(operand).ToFloat32();
    }

203
    double ToFloat64(const InstructionOperand* operand) const {
204
      return ToConstant(operand).ToFloat64().value();
205 206
    }

207 208 209 210
    int32_t ToInt32(const InstructionOperand* operand) const {
      return ToConstant(operand).ToInt32();
    }

211 212 213 214
    int64_t ToInt64(const InstructionOperand* operand) const {
      return ToConstant(operand).ToInt64();
    }

215 216 217 218
    Handle<HeapObject> ToHeapObject(const InstructionOperand* operand) const {
      return ToConstant(operand).ToHeapObject();
    }

219
    int ToVreg(const InstructionOperand* operand) const {
220 221 222
      if (operand->IsConstant()) {
        return ConstantOperand::cast(operand)->virtual_register();
      }
223 224 225 226
      EXPECT_EQ(InstructionOperand::UNALLOCATED, operand->kind());
      return UnallocatedOperand::cast(operand)->virtual_register();
    }

227 228
    int ToVreg(const Node* node) const;

229
    bool IsFixed(const InstructionOperand* operand, Register reg) const;
230
    bool IsSameAsFirst(const InstructionOperand* operand) const;
231 232
    bool IsUsedAtStart(const InstructionOperand* operand) const;

233 234
    FrameStateDescriptor* GetFrameStateDescriptor(int deoptimization_id) {
      EXPECT_LT(deoptimization_id, GetFrameStateDescriptorCount());
235 236 237
      return deoptimization_entries_[deoptimization_id];
    }

238
    int GetFrameStateDescriptorCount() {
239 240 241
      return static_cast<int>(deoptimization_entries_.size());
    }

242
   private:
243 244 245 246 247 248 249 250 251 252 253 254
    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();
    }

255 256 257
    Constant ToConstant(const InstructionOperand* operand) const {
      ConstantMap::const_iterator i;
      if (operand->IsConstant()) {
258 259
        i = constants_.find(ConstantOperand::cast(operand)->virtual_register());
        EXPECT_EQ(ConstantOperand::cast(operand)->virtual_register(), i->first);
ulan@chromium.org's avatar
ulan@chromium.org committed
260
        EXPECT_FALSE(constants_.end() == i);
261 262
      } else {
        EXPECT_EQ(InstructionOperand::IMMEDIATE, operand->kind());
263 264 265 266 267 268
        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);
ulan@chromium.org's avatar
ulan@chromium.org committed
269
        EXPECT_FALSE(immediates_.end() == i);
270 271 272 273 274 275 276
      }
      return i->second;
    }

    friend class StreamBuilder;

    typedef std::map<int, Constant> ConstantMap;
277
    typedef std::map<NodeId, int> VirtualRegisters;
278 279 280 281

    ConstantMap constants_;
    ConstantMap immediates_;
    std::deque<Instruction*> instructions_;
282 283
    std::set<int> doubles_;
    std::set<int> references_;
284
    VirtualRegisters virtual_registers_;
285
    std::deque<FrameStateDescriptor*> deoptimization_entries_;
286
  };
287 288

  base::RandomNumberGenerator rng_;
289 290
};

291 292 293 294 295 296

template <typename T>
class InstructionSelectorTestWithParam
    : public InstructionSelectorTest,
      public ::testing::WithParamInterface<T> {};

297 298 299 300
}  // namespace compiler
}  // namespace internal
}  // namespace v8

301
#endif  // V8_UNITTESTS_COMPILER_INSTRUCTION_SELECTOR_UNITTEST_H_