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