glsl: Consolidate duplicate copies of constant folding.
authorKenneth Graunke <kenneth@whitecape.org>
Fri, 29 Apr 2016 21:40:26 +0000 (14:40 -0700)
committerKenneth Graunke <kenneth@whitecape.org>
Mon, 16 May 2016 06:59:20 +0000 (23:59 -0700)
We could probably clean this up more (maybe make it a method), but at
least there's only one copy of this code now, and that's a start.

No change in shader-db.

Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Ian Romanick <ian.d.romanick@intel.com>
src/compiler/glsl/ir_optimization.h
src/compiler/glsl/opt_constant_folding.cpp
src/compiler/glsl/opt_constant_propagation.cpp

index f9599a39ff5c917f14338484c1d1c127e2f2b512..5fc27409a05ef5e2e23251f84e74a8b1c3269976 100644 (file)
@@ -73,6 +73,8 @@ bool do_common_optimization(exec_list *ir, bool linked,
                             const struct gl_shader_compiler_options *options,
                             bool native_integers);
 
+bool ir_constant_fold(ir_rvalue **rvalue);
+
 bool do_rebalance_tree(exec_list *instructions);
 bool do_algebraic(exec_list *instructions, bool native_integers,
                   const struct gl_shader_compiler_options *options);
index de13c9e068ec67b7eb5307918583a8dcd7740d84..0ea53a5ed1b2020c327882ecf54ce6344294f10c 100644 (file)
@@ -61,11 +61,11 @@ public:
 
 } /* unnamed namespace */
 
-void
-ir_constant_folding_visitor::handle_rvalue(ir_rvalue **rvalue)
+bool
+ir_constant_fold(ir_rvalue **rvalue)
 {
    if (*rvalue == NULL || (*rvalue)->ir_type == ir_type_constant)
-      return;
+      return false;
 
    /* Note that we do rvalue visitoring on leaving.  So if an
     * expression has a non-constant operand, no need to go looking
@@ -76,20 +76,28 @@ ir_constant_folding_visitor::handle_rvalue(ir_rvalue **rvalue)
    if (expr) {
       for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
         if (!expr->operands[i]->as_constant())
-           return;
+           return false;
       }
    }
 
    /* Ditto for swizzles. */
    ir_swizzle *swiz = (*rvalue)->as_swizzle();
    if (swiz && !swiz->val->as_constant())
-      return;
+      return false;
 
    ir_constant *constant = (*rvalue)->constant_expression_value();
    if (constant) {
       *rvalue = constant;
-      this->progress = true;
+      return true;
    }
+   return false;
+}
+
+void
+ir_constant_folding_visitor::handle_rvalue(ir_rvalue **rvalue)
+{
+   if (ir_constant_fold(rvalue))
+      this->progress = true;
 }
 
 ir_visitor_status
index 4764d16de6d3f8bc44c6d7abec2224c129c9c7b3..4b82bd13f9f1f36ccbe47c7f540068127245ed9e 100644 (file)
@@ -136,33 +136,10 @@ public:
 
 
 void
-ir_constant_propagation_visitor::constant_folding(ir_rvalue **rvalue) {
-
-   if (*rvalue == NULL || (*rvalue)->ir_type == ir_type_constant)
-      return;
-
-   /* Note that we visit rvalues one leaving.  So if an expression has a
-    * non-constant operand, no need to go looking down it to find if it's
-    * constant.  This cuts the time of this pass down drastically.
-    */
-   ir_expression *expr = (*rvalue)->as_expression();
-   if (expr) {
-      for (unsigned int i = 0; i < expr->get_num_operands(); i++) {
-        if (!expr->operands[i]->as_constant())
-           return;
-      }
-   }
-
-   /* Ditto for swizzles. */
-   ir_swizzle *swiz = (*rvalue)->as_swizzle();
-   if (swiz && !swiz->val->as_constant())
-      return;
-
-   ir_constant *constant = (*rvalue)->constant_expression_value();
-   if (constant) {
-      *rvalue = constant;
+ir_constant_propagation_visitor::constant_folding(ir_rvalue **rvalue)
+{
+   if (ir_constant_fold(rvalue))
       this->progress = true;
-   }
 }
 
 void