Check if both branches of an Cmm if have the same target.
authorklebinger.andreas@gmx.at <klebinger.andreas@gmx.at>
Thu, 7 Jun 2018 17:26:19 +0000 (13:26 -0400)
committerBen Gamari <ben@smart-cactus.org>
Thu, 7 Jun 2018 22:06:29 +0000 (18:06 -0400)
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

compiler/cmm/CmmContFlowOpt.hs
testsuite/tests/cmm/opt/Makefile [new file with mode: 0644]
testsuite/tests/cmm/opt/T15188.cmm [new file with mode: 0644]
testsuite/tests/cmm/opt/T15188.stdout [new file with mode: 0644]
testsuite/tests/cmm/opt/all.T [new file with mode: 0644]

index 9f091da..146c4f3 100644 (file)
@@ -254,8 +254,8 @@ blockConcat splitting_procs g@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@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 (file)
index 0000000..3c462ec
--- /dev/null
@@ -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 (file)
index 0000000..59df92d
--- /dev/null
@@ -0,0 +1,6 @@
+func(bits64 r2) {
+  foo:
+    if (r2 == 0) { goto bar; } else { goto bar; }
+  bar:
+    return (1);
+}
diff --git a/testsuite/tests/cmm/opt/T15188.stdout b/testsuite/tests/cmm/opt/T15188.stdout
new file mode 100644 (file)
index 0000000..d00491f
--- /dev/null
@@ -0,0 +1 @@
+1
diff --git a/testsuite/tests/cmm/opt/all.T b/testsuite/tests/cmm/opt/all.T
new file mode 100644 (file)
index 0000000..b2c0f5f
--- /dev/null
@@ -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'])