node-cache-unittest.cc 4.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

#include "src/compiler/node-cache.h"
#include "test/unittests/compiler/graph-unittest.h"
#include "test/unittests/test-utils.h"
#include "testing/gmock-support.h"

using testing::Contains;

namespace v8 {
namespace internal {
namespace compiler {
15
namespace node_cache_unittest {
16

17
using NodeCacheTest = GraphTest;
18 19

TEST_F(NodeCacheTest, Int32Constant_back_to_back) {
20
  Int32NodeCache cache(zone());
21 22

  for (int i = -2000000000; i < 2000000000; i += 3315177) {
23
    Node** pos = cache.Find(i);
24 25
    ASSERT_TRUE(pos != nullptr);
    for (int j = 0; j < 3; j++) {
26
      Node** npos = cache.Find(i);
27 28 29 30 31 32 33
      EXPECT_EQ(pos, npos);
    }
  }
}


TEST_F(NodeCacheTest, Int32Constant_five) {
34
  Int32NodeCache cache(zone());
35 36 37 38 39 40
  int32_t constants[] = {static_cast<int32_t>(0x80000000), -77, 0, 1, -1};
  Node* nodes[arraysize(constants)];

  for (size_t i = 0; i < arraysize(constants); i++) {
    int32_t k = constants[i];
    Node* node = graph()->NewNode(common()->Int32Constant(k));
41
    *cache.Find(k) = nodes[i] = node;
42 43 44 45
  }

  for (size_t i = 0; i < arraysize(constants); i++) {
    int32_t k = constants[i];
46
    EXPECT_EQ(nodes[i], *cache.Find(k));
47 48 49 50 51
  }
}


TEST_F(NodeCacheTest, Int32Constant_hits) {
52
  Int32NodeCache cache(zone());
53 54 55 56 57 58
  const int32_t kSize = 1500;
  Node** nodes = zone()->NewArray<Node*>(kSize);

  for (int i = 0; i < kSize; i++) {
    int32_t v = i * -55;
    nodes[i] = graph()->NewNode(common()->Int32Constant(v));
59
    *cache.Find(v) = nodes[i];
60 61 62 63 64
  }

  int hits = 0;
  for (int i = 0; i < kSize; i++) {
    int32_t v = i * -55;
65
    Node** pos = cache.Find(v);
66
    if (*pos != nullptr) {
67 68 69 70 71 72 73 74 75
      EXPECT_EQ(nodes[i], *pos);
      hits++;
    }
  }
  EXPECT_LT(4, hits);
}


TEST_F(NodeCacheTest, Int64Constant_back_to_back) {
76
  Int64NodeCache cache(zone());
77 78

  for (int64_t i = -2000000000; i < 2000000000; i += 3315177) {
79
    Node** pos = cache.Find(i);
80 81
    ASSERT_TRUE(pos != nullptr);
    for (int j = 0; j < 3; j++) {
82
      Node** npos = cache.Find(i);
83 84 85 86 87 88 89
      EXPECT_EQ(pos, npos);
    }
  }
}


TEST_F(NodeCacheTest, Int64Constant_hits) {
90
  Int64NodeCache cache(zone());
91 92 93 94 95 96
  const int32_t kSize = 1500;
  Node** nodes = zone()->NewArray<Node*>(kSize);

  for (int i = 0; i < kSize; i++) {
    int64_t v = static_cast<int64_t>(i) * static_cast<int64_t>(5003001);
    nodes[i] = graph()->NewNode(common()->Int32Constant(i));
97
    *cache.Find(v) = nodes[i];
98 99 100 101 102
  }

  int hits = 0;
  for (int i = 0; i < kSize; i++) {
    int64_t v = static_cast<int64_t>(i) * static_cast<int64_t>(5003001);
103
    Node** pos = cache.Find(v);
104
    if (*pos != nullptr) {
105 106 107 108 109 110 111 112 113
      EXPECT_EQ(nodes[i], *pos);
      hits++;
    }
  }
  EXPECT_LT(4, hits);
}


TEST_F(NodeCacheTest, GetCachedNodes_int32) {
114
  Int32NodeCache cache(zone());
115 116 117 118 119
  int32_t constants[] = {0, 311, 12,  13,  14,  555, -555, -44, -33, -22, -11,
                         0, 311, 311, 412, 412, 11,  11,   -33, -33, -22, -11};

  for (size_t i = 0; i < arraysize(constants); i++) {
    int32_t k = constants[i];
120
    Node** pos = cache.Find(k);
121
    if (*pos != nullptr) {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
      ZoneVector<Node*> nodes(zone());
      cache.GetCachedNodes(&nodes);
      EXPECT_THAT(nodes, Contains(*pos));
    } else {
      ZoneVector<Node*> nodes(zone());
      Node* n = graph()->NewNode(common()->Int32Constant(k));
      *pos = n;
      cache.GetCachedNodes(&nodes);
      EXPECT_THAT(nodes, Contains(n));
    }
  }
}


TEST_F(NodeCacheTest, GetCachedNodes_int64) {
137
  Int64NodeCache cache(zone());
138 139 140 141 142
  int64_t constants[] = {0, 311, 12,  13,  14,  555, -555, -44, -33, -22, -11,
                         0, 311, 311, 412, 412, 11,  11,   -33, -33, -22, -11};

  for (size_t i = 0; i < arraysize(constants); i++) {
    int64_t k = constants[i];
143
    Node** pos = cache.Find(k);
144
    if (*pos != nullptr) {
145 146 147 148 149 150 151 152 153 154 155 156 157
      ZoneVector<Node*> nodes(zone());
      cache.GetCachedNodes(&nodes);
      EXPECT_THAT(nodes, Contains(*pos));
    } else {
      ZoneVector<Node*> nodes(zone());
      Node* n = graph()->NewNode(common()->Int64Constant(k));
      *pos = n;
      cache.GetCachedNodes(&nodes);
      EXPECT_THAT(nodes, Contains(n));
    }
  }
}

158
}  // namespace node_cache_unittest
159 160 161
}  // namespace compiler
}  // namespace internal
}  // namespace v8