summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Anholt <eric@anholt.net>2010-04-01 18:07:08 -1000
committerIan Romanick <ian.d.romanick@intel.com>2010-04-02 11:22:41 -0700
commit160d092507c1ca341b7c5c88e5ba94b4cf5fb7d0 (patch)
tree2cb33458b814deded2b4b767cf9c2d1ce977afba
parent90b7825b0e92375dbe721d2dca1a4a3f1093f4ab (diff)
Simplify ir_constant_expression.cpp by factoring operand computation out.
-rw-r--r--ir.cpp58
-rw-r--r--ir.h6
-rw-r--r--ir_constant_expression.cpp36
3 files changed, 79 insertions, 21 deletions
diff --git a/ir.cpp b/ir.cpp
index 3939e5a7b5..35fecf7560 100644
--- 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 559f984478..381af357b8 100644
--- 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);
diff --git a/ir_constant_expression.cpp b/ir_constant_expression.cpp
index c7c4d7b244..476afe8b87 100644
--- a/ir_constant_expression.cpp
+++ b/ir_constant_expression.cpp
@@ -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;