typed-optimization-unittest.cc 3.93 KB
Newer Older
1 2 3 4 5
// Copyright 2016 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/typed-optimization.h"
6
#include "src/codegen/code-factory.h"
7
#include "src/compiler/access-builder.h"
8
#include "src/compiler/compilation-dependencies.h"
9 10 11 12 13
#include "src/compiler/js-graph.h"
#include "src/compiler/js-operator.h"
#include "src/compiler/machine-operator.h"
#include "src/compiler/node-properties.h"
#include "src/compiler/operator-properties.h"
14
#include "src/execution/isolate-inl.h"
15 16 17 18 19 20 21 22 23 24
#include "test/unittests/compiler/compiler-test-utils.h"
#include "test/unittests/compiler/graph-unittest.h"
#include "test/unittests/compiler/node-test-utils.h"
#include "testing/gmock-support.h"

using testing::IsNaN;

namespace v8 {
namespace internal {
namespace compiler {
25
namespace typed_optimization_unittest {
26 27 28 29

class TypedOptimizationTest : public TypedGraphTest {
 public:
  TypedOptimizationTest()
30
      : TypedGraphTest(3), simplified_(zone()), deps_(broker(), zone()) {}
31
  ~TypedOptimizationTest() override = default;
32 33 34 35

 protected:
  Reduction Reduce(Node* node) {
    MachineOperatorBuilder machine(zone());
36 37
    JSOperatorBuilder javascript(zone());
    JSGraph jsgraph(isolate(), graph(), common(), &javascript, simplified(),
38 39
                    &machine);
    // TODO(titzer): mock the GraphReducer here for better unit testing.
40
    GraphReducer graph_reducer(zone(), graph(), tick_counter());
41
    TypedOptimization reducer(&graph_reducer, &deps_, &jsgraph, broker());
42 43 44
    return reducer.Reduce(node);
  }

45
  SimplifiedOperatorBuilder* simplified() { return &simplified_; }
46 47

 private:
48
  SimplifiedOperatorBuilder simplified_;
49 50 51
  CompilationDependencies deps_;
};

52 53 54 55 56
// -----------------------------------------------------------------------------
// ToBoolean

TEST_F(TypedOptimizationTest, ToBooleanWithBoolean) {
  Node* input = Parameter(Type::Boolean(), 0);
57
  Reduction r = Reduce(graph()->NewNode(simplified()->ToBoolean(), input));
58 59 60 61 62 63
  ASSERT_TRUE(r.Changed());
  EXPECT_EQ(input, r.replacement());
}

TEST_F(TypedOptimizationTest, ToBooleanWithOrderedNumber) {
  Node* input = Parameter(Type::OrderedNumber(), 0);
64
  Reduction r = Reduce(graph()->NewNode(simplified()->ToBoolean(), input));
65 66 67 68 69 70 71
  ASSERT_TRUE(r.Changed());
  EXPECT_THAT(r.replacement(),
              IsBooleanNot(IsNumberEqual(input, IsNumberConstant(0.0))));
}

TEST_F(TypedOptimizationTest, ToBooleanWithNumber) {
  Node* input = Parameter(Type::Number(), 0);
72
  Reduction r = Reduce(graph()->NewNode(simplified()->ToBoolean(), input));
73 74 75 76 77 78
  ASSERT_TRUE(r.Changed());
  EXPECT_THAT(r.replacement(), IsNumberToBoolean(input));
}

TEST_F(TypedOptimizationTest, ToBooleanWithDetectableReceiverOrNull) {
  Node* input = Parameter(Type::DetectableReceiverOrNull(), 0);
79
  Reduction r = Reduce(graph()->NewNode(simplified()->ToBoolean(), input));
80 81 82 83 84 85 86
  ASSERT_TRUE(r.Changed());
  EXPECT_THAT(r.replacement(),
              IsBooleanNot(IsReferenceEqual(input, IsNullConstant())));
}

TEST_F(TypedOptimizationTest, ToBooleanWithReceiverOrNullOrUndefined) {
  Node* input = Parameter(Type::ReceiverOrNullOrUndefined(), 0);
87
  Reduction r = Reduce(graph()->NewNode(simplified()->ToBoolean(), input));
88 89 90 91 92 93
  ASSERT_TRUE(r.Changed());
  EXPECT_THAT(r.replacement(), IsBooleanNot(IsObjectIsUndetectable(input)));
}

TEST_F(TypedOptimizationTest, ToBooleanWithString) {
  Node* input = Parameter(Type::String(), 0);
94
  Reduction r = Reduce(graph()->NewNode(simplified()->ToBoolean(), input));
95 96 97 98 99 100 101 102
  ASSERT_TRUE(r.Changed());
  EXPECT_THAT(r.replacement(),
              IsBooleanNot(IsReferenceEqual(
                  input, IsHeapConstant(factory()->empty_string()))));
}

TEST_F(TypedOptimizationTest, ToBooleanWithAny) {
  Node* input = Parameter(Type::Any(), 0);
103
  Reduction r = Reduce(graph()->NewNode(simplified()->ToBoolean(), input));
104 105 106
  ASSERT_FALSE(r.Changed());
}

107
}  // namespace typed_optimization_unittest
108 109 110
}  // namespace compiler
}  // namespace internal
}  // namespace v8