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 /* For now, only do this pass when we don't have control flow. */
42 struct qblock
*block
= qir_entry_block(c
);
43 if (block
!= qir_exit_block(c
))
46 bool progress
= false;
47 struct qinst
*vpm_writes
[64] = { 0 };
48 uint32_t use_count
[c
->num_temps
];
49 uint32_t vpm_write_count
= 0;
50 memset(&use_count
, 0, sizeof(use_count
));
52 qir_for_each_inst_inorder(inst
, c
) {
53 switch (inst
->dst
.file
) {
55 vpm_writes
[vpm_write_count
++] = inst
;
61 for (int i
= 0; i
< qir_get_op_nsrc(inst
->op
); i
++) {
62 if (inst
->src
[i
].file
== QFILE_TEMP
) {
63 uint32_t temp
= inst
->src
[i
].index
;
69 /* For instructions reading from a temporary that contains a VPM read
70 * result, try to move the instruction up in place of the VPM read.
72 qir_for_each_inst_inorder(inst
, c
) {
76 if (qir_depends_on_flags(inst
) || inst
->sf
)
79 if (qir_has_side_effects(c
, inst
) ||
80 qir_has_side_effect_reads(c
, inst
) ||
84 for (int j
= 0; j
< qir_get_op_nsrc(inst
->op
); j
++) {
85 if (inst
->src
[j
].file
!= QFILE_TEMP
||
89 uint32_t temp
= inst
->src
[j
].index
;
91 /* Since VPM reads pull from a FIFO, we only get to
92 * read each VPM entry once (unless we reset the read
93 * pointer). That means we can't copy-propagate a VPM
94 * read to multiple locations.
96 if (use_count
[temp
] != 1)
99 struct qinst
*mov
= c
->defs
[temp
];
101 (mov
->op
!= QOP_MOV
&&
102 mov
->op
!= QOP_FMOV
&&
103 mov
->op
!= QOP_MMOV
) ||
104 mov
->src
[0].file
!= QFILE_VPM
) {
109 for (int k
= 0; k
< qir_get_op_nsrc(inst
->op
); k
++) {
110 if (inst
->src
[k
].file
== QFILE_TEMP
)
114 /* The instruction is safe to reorder if its other
115 * sources are independent of previous instructions
118 inst
->src
[j
] = mov
->src
[0];
120 list_del(&inst
->link
);
121 list_addtail(&inst
->link
, &mov
->link
);
122 qir_remove_instruction(c
, mov
);
130 for (int i
= 0; i
< vpm_write_count
; i
++) {
131 if (!qir_is_raw_mov(vpm_writes
[i
]) ||
132 vpm_writes
[i
]->src
[0].file
!= QFILE_TEMP
) {
136 uint32_t temp
= vpm_writes
[i
]->src
[0].index
;
137 if (use_count
[temp
] != 1)
140 struct qinst
*inst
= c
->defs
[temp
];
144 if (qir_depends_on_flags(inst
) || inst
->sf
)
147 if (qir_has_side_effects(c
, inst
) ||
148 qir_has_side_effect_reads(c
, inst
)) {
152 /* Move the generating instruction to the end of the program
153 * to maintain the order of the VPM writes.
155 assert(!vpm_writes
[i
]->sf
);
156 list_del(&inst
->link
);
157 list_addtail(&inst
->link
, &vpm_writes
[i
]->link
);
158 qir_remove_instruction(c
, vpm_writes
[i
]);
160 c
->defs
[inst
->dst
.index
] = NULL
;
161 inst
->dst
.file
= QFILE_VPM
;