diff --git a/src/intel/compiler/brw_fs.cpp b/src/intel/compiler/brw_fs.cpp index 3aaac3c8cb8..6e100367997 100644 --- a/src/intel/compiler/brw_fs.cpp +++ b/src/intel/compiler/brw_fs.cpp @@ -2568,6 +2568,62 @@ fs_visitor::lower_constant_loads() invalidate_analysis(DEPENDENCY_INSTRUCTIONS); } +static uint64_t +src_as_uint(const fs_reg &src) +{ + assert(src.file == IMM); + + switch (src.type) { + case BRW_REGISTER_TYPE_W: + return (uint64_t)(int16_t)(src.ud & 0xffff); + + case BRW_REGISTER_TYPE_UW: + return (uint64_t)(uint16_t)(src.ud & 0xffff); + + case BRW_REGISTER_TYPE_D: + return (uint64_t)src.d; + + case BRW_REGISTER_TYPE_UD: + return (uint64_t)src.ud; + + case BRW_REGISTER_TYPE_Q: + return src.d64; + + case BRW_REGISTER_TYPE_UQ: + return src.u64; + + default: + unreachable("Invalid integer type."); + } +} + +static fs_reg +brw_imm_for_type(uint64_t value, enum brw_reg_type type) +{ + switch (type) { + case BRW_REGISTER_TYPE_W: + return brw_imm_w(value); + + case BRW_REGISTER_TYPE_UW: + return brw_imm_uw(value); + + case BRW_REGISTER_TYPE_D: + return brw_imm_d(value); + + case BRW_REGISTER_TYPE_UD: + return brw_imm_ud(value); + + case BRW_REGISTER_TYPE_Q: + return brw_imm_d(value); + + case BRW_REGISTER_TYPE_UQ: + return brw_imm_uq(value); + + default: + unreachable("Invalid integer type."); + } +} + bool fs_visitor::opt_algebraic() { @@ -2701,7 +2757,35 @@ fs_visitor::opt_algebraic() break; } break; + + case BRW_OPCODE_AND: + if (inst->src[0].file == IMM && inst->src[1].file == IMM) { + const uint64_t src0 = src_as_uint(inst->src[0]); + const uint64_t src1 = src_as_uint(inst->src[1]); + + inst->opcode = BRW_OPCODE_MOV; + inst->sources = 1; + inst->src[0] = brw_imm_for_type(src0 & src1, inst->dst.type); + inst->src[1] = reg_undef; + progress = true; + break; + } + + break; + case BRW_OPCODE_OR: + if (inst->src[0].file == IMM && inst->src[1].file == IMM) { + const uint64_t src0 = src_as_uint(inst->src[0]); + const uint64_t src1 = src_as_uint(inst->src[1]); + + inst->opcode = BRW_OPCODE_MOV; + inst->sources = 1; + inst->src[0] = brw_imm_for_type(src0 | src1, inst->dst.type); + inst->src[1] = reg_undef; + progress = true; + break; + } + if (inst->src[0].equals(inst->src[1]) || inst->src[1].is_zero()) { /* On Gfx8+, the OR instruction can have a source modifier that diff --git a/src/intel/compiler/brw_fs_copy_propagation.cpp b/src/intel/compiler/brw_fs_copy_propagation.cpp index d16077c3c13..ba0ebc58909 100644 --- a/src/intel/compiler/brw_fs_copy_propagation.cpp +++ b/src/intel/compiler/brw_fs_copy_propagation.cpp @@ -935,8 +935,6 @@ fs_visitor::try_constant_propagate(fs_inst *inst, acp_entry *entry) case BRW_OPCODE_MUL: case SHADER_OPCODE_MULH: case BRW_OPCODE_ADD: - case BRW_OPCODE_OR: - case BRW_OPCODE_AND: case BRW_OPCODE_XOR: case BRW_OPCODE_ADDC: if (i == 1) { @@ -1072,6 +1070,8 @@ fs_visitor::try_constant_propagate(fs_inst *inst, acp_entry *entry) } break; + case BRW_OPCODE_AND: + case BRW_OPCODE_OR: case SHADER_OPCODE_TEX_LOGICAL: case SHADER_OPCODE_TXD_LOGICAL: case SHADER_OPCODE_TXF_LOGICAL: @@ -1120,6 +1120,17 @@ fs_visitor::try_constant_propagate(fs_inst *inst, acp_entry *entry) } } + /* If only one of the sources of a 2-source, commutative instruction (e.g., + * AND) is immediate, it must be src1. If both are immediate, opt_algebraic + * should fold it away. + */ + if (progress && inst->sources == 2 && inst->is_commutative() && + inst->src[0].file == IMM && inst->src[1].file != IMM) { + const auto src1 = inst->src[1]; + inst->src[1] = inst->src[0]; + inst->src[0] = src1; + } + return progress; }