2 * Copyright (C) 2010 Marek Olšák <maraeo@gmail.com>
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:
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.
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.
28 #include "radeon_remove_constants.h"
30 void rc_remove_unused_constants(struct radeon_compiler
*c
, void *user
)
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
;
41 if (!c
->Program
.Constants
.Count
) {
42 *out_remap_table
= NULL
;
46 const_used
= malloc(c
->Program
.Constants
.Count
);
47 memset(const_used
, 0, c
->Program
.Constants
.Count
);
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
);
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
) {
59 const_used
[inst
->U
.I
.SrcReg
[i
].Index
] = 1;
65 /* Pass 2: If there is relative addressing, mark all externals as used. */
67 for (unsigned i
= 0; i
< c
->Program
.Constants
.Count
; i
++)
68 if (constants
[i
].Type
== RC_CONSTANT_EXTERNAL
)
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;
78 for (unsigned i
= 0; i
< c
->Program
.Constants
.Count
; i
++) {
80 remap_table
[new_count
] = i
;
81 inv_remap_table
[i
] = new_count
;
84 if (constants
[i
].Type
== RC_CONSTANT_EXTERNAL
)
85 are_externals_remapped
= 1;
87 constants
[new_count
] = constants
[i
];
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
));
99 /* Pass 4: Redirect reads of all constants to their new locations. */
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
);
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
];
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
;
119 if (are_externals_remapped
) {
120 *out_remap_table
= remap_table
;
122 *out_remap_table
= NULL
;
127 free(inv_remap_table
);