Bug 1123064 - IonMonkey: Fold constant numbers in MToInt32, r=h4writer
authorZongShen Shen <andy.zsshen@gmail.com>
Thu, 22 Jan 2015 09:27:43 -0800
changeset 226471 07e3fec694b1e50861134eb465c0bf8e992d3e02
parent 226470 59a95e8ba6bbabfa6c70c50b9476c01887a600e1
child 226472 b6b0706f28ff3e6881af9cec7bfb958eeef33853
push id28196
push usercbook@mozilla.com
push dateThu, 29 Jan 2015 14:04:15 +0000
treeherdermozilla-central@a98d16e6a3b4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersh4writer
bugs1123064
milestone38.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1123064 - IonMonkey: Fold constant numbers in MToInt32, r=h4writer
js/src/jit-test/tests/ion/bug1123064.js
js/src/jit/MIR.cpp
new file mode 100644
--- /dev/null
+++ b/js/src/jit-test/tests/ion/bug1123064.js
@@ -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();
--- a/js/src/jit/MIR.cpp
+++ b/js/src/jit/MIR.cpp
@@ -3039,16 +3039,40 @@ MResumePoint::isRecoverableOperand(MUse 
 {
     return block()->info().isRecoverableOperand(indexOf(u));
 }
 
 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;
 }
 
 void
 MToInt32::analyzeEdgeCasesBackward()
 {