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

Side by Side Diff: runtime/vm/intermediate_language.cc

Issue 11773040: Canonicalize away simple arithmetic equivalences. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « runtime/vm/intermediate_language.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "vm/intermediate_language.h" 5 #include "vm/intermediate_language.h"
6 6
7 #include "vm/bit_vector.h" 7 #include "vm/bit_vector.h"
8 #include "vm/dart_entry.h" 8 #include "vm/dart_entry.h"
9 #include "vm/flow_graph_allocator.h" 9 #include "vm/flow_graph_allocator.h"
10 #include "vm/flow_graph_builder.h" 10 #include "vm/flow_graph_builder.h"
(...skipping 1362 matching lines...) Expand 10 before | Expand all | Expand 10 after
1373 return Type::Double(); 1373 return Type::Double();
1374 } 1374 }
1375 1375
1376 1376
1377 intptr_t BinaryDoubleOpInstr::ResultCid() const { 1377 intptr_t BinaryDoubleOpInstr::ResultCid() const {
1378 // The output is not an instance but when it is boxed it becomes double. 1378 // The output is not an instance but when it is boxed it becomes double.
1379 return kDoubleCid; 1379 return kDoubleCid;
1380 } 1380 }
1381 1381
1382 1382
1383 static bool ToIntegerConstant(Value* value, intptr_t* result) {
1384 if (!value->BindsToConstant()) {
1385 return false;
1386 }
1387
1388 const Object& constant = value->BoundConstant();
1389 if (constant.IsDouble()) {
1390 const Double& double_constant = Double::Cast(constant);
1391 *result = static_cast<intptr_t>(double_constant.value());
1392 return (static_cast<double>(*result) == double_constant.value());
1393 } else if (constant.IsSmi()) {
1394 *result = Smi::Cast(constant).Value();
1395 return true;
1396 }
1397
1398 return false;
1399 }
1400
1401
1402 static Definition* CanonicalizeCommutativeArithmetic(Token::Kind op,
1403 intptr_t cid,
1404 Value* left,
1405 Value* right) {
1406 ASSERT((cid == kSmiCid) || (cid == kDoubleCid));
1407
1408 intptr_t value;
Florian Schneider 2013/01/08 12:15:47 For better readability I'd rename this to left_val
1409 if (!ToIntegerConstant(left, &value)) {
1410 return NULL;
1411 }
1412
1413 switch (op) {
1414 case Token::kMUL:
1415 if (value == 1) {
1416 return right->definition();
1417 } else if (value == 0) {
1418 return left->definition();
1419 }
1420 break;
1421 case Token::kADD:
1422 if ((value == 0) && (cid == kSmiCid)) {
1423 // Can't apply this equivalence to double operations because
1424 // 0.0 + (-0.0) is 0.0 not -0.0.
1425 return right->definition();
1426 }
1427 break;
1428 case Token::kBIT_AND:
1429 ASSERT(cid == kSmiCid);
1430 if (value == 0) {
1431 return left->definition();
1432 } else if (value == -1) {
1433 return right->definition();
1434 }
1435 break;
1436 case Token::kBIT_OR:
1437 ASSERT(cid == kSmiCid);
1438 if (value == 0) {
1439 return right->definition();
1440 } else if (value == -1) {
1441 return left->definition();
1442 }
1443 break;
1444 case Token::kBIT_XOR:
1445 ASSERT(cid == kSmiCid);
1446 if (value == 0) {
1447 return right->definition();
1448 }
1449 break;
1450 default:
1451 break;
1452 }
1453
1454 return NULL;
1455 }
1456
1457
1458 Definition* BinaryDoubleOpInstr::Canonicalize(FlowGraphOptimizer* optimizer) {
1459 Definition* result = NULL;
1460
Florian Schneider 2013/01/08 12:15:47 Remove extra \n.
1461
1462 result = CanonicalizeCommutativeArithmetic(op_kind(),
1463 kDoubleCid,
1464 left(),
1465 right());
1466 if (result != NULL) {
1467 return result;
1468 }
1469
1470 result = CanonicalizeCommutativeArithmetic(op_kind(),
1471 kDoubleCid,
1472 right(),
1473 left());
1474 if (result != NULL) {
1475 return result;
1476 }
1477
1478 return this;
1479 }
1480
1481
1482 Definition* BinarySmiOpInstr::Canonicalize(FlowGraphOptimizer* optimizer) {
1483 Definition* result = NULL;
1484
Florian Schneider 2013/01/08 12:15:47 Remove extra \n.
1485
1486 result = CanonicalizeCommutativeArithmetic(op_kind(),
1487 kSmiCid,
1488 left(),
1489 right());
1490 if (result != NULL) {
1491 return result;
1492 }
1493
1494 result = CanonicalizeCommutativeArithmetic(op_kind(),
1495 kSmiCid,
1496 right(),
1497 left());
1498 if (result != NULL) {
1499 return result;
1500 }
1501
1502 return this;
1503 }
1504
1505
1383 RawAbstractType* MathSqrtInstr::CompileType() const { 1506 RawAbstractType* MathSqrtInstr::CompileType() const {
1384 return Type::Double(); 1507 return Type::Double();
1385 } 1508 }
1386 1509
1387 1510
1388 RawAbstractType* UnboxDoubleInstr::CompileType() const { 1511 RawAbstractType* UnboxDoubleInstr::CompileType() const {
1389 return Type::null(); 1512 return Type::null();
1390 } 1513 }
1391 1514
1392 1515
(...skipping 1386 matching lines...) Expand 10 before | Expand all | Expand 10 after
2779 default: 2902 default:
2780 UNREACHABLE(); 2903 UNREACHABLE();
2781 return -1; 2904 return -1;
2782 } 2905 }
2783 } 2906 }
2784 2907
2785 2908
2786 #undef __ 2909 #undef __
2787 2910
2788 } // namespace dart 2911 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/intermediate_language.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698