Bug 1123064 - IonMonkey: Fold constant numbers in MToInt32, r=h4writer

This commit is contained in:
ZongShen Shen 2015-01-22 09:27:43 -08:00
Родитель 880729b01f
Коммит f89dae3301
2 изменённых файлов: 54 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,30 @@
function toint32() {
// The test case to trigger MToInt32 operation.
var ToInteger = getSelfHostedValue("ToInteger");
// Case1: The input operand is constant int32.
var result = ToInteger(1);
assertEq(result, 1);
// Case2: The input operand is constant double.
result = ToInteger(0.12);
assertEq(result, 0);
// Case3: The input operand is constant float.
result = ToInteger(Math.fround(0.13));
assertEq(result, 0);
// Case4: The input operand is constant boolean.
result = ToInteger(true);
assertEq(result, 1);
// Case5: The input operand is null.
result = ToInteger(null);
assertEq(result, 0);
}
toint32();
toint32();

Просмотреть файл

@ -3044,6 +3044,30 @@ MDefinition *
MToInt32::foldsTo(TempAllocator &alloc)
{
MDefinition *input = getOperand(0);
// Fold this operation if the input operand is constant.
if (input->isConstant()) {
Value val = input->toConstant()->value();
MacroAssembler::IntConversionInputKind convert = conversion();
switch (input->type()) {
case MIRType_Null:
MOZ_ASSERT(convert == MacroAssembler::IntConversion_Any);
return MConstant::New(alloc, Int32Value(0));
case MIRType_Boolean:
MOZ_ASSERT(convert == MacroAssembler::IntConversion_Any ||
convert == MacroAssembler::IntConversion_NumbersOrBoolsOnly);
return MConstant::New(alloc, Int32Value(val.toBoolean()));
case MIRType_Int32:
return MConstant::New(alloc, Int32Value(val.toInt32()));
case MIRType_Float32:
case MIRType_Double:
int32_t ival;
// Only the value within the range of Int32 can be substitued as constant.
if (mozilla::NumberEqualsInt32(val.toNumber(), &ival))
return MConstant::New(alloc, Int32Value(ival));
}
}
if (input->type() == MIRType_Int32)
return input;
return this;