• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 v3d_opt_dead_code.c
26  *
27  * This is a simple dead code eliminator for SSA values in VIR.
28  *
29  * It walks all the instructions finding what temps are used, then walks again
30  * to remove instructions writing unused temps.
31  *
32  * This is an inefficient implementation if you have long chains of
33  * instructions where the entire chain is dead, but we expect those to have
34  * been eliminated at the NIR level, and here we're just cleaning up small
35  * problems produced by NIR->VIR.
36  */
37 
38 #include "v3d_compiler.h"
39 
40 static bool debug;
41 
42 static void
dce(struct v3d_compile * c,struct qinst * inst)43 dce(struct v3d_compile *c, struct qinst *inst)
44 {
45         if (debug) {
46                 fprintf(stderr, "Removing: ");
47                 vir_dump_inst(c, inst);
48                 fprintf(stderr, "\n");
49         }
50         assert(inst->qpu.flags.apf == V3D_QPU_PF_NONE);
51         assert(inst->qpu.flags.mpf == V3D_QPU_PF_NONE);
52         assert(inst->qpu.flags.auf == V3D_QPU_UF_NONE);
53         assert(inst->qpu.flags.muf == V3D_QPU_UF_NONE);
54         vir_remove_instruction(c, inst);
55 }
56 
57 static bool
has_nonremovable_reads(struct v3d_compile * c,struct qinst * inst)58 has_nonremovable_reads(struct v3d_compile *c, struct qinst *inst)
59 {
60         for (int i = 0; i < vir_get_nsrc(inst); i++) {
61                 if (inst->src[i].file == QFILE_VPM) {
62                         /* Instance ID, Vertex ID: Should have been removed at
63                          * the NIR level
64                          */
65                         if (inst->src[i].index == ~0)
66                                 return true;
67 
68                         uint32_t attr = inst->src[i].index / 4;
69                         uint32_t offset = inst->src[i].index % 4;
70 
71                         if (c->vattr_sizes[attr] != offset)
72                                 return true;
73 
74                         /* Can't get rid of the last VPM read, or the
75                          * simulator (at least) throws an error.
76                          */
77                         uint32_t total_size = 0;
78                         for (uint32_t i = 0; i < ARRAY_SIZE(c->vattr_sizes); i++)
79                                 total_size += c->vattr_sizes[i];
80                         if (total_size == 1)
81                                 return true;
82                 }
83         }
84 
85         return false;
86 }
87 
88 bool
vir_opt_dead_code(struct v3d_compile * c)89 vir_opt_dead_code(struct v3d_compile *c)
90 {
91         bool progress = false;
92         bool *used = calloc(c->num_temps, sizeof(bool));
93 
94         vir_for_each_inst_inorder(inst, c) {
95                 for (int i = 0; i < vir_get_nsrc(inst); i++) {
96                         if (inst->src[i].file == QFILE_TEMP)
97                                 used[inst->src[i].index] = true;
98                 }
99         }
100 
101         vir_for_each_block(block, c) {
102                 vir_for_each_inst_safe(inst, block) {
103                         if (inst->dst.file != QFILE_NULL &&
104                             !(inst->dst.file == QFILE_TEMP &&
105                               !used[inst->dst.index])) {
106                                 continue;
107                         }
108 
109                         if (vir_has_side_effects(c, inst))
110                                 continue;
111 
112                         if (inst->qpu.flags.apf != V3D_QPU_PF_NONE ||
113                             inst->qpu.flags.mpf != V3D_QPU_PF_NONE ||
114                             inst->qpu.flags.auf != V3D_QPU_UF_NONE ||
115                             inst->qpu.flags.muf != V3D_QPU_UF_NONE ||
116                             has_nonremovable_reads(c, inst)) {
117                                 /* If we can't remove the instruction, but we
118                                  * don't need its destination value, just
119                                  * remove the destination.  The register
120                                  * allocator would trivially color it and it
121                                  * wouldn't cause any register pressure, but
122                                  * it's nicer to read the VIR code without
123                                  * unused destination regs.
124                                  */
125                                 if (inst->dst.file == QFILE_TEMP) {
126                                         if (debug) {
127                                                 fprintf(stderr,
128                                                         "Removing dst from: ");
129                                                 vir_dump_inst(c, inst);
130                                                 fprintf(stderr, "\n");
131                                         }
132                                         c->defs[inst->dst.index] = NULL;
133                                         inst->dst.file = QFILE_NULL;
134                                         progress = true;
135                                 }
136                                 continue;
137                         }
138 
139                         for (int i = 0; i < vir_get_nsrc(inst); i++) {
140                                 if (inst->src[i].file != QFILE_VPM)
141                                         continue;
142                                 uint32_t attr = inst->src[i].index / 4;
143                                 uint32_t offset = (inst->src[i].index % 4);
144 
145                                 if (c->vattr_sizes[attr] == offset) {
146                                         c->num_inputs--;
147                                         c->vattr_sizes[attr]--;
148                                 }
149                         }
150 
151                         dce(c, inst);
152                         progress = true;
153                         continue;
154                 }
155         }
156 
157         free(used);
158 
159         return progress;
160 }
161