[JSC] Do not generate an Add when adding a zero immediate to something
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 13 Nov 2015 06:13:11 +0000 (06:13 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 13 Nov 2015 06:13:11 +0000 (06:13 +0000)
https://bugs.webkit.org/show_bug.cgi?id=151171

Patch by Benjamin Poulain <bpoulain@apple.com> on 2015-11-12
Reviewed by Geoffrey Garen.

Avoid generating an add if one of arguments is a zero immediate.

On x86, the add32/64() were also used internally for branchAdd32/64.
I split the code that sets flag to add32AndSetFlags() to make sure
we always force the flags before testing.

I could have used CMp to set the flags but I would gain nothing from
that, cmp is just a SUB.

* assembler/MacroAssemblerARM64.h:
(JSC::MacroAssemblerARM64::add32):
(JSC::MacroAssemblerARM64::add64):
* assembler/MacroAssemblerARMv7.h:
(JSC::MacroAssemblerARMv7::add32):
* assembler/MacroAssemblerX86.h:
(JSC::MacroAssemblerX86::add32):
* assembler/MacroAssemblerX86Common.h:
(JSC::MacroAssemblerX86Common::add32):
(JSC::MacroAssemblerX86Common::branchAdd32):
(JSC::MacroAssemblerX86Common::add32AndSetFlags):
* assembler/MacroAssemblerX86_64.h:
(JSC::MacroAssemblerX86_64::add32):
(JSC::MacroAssemblerX86_64::add64):
(JSC::MacroAssemblerX86_64::branchAdd64):
(JSC::MacroAssemblerX86_64::add64AndSetFlags):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@192409 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/assembler/MacroAssemblerARM64.h
Source/JavaScriptCore/assembler/MacroAssemblerARMv7.h
Source/JavaScriptCore/assembler/MacroAssemblerX86.h
Source/JavaScriptCore/assembler/MacroAssemblerX86Common.h
Source/JavaScriptCore/assembler/MacroAssemblerX86_64.h

index 995e1c8..7097449 100644 (file)
@@ -1,3 +1,36 @@
+2015-11-12  Benjamin Poulain  <bpoulain@apple.com>
+
+        [JSC] Do not generate an Add when adding a zero immediate to something
+        https://bugs.webkit.org/show_bug.cgi?id=151171
+
+        Reviewed by Geoffrey Garen.
+
+        Avoid generating an add if one of arguments is a zero immediate.
+
+        On x86, the add32/64() were also used internally for branchAdd32/64.
+        I split the code that sets flag to add32AndSetFlags() to make sure
+        we always force the flags before testing.
+
+        I could have used CMp to set the flags but I would gain nothing from
+        that, cmp is just a SUB.
+
+        * assembler/MacroAssemblerARM64.h:
+        (JSC::MacroAssemblerARM64::add32):
+        (JSC::MacroAssemblerARM64::add64):
+        * assembler/MacroAssemblerARMv7.h:
+        (JSC::MacroAssemblerARMv7::add32):
+        * assembler/MacroAssemblerX86.h:
+        (JSC::MacroAssemblerX86::add32):
+        * assembler/MacroAssemblerX86Common.h:
+        (JSC::MacroAssemblerX86Common::add32):
+        (JSC::MacroAssemblerX86Common::branchAdd32):
+        (JSC::MacroAssemblerX86Common::add32AndSetFlags):
+        * assembler/MacroAssemblerX86_64.h:
+        (JSC::MacroAssemblerX86_64::add32):
+        (JSC::MacroAssemblerX86_64::add64):
+        (JSC::MacroAssemblerX86_64::branchAdd64):
+        (JSC::MacroAssemblerX86_64::add64AndSetFlags):
+
 2015-11-12  Geoffrey Garen  <ggaren@apple.com>
 
         Restore CodeBlock jettison code I accidentally removed
index dda9aa2..ed685e0 100644 (file)
@@ -141,11 +141,19 @@ public:
 
     void add32(TrustedImm32 imm, RegisterID dest)
     {
+        if (!imm.m_value)
+            return;
+
         add32(imm, dest, dest);
     }
 
     void add32(TrustedImm32 imm, RegisterID src, RegisterID dest)
     {
+        if (!imm.m_value) {
+            move(src, dest);
+            return;
+        }
+
         if (isUInt12(imm.m_value))
             m_assembler.add<32>(dest, src, UInt12(imm.m_value));
         else if (isUInt12(-imm.m_value))
@@ -158,6 +166,9 @@ public:
 
     void add32(TrustedImm32 imm, Address address)
     {
+        if (!imm.m_value)
+            return;
+
         load32(address, getCachedDataTempRegisterIDAndInvalidate());
 
         if (isUInt12(imm.m_value))
@@ -174,6 +185,9 @@ public:
 
     void add32(TrustedImm32 imm, AbsoluteAddress address)
     {
+        if (!imm.m_value)
+            return;
+
         load32(address.m_ptr, getCachedDataTempRegisterIDAndInvalidate());
 
         if (isUInt12(imm.m_value)) {
@@ -209,6 +223,9 @@ public:
 
     void add64(TrustedImm32 imm, RegisterID dest)
     {
+        if (!imm.m_value)
+            return;
+
         if (isUInt12(imm.m_value)) {
             m_assembler.add<64>(dest, dest, UInt12(imm.m_value));
             return;
@@ -225,6 +242,8 @@ public:
     void add64(TrustedImm64 imm, RegisterID dest)
     {
         intptr_t immediate = imm.m_value;
+        if (!immediate)
+            return;
 
         if (isUInt12(immediate)) {
             m_assembler.add<64>(dest, dest, UInt12(static_cast<int32_t>(immediate)));
@@ -241,6 +260,11 @@ public:
 
     void add64(TrustedImm32 imm, RegisterID src, RegisterID dest)
     {
+        if (!imm.m_value) {
+            move(src, dest);
+            return;
+        }
+
         if (isUInt12(imm.m_value)) {
             m_assembler.add<64>(dest, src, UInt12(imm.m_value));
             return;
@@ -256,6 +280,9 @@ public:
 
     void add64(TrustedImm32 imm, Address address)
     {
+        if (!imm.m_value)
+            return;
+
         load64(address, getCachedDataTempRegisterIDAndInvalidate());
 
         if (isUInt12(imm.m_value))
@@ -272,6 +299,9 @@ public:
 
     void add64(TrustedImm32 imm, AbsoluteAddress address)
     {
+        if (!imm.m_value)
+            return;
+
         load64(address.m_ptr, getCachedDataTempRegisterIDAndInvalidate());
 
         if (isUInt12(imm.m_value)) {
index b5ef391..6fe451f 100644 (file)
@@ -168,6 +168,11 @@ public:
 
     void add32(TrustedImm32 imm, RegisterID src, RegisterID dest)
     {
+        if (!imm.m_value) {
+            move(src, dest);
+            return;
+        }
+
         ARMThumbImmediate armImm = ARMThumbImmediate::makeUInt12OrEncodedImm(imm.m_value);
 
         // For adds with stack pointer destination, moving the src first to sp is
@@ -187,6 +192,9 @@ public:
 
     void add32(TrustedImm32 imm, Address address)
     {
+        if (!imm.m_value)
+            return;
+
         load32(address, dataTempRegister);
 
         ARMThumbImmediate armImm = ARMThumbImmediate::makeUInt12OrEncodedImm(imm.m_value);
@@ -210,6 +218,9 @@ public:
 
     void add32(TrustedImm32 imm, AbsoluteAddress address)
     {
+        if (!imm.m_value)
+            return;
+
         load32(address.m_ptr, dataTempRegister);
 
         ARMThumbImmediate armImm = ARMThumbImmediate::makeUInt12OrEncodedImm(imm.m_value);
index c285f25..d8e9652 100644 (file)
@@ -58,11 +58,19 @@ public:
 
     void add32(TrustedImm32 imm, RegisterID src, RegisterID dest)
     {
+        if (!imm.m_value) {
+            move(src, dest);
+            return;
+        }
+
         m_assembler.leal_mr(imm.m_value, src, dest);
     }
 
     void add32(TrustedImm32 imm, AbsoluteAddress address)
     {
+        if (!imm.m_value)
+            return;
+
         m_assembler.addl_im(imm.m_value, address.m_ptr);
     }
     
index 5a2679c..3c51cd0 100644 (file)
@@ -114,15 +114,16 @@ public:
 
     void add32(TrustedImm32 imm, Address address)
     {
-        m_assembler.addl_im(imm.m_value, address.offset, address.base);
+        if (!imm.m_value)
+            return;
+        add32AndSetFlags(imm, address);
     }
 
     void add32(TrustedImm32 imm, RegisterID dest)
     {
-        if (imm.m_value == 1)
-            m_assembler.inc_r(dest);
-        else
-            m_assembler.addl_ir(imm.m_value, dest);
+        if (!imm.m_value)
+            return;
+        add32AndSetFlags(imm, dest);
     }
     
     void add32(Address src, RegisterID dest)
@@ -137,6 +138,11 @@ public:
 
     void add32(TrustedImm32 imm, RegisterID src, RegisterID dest)
     {
+        if (!imm.m_value) {
+            move(src, dest);
+            return;
+        }
+
         m_assembler.leal_mr(imm.m_value, src, dest);
     }
     
@@ -1400,13 +1406,13 @@ public:
 
     Jump branchAdd32(ResultCondition cond, TrustedImm32 imm, RegisterID dest)
     {
-        add32(imm, dest);
+        add32AndSetFlags(imm, dest);
         return Jump(m_assembler.jCC(x86Condition(cond)));
     }
     
     Jump branchAdd32(ResultCondition cond, TrustedImm32 src, Address dest)
     {
-        add32(src, dest);
+        add32AndSetFlags(src, dest);
         return Jump(m_assembler.jCC(x86Condition(cond)));
     }
 
@@ -1684,6 +1690,19 @@ private:
             m_assembler.testl_i32m(mask.m_value, address.offset, address.base);
     }
 
+    void add32AndSetFlags(TrustedImm32 imm, RegisterID dest)
+    {
+        if (imm.m_value == 1)
+            m_assembler.inc_r(dest);
+        else
+            m_assembler.addl_ir(imm.m_value, dest);
+    }
+
+    void add32AndSetFlags(TrustedImm32 imm, Address address)
+    {
+        m_assembler.addl_im(imm.m_value, address.offset, address.base);
+    }
+
 #if CPU(X86)
 #if OS(MAC_OS_X)
 
index 37ac8a2..6ab3496 100644 (file)
@@ -57,6 +57,9 @@ public:
 
     void add32(TrustedImm32 imm, AbsoluteAddress address)
     {
+        if (!imm.m_value)
+            return;
+
         move(TrustedImmPtr(address.m_ptr), scratchRegister);
         add32(imm, Address(scratchRegister));
     }
@@ -268,29 +271,33 @@ public:
 
     void add64(TrustedImm32 imm, RegisterID srcDest)
     {
-        if (imm.m_value == 1)
-            m_assembler.incq_r(srcDest);
-        else
-            m_assembler.addq_ir(imm.m_value, srcDest);
+        if (!imm.m_value)
+            return;
+        add64AndSetFlags(imm, srcDest);
     }
 
-    void add64(TrustedImm64 imm, RegisterID dest)
+    void add64(TrustedImm64 imm, RegisterID srcDest)
     {
-        if (imm.m_value == 1)
-            m_assembler.incq_r(dest);
-        else {
-            move(imm, scratchRegister);
-            add64(scratchRegister, dest);
-        }
+        if (!imm.m_value)
+            return;
+        add64AndSetFlags(imm, srcDest);
     }
 
     void add64(TrustedImm32 imm, RegisterID src, RegisterID dest)
     {
+        if (!imm.m_value) {
+            move(src, dest);
+            return;
+        }
+
         m_assembler.leaq_mr(imm.m_value, src, dest);
     }
 
     void add64(TrustedImm32 imm, Address address)
     {
+        if (!imm.m_value)
+            return;
+
         if (imm.m_value == 1)
             m_assembler.incq_m(address.offset, address.base);
         else
@@ -299,6 +306,9 @@ public:
 
     void add64(TrustedImm32 imm, AbsoluteAddress address)
     {
+        if (!imm.m_value)
+            return;
+
         move(TrustedImmPtr(address.m_ptr), scratchRegister);
         add64(imm, Address(scratchRegister));
     }
@@ -767,7 +777,7 @@ public:
 
     Jump branchAdd64(ResultCondition cond, TrustedImm32 imm, RegisterID dest)
     {
-        add64(imm, dest);
+        add64AndSetFlags(imm, dest);
         return Jump(m_assembler.jCC(x86Condition(cond)));
     }
 
@@ -985,6 +995,24 @@ public:
     }
 
 private:
+    void add64AndSetFlags(TrustedImm32 imm, RegisterID srcDest)
+    {
+        if (imm.m_value == 1)
+            m_assembler.incq_r(srcDest);
+        else
+            m_assembler.addq_ir(imm.m_value, srcDest);
+    }
+
+    void add64AndSetFlags(TrustedImm64 imm, RegisterID dest)
+    {
+        if (imm.m_value == 1)
+            m_assembler.incq_r(dest);
+        else {
+            move(imm, scratchRegister);
+            add64(scratchRegister, dest);
+        }
+    }
+
     friend class LinkBuffer;
 
     static void linkCall(void* code, Call call, FunctionPtr function)