[Mesa-dev] [PATCH 10/15] i965/fs: Add a pass to remove dead control flow.

Matt Turner mattst88 at gmail.com
Wed Oct 30 07:36:16 CET 2013


Removes if/endif and if/else/endif.

total instructions in shared programs: 1366420 -> 1356988 (-0.69%)
instructions in affected programs:     160818 -> 151386 (-5.87%)
---
Split mistakenly squashed patch.

 src/mesa/drivers/dri/i965/Makefile.sources         |  1 +
 src/mesa/drivers/dri/i965/brw_fs.cpp               |  1 +
 src/mesa/drivers/dri/i965/brw_fs.h                 |  1 +
 .../drivers/dri/i965/brw_fs_dead_control_flow.cpp  | 80 ++++++++++++++++++++++
 4 files changed, 83 insertions(+)
 create mode 100644 src/mesa/drivers/dri/i965/brw_fs_dead_control_flow.cpp

diff --git a/src/mesa/drivers/dri/i965/Makefile.sources b/src/mesa/drivers/dri/i965/Makefile.sources
index 5ddb421..37a8380 100644
--- a/src/mesa/drivers/dri/i965/Makefile.sources
+++ b/src/mesa/drivers/dri/i965/Makefile.sources
@@ -56,6 +56,7 @@ i965_FILES = \
 	brw_fs_channel_expressions.cpp \
 	brw_fs_copy_propagation.cpp \
 	brw_fs_cse.cpp \
+	brw_fs_dead_control_flow.cpp \
 	brw_fs_fp.cpp \
 	brw_fs_generator.cpp \
 	brw_fs_live_variables.cpp \
diff --git a/src/mesa/drivers/dri/i965/brw_fs.cpp b/src/mesa/drivers/dri/i965/brw_fs.cpp
index 7a4e005..4f1d2ca 100644
--- a/src/mesa/drivers/dri/i965/brw_fs.cpp
+++ b/src/mesa/drivers/dri/i965/brw_fs.cpp
@@ -3133,6 +3133,7 @@ fs_visitor::run()
          progress = opt_peephole_sel() || progress;
 	 progress = dead_code_eliminate() || progress;
 	 progress = dead_code_eliminate_local() || progress;
+         progress = dead_control_flow_eliminate() || progress;
 	 progress = register_coalesce() || progress;
 	 progress = register_coalesce_2() || progress;
 	 progress = compute_to_mrf() || progress;
diff --git a/src/mesa/drivers/dri/i965/brw_fs.h b/src/mesa/drivers/dri/i965/brw_fs.h
index a67ef86..208d3ab 100644
--- a/src/mesa/drivers/dri/i965/brw_fs.h
+++ b/src/mesa/drivers/dri/i965/brw_fs.h
@@ -318,6 +318,7 @@ public:
    bool compute_to_mrf();
    bool dead_code_eliminate();
    bool dead_code_eliminate_local();
+   bool dead_control_flow_eliminate();
    bool remove_dead_constants();
    bool remove_duplicate_mrf_writes();
    bool virtual_grf_interferes(int a, int b);
diff --git a/src/mesa/drivers/dri/i965/brw_fs_dead_control_flow.cpp b/src/mesa/drivers/dri/i965/brw_fs_dead_control_flow.cpp
new file mode 100644
index 0000000..4e8fdcd
--- /dev/null
+++ b/src/mesa/drivers/dri/i965/brw_fs_dead_control_flow.cpp
@@ -0,0 +1,80 @@
+/*
+ * Copyright © 2013 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+/** @file brw_fs_dead_control_flow.cpp
+ *
+ * This file implements the dead control flow elimination optimization pass.
+ */
+
+#include "brw_fs.h"
+#include "brw_cfg.h"
+
+/* Look for and eliminate dead control flow:
+ *
+ *   - if/endif
+ *   - if/else/endif
+ */
+bool
+fs_visitor::dead_control_flow_eliminate()
+{
+   bool progress = false;
+
+   cfg_t cfg(this);
+
+   for (int b = 0; b < cfg.num_blocks; b++) {
+      bblock_t *block = cfg.blocks[b];
+      bool found = false;
+
+      /* ENDIF instructions, by definition, can only be found at the ends of
+       * basic blocks.
+       */
+      fs_inst *endif_inst = (fs_inst *) block->end;
+      if (endif_inst->opcode != BRW_OPCODE_ENDIF)
+         continue;
+
+      fs_inst *if_inst = NULL, *else_inst = NULL;
+      fs_inst *prev_inst = (fs_inst *) endif_inst->prev;
+      if (prev_inst->opcode == BRW_OPCODE_IF) {
+         if_inst = prev_inst;
+         found = true;
+      } else if (prev_inst->opcode == BRW_OPCODE_ELSE) {
+         else_inst = prev_inst;
+
+         prev_inst = (fs_inst *) prev_inst->prev;
+         if (prev_inst->opcode == BRW_OPCODE_IF) {
+            if_inst = prev_inst;
+            found = true;
+         }
+      }
+
+      if (found) {
+         if_inst->remove();
+         if (else_inst)
+            else_inst->remove();
+         endif_inst->remove();
+         progress = true;
+      }
+   }
+
+   return progress;
+}
-- 
1.8.3.2



More information about the mesa-dev mailing list