From acc253f0bc99f17480788104527b0760a88103c3 Mon Sep 17 00:00:00 2001
From: Hicham HAOUARI <hicham.haouari@gmail.com>
Date: Thu, 16 Dec 2010 00:55:17 +0000
Subject: [PATCH] Backport fdbz #31827 fix to 7.9

Signed-off-by: Hicham HAOUARI <hicham.haouari@gmail.com>
---
 src/glsl/Makefile                      |    2 +
 src/glsl/SConscript                    |    2 +
 src/glsl/glsl_parser_extras.cpp        |    1 +
 src/glsl/ir.h                          |    6 +
 src/glsl/ir_discard_simplification.cpp |  180 +++++++++++++++++++++++++++++
 src/glsl/ir_lower_jumps.cpp            |   10 +--
 src/glsl/ir_optimization.h             |    2 +
 src/glsl/lower_discard.cpp             |  198 ++++++++++++++++++++++++++++++++
 src/mesa/program/ir_to_mesa.cpp        |   13 ++-
 9 files changed, 402 insertions(+), 12 deletions(-)
 create mode 100644 src/glsl/ir_discard_simplification.cpp
 create mode 100644 src/glsl/lower_discard.cpp

diff --git a/src/glsl/Makefile b/src/glsl/Makefile
index 47ac426..ea099c6 100644
--- a/src/glsl/Makefile
+++ b/src/glsl/Makefile
@@ -42,6 +42,7 @@ CXX_SOURCES = \
 	ir_dead_code.cpp \
 	ir_dead_code_local.cpp \
 	ir_dead_functions.cpp \
+	ir_discard_simplification.cpp \
 	ir_div_to_mul_rcp.cpp \
 	ir_explog_to_explog2.cpp \
 	ir_expression_flattening.cpp \
@@ -75,6 +76,7 @@ CXX_SOURCES = \
 	loop_analysis.cpp \
 	loop_controls.cpp \
 	loop_unroll.cpp \
+	lower_discard.cpp \
 	lower_noise.cpp \
 	lower_variable_index_to_cond_assign.cpp \
 	opt_redundant_jumps.cpp \
diff --git a/src/glsl/SConscript b/src/glsl/SConscript
index c183e20..d5e41af 100644
--- a/src/glsl/SConscript
+++ b/src/glsl/SConscript
@@ -39,6 +39,7 @@ sources = [
     'ir_dead_code.cpp',
     'ir_dead_code_local.cpp',
     'ir_dead_functions.cpp',
+    'ir_discard_simplification.cpp'
     'ir_div_to_mul_rcp.cpp',
     'ir_explog_to_explog2.cpp',
     'ir_expression_flattening.cpp',
@@ -72,6 +73,7 @@ sources = [
     'loop_analysis.cpp',
     'loop_controls.cpp',
     'loop_unroll.cpp',
+    'lower_discard.cpp',
     'lower_noise.cpp',
     'lower_variable_index_to_cond_assign.cpp',
     'opt_redundant_jumps.cpp',
diff --git a/src/glsl/glsl_parser_extras.cpp b/src/glsl/glsl_parser_extras.cpp
index 33ea664..13fc253 100644
--- a/src/glsl/glsl_parser_extras.cpp
+++ b/src/glsl/glsl_parser_extras.cpp
@@ -702,6 +702,7 @@ do_common_optimization(exec_list *ir, bool linked, unsigned max_unroll_iteration
    }
    progress = do_structure_splitting(ir) || progress;
    progress = do_if_simplification(ir) || progress;
+   progress = do_discard_simplification(ir) || progress;
    progress = do_copy_propagation(ir) || progress;
    if (linked)
       progress = do_dead_code(ir) || progress;
diff --git a/src/glsl/ir.h b/src/glsl/ir.h
index fa246b5..e0347fb 100644
--- a/src/glsl/ir.h
+++ b/src/glsl/ir.h
@@ -125,6 +125,7 @@ public:
    virtual class ir_if *                as_if()               { return NULL; }
    virtual class ir_swizzle *           as_swizzle()          { return NULL; }
    virtual class ir_constant *          as_constant()         { return NULL; }
+   virtual class ir_discard *           as_discard()          { return NULL; }
    /*@}*/
 
 protected:
@@ -1044,6 +1045,11 @@ public:
 
    virtual ir_visitor_status accept(ir_hierarchical_visitor *);
 
+   virtual ir_discard *as_discard()
+   {
+      return this;
+   }
+
    ir_rvalue *condition;
 };
 /*@}*/
diff --git a/src/glsl/ir_discard_simplification.cpp b/src/glsl/ir_discard_simplification.cpp
new file mode 100644
index 0000000..0e577c4
--- /dev/null
+++ b/src/glsl/ir_discard_simplification.cpp
@@ -0,0 +1,180 @@
+/*
+ * Copyright © 2010 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 opt_discard_simplification.cpp
+ *
+ * This pass simplifies if-statements and loops containing unconditional
+ * discards.
+ *
+ * Case 1: Both branches contain unconditional discards:
+ * -----------------------------------------------------
+ *
+ *    if (cond) {
+ *	 s1;
+ *	 discard;
+ *	 s2;
+ *    } else {
+ *	 s3;
+ *	 discard;
+ *	 s4;
+ *    }
+ *
+ * becomes:
+ *
+ *    discard
+ *
+ * Case 2: The "then" clause contains an unconditional discard:
+ * ------------------------------------------------------------
+ *
+ *    if (cond) {
+ *       s1;
+ *       discard;
+ *       s2;
+ *    } else {
+ *	 s3;
+ *    }
+ *
+ * becomes:
+ *
+ *    if (cond) {
+ *	 discard;
+ *    } else {
+ *	 s3;
+ *    }
+ *
+ * Case 3: The "else" clause contains an unconditional discard:
+ * ------------------------------------------------------------
+ *
+ *    if (cond) {
+ *       s1;
+ *    } else {
+ *       s2;
+ *       discard;
+ *	 s3;
+ *    }
+ *
+ * becomes:
+ *
+ *    if (cond) {
+ *	 s1;
+ *    } else {
+ *	 discard;
+ *    }
+ */
+
+#include "glsl_types.h"
+#include "ir.h"
+
+class discard_simplifier : public ir_hierarchical_visitor {
+public:
+   discard_simplifier()
+   {
+      this->progress = false;
+   }
+
+   ir_visitor_status visit_enter(ir_if *);
+   ir_visitor_status visit_enter(ir_loop *);
+
+   bool progress;
+};
+
+static ir_discard *
+find_unconditional_discard(exec_list &instructions)
+{
+   foreach_list(n, &instructions) {
+      ir_discard *ir = ((ir_instruction *) n)->as_discard();
+      if (ir != NULL && ir->condition == NULL)
+	 return ir;
+   }
+   return NULL;
+}
+
+static bool
+is_only_instruction(ir_discard *discard)
+{
+   return (discard->prev->is_head_sentinel() &&
+	   discard->next->is_tail_sentinel());
+}
+
+ir_visitor_status
+discard_simplifier::visit_enter(ir_if *ir)
+{
+   ir_discard *then_discard = find_unconditional_discard(ir->then_instructions);
+   ir_discard *else_discard = find_unconditional_discard(ir->else_instructions);
+
+   if (then_discard == NULL && else_discard == NULL)
+      return visit_continue;
+
+   /* If both branches result in discard, replace whole if with discard. */
+   if (then_discard != NULL && else_discard != NULL) {
+      this->progress = true;
+      ir->replace_with(then_discard);
+      return visit_continue_with_parent;
+   }
+
+   /* Otherwise, one branch has a discard. */
+   if (then_discard != NULL && !is_only_instruction(then_discard)) {
+      this->progress = true;
+      ir->then_instructions.make_empty();
+      ir->then_instructions.push_tail(then_discard);
+   } else if (else_discard != NULL && !is_only_instruction(else_discard)) {
+      this->progress = true;
+      ir->else_instructions.make_empty();
+      ir->else_instructions.push_tail(else_discard);
+   }
+
+   visit_list_elements(this, &ir->then_instructions);
+   return visit_continue_with_parent;
+}
+
+ir_visitor_status
+discard_simplifier::visit_enter(ir_loop *ir)
+{
+   ir_discard *discard = find_unconditional_discard(ir->body_instructions);
+
+   if (discard) {
+      ir->replace_with(discard);
+      return visit_continue_with_parent;
+   }
+
+   return visit_continue;
+}
+
+bool
+do_discard_simplification(exec_list *instructions)
+{
+   /* Look for a top-level unconditional discard */
+   ir_discard *discard = find_unconditional_discard(*instructions);
+   if (discard != NULL) {
+      instructions->make_empty();
+      instructions->push_tail(discard);
+      return true;
+   }
+
+   discard_simplifier v;
+
+   visit_list_elements(&v, instructions);
+
+   return v.progress;
+}
diff --git a/src/glsl/ir_lower_jumps.cpp b/src/glsl/ir_lower_jumps.cpp
index b69cc1e..2ba94b4 100644
--- a/src/glsl/ir_lower_jumps.cpp
+++ b/src/glsl/ir_lower_jumps.cpp
@@ -35,8 +35,7 @@ enum jump_strength
    strength_always_clears_execute_flag,
    strength_continue,
    strength_break,
-   strength_return,
-   strength_discard
+   strength_return
 };
 
 struct block_record
@@ -202,8 +201,6 @@ struct ir_lower_jumps_visitor : public ir_control_flow_visitor {
 
    virtual void visit(class ir_discard * ir)
    {
-      truncate_after_instruction(ir);
-      this->block.min_strength = strength_discard;
    }
 
    enum jump_strength get_jump_strength(ir_instruction* ir)
@@ -217,8 +214,6 @@ struct ir_lower_jumps_visitor : public ir_control_flow_visitor {
             return strength_continue;
       } else if(ir->ir_type == ir_type_return)
          return strength_return;
-      else if(ir->ir_type == ir_type_discard)
-         return strength_discard;
       else
          return strength_none;
    }
@@ -253,9 +248,6 @@ struct ir_lower_jumps_visitor : public ir_control_flow_visitor {
          else
             lower = lower_sub_return;
          break;
-      case strength_discard:
-         lower = false; /* probably nothing needs this lowered */
-         break;
       }
       return lower;
    }
diff --git a/src/glsl/ir_optimization.h b/src/glsl/ir_optimization.h
index 6a37e16..ce939b3 100644
--- a/src/glsl/ir_optimization.h
+++ b/src/glsl/ir_optimization.h
@@ -45,6 +45,7 @@ bool do_explog_to_explog2(exec_list *instructions);
 bool do_function_inlining(exec_list *instructions);
 bool do_lower_jumps(exec_list *instructions, bool pull_out_jumps = true, bool lower_sub_return = true, bool lower_main_return = false, bool lower_continue = false, bool lower_break = false);
 bool do_if_simplification(exec_list *instructions);
+bool do_discard_simplification(exec_list *instructions);
 bool do_if_to_cond_assign(exec_list *instructions);
 bool do_mat_op_to_vec(exec_list *instructions);
 bool do_mod_to_fract(exec_list *instructions);
@@ -55,6 +56,7 @@ bool do_swizzle_swizzle(exec_list *instructions);
 bool do_tree_grafting(exec_list *instructions);
 bool do_vec_index_to_cond_assign(exec_list *instructions);
 bool do_vec_index_to_swizzle(exec_list *instructions);
+bool lower_discard(exec_list *instructions);
 bool lower_noise(exec_list *instructions);
 bool lower_variable_index_to_cond_assign(exec_list *instructions,
     bool lower_input, bool lower_output, bool lower_temp, bool lower_uniform);
diff --git a/src/glsl/lower_discard.cpp b/src/glsl/lower_discard.cpp
new file mode 100644
index 0000000..b95313d
--- /dev/null
+++ b/src/glsl/lower_discard.cpp
@@ -0,0 +1,198 @@
+/*
+ * Copyright © 2010 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 lower_discard.cpp
+ *
+ * This pass moves discards out of if-statements.
+ *
+ * Case 1: The "then" branch contains a conditional discard:
+ * ---------------------------------------------------------
+ *
+ *    if (cond1) {
+ *	 s1;
+ *	 discard cond2;
+ *	 s2;
+ *    } else {
+ *	 s3;
+ *    }
+ *
+ * becomes:
+ *
+ *    temp = false;
+ *    if (cond1) {
+ *	 s1;
+ *	 temp = cond2;
+ *	 s2;
+ *    } else {
+ *	 s3;
+ *    }
+ *    discard temp;
+ *
+ * Case 2: The "else" branch contains a conditional discard:
+ * ---------------------------------------------------------
+ *
+ *    if (cond1) {
+ *	 s1;
+ *    } else {
+ *	 s2;
+ *	 discard cond2;
+ *	 s3;
+ *    }
+ *
+ * becomes:
+ *
+ *    temp = false;
+ *    if (cond1) {
+ *	 s1;
+ *    } else {
+ *	 s2;
+ *	 temp = cond2;
+ *	 s3;
+ *    }
+ *    discard temp;
+ *
+ * Case 3: Both branches contain a conditional discard:
+ * ----------------------------------------------------
+ *
+ *    if (cond1) {
+ *	 s1;
+ *	 discard cond2;
+ *	 s2;
+ *    } else {
+ *	 s3;
+ *	 discard cond3;
+ *	 s4;
+ *    }
+ *
+ * becomes:
+ *
+ *    temp = false;
+ *    if (cond1) {
+ *	 s1;
+ *	 temp = cond2;
+ *	 s2;
+ *    } else {
+ *	 s3;
+ *	 temp = cond3;
+ *	 s4;
+ *    }
+ *    discard temp;
+ *
+ * If there are multiple conditional discards, we need only deal with one of
+ * them.  Repeatedly applying this pass will take care of the others.
+ *
+ * Unconditional discards are treated as having a condition of "true".
+ */
+
+#include "glsl_types.h"
+#include "ir.h"
+
+class lower_discard_visitor : public ir_hierarchical_visitor {
+public:
+   lower_discard_visitor()
+   {
+      this->progress = false;
+   }
+
+   ir_visitor_status visit_leave(ir_if *);
+
+   bool progress;
+};
+
+
+bool
+lower_discard(exec_list *instructions)
+{
+   lower_discard_visitor v;
+
+   visit_list_elements(&v, instructions);
+
+   return v.progress;
+}
+
+
+static ir_discard *
+find_discard(exec_list &instructions)
+{
+   foreach_list(n, &instructions) {
+      ir_discard *ir = ((ir_instruction *) n)->as_discard();
+      if (ir != NULL)
+	 return ir;
+   }
+   return NULL;
+}
+
+
+static void
+replace_discard(void *mem_ctx, ir_variable *var, ir_discard *ir)
+{
+   ir_rvalue *condition = ir->condition;
+
+   /* For unconditional discards, use "true" as the condition. */
+   if (condition == NULL)
+      condition = new(mem_ctx) ir_constant(true);
+
+   ir_assignment *assignment =
+      new(mem_ctx) ir_assignment(new(mem_ctx) ir_dereference_variable(var),
+				 condition, NULL);
+
+   ir->replace_with(assignment);
+}
+
+
+ir_visitor_status
+lower_discard_visitor::visit_leave(ir_if *ir)
+{
+   ir_discard *then_discard = find_discard(ir->then_instructions);
+   ir_discard *else_discard = find_discard(ir->else_instructions);
+
+   if (then_discard == NULL && else_discard == NULL)
+      return visit_continue;
+
+   void *mem_ctx = talloc_parent(ir);
+
+   ir_variable *temp = new(mem_ctx) ir_variable(glsl_type::bool_type,
+						"discard_cond_temp",
+						ir_var_temporary);
+   ir_assignment *temp_initializer =
+      new(mem_ctx) ir_assignment(new(mem_ctx) ir_dereference_variable(temp),
+				 new(mem_ctx) ir_constant(false), NULL);
+
+   ir->insert_before(temp);
+   ir->insert_before(temp_initializer);
+
+   if (then_discard != NULL)
+      replace_discard(mem_ctx, temp, then_discard);
+
+   if (else_discard != NULL)
+      replace_discard(mem_ctx, temp, else_discard);
+
+   ir_discard *discard = then_discard != NULL ? then_discard : else_discard;
+   discard->condition = new(mem_ctx) ir_dereference_variable(temp);
+   ir->insert_after(discard);
+
+   this->progress = true;
+
+   return visit_continue;
+}
diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp
index 12767c9..4737c6d 100644
--- a/src/mesa/program/ir_to_mesa.cpp
+++ b/src/mesa/program/ir_to_mesa.cpp
@@ -2137,9 +2137,14 @@ ir_to_mesa_visitor::visit(ir_discard *ir)
 {
    struct gl_fragment_program *fp = (struct gl_fragment_program *)this->prog;
 
-   assert(ir->condition == NULL); /* FINISHME */
+   if (ir->condition) {
+      ir->condition->accept(this);
+      this->result.negate = ~this->result.negate;
+      ir_to_mesa_emit_op1(ir, OPCODE_KIL, ir_to_mesa_undef_dst, this->result);
+   } else {
+      ir_to_mesa_emit_op0(ir, OPCODE_KIL_NV);
+   }
 
-   ir_to_mesa_emit_op0(ir, OPCODE_KIL_NV);
    fp->UsesKill = GL_TRUE;
 }
 
@@ -2796,8 +2801,10 @@ _mesa_ir_link_shader(GLcontext *ctx, struct gl_shader_program *prog)
 
 	 progress = do_common_optimization(ir, true, options->MaxUnrollIterations) || progress;
 
-	 if (options->EmitNoIfs)
+	 if (options->EmitNoIfs) {
+	    progress = lower_discard(ir) || progress;
 	    progress = do_if_to_cond_assign(ir) || progress;
+	 }
 
 	 if (options->EmitNoNoise)
 	    progress = lower_noise(ir) || progress;
-- 
1.7.3.3

