qir_opt_copy_propagation(struct vc4_compile *c)
{
bool progress = false;
- struct simple_node *node;
bool debug = false;
- struct qreg *movs = calloc(c->num_temps, sizeof(struct qreg));
- struct qinst **defs = calloc(c->num_temps, sizeof(struct qreg));
- foreach(node, &c->instructions) {
- struct qinst *inst = (struct qinst *)node;
+ list_for_each_entry(struct qinst, inst, &c->instructions, link) {
+ int nsrc = qir_get_op_nsrc(inst->op);
+ for (int i = 0; i < nsrc; i++) {
+ if (inst->src[i].file != QFILE_TEMP)
+ continue;
- if (inst->dst.file == QFILE_TEMP)
- defs[inst->dst.index] = inst;
+ struct qinst *mov = c->defs[inst->src[i].index];
+ if (!mov ||
+ (mov->op != QOP_MOV &&
+ mov->op != QOP_FMOV &&
+ mov->op != QOP_MMOV)) {
+ continue;
+ }
- /* A single instruction can only read one uniform value. (It
- * could maybe read the same uniform value in two operands,
- * but that doesn't seem important to do).
- */
- bool reads_a_uniform = false;
- for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
- if (inst->src[i].file == QFILE_UNIF)
- reads_a_uniform = true;
- }
+ if (mov->src[0].file != QFILE_TEMP &&
+ mov->src[0].file != QFILE_UNIF) {
+ continue;
+ }
- for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
- int index = inst->src[i].index;
- if (inst->src[i].file == QFILE_TEMP &&
- (movs[index].file == QFILE_TEMP ||
- (movs[index].file == QFILE_UNIF &&
- !reads_a_uniform))) {
- if (debug) {
- fprintf(stderr, "Copy propagate: ");
- qir_dump_inst(c, inst);
- fprintf(stderr, "\n");
- }
+ if (mov->dst.pack)
+ continue;
- inst->src[i] = movs[index];
- if (movs[index].file == QFILE_UNIF)
- reads_a_uniform = true;
+ uint8_t unpack;
+ if (mov->src[0].pack) {
+ /* Make sure that the meaning of the unpack
+ * would be the same between the two
+ * instructions.
+ */
+ if (qir_is_float_input(inst) !=
+ qir_is_float_input(mov)) {
+ continue;
+ }
- if (debug) {
- fprintf(stderr, "to: ");
- qir_dump_inst(c, inst);
- fprintf(stderr, "\n");
+ /* There's only one unpack field, so make sure
+ * this instruction doesn't already use it.
+ */
+ bool already_has_unpack = false;
+ for (int j = 0; j < nsrc; j++) {
+ if (inst->src[j].pack)
+ already_has_unpack = true;
}
+ if (already_has_unpack)
+ continue;
+
+ /* A destination pack requires the PM bit to
+ * be set to a specific value already, which
+ * may be different from ours.
+ */
+ if (inst->dst.pack)
+ continue;
- progress = true;
+ unpack = mov->src[0].pack;
+ } else {
+ unpack = inst->src[i].pack;
}
- }
- if (inst->op == QOP_MOV &&
- inst->dst.file == QFILE_TEMP &&
- (inst->src[0].file != QFILE_TEMP ||
- (defs[inst->src[0].index]->op != QOP_TEX_RESULT &&
- defs[inst->dst.index]->op != QOP_TLB_COLOR_READ))) {
- movs[inst->dst.index] = inst->src[0];
+ if (debug) {
+ fprintf(stderr, "Copy propagate: ");
+ qir_dump_inst(c, inst);
+ fprintf(stderr, "\n");
+ }
+
+ inst->src[i] = mov->src[0];
+ inst->src[i].pack = unpack;
+
+ if (debug) {
+ fprintf(stderr, "to: ");
+ qir_dump_inst(c, inst);
+ fprintf(stderr, "\n");
+ }
+
+ progress = true;
}
}
-
- free(movs);
- free(defs);
return progress;
}