Simplify ir_constant_expression.cpp by factoring operand computation out.

This commit is contained in:
Eric Anholt
2010-04-01 18:07:08 -10:00
committed by Ian Romanick
parent 90b7825b0e
commit 160d092507
3 changed files with 79 additions and 21 deletions

58
ir.cpp
View File

@@ -46,6 +46,64 @@ ir_expression::ir_expression(int op, const struct glsl_type *type,
this->operands[1] = op1; 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_label::ir_label(const char *label)
: ir_instruction(), label(label) : ir_instruction(), label(label)

6
ir.h
View File

@@ -299,7 +299,9 @@ public:
ir_rvalue *condition; 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 { enum ir_expression_operation {
ir_unop_bit_not, ir_unop_bit_not,
ir_unop_logic_not, ir_unop_logic_not,
@@ -370,6 +372,8 @@ public:
ir_expression(int op, const struct glsl_type *type, ir_expression(int op, const struct glsl_type *type,
ir_rvalue *, ir_rvalue *); ir_rvalue *, ir_rvalue *);
unsigned int get_num_operands(void);
virtual void accept(ir_visitor *v) virtual void accept(ir_visitor *v)
{ {
v->visit(this); v->visit(this);

View File

@@ -133,40 +133,36 @@ ir_constant_visitor::visit(ir_expression *ir)
{ {
value = NULL; value = NULL;
ir_constant *op[2]; 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) { switch (ir->operation) {
case ir_unop_logic_not: 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 = new ir_constant(!op[0]->value.b[0]);
value->type = glsl_type::bool_type; value->type = glsl_type::bool_type;
}
break; break;
case ir_binop_mul: case ir_binop_mul:
op[0] = ir->operands[0]->constant_expression_value(); if (ir->operands[0]->type == ir->operands[1]->type) {
op[1] = ir->operands[1]->constant_expression_value();
if (op[0] && op[1] && ir->operands[0]->type == ir->operands[1]->type) {
if (ir->operands[1]->type == glsl_type::float_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 = new ir_constant(op[0]->value.f[0] * op[1]->value.f[0]);
value->type = glsl_type::float_type; value->type = glsl_type::float_type;
} }
} }
if (value)
value->type = ir->operands[1]->type;
break; break;
case ir_binop_logic_and: 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 = new ir_constant(op[0]->value.b[0] && op[1]->value.b[0]);
value->type = glsl_type::bool_type; value->type = glsl_type::bool_type;
}
break; break;
case ir_binop_logic_or: 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 = new ir_constant(op[0]->value.b[0] || op[1]->value.b[0]);
value->type = glsl_type::bool_type; value->type = glsl_type::bool_type;
}
break; break;
default: default:
break; break;