i965: Move up fs_inst::regs_written to backend_instruction.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_dead_code_eliminate.cpp
index 7b2d4aa9b72bbc14a8c546cff985a65992f6806f..4b5548a9dc561403c54131e0cdc5678eaaeb6ace 100644 (file)
@@ -39,29 +39,27 @@ fs_visitor::dead_code_eliminate()
 {
    bool progress = false;
 
-   cfg_t cfg(&instructions);
-
    calculate_live_intervals();
 
    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 (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()) {
+         if (inst->dst.file == GRF && !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);
                }
@@ -70,7 +68,7 @@ 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;
@@ -79,33 +77,61 @@ fs_visitor::dead_code_eliminate()
             }
          }
 
+         if (inst->dst.is_null() && inst->writes_flag()) {
+            if (!BITSET_TEST(flag_live, inst->flag_subreg)) {
+               inst->opcode = BRW_OPCODE_NOP;
+               progress = true;
+               continue;
+            }
+         }
+
+         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;
+            continue;
+         }
+
          if (inst->dst.file == GRF) {
             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()) {
+            BITSET_CLEAR(flag_live, inst->flag_subreg);
+         }
+
          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];
+               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) {
+      foreach_block_and_inst_safe (block, backend_instruction, inst, cfg) {
          if (inst->opcode == BRW_OPCODE_NOP) {
-            inst->remove();
+            inst->remove(block);
          }
       }