• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "src/compiler/node-cache.h"
6 #include "test/unittests/compiler/graph-unittest.h"
7 #include "test/unittests/test-utils.h"
8 #include "testing/gmock-support.h"
9 
10 using testing::Contains;
11 
12 namespace v8 {
13 namespace internal {
14 namespace compiler {
15 
16 typedef GraphTest NodeCacheTest;
17 
TEST_F(NodeCacheTest,Int32Constant_back_to_back)18 TEST_F(NodeCacheTest, Int32Constant_back_to_back) {
19   Int32NodeCache cache;
20 
21   for (int i = -2000000000; i < 2000000000; i += 3315177) {
22     Node** pos = cache.Find(zone(), i);
23     ASSERT_TRUE(pos != nullptr);
24     for (int j = 0; j < 3; j++) {
25       Node** npos = cache.Find(zone(), i);
26       EXPECT_EQ(pos, npos);
27     }
28   }
29 }
30 
31 
TEST_F(NodeCacheTest,Int32Constant_five)32 TEST_F(NodeCacheTest, Int32Constant_five) {
33   Int32NodeCache cache;
34   int32_t constants[] = {static_cast<int32_t>(0x80000000), -77, 0, 1, -1};
35   Node* nodes[arraysize(constants)];
36 
37   for (size_t i = 0; i < arraysize(constants); i++) {
38     int32_t k = constants[i];
39     Node* node = graph()->NewNode(common()->Int32Constant(k));
40     *cache.Find(zone(), k) = nodes[i] = node;
41   }
42 
43   for (size_t i = 0; i < arraysize(constants); i++) {
44     int32_t k = constants[i];
45     EXPECT_EQ(nodes[i], *cache.Find(zone(), k));
46   }
47 }
48 
49 
TEST_F(NodeCacheTest,Int32Constant_hits)50 TEST_F(NodeCacheTest, Int32Constant_hits) {
51   Int32NodeCache cache;
52   const int32_t kSize = 1500;
53   Node** nodes = zone()->NewArray<Node*>(kSize);
54 
55   for (int i = 0; i < kSize; i++) {
56     int32_t v = i * -55;
57     nodes[i] = graph()->NewNode(common()->Int32Constant(v));
58     *cache.Find(zone(), v) = nodes[i];
59   }
60 
61   int hits = 0;
62   for (int i = 0; i < kSize; i++) {
63     int32_t v = i * -55;
64     Node** pos = cache.Find(zone(), v);
65     if (*pos != NULL) {
66       EXPECT_EQ(nodes[i], *pos);
67       hits++;
68     }
69   }
70   EXPECT_LT(4, hits);
71 }
72 
73 
TEST_F(NodeCacheTest,Int64Constant_back_to_back)74 TEST_F(NodeCacheTest, Int64Constant_back_to_back) {
75   Int64NodeCache cache;
76 
77   for (int64_t i = -2000000000; i < 2000000000; i += 3315177) {
78     Node** pos = cache.Find(zone(), i);
79     ASSERT_TRUE(pos != nullptr);
80     for (int j = 0; j < 3; j++) {
81       Node** npos = cache.Find(zone(), i);
82       EXPECT_EQ(pos, npos);
83     }
84   }
85 }
86 
87 
TEST_F(NodeCacheTest,Int64Constant_hits)88 TEST_F(NodeCacheTest, Int64Constant_hits) {
89   Int64NodeCache cache;
90   const int32_t kSize = 1500;
91   Node** nodes = zone()->NewArray<Node*>(kSize);
92 
93   for (int i = 0; i < kSize; i++) {
94     int64_t v = static_cast<int64_t>(i) * static_cast<int64_t>(5003001);
95     nodes[i] = graph()->NewNode(common()->Int32Constant(i));
96     *cache.Find(zone(), v) = nodes[i];
97   }
98 
99   int hits = 0;
100   for (int i = 0; i < kSize; i++) {
101     int64_t v = static_cast<int64_t>(i) * static_cast<int64_t>(5003001);
102     Node** pos = cache.Find(zone(), v);
103     if (*pos != NULL) {
104       EXPECT_EQ(nodes[i], *pos);
105       hits++;
106     }
107   }
108   EXPECT_LT(4, hits);
109 }
110 
111 
TEST_F(NodeCacheTest,GetCachedNodes_int32)112 TEST_F(NodeCacheTest, GetCachedNodes_int32) {
113   Int32NodeCache cache;
114   int32_t constants[] = {0, 311, 12,  13,  14,  555, -555, -44, -33, -22, -11,
115                          0, 311, 311, 412, 412, 11,  11,   -33, -33, -22, -11};
116 
117   for (size_t i = 0; i < arraysize(constants); i++) {
118     int32_t k = constants[i];
119     Node** pos = cache.Find(zone(), k);
120     if (*pos != NULL) {
121       ZoneVector<Node*> nodes(zone());
122       cache.GetCachedNodes(&nodes);
123       EXPECT_THAT(nodes, Contains(*pos));
124     } else {
125       ZoneVector<Node*> nodes(zone());
126       Node* n = graph()->NewNode(common()->Int32Constant(k));
127       *pos = n;
128       cache.GetCachedNodes(&nodes);
129       EXPECT_THAT(nodes, Contains(n));
130     }
131   }
132 }
133 
134 
TEST_F(NodeCacheTest,GetCachedNodes_int64)135 TEST_F(NodeCacheTest, GetCachedNodes_int64) {
136   Int64NodeCache cache;
137   int64_t constants[] = {0, 311, 12,  13,  14,  555, -555, -44, -33, -22, -11,
138                          0, 311, 311, 412, 412, 11,  11,   -33, -33, -22, -11};
139 
140   for (size_t i = 0; i < arraysize(constants); i++) {
141     int64_t k = constants[i];
142     Node** pos = cache.Find(zone(), k);
143     if (*pos != NULL) {
144       ZoneVector<Node*> nodes(zone());
145       cache.GetCachedNodes(&nodes);
146       EXPECT_THAT(nodes, Contains(*pos));
147     } else {
148       ZoneVector<Node*> nodes(zone());
149       Node* n = graph()->NewNode(common()->Int64Constant(k));
150       *pos = n;
151       cache.GetCachedNodes(&nodes);
152       EXPECT_THAT(nodes, Contains(n));
153     }
154   }
155 }
156 
157 }  // namespace compiler
158 }  // namespace internal
159 }  // namespace v8
160