decompression-optimizer-unittest.cc 19.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Copyright 2019 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.

#include "src/compiler/decompression-optimizer.h"

#include "test/unittests/compiler/graph-unittest.h"

namespace v8 {
namespace internal {
namespace compiler {

class DecompressionOptimizerTest : public GraphTest {
 public:
  DecompressionOptimizerTest()
      : GraphTest(),
        machine_(zone(), MachineType::PointerRepresentation(),
                 MachineOperatorBuilder::kNoFlags) {}
  ~DecompressionOptimizerTest() override = default;

 protected:
  void Reduce() {
23 24
    DecompressionOptimizer decompression_optimizer(zone(), graph(), common(),
                                                   machine());
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    decompression_optimizer.Reduce();
  }

  MachineRepresentation CompressedMachRep(MachineRepresentation mach_rep) {
    if (mach_rep == MachineRepresentation::kTagged) {
      return MachineRepresentation::kCompressed;
    } else {
      DCHECK_EQ(mach_rep, MachineRepresentation::kTaggedPointer);
      return MachineRepresentation::kCompressedPointer;
    }
  }

  MachineRepresentation CompressedMachRep(MachineType type) {
    return CompressedMachRep(type.representation());
  }

  MachineRepresentation LoadMachRep(Node* node) {
    return LoadRepresentationOf(node->op()).representation();
  }
  StoreRepresentation CreateStoreRep(MachineType type) {
    return StoreRepresentation(type.representation(),
                               WriteBarrierKind::kFullWriteBarrier);
  }

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  const MachineType types[2] = {MachineType::AnyTagged(),
                                MachineType::TaggedPointer()};

  const Handle<HeapNumber> heap_constants[15] = {
      factory()->NewHeapNumber(0.0),
      factory()->NewHeapNumber(-0.0),
      factory()->NewHeapNumber(11.2),
      factory()->NewHeapNumber(-11.2),
      factory()->NewHeapNumber(3.1415 + 1.4142),
      factory()->NewHeapNumber(3.1415 - 1.4142),
      factory()->NewHeapNumber(0x0000000000000000),
      factory()->NewHeapNumber(0x0000000000000001),
      factory()->NewHeapNumber(0x0000FFFFFFFF0000),
      factory()->NewHeapNumber(0x7FFFFFFFFFFFFFFF),
      factory()->NewHeapNumber(0x8000000000000000),
      factory()->NewHeapNumber(0x8000000000000001),
      factory()->NewHeapNumber(0x8000FFFFFFFF0000),
      factory()->NewHeapNumber(0x8FFFFFFFFFFFFFFF),
      factory()->NewHeapNumber(0xFFFFFFFFFFFFFFFF)};

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  MachineOperatorBuilder* machine() { return &machine_; }

 private:
  MachineOperatorBuilder machine_;
};

// -----------------------------------------------------------------------------
// Direct Load into Store.

TEST_F(DecompressionOptimizerTest, DirectLoadStore) {
  // Define variables.
  Node* const control = graph()->start();
  Node* object = Parameter(Type::Any(), 0);
  Node* effect = graph()->start();
  Node* index = Parameter(Type::UnsignedSmall(), 1);

  // Test for both AnyTagged and TaggedPointer.
  for (size_t i = 0; i < arraysize(types); ++i) {
    // Create the graph.
    Node* base_pointer = graph()->NewNode(machine()->Load(types[i]), object,
                                          index, effect, control);
    Node* value = graph()->NewNode(machine()->Load(types[i]), base_pointer,
                                   index, effect, control);
    graph()->SetEnd(graph()->NewNode(machine()->Store(CreateStoreRep(types[i])),
                                     object, index, value, effect, control));

95
    // Change the nodes, and test the change.
96 97 98 99 100 101
    Reduce();
    EXPECT_EQ(LoadMachRep(base_pointer), types[i].representation());
    EXPECT_EQ(LoadMachRep(value), CompressedMachRep(types[i]));
  }
}

102
// -----------------------------------------------------------------------------
103
// Word32 Operations.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

TEST_F(DecompressionOptimizerTest, Word32EqualTwoDecompresses) {
  // Define variables.
  Node* const control = graph()->start();
  Node* object = Parameter(Type::Any(), 0);
  Node* effect = graph()->start();
  Node* index = Parameter(Type::UnsignedSmall(), 1);

  // Test for both AnyTagged and TaggedPointer, for both loads.
  for (size_t i = 0; i < arraysize(types); ++i) {
    for (size_t j = 0; j < arraysize(types); ++j) {
      // Create the graph.
      Node* load_1 = graph()->NewNode(machine()->Load(types[i]), object, index,
                                      effect, control);
      Node* load_2 = graph()->NewNode(machine()->Load(types[j]), object, index,
                                      effect, control);
120
      Node* equal = graph()->NewNode(machine()->Word32Equal(), load_1, load_2);
121
      graph()->SetEnd(equal);
122 123 124 125 126 127 128 129 130

      // Change the nodes, and test the change.
      Reduce();
      EXPECT_EQ(LoadMachRep(load_1), CompressedMachRep(types[i]));
      EXPECT_EQ(LoadMachRep(load_2), CompressedMachRep(types[j]));
    }
  }
}

131
TEST_F(DecompressionOptimizerTest, Word32EqualDecompressAndConstant) {
132 133 134 135 136 137
  // Define variables.
  Node* const control = graph()->start();
  Node* object = Parameter(Type::Any(), 0);
  Node* effect = graph()->start();
  Node* index = Parameter(Type::UnsignedSmall(), 1);

138
  // Test for both AnyTagged and TaggedPointer.
139 140 141 142 143 144
  for (size_t i = 0; i < arraysize(types); ++i) {
    for (size_t j = 0; j < arraysize(heap_constants); ++j) {
      // Create the graph.
      Node* load = graph()->NewNode(machine()->Load(types[i]), object, index,
                                    effect, control);
      Node* constant =
145
          graph()->NewNode(common()->HeapConstant(heap_constants[j]));
146
      Node* equal = graph()->NewNode(machine()->Word32Equal(), load, constant);
147
      graph()->SetEnd(equal);
148 149 150 151

      // Change the nodes, and test the change.
      Reduce();
      EXPECT_EQ(LoadMachRep(load), CompressedMachRep(types[i]));
152
      EXPECT_EQ(constant->opcode(), IrOpcode::kCompressedHeapConstant);
153 154 155 156
    }
  }
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
TEST_F(DecompressionOptimizerTest, Word32AndSmiCheck) {
  // Define variables.
  Node* const control = graph()->start();
  Node* object = Parameter(Type::Any(), 0);
  Node* effect = graph()->start();
  Node* index = Parameter(Type::UnsignedSmall(), 1);

  // Test for both AnyTagged and TaggedPointer.
  for (size_t i = 0; i < arraysize(types); ++i) {
    // Create the graph.
    Node* load = graph()->NewNode(machine()->Load(types[i]), object, index,
                                  effect, control);
    Node* smi_tag_mask = graph()->NewNode(common()->Int32Constant(kSmiTagMask));
    Node* word32_and =
        graph()->NewNode(machine()->Word32And(), load, smi_tag_mask);
    Node* smi_tag = graph()->NewNode(common()->Int32Constant(kSmiTag));
    graph()->SetEnd(
        graph()->NewNode(machine()->Word32Equal(), word32_and, smi_tag));
    // Change the nodes, and test the change.
    Reduce();
    EXPECT_EQ(LoadMachRep(load), CompressedMachRep(types[i]));
  }
}

181 182 183 184 185 186 187 188 189 190 191 192 193
TEST_F(DecompressionOptimizerTest, Word32ShlSmiTag) {
  // Define variables.
  Node* const control = graph()->start();
  Node* object = Parameter(Type::Any(), 0);
  Node* effect = graph()->start();
  Node* index = Parameter(Type::UnsignedSmall(), 1);

  // Test only for AnyTagged, since TaggedPointer can't be Smi tagged.
  // Create the graph.
  Node* load = graph()->NewNode(machine()->Load(MachineType::AnyTagged()),
                                object, index, effect, control);
  Node* smi_shift_bits =
      graph()->NewNode(common()->Int32Constant(kSmiShiftSize + kSmiTagSize));
194
  Node* word32_shl =
195
      graph()->NewNode(machine()->Word32Shl(), load, smi_shift_bits);
196
  graph()->SetEnd(
197
      graph()->NewNode(machine()->BitcastWord32ToWord64(), word32_shl));
198 199 200 201 202
  // Change the nodes, and test the change.
  Reduce();
  EXPECT_EQ(LoadMachRep(load), CompressedMachRep(MachineType::AnyTagged()));
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
TEST_F(DecompressionOptimizerTest, Word32SarSmiUntag) {
  // Define variables.
  Node* const control = graph()->start();
  Node* object = Parameter(Type::Any(), 0);
  Node* effect = graph()->start();
  Node* index = Parameter(Type::UnsignedSmall(), 1);

  // Test only for AnyTagged, since TaggedPointer can't be Smi tagged.
  // Create the graph.
  Node* load = graph()->NewNode(machine()->Load(MachineType::AnyTagged()),
                                object, index, effect, control);
  Node* truncation = graph()->NewNode(machine()->TruncateInt64ToInt32(), load);
  Node* smi_shift_bits =
      graph()->NewNode(common()->Int32Constant(kSmiShiftSize + kSmiTagSize));
  Node* word32_sar =
      graph()->NewNode(machine()->Word32Sar(), truncation, smi_shift_bits);
  graph()->SetEnd(
      graph()->NewNode(machine()->ChangeInt32ToInt64(), word32_sar));
  // Change the nodes, and test the change.
  Reduce();
  EXPECT_EQ(LoadMachRep(load), CompressedMachRep(MachineType::AnyTagged()));
}

226 227 228 229 230 231 232 233 234 235 236
// -----------------------------------------------------------------------------
// FrameState and TypedStateValues interaction.

TEST_F(DecompressionOptimizerTest, TypedStateValues) {
  // Define variables.
  Node* const control = graph()->start();
  Node* object = Parameter(Type::Any(), 0);
  Node* effect = graph()->start();
  Node* index = Parameter(Type::UnsignedSmall(), 1);
  const int number_of_inputs = 2;
  const ZoneVector<MachineType>* types_for_state_values =
237 238
      graph()->zone()->New<ZoneVector<MachineType>>(number_of_inputs,
                                                    graph()->zone());
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  SparseInputMask dense = SparseInputMask::Dense();

  // Test for both AnyTagged and TaggedPointer.
  for (size_t i = 0; i < arraysize(types); ++i) {
    for (size_t j = 0; j < arraysize(heap_constants); ++j) {
      // Create the graph.
      Node* load = graph()->NewNode(machine()->Load(types[i]), object, index,
                                    effect, control);
      Node* constant_1 =
          graph()->NewNode(common()->HeapConstant(heap_constants[j]));
      Node* typed_state_values = graph()->NewNode(
          common()->TypedStateValues(types_for_state_values, dense), load,
          constant_1);
      Node* constant_2 =
          graph()->NewNode(common()->HeapConstant(heap_constants[j]));
      graph()->SetEnd(graph()->NewNode(
          common()->FrameState(BailoutId::None(),
                               OutputFrameStateCombine::Ignore(), nullptr),
          typed_state_values, typed_state_values, typed_state_values,
          constant_2, UndefinedConstant(), graph()->start()));

      // Change the nodes, and test the change.
      Reduce();
      EXPECT_EQ(LoadMachRep(load), CompressedMachRep(types[i]));
      EXPECT_EQ(constant_1->opcode(), IrOpcode::kCompressedHeapConstant);
      EXPECT_EQ(constant_2->opcode(), IrOpcode::kCompressedHeapConstant);
    }
  }
}

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
// -----------------------------------------------------------------------------
// Phi

TEST_F(DecompressionOptimizerTest, PhiDecompressOrNot) {
  // Define variables.
  Node* const control = graph()->start();
  Node* object = Parameter(Type::Any(), 0);
  Node* effect = graph()->start();
  Node* index = Parameter(Type::UnsignedSmall(), 1);
  const int number_of_inputs = 2;

  // Test for both AnyTagged and TaggedPointer.
  for (size_t i = 0; i < arraysize(types); ++i) {
    for (size_t j = 0; j < arraysize(heap_constants); ++j) {
      // Create the graph.
      // Base pointer
      Node* load_1 = graph()->NewNode(machine()->Load(types[i]), object, index,
                                      effect, control);
      Node* constant_1 =
          graph()->NewNode(common()->HeapConstant(heap_constants[j]));
      Node* phi_1 = graph()->NewNode(
          common()->Phi(types[i].representation(), number_of_inputs), load_1,
          constant_1, control);

      // Value
      Node* load_2 = graph()->NewNode(machine()->Load(types[i]), object, index,
                                      effect, control);
      Node* constant_2 =
          graph()->NewNode(common()->HeapConstant(heap_constants[j]));
      Node* phi_2 = graph()->NewNode(
          common()->Phi(types[i].representation(), number_of_inputs), load_2,
          constant_2, control);

      graph()->SetEnd(
          graph()->NewNode(machine()->Store(CreateStoreRep(types[i])), phi_1,
                           index, phi_2, effect, control));

      // Change the nodes, and test the change.
      Reduce();
      // Base pointer should not be compressed.
      EXPECT_EQ(LoadMachRep(load_1), types[i].representation());
      EXPECT_EQ(constant_1->opcode(), IrOpcode::kHeapConstant);
      EXPECT_EQ(PhiRepresentationOf(phi_1->op()), types[i].representation());
      // Value should be compressed.
      EXPECT_EQ(LoadMachRep(load_2), CompressedMachRep(types[i]));
      EXPECT_EQ(constant_2->opcode(), IrOpcode::kCompressedHeapConstant);
      EXPECT_EQ(PhiRepresentationOf(phi_2->op()), CompressedMachRep(types[i]));
    }
  }
}

TEST_F(DecompressionOptimizerTest, CascadingPhi) {
  // Define variables.
  Node* const control = graph()->start();
  Node* object = Parameter(Type::Any(), 0);
  Node* effect = graph()->start();
  Node* index = Parameter(Type::UnsignedSmall(), 1);
  const int number_of_inputs = 2;

  // Test for both AnyTagged and TaggedPointer.
  for (size_t i = 0; i < arraysize(types); ++i) {
    // Create the graph.
    Node* load_1 = graph()->NewNode(machine()->Load(types[i]), object, index,
                                    effect, control);
    Node* load_2 = graph()->NewNode(machine()->Load(types[i]), object, index,
                                    effect, control);
    Node* load_3 = graph()->NewNode(machine()->Load(types[i]), object, index,
                                    effect, control);
    Node* load_4 = graph()->NewNode(machine()->Load(types[i]), object, index,
                                    effect, control);

    Node* phi_1 = graph()->NewNode(
        common()->Phi(types[i].representation(), number_of_inputs), load_1,
        load_2, control);
    Node* phi_2 = graph()->NewNode(
        common()->Phi(types[i].representation(), number_of_inputs), load_3,
        load_4, control);

    Node* final_phi = graph()->NewNode(
        common()->Phi(types[i].representation(), number_of_inputs), phi_1,
        phi_2, control);

    // Value
    graph()->SetEnd(final_phi);
    // Change the nodes, and test the change.
    Reduce();
    // Loads are all compressed
    EXPECT_EQ(LoadMachRep(load_1), CompressedMachRep(types[i]));
    EXPECT_EQ(LoadMachRep(load_2), CompressedMachRep(types[i]));
    EXPECT_EQ(LoadMachRep(load_3), CompressedMachRep(types[i]));
    EXPECT_EQ(LoadMachRep(load_4), CompressedMachRep(types[i]));
    // Phis too
    EXPECT_EQ(PhiRepresentationOf(phi_1->op()), CompressedMachRep(types[i]));
    EXPECT_EQ(PhiRepresentationOf(phi_2->op()), CompressedMachRep(types[i]));
    EXPECT_EQ(PhiRepresentationOf(final_phi->op()),
              CompressedMachRep(types[i]));
  }
}

TEST_F(DecompressionOptimizerTest, PhiWithOneCompressedAndOneTagged) {
  // Define variables.
  Node* const control = graph()->start();
  Node* object = Parameter(Type::Any(), 0);
  Node* effect = graph()->start();
  Node* index = Parameter(Type::UnsignedSmall(), 1);
  const int number_of_inputs = 2;

  // Test for both AnyTagged and TaggedPointer.
  for (size_t i = 0; i < arraysize(types); ++i) {
    for (size_t j = 0; j < arraysize(heap_constants); ++j) {
      // Create the graph.
      // Base pointer in load_2, and phi input for value
      Node* load_1 = graph()->NewNode(machine()->Load(types[i]), object, index,
                                      effect, control);

      // load_2 blocks load_1 from being compressed.
      Node* load_2 = graph()->NewNode(machine()->Load(types[i]), load_1, index,
                                      effect, control);
      Node* phi = graph()->NewNode(
          common()->Phi(types[i].representation(), number_of_inputs), load_1,
          load_2, control);

      graph()->SetEnd(
          graph()->NewNode(machine()->Store(CreateStoreRep(types[i])), object,
                           index, phi, effect, control));

      // Change the nodes, and test the change.
      Reduce();
      EXPECT_EQ(LoadMachRep(load_1), types[i].representation());
      EXPECT_EQ(LoadMachRep(load_2), CompressedMachRep(types[i]));
      EXPECT_EQ(PhiRepresentationOf(phi->op()), CompressedMachRep(types[i]));
    }
  }
}

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
// -----------------------------------------------------------------------------
// Int cases.

TEST_F(DecompressionOptimizerTest, Int32LessThanOrEqualFromSpeculative) {
  // This case tests for what SpeculativeNumberLessThanOrEqual is lowered to.
  // Define variables.
  Node* const control = graph()->start();
  Node* object = Parameter(Type::Any(), 0);
  Node* effect = graph()->start();
  Node* index = Parameter(Type::UnsignedSmall(), 1);

  // Test only for AnyTagged, since TaggedPointer can't be a Smi.
  // Create the graph.
  Node* load = graph()->NewNode(machine()->Load(MachineType::AnyTagged()),
                                object, index, effect, control);
  Node* constant = graph()->NewNode(common()->Int64Constant(5));
  graph()->SetEnd(
      graph()->NewNode(machine()->Int32LessThanOrEqual(), load, constant));
  // Change the nodes, and test the change.
  Reduce();
  EXPECT_EQ(LoadMachRep(load), CompressedMachRep(MachineType::AnyTagged()));
}

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
// -----------------------------------------------------------------------------
// Bitcast cases.

TEST_F(DecompressionOptimizerTest, BitcastTaggedToWord) {
  // Define variables.
  Node* const control = graph()->start();
  Node* object = Parameter(Type::Any(), 0);
  Node* effect = graph()->start();
  Node* index = Parameter(Type::UnsignedSmall(), 1);

  // Test for both AnyTagged and TaggedPointer, for both loads.
  for (size_t i = 0; i < arraysize(types); ++i) {
    for (size_t j = 0; j < arraysize(types); ++j) {
      // Create the graph.
      Node* load_1 = graph()->NewNode(machine()->Load(types[i]), object, index,
                                      effect, control);
      Node* bitcast_1 = graph()->NewNode(machine()->BitcastTaggedToWord(),
                                         load_1, effect, control);
      Node* load_2 = graph()->NewNode(machine()->Load(types[j]), object, index,
                                      effect, control);
      Node* bitcast_2 = graph()->NewNode(machine()->BitcastTaggedToWord(),
                                         load_2, effect, control);
      Node* equal =
          graph()->NewNode(machine()->Word32Equal(), bitcast_1, bitcast_2);
      graph()->SetEnd(equal);

      // Change the nodes, and test the change.
      Reduce();
      EXPECT_EQ(LoadMachRep(load_1), CompressedMachRep(types[i]));
      EXPECT_EQ(LoadMachRep(load_2), CompressedMachRep(types[j]));
    }
  }
}

TEST_F(DecompressionOptimizerTest, BitcastTaggedToWordForTagAndSmiBits) {
  // Define variables.
  Node* const control = graph()->start();
  Node* object = Parameter(Type::Any(), 0);
  Node* effect = graph()->start();
  Node* index = Parameter(Type::UnsignedSmall(), 1);

  // Test for both AnyTagged and TaggedPointer, for both loads.
  for (size_t i = 0; i < arraysize(types); ++i) {
    for (size_t j = 0; j < arraysize(types); ++j) {
      // Create the graph.
      Node* load_1 = graph()->NewNode(machine()->Load(types[i]), object, index,
                                      effect, control);
      Node* bitcast_1 = graph()->NewNode(
          machine()->BitcastTaggedToWordForTagAndSmiBits(), load_1);
      Node* load_2 = graph()->NewNode(machine()->Load(types[j]), object, index,
                                      effect, control);
      Node* bitcast_2 = graph()->NewNode(
          machine()->BitcastTaggedToWordForTagAndSmiBits(), load_2);
      Node* equal =
          graph()->NewNode(machine()->Word32Equal(), bitcast_1, bitcast_2);
      graph()->SetEnd(equal);

      // Change the nodes, and test the change.
      Reduce();
      EXPECT_EQ(LoadMachRep(load_1), CompressedMachRep(types[i]));
      EXPECT_EQ(LoadMachRep(load_2), CompressedMachRep(types[j]));
    }
  }
}

492 493 494
}  // namespace compiler
}  // namespace internal
}  // namespace v8