Mesa (master): glsl: Delete dead discard conditions in constant folding.

Kenneth Graunke kwg at kemper.freedesktop.org
Tue Feb 24 23:28:19 UTC 2015


Module: Mesa
Branch: master
Commit: 23d42b46e306d4104e534842f6dec3500d331cae
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=23d42b46e306d4104e534842f6dec3500d331cae

Author: Kenneth Graunke <kenneth at whitecape.org>
Date:   Tue Feb 24 01:00:22 2015 -0800

glsl: Delete dead discard conditions in constant folding.

opt_constant_folding() already detects conditional assignments where the
condition is constant, and either deletes the assignment or the
condition.

Make it handle discards in the same fashion.

Spotted happening in the wild in Tropico 5 shaders.

Signed-off-by: Kenneth Graunke <kenneth at whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick at intel.com>
Reviewed-by: Connor Abbott <cwabbott0 at gmail.com>
Reviewed-by: Matt Turner <mattst88 at gmail.com>
Reviewed-by: Eric Anholt <eric at anholt.net>

---

 src/glsl/opt_constant_folding.cpp |   24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/src/glsl/opt_constant_folding.cpp b/src/glsl/opt_constant_folding.cpp
index 74b855e..4aae3f0 100644
--- a/src/glsl/opt_constant_folding.cpp
+++ b/src/glsl/opt_constant_folding.cpp
@@ -50,6 +50,7 @@ public:
       /* empty */
    }
 
+   virtual ir_visitor_status visit_enter(ir_discard *ir);
    virtual ir_visitor_status visit_enter(ir_assignment *ir);
    virtual ir_visitor_status visit_enter(ir_call *ir);
 
@@ -94,6 +95,29 @@ ir_constant_folding_visitor::handle_rvalue(ir_rvalue **rvalue)
 }
 
 ir_visitor_status
+ir_constant_folding_visitor::visit_enter(ir_discard *ir)
+{
+   if (ir->condition) {
+      ir->condition->accept(this);
+      handle_rvalue(&ir->condition);
+
+      ir_constant *const_val = ir->condition->as_constant();
+      /* If the condition is constant, either remove the condition or
+       * remove the never-executed assignment.
+       */
+      if (const_val) {
+         if (const_val->value.b[0])
+            ir->condition = NULL;
+         else
+            ir->remove();
+         this->progress = true;
+      }
+   }
+
+   return visit_continue_with_parent;
+}
+
+ir_visitor_status
 ir_constant_folding_visitor::visit_enter(ir_assignment *ir)
 {
    ir->rhs->accept(this);




More information about the mesa-commit mailing list