[commit: ghc] master: Check if both branches of an Cmm if have the same target. (efea32c)

git at git.haskell.org git at git.haskell.org
Fri Jun 8 00:07:28 UTC 2018


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/efea32cf2c41d35f2ba5a79bf70cc7768b7b0fd5/ghc

>---------------------------------------------------------------

commit efea32cf2c41d35f2ba5a79bf70cc7768b7b0fd5
Author: klebinger.andreas at gmx.at <klebinger.andreas at gmx.at>
Date:   Thu Jun 7 13:26:19 2018 -0400

    Check if both branches of an Cmm if have the same target.
    
    This for some reason or the other and makes it into the final
    binary. I've added the check to ContFlowOpt as that seems
    like a logical place for this.
    
    In a regular nofib run there were 30 occurences of this pattern.
    
    Test Plan: ci
    
    Reviewers: bgamari, simonmar, dfeuer, jrtc27, tdammers
    
    Reviewed By: bgamari, simonmar
    
    Subscribers: tdammers, dfeuer, rwbarton, thomie, carter
    
    GHC Trac Issues: #15188
    
    Differential Revision: https://phabricator.haskell.org/D4740


>---------------------------------------------------------------

efea32cf2c41d35f2ba5a79bf70cc7768b7b0fd5
 compiler/cmm/CmmContFlowOpt.hs                              | 13 +++++++++----
 testsuite/tests/cmm/opt/Makefile                            |  6 ++++++
 testsuite/tests/cmm/opt/T15188.cmm                          |  6 ++++++
 .../should_run/bkprun02.stdout => cmm/opt/T15188.stdout}    |  0
 testsuite/tests/cmm/opt/all.T                               |  3 +++
 5 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/compiler/cmm/CmmContFlowOpt.hs b/compiler/cmm/CmmContFlowOpt.hs
index 9f091da..146c4f3 100644
--- a/compiler/cmm/CmmContFlowOpt.hs
+++ b/compiler/cmm/CmmContFlowOpt.hs
@@ -254,8 +254,8 @@ blockConcat splitting_procs g at CmmGraph { g_entry = entry_id }
         -- unconditional jump to a block that can be shortcut.
         | Nothing <- callContinuation_maybe last
         = let oldSuccs = successors last
-              newSuccs = successors swapcond_last
-          in ( mapInsert bid (blockJoinTail head swapcond_last) blocks
+              newSuccs = successors rewrite_last
+          in ( mapInsert bid (blockJoinTail head rewrite_last) blocks
              , shortcut_map
              , if oldSuccs == newSuccs
                then backEdges
@@ -283,8 +283,13 @@ blockConcat splitting_procs g at CmmGraph { g_entry = entry_id }
                    Just b | Just dest <- canShortcut b -> dest
                    _otherwise -> l
 
-          -- See Note [Invert Cmm conditionals]
-          swapcond_last
+          rewrite_last
+            -- Sometimes we can get rid of the conditional completely.
+            | CmmCondBranch _cond t f _l <- shortcut_last
+            , t == f
+            = CmmBranch t
+
+            -- See Note [Invert Cmm conditionals]
             | CmmCondBranch cond t f l <- shortcut_last
             , hasOnePredecessor t -- inverting will make t a fallthrough
             , likelyTrue l || (numPreds f > 1)
diff --git a/testsuite/tests/cmm/opt/Makefile b/testsuite/tests/cmm/opt/Makefile
new file mode 100644
index 0000000..3c462ec
--- /dev/null
+++ b/testsuite/tests/cmm/opt/Makefile
@@ -0,0 +1,6 @@
+TOP=../../..
+include $(TOP)/mk/boilerplate.mk
+include $(TOP)/mk/test.mk
+
+T15188:
+	'$(TEST_HC)' $(TEST_HC_OPTS) -c -O -ddump-cmm-cps -dsuppress-all T15188.cmm | grep if ; echo $$?
diff --git a/testsuite/tests/cmm/opt/T15188.cmm b/testsuite/tests/cmm/opt/T15188.cmm
new file mode 100644
index 0000000..59df92d
--- /dev/null
+++ b/testsuite/tests/cmm/opt/T15188.cmm
@@ -0,0 +1,6 @@
+func(bits64 r2) {
+  foo:
+    if (r2 == 0) { goto bar; } else { goto bar; }
+  bar:
+    return (1);
+}
diff --git a/testsuite/tests/backpack/should_run/bkprun02.stdout b/testsuite/tests/cmm/opt/T15188.stdout
similarity index 100%
copy from testsuite/tests/backpack/should_run/bkprun02.stdout
copy to testsuite/tests/cmm/opt/T15188.stdout
diff --git a/testsuite/tests/cmm/opt/all.T b/testsuite/tests/cmm/opt/all.T
new file mode 100644
index 0000000..b2c0f5f
--- /dev/null
+++ b/testsuite/tests/cmm/opt/all.T
@@ -0,0 +1,3 @@
+# Verify that we optimize away conditional branches which always jump
+# to the same target.
+test('T15188', normal, run_command, ['$MAKE -s --no-print-directory T15188'])



More information about the ghc-commits mailing list