From a47705644e67ddc4b1057d030739fa9ec89c7b69 Mon Sep 17 00:00:00 2001 From: "jkummerow@chromium.org" Date: Fri, 2 Aug 2013 08:59:02 +0000 Subject: [PATCH] Avoid redundant smi check for Math.abs R=jkummerow@chromium.org Review URL: https://codereview.chromium.org/21180004 Patch from Weiliang Lin . git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@16021 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/arm/lithium-codegen-arm.cc | 2 +- src/ia32/lithium-codegen-ia32.cc | 2 +- src/x64/lithium-codegen-x64.cc | 13 +++++++++++++ src/x64/lithium-codegen-x64.h | 1 + test/mjsunit/math-abs.js | 11 +++++++++++ 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/arm/lithium-codegen-arm.cc b/src/arm/lithium-codegen-arm.cc index cf1e7c7..6943910 100644 --- a/src/arm/lithium-codegen-arm.cc +++ b/src/arm/lithium-codegen-arm.cc @@ -3765,7 +3765,7 @@ void LCodeGen::DoMathAbs(LMathAbs* instr) { DwVfpRegister input = ToDoubleRegister(instr->value()); DwVfpRegister result = ToDoubleRegister(instr->result()); __ vabs(result, input); - } else if (r.IsInteger32()) { + } else if (r.IsSmiOrInteger32()) { EmitIntegerMathAbs(instr); } else { // Representation is tagged. diff --git a/src/ia32/lithium-codegen-ia32.cc b/src/ia32/lithium-codegen-ia32.cc index 7a601cf..857d72d 100644 --- a/src/ia32/lithium-codegen-ia32.cc +++ b/src/ia32/lithium-codegen-ia32.cc @@ -3821,7 +3821,7 @@ void LCodeGen::DoMathAbs(LMathAbs* instr) { __ xorps(scratch, scratch); __ subsd(scratch, input_reg); __ pand(input_reg, scratch); - } else if (r.IsInteger32()) { + } else if (r.IsSmiOrInteger32()) { EmitIntegerMathAbs(instr); } else { // Tagged case. DeferredMathAbsTaggedHeapNumber* deferred = diff --git a/src/x64/lithium-codegen-x64.cc b/src/x64/lithium-codegen-x64.cc index f84c357..baed25f 100644 --- a/src/x64/lithium-codegen-x64.cc +++ b/src/x64/lithium-codegen-x64.cc @@ -3426,6 +3426,17 @@ void LCodeGen::EmitIntegerMathAbs(LMathAbs* instr) { } +void LCodeGen::EmitInteger64MathAbs(LMathAbs* instr) { + Register input_reg = ToRegister(instr->value()); + __ testq(input_reg, input_reg); + Label is_positive; + __ j(not_sign, &is_positive, Label::kNear); + __ neg(input_reg); // Sets flags. + DeoptimizeIf(negative, instr->environment()); + __ bind(&is_positive); +} + + void LCodeGen::DoMathAbs(LMathAbs* instr) { // Class for deferred case. class DeferredMathAbsTaggedHeapNumber: public LDeferredCode { @@ -3451,6 +3462,8 @@ void LCodeGen::DoMathAbs(LMathAbs* instr) { __ andpd(input_reg, scratch); } else if (r.IsInteger32()) { EmitIntegerMathAbs(instr); + } else if (r.IsSmi()) { + EmitInteger64MathAbs(instr); } else { // Tagged case. DeferredMathAbsTaggedHeapNumber* deferred = new(zone()) DeferredMathAbsTaggedHeapNumber(this, instr); diff --git a/src/x64/lithium-codegen-x64.h b/src/x64/lithium-codegen-x64.h index 4286d07..93de386 100644 --- a/src/x64/lithium-codegen-x64.h +++ b/src/x64/lithium-codegen-x64.h @@ -268,6 +268,7 @@ class LCodeGen BASE_EMBEDDED { uint32_t additional_index = 0); void EmitIntegerMathAbs(LMathAbs* instr); + void EmitInteger64MathAbs(LMathAbs* instr); // Support for recording safepoint and position information. void RecordSafepoint(LPointerMap* pointers, diff --git a/test/mjsunit/math-abs.js b/test/mjsunit/math-abs.js index 2b07954..d6ee3f2 100644 --- a/test/mjsunit/math-abs.js +++ b/test/mjsunit/math-abs.js @@ -109,3 +109,14 @@ for(var i = 0; i < 1000; i++) { assertEquals(42, foo(-42)); %OptimizeFunctionOnNextCall(foo) assertEquals(42, foo(-42)); + +// Regression test for SMI input of Math.abs on X64, see: +// https://codereview.chromium.org/21180004/ +var a = [-1, -2]; +function foo2() { + return Math.abs(a[0]); +} +assertEquals(1, foo2()); +assertEquals(1, foo2()); +%OptimizeFunctionOnNextCall(foo2); +assertEquals(1, foo2()); -- 2.7.4