i965/fs: Use offset a lot more places
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_cse.cpp
index fd28e14fd0e6f4e679c8efe7c689dfb185f739a2..7edbe1915c2fdbbe2d45070c8a89c1daa2cbcddd 100644 (file)
  */
 
 #include "brw_fs.h"
-#include "brw_fs_cfg.h"
+#include "brw_cfg.h"
 
 /** @file brw_fs_cse.cpp
  *
  * Support for local common subexpression elimination.
  *
- * See Muchnik's Advanced Compiler Design and Implementation, section
+ * See Muchnick's Advanced Compiler Design and Implementation, section
  * 13.1 (p378).
  */
 
@@ -42,6 +42,22 @@ 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)
 {
@@ -53,9 +69,9 @@ is_expression(const fs_inst *const inst)
    case BRW_OPCODE_XOR:
    case BRW_OPCODE_SHR:
    case BRW_OPCODE_SHL:
-   case BRW_OPCODE_RSR:
-   case BRW_OPCODE_RSL:
    case BRW_OPCODE_ASR:
+   case BRW_OPCODE_CMP:
+   case BRW_OPCODE_CMPN:
    case BRW_OPCODE_ADD:
    case BRW_OPCODE_MUL:
    case BRW_OPCODE_FRC:
@@ -66,113 +82,219 @@ is_expression(const fs_inst *const inst)
    case BRW_OPCODE_LINE:
    case BRW_OPCODE_PLN:
    case BRW_OPCODE_MAD:
+   case BRW_OPCODE_LRP:
+   case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
+   case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7:
+   case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD:
    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 == 0;
+   case SHADER_OPCODE_LOAD_PAYLOAD:
+      return !is_copy_payload(inst);
+   default:
+      return inst->is_send_from_grf() && !inst->has_side_effects();
+   }
+}
+
+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_reg *xs, fs_reg *ys)
+operands_match(fs_inst *a, fs_inst *b)
+{
+   fs_reg *xs = a->src;
+   fs_reg *ys = b->src;
+
+   if (!is_expression_commutative(a->opcode)) {
+      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
+instructions_match(fs_inst *a, fs_inst *b)
 {
-   return xs[0].equals(ys[0]) && xs[1].equals(ys[1]) && xs[2].equals(ys[2]);
+   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->texture_offset == b->texture_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);
 }
 
 bool
-fs_visitor::opt_cse_local(fs_bblock *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);
-
-   for (fs_inst *inst = block->start;
-       inst != block->end->next;
-       inst = (fs_inst *) inst->next) {
+   void *cse_ctx = ralloc_context(NULL);
 
+   int ip = block->start_ip;
+   foreach_inst_in_block(fs_inst, inst, block) {
       /* Skip some cases. */
-      if (is_expression(inst) && !inst->predicated && inst->mlen == 0 &&
-          !inst->force_uncompressed && !inst->force_sechalf &&
-          !inst->conditional_mod)
+      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 &&
-               operands_match(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->tmp = fs_reg(this, glsl_type::float_type);
-              entry->tmp.type = inst->dst.type;
-
-              fs_inst *copy = new(ralloc_parent(inst))
-                 fs_inst(BRW_OPCODE_MOV, entry->generator->dst, entry->tmp);
-              entry->generator->insert_after(copy);
-              entry->generator->dst = entry->tmp;
-           }
-
-           /* dest <- temp */
-           fs_inst *copy = new(ralloc_parent(inst))
-              fs_inst(BRW_OPCODE_MOV, inst->dst, entry->tmp);
-           inst->replace_with(copy);
-
-           /* Appending an instruction may have changed our bblock end. */
-           if (inst == block->end) {
-              block->end = copy;
-           }
-
-           /* Continue iteration with copy->next */
-           inst = copy;
-        }
+         bool found = 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)) {
+               found = true;
+               progress = true;
+               break;
+            }
+         }
+
+         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);
+         } 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;
+
+               fs_reg orig_dst = entry->generator->dst;
+               fs_reg tmp = fs_reg(GRF, virtual_grf_alloc(written),
+                                   orig_dst.type);
+               entry->tmp = tmp;
+               entry->generator->dst = tmp;
+
+               fs_inst *copy;
+               if (written > 1) {
+                  fs_reg *sources = ralloc_array(mem_ctx, fs_reg, written);
+                  for (int i = 0; i < written; i++)
+                     sources[i] = offset(tmp, i);
+                  copy = LOAD_PAYLOAD(orig_dst, sources, written);
+               } else {
+                  copy = MOV(orig_dst, tmp);
+                  copy->force_writemask_all =
+                     entry->generator->force_writemask_all;
+               }
+               entry->generator->insert_after(block, copy);
+            }
+
+            /* dest <- temp */
+            if (!inst->dst.is_null()) {
+               int written = inst->regs_written;
+               assert(written == entry->generator->regs_written);
+               assert(inst->dst.type == entry->tmp.type);
+               fs_reg dst = inst->dst;
+               fs_reg tmp = entry->tmp;
+               fs_inst *copy;
+               if (written > 1) {
+                  fs_reg *sources = ralloc_array(mem_ctx, fs_reg, written);
+                  for (int i = 0; i < written; i++)
+                     sources[i] = offset(tmp, i);
+                  copy = LOAD_PAYLOAD(dst, sources, written);
+               } else {
+                  copy = MOV(dst, tmp);
+                  copy->force_writemask_all = inst->force_writemask_all;
+               }
+               inst->insert_before(block, copy);
+            }
+
+            /* Set our iterator so that next time through the loop inst->next
+             * will get the instruction in the basic block after the one we've
+             * removed.
+             */
+            fs_inst *prev = (fs_inst *)inst->prev;
+
+            inst->remove(block);
+            inst = prev;
+         }
       }
 
-      /* Kill all AEB entries that use the destination. */
-      int start_offset = inst->dst.reg_offset;
-      int end_offset = start_offset + inst->regs_written();
-
-      foreach_list_safe(entry_node, aeb) {
-        aeb_entry *entry = (aeb_entry *)entry_node;
-
-        for (int i = 0; i < 3; i++) {
-           if (entry->generator->src[i].file == inst->dst.file &&
-               entry->generator->src[i].reg == inst->dst.reg &&
-               entry->generator->src[i].reg_offset >= start_offset &&
-               entry->generator->src[i].reg_offset < end_offset) {
-              entry->remove();
-              ralloc_free(entry);
-              break;
-           }
-        }
+      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()) {
+            if (entry->generator->reads_flag() ||
+                (entry->generator->writes_flag() &&
+                 !instructions_match(inst, entry->generator))) {
+               entry->remove();
+               ralloc_free(entry);
+               continue;
+            }
+         }
+
+         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;
+            }
+
+            /* Kill any AEB entries using registers that don't get reused any
+             * more -- a sure sign they'll fail operands_match().
+             */
+            if (src_reg->file == GRF && virtual_grf_end[src_reg->reg] < ip) {
+               entry->remove();
+               ralloc_free(entry);
+               break;
+            }
+         }
       }
-   }
 
-   ralloc_free(mem_ctx);
+      ip++;
+   }
 
-   if (progress)
-      this->live_intervals_valid = false;
+   ralloc_free(cse_ctx);
 
    return progress;
 }
@@ -182,14 +304,14 @@ fs_visitor::opt_cse()
 {
    bool progress = false;
 
-   fs_cfg cfg(this);
-
-   for (int b = 0; b < cfg.num_blocks; b++) {
-      fs_bblock *block = cfg.blocks[b];
-      exec_list aeb;
+   calculate_live_intervals();
 
-      progress = opt_cse_local(block, &aeb) || progress;
+   foreach_block (block, cfg) {
+      progress = opt_cse_local(block) || progress;
    }
 
+   if (progress)
+      invalidate_live_intervals();
+
    return progress;
 }