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

Side by Side Diff: src/ia32/lithium-codegen-ia32.cc

Issue 190383002: Handle non-power-of-2 divisors in division-like operations (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Rebased Created 6 years, 9 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 | « src/assembler.cc ('k') | src/ia32/lithium-ia32.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 1383 matching lines...) Expand 10 before | Expand all | Expand 10 after
1394 } 1394 }
1395 __ jmp(&done, Label::kNear); 1395 __ jmp(&done, Label::kNear);
1396 } 1396 }
1397 1397
1398 __ bind(&dividend_is_not_negative); 1398 __ bind(&dividend_is_not_negative);
1399 __ and_(dividend, mask); 1399 __ and_(dividend, mask);
1400 __ bind(&done); 1400 __ bind(&done);
1401 } 1401 }
1402 1402
1403 1403
1404 void LCodeGen::DoModByConstI(LModByConstI* instr) {
1405 Register dividend = ToRegister(instr->dividend());
1406 int32_t divisor = instr->divisor();
1407 ASSERT(ToRegister(instr->result()).is(eax));
1408
1409 if (divisor == 0) {
1410 DeoptimizeIf(no_condition, instr->environment());
1411 return;
1412 }
1413
1414 __ FlooringDiv(dividend, Abs(divisor));
1415 __ imul(edx, edx, Abs(divisor));
1416 __ mov(eax, dividend);
1417 __ sub(eax, edx);
1418
1419 // Check for negative zero.
1420 HMod* hmod = instr->hydrogen();
1421 if (hmod->CheckFlag(HValue::kBailoutOnMinusZero) &&
1422 hmod->left()->CanBeNegative()) {
1423 Label remainder_not_zero;
1424 __ j(not_zero, &remainder_not_zero, Label::kNear);
1425 __ cmp(dividend, Immediate(0));
1426 DeoptimizeIf(less, instr->environment());
1427 __ bind(&remainder_not_zero);
1428 }
1429 }
1430
1431
1404 void LCodeGen::DoModI(LModI* instr) { 1432 void LCodeGen::DoModI(LModI* instr) {
1405 HMod* hmod = instr->hydrogen(); 1433 HMod* hmod = instr->hydrogen();
1406 HValue* left = hmod->left(); 1434 HValue* left = hmod->left();
1407 HValue* right = hmod->right(); 1435 HValue* right = hmod->right();
1408 1436
1409 Register left_reg = ToRegister(instr->left()); 1437 Register left_reg = ToRegister(instr->left());
1410 ASSERT(left_reg.is(eax)); 1438 ASSERT(left_reg.is(eax));
1411 Register right_reg = ToRegister(instr->right()); 1439 Register right_reg = ToRegister(instr->right());
1412 ASSERT(!right_reg.is(eax)); 1440 ASSERT(!right_reg.is(eax));
1413 ASSERT(!right_reg.is(edx)); 1441 ASSERT(!right_reg.is(edx));
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1493 // The arithmetic shift is always OK, the 'if' is an optimization only. 1521 // The arithmetic shift is always OK, the 'if' is an optimization only.
1494 if (shift > 1) __ sar(result, 31); 1522 if (shift > 1) __ sar(result, 31);
1495 __ shr(result, 32 - shift); 1523 __ shr(result, 32 - shift);
1496 __ add(result, dividend); 1524 __ add(result, dividend);
1497 __ sar(result, shift); 1525 __ sar(result, shift);
1498 } 1526 }
1499 if (divisor < 0) __ neg(result); 1527 if (divisor < 0) __ neg(result);
1500 } 1528 }
1501 1529
1502 1530
1531 void LCodeGen::DoDivByConstI(LDivByConstI* instr) {
1532 Register dividend = ToRegister(instr->dividend());
1533 int32_t divisor = instr->divisor();
1534 ASSERT(ToRegister(instr->result()).is(edx));
1535
1536 if (divisor == 0) {
1537 DeoptimizeIf(no_condition, instr->environment());
1538 return;
1539 }
1540
1541 // Check for (0 / -x) that will produce negative zero.
1542 HDiv* hdiv = instr->hydrogen();
1543 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) &&
1544 hdiv->left()->RangeCanInclude(0) && divisor < 0) {
1545 __ test(dividend, dividend);
1546 DeoptimizeIf(zero, instr->environment());
1547 }
1548
1549 __ FlooringDiv(dividend, Abs(divisor));
1550 if (divisor < 0) __ neg(edx);
1551
1552 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
1553 __ mov(eax, edx);
1554 __ imul(eax, eax, divisor);
1555 __ sub(eax, dividend);
1556 DeoptimizeIf(not_equal, instr->environment());
1557 }
1558 }
1559
1560
1503 void LCodeGen::DoDivI(LDivI* instr) { 1561 void LCodeGen::DoDivI(LDivI* instr) {
1504 Register dividend = ToRegister(instr->left()); 1562 Register dividend = ToRegister(instr->left());
1505 Register divisor = ToRegister(instr->right()); 1563 Register divisor = ToRegister(instr->right());
1506 Register remainder = ToRegister(instr->temp()); 1564 Register remainder = ToRegister(instr->temp());
1507 Register result = ToRegister(instr->result()); 1565 Register result = ToRegister(instr->result());
1508 ASSERT(dividend.is(eax)); 1566 ASSERT(dividend.is(eax));
1509 ASSERT(remainder.is(edx)); 1567 ASSERT(remainder.is(edx));
1510 ASSERT(result.is(eax)); 1568 ASSERT(result.is(eax));
1511 ASSERT(!divisor.is(eax)); 1569 ASSERT(!divisor.is(eax));
1512 ASSERT(!divisor.is(edx)); 1570 ASSERT(!divisor.is(edx));
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1592 } 1650 }
1593 __ bind(&not_kmin_int); 1651 __ bind(&not_kmin_int);
1594 __ sar(dividend, shift); 1652 __ sar(dividend, shift);
1595 __ bind(&done); 1653 __ bind(&done);
1596 } 1654 }
1597 1655
1598 1656
1599 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) { 1657 void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) {
1600 Register dividend = ToRegister(instr->dividend()); 1658 Register dividend = ToRegister(instr->dividend());
1601 int32_t divisor = instr->divisor(); 1659 int32_t divisor = instr->divisor();
1602 Register scratch = ToRegister(instr->temp());
1603 ASSERT(ToRegister(instr->dividend()).is(eax));
1604 ASSERT(ToRegister(instr->result()).is(edx)); 1660 ASSERT(ToRegister(instr->result()).is(edx));
1605 1661
1606 if (divisor == 0) { 1662 if (divisor == 0) {
1607 DeoptimizeIf(no_condition, instr->environment()); 1663 DeoptimizeIf(no_condition, instr->environment());
1608 return; 1664 return;
1609 } 1665 }
1610 1666
1611 // Find b which: 2^b < divisor_abs < 2^(b+1). 1667 // Check for (0 / -x) that will produce negative zero.
1612 uint32_t divisor_abs = abs(divisor); 1668 HMathFloorOfDiv* hdiv = instr->hydrogen();
1613 unsigned b = 31 - CompilerIntrinsics::CountLeadingZeros(divisor_abs); 1669 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero) &&
1614 unsigned shift = 32 + b; // Precision +1bit (effectively). 1670 hdiv->left()->RangeCanInclude(0) && divisor < 0) {
1615 double multiplier_f =
1616 static_cast<double>(static_cast<uint64_t>(1) << shift) / divisor_abs;
1617 int64_t multiplier;
1618 if (multiplier_f - std::floor(multiplier_f) < 0.5) {
1619 multiplier = static_cast<int64_t>(std::floor(multiplier_f));
1620 } else {
1621 multiplier = static_cast<int64_t>(std::floor(multiplier_f)) + 1;
1622 }
1623 // The multiplier is a uint32.
1624 ASSERT(multiplier > 0 &&
1625 multiplier < (static_cast<int64_t>(1) << 32));
1626 __ mov(scratch, dividend);
1627 if (divisor < 0 &&
1628 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1629 __ test(dividend, dividend); 1671 __ test(dividend, dividend);
1630 DeoptimizeIf(zero, instr->environment()); 1672 DeoptimizeIf(zero, instr->environment());
1631 } 1673 }
1632 __ mov(edx, static_cast<int32_t>(multiplier)); 1674
1633 __ imul(edx); 1675 __ FlooringDiv(dividend, divisor);
1634 if (static_cast<int32_t>(multiplier) < 0) {
1635 __ add(edx, scratch);
1636 }
1637 Register reg_lo = eax;
1638 Register reg_byte_scratch = scratch;
1639 if (!reg_byte_scratch.is_byte_register()) {
1640 __ xchg(reg_lo, reg_byte_scratch);
1641 reg_lo = scratch;
1642 reg_byte_scratch = eax;
1643 }
1644 if (divisor < 0) {
1645 __ xor_(reg_byte_scratch, reg_byte_scratch);
1646 __ cmp(reg_lo, 0x40000000);
1647 __ setcc(above, reg_byte_scratch);
1648 __ neg(edx);
1649 __ sub(edx, reg_byte_scratch);
1650 } else {
1651 __ xor_(reg_byte_scratch, reg_byte_scratch);
1652 __ cmp(reg_lo, 0xC0000000);
1653 __ setcc(above_equal, reg_byte_scratch);
1654 __ add(edx, reg_byte_scratch);
1655 }
1656 __ sar(edx, shift - 32);
1657 } 1676 }
1658 1677
1659 1678
1660 void LCodeGen::DoMulI(LMulI* instr) { 1679 void LCodeGen::DoMulI(LMulI* instr) {
1661 Register left = ToRegister(instr->left()); 1680 Register left = ToRegister(instr->left());
1662 LOperand* right = instr->right(); 1681 LOperand* right = instr->right();
1663 1682
1664 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { 1683 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
1665 __ mov(ToRegister(instr->temp()), left); 1684 __ mov(ToRegister(instr->temp()), left);
1666 } 1685 }
(...skipping 4622 matching lines...) Expand 10 before | Expand all | Expand 10 after
6289 FixedArray::kHeaderSize - kPointerSize)); 6308 FixedArray::kHeaderSize - kPointerSize));
6290 __ bind(&done); 6309 __ bind(&done);
6291 } 6310 }
6292 6311
6293 6312
6294 #undef __ 6313 #undef __
6295 6314
6296 } } // namespace v8::internal 6315 } } // namespace v8::internal
6297 6316
6298 #endif // V8_TARGET_ARCH_IA32 6317 #endif // V8_TARGET_ARCH_IA32
OLDNEW
« no previous file with comments | « src/assembler.cc ('k') | src/ia32/lithium-ia32.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698