Revision: 20123
Author:   svenpa...@chromium.org
Date:     Thu Mar 20 13:10:23 2014 UTC
Log: Implement flooring division by a constant via truncating division by a constant.

R=bmeu...@chromium.org

Review URL: https://codereview.chromium.org/204583002
http://code.google.com/p/v8/source/detail?r=20123

Modified:
 /branches/bleeding_edge/src/a64/lithium-a64.cc
 /branches/bleeding_edge/src/a64/lithium-a64.h
 /branches/bleeding_edge/src/a64/lithium-codegen-a64.cc
 /branches/bleeding_edge/src/arm/lithium-arm.cc
 /branches/bleeding_edge/src/arm/lithium-arm.h
 /branches/bleeding_edge/src/arm/lithium-codegen-arm.cc
 /branches/bleeding_edge/src/hydrogen-instructions.cc
 /branches/bleeding_edge/src/hydrogen-instructions.h
 /branches/bleeding_edge/src/ia32/lithium-codegen-ia32.cc
 /branches/bleeding_edge/src/ia32/lithium-ia32.cc
 /branches/bleeding_edge/src/ia32/lithium-ia32.h
 /branches/bleeding_edge/src/x64/lithium-codegen-x64.cc
 /branches/bleeding_edge/src/x64/lithium-x64.cc
 /branches/bleeding_edge/src/x64/lithium-x64.h

=======================================
--- /branches/bleeding_edge/src/a64/lithium-a64.cc Tue Mar 18 15:01:55 2014 UTC +++ /branches/bleeding_edge/src/a64/lithium-a64.cc Thu Mar 20 13:10:23 2014 UTC
@@ -1762,12 +1762,17 @@
   ASSERT(instr->right()->representation().Equals(instr->representation()));
   LOperand* dividend = UseRegister(instr->left());
   int32_t divisor = instr->right()->GetInteger32Constant();
-  LInstruction* result =
- DefineAsRegister(new(zone()) LFlooringDivByConstI(dividend, divisor));
-  bool can_deopt =
-      divisor == 0 ||
-      (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0);
-  return can_deopt ? AssignEnvironment(result) : result;
+  LOperand* temp =
+      ((divisor > 0 && !instr->CheckFlag(HValue::kLeftCanBeNegative)) ||
+       (divisor < 0 && !instr->CheckFlag(HValue::kLeftCanBePositive))) ?
+      NULL : TempRegister();
+  LInstruction* result = DefineAsRegister(
+      new(zone()) LFlooringDivByConstI(dividend, divisor, temp));
+  if (divisor == 0 ||
+      (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0)) {
+    result = AssignEnvironment(result);
+  }
+  return result;
 }


@@ -1784,8 +1789,8 @@
 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
   if (instr->RightIsPowerOf2()) {
     return DoFlooringDivByPowerOf2I(instr);
-  } else if (false && instr->right()->IsConstant()) {
- return DoFlooringDivByConstI(instr); // TODO(svenpanne) Fix and re-enable.
+  } else if (instr->right()->IsConstant()) {
+    return DoFlooringDivByConstI(instr);
   } else {
     return DoFlooringDivI(instr);
   }
=======================================
--- /branches/bleeding_edge/src/a64/lithium-a64.h Tue Mar 18 15:01:55 2014 UTC +++ /branches/bleeding_edge/src/a64/lithium-a64.h Thu Mar 20 13:10:23 2014 UTC
@@ -1956,16 +1956,17 @@
 };


-class LFlooringDivByConstI V8_FINAL : public LTemplateInstruction<1, 1, 0> { +class LFlooringDivByConstI V8_FINAL : public LTemplateInstruction<1, 1, 2> {
  public:
-  LFlooringDivByConstI(LOperand* dividend, int32_t divisor) {
+ LFlooringDivByConstI(LOperand* dividend, int32_t divisor, LOperand* temp) {
     inputs_[0] = dividend;
     divisor_ = divisor;
+    temps_[0] = temp;
   }

   LOperand* dividend() { return inputs_[0]; }
   int32_t divisor() const { return divisor_; }
-  LOperand* temp1() { return temps_[0]; }
+  LOperand* temp() { return temps_[0]; }

DECLARE_CONCRETE_INSTRUCTION(FlooringDivByConstI, "flooring-div-by-const-i")
   DECLARE_HYDROGEN_ACCESSOR(MathFloorOfDiv)
=======================================
--- /branches/bleeding_edge/src/a64/lithium-codegen-a64.cc Thu Mar 20 10:37:19 2014 UTC +++ /branches/bleeding_edge/src/a64/lithium-codegen-a64.cc Thu Mar 20 13:10:23 2014 UTC
@@ -3891,8 +3891,31 @@
     DeoptimizeIf(eq, instr->environment());
   }

-  // TODO(svenpanne) Add correction terms.
-  __ TruncatingDiv(result, dividend, divisor);
+  // Easy case: We need no dynamic check for the dividend and the flooring
+  // division is the same as the truncating division.
+  if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
+      (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
+    __ TruncatingDiv(result, dividend, Abs(divisor));
+    if (divisor < 0) __ Neg(result, result);
+    return;
+  }
+
+ // In the general case we may need to adjust before and after the truncating
+  // division to get a flooring division.
+  Register temp = ToRegister32(instr->temp());
+  ASSERT(!AreAliased(temp, dividend, result));
+  Label needs_adjustment, done;
+  __ Cmp(dividend, 0);
+  __ B(divisor > 0 ? lt : gt, &needs_adjustment);
+  __ TruncatingDiv(result, dividend, Abs(divisor));
+  if (divisor < 0) __ Neg(result, result);
+  __ B(&done);
+  __ bind(&needs_adjustment);
+  __ Add(temp, dividend, Operand(divisor > 0 ? 1 : -1));
+  __ TruncatingDiv(result, temp, Abs(divisor));
+  if (divisor < 0) __ Neg(result, result);
+  __ Sub(result, result, Operand(1));
+  __ bind(&done);
 }


=======================================
--- /branches/bleeding_edge/src/arm/lithium-arm.cc Thu Mar 13 07:58:58 2014 UTC +++ /branches/bleeding_edge/src/arm/lithium-arm.cc Thu Mar 20 13:10:23 2014 UTC
@@ -1332,20 +1332,25 @@
   ASSERT(instr->right()->representation().Equals(instr->representation()));
   LOperand* dividend = UseRegister(instr->left());
   int32_t divisor = instr->right()->GetInteger32Constant();
-  LInstruction* result =
- DefineAsRegister(new(zone()) LFlooringDivByConstI(dividend, divisor));
-  bool can_deopt =
-      divisor == 0 ||
-      (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0);
-  return can_deopt ? AssignEnvironment(result) : result;
+  LOperand* temp =
+      ((divisor > 0 && !instr->CheckFlag(HValue::kLeftCanBeNegative)) ||
+       (divisor < 0 && !instr->CheckFlag(HValue::kLeftCanBePositive))) ?
+      NULL : TempRegister();
+  LInstruction* result = DefineAsRegister(
+      new(zone()) LFlooringDivByConstI(dividend, divisor, temp));
+  if (divisor == 0 ||
+      (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0)) {
+    result = AssignEnvironment(result);
+  }
+  return result;
 }


 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
   if (instr->RightIsPowerOf2()) {
     return DoFlooringDivByPowerOf2I(instr);
-  } else if (false && instr->right()->IsConstant()) {
- return DoFlooringDivByConstI(instr); // TODO(svenpanne) Fix and re-enable.
+  } else if (instr->right()->IsConstant()) {
+    return DoFlooringDivByConstI(instr);
   } else {
     return DoDivI(instr);
   }
=======================================
--- /branches/bleeding_edge/src/arm/lithium-arm.h Thu Mar 13 06:11:52 2014 UTC +++ /branches/bleeding_edge/src/arm/lithium-arm.h Thu Mar 20 13:10:23 2014 UTC
@@ -747,16 +747,17 @@
 };


-class LFlooringDivByConstI V8_FINAL : public LTemplateInstruction<1, 1, 0> { +class LFlooringDivByConstI V8_FINAL : public LTemplateInstruction<1, 1, 2> {
  public:
-  LFlooringDivByConstI(LOperand* dividend, int32_t divisor) {
+ LFlooringDivByConstI(LOperand* dividend, int32_t divisor, LOperand* temp) {
     inputs_[0] = dividend;
     divisor_ = divisor;
+    temps_[0] = temp;
   }

   LOperand* dividend() { return inputs_[0]; }
   int32_t divisor() const { return divisor_; }
-  LOperand* temp1() { return temps_[0]; }
+  LOperand* temp() { return temps_[0]; }

DECLARE_CONCRETE_INSTRUCTION(FlooringDivByConstI, "flooring-div-by-const-i")
   DECLARE_HYDROGEN_ACCESSOR(MathFloorOfDiv)
=======================================
--- /branches/bleeding_edge/src/arm/lithium-codegen-arm.cc Wed Mar 19 11:51:30 2014 UTC +++ /branches/bleeding_edge/src/arm/lithium-codegen-arm.cc Thu Mar 20 13:10:23 2014 UTC
@@ -1507,8 +1507,31 @@
     DeoptimizeIf(eq, instr->environment());
   }

-  // TODO(svenpanne) Add correction terms.
-  __ TruncatingDiv(result, dividend, divisor);
+  // Easy case: We need no dynamic check for the dividend and the flooring
+  // division is the same as the truncating division.
+  if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
+      (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
+    __ TruncatingDiv(result, dividend, Abs(divisor));
+    if (divisor < 0) __ rsb(result, result, Operand::Zero());
+    return;
+  }
+
+ // In the general case we may need to adjust before and after the truncating
+  // division to get a flooring division.
+  Register temp = ToRegister(instr->temp());
+  ASSERT(!temp.is(dividend) && !temp.is(result));
+  Label needs_adjustment, done;
+  __ cmp(dividend, Operand::Zero());
+  __ b(divisor > 0 ? lt : gt, &needs_adjustment);
+  __ TruncatingDiv(result, dividend, Abs(divisor));
+  if (divisor < 0) __ rsb(result, result, Operand::Zero());
+  __ jmp(&done);
+  __ bind(&needs_adjustment);
+  __ add(temp, dividend, Operand(divisor > 0 ? 1 : -1));
+  __ TruncatingDiv(result, temp, Abs(divisor));
+  if (divisor < 0) __ rsb(result, result, Operand::Zero());
+  __ sub(result, result, Operand(1));
+  __ bind(&done);
 }


=======================================
--- /branches/bleeding_edge/src/hydrogen-instructions.cc Mon Mar 17 09:11:38 2014 UTC +++ /branches/bleeding_edge/src/hydrogen-instructions.cc Thu Mar 20 13:10:23 2014 UTC
@@ -1832,6 +1832,14 @@
     if (!a->Includes(kMinInt)) {
       ClearFlag(kLeftCanBeMinInt);
     }
+
+    if (!a->CanBeNegative()) {
+      ClearFlag(HValue::kLeftCanBeNegative);
+    }
+
+    if (!a->CanBePositive()) {
+      ClearFlag(HValue::kLeftCanBePositive);
+    }

     if (!a->Includes(kMinInt) || !b->Includes(-1)) {
       ClearFlag(kCanOverflow);
=======================================
--- /branches/bleeding_edge/src/hydrogen-instructions.h Mon Mar 17 09:11:38 2014 UTC +++ /branches/bleeding_edge/src/hydrogen-instructions.h Thu Mar 20 13:10:23 2014 UTC
@@ -624,6 +624,7 @@
     kCanBeDivByZero,
     kLeftCanBeMinInt,
     kLeftCanBeNegative,
+    kLeftCanBePositive,
     kAllowUndefinedAsNaN,
     kIsArguments,
     kTruncatingToInt32,
@@ -4105,6 +4106,8 @@
     SetFlag(kCanOverflow);
     SetFlag(kCanBeDivByZero);
     SetFlag(kLeftCanBeMinInt);
+    SetFlag(kLeftCanBeNegative);
+    SetFlag(kLeftCanBePositive);
     SetFlag(kAllowUndefinedAsNaN);
   }

=======================================
--- /branches/bleeding_edge/src/ia32/lithium-codegen-ia32.cc Wed Mar 19 11:51:30 2014 UTC +++ /branches/bleeding_edge/src/ia32/lithium-codegen-ia32.cc Thu Mar 20 13:10:23 2014 UTC
@@ -1665,8 +1665,31 @@
     DeoptimizeIf(zero, instr->environment());
   }

-  // TODO(svenpanne) Add correction terms.
-  __ TruncatingDiv(dividend, divisor);
+  // Easy case: We need no dynamic check for the dividend and the flooring
+  // division is the same as the truncating division.
+  if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
+      (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
+    __ TruncatingDiv(dividend, Abs(divisor));
+    if (divisor < 0) __ neg(edx);
+    return;
+  }
+
+ // In the general case we may need to adjust before and after the truncating
+  // division to get a flooring division.
+  Register temp = ToRegister(instr->temp3());
+  ASSERT(!temp.is(dividend) && !temp.is(eax) && !temp.is(edx));
+  Label needs_adjustment, done;
+  __ cmp(dividend, Immediate(0));
+  __ j(divisor > 0 ? less : greater, &needs_adjustment, Label::kNear);
+  __ TruncatingDiv(dividend, Abs(divisor));
+  if (divisor < 0) __ neg(edx);
+  __ jmp(&done, Label::kNear);
+  __ bind(&needs_adjustment);
+  __ lea(temp, Operand(dividend, divisor > 0 ? 1 : -1));
+  __ TruncatingDiv(temp, Abs(divisor));
+  if (divisor < 0) __ neg(edx);
+  __ dec(edx);
+  __ bind(&done);
 }


=======================================
--- /branches/bleeding_edge/src/ia32/lithium-ia32.cc Thu Mar 13 07:58:58 2014 UTC +++ /branches/bleeding_edge/src/ia32/lithium-ia32.cc Thu Mar 20 13:10:23 2014 UTC
@@ -1427,24 +1427,30 @@
   int32_t divisor = instr->right()->GetInteger32Constant();
   LOperand* temp1 = FixedTemp(eax);
   LOperand* temp2 = FixedTemp(edx);
+  LOperand* temp3 =
+      ((divisor > 0 && !instr->CheckFlag(HValue::kLeftCanBeNegative)) ||
+       (divisor < 0 && !instr->CheckFlag(HValue::kLeftCanBePositive))) ?
+      NULL : TempRegister();
   LInstruction* result =
       DefineFixed(new(zone()) LFlooringDivByConstI(dividend,
                                                    divisor,
                                                    temp1,
-                                                   temp2),
+                                                   temp2,
+                                                   temp3),
                   edx);
-  bool can_deopt =
-      divisor == 0 ||
-      (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0);
-  return can_deopt ? AssignEnvironment(result) : result;
+  if (divisor == 0 ||
+      (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0)) {
+    result = AssignEnvironment(result);
+  }
+  return result;
 }


 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
   if (instr->RightIsPowerOf2()) {
     return DoFlooringDivByPowerOf2I(instr);
-  } else if (false && instr->right()->IsConstant()) {
- return DoFlooringDivByConstI(instr); // TODO(svenpanne) Fix and re-enable.
+  } else if (instr->right()->IsConstant()) {
+    return DoFlooringDivByConstI(instr);
   } else {
     return DoDivI(instr);
   }
=======================================
--- /branches/bleeding_edge/src/ia32/lithium-ia32.h Thu Mar 13 06:11:52 2014 UTC +++ /branches/bleeding_edge/src/ia32/lithium-ia32.h Thu Mar 20 13:10:23 2014 UTC
@@ -778,22 +778,25 @@
 };


-class LFlooringDivByConstI V8_FINAL : public LTemplateInstruction<1, 1, 2> { +class LFlooringDivByConstI V8_FINAL : public LTemplateInstruction<1, 1, 3> {
  public:
   LFlooringDivByConstI(LOperand* dividend,
                        int32_t divisor,
                        LOperand* temp1,
-                       LOperand* temp2) {
+                       LOperand* temp2,
+                       LOperand* temp3) {
     inputs_[0] = dividend;
     divisor_ = divisor;
     temps_[0] = temp1;
     temps_[1] = temp2;
+    temps_[2] = temp3;
   }

   LOperand* dividend() { return inputs_[0]; }
   int32_t divisor() const { return divisor_; }
   LOperand* temp1() { return temps_[0]; }
   LOperand* temp2() { return temps_[1]; }
+  LOperand* temp3() { return temps_[2]; }

DECLARE_CONCRETE_INSTRUCTION(FlooringDivByConstI, "flooring-div-by-const-i")
   DECLARE_HYDROGEN_ACCESSOR(MathFloorOfDiv)
=======================================
--- /branches/bleeding_edge/src/x64/lithium-codegen-x64.cc Wed Mar 19 11:51:30 2014 UTC +++ /branches/bleeding_edge/src/x64/lithium-codegen-x64.cc Thu Mar 20 13:10:23 2014 UTC
@@ -1160,8 +1160,31 @@
     DeoptimizeIf(zero, instr->environment());
   }

-  // TODO(svenpanne) Add correction terms.
-  __ TruncatingDiv(dividend, divisor);
+  // Easy case: We need no dynamic check for the dividend and the flooring
+  // division is the same as the truncating division.
+  if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) ||
+      (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) {
+    __ TruncatingDiv(dividend, Abs(divisor));
+    if (divisor < 0) __ negl(rdx);
+    return;
+  }
+
+ // In the general case we may need to adjust before and after the truncating
+  // division to get a flooring division.
+  Register temp = ToRegister(instr->temp3());
+  ASSERT(!temp.is(dividend) && !temp.is(rax) && !temp.is(rdx));
+  Label needs_adjustment, done;
+  __ cmpl(dividend, Immediate(0));
+  __ j(divisor > 0 ? less : greater, &needs_adjustment, Label::kNear);
+  __ TruncatingDiv(dividend, Abs(divisor));
+  if (divisor < 0) __ negl(rdx);
+  __ jmp(&done, Label::kNear);
+  __ bind(&needs_adjustment);
+  __ leal(temp, Operand(dividend, divisor > 0 ? 1 : -1));
+  __ TruncatingDiv(temp, Abs(divisor));
+  if (divisor < 0) __ negl(rdx);
+  __ decl(rdx);
+  __ bind(&done);
 }


=======================================
--- /branches/bleeding_edge/src/x64/lithium-x64.cc Thu Mar 13 13:57:21 2014 UTC +++ /branches/bleeding_edge/src/x64/lithium-x64.cc Thu Mar 20 13:10:23 2014 UTC
@@ -1349,24 +1349,30 @@
   int32_t divisor = instr->right()->GetInteger32Constant();
   LOperand* temp1 = FixedTemp(rax);
   LOperand* temp2 = FixedTemp(rdx);
+  LOperand* temp3 =
+      ((divisor > 0 && !instr->CheckFlag(HValue::kLeftCanBeNegative)) ||
+       (divisor < 0 && !instr->CheckFlag(HValue::kLeftCanBePositive))) ?
+      NULL : TempRegister();
   LInstruction* result =
       DefineFixed(new(zone()) LFlooringDivByConstI(dividend,
                                                    divisor,
                                                    temp1,
-                                                   temp2),
+                                                   temp2,
+                                                   temp3),
                   rdx);
-  bool can_deopt =
-      divisor == 0 ||
-      (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0);
-  return can_deopt ? AssignEnvironment(result) : result;
+  if (divisor == 0 ||
+      (instr->CheckFlag(HValue::kBailoutOnMinusZero) && divisor < 0)) {
+    result = AssignEnvironment(result);
+  }
+  return result;
 }


 LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
   if (instr->RightIsPowerOf2()) {
     return DoFlooringDivByPowerOf2I(instr);
-  } else if (false && instr->right()->IsConstant()) {
- return DoFlooringDivByConstI(instr); // TODO(svenpanne) Fix and re-enable.
+  } else if (instr->right()->IsConstant()) {
+    return DoFlooringDivByConstI(instr);
   } else {
     return DoDivI(instr);
   }
=======================================
--- /branches/bleeding_edge/src/x64/lithium-x64.h Thu Mar 13 13:57:21 2014 UTC +++ /branches/bleeding_edge/src/x64/lithium-x64.h Thu Mar 20 13:10:23 2014 UTC
@@ -759,22 +759,25 @@
 };


-class LFlooringDivByConstI V8_FINAL : public LTemplateInstruction<1, 1, 2> { +class LFlooringDivByConstI V8_FINAL : public LTemplateInstruction<1, 1, 3> {
  public:
   LFlooringDivByConstI(LOperand* dividend,
                        int32_t divisor,
                        LOperand* temp1,
-                       LOperand* temp2) {
+                       LOperand* temp2,
+                       LOperand* temp3) {
     inputs_[0] = dividend;
     divisor_ = divisor;
     temps_[0] = temp1;
     temps_[1] = temp2;
+    temps_[2] = temp3;
   }

   LOperand* dividend() { return inputs_[0]; }
   int32_t divisor() const { return divisor_; }
   LOperand* temp1() { return temps_[0]; }
-  LOperand* temp2() { return temps_[0]; }
+  LOperand* temp2() { return temps_[1]; }
+  LOperand* temp3() { return temps_[2]; }

DECLARE_CONCRETE_INSTRUCTION(FlooringDivByConstI, "flooring-div-by-const-i")
   DECLARE_HYDROGEN_ACCESSOR(MathFloorOfDiv)

--
--
v8-dev mailing list
v8-dev@googlegroups.com
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to v8-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to