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 3f59339f716aba8a0775465d6d8f902e6791ce88..c1d06161cbf1311a0a006f76e8e2b5fb43d93dd7 100644 (file)
@@ -42,10 +42,27 @@ struct aeb_entry : public exec_node {
 };
 }
 
+static bool
+is_copy_payload(const fs_inst *inst)
+{
+   const int reg = inst->src[0].reg;
+   if (inst->src[0].reg_offset != 0)
+      return false;
+
+   for (int i = 1; i < inst->sources; i++) {
+      if (inst->src[i].reg != reg ||
+          inst->src[i].reg_offset != i) {
+         return false;
+      }
+   }
+   return true;
+}
+
 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:
@@ -54,6 +71,8 @@ is_expression(const fs_inst *const inst)
    case BRW_OPCODE_SHR:
    case BRW_OPCODE_SHL:
    case BRW_OPCODE_ASR:
+   case BRW_OPCODE_CMP:
+   case BRW_OPCODE_CMPN:
    case BRW_OPCODE_ADD:
    case BRW_OPCODE_MUL:
    case BRW_OPCODE_FRC:
@@ -71,121 +90,188 @@ is_expression(const fs_inst *const inst)
    case FS_OPCODE_CINTERP:
    case FS_OPCODE_LINTERP:
       return true;
+   case SHADER_OPCODE_RCP:
+   case SHADER_OPCODE_RSQ:
+   case SHADER_OPCODE_SQRT:
+   case SHADER_OPCODE_EXP2:
+   case SHADER_OPCODE_LOG2:
+   case SHADER_OPCODE_POW:
+   case SHADER_OPCODE_INT_QUOTIENT:
+   case SHADER_OPCODE_INT_REMAINDER:
+   case SHADER_OPCODE_SIN:
+   case SHADER_OPCODE_COS:
+      return inst->mlen < 2;
+   case SHADER_OPCODE_LOAD_PAYLOAD:
+      return !is_copy_payload(inst);
    default:
-      return false;
+      return inst->is_send_from_grf() && !inst->has_side_effects();
    }
 }
 
 static bool
-is_expression_commutative(enum opcode op)
+operands_match(const fs_inst *a, const fs_inst *b, bool *negate)
 {
-   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;
+   fs_reg *xs = a->src;
+   fs_reg *ys = b->src;
+
+   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])) {
+            match = false;
+            break;
+         }
+      }
+      return match;
+   } else {
+      return (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
+             (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
    }
 }
 
 static bool
-operands_match(enum opcode op, fs_reg *xs, fs_reg *ys)
+instructions_match(fs_inst *a, fs_inst *b, bool *negate)
 {
-   if (!is_expression_commutative(op)) {
-      return xs[0].equals(ys[0]) && xs[1].equals(ys[1]) && xs[2].equals(ys[2]);
-   } else {
-      return (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
-             (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
-   }
+   return a->opcode == b->opcode &&
+          a->saturate == b->saturate &&
+          a->predicate == b->predicate &&
+          a->predicate_inverse == b->predicate_inverse &&
+          a->conditional_mod == b->conditional_mod &&
+          a->dst.type == b->dst.type &&
+          a->sources == b->sources &&
+          (a->is_tex() ? (a->offset == b->offset &&
+                          a->mlen == b->mlen &&
+                          a->regs_written == b->regs_written &&
+                          a->base_mrf == b->base_mrf &&
+                          a->eot == b->eot &&
+                          a->header_present == b->header_present &&
+                          a->shadow_compare == b->shadow_compare)
+                       : true) &&
+          operands_match(a, b, negate);
 }
 
 bool
-fs_visitor::opt_cse_local(bblock_t *block, exec_list *aeb)
+fs_visitor::opt_cse_local(bblock_t *block)
 {
    bool progress = false;
+   exec_list aeb;
 
-   void *mem_ctx = ralloc_context(this->mem_ctx);
+   void *cse_ctx = ralloc_context(NULL);
 
    int ip = block->start_ip;
-   for (fs_inst *inst = (fs_inst *)block->start;
-       inst != block->end->next;
-       inst = (fs_inst *) inst->next) {
-
+   foreach_inst_in_block(fs_inst, inst, block) {
       /* Skip some cases. */
-      if (is_expression(inst) &&
-          !inst->predicate &&
-          !inst->is_partial_write() &&
-          !inst->conditional_mod &&
-          inst->dst.file != HW_REG)
+      if (is_expression(inst) && !inst->is_partial_write() &&
+          (inst->dst.file != HW_REG || inst->dst.is_null()))
       {
-        bool found = false;
-
-        aeb_entry *entry;
-        foreach_list(entry_node, aeb) {
-           entry = (aeb_entry *) entry_node;
-
-           /* Match current instruction's expression against those in AEB. */
-           if (inst->opcode == entry->generator->opcode &&
-               inst->saturate == entry->generator->saturate &&
-                inst->dst.type == entry->generator->dst.type &&
-                operands_match(inst->opcode, entry->generator->src, inst->src)) {
-
-              found = true;
-              progress = true;
-              break;
-           }
-        }
-
-        if (!found) {
-           /* Our first sighting of this expression.  Create an entry. */
-           aeb_entry *entry = ralloc(mem_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.
-            */
-           bool no_existing_temp = entry->tmp.file == BAD_FILE;
-           if (no_existing_temp && !entry->generator->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 (!(entry->generator->dst.is_null() && !inst->dst.is_null()) &&
+                instructions_match(inst, entry->generator, &negate)) {
+               found = true;
+               progress = true;
+               break;
+            }
+         }
+
+         if (!found) {
+            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.
+             */
+            bool no_existing_temp = entry->tmp.file == BAD_FILE;
+            if (no_existing_temp && !entry->generator->dst.is_null()) {
                int written = entry->generator->regs_written;
+               int dst_width = entry->generator->dst.width / 8;
+               assert(written % dst_width == 0);
 
                fs_reg orig_dst = entry->generator->dst;
-               fs_reg tmp = fs_reg(GRF, virtual_grf_alloc(written),
-                                   orig_dst.type);
+               fs_reg tmp = fs_reg(GRF, alloc.allocate(written),
+                                   orig_dst.type, orig_dst.width);
                entry->tmp = tmp;
                entry->generator->dst = tmp;
 
-               for (int i = 0; i < written; i++) {
-                  fs_inst *copy = MOV(orig_dst, tmp);
+               fs_inst *copy;
+               if (written > dst_width) {
+                  fs_reg *sources = ralloc_array(mem_ctx, fs_reg, written / dst_width);
+                  for (int i = 0; i < written / dst_width; i++)
+                     sources[i] = offset(tmp, i);
+                  copy = LOAD_PAYLOAD(orig_dst, sources, written / dst_width);
+               } else {
+                  copy = MOV(orig_dst, tmp);
                   copy->force_writemask_all =
                      entry->generator->force_writemask_all;
-                  entry->generator->insert_after(copy);
-
-                  orig_dst.reg_offset++;
-                  tmp.reg_offset++;
                }
-           }
+               entry->generator->insert_after(block, copy);
+            }
 
-           /* dest <- temp */
+            /* dest <- temp */
             if (!inst->dst.is_null()) {
                int written = inst->regs_written;
+               int dst_width = inst->dst.width / 8;
                assert(written == entry->generator->regs_written);
+               assert(dst_width == entry->generator->dst.width / 8);
                assert(inst->dst.type == entry->tmp.type);
                fs_reg dst = inst->dst;
                fs_reg tmp = entry->tmp;
-               fs_inst *copy = NULL;
-               for (int i = 0; i < written; i++) {
+               fs_inst *copy;
+               if (written > dst_width) {
+                  fs_reg *sources = ralloc_array(mem_ctx, fs_reg, written / dst_width);
+                  for (int i = 0; i < written / dst_width; i++)
+                     sources[i] = offset(tmp, i);
+                  copy = LOAD_PAYLOAD(dst, sources, written / dst_width);
+               } else {
                   copy = MOV(dst, tmp);
                   copy->force_writemask_all = inst->force_writemask_all;
-                  inst->insert_before(copy);
-
-                  dst.reg_offset++;
-                  tmp.reg_offset++;
+                  copy->src[0].negate = negate;
                }
+               inst->insert_before(block, copy);
             }
 
             /* Set our iterator so that next time through the loop inst->next
@@ -194,31 +280,37 @@ fs_visitor::opt_cse_local(bblock_t *block, exec_list *aeb)
              */
             fs_inst *prev = (fs_inst *)inst->prev;
 
-            inst->remove();
-
-           /* Appending an instruction may have changed our bblock end. */
-           if (inst == block->end) {
-              block->end = prev;
-           }
-
+            inst->remove(block);
             inst = prev;
-        }
+         }
       }
 
-      foreach_list_safe(entry_node, aeb) {
-        aeb_entry *entry = (aeb_entry *)entry_node;
+      foreach_in_list_safe(aeb_entry, entry, &aeb) {
+         /* Kill all AEB entries that write a different value to or read from
+          * 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, &negate))) {
+               entry->remove();
+               ralloc_free(entry);
+               continue;
+            }
+         }
 
-        for (int i = 0; i < 3; i++) {
+         for (int i = 0; i < entry->generator->sources; i++) {
             fs_reg *src_reg = &entry->generator->src[i];
 
             /* Kill all AEB entries that use the destination we just
              * overwrote.
              */
             if (inst->overwrites_reg(entry->generator->src[i])) {
-              entry->remove();
-              ralloc_free(entry);
-              break;
-           }
+               entry->remove();
+               ralloc_free(entry);
+               break;
+            }
 
             /* Kill any AEB entries using registers that don't get reused any
              * more -- a sure sign they'll fail operands_match().
@@ -226,18 +318,15 @@ fs_visitor::opt_cse_local(bblock_t *block, exec_list *aeb)
             if (src_reg->file == GRF && virtual_grf_end[src_reg->reg] < ip) {
                entry->remove();
                ralloc_free(entry);
-              break;
+               break;
             }
-        }
+         }
       }
 
       ip++;
    }
 
-   ralloc_free(mem_ctx);
-
-   if (progress)
-      invalidate_live_intervals();
+   ralloc_free(cse_ctx);
 
    return progress;
 }
@@ -249,14 +338,12 @@ fs_visitor::opt_cse()
 
    calculate_live_intervals();
 
-   cfg_t cfg(this);
-
-   for (int b = 0; b < cfg.num_blocks; b++) {
-      bblock_t *block = cfg.blocks[b];
-      exec_list aeb;
-
-      progress = opt_cse_local(block, &aeb) || progress;
+   foreach_block (block, cfg) {
+      progress = opt_cse_local(block) || progress;
    }
 
+   if (progress)
+      invalidate_live_intervals();
+
    return progress;
 }