i965: Add a devinfo field to backend_visitor and use it for gen checks
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_cse.cpp
index 817fc1f1a1fd4d7435ef08ca6f75f1a3e69b510b..c1d06161cbf1311a0a006f76e8e2b5fb43d93dd7 100644 (file)
@@ -62,6 +62,7 @@ static bool
 is_expression(const fs_inst *const inst)
 {
    switch (inst->opcode) {
+   case BRW_OPCODE_MOV:
    case BRW_OPCODE_SEL:
    case BRW_OPCODE_NOT:
    case BRW_OPCODE_AND:
@@ -99,7 +100,7 @@ is_expression(const fs_inst *const inst)
    case SHADER_OPCODE_INT_REMAINDER:
    case SHADER_OPCODE_SIN:
    case SHADER_OPCODE_COS:
-      return inst->mlen == 0;
+      return inst->mlen < 2;
    case SHADER_OPCODE_LOAD_PAYLOAD:
       return !is_copy_payload(inst);
    default:
@@ -108,27 +109,45 @@ is_expression(const fs_inst *const inst)
 }
 
 static bool
-is_expression_commutative(enum opcode op)
-{
-   switch (op) {
-   case BRW_OPCODE_AND:
-   case BRW_OPCODE_OR:
-   case BRW_OPCODE_XOR:
-   case BRW_OPCODE_ADD:
-   case BRW_OPCODE_MUL:
-      return true;
-   default:
-      return false;
-   }
-}
-
-static bool
-operands_match(fs_inst *a, fs_inst *b)
+operands_match(const fs_inst *a, const fs_inst *b, bool *negate)
 {
    fs_reg *xs = a->src;
    fs_reg *ys = b->src;
 
-   if (!is_expression_commutative(a->opcode)) {
+   if (a->opcode == BRW_OPCODE_MAD) {
+      return xs[0].equals(ys[0]) &&
+             ((xs[1].equals(ys[1]) && xs[2].equals(ys[2])) ||
+              (xs[2].equals(ys[1]) && xs[1].equals(ys[2])));
+   } else if (a->opcode == BRW_OPCODE_MUL && a->dst.type == BRW_REGISTER_TYPE_F) {
+      bool xs0_negate = xs[0].negate;
+      bool xs1_negate = xs[1].file == IMM ? xs[1].fixed_hw_reg.dw1.f < 0.0f
+                                          : xs[1].negate;
+      bool ys0_negate = ys[0].negate;
+      bool ys1_negate = ys[1].file == IMM ? ys[1].fixed_hw_reg.dw1.f < 0.0f
+                                          : ys[1].negate;
+      float xs1_imm = xs[1].fixed_hw_reg.dw1.f;
+      float ys1_imm = ys[1].fixed_hw_reg.dw1.f;
+
+      xs[0].negate = false;
+      xs[1].negate = false;
+      ys[0].negate = false;
+      ys[1].negate = false;
+      xs[1].fixed_hw_reg.dw1.f = fabsf(xs[1].fixed_hw_reg.dw1.f);
+      ys[1].fixed_hw_reg.dw1.f = fabsf(ys[1].fixed_hw_reg.dw1.f);
+
+      bool ret = (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
+                 (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
+
+      xs[0].negate = xs0_negate;
+      xs[1].negate = xs[1].file == IMM ? false : xs1_negate;
+      ys[0].negate = ys0_negate;
+      ys[1].negate = ys[1].file == IMM ? false : ys1_negate;
+      xs[1].fixed_hw_reg.dw1.f = xs1_imm;
+      ys[1].fixed_hw_reg.dw1.f = ys1_imm;
+
+      *negate = (xs0_negate != xs1_negate) != (ys0_negate != ys1_negate);
+      return ret;
+   } else if (!a->is_commutative()) {
       bool match = true;
       for (int i = 0; i < a->sources; i++) {
          if (!xs[i].equals(ys[i])) {
@@ -144,7 +163,7 @@ operands_match(fs_inst *a, fs_inst *b)
 }
 
 static bool
-instructions_match(fs_inst *a, fs_inst *b)
+instructions_match(fs_inst *a, fs_inst *b, bool *negate)
 {
    return a->opcode == b->opcode &&
           a->saturate == b->saturate &&
@@ -153,7 +172,7 @@ instructions_match(fs_inst *a, fs_inst *b)
           a->conditional_mod == b->conditional_mod &&
           a->dst.type == b->dst.type &&
           a->sources == b->sources &&
-          (a->is_tex() ? (a->texture_offset == b->texture_offset &&
+          (a->is_tex() ? (a->offset == b->offset &&
                           a->mlen == b->mlen &&
                           a->regs_written == b->regs_written &&
                           a->base_mrf == b->base_mrf &&
@@ -161,7 +180,7 @@ instructions_match(fs_inst *a, fs_inst *b)
                           a->header_present == b->header_present &&
                           a->shadow_compare == b->shadow_compare)
                        : true) &&
-          operands_match(a, b);
+          operands_match(a, b, negate);
 }
 
 bool
@@ -179,10 +198,12 @@ fs_visitor::opt_cse_local(bblock_t *block)
           (inst->dst.file != HW_REG || inst->dst.is_null()))
       {
          bool found = false;
+         bool negate = false;
 
          foreach_in_list_use_after(aeb_entry, entry, &aeb) {
             /* Match current instruction's expression against those in AEB. */
-            if (instructions_match(inst, entry->generator)) {
+            if (!(entry->generator->dst.is_null() && !inst->dst.is_null()) &&
+                instructions_match(inst, entry->generator, &negate)) {
                found = true;
                progress = true;
                break;
@@ -190,11 +211,16 @@ fs_visitor::opt_cse_local(bblock_t *block)
          }
 
          if (!found) {
-            /* Our first sighting of this expression.  Create an entry. */
-            aeb_entry *entry = ralloc(cse_ctx, aeb_entry);
-            entry->tmp = reg_undef;
-            entry->generator = inst;
-            aeb.push_tail(entry);
+            if (inst->opcode != BRW_OPCODE_MOV ||
+                (inst->opcode == BRW_OPCODE_MOV &&
+                 inst->src[0].file == IMM &&
+                 inst->src[0].type == BRW_REGISTER_TYPE_VF)) {
+               /* Our first sighting of this expression.  Create an entry. */
+               aeb_entry *entry = ralloc(cse_ctx, aeb_entry);
+               entry->tmp = reg_undef;
+               entry->generator = inst;
+               aeb.push_tail(entry);
+            }
          } else {
             /* This is at least our second sighting of this expression.
              * If we don't have a temporary already, make one.
@@ -206,7 +232,7 @@ fs_visitor::opt_cse_local(bblock_t *block)
                assert(written % dst_width == 0);
 
                fs_reg orig_dst = entry->generator->dst;
-               fs_reg tmp = fs_reg(GRF, virtual_grf_alloc(written),
+               fs_reg tmp = fs_reg(GRF, alloc.allocate(written),
                                    orig_dst.type, orig_dst.width);
                entry->tmp = tmp;
                entry->generator->dst = tmp;
@@ -243,6 +269,7 @@ fs_visitor::opt_cse_local(bblock_t *block)
                } else {
                   copy = MOV(dst, tmp);
                   copy->force_writemask_all = inst->force_writemask_all;
+                  copy->src[0].negate = negate;
                }
                inst->insert_before(block, copy);
             }
@@ -263,9 +290,10 @@ fs_visitor::opt_cse_local(bblock_t *block)
           * the flag register if we just wrote it.
           */
          if (inst->writes_flag()) {
+            bool negate; /* dummy */
             if (entry->generator->reads_flag() ||
                 (entry->generator->writes_flag() &&
-                 !instructions_match(inst, entry->generator))) {
+                 !instructions_match(inst, entry->generator, &negate))) {
                entry->remove();
                ralloc_free(entry);
                continue;