i965: Make can_do_source_mods() a member of the instruction classes.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vec4_copy_propagation.cpp
index 1a667ebf2b2e7943758f86aeca393b39faedac51..11571ad3b0a4ec2c86f586a159107ad55395632d 100644 (file)
@@ -57,6 +57,21 @@ is_dominated_by_previous_instruction(vec4_instruction *inst)
           inst->opcode != BRW_OPCODE_ENDIF);
 }
 
+static bool
+is_channel_updated(vec4_instruction *inst, src_reg *values[4], int ch)
+{
+   const src_reg *src = values[ch];
+
+   /* consider GRF only */
+   assert(inst->dst.file == GRF);
+   if (!src || src->file != GRF)
+      return false;
+
+   return (src->reg == inst->dst.reg &&
+          src->reg_offset == inst->dst.reg_offset &&
+          inst->dst.writemask & (1 << BRW_GET_SWZ(src->swizzle, ch)));
+}
+
 static bool
 try_constant_propagation(vec4_instruction *inst, int arg, src_reg *values[4])
 {
@@ -67,7 +82,7 @@ try_constant_propagation(vec4_instruction *inst, int arg, src_reg *values[4])
     */
    src_reg value = *values[0];
    for (int i = 1; i < 4; i++) {
-      if (!value.equals(values[i]))
+      if (!value.equals(*values[i]))
         return false;
    }
 
@@ -95,16 +110,37 @@ try_constant_propagation(vec4_instruction *inst, int arg, src_reg *values[4])
       inst->src[arg] = value;
       return true;
 
+   case BRW_OPCODE_DP2:
+   case BRW_OPCODE_DP3:
+   case BRW_OPCODE_DP4:
+   case BRW_OPCODE_DPH:
+   case BRW_OPCODE_BFI1:
+   case BRW_OPCODE_ASR:
+   case BRW_OPCODE_SHL:
+   case BRW_OPCODE_SHR:
+   case BRW_OPCODE_SUBB:
+      if (arg == 1) {
+         inst->src[arg] = value;
+         return true;
+      }
+      break;
+
+   case BRW_OPCODE_MACH:
    case BRW_OPCODE_MUL:
    case BRW_OPCODE_ADD:
+   case BRW_OPCODE_OR:
+   case BRW_OPCODE_AND:
+   case BRW_OPCODE_XOR:
+   case BRW_OPCODE_ADDC:
       if (arg == 1) {
         inst->src[arg] = value;
         return true;
       } else if (arg == 0 && inst->src[1].file != IMM) {
         /* Fit this constant in by commuting the operands.  Exception: we
-         * can't do this for 32-bit integer MUL because it's asymmetric.
+         * can't do this for 32-bit integer MUL/MACH because it's asymmetric.
          */
-        if (inst->opcode == BRW_OPCODE_MUL &&
+        if ((inst->opcode == BRW_OPCODE_MUL ||
+              inst->opcode == BRW_OPCODE_MACH) &&
             (inst->src[1].type == BRW_REGISTER_TYPE_D ||
              inst->src[1].type == BRW_REGISTER_TYPE_UD))
            break;
@@ -159,9 +195,17 @@ try_constant_propagation(vec4_instruction *inst, int arg, src_reg *values[4])
    return false;
 }
 
+static bool
+is_logic_op(enum opcode opcode)
+{
+   return (opcode == BRW_OPCODE_AND ||
+           opcode == BRW_OPCODE_OR  ||
+           opcode == BRW_OPCODE_XOR ||
+           opcode == BRW_OPCODE_NOT);
+}
+
 bool
-vec4_visitor::try_copy_propagation(struct intel_context *intel,
-                                   vec4_instruction *inst, int arg,
+vec4_visitor::try_copy_propagation(vec4_instruction *inst, int arg,
                                    src_reg *values[4])
 {
    /* For constant propagation, we only handle the same constant
@@ -198,6 +242,14 @@ vec4_visitor::try_copy_propagation(struct intel_context *intel,
        value.file != ATTR)
       return false;
 
+   if (brw->gen >= 8) {
+      if (value.negate) {
+         if (is_logic_op(inst->opcode)) {
+            return false;
+         }
+      }
+   }
+
    if (inst->src[arg].abs) {
       value.negate = false;
       value.abs = true;
@@ -205,14 +257,16 @@ vec4_visitor::try_copy_propagation(struct intel_context *intel,
    if (inst->src[arg].negate)
       value.negate = !value.negate;
 
-   bool has_source_modifiers = (value.negate || value.abs ||
-                                value.swizzle != BRW_SWIZZLE_XYZW ||
-                                value.file == UNIFORM);
+   bool has_source_modifiers = value.negate || value.abs;
 
    /* gen6 math and gen7+ SENDs from GRFs ignore source modifiers on
     * instructions.
     */
-   if (has_source_modifiers && !can_do_source_mods(inst))
+   if ((has_source_modifiers || value.file == UNIFORM ||
+        value.swizzle != BRW_SWIZZLE_XYZW) && !inst->can_do_source_mods(brw))
+      return false;
+
+   if (has_source_modifiers && value.type != inst->src[arg].type)
       return false;
 
    bool is_3src_inst = (inst->opcode == BRW_OPCODE_LRP ||
@@ -222,6 +276,9 @@ vec4_visitor::try_copy_propagation(struct intel_context *intel,
    if (is_3src_inst && value.file == UNIFORM)
       return false;
 
+   if (inst->is_send_from_grf())
+      return false;
+
    /* We can't copy-propagate a UD negation into a condmod
     * instruction, because the condmod ends up looking at the 33-bit
     * signed accumulator value instead of the 32-bit value we wanted
@@ -232,7 +289,7 @@ vec4_visitor::try_copy_propagation(struct intel_context *intel,
       return false;
 
    /* Don't report progress if this is a noop. */
-   if (value.equals(&inst->src[arg]))
+   if (value.equals(inst->src[arg]))
       return false;
 
    value.type = inst->src[arg].type;
@@ -304,7 +361,7 @@ vec4_visitor::opt_copy_propagation()
            continue;
 
         if (try_constant_propagation(inst, i, values) ||
-            try_copy_propagation(intel, inst, i, values))
+            try_copy_propagation(inst, i, values))
            progress = true;
       }
 
@@ -332,11 +389,7 @@ vec4_visitor::opt_copy_propagation()
         else {
            for (int i = 0; i < virtual_grf_reg_count; i++) {
               for (int j = 0; j < 4; j++) {
-                 if (inst->dst.writemask & (1 << j) &&
-                     cur_value[i][j] &&
-                     cur_value[i][j]->file == GRF &&
-                     cur_value[i][j]->reg == inst->dst.reg &&
-                     cur_value[i][j]->reg_offset == inst->dst.reg_offset) {
+                 if (is_channel_updated(inst, cur_value[i], j)){
                     cur_value[i][j] = NULL;
                  }
               }
@@ -346,7 +399,7 @@ vec4_visitor::opt_copy_propagation()
    }
 
    if (progress)
-      live_intervals_valid = false;
+      invalidate_live_intervals();
 
    return progress;
 }