i965/fs: Add fs_reg::is_zero() and is_one(); use for opt_algebraic().

These helper macros save you from writing nasty expressions like:

   if ((inst->src[1].type == BRW_REGISTER_TYPE_F &&
         inst->src[1].imm.f == 1.0) ||
        ((inst->src[1].type == BRW_REGISTER_TYPE_D ||
          inst->src[1].type == BRW_REGISTER_TYPE_UD) &&
         inst->src[1].imm.u == 1)) {

Instead, you simply get to write inst->src[1].is_one().  Simple.
Also, this makes the FS backend match the VS backend (which has these).

This patch also converts opt_algebraic to use the new helper functions.
As a consequence, it will now also optimize integer-typed expressions.

Reviewed-by: Eric Anholt <eric@anholt.net>
This commit is contained in:
Kenneth Graunke 2012-11-17 15:10:53 -08:00 committed by Eric Anholt
parent 4cedb65a43
commit 8d0bb74a11
2 changed files with 24 additions and 7 deletions

View file

@ -353,6 +353,24 @@ fs_reg::equals(const fs_reg &r) const
imm.u == r.imm.u); imm.u == r.imm.u);
} }
bool
fs_reg::is_zero() const
{
if (file != IMM)
return false;
return type == BRW_REGISTER_TYPE_F ? imm.f == 0.0 : imm.i == 0;
}
bool
fs_reg::is_one() const
{
if (file != IMM)
return false;
return type == BRW_REGISTER_TYPE_F ? imm.f == 1.0 : imm.i == 1;
}
int int
fs_visitor::type_size(const struct glsl_type *type) fs_visitor::type_size(const struct glsl_type *type)
{ {
@ -1430,8 +1448,7 @@ fs_visitor::opt_algebraic()
continue; continue;
/* a * 1.0 = a */ /* a * 1.0 = a */
if (inst->src[1].type == BRW_REGISTER_TYPE_F && if (inst->src[1].is_one()) {
inst->src[1].imm.f == 1.0) {
inst->opcode = BRW_OPCODE_MOV; inst->opcode = BRW_OPCODE_MOV;
inst->src[1] = reg_undef; inst->src[1] = reg_undef;
progress = true; progress = true;
@ -1439,10 +1456,9 @@ fs_visitor::opt_algebraic()
} }
/* a * 0.0 = 0.0 */ /* a * 0.0 = 0.0 */
if (inst->src[1].type == BRW_REGISTER_TYPE_F && if (inst->src[1].is_zero()) {
inst->src[1].imm.f == 0.0) {
inst->opcode = BRW_OPCODE_MOV; inst->opcode = BRW_OPCODE_MOV;
inst->src[0] = fs_reg(0.0f); inst->src[0] = inst->src[1];
inst->src[1] = reg_undef; inst->src[1] = reg_undef;
progress = true; progress = true;
break; break;
@ -1454,8 +1470,7 @@ fs_visitor::opt_algebraic()
continue; continue;
/* a + 0.0 = a */ /* a + 0.0 = a */
if (inst->src[1].type == BRW_REGISTER_TYPE_F && if (inst->src[1].is_zero()) {
inst->src[1].imm.f == 0.0) {
inst->opcode = BRW_OPCODE_MOV; inst->opcode = BRW_OPCODE_MOV;
inst->src[1] = reg_undef; inst->src[1] = reg_undef;
progress = true; progress = true;

View file

@ -91,6 +91,8 @@ public:
fs_reg(class fs_visitor *v, const struct glsl_type *type); fs_reg(class fs_visitor *v, const struct glsl_type *type);
bool equals(const fs_reg &r) const; bool equals(const fs_reg &r) const;
bool is_zero() const;
bool is_one() const;
/** Register file: ARF, GRF, MRF, IMM. */ /** Register file: ARF, GRF, MRF, IMM. */
enum register_file file; enum register_file file;