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

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

Issue 166793002: Fixed and improved code for integral division. Fixed and extended tests. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: A64 fixes and cleanup Created 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 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 2564 matching lines...) Expand 10 before | Expand all | Expand 10 after
2575 type = Deoptimizer::LAZY; 2575 type = Deoptimizer::LAZY;
2576 } 2576 }
2577 2577
2578 Comment(";;; deoptimize: %s", instr->hydrogen()->reason()); 2578 Comment(";;; deoptimize: %s", instr->hydrogen()->reason());
2579 DeoptimizeHeader(instr->environment(), &type); 2579 DeoptimizeHeader(instr->environment(), &type);
2580 Deoptimize(instr->environment(), type); 2580 Deoptimize(instr->environment(), type);
2581 } 2581 }
2582 2582
2583 2583
2584 void LCodeGen::DoDivI(LDivI* instr) { 2584 void LCodeGen::DoDivI(LDivI* instr) {
2585 if (!instr->is_flooring() && instr->hydrogen()->RightIsPowerOf2()) {
2586 HDiv* hdiv = instr->hydrogen();
2587 Register dividend = ToRegister32(instr->left());
2588 int32_t divisor = hdiv->right()->GetInteger32Constant();
2589 Register result = ToRegister32(instr->result());
2590 ASSERT(!result.is(dividend));
2591
2592 // Check for (0 / -x) that will produce negative zero.
2593 if (hdiv->left()->RangeCanInclude(0) && divisor < 0 &&
2594 hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
2595 __ Cmp(dividend, 0);
2596 DeoptimizeIf(eq, instr->environment());
2597 }
2598 // Check for (kMinInt / -1).
2599 if (hdiv->left()->RangeCanInclude(kMinInt) && divisor == -1 &&
2600 hdiv->CheckFlag(HValue::kCanOverflow)) {
2601 __ Cmp(dividend, kMinInt);
2602 DeoptimizeIf(eq, instr->environment());
2603 }
2604 // Deoptimize if remainder will not be 0.
2605 if (!hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32) &&
2606 Abs(divisor) != 1) {
2607 __ Tst(dividend, Abs(divisor) - 1);
2608 DeoptimizeIf(ne, instr->environment());
2609 }
2610 if (divisor == -1) { // Nice shortcut, not needed for correctness.
2611 __ Neg(result, dividend);
2612 return;
2613 }
2614 int32_t shift = WhichPowerOf2(Abs(divisor));
2615 if (shift == 0) {
2616 __ Mov(result, dividend);
2617 } else if (shift == 1) {
2618 __ Add(result, dividend, Operand(dividend, LSR, 31));
2619 } else {
2620 __ Mov(result, Operand(dividend, ASR, 31));
2621 __ Mov(result, Operand(result, LSR, 32 - shift));
2622 __ Add(result, result, dividend);
Benedikt Meurer 2014/02/18 09:43:30 Please merge the last Mov and the Add.
2623 }
2624 if (shift > 0) __ Mov(result, Operand(result, ASR, shift));
2625 if (divisor < 0) __ Neg(result, result);
2626 return;
2627 }
2628
2585 Register dividend = ToRegister32(instr->left()); 2629 Register dividend = ToRegister32(instr->left());
2630 Register divisor = ToRegister32(instr->right());
2586 Register result = ToRegister32(instr->result()); 2631 Register result = ToRegister32(instr->result());
2632 HValue* hdiv = instr->hydrogen_value();
2587 2633
2588 bool has_power_of_2_divisor = instr->hydrogen()->RightIsPowerOf2(); 2634 // Issue the division first, and then check for any deopt cases whilst the
2589 bool can_overflow = instr->hydrogen()->CheckFlag(HValue::kCanOverflow); 2635 // result is computed.
2590 bool bailout_on_minus_zero = 2636 __ Sdiv(result, dividend, divisor);
2591 instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero);
2592 bool can_be_div_by_zero =
2593 instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero);
2594 bool all_uses_truncating_to_int32 =
2595 instr->hydrogen()->CheckFlag(HInstruction::kAllUsesTruncatingToInt32);
2596 2637
2597 if (has_power_of_2_divisor) { 2638 if (hdiv->CheckFlag(HInstruction::kAllUsesTruncatingToInt32)) {
2598 ASSERT(instr->temp() == NULL); 2639 ASSERT_EQ(NULL, instr->temp());
2599 int32_t divisor = ToInteger32(LConstantOperand::cast(instr->right())); 2640 return;
2600 int32_t power; 2641 }
2601 int32_t power_mask;
2602 Label deopt, done;
2603 2642
2604 ASSERT(divisor != 0); 2643 Label deopt;
2605 if (divisor > 0) { 2644 // Check for x / 0.
2606 power = WhichPowerOf2(divisor); 2645 if (hdiv->CheckFlag(HValue::kCanBeDivByZero)) {
2607 power_mask = divisor - 1; 2646 __ Cbz(divisor, &deopt);
2608 } else { 2647 }
2609 // Check for (0 / -x) as that will produce negative zero.
2610 if (bailout_on_minus_zero) {
2611 if (all_uses_truncating_to_int32) {
2612 // If all uses truncate, and the dividend is zero, the truncated
2613 // result is zero.
2614 __ Mov(result, 0);
2615 __ Cbz(dividend, &done);
2616 } else {
2617 __ Cbz(dividend, &deopt);
2618 }
2619 }
2620 // Check for (kMinInt / -1).
2621 if ((divisor == -1) && can_overflow && !all_uses_truncating_to_int32) {
2622 // Check for kMinInt by subtracting one and checking for overflow.
2623 __ Cmp(dividend, 1);
2624 __ B(vs, &deopt);
2625 }
2626 power = WhichPowerOf2(-divisor);
2627 power_mask = -divisor - 1;
2628 }
2629 2648
2630 if (power_mask != 0) { 2649 // Check for (0 / -x) as that will produce negative zero.
2631 if (all_uses_truncating_to_int32) { 2650 if (hdiv->CheckFlag(HValue::kBailoutOnMinusZero)) {
2632 __ Cmp(dividend, 0); 2651 __ Cmp(divisor, 0);
2633 __ Cneg(result, dividend, lt);
2634 __ Asr(result, result, power);
2635 if (divisor > 0) __ Cneg(result, result, lt);
2636 if (divisor < 0) __ Cneg(result, result, gt);
2637 return; // Don't fall through to negation below.
2638 } else {
2639 // Deoptimize if remainder is not 0. If the least-significant
2640 // power bits aren't 0, it's not a multiple of 2^power, and
2641 // therefore, there will be a remainder.
2642 __ TestAndBranchIfAnySet(dividend, power_mask, &deopt);
2643 __ Asr(result, dividend, power);
2644 if (divisor < 0) __ Neg(result, result);
2645 }
2646 } else {
2647 ASSERT((divisor == 1) || (divisor == -1));
2648 if (divisor < 0) {
2649 __ Neg(result, dividend);
2650 } else {
2651 __ Mov(result, dividend);
2652 }
2653 }
2654 __ B(&done);
2655 __ Bind(&deopt);
2656 Deoptimize(instr->environment());
2657 __ Bind(&done);
2658 } else {
2659 Register divisor = ToRegister32(instr->right());
2660 2652
2661 // Issue the division first, and then check for any deopt cases whilst the 2653 // If the divisor < 0 (mi), compare the dividend, and deopt if it is
2662 // result is computed. 2654 // zero, ie. zero dividend with negative divisor deopts.
2663 __ Sdiv(result, dividend, divisor); 2655 // If the divisor >= 0 (pl, the opposite of mi) set the flags to
2656 // condition ne, so we don't deopt, ie. positive divisor doesn't deopt.
2657 __ Ccmp(dividend, 0, NoFlag, mi);
2658 __ B(eq, &deopt);
2659 }
2664 2660
2665 if (!all_uses_truncating_to_int32) { 2661 // Check for (kMinInt / -1).
2666 Label deopt; 2662 if (hdiv->CheckFlag(HValue::kCanOverflow)) {
2667 // Check for x / 0. 2663 // Test dividend for kMinInt by subtracting one (cmp) and checking for
2668 if (can_be_div_by_zero) { 2664 // overflow.
2669 __ Cbz(divisor, &deopt); 2665 __ Cmp(dividend, 1);
2670 } 2666 // If overflow is set, ie. dividend = kMinInt, compare the divisor with
2667 // -1. If overflow is clear, set the flags for condition ne, as the
2668 // dividend isn't -1, and thus we shouldn't deopt.
2669 __ Ccmp(divisor, -1, NoFlag, vs);
2670 __ B(eq, &deopt);
2671 }
2671 2672
2672 // Check for (0 / -x) as that will produce negative zero. 2673 // Compute remainder and deopt if it's not zero.
2673 if (bailout_on_minus_zero) { 2674 Register remainder = ToRegister32(instr->temp());
2674 __ Cmp(divisor, 0); 2675 __ Msub(remainder, result, divisor, dividend);
2676 __ Cbnz(remainder, &deopt);
2675 2677
2676 // If the divisor < 0 (mi), compare the dividend, and deopt if it is 2678 Label div_ok;
2677 // zero, ie. zero dividend with negative divisor deopts. 2679 __ B(&div_ok);
2678 // If the divisor >= 0 (pl, the opposite of mi) set the flags to 2680 __ Bind(&deopt);
2679 // condition ne, so we don't deopt, ie. positive divisor doesn't deopt. 2681 Deoptimize(instr->environment());
2680 __ Ccmp(dividend, 0, NoFlag, mi); 2682 __ Bind(&div_ok);
2681 __ B(eq, &deopt);
2682 }
2683
2684 // Check for (kMinInt / -1).
2685 if (can_overflow) {
2686 // Test dividend for kMinInt by subtracting one (cmp) and checking for
2687 // overflow.
2688 __ Cmp(dividend, 1);
2689 // If overflow is set, ie. dividend = kMinInt, compare the divisor with
2690 // -1. If overflow is clear, set the flags for condition ne, as the
2691 // dividend isn't -1, and thus we shouldn't deopt.
2692 __ Ccmp(divisor, -1, NoFlag, vs);
2693 __ B(eq, &deopt);
2694 }
2695
2696 // Compute remainder and deopt if it's not zero.
2697 Register remainder = ToRegister32(instr->temp());
2698 __ Msub(remainder, result, divisor, dividend);
2699 __ Cbnz(remainder, &deopt);
2700
2701 Label div_ok;
2702 __ B(&div_ok);
2703 __ Bind(&deopt);
2704 Deoptimize(instr->environment());
2705 __ Bind(&div_ok);
2706 } else {
2707 ASSERT(instr->temp() == NULL);
2708 }
2709 }
2710 } 2683 }
2711 2684
2712 2685
2713 void LCodeGen::DoDoubleToIntOrSmi(LDoubleToIntOrSmi* instr) { 2686 void LCodeGen::DoDoubleToIntOrSmi(LDoubleToIntOrSmi* instr) {
2714 DoubleRegister input = ToDoubleRegister(instr->value()); 2687 DoubleRegister input = ToDoubleRegister(instr->value());
2715 Register result = ToRegister32(instr->result()); 2688 Register result = ToRegister32(instr->result());
2716 Label done, deopt; 2689 Label done, deopt;
2717 2690
2718 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { 2691 if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
2719 __ JumpIfMinusZero(input, &deopt); 2692 __ JumpIfMinusZero(input, &deopt);
(...skipping 2994 matching lines...) Expand 10 before | Expand all | Expand 10 after
5714 __ Bind(&out_of_object); 5687 __ Bind(&out_of_object);
5715 __ Ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset)); 5688 __ Ldr(result, FieldMemOperand(object, JSObject::kPropertiesOffset));
5716 // Index is equal to negated out of object property index plus 1. 5689 // Index is equal to negated out of object property index plus 1.
5717 __ Sub(result, result, Operand::UntagSmiAndScale(index, kPointerSizeLog2)); 5690 __ Sub(result, result, Operand::UntagSmiAndScale(index, kPointerSizeLog2));
5718 __ Ldr(result, FieldMemOperand(result, 5691 __ Ldr(result, FieldMemOperand(result,
5719 FixedArray::kHeaderSize - kPointerSize)); 5692 FixedArray::kHeaderSize - kPointerSize));
5720 __ Bind(&done); 5693 __ Bind(&done);
5721 } 5694 }
5722 5695
5723 } } // namespace v8::internal 5696 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/a64/lithium-a64.cc ('k') | src/arm/lithium-arm.cc » ('j') | src/arm/lithium-codegen-arm.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698