2 * Copyright © 2014 Broadcom
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 * This modifies instructions that:
28 * 1. exclusively consume a value read from the VPM to directly read the VPM if
29 * other operands allow it.
30 * 2. generate the value consumed by a VPM write to write directly into the VPM.
36 qir_opt_vpm(struct vc4_compile *c)
38 if (c->stage == QSTAGE_FRAG)
41 bool progress = false;
42 struct qinst *vpm_writes[64] = { 0 };
43 uint32_t use_count[c->num_temps];
44 uint32_t vpm_write_count = 0;
45 memset(&use_count, 0, sizeof(use_count));
47 list_for_each_entry(struct qinst, inst, &c->instructions, link) {
48 switch (inst->dst.file) {
50 vpm_writes[vpm_write_count++] = inst;
56 for (int i = 0; i < qir_get_op_nsrc(inst->op); i++) {
57 if (inst->src[i].file == QFILE_TEMP) {
58 uint32_t temp = inst->src[i].index;
64 /* For instructions reading from a temporary that contains a VPM read
65 * result, try to move the instruction up in place of the VPM read.
67 list_for_each_entry(struct qinst, inst, &c->instructions, link) {
71 if (qir_depends_on_flags(inst) || inst->sf)
74 if (qir_has_side_effects(c, inst) ||
75 qir_has_side_effect_reads(c, inst) ||
79 for (int j = 0; j < qir_get_op_nsrc(inst->op); j++) {
80 if (inst->src[j].file != QFILE_TEMP ||
84 uint32_t temp = inst->src[j].index;
86 /* Since VPM reads pull from a FIFO, we only get to
87 * read each VPM entry once (unless we reset the read
88 * pointer). That means we can't copy-propagate a VPM
89 * read to multiple locations.
91 if (use_count[temp] != 1)
94 struct qinst *mov = c->defs[temp];
96 (mov->op != QOP_MOV &&
97 mov->op != QOP_FMOV &&
98 mov->op != QOP_MMOV) ||
99 mov->src[0].file != QFILE_VPM) {
104 for (int k = 0; k < qir_get_op_nsrc(inst->op); k++) {
105 if (inst->src[k].file == QFILE_TEMP)
109 /* The instruction is safe to reorder if its other
110 * sources are independent of previous instructions
113 list_del(&inst->link);
114 inst->src[j] = mov->src[0];
115 list_replace(&mov->link, &inst->link);
116 c->defs[temp] = NULL;
124 for (int i = 0; i < vpm_write_count; i++) {
125 if (!qir_is_raw_mov(vpm_writes[i]) ||
126 vpm_writes[i]->src[0].file != QFILE_TEMP) {
130 uint32_t temp = vpm_writes[i]->src[0].index;
131 if (use_count[temp] != 1)
134 struct qinst *inst = c->defs[temp];
138 if (qir_depends_on_flags(inst) || inst->sf)
141 if (qir_has_side_effects(c, inst) ||
142 qir_has_side_effect_reads(c, inst)) {
146 /* Move the generating instruction to the end of the program
147 * to maintain the order of the VPM writes.
149 assert(!vpm_writes[i]->sf);
150 list_del(&inst->link);
151 list_addtail(&inst->link, &vpm_writes[i]->link);
152 qir_remove_instruction(c, vpm_writes[i]);
154 c->defs[inst->dst.index] = NULL;
155 inst->dst.file = QFILE_VPM;