[Mesa-dev] [PATCH 20/22] glsl: Optimize some cases of undefined behaviour.

Thomas Helland thomashelland90 at gmail.com
Sat Jan 3 11:18:25 PST 2015


---
 src/glsl/opt_minmax.cpp | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/src/glsl/opt_minmax.cpp b/src/glsl/opt_minmax.cpp
index 441ac69..07d3d53 100644
--- a/src/glsl/opt_minmax.cpp
+++ b/src/glsl/opt_minmax.cpp
@@ -78,6 +78,8 @@ public:
 
    ir_rvalue *opt_saturate(ir_expression *expr, minmax_range baserange);
 
+   ir_rvalue *opt_undefined_behaviour(ir_expression *expr, minmax_range baserange);
+
    void handle_rvalue(ir_rvalue **rvalue);
 
    bool progress;
@@ -559,6 +561,28 @@ get_range(ir_rvalue *rval)
 }
 
 /**
+ * Sets to 0 operations that we know are to trigger undefined behaviour
+ * based on the range of the operand
+ *
+ * @param baserange the range of this expression that will be used by its
+ * parents, if this is applicable
+ */
+ir_rvalue *
+ir_minmax_visitor::opt_undefined_behaviour(ir_expression *expr, minmax_range baserange)
+{
+   void *mem_ctx = ralloc_parent(expr);
+
+   // Get the range for the operand
+   minmax_range r0 = get_range(expr->operands[0]);
+
+   // Operand <= 0.0 gives 0.0
+   if (IS_CONSTANT(r0.high, <=, 0.0f))
+      return new(mem_ctx) ir_constant(0.0f);
+
+   return expr;
+}
+
+/**
  * Eliminates saturate-operations that are defined to either
  * 0 or 1 based on the operand being less than zero, or larger than 1.
  *
@@ -746,6 +770,13 @@ ir_minmax_visitor::handle_rvalue(ir_rvalue **rvalue)
    if (expr->operation == ir_unop_saturate)
       new_rvalue = opt_saturate(expr, minmax_range());
 
+   if (expr->operation == ir_unop_sqrt ||
+       expr->operation == ir_unop_rsq ||
+       expr->operation == ir_binop_pow ||
+       expr->operation == ir_unop_log ||
+       expr->operation == ir_unop_log2)
+      new_rvalue = opt_undefined_behaviour(expr, minmax_range());
+
    if (!new_rvalue || new_rvalue == *rvalue)
       return;
 
-- 
2.2.1



More information about the mesa-dev mailing list