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