i965/fs: Handle instruction predication in SIMD lowering pass.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_dead_code_eliminate.cpp
index c00ec1b4de89b8846f16a2cfb4a690092394002b..bd57e09fe0c19650bf9696a1d59091eff452628b 100644 (file)
@@ -43,23 +43,23 @@ fs_visitor::dead_code_eliminate()
 
    int num_vars = live_intervals->num_vars;
    BITSET_WORD *live = ralloc_array(NULL, BITSET_WORD, BITSET_WORDS(num_vars));
+   BITSET_WORD *flag_live = ralloc_array(NULL, BITSET_WORD, 1);
 
-   for (int b = 0; b < cfg->num_blocks; b++) {
-      bblock_t *block = cfg->blocks[b];
-      memcpy(live, live_intervals->bd[b].liveout,
+   foreach_block_reverse_safe(block, cfg) {
+      memcpy(live, live_intervals->block_data[block->num].liveout,
              sizeof(BITSET_WORD) * BITSET_WORDS(num_vars));
+      memcpy(flag_live, live_intervals->block_data[block->num].flag_liveout,
+             sizeof(BITSET_WORD));
 
-      foreach_inst_in_block_reverse(fs_inst, inst, block) {
-         if (inst->dst.file == GRF &&
-             !inst->has_side_effects() &&
-             !inst->writes_flag()) {
+      foreach_inst_in_block_reverse_safe(fs_inst, inst, block) {
+         if (inst->dst.file == VGRF && !inst->has_side_effects()) {
             bool result_live = false;
 
             if (inst->regs_written == 1) {
-               int var = live_intervals->var_from_reg(&inst->dst);
+               int var = live_intervals->var_from_reg(inst->dst);
                result_live = BITSET_TEST(live, var);
             } else {
-               int var = live_intervals->var_from_vgrf[inst->dst.reg];
+               int var = live_intervals->var_from_reg(inst->dst);
                for (int i = 0; i < inst->regs_written; i++) {
                   result_live = result_live || BITSET_TEST(live, var + i);
                }
@@ -68,47 +68,70 @@ fs_visitor::dead_code_eliminate()
             if (!result_live) {
                progress = true;
 
-               if (inst->writes_accumulator) {
+               if (inst->writes_accumulator || inst->writes_flag()) {
                   inst->dst = fs_reg(retype(brw_null_reg(), inst->dst.type));
                } else {
                   inst->opcode = BRW_OPCODE_NOP;
-                  continue;
                }
             }
          }
 
-         if (inst->dst.file == GRF) {
+         if (inst->dst.is_null() && inst->writes_flag()) {
+            if (!BITSET_TEST(flag_live, inst->flag_subreg)) {
+               inst->opcode = BRW_OPCODE_NOP;
+               progress = true;
+            }
+         }
+
+         if ((inst->opcode != BRW_OPCODE_IF &&
+              inst->opcode != BRW_OPCODE_WHILE) &&
+             inst->dst.is_null() &&
+             !inst->has_side_effects() &&
+             !inst->writes_flag() &&
+             !inst->writes_accumulator) {
+            inst->opcode = BRW_OPCODE_NOP;
+            progress = true;
+         }
+
+         if (inst->dst.file == VGRF) {
             if (!inst->is_partial_write()) {
-               int var = live_intervals->var_from_vgrf[inst->dst.reg];
+               int var = live_intervals->var_from_reg(inst->dst);
                for (int i = 0; i < inst->regs_written; i++) {
-                  BITSET_CLEAR(live, var + inst->dst.reg_offset + i);
+                  BITSET_CLEAR(live, var + i);
                }
             }
          }
 
+         if (inst->writes_flag() && !inst->predicate) {
+            BITSET_CLEAR(flag_live, inst->flag_subreg);
+         }
+
+         if (inst->opcode == BRW_OPCODE_NOP) {
+            inst->remove(block);
+            continue;
+         }
+
          for (int i = 0; i < inst->sources; i++) {
-            if (inst->src[i].file == GRF) {
-               int var = live_intervals->var_from_vgrf[inst->src[i].reg];
+            if (inst->src[i].file == VGRF) {
+               int var = live_intervals->var_from_reg(inst->src[i]);
 
-               for (int j = 0; j < inst->regs_read(this, i); j++) {
-                  BITSET_SET(live, var + inst->src[i].reg_offset + j);
+               for (int j = 0; j < inst->regs_read(i); j++) {
+                  BITSET_SET(live, var + j);
                }
             }
          }
+
+         if (inst->reads_flag()) {
+            BITSET_SET(flag_live, inst->flag_subreg);
+         }
       }
    }
 
    ralloc_free(live);
+   ralloc_free(flag_live);
 
-   if (progress) {
-      foreach_in_list_safe(fs_inst, inst, &instructions) {
-         if (inst->opcode == BRW_OPCODE_NOP) {
-            inst->remove();
-         }
-      }
-
+   if (progress)
       invalidate_live_intervals();
-   }
 
    return progress;
 }