i965/vec4: Don't lose the force_writemask_all flag during CSE.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_saturate_propagation.cpp
index 4c4b6bf47a817e24e1f9dc8956c898efa2fc5bdb..e406c2899e89c954b257364ad1da979f70de42d9 100644 (file)
 #include "brw_cfg.h"
 
 /** @file brw_fs_saturate_propagation.cpp
+ *
+ * Implements a pass that propagates the SAT modifier from a MOV.SAT into the
+ * instruction that produced the source of the MOV.SAT, thereby allowing the
+ * MOV's src and dst to be coalesced and the MOV removed.
+ *
+ * For instance,
+ *
+ *    ADD     tmp, src0, src1
+ *    MOV.SAT dst, tmp
+ *
+ * would be transformed into
+ *
+ *    ADD.SAT tmp, src0, src1
+ *    MOV     dst, tmp
  */
 
 static bool
 opt_saturate_propagation_local(fs_visitor *v, bblock_t *block)
 {
    bool progress = false;
-   int ip = block->start_ip - 1;
+   int ip = block->end_ip + 1;
 
-   foreach_inst_in_block(fs_inst, inst, block) {
-      ip++;
+   foreach_inst_in_block_reverse(fs_inst, inst, block) {
+      ip--;
 
       if (inst->opcode != BRW_OPCODE_MOV ||
           inst->dst.file != GRF ||
@@ -45,15 +59,15 @@ opt_saturate_propagation_local(fs_visitor *v, bblock_t *block)
           !inst->saturate)
          continue;
 
-      int src_var = v->live_intervals->var_from_reg(&inst->src[0]);
+      int src_var = v->live_intervals->var_from_reg(inst->src[0]);
       int src_end_ip = v->live_intervals->end[src_var];
 
       bool interfered = false;
       foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst, block) {
-         if (scan_inst->dst.file == GRF &&
-             scan_inst->dst.reg == inst->src[0].reg &&
-             scan_inst->dst.reg_offset == inst->src[0].reg_offset &&
-             !scan_inst->is_partial_write()) {
+         if (scan_inst->overwrites_reg(inst->src[0])) {
+            if (scan_inst->is_partial_write())
+               break;
+
             if (scan_inst->saturate) {
                inst->saturate = false;
                progress = true;
@@ -70,8 +84,13 @@ opt_saturate_propagation_local(fs_visitor *v, bblock_t *block)
             if (scan_inst->src[i].file == GRF &&
                 scan_inst->src[i].reg == inst->src[0].reg &&
                 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
-               interfered = true;
-               break;
+               if (scan_inst->opcode != BRW_OPCODE_MOV ||
+                   !scan_inst->saturate ||
+                   scan_inst->src[0].abs ||
+                   scan_inst->src[0].negate) {
+                  interfered = true;
+                  break;
+               }
             }
          }
 
@@ -94,8 +113,7 @@ fs_visitor::opt_saturate_propagation()
       progress = opt_saturate_propagation_local(this, block) || progress;
    }
 
-   if (progress)
-      invalidate_live_intervals();
+   /* Live intervals are still valid. */
 
    return progress;
 }