OLD | NEW |
(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 "test/cctest/cctest.h" |
| 6 |
| 7 #include "src/base/utils/random-number-generator.h" |
| 8 #include "src/compiler/graph-inl.h" |
| 9 #include "src/compiler/machine-operator-reducer.h" |
| 10 #include "test/cctest/compiler/value-helper.h" |
| 11 |
| 12 using namespace v8::internal; |
| 13 using namespace v8::internal::compiler; |
| 14 |
| 15 template <typename T> |
| 16 Operator* NewConstantOperator(CommonOperatorBuilder* common, volatile T value); |
| 17 |
| 18 template <> |
| 19 Operator* NewConstantOperator<int32_t>(CommonOperatorBuilder* common, |
| 20 volatile int32_t value) { |
| 21 return common->Int32Constant(value); |
| 22 } |
| 23 |
| 24 template <> |
| 25 Operator* NewConstantOperator<double>(CommonOperatorBuilder* common, |
| 26 volatile double value) { |
| 27 return common->Float64Constant(value); |
| 28 } |
| 29 |
| 30 |
| 31 class ReducerTester : public HandleAndZoneScope { |
| 32 public: |
| 33 ReducerTester() |
| 34 : isolate(main_isolate()), |
| 35 binop(NULL), |
| 36 unop(NULL), |
| 37 machine(main_zone()), |
| 38 common(main_zone()), |
| 39 graph(main_zone()), |
| 40 maxuint32(Constant<int32_t>(kMaxUInt32)) { |
| 41 } |
| 42 |
| 43 Isolate* isolate; |
| 44 Operator* binop; |
| 45 Operator* unop; |
| 46 MachineOperatorBuilder machine; |
| 47 CommonOperatorBuilder common; |
| 48 Graph graph; |
| 49 Node* maxuint32; |
| 50 |
| 51 template <typename T> |
| 52 Node* Constant(volatile T value) { |
| 53 return graph.NewNode(NewConstantOperator<T>(&common, value)); |
| 54 } |
| 55 |
| 56 // Check that the reduction of this binop applied to constants {a} and {b} |
| 57 // yields the {expect} value. |
| 58 template <typename T> |
| 59 void CheckFoldBinop(volatile T expect, volatile T a, volatile T b) { |
| 60 CheckFoldBinop<T>(expect, Constant<T>(a), Constant<T>(b)); |
| 61 } |
| 62 |
| 63 // Check that the reduction of this binop applied to {a} and {b} yields |
| 64 // the {expect} value. |
| 65 template <typename T> |
| 66 void CheckFoldBinop(volatile T expect, Node* a, Node* b) { |
| 67 CHECK_NE(NULL, binop); |
| 68 Node* n = graph.NewNode(binop, a, b); |
| 69 MachineOperatorReducer reducer(&graph); |
| 70 Reduction reduction = reducer.Reduce(n); |
| 71 CHECK(reduction.Changed()); |
| 72 CHECK_NE(n, reduction.replacement()); |
| 73 CHECK_EQ(expect, ValueOf<T>(reduction.replacement()->op())); |
| 74 } |
| 75 |
| 76 // Check that the reduction of this binop applied to {a} and {b} yields |
| 77 // the {expect} node. |
| 78 void CheckBinop(Node* expect, Node* a, Node* b) { |
| 79 CHECK_NE(NULL, binop); |
| 80 Node* n = graph.NewNode(binop, a, b); |
| 81 MachineOperatorReducer reducer(&graph); |
| 82 Reduction reduction = reducer.Reduce(n); |
| 83 CHECK(reduction.Changed()); |
| 84 CHECK_EQ(expect, reduction.replacement()); |
| 85 } |
| 86 |
| 87 // Check that the reduction of this binop applied to {left} and {right} yields |
| 88 // this binop applied to {left_expect} and {right_expect}. |
| 89 void CheckFoldBinop(Node* left_expect, |
| 90 Node* right_expect, |
| 91 Node* left, |
| 92 Node* right) { |
| 93 CHECK_NE(NULL, binop); |
| 94 Node* n = graph.NewNode(binop, left, right); |
| 95 MachineOperatorReducer reducer(&graph); |
| 96 Reduction reduction = reducer.Reduce(n); |
| 97 CHECK(reduction.Changed()); |
| 98 CHECK_EQ(binop, reduction.replacement()->op()); |
| 99 CHECK_EQ(left_expect, reduction.replacement()->InputAt(0)); |
| 100 CHECK_EQ(right_expect, reduction.replacement()->InputAt(1)); |
| 101 } |
| 102 |
| 103 // Check that the reduction of this binop applied to {left} and {right} yields |
| 104 // the {op_expect} applied to {left_expect} and {right_expect}. |
| 105 template <typename T> |
| 106 void CheckFoldBinop(volatile T left_expect, |
| 107 Operator* op_expect, |
| 108 Node* right_expect, |
| 109 Node* left, |
| 110 Node* right) { |
| 111 CHECK_NE(NULL, binop); |
| 112 Node* n = graph.NewNode(binop, left, right); |
| 113 MachineOperatorReducer reducer(&graph); |
| 114 Reduction r = reducer.Reduce(n); |
| 115 CHECK(r.Changed()); |
| 116 CHECK_EQ(op_expect->opcode(), r.replacement()->op()->opcode()); |
| 117 CHECK_EQ(left_expect, ValueOf<T>(r.replacement()->InputAt(0)->op())); |
| 118 CHECK_EQ(right_expect, r.replacement()->InputAt(1)); |
| 119 } |
| 120 |
| 121 // Check that the reduction of this binop applied to {left} and {right} yields |
| 122 // the {op_expect} applied to {left_expect} and {right_expect}. |
| 123 template <typename T> |
| 124 void CheckFoldBinop(Node* left_expect, |
| 125 Operator* op_expect, |
| 126 volatile T right_expect, |
| 127 Node* left, |
| 128 Node* right) { |
| 129 CHECK_NE(NULL, binop); |
| 130 Node* n = graph.NewNode(binop, left, right); |
| 131 MachineOperatorReducer reducer(&graph); |
| 132 Reduction r = reducer.Reduce(n); |
| 133 CHECK(r.Changed()); |
| 134 CHECK_EQ(op_expect->opcode(), r.replacement()->op()->opcode()); |
| 135 CHECK_EQ(left_expect, r.replacement()->InputAt(0)); |
| 136 CHECK_EQ(right_expect, ValueOf<T>(r.replacement()->InputAt(1)->op())); |
| 137 } |
| 138 |
| 139 // Check that if the given constant appears on the left, the reducer will |
| 140 // swap it to be on the right. |
| 141 template <typename T> |
| 142 void CheckPutConstantOnRight(volatile T constant) { |
| 143 // TODO(titzer): CHECK(binop->HasProperty(Operator::kCommutative)); |
| 144 Node* p = Parameter(); |
| 145 Node* k = Constant<T>(constant); |
| 146 { |
| 147 Node* n = graph.NewNode(binop, k, p); |
| 148 MachineOperatorReducer reducer(&graph); |
| 149 Reduction reduction = reducer.Reduce(n); |
| 150 CHECK(!reduction.Changed() || reduction.replacement() == n); |
| 151 CHECK_EQ(p, n->InputAt(0)); |
| 152 CHECK_EQ(k, n->InputAt(1)); |
| 153 } |
| 154 { |
| 155 Node* n = graph.NewNode(binop, p, k); |
| 156 MachineOperatorReducer reducer(&graph); |
| 157 Reduction reduction = reducer.Reduce(n); |
| 158 CHECK(!reduction.Changed()); |
| 159 CHECK_EQ(p, n->InputAt(0)); |
| 160 CHECK_EQ(k, n->InputAt(1)); |
| 161 } |
| 162 } |
| 163 |
| 164 // Check that if the given constant appears on the left, the reducer will |
| 165 // *NOT* swap it to be on the right. |
| 166 template <typename T> |
| 167 void CheckDontPutConstantOnRight(volatile T constant) { |
| 168 CHECK(!binop->HasProperty(Operator::kCommutative)); |
| 169 Node* p = Parameter(); |
| 170 Node* k = Constant<T>(constant); |
| 171 Node* n = graph.NewNode(binop, k, p); |
| 172 MachineOperatorReducer reducer(&graph); |
| 173 Reduction reduction = reducer.Reduce(n); |
| 174 CHECK(!reduction.Changed()); |
| 175 CHECK_EQ(k, n->InputAt(0)); |
| 176 CHECK_EQ(p, n->InputAt(1)); |
| 177 } |
| 178 |
| 179 Node* Parameter(int32_t index = 0) { |
| 180 return graph.NewNode(common.Parameter(index)); |
| 181 } |
| 182 }; |
| 183 |
| 184 |
| 185 TEST(ReduceWord32And) { |
| 186 ReducerTester R; |
| 187 R.binop = R.machine.Word32And(); |
| 188 |
| 189 FOR_INT32_INPUTS(pl) { |
| 190 FOR_INT32_INPUTS(pr) { |
| 191 int32_t x = *pl, y = *pr; |
| 192 R.CheckFoldBinop<int32_t>(x & y, x, y); |
| 193 } |
| 194 } |
| 195 |
| 196 R.CheckPutConstantOnRight(33); |
| 197 R.CheckPutConstantOnRight(44000); |
| 198 |
| 199 Node* x = R.Parameter(); |
| 200 Node* zero = R.Constant<int32_t>(0); |
| 201 Node* minus_1 = R.Constant<int32_t>(-1); |
| 202 |
| 203 R.CheckBinop(zero, x, zero); // x & 0 => 0 |
| 204 R.CheckBinop(zero, zero, x); // 0 & x => 0 |
| 205 R.CheckBinop(x, x, minus_1); // x & -1 => 0 |
| 206 R.CheckBinop(x, minus_1, x); // -1 & x => 0 |
| 207 R.CheckBinop(x, x, x); // x & x => x |
| 208 } |
| 209 |
| 210 |
| 211 TEST(ReduceWord32Or) { |
| 212 ReducerTester R; |
| 213 R.binop = R.machine.Word32Or(); |
| 214 |
| 215 FOR_INT32_INPUTS(pl) { |
| 216 FOR_INT32_INPUTS(pr) { |
| 217 int32_t x = *pl, y = *pr; |
| 218 R.CheckFoldBinop<int32_t>(x | y, x, y); |
| 219 } |
| 220 } |
| 221 |
| 222 R.CheckPutConstantOnRight(36); |
| 223 R.CheckPutConstantOnRight(44001); |
| 224 |
| 225 Node* x = R.Parameter(); |
| 226 Node* zero = R.Constant<int32_t>(0); |
| 227 Node* minus_1 = R.Constant<int32_t>(-1); |
| 228 |
| 229 R.CheckBinop(x, x, zero); // x & 0 => x |
| 230 R.CheckBinop(x, zero, x); // 0 & x => x |
| 231 R.CheckBinop(minus_1, x, minus_1); // x & -1 => -1 |
| 232 R.CheckBinop(minus_1, minus_1, x); // -1 & x => -1 |
| 233 R.CheckBinop(x, x, x); // x & x => x |
| 234 } |
| 235 |
| 236 |
| 237 TEST(ReduceWord32Xor) { |
| 238 ReducerTester R; |
| 239 R.binop = R.machine.Word32Xor(); |
| 240 |
| 241 FOR_INT32_INPUTS(pl) { |
| 242 FOR_INT32_INPUTS(pr) { |
| 243 int32_t x = *pl, y = *pr; |
| 244 R.CheckFoldBinop<int32_t>(x ^ y, x, y); |
| 245 } |
| 246 } |
| 247 |
| 248 R.CheckPutConstantOnRight(39); |
| 249 R.CheckPutConstantOnRight(4403); |
| 250 |
| 251 Node* x = R.Parameter(); |
| 252 Node* zero = R.Constant<int32_t>(0); |
| 253 |
| 254 R.CheckBinop(x, x, zero); // x ^ 0 => x |
| 255 R.CheckBinop(x, zero, x); // 0 ^ x => x |
| 256 R.CheckFoldBinop<int32_t>(0, x, x); // x ^ x => 0 |
| 257 } |
| 258 |
| 259 |
| 260 TEST(ReduceWord32Shl) { |
| 261 ReducerTester R; |
| 262 R.binop = R.machine.Word32Shl(); |
| 263 |
| 264 // TODO(titzer): out of range shifts |
| 265 FOR_INT32_INPUTS(i) { |
| 266 for (int y = 0; y < 32; y++) { |
| 267 int32_t x = *i; |
| 268 R.CheckFoldBinop<int32_t>(x << y, x, y); |
| 269 } |
| 270 } |
| 271 |
| 272 R.CheckDontPutConstantOnRight(44); |
| 273 |
| 274 Node* x = R.Parameter(); |
| 275 Node* zero = R.Constant<int32_t>(0); |
| 276 |
| 277 R.CheckBinop(x, x, zero); // x << 0 => x |
| 278 } |
| 279 |
| 280 |
| 281 TEST(ReduceWord32Shr) { |
| 282 ReducerTester R; |
| 283 R.binop = R.machine.Word32Shr(); |
| 284 |
| 285 // TODO(titzer): test out of range shifts |
| 286 FOR_UINT32_INPUTS(i) { |
| 287 for (uint32_t y = 0; y < 32; y++) { |
| 288 uint32_t x = *i; |
| 289 R.CheckFoldBinop<int32_t>(x >> y, x, y); |
| 290 } |
| 291 } |
| 292 |
| 293 R.CheckDontPutConstantOnRight(44); |
| 294 |
| 295 Node* x = R.Parameter(); |
| 296 Node* zero = R.Constant<int32_t>(0); |
| 297 |
| 298 R.CheckBinop(x, x, zero); // x >>> 0 => x |
| 299 } |
| 300 |
| 301 |
| 302 TEST(ReduceWord32Sar) { |
| 303 ReducerTester R; |
| 304 R.binop = R.machine.Word32Sar(); |
| 305 |
| 306 // TODO(titzer): test out of range shifts |
| 307 FOR_INT32_INPUTS(i) { |
| 308 for (int32_t y = 0; y < 32; y++) { |
| 309 int32_t x = *i; |
| 310 R.CheckFoldBinop<int32_t>(x >> y, x, y); |
| 311 } |
| 312 } |
| 313 |
| 314 R.CheckDontPutConstantOnRight(44); |
| 315 |
| 316 Node* x = R.Parameter(); |
| 317 Node* zero = R.Constant<int32_t>(0); |
| 318 |
| 319 R.CheckBinop(x, x, zero); // x >> 0 => x |
| 320 } |
| 321 |
| 322 |
| 323 TEST(ReduceWord32Equal) { |
| 324 ReducerTester R; |
| 325 R.binop = R.machine.Word32Equal(); |
| 326 |
| 327 FOR_INT32_INPUTS(pl) { |
| 328 FOR_INT32_INPUTS(pr) { |
| 329 int32_t x = *pl, y = *pr; |
| 330 R.CheckFoldBinop<int32_t>(x == y ? 1 : 0, x, y); |
| 331 } |
| 332 } |
| 333 |
| 334 R.CheckPutConstantOnRight(48); |
| 335 R.CheckPutConstantOnRight(-48); |
| 336 |
| 337 Node* x = R.Parameter(0); |
| 338 Node* y = R.Parameter(1); |
| 339 Node* zero = R.Constant<int32_t>(0); |
| 340 Node* sub = R.graph.NewNode(R.machine.Int32Sub(), x, y); |
| 341 |
| 342 R.CheckFoldBinop<int32_t>(1, x, x); // x == x => 1 |
| 343 R.CheckFoldBinop(x, y, sub, zero); // x - y == 0 => x == y |
| 344 R.CheckFoldBinop(x, y, zero, sub); // 0 == x - y => x == y |
| 345 } |
| 346 |
| 347 |
| 348 TEST(ReduceInt32Add) { |
| 349 ReducerTester R; |
| 350 R.binop = R.machine.Int32Add(); |
| 351 |
| 352 FOR_INT32_INPUTS(pl) { |
| 353 FOR_INT32_INPUTS(pr) { |
| 354 int32_t x = *pl, y = *pr; |
| 355 R.CheckFoldBinop<int32_t>(x + y, x, y); // TODO(titzer): signed overflow |
| 356 } |
| 357 } |
| 358 |
| 359 R.CheckPutConstantOnRight(41); |
| 360 R.CheckPutConstantOnRight(4407); |
| 361 |
| 362 Node* x = R.Parameter(); |
| 363 Node* zero = R.Constant<int32_t>(0); |
| 364 |
| 365 R.CheckBinop(x, x, zero); // x + 0 => x |
| 366 R.CheckBinop(x, zero, x); // 0 + x => x |
| 367 } |
| 368 |
| 369 |
| 370 TEST(ReduceInt32Sub) { |
| 371 ReducerTester R; |
| 372 R.binop = R.machine.Int32Sub(); |
| 373 |
| 374 FOR_INT32_INPUTS(pl) { |
| 375 FOR_INT32_INPUTS(pr) { |
| 376 int32_t x = *pl, y = *pr; |
| 377 R.CheckFoldBinop<int32_t>(x - y, x, y); |
| 378 } |
| 379 } |
| 380 |
| 381 R.CheckDontPutConstantOnRight(412); |
| 382 |
| 383 Node* x = R.Parameter(); |
| 384 Node* zero = R.Constant<int32_t>(0); |
| 385 |
| 386 R.CheckBinop(x, x, zero); // x - 0 => x |
| 387 } |
| 388 |
| 389 |
| 390 TEST(ReduceInt32Mul) { |
| 391 ReducerTester R; |
| 392 R.binop = R.machine.Int32Mul(); |
| 393 |
| 394 FOR_INT32_INPUTS(pl) { |
| 395 FOR_INT32_INPUTS(pr) { |
| 396 int32_t x = *pl, y = *pr; |
| 397 R.CheckFoldBinop<int32_t>(x * y, x, y); // TODO(titzer): signed overflow |
| 398 } |
| 399 } |
| 400 |
| 401 R.CheckPutConstantOnRight(4111); |
| 402 R.CheckPutConstantOnRight(-4407); |
| 403 |
| 404 Node* x = R.Parameter(); |
| 405 Node* zero = R.Constant<int32_t>(0); |
| 406 Node* one = R.Constant<int32_t>(1); |
| 407 Node* minus_one = R.Constant<int32_t>(-1); |
| 408 |
| 409 R.CheckBinop(zero, x, zero); // x * 0 => 0 |
| 410 R.CheckBinop(zero, zero, x); // 0 * x => 0 |
| 411 R.CheckBinop(x, x, one); // x * 1 => x |
| 412 R.CheckBinop(x, one, x); // 1 * x => x |
| 413 R.CheckFoldBinop<int32_t>(0, R.machine.Int32Sub(), x, |
| 414 minus_one, x); // -1 * x => 0 - x |
| 415 R.CheckFoldBinop<int32_t>(0, R.machine.Int32Sub(), x, |
| 416 x, minus_one); // x * -1 => 0 - x |
| 417 |
| 418 for (int32_t n = 1; n < 31; ++n) { |
| 419 Node* multiplier = R.Constant<int32_t>(1 << n); |
| 420 R.CheckFoldBinop<int32_t>(x, R.machine.Word32Shl(), n, |
| 421 x, multiplier); // x * 2^n => x << n |
| 422 R.CheckFoldBinop<int32_t>(x, R.machine.Word32Shl(), n, |
| 423 multiplier, x); // 2^n * x => x << n |
| 424 } |
| 425 } |
| 426 |
| 427 |
| 428 TEST(ReduceInt32Div) { |
| 429 ReducerTester R; |
| 430 R.binop = R.machine.Int32Div(); |
| 431 |
| 432 FOR_INT32_INPUTS(pl) { |
| 433 FOR_INT32_INPUTS(pr) { |
| 434 int32_t x = *pl, y = *pr; |
| 435 if (y == 0) continue; // TODO(titzer): test / 0 |
| 436 int32_t r = y == -1 ? -x : x / y; // INT_MIN / -1 may explode in C |
| 437 R.CheckFoldBinop<int32_t>(r, x, y); |
| 438 } |
| 439 } |
| 440 |
| 441 R.CheckDontPutConstantOnRight(41111); |
| 442 R.CheckDontPutConstantOnRight(-44071); |
| 443 |
| 444 Node* x = R.Parameter(); |
| 445 Node* one = R.Constant<int32_t>(1); |
| 446 Node* minus_one = R.Constant<int32_t>(-1); |
| 447 |
| 448 R.CheckBinop(x, x, one); // x / 1 => x |
| 449 // TODO(titzer): // 0 / x => 0 if x != 0 |
| 450 // TODO(titzer): // x / 2^n => x >> n and round |
| 451 R.CheckFoldBinop<int32_t>(0, R.machine.Int32Sub(), x, |
| 452 x, minus_one); // x / -1 => 0 - x |
| 453 } |
| 454 |
| 455 |
| 456 TEST(ReduceInt32UDiv) { |
| 457 ReducerTester R; |
| 458 R.binop = R.machine.Int32UDiv(); |
| 459 |
| 460 FOR_UINT32_INPUTS(pl) { |
| 461 FOR_UINT32_INPUTS(pr) { |
| 462 uint32_t x = *pl, y = *pr; |
| 463 if (y == 0) continue; // TODO(titzer): test / 0 |
| 464 R.CheckFoldBinop<int32_t>(x / y, x, y); |
| 465 } |
| 466 } |
| 467 |
| 468 R.CheckDontPutConstantOnRight(41311); |
| 469 R.CheckDontPutConstantOnRight(-44371); |
| 470 |
| 471 Node* x = R.Parameter(); |
| 472 Node* one = R.Constant<int32_t>(1); |
| 473 |
| 474 R.CheckBinop(x, x, one); // x / 1 => x |
| 475 // TODO(titzer): // 0 / x => 0 if x != 0 |
| 476 |
| 477 for (uint32_t n = 1; n < 32; ++n) { |
| 478 Node* divisor = R.Constant<int32_t>(1u << n); |
| 479 R.CheckFoldBinop<int32_t>(x, R.machine.Word32Shr(), n, |
| 480 x, divisor); // x / 2^n => x >> n |
| 481 } |
| 482 } |
| 483 |
| 484 |
| 485 TEST(ReduceInt32Mod) { |
| 486 ReducerTester R; |
| 487 R.binop = R.machine.Int32Mod(); |
| 488 |
| 489 FOR_INT32_INPUTS(pl) { |
| 490 FOR_INT32_INPUTS(pr) { |
| 491 int32_t x = *pl, y = *pr; |
| 492 if (y == 0) continue; // TODO(titzer): test % 0 |
| 493 int32_t r = y == -1 ? 0 : x % y; // INT_MIN % -1 may explode in C |
| 494 R.CheckFoldBinop<int32_t>(r, x, y); |
| 495 } |
| 496 } |
| 497 |
| 498 R.CheckDontPutConstantOnRight(413); |
| 499 R.CheckDontPutConstantOnRight(-4401); |
| 500 |
| 501 Node* x = R.Parameter(); |
| 502 Node* one = R.Constant<int32_t>(1); |
| 503 |
| 504 R.CheckFoldBinop<int32_t>(0, x, one); // x % 1 => 0 |
| 505 // TODO(titzer): // x % 2^n => x & 2^n-1 and round |
| 506 } |
| 507 |
| 508 |
| 509 TEST(ReduceInt32UMod) { |
| 510 ReducerTester R; |
| 511 R.binop = R.machine.Int32UMod(); |
| 512 |
| 513 FOR_INT32_INPUTS(pl) { |
| 514 FOR_INT32_INPUTS(pr) { |
| 515 uint32_t x = *pl, y = *pr; |
| 516 if (y == 0) continue; // TODO(titzer): test x % 0 |
| 517 R.CheckFoldBinop<int32_t>(x % y, x, y); |
| 518 } |
| 519 } |
| 520 |
| 521 R.CheckDontPutConstantOnRight(417); |
| 522 R.CheckDontPutConstantOnRight(-4371); |
| 523 |
| 524 Node* x = R.Parameter(); |
| 525 Node* one = R.Constant<int32_t>(1); |
| 526 |
| 527 R.CheckFoldBinop<int32_t>(0, x, one); // x % 1 => 0 |
| 528 |
| 529 for (uint32_t n = 1; n < 32; ++n) { |
| 530 Node* divisor = R.Constant<int32_t>(1u << n); |
| 531 R.CheckFoldBinop<int32_t>(x, R.machine.Word32And(), (1u << n) - 1, |
| 532 x, divisor); // x % 2^n => x & 2^n-1 |
| 533 } |
| 534 } |
| 535 |
| 536 |
| 537 TEST(ReduceInt32LessThan) { |
| 538 ReducerTester R; |
| 539 R.binop = R.machine.Int32LessThan(); |
| 540 |
| 541 FOR_INT32_INPUTS(pl) { |
| 542 FOR_INT32_INPUTS(pr) { |
| 543 int32_t x = *pl, y = *pr; |
| 544 R.CheckFoldBinop<int32_t>(x < y ? 1 : 0, x, y); |
| 545 } |
| 546 } |
| 547 |
| 548 R.CheckDontPutConstantOnRight(41399); |
| 549 R.CheckDontPutConstantOnRight(-440197); |
| 550 |
| 551 Node* x = R.Parameter(0); |
| 552 Node* y = R.Parameter(1); |
| 553 Node* zero = R.Constant<int32_t>(0); |
| 554 Node* sub = R.graph.NewNode(R.machine.Int32Sub(), x, y); |
| 555 |
| 556 R.CheckFoldBinop<int32_t>(0, x, x); // x < x => 0 |
| 557 R.CheckFoldBinop(x, y, sub, zero); // x - y < 0 => x < y |
| 558 R.CheckFoldBinop(y, x, zero, sub); // 0 < x - y => y < x |
| 559 } |
| 560 |
| 561 |
| 562 TEST(ReduceInt32LessThanOrEqual) { |
| 563 ReducerTester R; |
| 564 R.binop = R.machine.Int32LessThanOrEqual(); |
| 565 |
| 566 FOR_INT32_INPUTS(pl) { |
| 567 FOR_INT32_INPUTS(pr) { |
| 568 int32_t x = *pl, y = *pr; |
| 569 R.CheckFoldBinop<int32_t>(x <= y ? 1 : 0, x, y); |
| 570 } |
| 571 } |
| 572 |
| 573 FOR_INT32_INPUTS(i) { R.CheckDontPutConstantOnRight<int32_t>(*i); } |
| 574 |
| 575 Node* x = R.Parameter(0); |
| 576 Node* y = R.Parameter(1); |
| 577 Node* zero = R.Constant<int32_t>(0); |
| 578 Node* sub = R.graph.NewNode(R.machine.Int32Sub(), x, y); |
| 579 |
| 580 R.CheckFoldBinop<int32_t>(1, x, x); // x <= x => 1 |
| 581 R.CheckFoldBinop(x, y, sub, zero); // x - y <= 0 => x <= y |
| 582 R.CheckFoldBinop(y, x, zero, sub); // 0 <= x - y => y <= x |
| 583 } |
| 584 |
| 585 |
| 586 TEST(ReduceUint32LessThan) { |
| 587 ReducerTester R; |
| 588 R.binop = R.machine.Uint32LessThan(); |
| 589 |
| 590 FOR_UINT32_INPUTS(pl) { |
| 591 FOR_UINT32_INPUTS(pr) { |
| 592 uint32_t x = *pl, y = *pr; |
| 593 R.CheckFoldBinop<int32_t>(x < y ? 1 : 0, x, y); |
| 594 } |
| 595 } |
| 596 |
| 597 R.CheckDontPutConstantOnRight(41399); |
| 598 R.CheckDontPutConstantOnRight(-440197); |
| 599 |
| 600 Node* x = R.Parameter(); |
| 601 Node* max = R.maxuint32; |
| 602 Node* zero = R.Constant<int32_t>(0); |
| 603 |
| 604 R.CheckFoldBinop<int32_t>(0, max, x); // M < x => 0 |
| 605 R.CheckFoldBinop<int32_t>(0, x, zero); // x < 0 => 0 |
| 606 R.CheckFoldBinop<int32_t>(0, x, x); // x < x => 0 |
| 607 } |
| 608 |
| 609 |
| 610 TEST(ReduceUint32LessThanOrEqual) { |
| 611 ReducerTester R; |
| 612 R.binop = R.machine.Uint32LessThanOrEqual(); |
| 613 |
| 614 FOR_UINT32_INPUTS(pl) { |
| 615 FOR_UINT32_INPUTS(pr) { |
| 616 uint32_t x = *pl, y = *pr; |
| 617 R.CheckFoldBinop<int32_t>(x <= y ? 1 : 0, x, y); |
| 618 } |
| 619 } |
| 620 |
| 621 R.CheckDontPutConstantOnRight(41399); |
| 622 R.CheckDontPutConstantOnRight(-440197); |
| 623 |
| 624 Node* x = R.Parameter(); |
| 625 Node* max = R.maxuint32; |
| 626 Node* zero = R.Constant<int32_t>(0); |
| 627 |
| 628 R.CheckFoldBinop<int32_t>(1, x, max); // x <= M => 1 |
| 629 R.CheckFoldBinop<int32_t>(1, zero, x); // 0 <= x => 1 |
| 630 R.CheckFoldBinop<int32_t>(1, x, x); // x <= x => 1 |
| 631 } |
| 632 |
| 633 |
| 634 TEST(ReduceLoadStore) { |
| 635 ReducerTester R; |
| 636 |
| 637 Node* base = R.Constant<int32_t>(11); |
| 638 Node* index = R.Constant<int32_t>(4); |
| 639 Node* load = R.graph.NewNode(R.machine.Load(kMachineWord32), base, index); |
| 640 |
| 641 { |
| 642 MachineOperatorReducer reducer(&R.graph); |
| 643 Reduction reduction = reducer.Reduce(load); |
| 644 CHECK(!reduction.Changed()); // loads should not be reduced. |
| 645 } |
| 646 |
| 647 { |
| 648 Node* store = R.graph.NewNode(R.machine.Store(kMachineWord32), |
| 649 base, index, load); |
| 650 MachineOperatorReducer reducer(&R.graph); |
| 651 Reduction reduction = reducer.Reduce(store); |
| 652 CHECK(!reduction.Changed()); // stores should not be reduced. |
| 653 } |
| 654 } |
| 655 |
| 656 |
| 657 static void CheckNans(ReducerTester* R) { |
| 658 Node* x = R->Parameter(); |
| 659 std::vector<double> nans = ValueHelper::nan_vector(); |
| 660 for (std::vector<double>::const_iterator pl = nans.begin(); pl != nans.end(); |
| 661 ++pl) { |
| 662 for (std::vector<double>::const_iterator pr = nans.begin(); |
| 663 pr != nans.end(); ++pr) { |
| 664 Node* nan1 = R->Constant<double>(*pl); |
| 665 Node* nan2 = R->Constant<double>(*pr); |
| 666 R->CheckBinop(nan1, x, nan1); // x % NaN => NaN |
| 667 R->CheckBinop(nan1, nan1, x); // NaN % x => NaN |
| 668 R->CheckBinop(nan1, nan2, nan1); // NaN % NaN => NaN |
| 669 } |
| 670 } |
| 671 } |
| 672 |
| 673 |
| 674 TEST(ReduceFloat64Add) { |
| 675 ReducerTester R; |
| 676 R.binop = R.machine.Float64Add(); |
| 677 |
| 678 FOR_FLOAT64_INPUTS(pl) { |
| 679 FOR_FLOAT64_INPUTS(pr) { |
| 680 double x = *pl, y = *pr; |
| 681 R.CheckFoldBinop<double>(x + y, x, y); |
| 682 } |
| 683 } |
| 684 |
| 685 FOR_FLOAT64_INPUTS(i) { R.CheckPutConstantOnRight(*i); } |
| 686 // TODO(titzer): CheckNans(&R); |
| 687 } |
| 688 |
| 689 |
| 690 TEST(ReduceFloat64Sub) { |
| 691 ReducerTester R; |
| 692 R.binop = R.machine.Float64Sub(); |
| 693 |
| 694 FOR_FLOAT64_INPUTS(pl) { |
| 695 FOR_FLOAT64_INPUTS(pr) { |
| 696 double x = *pl, y = *pr; |
| 697 R.CheckFoldBinop<double>(x - y, x, y); |
| 698 } |
| 699 } |
| 700 // TODO(titzer): CheckNans(&R); |
| 701 } |
| 702 |
| 703 |
| 704 TEST(ReduceFloat64Mul) { |
| 705 ReducerTester R; |
| 706 R.binop = R.machine.Float64Mul(); |
| 707 |
| 708 FOR_FLOAT64_INPUTS(pl) { |
| 709 FOR_FLOAT64_INPUTS(pr) { |
| 710 double x = *pl, y = *pr; |
| 711 R.CheckFoldBinop<double>(x * y, x, y); |
| 712 } |
| 713 } |
| 714 |
| 715 double inf = V8_INFINITY; |
| 716 R.CheckPutConstantOnRight(-inf); |
| 717 R.CheckPutConstantOnRight(-0.1); |
| 718 R.CheckPutConstantOnRight(0.1); |
| 719 R.CheckPutConstantOnRight(inf); |
| 720 |
| 721 Node* x = R.Parameter(); |
| 722 Node* one = R.Constant<double>(1.0); |
| 723 |
| 724 R.CheckBinop(x, x, one); // x * 1.0 => x |
| 725 R.CheckBinop(x, one, x); // 1.0 * x => x |
| 726 |
| 727 CheckNans(&R); |
| 728 } |
| 729 |
| 730 |
| 731 TEST(ReduceFloat64Div) { |
| 732 ReducerTester R; |
| 733 R.binop = R.machine.Float64Div(); |
| 734 |
| 735 FOR_FLOAT64_INPUTS(pl) { |
| 736 FOR_FLOAT64_INPUTS(pr) { |
| 737 double x = *pl, y = *pr; |
| 738 R.CheckFoldBinop<double>(x / y, x, y); |
| 739 } |
| 740 } |
| 741 |
| 742 Node* x = R.Parameter(); |
| 743 Node* one = R.Constant<double>(1.0); |
| 744 |
| 745 R.CheckBinop(x, x, one); // x / 1.0 => x |
| 746 |
| 747 CheckNans(&R); |
| 748 } |
| 749 |
| 750 |
| 751 TEST(ReduceFloat64Mod) { |
| 752 ReducerTester R; |
| 753 R.binop = R.machine.Float64Mod(); |
| 754 |
| 755 FOR_FLOAT64_INPUTS(pl) { |
| 756 FOR_FLOAT64_INPUTS(pr) { |
| 757 double x = *pl, y = *pr; |
| 758 R.CheckFoldBinop<double>(modulo(x, y), x, y); |
| 759 } |
| 760 } |
| 761 |
| 762 CheckNans(&R); |
| 763 } |
| 764 |
| 765 |
| 766 // TODO(titzer): test MachineOperatorReducer for Word64And |
| 767 // TODO(titzer): test MachineOperatorReducer for Word64Or |
| 768 // TODO(titzer): test MachineOperatorReducer for Word64Xor |
| 769 // TODO(titzer): test MachineOperatorReducer for Word64Shl |
| 770 // TODO(titzer): test MachineOperatorReducer for Word64Shr |
| 771 // TODO(titzer): test MachineOperatorReducer for Word64Sar |
| 772 // TODO(titzer): test MachineOperatorReducer for Word64Equal |
| 773 // TODO(titzer): test MachineOperatorReducer for Word64Not |
| 774 // TODO(titzer): test MachineOperatorReducer for Int64Add |
| 775 // TODO(titzer): test MachineOperatorReducer for Int64Sub |
| 776 // TODO(titzer): test MachineOperatorReducer for Int64Mul |
| 777 // TODO(titzer): test MachineOperatorReducer for Int64UMul |
| 778 // TODO(titzer): test MachineOperatorReducer for Int64Div |
| 779 // TODO(titzer): test MachineOperatorReducer for Int64UDiv |
| 780 // TODO(titzer): test MachineOperatorReducer for Int64Mod |
| 781 // TODO(titzer): test MachineOperatorReducer for Int64UMod |
| 782 // TODO(titzer): test MachineOperatorReducer for Int64Neg |
| 783 // TODO(titzer): test MachineOperatorReducer for ConvertInt32ToFloat64 |
| 784 // TODO(titzer): test MachineOperatorReducer for ConvertFloat64ToInt32 |
| 785 // TODO(titzer): test MachineOperatorReducer for Float64Compare |
OLD | NEW |