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