Simplify ir_constant_expression.cpp by factoring operand computation out.
authorEric Anholt <eric@anholt.net>
Fri, 2 Apr 2010 04:07:08 +0000 (18:07 -1000)
committerIan Romanick <ian.d.romanick@intel.com>
Fri, 2 Apr 2010 18:22:41 +0000 (11:22 -0700)
ir.cpp
ir.h
ir_constant_expression.cpp

diff --git a/ir.cpp b/ir.cpp
index 3939e5a7b553d4030c98e378fed7a04de77ba500..35fecf75608aef33e0aa27ffbab7cdc4ece11eac 100644 (file)
--- a/ir.cpp
+++ b/ir.cpp
@@ -46,6 +46,64 @@ ir_expression::ir_expression(int op, const struct glsl_type *type,
    this->operands[1] = op1;
 }
 
+unsigned int
+ir_expression::get_num_operands(void)
+{
+/* Update ir_print_visitor.cpp when updating this list. */
+   const int num_operands[] = {
+      1, /* ir_unop_bit_not */
+      1, /* ir_unop_logic_not */
+      1, /* ir_unop_neg */
+      1, /* ir_unop_abs */
+      1, /* ir_unop_rcp */
+      1, /* ir_unop_rsq */
+      1, /* ir_unop_sqrt */
+      1, /* ir_unop_exp */
+      1, /* ir_unop_log */
+      1, /* ir_unop_exp2 */
+      1, /* ir_unop_log2 */
+      1, /* ir_unop_f2i */
+      1, /* ir_unop_i2f */
+      1, /* ir_unop_u2f */
+
+      1, /* ir_unop_trunc */
+      1, /* ir_unop_ceil */
+      1, /* ir_unop_floor */
+
+      2, /* ir_binop_add */
+      2, /* ir_binop_sub */
+      2, /* ir_binop_mul */
+      2, /* ir_binop_div */
+      2, /* ir_binop_mod */
+
+      2, /* ir_binop_less */
+      2, /* ir_binop_greater */
+      2, /* ir_binop_lequal */
+      2, /* ir_binop_gequal */
+      2, /* ir_binop_equal */
+      2, /* ir_binop_nequal */
+
+      2, /* ir_binop_lshift */
+      2, /* ir_binop_rshift */
+      2, /* ir_binop_bit_and */
+      2, /* ir_binop_bit_xor */
+      2, /* ir_binop_bit_or */
+
+      2, /* ir_binop_logic_and */
+      2, /* ir_binop_logic_xor */
+      2, /* ir_binop_logic_or */
+
+      2, /* ir_binop_dot */
+      2, /* ir_binop_min */
+      2, /* ir_binop_max */
+
+      2, /* ir_binop_pow */
+   };
+
+   assert(sizeof(num_operands) / sizeof(num_operands[0]) == ir_binop_pow + 1);
+
+   return num_operands[this->operation];
+}
 
 ir_label::ir_label(const char *label)
    : ir_instruction(), label(label)
diff --git a/ir.h b/ir.h
index 559f9844788a4d3908c939f77a34a152abb4d114..381af357b81dcbde287b0c4f424483d6b1112c21 100644 (file)
--- a/ir.h
+++ b/ir.h
@@ -299,7 +299,9 @@ public:
    ir_rvalue *condition;
 };
 
-/* Update ir_print_visitor.cpp when updating this list. */
+/* Update ir_expression::num_operands() and ir_print_visitor.cpp when
+ * updating this list.
+*/
 enum ir_expression_operation {
    ir_unop_bit_not,
    ir_unop_logic_not,
@@ -370,6 +372,8 @@ public:
    ir_expression(int op, const struct glsl_type *type,
                 ir_rvalue *, ir_rvalue *);
 
+   unsigned int get_num_operands(void);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
index c7c4d7b2449784d6b17cd6e072d491c093db6117..476afe8b87d4fb2e679fc47404e90cf5ca24c4fd 100644 (file)
@@ -133,40 +133,36 @@ ir_constant_visitor::visit(ir_expression *ir)
 {
    value = NULL;
    ir_constant *op[2];
+   unsigned int i;
+
+   for (i = 0; i < ir->get_num_operands(); i++) {
+      op[i] = ir->operands[i]->constant_expression_value();
+      if (!op[i])
+        return;
+   }
 
    switch (ir->operation) {
    case ir_unop_logic_not:
-      op[0] = ir->operands[0]->constant_expression_value();
-      if (op[0]) {
-        value = new ir_constant(!op[0]->value.b[0]);
-        value->type = glsl_type::bool_type;
-      }
+      value = new ir_constant(!op[0]->value.b[0]);
+      value->type = glsl_type::bool_type;
       break;
    case ir_binop_mul:
-      op[0] = ir->operands[0]->constant_expression_value();
-      op[1] = ir->operands[1]->constant_expression_value();
-      if (op[0] && op[1] && ir->operands[0]->type == ir->operands[1]->type) {
+      if (ir->operands[0]->type == ir->operands[1]->type) {
         if (ir->operands[1]->type == glsl_type::float_type) {
            value = new ir_constant(op[0]->value.f[0] * op[1]->value.f[0]);
            value->type = glsl_type::float_type;
         }
       }
+      if (value)
+        value->type = ir->operands[1]->type;
       break;
    case ir_binop_logic_and:
-      op[0] = ir->operands[0]->constant_expression_value();
-      op[1] = ir->operands[1]->constant_expression_value();
-      if (op[0] && op[1]) {
-        value = new ir_constant(op[0]->value.b[0] && op[1]->value.b[0]);
-        value->type = glsl_type::bool_type;
-      }
+      value = new ir_constant(op[0]->value.b[0] && op[1]->value.b[0]);
+      value->type = glsl_type::bool_type;
       break;
    case ir_binop_logic_or:
-      op[0] = ir->operands[0]->constant_expression_value();
-      op[1] = ir->operands[1]->constant_expression_value();
-      if (op[0] && op[1]) {
-        value = new ir_constant(op[0]->value.b[0] || op[1]->value.b[0]);
-        value->type = glsl_type::bool_type;
-      }
+      value = new ir_constant(op[0]->value.b[0] || op[1]->value.b[0]);
+      value->type = glsl_type::bool_type;
       break;
    default:
       break;