Improve code for integral modulus calculation.

Depending on what we know about the right operand, we basically do 3
different things (and the code is actually structured this way):

* If we statically know that the right operand is a power of 2, we do
  some bit fiddling instead of doing a "real" modulus calculation.
  This should actually be done on the Hydrogen level, not on the
  Lithium level, but this will be a separate CL.

* If type feedback tells us that the right operand is a power of 2, we
  do the same as above, but guarded by conditional deoptimization to
  make sure that the assumption is still valid. In the long run, we
  should make this guard visible on the Hydrogen level to make it
  visible for GVN and other optimizations.

* In the general case we only do the minimum steps necessary and don't
  try to be too clever, because cleverness actually slows us down on
  real-world code.

If we look at the code gerators for LModI, we actually see that we
basically have 3 (4 on ARM) fundamentally different translations. I
don't really like lumping them together, they should probably be
different Lithium instructions. For the time being, I restructured the
generators to make this crystal-clear, at the cost of some duplication
regarding the power-of-2 cases. This will go away when we do the
strength reduction on the Hydrogen level, so I'd like to keep it as it
is for now.

Note that the MIPS part was only slightly restructured, there is still
some work to do there.

R=jkummerow@chromium.org

Review URL: https://codereview.chromium.org/15769010

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@15034 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent ecc41e30
...@@ -1446,43 +1446,61 @@ LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) { ...@@ -1446,43 +1446,61 @@ LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
LInstruction* LChunkBuilder::DoMod(HMod* instr) { LInstruction* LChunkBuilder::DoMod(HMod* instr) {
HValue* left = instr->left();
HValue* right = instr->right();
if (instr->representation().IsInteger32()) { if (instr->representation().IsInteger32()) {
ASSERT(instr->left()->representation().IsInteger32()); ASSERT(left->representation().IsInteger32());
ASSERT(instr->right()->representation().IsInteger32()); ASSERT(right->representation().IsInteger32());
LModI* mod;
if (instr->HasPowerOf2Divisor()) { if (instr->HasPowerOf2Divisor()) {
ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero)); ASSERT(!right->CanBeZero());
LOperand* value = UseRegisterAtStart(instr->left()); LModI* mod = new(zone()) LModI(UseRegisterAtStart(left),
mod = new(zone()) LModI(value, UseOrConstant(instr->right())); UseOrConstant(right));
} else { LInstruction* result = DefineAsRegister(mod);
LOperand* dividend = UseRegister(instr->left()); return (left->CanBeNegative() &&
LOperand* divisor = UseRegister(instr->right()); instr->CheckFlag(HValue::kBailoutOnMinusZero))
mod = new(zone()) LModI(dividend, ? AssignEnvironment(result)
divisor, : result;
TempRegister(), } else if (instr->has_fixed_right_arg()) {
FixedTemp(d10), LModI* mod = new(zone()) LModI(UseRegisterAtStart(left),
FixedTemp(d11)); UseRegisterAtStart(right));
}
if (instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
instr->CheckFlag(HValue::kCanBeDivByZero) ||
instr->CheckFlag(HValue::kCanOverflow)) {
return AssignEnvironment(DefineAsRegister(mod)); return AssignEnvironment(DefineAsRegister(mod));
} else if (CpuFeatures::IsSupported(SUDIV)) {
LModI* mod = new(zone()) LModI(UseRegister(left),
UseRegister(right));
LInstruction* result = DefineAsRegister(mod);
return (right->CanBeZero() ||
(left->RangeCanInclude(kMinInt) &&
right->RangeCanInclude(-1) &&
instr->CheckFlag(HValue::kBailoutOnMinusZero)) ||
(left->CanBeNegative() &&
instr->CanBeZero() &&
instr->CheckFlag(HValue::kBailoutOnMinusZero)))
? AssignEnvironment(result)
: result;
} else { } else {
return DefineAsRegister(mod); LModI* mod = new(zone()) LModI(UseRegister(left),
UseRegister(right),
FixedTemp(d10),
FixedTemp(d11));
LInstruction* result = DefineAsRegister(mod);
return (right->CanBeZero() ||
(left->CanBeNegative() &&
instr->CanBeZero() &&
instr->CheckFlag(HValue::kBailoutOnMinusZero)))
? AssignEnvironment(result)
: result;
} }
} else if (instr->representation().IsSmiOrTagged()) { } else if (instr->representation().IsSmiOrTagged()) {
return DoArithmeticT(Token::MOD, instr); return DoArithmeticT(Token::MOD, instr);
} else { } else {
ASSERT(instr->representation().IsDouble()); ASSERT(instr->representation().IsDouble());
// We call a C function for double modulo. It can't trigger a GC. // We call a C function for double modulo. It can't trigger a GC. We need
// We need to use fixed result register for the call. // to use fixed result register for the call.
// TODO(fschneider): Allow any register as input registers. // TODO(fschneider): Allow any register as input registers.
LOperand* left = UseFixedDouble(instr->left(), d1); LArithmeticD* mod = new(zone()) LArithmeticD(Token::MOD,
LOperand* right = UseFixedDouble(instr->right(), d2); UseFixedDouble(left, d1),
LArithmeticD* result = new(zone()) LArithmeticD(Token::MOD, left, right); UseFixedDouble(right, d2));
return MarkAsCall(DefineFixedDouble(result, d1), instr); return MarkAsCall(DefineFixedDouble(mod, d1), instr);
} }
} }
......
...@@ -575,36 +575,22 @@ class LArgumentsElements: public LTemplateInstruction<1, 0, 0> { ...@@ -575,36 +575,22 @@ class LArgumentsElements: public LTemplateInstruction<1, 0, 0> {
}; };
class LModI: public LTemplateInstruction<1, 2, 3> { class LModI: public LTemplateInstruction<1, 2, 2> {
public: public:
// Used when the right hand is a constant power of 2.
LModI(LOperand* left,
LOperand* right) {
inputs_[0] = left;
inputs_[1] = right;
temps_[0] = NULL;
temps_[1] = NULL;
temps_[2] = NULL;
}
// Used for the standard case.
LModI(LOperand* left, LModI(LOperand* left,
LOperand* right, LOperand* right,
LOperand* temp, LOperand* temp = NULL,
LOperand* temp2, LOperand* temp2 = NULL) {
LOperand* temp3) {
inputs_[0] = left; inputs_[0] = left;
inputs_[1] = right; inputs_[1] = right;
temps_[0] = temp; temps_[0] = temp;
temps_[1] = temp2; temps_[1] = temp2;
temps_[2] = temp3;
} }
LOperand* left() { return inputs_[0]; } LOperand* left() { return inputs_[0]; }
LOperand* right() { return inputs_[1]; } LOperand* right() { return inputs_[1]; }
LOperand* temp() { return temps_[0]; } LOperand* temp() { return temps_[0]; }
LOperand* temp2() { return temps_[1]; } LOperand* temp2() { return temps_[1]; }
LOperand* temp3() { return temps_[2]; }
DECLARE_CONCRETE_INSTRUCTION(ModI, "mod-i") DECLARE_CONCRETE_INSTRUCTION(ModI, "mod-i")
DECLARE_HYDROGEN_ACCESSOR(Mod) DECLARE_HYDROGEN_ACCESSOR(Mod)
......
This diff is collapsed.
...@@ -1039,7 +1039,13 @@ class HValue: public ZoneObject { ...@@ -1039,7 +1039,13 @@ class HValue: public ZoneObject {
} }
Range* range() const { return range_; } Range* range() const { return range_; }
// TODO(svenpanne) We should really use the null object pattern here.
bool HasRange() const { return range_ != NULL; } bool HasRange() const { return range_ != NULL; }
bool CanBeNegative() const { return !HasRange() || range()->CanBeNegative(); }
bool CanBeZero() const { return !HasRange() || range()->CanBeZero(); }
bool RangeCanInclude(int value) const {
return !HasRange() || range()->Includes(value);
}
void AddNewRange(Range* r, Zone* zone); void AddNewRange(Range* r, Zone* zone);
void RemoveLastAddedRange(); void RemoveLastAddedRange();
void ComputeInitialRange(Zone* zone); void ComputeInitialRange(Zone* zone);
......
...@@ -1221,110 +1221,115 @@ void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) { ...@@ -1221,110 +1221,115 @@ void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
void LCodeGen::DoModI(LModI* instr) { void LCodeGen::DoModI(LModI* instr) {
if (instr->hydrogen()->HasPowerOf2Divisor()) { HMod* hmod = instr->hydrogen();
Register dividend = ToRegister(instr->left()); HValue* left = hmod->left();
HValue* right = hmod->right();
int32_t divisor = if (hmod->HasPowerOf2Divisor()) {
HConstant::cast(instr->hydrogen()->right())->Integer32Value(); // TODO(svenpanne) We should really do the strength reduction on the
// Hydrogen level.
Register left_reg = ToRegister(instr->left());
ASSERT(left_reg.is(ToRegister(instr->result())));
if (divisor < 0) divisor = -divisor; // Note: The code below even works when right contains kMinInt.
int32_t divisor = Abs(right->GetInteger32Constant());
Label positive_dividend, done; Label left_is_not_negative, done;
__ test(dividend, Operand(dividend)); if (left->CanBeNegative()) {
__ j(not_sign, &positive_dividend, Label::kNear); __ test(left_reg, Operand(left_reg));
__ neg(dividend); __ j(not_sign, &left_is_not_negative, Label::kNear);
__ and_(dividend, divisor - 1); __ neg(left_reg);
__ neg(dividend); __ and_(left_reg, divisor - 1);
if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { __ neg(left_reg);
__ j(not_zero, &done, Label::kNear); if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
DeoptimizeIf(no_condition, instr->environment()); DeoptimizeIf(zero, instr->environment());
} else { }
__ jmp(&done, Label::kNear); __ jmp(&done, Label::kNear);
} }
__ bind(&positive_dividend);
__ and_(dividend, divisor - 1); __ bind(&left_is_not_negative);
__ and_(left_reg, divisor - 1);
__ bind(&done); __ bind(&done);
} else {
Label done, remainder_eq_dividend, slow, both_positive; } else if (hmod->has_fixed_right_arg()) {
Register left_reg = ToRegister(instr->left()); Register left_reg = ToRegister(instr->left());
ASSERT(left_reg.is(ToRegister(instr->result())));
Register right_reg = ToRegister(instr->right()); Register right_reg = ToRegister(instr->right());
Register result_reg = ToRegister(instr->result());
int32_t divisor = hmod->fixed_right_arg_value();
ASSERT(IsPowerOf2(divisor));
// Check if our assumption of a fixed right operand still holds.
__ cmp(right_reg, Immediate(divisor));
DeoptimizeIf(not_equal, instr->environment());
Label left_is_not_negative, done;
if (left->CanBeNegative()) {
__ test(left_reg, Operand(left_reg));
__ j(not_sign, &left_is_not_negative, Label::kNear);
__ neg(left_reg);
__ and_(left_reg, divisor - 1);
__ neg(left_reg);
if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
DeoptimizeIf(zero, instr->environment());
}
__ jmp(&done, Label::kNear);
}
__ bind(&left_is_not_negative);
__ and_(left_reg, divisor - 1);
__ bind(&done);
} else {
Register left_reg = ToRegister(instr->left());
ASSERT(left_reg.is(eax)); ASSERT(left_reg.is(eax));
ASSERT(result_reg.is(edx)); Register right_reg = ToRegister(instr->right());
ASSERT(!right_reg.is(eax)); ASSERT(!right_reg.is(eax));
ASSERT(!right_reg.is(edx)); ASSERT(!right_reg.is(edx));
Register result_reg = ToRegister(instr->result());
ASSERT(result_reg.is(edx));
// Check for x % 0. Label done;
if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) { // Check for x % 0, idiv would signal a divide error. We have to
// deopt in this case because we can't return a NaN.
if (right->CanBeZero()) {
__ test(right_reg, Operand(right_reg)); __ test(right_reg, Operand(right_reg));
DeoptimizeIf(zero, instr->environment()); DeoptimizeIf(zero, instr->environment());
} }
__ test(left_reg, Operand(left_reg)); // Check for kMinInt % -1, idiv would signal a divide error. We
__ j(zero, &remainder_eq_dividend, Label::kNear); // have to deopt if we care about -0, because we can't return that.
__ j(sign, &slow, Label::kNear); if (left->RangeCanInclude(kMinInt) && right->RangeCanInclude(-1)) {
Label no_overflow_possible;
__ test(right_reg, Operand(right_reg));
__ j(not_sign, &both_positive, Label::kNear);
// The sign of the divisor doesn't matter.
__ neg(right_reg);
__ bind(&both_positive);
// If the dividend is smaller than the nonnegative
// divisor, the dividend is the result.
__ cmp(left_reg, Operand(right_reg));
__ j(less, &remainder_eq_dividend, Label::kNear);
// Check if the divisor is a PowerOfTwo integer.
Register scratch = ToRegister(instr->temp());
__ mov(scratch, right_reg);
__ sub(Operand(scratch), Immediate(1));
__ test(scratch, Operand(right_reg));
__ j(not_zero, &slow, Label::kNear);
__ and_(left_reg, Operand(scratch));
__ jmp(&remainder_eq_dividend, Label::kNear);
// Slow case, using idiv instruction.
__ bind(&slow);
// Check for (kMinInt % -1).
if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
Label left_not_min_int;
__ cmp(left_reg, kMinInt); __ cmp(left_reg, kMinInt);
__ j(not_zero, &left_not_min_int, Label::kNear); __ j(not_equal, &no_overflow_possible, Label::kNear);
__ cmp(right_reg, -1); __ cmp(right_reg, -1);
DeoptimizeIf(zero, instr->environment()); if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
__ bind(&left_not_min_int); DeoptimizeIf(equal, instr->environment());
} else {
__ j(not_equal, &no_overflow_possible, Label::kNear);
__ Set(result_reg, Immediate(0));
__ jmp(&done, Label::kNear);
}
__ bind(&no_overflow_possible);
} }
// Sign extend to edx. // Sign extend dividend in eax into edx:eax.
__ cdq(); __ cdq();
// Check for (0 % -x) that will produce negative zero. // If we care about -0, test if the dividend is <0 and the result is 0.
if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { if (left->CanBeNegative() &&
hmod->CanBeZero() &&
hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
Label positive_left; Label positive_left;
Label done;
__ test(left_reg, Operand(left_reg)); __ test(left_reg, Operand(left_reg));
__ j(not_sign, &positive_left, Label::kNear); __ j(not_sign, &positive_left, Label::kNear);
__ idiv(right_reg); __ idiv(right_reg);
// Test the remainder for 0, because then the result would be -0.
__ test(result_reg, Operand(result_reg)); __ test(result_reg, Operand(result_reg));
__ j(not_zero, &done, Label::kNear); DeoptimizeIf(zero, instr->environment());
__ jmp(&done, Label::kNear);
DeoptimizeIf(no_condition, instr->environment());
__ bind(&positive_left); __ bind(&positive_left);
__ idiv(right_reg);
__ bind(&done);
} else {
__ idiv(right_reg);
} }
__ jmp(&done, Label::kNear); __ idiv(right_reg);
__ bind(&remainder_eq_dividend);
__ mov(result_reg, left_reg);
__ bind(&done); __ bind(&done);
} }
} }
......
...@@ -1524,43 +1524,54 @@ LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) { ...@@ -1524,43 +1524,54 @@ LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
LInstruction* LChunkBuilder::DoMod(HMod* instr) { LInstruction* LChunkBuilder::DoMod(HMod* instr) {
HValue* left = instr->left();
HValue* right = instr->right();
if (instr->representation().IsInteger32()) { if (instr->representation().IsInteger32()) {
ASSERT(instr->left()->representation().IsInteger32()); ASSERT(left->representation().IsInteger32());
ASSERT(instr->right()->representation().IsInteger32()); ASSERT(right->representation().IsInteger32());
LInstruction* result;
if (instr->HasPowerOf2Divisor()) { if (instr->HasPowerOf2Divisor()) {
ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero)); ASSERT(!right->CanBeZero());
LOperand* value = UseRegisterAtStart(instr->left()); LModI* mod = new(zone()) LModI(UseRegisterAtStart(left),
LModI* mod = UseOrConstant(right),
new(zone()) LModI(value, UseOrConstant(instr->right()), NULL); NULL);
result = DefineSameAsFirst(mod); LInstruction* result = DefineSameAsFirst(mod);
return (left->CanBeNegative() &&
instr->CheckFlag(HValue::kBailoutOnMinusZero))
? AssignEnvironment(result)
: result;
} else if (instr->has_fixed_right_arg()) {
LModI* mod = new(zone()) LModI(UseRegister(left),
UseRegisterAtStart(right),
NULL);
return AssignEnvironment(DefineSameAsFirst(mod));
} else { } else {
// The temporary operand is necessary to ensure that right is // The temporary operand is necessary to ensure that right is not
// not allocated into edx. // allocated into edx.
LOperand* temp = FixedTemp(edx); LModI* mod = new(zone()) LModI(UseFixed(left, eax),
LOperand* value = UseFixed(instr->left(), eax); UseRegister(right),
LOperand* divisor = UseRegister(instr->right()); FixedTemp(edx));
LModI* mod = new(zone()) LModI(value, divisor, temp); LInstruction* result = DefineFixed(mod, edx);
result = DefineFixed(mod, edx); return (right->CanBeZero() ||
(left->RangeCanInclude(kMinInt) &&
right->RangeCanInclude(-1) &&
instr->CheckFlag(HValue::kBailoutOnMinusZero)) ||
(left->CanBeNegative() &&
instr->CanBeZero() &&
instr->CheckFlag(HValue::kBailoutOnMinusZero)))
? AssignEnvironment(result)
: result;
} }
return (instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
instr->CheckFlag(HValue::kCanBeDivByZero) ||
instr->CheckFlag(HValue::kCanOverflow))
? AssignEnvironment(result)
: result;
} else if (instr->representation().IsSmiOrTagged()) { } else if (instr->representation().IsSmiOrTagged()) {
return DoArithmeticT(Token::MOD, instr); return DoArithmeticT(Token::MOD, instr);
} else { } else {
ASSERT(instr->representation().IsDouble()); ASSERT(instr->representation().IsDouble());
// We call a C function for double modulo. It can't trigger a GC. // We call a C function for double modulo. It can't trigger a GC. We need
// We need to use fixed result register for the call. // to use fixed result register for the call.
// TODO(fschneider): Allow any register as input registers. // TODO(fschneider): Allow any register as input registers.
LOperand* left = UseFixedDouble(instr->left(), xmm2); LArithmeticD* mod = new(zone()) LArithmeticD(Token::MOD,
LOperand* right = UseFixedDouble(instr->right(), xmm1); UseFixedDouble(left, xmm2),
LArithmeticD* result = new(zone()) LArithmeticD(Token::MOD, left, right); UseFixedDouble(right, xmm1));
return MarkAsCall(DefineFixedDouble(result, xmm1), instr); return MarkAsCall(DefineFixedDouble(mod, xmm1), instr);
} }
} }
......
...@@ -1132,59 +1132,74 @@ void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) { ...@@ -1132,59 +1132,74 @@ void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
void LCodeGen::DoModI(LModI* instr) { void LCodeGen::DoModI(LModI* instr) {
Register scratch = scratch0(); HMod* hmod = instr->hydrogen();
const Register left = ToRegister(instr->left()); HValue* left = hmod->left();
const Register result = ToRegister(instr->result()); HValue* right = hmod->right();
if (hmod->HasPowerOf2Divisor()) {
const Register scratch = scratch0();
const Register left_reg = ToRegister(instr->left());
ASSERT(!left_reg.is(scratch));
const Register result_reg = ToRegister(instr->result());
// Note: The code below even works when right contains kMinInt.
int32_t divisor = Abs(right->GetInteger32Constant());
__ mov(scratch, left_reg);
Label left_is_not_negative, done;
if (left->CanBeNegative()) {
__ Branch(USE_DELAY_SLOT, &left_is_not_negative,
ge, left_reg, Operand(zero_reg));
__ subu(result_reg, zero_reg, left_reg);
__ And(result_reg, result_reg, divisor - 1);
if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
DeoptimizeIf(eq, instr->environment(), result_reg, Operand(zero_reg));
}
__ Branch(USE_DELAY_SLOT, &done);
__ subu(result_reg, zero_reg, result_reg);
}
Label done; __ bind(&left_is_not_negative);
__ And(result_reg, scratch, divisor - 1);
__ bind(&done);
if (instr->hydrogen()->HasPowerOf2Divisor()) {
Register scratch = scratch0();
ASSERT(!left.is(scratch));
__ mov(scratch, left);
int32_t p2constant = HConstant::cast(
instr->hydrogen()->right())->Integer32Value();
ASSERT(p2constant != 0);
// Result always takes the sign of the dividend (left).
p2constant = abs(p2constant);
Label positive_dividend;
__ Branch(USE_DELAY_SLOT, &positive_dividend, ge, left, Operand(zero_reg));
__ subu(result, zero_reg, left);
__ And(result, result, p2constant - 1);
if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
DeoptimizeIf(eq, instr->environment(), result, Operand(zero_reg));
}
__ Branch(USE_DELAY_SLOT, &done);
__ subu(result, zero_reg, result);
__ bind(&positive_dividend);
__ And(result, scratch, p2constant - 1);
} else { } else {
// TODO(svenpanne) Add right->has_fixed_right_arg() case.
const Register scratch = scratch0();
const Register left_reg = ToRegister(instr->left());
const Register result_reg = ToRegister(instr->result());
// div runs in the background while we check for special cases. // div runs in the background while we check for special cases.
Register right = EmitLoadRegister(instr->right(), scratch); Register right_reg = EmitLoadRegister(instr->right(), scratch);
__ div(left, right); __ div(left_reg, right_reg);
// Check for x % 0. Label done;
if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) { // Check for x % 0, we have to deopt in this case because we can't return a
DeoptimizeIf(eq, instr->environment(), right, Operand(zero_reg)); // NaN.
if (right->CanBeZero()) {
DeoptimizeIf(eq, instr->environment(), right_reg, Operand(zero_reg));
} }
// Check for (kMinInt % -1). // Check for kMinInt % -1, we have to deopt if we care about -0, because we
if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) { // can't return that.
if (left->RangeCanInclude(kMinInt) && right->RangeCanInclude(-1)) {
Label left_not_min_int; Label left_not_min_int;
__ Branch(&left_not_min_int, ne, left, Operand(kMinInt)); __ Branch(&left_not_min_int, ne, left_reg, Operand(kMinInt));
DeoptimizeIf(eq, instr->environment(), right, Operand(-1)); // TODO(svenpanne) Don't deopt when we don't care about -0.
DeoptimizeIf(eq, instr->environment(), right_reg, Operand(-1));
__ bind(&left_not_min_int); __ bind(&left_not_min_int);
} }
__ Branch(USE_DELAY_SLOT, &done, ge, left, Operand(zero_reg)); // TODO(svenpanne) Only emit the test/deopt if we have to.
__ mfhi(result); __ Branch(USE_DELAY_SLOT, &done, ge, left_reg, Operand(zero_reg));
__ mfhi(result_reg);
if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
DeoptimizeIf(eq, instr->environment(), result, Operand(zero_reg)); DeoptimizeIf(eq, instr->environment(), result_reg, Operand(zero_reg));
} }
__ bind(&done);
} }
__ bind(&done);
} }
......
...@@ -1364,43 +1364,45 @@ LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) { ...@@ -1364,43 +1364,45 @@ LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
LInstruction* LChunkBuilder::DoMod(HMod* instr) { LInstruction* LChunkBuilder::DoMod(HMod* instr) {
HValue* left = instr->left();
HValue* right = instr->right();
if (instr->representation().IsInteger32()) { if (instr->representation().IsInteger32()) {
ASSERT(instr->left()->representation().IsInteger32()); ASSERT(left->representation().IsInteger32());
ASSERT(instr->right()->representation().IsInteger32()); ASSERT(right->representation().IsInteger32());
LModI* mod;
if (instr->HasPowerOf2Divisor()) { if (instr->HasPowerOf2Divisor()) {
ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero)); ASSERT(!right->CanBeZero());
LOperand* value = UseRegisterAtStart(instr->left()); LModI* mod = new(zone()) LModI(UseRegisterAtStart(left),
mod = new(zone()) LModI(value, UseOrConstant(instr->right())); UseOrConstant(right));
} else { LInstruction* result = DefineAsRegister(mod);
LOperand* dividend = UseRegister(instr->left()); return (left->CanBeNegative() &&
LOperand* divisor = UseRegister(instr->right()); instr->CheckFlag(HValue::kBailoutOnMinusZero))
mod = new(zone()) LModI(dividend, ? AssignEnvironment(result)
divisor, : result;
TempRegister(),
FixedTemp(f20),
FixedTemp(f22));
}
if (instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
instr->CheckFlag(HValue::kCanBeDivByZero) ||
instr->CheckFlag(HValue::kCanOverflow)) {
return AssignEnvironment(DefineAsRegister(mod));
} else { } else {
return DefineAsRegister(mod); LModI* mod = new(zone()) LModI(UseRegister(left),
UseRegister(right),
TempRegister(),
FixedTemp(f20),
FixedTemp(f22));
LInstruction* result = DefineAsRegister(mod);
return (right->CanBeZero() ||
(left->RangeCanInclude(kMinInt) &&
right->RangeCanInclude(-1)) ||
instr->CheckFlag(HValue::kBailoutOnMinusZero))
? AssignEnvironment(result)
: result;
} }
} else if (instr->representation().IsSmiOrTagged()) { } else if (instr->representation().IsSmiOrTagged()) {
return DoArithmeticT(Token::MOD, instr); return DoArithmeticT(Token::MOD, instr);
} else { } else {
ASSERT(instr->representation().IsDouble()); ASSERT(instr->representation().IsDouble());
// We call a C function for double modulo. It can't trigger a GC. // We call a C function for double modulo. It can't trigger a GC. We need
// We need to use fixed result register for the call. // to use fixed result register for the call.
// TODO(fschneider): Allow any register as input registers. // TODO(fschneider): Allow any register as input registers.
LOperand* left = UseFixedDouble(instr->left(), f2); LArithmeticD* mod = new(zone()) LArithmeticD(Token::MOD,
LOperand* right = UseFixedDouble(instr->right(), f4); UseFixedDouble(left, f2),
LArithmeticD* result = new(zone()) LArithmeticD(Token::MOD, left, right); UseFixedDouble(right, f4));
return MarkAsCall(DefineFixedDouble(result, f2), instr); return MarkAsCall(DefineFixedDouble(mod, f2), instr);
} }
} }
......
...@@ -251,6 +251,13 @@ T Min(T a, T b) { ...@@ -251,6 +251,13 @@ T Min(T a, T b) {
} }
// Returns the absolute value of its argument.
template <typename T>
T Abs(T a) {
return a < 0 ? -a : a;
}
// Returns the negative absolute value of its argument. // Returns the negative absolute value of its argument.
template <typename T> template <typename T>
T NegAbs(T a) { T NegAbs(T a) {
......
...@@ -1032,111 +1032,116 @@ void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) { ...@@ -1032,111 +1032,116 @@ void LCodeGen::DoUnknownOSRValue(LUnknownOSRValue* instr) {
void LCodeGen::DoModI(LModI* instr) { void LCodeGen::DoModI(LModI* instr) {
if (instr->hydrogen()->HasPowerOf2Divisor()) { HMod* hmod = instr->hydrogen();
Register dividend = ToRegister(instr->left()); HValue* left = hmod->left();
HValue* right = hmod->right();
int32_t divisor = if (hmod->HasPowerOf2Divisor()) {
HConstant::cast(instr->hydrogen()->right())->Integer32Value(); // TODO(svenpanne) We should really do the strength reduction on the
// Hydrogen level.
Register left_reg = ToRegister(instr->left());
ASSERT(left_reg.is(ToRegister(instr->result())));
if (divisor < 0) divisor = -divisor; // Note: The code below even works when right contains kMinInt.
int32_t divisor = Abs(right->GetInteger32Constant());
Label positive_dividend, done; Label left_is_not_negative, done;
__ testl(dividend, dividend); if (left->CanBeNegative()) {
__ j(not_sign, &positive_dividend, Label::kNear); __ testl(left_reg, left_reg);
__ negl(dividend); __ j(not_sign, &left_is_not_negative, Label::kNear);
__ andl(dividend, Immediate(divisor - 1)); __ negl(left_reg);
__ negl(dividend); __ andl(left_reg, Immediate(divisor - 1));
if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { __ negl(left_reg);
__ j(not_zero, &done, Label::kNear); if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
DeoptimizeIf(no_condition, instr->environment()); DeoptimizeIf(zero, instr->environment());
} else { }
__ jmp(&done, Label::kNear); __ jmp(&done, Label::kNear);
} }
__ bind(&positive_dividend);
__ andl(dividend, Immediate(divisor - 1)); __ bind(&left_is_not_negative);
__ andl(left_reg, Immediate(divisor - 1));
__ bind(&done); __ bind(&done);
} else {
Label done, remainder_eq_dividend, slow, both_positive; } else if (hmod->has_fixed_right_arg()) {
Register left_reg = ToRegister(instr->left()); Register left_reg = ToRegister(instr->left());
ASSERT(left_reg.is(ToRegister(instr->result())));
Register right_reg = ToRegister(instr->right()); Register right_reg = ToRegister(instr->right());
Register result_reg = ToRegister(instr->result());
int32_t divisor = hmod->fixed_right_arg_value();
ASSERT(IsPowerOf2(divisor));
// Check if our assumption of a fixed right operand still holds.
__ cmpl(right_reg, Immediate(divisor));
DeoptimizeIf(not_equal, instr->environment());
Label left_is_not_negative, done;
if (left->CanBeNegative()) {
__ testl(left_reg, left_reg);
__ j(not_sign, &left_is_not_negative, Label::kNear);
__ negl(left_reg);
__ andl(left_reg, Immediate(divisor - 1));
__ negl(left_reg);
if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
DeoptimizeIf(zero, instr->environment());
}
__ jmp(&done, Label::kNear);
}
__ bind(&left_is_not_negative);
__ andl(left_reg, Immediate(divisor - 1));
__ bind(&done);
} else {
Register left_reg = ToRegister(instr->left());
ASSERT(left_reg.is(rax)); ASSERT(left_reg.is(rax));
ASSERT(result_reg.is(rdx)); Register right_reg = ToRegister(instr->right());
ASSERT(!right_reg.is(rax)); ASSERT(!right_reg.is(rax));
ASSERT(!right_reg.is(rdx)); ASSERT(!right_reg.is(rdx));
Register result_reg = ToRegister(instr->result());
ASSERT(result_reg.is(rdx));
// Check for x % 0. Label done;
if (instr->hydrogen()->CheckFlag(HValue::kCanBeDivByZero)) { // Check for x % 0, idiv would signal a divide error. We have to
// deopt in this case because we can't return a NaN.
if (right->CanBeZero()) {
__ testl(right_reg, right_reg); __ testl(right_reg, right_reg);
DeoptimizeIf(zero, instr->environment()); DeoptimizeIf(zero, instr->environment());
} }
__ testl(left_reg, left_reg); // Check for kMinInt % -1, idiv would signal a divide error. We
__ j(zero, &remainder_eq_dividend, Label::kNear); // have to deopt if we care about -0, because we can't return that.
__ j(sign, &slow, Label::kNear); if (left->RangeCanInclude(kMinInt) && right->RangeCanInclude(-1)) {
Label no_overflow_possible;
__ testl(right_reg, right_reg);
__ j(not_sign, &both_positive, Label::kNear);
// The sign of the divisor doesn't matter.
__ neg(right_reg);
__ bind(&both_positive);
// If the dividend is smaller than the nonnegative
// divisor, the dividend is the result.
__ cmpl(left_reg, right_reg);
__ j(less, &remainder_eq_dividend, Label::kNear);
// Check if the divisor is a PowerOfTwo integer.
Register scratch = ToRegister(instr->temp());
__ movl(scratch, right_reg);
__ subl(scratch, Immediate(1));
__ testl(scratch, right_reg);
__ j(not_zero, &slow, Label::kNear);
__ andl(left_reg, scratch);
__ jmp(&remainder_eq_dividend, Label::kNear);
// Slow case, using idiv instruction.
__ bind(&slow);
// Check for (kMinInt % -1).
if (instr->hydrogen()->CheckFlag(HValue::kCanOverflow)) {
Label left_not_min_int;
__ cmpl(left_reg, Immediate(kMinInt)); __ cmpl(left_reg, Immediate(kMinInt));
__ j(not_zero, &left_not_min_int, Label::kNear); __ j(not_zero, &no_overflow_possible, Label::kNear);
__ cmpl(right_reg, Immediate(-1)); __ cmpl(right_reg, Immediate(-1));
DeoptimizeIf(zero, instr->environment()); if (hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
__ bind(&left_not_min_int); DeoptimizeIf(equal, instr->environment());
} else {
__ j(not_equal, &no_overflow_possible, Label::kNear);
__ Set(result_reg, 0);
__ jmp(&done, Label::kNear);
}
__ bind(&no_overflow_possible);
} }
// Sign extend eax to edx. // Sign extend dividend in eax into edx:eax, since we are using only the low
// (We are using only the low 32 bits of the values.) // 32 bits of the values.
__ cdq(); __ cdq();
// Check for (0 % -x) that will produce negative zero. // If we care about -0, test if the dividend is <0 and the result is 0.
if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) { if (left->CanBeNegative() &&
hmod->CanBeZero() &&
hmod->CheckFlag(HValue::kBailoutOnMinusZero)) {
Label positive_left; Label positive_left;
Label done;
__ testl(left_reg, left_reg); __ testl(left_reg, left_reg);
__ j(not_sign, &positive_left, Label::kNear); __ j(not_sign, &positive_left, Label::kNear);
__ idivl(right_reg); __ idivl(right_reg);
// Test the remainder for 0, because then the result would be -0.
__ testl(result_reg, result_reg); __ testl(result_reg, result_reg);
__ j(not_zero, &done, Label::kNear); DeoptimizeIf(zero, instr->environment());
__ jmp(&done, Label::kNear);
DeoptimizeIf(no_condition, instr->environment());
__ bind(&positive_left); __ bind(&positive_left);
__ idivl(right_reg);
__ bind(&done);
} else {
__ idivl(right_reg);
} }
__ jmp(&done, Label::kNear); __ idivl(right_reg);
__ bind(&remainder_eq_dividend);
__ movl(result_reg, left_reg);
__ bind(&done); __ bind(&done);
} }
} }
......
...@@ -1437,43 +1437,54 @@ LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) { ...@@ -1437,43 +1437,54 @@ LInstruction* LChunkBuilder::DoMathFloorOfDiv(HMathFloorOfDiv* instr) {
LInstruction* LChunkBuilder::DoMod(HMod* instr) { LInstruction* LChunkBuilder::DoMod(HMod* instr) {
HValue* left = instr->left();
HValue* right = instr->right();
if (instr->representation().IsInteger32()) { if (instr->representation().IsInteger32()) {
ASSERT(instr->left()->representation().IsInteger32()); ASSERT(left->representation().IsInteger32());
ASSERT(instr->right()->representation().IsInteger32()); ASSERT(right->representation().IsInteger32());
LInstruction* result;
if (instr->HasPowerOf2Divisor()) { if (instr->HasPowerOf2Divisor()) {
ASSERT(!instr->CheckFlag(HValue::kCanBeDivByZero)); ASSERT(!right->CanBeZero());
LOperand* value = UseRegisterAtStart(instr->left()); LModI* mod = new(zone()) LModI(UseRegisterAtStart(left),
LModI* mod = UseOrConstant(right),
new(zone()) LModI(value, UseOrConstant(instr->right()), NULL); NULL);
result = DefineSameAsFirst(mod); LInstruction* result = DefineSameAsFirst(mod);
return (left->CanBeNegative() &&
instr->CheckFlag(HValue::kBailoutOnMinusZero))
? AssignEnvironment(result)
: result;
} else if (instr->has_fixed_right_arg()) {
LModI* mod = new(zone()) LModI(UseRegister(left),
UseRegisterAtStart(right),
NULL);
return AssignEnvironment(DefineSameAsFirst(mod));
} else { } else {
// The temporary operand is necessary to ensure that right is not // The temporary operand is necessary to ensure that right is not
// allocated into edx. // allocated into edx.
LOperand* temp = FixedTemp(rdx); LModI* mod = new(zone()) LModI(UseFixed(left, rax),
LOperand* value = UseFixed(instr->left(), rax); UseRegister(right),
LOperand* divisor = UseRegister(instr->right()); FixedTemp(rdx));
LModI* mod = new(zone()) LModI(value, divisor, temp); LInstruction* result = DefineFixed(mod, rdx);
result = DefineFixed(mod, rdx); return (right->CanBeZero() ||
(left->RangeCanInclude(kMinInt) &&
right->RangeCanInclude(-1) &&
instr->CheckFlag(HValue::kBailoutOnMinusZero)) ||
(left->CanBeNegative() &&
instr->CanBeZero() &&
instr->CheckFlag(HValue::kBailoutOnMinusZero)))
? AssignEnvironment(result)
: result;
} }
return (instr->CheckFlag(HValue::kBailoutOnMinusZero) ||
instr->CheckFlag(HValue::kCanBeDivByZero) ||
instr->CheckFlag(HValue::kCanOverflow))
? AssignEnvironment(result)
: result;
} else if (instr->representation().IsSmiOrTagged()) { } else if (instr->representation().IsSmiOrTagged()) {
return DoArithmeticT(Token::MOD, instr); return DoArithmeticT(Token::MOD, instr);
} else { } else {
ASSERT(instr->representation().IsDouble()); ASSERT(instr->representation().IsDouble());
// We call a C function for double modulo. It can't trigger a GC. // We call a C function for double modulo. It can't trigger a GC. We need to
// We need to use fixed result register for the call. // use fixed result register for the call.
// TODO(fschneider): Allow any register as input registers. // TODO(fschneider): Allow any register as input registers.
LOperand* left = UseFixedDouble(instr->left(), xmm2); LArithmeticD* mod = new(zone()) LArithmeticD(Token::MOD,
LOperand* right = UseFixedDouble(instr->right(), xmm1); UseFixedDouble(left, xmm2),
LArithmeticD* result = new(zone()) LArithmeticD(Token::MOD, left, right); UseFixedDouble(right, xmm1));
return MarkAsCall(DefineFixedDouble(result, xmm1), instr); return MarkAsCall(DefineFixedDouble(mod, xmm1), instr);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment