i965/vec4: Add a test for copy propagation behavior.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_cse.cpp
index 44479d8e9ffef2286450ccb757aee6df246b556f..ea610bd554c87311fa47dfbbde120768d0335c66 100644 (file)
@@ -28,7 +28,7 @@
  *
  * 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).
  */
 
@@ -53,9 +53,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,7 +66,10 @@ 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;
@@ -76,9 +79,41 @@ is_expression(const fs_inst *const inst)
 }
 
 static bool
-operands_match(fs_reg *xs, fs_reg *ys)
+is_expression_commutative(enum opcode op)
 {
-   return xs[0].equals(ys[0]) && xs[1].equals(ys[1]) && xs[2].equals(ys[2]);
+   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(enum opcode op, fs_reg *xs, fs_reg *ys)
+{
+   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]));
+   }
+}
+
+static bool
+instructions_match(fs_inst *a, fs_inst *b)
+{
+   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 &&
+          operands_match(a->opcode, a->src, b->src);
 }
 
 bool
@@ -86,16 +121,16 @@ fs_visitor::opt_cse_local(bblock_t *block, exec_list *aeb)
 {
    bool progress = false;
 
-   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) {
 
       /* Skip some cases. */
-      if (is_expression(inst) && !inst->predicate && 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;
 
@@ -104,11 +139,7 @@ fs_visitor::opt_cse_local(bblock_t *block, exec_list *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(entry->generator->src, inst->src)) {
-
+           if (instructions_match(inst, entry->generator)) {
               found = true;
               progress = true;
               break;
@@ -117,7 +148,7 @@ fs_visitor::opt_cse_local(bblock_t *block, exec_list *aeb)
 
         if (!found) {
            /* Our first sighting of this expression.  Create an entry. */
-           aeb_entry *entry = ralloc(mem_ctx, aeb_entry);
+           aeb_entry *entry = ralloc(cse_ctx, aeb_entry);
            entry->tmp = reg_undef;
            entry->generator = inst;
            aeb->push_tail(entry);
@@ -126,51 +157,107 @@ fs_visitor::opt_cse_local(bblock_t *block, exec_list *aeb)
             * 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;
+           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;
+
+               for (int i = 0; i < written; i++) {
+                  fs_inst *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++;
+               }
            }
 
            /* dest <- temp */
-            assert(inst->dst.type == entry->tmp.type);
-           fs_inst *copy = new(ralloc_parent(inst))
-              fs_inst(BRW_OPCODE_MOV, inst->dst, entry->tmp);
-            copy->force_writemask_all = inst->force_writemask_all;
-           inst->replace_with(copy);
+            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 = NULL;
+               for (int i = 0; i < written; i++) {
+                  copy = MOV(dst, tmp);
+                  copy->force_writemask_all = inst->force_writemask_all;
+                  inst->insert_before(copy);
+
+                  dst.reg_offset++;
+                  tmp.reg_offset++;
+               }
+            }
+
+            /* 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();
 
            /* Appending an instruction may have changed our bblock end. */
            if (inst == block->end) {
-              block->end = copy;
+              block->end = prev;
            }
 
-           /* Continue iteration with copy->next */
-           inst = copy;
+            inst = prev;
         }
       }
 
-      /* Kill all AEB entries that use the destination. */
       foreach_list_safe(entry_node, aeb) {
         aeb_entry *entry = (aeb_entry *)entry_node;
 
+         /* 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 < 3; 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;
+            }
         }
       }
+
+      ip++;
    }
 
-   ralloc_free(mem_ctx);
+   ralloc_free(cse_ctx);
 
    if (progress)
-      this->live_intervals_valid = false;
+      invalidate_live_intervals();
 
    return progress;
 }
@@ -180,7 +267,9 @@ fs_visitor::opt_cse()
 {
    bool progress = false;
 
-   cfg_t cfg(this);
+   calculate_live_intervals();
+
+   cfg_t cfg(&instructions);
 
    for (int b = 0; b < cfg.num_blocks; b++) {
       bblock_t *block = cfg.blocks[b];