*/
#include "brw_vec4.h"
+#include "brw_cfg.h"
extern "C" {
#include "main/macros.h"
}
namespace brw {
+struct copy_entry {
+ src_reg *value[4];
+ int saturatemask;
+};
+
static bool
is_direct_copy(vec4_instruction *inst)
{
return (inst->opcode == BRW_OPCODE_MOV &&
!inst->predicate &&
inst->dst.file == GRF &&
- !inst->saturate &&
!inst->dst.reladdr &&
!inst->src[0].reladdr &&
- inst->dst.type == inst->src[0].type);
+ (inst->dst.type == inst->src[0].type ||
+ (inst->dst.type == BRW_REGISTER_TYPE_F &&
+ inst->src[0].type == BRW_REGISTER_TYPE_VF)));
}
static bool
inst->dst.writemask & (1 << BRW_GET_SWZ(src->swizzle, ch)));
}
+static unsigned
+swizzle_vf_imm(unsigned vf4, unsigned swizzle)
+{
+ union {
+ unsigned vf4;
+ uint8_t vf[4];
+ } v = { vf4 }, ret;
+
+ ret.vf[0] = v.vf[BRW_GET_SWZ(swizzle, 0)];
+ ret.vf[1] = v.vf[BRW_GET_SWZ(swizzle, 1)];
+ ret.vf[2] = v.vf[BRW_GET_SWZ(swizzle, 2)];
+ ret.vf[3] = v.vf[BRW_GET_SWZ(swizzle, 3)];
+
+ return ret.vf4;
+}
+
+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);
+}
+
static bool
try_constant_propagate(struct brw_context *brw, vec4_instruction *inst,
- int arg, src_reg *values[4])
+ int arg, struct copy_entry *entry)
{
/* For constant propagation, we only handle the same constant
* across all 4 channels. Some day, we should handle the 8-bit
* float vector format, which would let us constant propagate
* vectors better.
*/
- src_reg value = *values[0];
+ src_reg value = *entry->value[0];
for (int i = 1; i < 4; i++) {
- if (!value.equals(*values[i]))
+ if (!value.equals(*entry->value[i]))
return false;
}
if (value.file != IMM)
return false;
+ if (value.type == BRW_REGISTER_TYPE_VF) {
+ /* The result of bit-casting the component values of a vector float
+ * cannot in general be represented as an immediate.
+ */
+ if (inst->src[arg].type != BRW_REGISTER_TYPE_F)
+ return false;
+ } else {
+ value.type = inst->src[arg].type;
+ }
+
if (inst->src[arg].abs) {
- if (value.type == BRW_REGISTER_TYPE_F) {
- value.fixed_hw_reg.dw1.f = fabs(value.fixed_hw_reg.dw1.f);
- } else if (value.type == BRW_REGISTER_TYPE_D) {
- if (value.fixed_hw_reg.dw1.d < 0)
- value.fixed_hw_reg.dw1.d = -value.fixed_hw_reg.dw1.d;
+ if ((brw->gen >= 8 && is_logic_op(inst->opcode)) ||
+ !brw_abs_immediate(value.type, &value.fixed_hw_reg)) {
+ return false;
}
}
if (inst->src[arg].negate) {
- if (value.type == BRW_REGISTER_TYPE_F)
- value.fixed_hw_reg.dw1.f = -value.fixed_hw_reg.dw1.f;
- else
- value.fixed_hw_reg.dw1.ud = -value.fixed_hw_reg.dw1.ud;
+ if ((brw->gen >= 8 && is_logic_op(inst->opcode)) ||
+ !brw_negate_immediate(value.type, &value.fixed_hw_reg)) {
+ return false;
+ }
}
+ if (value.type == BRW_REGISTER_TYPE_VF)
+ value.fixed_hw_reg.dw1.ud = swizzle_vf_imm(value.fixed_hw_reg.dw1.ud,
+ inst->src[arg].swizzle);
+
switch (inst->opcode) {
case BRW_OPCODE_MOV:
inst->src[arg] = value;
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);
-}
-
static bool
try_copy_propagate(struct brw_context *brw, vec4_instruction *inst,
- int arg, src_reg *values[4])
+ int arg, struct copy_entry *entry)
{
/* For constant propagation, we only handle the same constant
* across all 4 channels. Some day, we should handle the 8-bit
* float vector format, which would let us constant propagate
* vectors better.
*/
- src_reg value = *values[0];
+ src_reg value = *entry->value[0];
for (int i = 1; i < 4; i++) {
/* This is equals() except we don't care about the swizzle. */
- if (value.file != values[i]->file ||
- value.reg != values[i]->reg ||
- value.reg_offset != values[i]->reg_offset ||
- value.type != values[i]->type ||
- value.negate != values[i]->negate ||
- value.abs != values[i]->abs) {
+ if (value.file != entry->value[i]->file ||
+ value.reg != entry->value[i]->reg ||
+ value.reg_offset != entry->value[i]->reg_offset ||
+ value.type != entry->value[i]->type ||
+ value.negate != entry->value[i]->negate ||
+ value.abs != entry->value[i]->abs) {
return false;
}
}
*/
int s[4];
for (int i = 0; i < 4; i++) {
- s[i] = BRW_GET_SWZ(values[i]->swizzle,
+ s[i] = BRW_GET_SWZ(entry->value[i]->swizzle,
BRW_GET_SWZ(inst->src[arg].swizzle, i));
}
value.swizzle = BRW_SWIZZLE4(s[0], s[1], s[2], s[3]);
inst->opcode == SHADER_OPCODE_GEN4_SCRATCH_WRITE)
return false;
- bool is_3src_inst = (inst->opcode == BRW_OPCODE_LRP ||
- inst->opcode == BRW_OPCODE_MAD ||
- inst->opcode == BRW_OPCODE_BFE ||
- inst->opcode == BRW_OPCODE_BFI2);
- if (is_3src_inst && value.file == UNIFORM)
+ if (inst->is_3src() && 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
+ /* we can't generally copy-propagate UD negations becuse we
+ * end up accessing the resulting values as signed integers
+ * instead. See also resolve_ud_negate().
*/
- if (inst->conditional_mod &&
- value.negate &&
+ if (value.negate &&
value.type == BRW_REGISTER_TYPE_UD)
return false;
if (value.equals(inst->src[arg]))
return false;
+ /* Limit saturate propagation only to SEL with src1 bounded within 1.0 and 1.0
+ * otherwise, skip copy propagate altogether
+ */
+ if (entry->saturatemask & (1 << arg)) {
+ switch(inst->opcode) {
+ case BRW_OPCODE_SEL:
+ if (inst->src[1].file != IMM ||
+ inst->src[1].fixed_hw_reg.dw1.f < 0.0 ||
+ inst->src[1].fixed_hw_reg.dw1.f > 1.0) {
+ return false;
+ }
+ if (!inst->saturate)
+ inst->saturate = true;
+ break;
+ default:
+ return false;
+ }
+ }
+
value.type = inst->src[arg].type;
inst->src[arg] = value;
return true;
}
bool
-vec4_visitor::opt_copy_propagation()
+vec4_visitor::opt_copy_propagation(bool do_constant_prop)
{
bool progress = false;
- src_reg *cur_value[virtual_grf_reg_count][4];
+ struct copy_entry entries[alloc.total_size];
- memset(&cur_value, 0, sizeof(cur_value));
+ memset(&entries, 0, sizeof(entries));
- foreach_in_list(vec4_instruction, inst, &instructions) {
+ foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
/* This pass only works on basic blocks. If there's flow
* control, throw out all our information and start from
* scratch.
* src/glsl/opt_copy_propagation.cpp to track available copies.
*/
if (!is_dominated_by_previous_instruction(inst)) {
- memset(cur_value, 0, sizeof(cur_value));
+ memset(&entries, 0, sizeof(entries));
continue;
}
inst->src[i].reladdr)
continue;
- int reg = (virtual_grf_reg_map[inst->src[i].reg] +
+ int reg = (alloc.offsets[inst->src[i].reg] +
inst->src[i].reg_offset);
/* Find the regs that each swizzle component came from.
*/
- src_reg *values[4];
+ struct copy_entry entry;
+ memset(&entry, 0, sizeof(copy_entry));
int c;
for (c = 0; c < 4; c++) {
- values[c] = cur_value[reg][BRW_GET_SWZ(inst->src[i].swizzle, c)];
+ int channel = BRW_GET_SWZ(inst->src[i].swizzle, c);
+ entry.value[c] = entries[reg].value[channel];
/* If there's no available copy for this channel, bail.
* We could be more aggressive here -- some channels might
* not get used based on the destination writemask.
*/
- if (!values[c])
+ if (!entry.value[c])
break;
+ entry.saturatemask |=
+ (entries[reg].saturatemask & (1 << channel) ? 1 : 0) << c;
+
/* We'll only be able to copy propagate if the sources are
* all from the same file -- there's no ability to swizzle
* 0 or 1 constants in with source registers like in i915.
*/
- if (c > 0 && values[c - 1]->file != values[c]->file)
+ if (c > 0 && entry.value[c - 1]->file != entry.value[c]->file)
break;
}
if (c != 4)
continue;
- if (try_constant_propagate(brw, inst, i, values))
+ if (do_constant_prop && try_constant_propagate(brw, inst, i, &entry))
progress = true;
- if (try_copy_propagate(brw, inst, i, values))
+ if (try_copy_propagate(brw, inst, i, &entry))
progress = true;
}
/* Track available source registers. */
if (inst->dst.file == GRF) {
const int reg =
- virtual_grf_reg_map[inst->dst.reg] + inst->dst.reg_offset;
+ alloc.offsets[inst->dst.reg] + inst->dst.reg_offset;
/* Update our destination's current channel values. For a direct copy,
* the value is the newly propagated source. Otherwise, we don't know
* the new value, so clear it.
*/
bool direct_copy = is_direct_copy(inst);
+ entries[reg].saturatemask = 0x0;
for (int i = 0; i < 4; i++) {
if (inst->dst.writemask & (1 << i)) {
- cur_value[reg][i] = direct_copy ? &inst->src[0] : NULL;
+ entries[reg].value[i] = (!inst->saturate && direct_copy) ? &inst->src[0] : NULL;
+ entries[reg].saturatemask |= (((inst->saturate && direct_copy) ? 1 : 0) << i);
}
}
* our destination's updated channels, as the two are no longer equal.
*/
if (inst->dst.reladdr)
- memset(cur_value, 0, sizeof(cur_value));
+ memset(&entries, 0, sizeof(entries));
else {
- for (int i = 0; i < virtual_grf_reg_count; i++) {
+ for (unsigned i = 0; i < alloc.total_size; i++) {
for (int j = 0; j < 4; j++) {
- if (is_channel_updated(inst, cur_value[i], j)){
- cur_value[i][j] = NULL;
- }
+ if (is_channel_updated(inst, entries[i].value, j)){
+ entries[i].value[j] = NULL;
+ entries[i].saturatemask &= ~(1 << j);
+ }
}
}
}