- struct simple_node *node, *t;
- for (node = c->instructions.prev, t = node->prev;
- &c->instructions != node;
- node = t, t = t->prev) {
- struct qinst *inst = (struct qinst *)node;
-
- if (inst->dst.file == QFILE_TEMP &&
- !used[inst->dst.index] &&
- !qir_has_side_effects(inst)) {
- if (debug) {
- fprintf(stderr, "Removing: ");
- qir_dump_inst(inst);
- fprintf(stderr, "\n");
+ qir_for_each_inst_inorder(inst, c) {
+ for (int i = 0; i < qir_get_nsrc(inst); i++) {
+ if (inst->src[i].file == QFILE_TEMP)
+ used[inst->src[i].index] = true;
+ }
+ }
+
+ qir_for_each_block(block, c) {
+ qir_for_each_inst_safe(inst, block) {
+ if (inst->dst.file != QFILE_NULL &&
+ !(inst->dst.file == QFILE_TEMP &&
+ !used[inst->dst.index])) {
+ continue;
+ }
+
+ if (qir_has_side_effects(c, inst))
+ continue;
+
+ if (inst->sf ||
+ has_nonremovable_reads(c, inst)) {
+ /* If we can't remove the instruction, but we
+ * don't need its destination value, just
+ * remove the destination. The register
+ * allocator would trivially color it and it
+ * wouldn't cause any register pressure, but
+ * it's nicer to read the QIR code without
+ * unused destination regs.
+ */
+ if (inst->dst.file == QFILE_TEMP) {
+ if (debug) {
+ fprintf(stderr,
+ "Removing dst from: ");
+ qir_dump_inst(c, inst);
+ fprintf(stderr, "\n");
+ }
+ c->defs[inst->dst.index] = NULL;
+ inst->dst.file = QFILE_NULL;
+ progress = true;
+ }
+ continue;