r300/compiler: refactor vertex shader compilation
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / radeon_remove_constants.c
1 /*
2 * Copyright (C) 2010 Marek Olšák <maraeo@gmail.com>
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28 #include "radeon_remove_constants.h"
29
30 void rc_remove_unused_constants(struct radeon_compiler *c, void *user)
31 {
32 unsigned **out_remap_table = (unsigned**)user;
33 unsigned char *const_used;
34 unsigned *remap_table;
35 unsigned *inv_remap_table;
36 unsigned has_rel_addr = 0;
37 unsigned is_identity = 1;
38 unsigned are_externals_remapped = 0;
39 struct rc_constant *constants = c->Program.Constants.Constants;
40
41 if (!c->Program.Constants.Count) {
42 *out_remap_table = NULL;
43 return;
44 }
45
46 const_used = malloc(c->Program.Constants.Count);
47 memset(const_used, 0, c->Program.Constants.Count);
48
49 /* Pass 1: Mark used constants. */
50 for (struct rc_instruction *inst = c->Program.Instructions.Next;
51 inst != &c->Program.Instructions; inst = inst->Next) {
52 const struct rc_opcode_info *opcode = rc_get_opcode_info(inst->U.I.Opcode);
53
54 for (unsigned i = 0; i < opcode->NumSrcRegs; i++) {
55 if (inst->U.I.SrcReg[i].File == RC_FILE_CONSTANT) {
56 if (inst->U.I.SrcReg[i].RelAddr) {
57 has_rel_addr = 1;
58 } else {
59 const_used[inst->U.I.SrcReg[i].Index] = 1;
60 }
61 }
62 }
63 }
64
65 /* Pass 2: If there is relative addressing, mark all externals as used. */
66 if (has_rel_addr) {
67 for (unsigned i = 0; i < c->Program.Constants.Count; i++)
68 if (constants[i].Type == RC_CONSTANT_EXTERNAL)
69 const_used[i] = 1;
70 }
71
72 /* Pass 3: Make the remapping table and remap constants.
73 * This pass removes unused constants simply by overwriting them by other constants. */
74 remap_table = malloc(c->Program.Constants.Count * sizeof(unsigned));
75 inv_remap_table = malloc(c->Program.Constants.Count * sizeof(unsigned));
76 unsigned new_count = 0;
77
78 for (unsigned i = 0; i < c->Program.Constants.Count; i++) {
79 if (const_used[i]) {
80 remap_table[new_count] = i;
81 inv_remap_table[i] = new_count;
82
83 if (i != new_count) {
84 if (constants[i].Type == RC_CONSTANT_EXTERNAL)
85 are_externals_remapped = 1;
86
87 constants[new_count] = constants[i];
88 is_identity = 0;
89 }
90 new_count++;
91 }
92 }
93
94 /* is_identity ==> new_count == old_count
95 * !is_identity ==> new_count < old_count */
96 assert( is_identity || new_count < c->Program.Constants.Count);
97 assert(!(has_rel_addr && are_externals_remapped));
98
99 /* Pass 4: Redirect reads of all constants to their new locations. */
100 if (!is_identity) {
101 for (struct rc_instruction *inst = c->Program.Instructions.Next;
102 inst != &c->Program.Instructions; inst = inst->Next) {
103 const struct rc_opcode_info *opcode = rc_get_opcode_info(inst->U.I.Opcode);
104
105 for (unsigned i = 0; i < opcode->NumSrcRegs; i++) {
106 if (inst->U.I.SrcReg[i].File == RC_FILE_CONSTANT) {
107 inst->U.I.SrcReg[i].Index = inv_remap_table[inst->U.I.SrcReg[i].Index];
108 }
109 }
110 }
111
112 }
113
114 /* Set the new constant count. Note that new_count may be less than
115 * Count even though the remapping function is identity. In that case,
116 * the constants have been removed at the end of the array. */
117 c->Program.Constants.Count = new_count;
118
119 if (are_externals_remapped) {
120 *out_remap_table = remap_table;
121 } else {
122 *out_remap_table = NULL;
123 free(remap_table);
124 }
125
126 free(const_used);
127 free(inv_remap_table);
128 }