B3 should run tail duplication at the bitter end
https://bugs.webkit.org/show_bug.cgi?id=185123

Reviewed by Geoffrey Garen.
        
Also added an option to disable taildup. This appears to be a 1% AsmBench speed-up. It's neutral
everywhere else.
        
The goal of this change is to allow us to run path specialization after switch lowering but
before tail duplication.

* b3/B3Generate.cpp:
(JSC::B3::generateToAir):
* runtime/Options.h:



git-svn-id: http://svn.webkit.org/repository/webkit/trunk@231154 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/JavaScriptCore/ChangeLog b/Source/JavaScriptCore/ChangeLog
index c38034d..4d0f8b5 100644
--- a/Source/JavaScriptCore/ChangeLog
+++ b/Source/JavaScriptCore/ChangeLog
@@ -1,3 +1,20 @@
+2018-04-29  Filip Pizlo  <fpizlo@apple.com>
+
+        B3 should run tail duplication at the bitter end
+        https://bugs.webkit.org/show_bug.cgi?id=185123
+
+        Reviewed by Geoffrey Garen.
+        
+        Also added an option to disable taildup. This appears to be a 1% AsmBench speed-up. It's neutral
+        everywhere else.
+        
+        The goal of this change is to allow us to run path specialization after switch lowering but
+        before tail duplication.
+
+        * b3/B3Generate.cpp:
+        (JSC::B3::generateToAir):
+        * runtime/Options.h:
+
 2018-04-29  Commit Queue  <commit-queue@webkit.org>
 
         Unreviewed, rolling out r231137.
diff --git a/Source/JavaScriptCore/b3/B3Generate.cpp b/Source/JavaScriptCore/b3/B3Generate.cpp
index 975ba66..c498345 100644
--- a/Source/JavaScriptCore/b3/B3Generate.cpp
+++ b/Source/JavaScriptCore/b3/B3Generate.cpp
@@ -80,19 +80,14 @@
     
     if (shouldValidateIR())
         validate(procedure);
-
+    
     if (procedure.optLevel() >= 2) {
         reduceDoubleToFloat(procedure);
         reduceStrength(procedure);
         hoistLoopInvariantValues(procedure);
         if (eliminateCommonSubexpressions(procedure))
             eliminateCommonSubexpressions(procedure);
-        foldPathConstants(procedure);
-        reduceStrength(procedure);
         inferSwitches(procedure);
-        duplicateTails(procedure);
-        fixSSA(procedure);
-        foldPathConstants(procedure);
         
         // FIXME: Add more optimizations here.
         // https://bugs.webkit.org/show_bug.cgi?id=150507
@@ -106,6 +101,11 @@
 
     if (procedure.optLevel() >= 2) {
         reduceStrength(procedure);
+        if (Options::useB3TailDup())
+            duplicateTails(procedure);
+        fixSSA(procedure);
+        foldPathConstants(procedure);
+        reduceStrength(procedure);
 
         // FIXME: Add more optimizations here.
         // https://bugs.webkit.org/show_bug.cgi?id=150507
diff --git a/Source/JavaScriptCore/runtime/Options.h b/Source/JavaScriptCore/runtime/Options.h
index b3e7954..5dfc8aa 100644
--- a/Source/JavaScriptCore/runtime/Options.h
+++ b/Source/JavaScriptCore/runtime/Options.h
@@ -437,6 +437,7 @@
     v(bool, airRandomizeRegs, false, Normal, nullptr) \
     v(bool, coalesceSpillSlots, true, Normal, nullptr) \
     v(bool, logAirRegisterPressure, false, Normal, nullptr) \
+    v(bool, useB3TailDup, true, Normal, nullptr) \
     v(unsigned, maxB3TailDupBlockSize, 3, Normal, nullptr) \
     v(unsigned, maxB3TailDupBlockSuccessors, 3, Normal, nullptr) \
     \