1 /*
2 * Copyright © 2014 Broadcom
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 /**
25 * @file vc4_opt_vpm.c
26 *
27 * This modifies instructions that exclusively consume a value read from the
28 * VPM to directly read the VPM if other operands allow it.
29 */
30
31 #include "vc4_qir.h"
32
33 bool
qir_opt_vpm(struct vc4_compile * c)34 qir_opt_vpm(struct vc4_compile *c)
35 {
36 if (c->stage == QSTAGE_FRAG)
37 return false;
38
39 /* For now, only do this pass when we don't have control flow. */
40 struct qblock *block = qir_entry_block(c);
41 if (block != qir_exit_block(c))
42 return false;
43
44 bool progress = false;
45 uint32_t use_count[c->num_temps];
46 memset(&use_count, 0, sizeof(use_count));
47
48 qir_for_each_inst_inorder(inst, c) {
49 for (int i = 0; i < qir_get_nsrc(inst); i++) {
50 if (inst->src[i].file == QFILE_TEMP) {
51 uint32_t temp = inst->src[i].index;
52 use_count[temp]++;
53 }
54 }
55 }
56
57 /* For instructions reading from a temporary that contains a VPM read
58 * result, try to move the instruction up in place of the VPM read.
59 */
60 qir_for_each_inst_inorder(inst, c) {
61 if (!inst)
62 continue;
63
64 if (qir_depends_on_flags(inst) || inst->sf)
65 continue;
66
67 if (qir_has_side_effects(c, inst) ||
68 qir_has_side_effect_reads(c, inst) ||
69 qir_is_tex(inst))
70 continue;
71
72 for (int j = 0; j < qir_get_nsrc(inst); j++) {
73 if (inst->src[j].file != QFILE_TEMP ||
74 inst->src[j].pack)
75 continue;
76
77 uint32_t temp = inst->src[j].index;
78
79 /* Since VPM reads pull from a FIFO, we only get to
80 * read each VPM entry once (unless we reset the read
81 * pointer). That means we can't copy-propagate a VPM
82 * read to multiple locations.
83 */
84 if (use_count[temp] != 1)
85 continue;
86
87 struct qinst *mov = c->defs[temp];
88 if (!mov ||
89 (mov->op != QOP_MOV &&
90 mov->op != QOP_FMOV &&
91 mov->op != QOP_MMOV) ||
92 mov->src[0].file != QFILE_VPM) {
93 continue;
94 }
95
96 uint32_t temps = 0;
97 for (int k = 0; k < qir_get_nsrc(inst); k++) {
98 if (inst->src[k].file == QFILE_TEMP)
99 temps++;
100 }
101
102 /* The instruction is safe to reorder if its other
103 * sources are independent of previous instructions
104 */
105 if (temps == 1) {
106 inst->src[j] = mov->src[0];
107
108 list_del(&inst->link);
109 list_addtail(&inst->link, &mov->link);
110 qir_remove_instruction(c, mov);
111
112 progress = true;
113 break;
114 }
115 }
116 }
117
118 return progress;
119 }
120