Title: [216027] trunk/Source/_javascript_Core
Revision
216027
Author
[email protected]
Date
2017-05-01 14:17:24 -0700 (Mon, 01 May 2017)

Log Message

Air::fixObviousSpills should remove totally redundant instructions
https://bugs.webkit.org/show_bug.cgi?id=171131

Reviewed by Saam Barati.
        
This is a modest compile-time-neutral improvement to fixObviousSpills. That phase
builds up a classic alias analysis data structure over spills and registers and then
uses it to remove the most common spill pathologies we encounter. For example, if you
use a spill but the spill is aliased to a register or constant, then we can replace the
use of the spill with a use of the register or constant.
        
But that phase was missing perhaps one of the most obvious fixups that its analysis
allows us to do: if any instruction creates an alias we already know about, then the
instruction is redundant. This turned out to be super important for
https://bugs.webkit.org/show_bug.cgi?id=171075. That patch didn't work out, but this
kind of optimization might be a good clean-up for many other kinds of optimizations.

* b3/air/AirFixObviousSpills.cpp:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (216026 => 216027)


--- trunk/Source/_javascript_Core/ChangeLog	2017-05-01 21:15:22 UTC (rev 216026)
+++ trunk/Source/_javascript_Core/ChangeLog	2017-05-01 21:17:24 UTC (rev 216027)
@@ -1,3 +1,24 @@
+2017-04-21  Filip Pizlo  <[email protected]>
+
+        Air::fixObviousSpills should remove totally redundant instructions
+        https://bugs.webkit.org/show_bug.cgi?id=171131
+
+        Reviewed by Saam Barati.
+        
+        This is a modest compile-time-neutral improvement to fixObviousSpills. That phase
+        builds up a classic alias analysis data structure over spills and registers and then
+        uses it to remove the most common spill pathologies we encounter. For example, if you
+        use a spill but the spill is aliased to a register or constant, then we can replace the
+        use of the spill with a use of the register or constant.
+        
+        But that phase was missing perhaps one of the most obvious fixups that its analysis
+        allows us to do: if any instruction creates an alias we already know about, then the
+        instruction is redundant. This turned out to be super important for
+        https://bugs.webkit.org/show_bug.cgi?id=171075. That patch didn't work out, but this
+        kind of optimization might be a good clean-up for many other kinds of optimizations.
+
+        * b3/air/AirFixObviousSpills.cpp:
+
 2017-04-30  Oleksandr Skachkov  <[email protected]>
 
         We initialize functions too early in an eval

Modified: trunk/Source/_javascript_Core/b3/air/AirFixObviousSpills.cpp (216026 => 216027)


--- trunk/Source/_javascript_Core/b3/air/AirFixObviousSpills.cpp	2017-05-01 21:15:22 UTC (rev 216026)
+++ trunk/Source/_javascript_Core/b3/air/AirFixObviousSpills.cpp	2017-05-01 21:17:24 UTC (rev 216027)
@@ -105,39 +105,27 @@
             }
         }
     }
-
-    void executeInst()
+    
+    template<typename Func>
+    void forAllAliases(const Func& func)
     {
         Inst& inst = m_block->at(m_instIndex);
 
-        if (verbose)
-            dataLog("    Executing ", inst, ": ", m_state, "\n");
-
-        Inst::forEachDefWithExtraClobberedRegs<Arg>(
-            &inst, &inst,
-            [&] (const Arg& arg, Arg::Role, Bank, Width) {
-                if (verbose)
-                    dataLog("        Clobbering ", arg, "\n");
-                m_state.clobber(arg);
-            });
-
         switch (inst.kind.opcode) {
         case Move:
             if (inst.args[0].isSomeImm()) {
                 if (inst.args[1].isReg())
-                    m_state.addAlias(RegConst(inst.args[1].reg(), inst.args[0].value()));
+                    func(RegConst(inst.args[1].reg(), inst.args[0].value()));
                 else if (isSpillSlot(inst.args[1]))
-                    m_state.addAlias(SlotConst(inst.args[1].stackSlot(), inst.args[0].value()));
+                    func(SlotConst(inst.args[1].stackSlot(), inst.args[0].value()));
             } else if (isSpillSlot(inst.args[0]) && inst.args[1].isReg()) {
                 if (std::optional<int64_t> constant = m_state.constantFor(inst.args[0]))
-                    m_state.addAlias(RegConst(inst.args[1].reg(), *constant));
-                m_state.addAlias(
-                    RegSlot(inst.args[1].reg(), inst.args[0].stackSlot(), RegSlot::AllBits));
+                    func(RegConst(inst.args[1].reg(), *constant));
+                func(RegSlot(inst.args[1].reg(), inst.args[0].stackSlot(), RegSlot::AllBits));
             } else if (inst.args[0].isReg() && isSpillSlot(inst.args[1])) {
                 if (std::optional<int64_t> constant = m_state.constantFor(inst.args[0]))
-                    m_state.addAlias(SlotConst(inst.args[1].stackSlot(), *constant));
-                m_state.addAlias(
-                    RegSlot(inst.args[0].reg(), inst.args[1].stackSlot(), RegSlot::AllBits));
+                    func(SlotConst(inst.args[1].stackSlot(), *constant));
+                func(RegSlot(inst.args[0].reg(), inst.args[1].stackSlot(), RegSlot::AllBits));
             }
             break;
 
@@ -144,47 +132,60 @@
         case Move32:
             if (inst.args[0].isSomeImm()) {
                 if (inst.args[1].isReg())
-                    m_state.addAlias(RegConst(inst.args[1].reg(), static_cast<uint32_t>(inst.args[0].value())));
+                    func(RegConst(inst.args[1].reg(), static_cast<uint32_t>(inst.args[0].value())));
                 else if (isSpillSlot(inst.args[1]))
-                    m_state.addAlias(SlotConst(inst.args[1].stackSlot(), static_cast<uint32_t>(inst.args[0].value())));
+                    func(SlotConst(inst.args[1].stackSlot(), static_cast<uint32_t>(inst.args[0].value())));
             } else if (isSpillSlot(inst.args[0]) && inst.args[1].isReg()) {
                 if (std::optional<int64_t> constant = m_state.constantFor(inst.args[0]))
-                    m_state.addAlias(RegConst(inst.args[1].reg(), static_cast<uint32_t>(*constant)));
-                m_state.addAlias(
-                    RegSlot(inst.args[1].reg(), inst.args[0].stackSlot(), RegSlot::ZExt32));
+                    func(RegConst(inst.args[1].reg(), static_cast<uint32_t>(*constant)));
+                func(RegSlot(inst.args[1].reg(), inst.args[0].stackSlot(), RegSlot::ZExt32));
             } else if (inst.args[0].isReg() && isSpillSlot(inst.args[1])) {
                 if (std::optional<int64_t> constant = m_state.constantFor(inst.args[0]))
-                    m_state.addAlias(SlotConst(inst.args[1].stackSlot(), static_cast<int32_t>(*constant)));
-                m_state.addAlias(
-                    RegSlot(inst.args[0].reg(), inst.args[1].stackSlot(), RegSlot::Match32));
+                    func(SlotConst(inst.args[1].stackSlot(), static_cast<int32_t>(*constant)));
+                func(RegSlot(inst.args[0].reg(), inst.args[1].stackSlot(), RegSlot::Match32));
             }
             break;
 
         case MoveFloat:
-            if (isSpillSlot(inst.args[0]) && inst.args[1].isReg()) {
-                m_state.addAlias(
-                    RegSlot(inst.args[1].reg(), inst.args[0].stackSlot(), RegSlot::Match32));
-            } else if (inst.args[0].isReg() && isSpillSlot(inst.args[1])) {
-                m_state.addAlias(
-                    RegSlot(inst.args[0].reg(), inst.args[1].stackSlot(), RegSlot::Match32));
-            }
+            if (isSpillSlot(inst.args[0]) && inst.args[1].isReg())
+                func(RegSlot(inst.args[1].reg(), inst.args[0].stackSlot(), RegSlot::Match32));
+            else if (inst.args[0].isReg() && isSpillSlot(inst.args[1]))
+                func(RegSlot(inst.args[0].reg(), inst.args[1].stackSlot(), RegSlot::Match32));
             break;
 
         case MoveDouble:
-            if (isSpillSlot(inst.args[0]) && inst.args[1].isReg()) {
-                m_state.addAlias(
-                    RegSlot(inst.args[1].reg(), inst.args[0].stackSlot(), RegSlot::AllBits));
-            } else if (inst.args[0].isReg() && isSpillSlot(inst.args[1])) {
-                m_state.addAlias(
-                    RegSlot(inst.args[0].reg(), inst.args[1].stackSlot(), RegSlot::AllBits));
-            }
+            if (isSpillSlot(inst.args[0]) && inst.args[1].isReg())
+                func(RegSlot(inst.args[1].reg(), inst.args[0].stackSlot(), RegSlot::AllBits));
+            else if (inst.args[0].isReg() && isSpillSlot(inst.args[1]))
+                func(RegSlot(inst.args[0].reg(), inst.args[1].stackSlot(), RegSlot::AllBits));
             break;
-
+            
         default:
             break;
         }
     }
 
+    void executeInst()
+    {
+        Inst& inst = m_block->at(m_instIndex);
+
+        if (verbose)
+            dataLog("    Executing ", inst, ": ", m_state, "\n");
+
+        Inst::forEachDefWithExtraClobberedRegs<Arg>(
+            &inst, &inst,
+            [&] (const Arg& arg, Arg::Role, Bank, Width) {
+                if (verbose)
+                    dataLog("        Clobbering ", arg, "\n");
+                m_state.clobber(arg);
+            });
+        
+        forAllAliases(
+            [&] (const auto& alias) {
+                m_state.addAlias(alias);
+            });
+    }
+
     void fixInst()
     {
         Inst& inst = m_block->at(m_instIndex);
@@ -192,6 +193,17 @@
         if (verbose)
             dataLog("Fixing inst ", inst, ": ", m_state, "\n");
         
+        // Check if alias analysis says that this is unnecessary.
+        bool shouldLive = true;
+        forAllAliases(
+            [&] (const auto& alias) {
+                shouldLive &= !m_state.contains(alias);
+            });
+        if (!shouldLive) {
+            inst = Inst();
+            return;
+        }
+        
         // First handle some special instructions.
         switch (inst.kind.opcode) {
         case Move: {
@@ -227,6 +239,10 @@
         default:
             break;
         }
+        
+        // FIXME: This code should be taught how to simplify the spill-to-spill move
+        // instruction. Basically it needs to know to remove the scratch arg.
+        // https://bugs.webkit.org/show_bug.cgi?id=171133
 
         // Create a copy in case we invalidate the instruction. That doesn't happen often.
         Inst instCopy = inst;
@@ -316,6 +332,12 @@
         {
             return !!reg;
         }
+        
+        bool operator==(const RegConst& other) const
+        {
+            return reg == other.reg
+                && constant == other.constant;
+        }
 
         void dump(PrintStream& out) const
         {
@@ -348,6 +370,13 @@
         {
             return slot && reg;
         }
+        
+        bool operator==(const RegSlot& other) const
+        {
+            return slot == other.slot
+                && reg == other.reg
+                && mode == other.mode;
+        }
 
         void dump(PrintStream& out) const
         {
@@ -385,6 +414,12 @@
         {
             return slot;
         }
+        
+        bool operator==(const SlotConst& other) const
+        {
+            return slot == other.slot
+                && constant == other.constant;
+        }
 
         void dump(PrintStream& out) const
         {
@@ -398,16 +433,29 @@
     struct State {
         void addAlias(const RegConst& newAlias)
         {
-            regConst.append(newAlias);
+            return regConst.append(newAlias);
         }
         void addAlias(const RegSlot& newAlias)
         {
-            regSlot.append(newAlias);
+            return regSlot.append(newAlias);
         }
         void addAlias(const SlotConst& newAlias)
         {
-            slotConst.append(newAlias);
+            return slotConst.append(newAlias);
         }
+        
+        bool contains(const RegConst& alias)
+        {
+            return regConst.contains(alias);
+        }
+        bool contains(const RegSlot& alias)
+        {
+            return regSlot.contains(alias);
+        }
+        bool contains(const SlotConst& alias)
+        {
+            return slotConst.contains(alias);
+        }
 
         const RegConst* getRegConst(Reg reg) const
         {
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to