Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(642)

Side by Side Diff: test/cctest/compiler/test-typer.cc

Issue 904863002: [turbofan] Separate representation type operations from the semantic types. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Fixes Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/cctest/cctest.gyp ('k') | test/cctest/test-types.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 <functional>
6
7 #include "src/codegen.h"
8 #include "src/compiler/js-operator.h"
9 #include "src/compiler/node-properties.h"
10 #include "src/compiler/typer.h"
11 #include "test/cctest/cctest.h"
12 #include "test/cctest/compiler/graph-builder-tester.h"
13 #include "test/cctest/types-fuzz.h"
14
15 using namespace v8::internal;
16 using namespace v8::internal::compiler;
17
18
19 // TODO(titzer): generate a large set of deterministic inputs for these tests.
20 class TyperTester : public HandleAndZoneScope, public GraphAndBuilders {
21 public:
22 TyperTester()
23 : GraphAndBuilders(main_zone()),
24 types_(main_zone(), isolate()),
25 typer_(isolate(), graph(), MaybeHandle<Context>()),
26 javascript_(main_zone()) {
27 Node* s = graph()->NewNode(common()->Start(3));
28 graph()->SetStart(s);
29 context_node_ = graph()->NewNode(common()->Parameter(2), graph()->start());
30 rng_ = isolate()->random_number_generator();
31
32 integers.push_back(0);
33 integers.push_back(0);
34 integers.push_back(-1);
35 integers.push_back(+1);
36 integers.push_back(-V8_INFINITY);
37 integers.push_back(+V8_INFINITY);
38 for (int i = 0; i < 5; ++i) {
39 double x = rng_->NextInt();
40 integers.push_back(x);
41 x *= rng_->NextInt();
42 if (!IsMinusZero(x)) integers.push_back(x);
43 }
44
45 int32s.push_back(0);
46 int32s.push_back(0);
47 int32s.push_back(-1);
48 int32s.push_back(+1);
49 int32s.push_back(kMinInt);
50 int32s.push_back(kMaxInt);
51 for (int i = 0; i < 10; ++i) {
52 int32s.push_back(rng_->NextInt());
53 }
54 }
55
56 Types<Type, Type*, Zone> types_;
57 Typer typer_;
58 JSOperatorBuilder javascript_;
59 Node* context_node_;
60 v8::base::RandomNumberGenerator* rng_;
61 std::vector<double> integers;
62 std::vector<double> int32s;
63
64 Isolate* isolate() { return main_isolate(); }
65 Graph* graph() { return main_graph_; }
66 CommonOperatorBuilder* common() { return &main_common_; }
67
68 Node* Parameter(int index = 0) {
69 return graph()->NewNode(common()->Parameter(index), graph()->start());
70 }
71
72 Type* TypeBinaryOp(const Operator* op, Type* lhs, Type* rhs) {
73 Node* p0 = Parameter(0);
74 Node* p1 = Parameter(1);
75 NodeProperties::SetBounds(p0, Bounds(lhs));
76 NodeProperties::SetBounds(p1, Bounds(rhs));
77 Node* n = graph()->NewNode(
78 op, p0, p1, context_node_, graph()->start(), graph()->start());
79 return NodeProperties::GetBounds(n).upper;
80 }
81
82 Type* RandomRange(bool int32 = false) {
83 std::vector<double>& numbers = int32 ? int32s : integers;
84 double i = numbers[rng_->NextInt(static_cast<int>(numbers.size()))];
85 double j = numbers[rng_->NextInt(static_cast<int>(numbers.size()))];
86 return NewRange(i, j);
87 }
88
89 Type* NewRange(double i, double j) {
90 if (i > j) std::swap(i, j);
91 return Type::Range(i, j, main_zone());
92 }
93
94 double RandomInt(double min, double max) {
95 switch (rng_->NextInt(4)) {
96 case 0: return min;
97 case 1: return max;
98 default: break;
99 }
100 if (min == +V8_INFINITY) return +V8_INFINITY;
101 if (max == -V8_INFINITY) return -V8_INFINITY;
102 if (min == -V8_INFINITY && max == +V8_INFINITY) {
103 return rng_->NextInt() * static_cast<double>(rng_->NextInt());
104 }
105 double result = nearbyint(min + (max - min) * rng_->NextDouble());
106 if (IsMinusZero(result)) return 0;
107 if (std::isnan(result)) return rng_->NextInt(2) ? min : max;
108 DCHECK(min <= result && result <= max);
109 return result;
110 }
111
112 double RandomInt(Type::RangeType* range) {
113 return RandomInt(range->Min(), range->Max());
114 }
115
116 // Careful, this function runs O(max_width^5) trials.
117 template <class BinaryFunction>
118 void TestBinaryArithOpCloseToZero(const Operator* op, BinaryFunction opfun,
119 int max_width) {
120 const int min_min = -2 - max_width / 2;
121 const int max_min = 2 + max_width / 2;
122 for (int width = 0; width < max_width; width++) {
123 for (int lmin = min_min; lmin <= max_min; lmin++) {
124 for (int rmin = min_min; rmin <= max_min; rmin++) {
125 Type* r1 = NewRange(lmin, lmin + width);
126 Type* r2 = NewRange(rmin, rmin + width);
127 Type* expected_type = TypeBinaryOp(op, r1, r2);
128
129 for (int x1 = lmin; x1 < lmin + width; x1++) {
130 for (int x2 = rmin; x2 < rmin + width; x2++) {
131 double result_value = opfun(x1, x2);
132 Type* result_type = Type::Constant(
133 isolate()->factory()->NewNumber(result_value), main_zone());
134 CHECK(result_type->Is(expected_type));
135 }
136 }
137 }
138 }
139 }
140 }
141
142 template <class BinaryFunction>
143 void TestBinaryArithOp(const Operator* op, BinaryFunction opfun) {
144 TestBinaryArithOpCloseToZero(op, opfun, 8);
145 for (int i = 0; i < 100; ++i) {
146 Type::RangeType* r1 = RandomRange()->AsRange();
147 Type::RangeType* r2 = RandomRange()->AsRange();
148 Type* expected_type = TypeBinaryOp(op, r1, r2);
149 for (int i = 0; i < 10; i++) {
150 double x1 = RandomInt(r1);
151 double x2 = RandomInt(r2);
152 double result_value = opfun(x1, x2);
153 Type* result_type = Type::Constant(
154 isolate()->factory()->NewNumber(result_value), main_zone());
155 CHECK(result_type->Is(expected_type));
156 }
157 }
158 }
159
160 template <class BinaryFunction>
161 void TestBinaryCompareOp(const Operator* op, BinaryFunction opfun) {
162 for (int i = 0; i < 100; ++i) {
163 Type::RangeType* r1 = RandomRange()->AsRange();
164 Type::RangeType* r2 = RandomRange()->AsRange();
165 Type* expected_type = TypeBinaryOp(op, r1, r2);
166 for (int i = 0; i < 10; i++) {
167 double x1 = RandomInt(r1);
168 double x2 = RandomInt(r2);
169 bool result_value = opfun(x1, x2);
170 Type* result_type =
171 Type::Constant(result_value ? isolate()->factory()->true_value()
172 : isolate()->factory()->false_value(),
173 main_zone());
174 CHECK(result_type->Is(expected_type));
175 }
176 }
177 }
178
179 template <class BinaryFunction>
180 void TestBinaryBitOp(const Operator* op, BinaryFunction opfun) {
181 for (int i = 0; i < 100; ++i) {
182 Type::RangeType* r1 = RandomRange(true)->AsRange();
183 Type::RangeType* r2 = RandomRange(true)->AsRange();
184 Type* expected_type = TypeBinaryOp(op, r1, r2);
185 for (int i = 0; i < 10; i++) {
186 int32_t x1 = static_cast<int32_t>(RandomInt(r1));
187 int32_t x2 = static_cast<int32_t>(RandomInt(r2));
188 double result_value = opfun(x1, x2);
189 Type* result_type = Type::Constant(
190 isolate()->factory()->NewNumber(result_value), main_zone());
191 CHECK(result_type->Is(expected_type));
192 }
193 }
194 }
195
196 Type* RandomSubtype(Type* type) {
197 Type* subtype;
198 do {
199 subtype = types_.Fuzz();
200 } while (!subtype->Is(type));
201 return subtype;
202 }
203
204 void TestBinaryMonotonicity(const Operator* op) {
205 for (int i = 0; i < 50; ++i) {
206 Type* type1 = types_.Fuzz();
207 Type* type2 = types_.Fuzz();
208 Type* type = TypeBinaryOp(op, type1, type2);
209 Type* subtype1 = RandomSubtype(type1);;
210 Type* subtype2 = RandomSubtype(type2);;
211 Type* subtype = TypeBinaryOp(op, subtype1, subtype2);
212 CHECK(subtype->Is(type));
213 }
214 }
215 };
216
217
218 static int32_t shift_left(int32_t x, int32_t y) { return x << y; }
219 static int32_t shift_right(int32_t x, int32_t y) { return x >> y; }
220 static int32_t bit_or(int32_t x, int32_t y) { return x | y; }
221 static int32_t bit_and(int32_t x, int32_t y) { return x & y; }
222 static int32_t bit_xor(int32_t x, int32_t y) { return x ^ y; }
223
224
225 //------------------------------------------------------------------------------
226 // Soundness
227 // For simplicity, we currently only test soundness on expression operators
228 // that have a direct equivalent in C++. Also, testing is currently limited
229 // to ranges as input types.
230
231
232 TEST(TypeJSAdd) {
233 TyperTester t;
234 t.TestBinaryArithOp(t.javascript_.Add(), std::plus<double>());
235 }
236
237
238 TEST(TypeJSSubtract) {
239 TyperTester t;
240 t.TestBinaryArithOp(t.javascript_.Subtract(), std::minus<double>());
241 }
242
243
244 TEST(TypeJSMultiply) {
245 TyperTester t;
246 t.TestBinaryArithOp(t.javascript_.Multiply(), std::multiplies<double>());
247 }
248
249
250 TEST(TypeJSDivide) {
251 TyperTester t;
252 t.TestBinaryArithOp(t.javascript_.Divide(), std::divides<double>());
253 }
254
255
256 TEST(TypeJSModulus) {
257 TyperTester t;
258 t.TestBinaryArithOp(t.javascript_.Modulus(), modulo);
259 }
260
261
262 TEST(TypeJSBitwiseOr) {
263 TyperTester t;
264 t.TestBinaryBitOp(t.javascript_.BitwiseOr(), bit_or);
265 }
266
267
268 TEST(TypeJSBitwiseAnd) {
269 TyperTester t;
270 t.TestBinaryBitOp(t.javascript_.BitwiseAnd(), bit_and);
271 }
272
273
274 TEST(TypeJSBitwiseXor) {
275 TyperTester t;
276 t.TestBinaryBitOp(t.javascript_.BitwiseXor(), bit_xor);
277 }
278
279
280 TEST(TypeJSShiftLeft) {
281 TyperTester t;
282 t.TestBinaryBitOp(t.javascript_.ShiftLeft(), shift_left);
283 }
284
285
286 TEST(TypeJSShiftRight) {
287 TyperTester t;
288 t.TestBinaryBitOp(t.javascript_.ShiftRight(), shift_right);
289 }
290
291
292 TEST(TypeJSLessThan) {
293 TyperTester t;
294 t.TestBinaryCompareOp(t.javascript_.LessThan(), std::less<double>());
295 }
296
297
298 TEST(TypeJSLessThanOrEqual) {
299 TyperTester t;
300 t.TestBinaryCompareOp(
301 t.javascript_.LessThanOrEqual(), std::less_equal<double>());
302 }
303
304
305 TEST(TypeJSGreaterThan) {
306 TyperTester t;
307 t.TestBinaryCompareOp(t.javascript_.GreaterThan(), std::greater<double>());
308 }
309
310
311 TEST(TypeJSGreaterThanOrEqual) {
312 TyperTester t;
313 t.TestBinaryCompareOp(
314 t.javascript_.GreaterThanOrEqual(), std::greater_equal<double>());
315 }
316
317
318 TEST(TypeJSEqual) {
319 TyperTester t;
320 t.TestBinaryCompareOp(t.javascript_.Equal(), std::equal_to<double>());
321 }
322
323
324 TEST(TypeJSNotEqual) {
325 TyperTester t;
326 t.TestBinaryCompareOp(t.javascript_.NotEqual(), std::not_equal_to<double>());
327 }
328
329
330 // For numbers there's no difference between strict and non-strict equality.
331 TEST(TypeJSStrictEqual) {
332 TyperTester t;
333 t.TestBinaryCompareOp(t.javascript_.StrictEqual(), std::equal_to<double>());
334 }
335
336
337 TEST(TypeJSStrictNotEqual) {
338 TyperTester t;
339 t.TestBinaryCompareOp(
340 t.javascript_.StrictNotEqual(), std::not_equal_to<double>());
341 }
342
343
344 //------------------------------------------------------------------------------
345 // Monotonicity
346
347
348 // List should be in sync with JS_SIMPLE_BINOP_LIST.
349 #define JSBINOP_LIST(V) \
350 V(Equal) \
351 V(NotEqual) \
352 V(StrictEqual) \
353 V(StrictNotEqual) \
354 V(LessThan) \
355 V(GreaterThan) \
356 V(LessThanOrEqual) \
357 V(GreaterThanOrEqual) \
358 V(BitwiseOr) \
359 V(BitwiseXor) \
360 V(BitwiseAnd) \
361 V(ShiftLeft) \
362 V(ShiftRight) \
363 V(ShiftRightLogical) \
364 V(Add) \
365 V(Subtract) \
366 V(Multiply) \
367 V(Divide) \
368 V(Modulus)
369
370
371 #define TEST_FUNC(name) \
372 TEST(Monotonicity_##name) { \
373 TyperTester t; \
374 t.TestBinaryMonotonicity(t.javascript_.name()); \
375 }
376 JSBINOP_LIST(TEST_FUNC)
377 #undef TEST_FUNC
OLDNEW
« no previous file with comments | « test/cctest/cctest.gyp ('k') | test/cctest/test-types.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698