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"
29 #include "radeon_dataflow.h"
31 struct mark_used_data
{
32 unsigned char * const_used
;
33 unsigned * has_rel_addr
;
36 static void remap_regs(void * userdata
, struct rc_instruction
* inst
,
37 rc_register_file
* pfile
, unsigned int * pindex
)
39 unsigned *inv_remap_table
= userdata
;
41 if (*pfile
== RC_FILE_CONSTANT
) {
42 *pindex
= inv_remap_table
[*pindex
];
46 static void mark_used(void * userdata
, struct rc_instruction
* inst
,
47 struct rc_src_register
* src
)
49 struct mark_used_data
* d
= userdata
;
51 if (src
->File
== RC_FILE_CONSTANT
) {
55 d
->const_used
[src
->Index
] = 1;
60 void rc_remove_unused_constants(struct radeon_compiler
*c
, void *user
)
62 unsigned **out_remap_table
= (unsigned**)user
;
63 unsigned char *const_used
;
64 unsigned *remap_table
;
65 unsigned *inv_remap_table
;
66 unsigned has_rel_addr
= 0;
67 unsigned is_identity
= 1;
68 unsigned are_externals_remapped
= 0;
69 struct rc_constant
*constants
= c
->Program
.Constants
.Constants
;
70 struct mark_used_data d
;
73 if (!c
->Program
.Constants
.Count
) {
74 *out_remap_table
= NULL
;
78 const_used
= malloc(c
->Program
.Constants
.Count
);
79 memset(const_used
, 0, c
->Program
.Constants
.Count
);
81 d
.const_used
= const_used
;
82 d
.has_rel_addr
= &has_rel_addr
;
84 /* Pass 1: Mark used constants. */
85 for (struct rc_instruction
*inst
= c
->Program
.Instructions
.Next
;
86 inst
!= &c
->Program
.Instructions
; inst
= inst
->Next
) {
87 rc_for_all_reads_src(inst
, mark_used
, &d
);
90 /* Pass 2: If there is relative addressing or dead constant elimination
91 * is disabled, mark all externals as used. */
92 if (has_rel_addr
|| !c
->remove_unused_constants
) {
93 for (unsigned i
= 0; i
< c
->Program
.Constants
.Count
; i
++)
94 if (constants
[i
].Type
== RC_CONSTANT_EXTERNAL
)
98 /* Pass 3: Make the remapping table and remap constants.
99 * This pass removes unused constants simply by overwriting them by other constants. */
100 remap_table
= malloc(c
->Program
.Constants
.Count
* sizeof(unsigned));
101 inv_remap_table
= malloc(c
->Program
.Constants
.Count
* sizeof(unsigned));
104 for (unsigned i
= 0; i
< c
->Program
.Constants
.Count
; i
++) {
106 remap_table
[new_count
] = i
;
107 inv_remap_table
[i
] = new_count
;
109 if (i
!= new_count
) {
110 if (constants
[i
].Type
== RC_CONSTANT_EXTERNAL
)
111 are_externals_remapped
= 1;
113 constants
[new_count
] = constants
[i
];
120 /* is_identity ==> new_count == old_count
121 * !is_identity ==> new_count < old_count */
122 assert( is_identity
|| new_count
< c
->Program
.Constants
.Count
);
123 assert(!((has_rel_addr
|| !c
->remove_unused_constants
) && are_externals_remapped
));
125 /* Pass 4: Redirect reads of all constants to their new locations. */
127 for (struct rc_instruction
*inst
= c
->Program
.Instructions
.Next
;
128 inst
!= &c
->Program
.Instructions
; inst
= inst
->Next
) {
129 rc_remap_registers(inst
, remap_regs
, inv_remap_table
);
133 /* Set the new constant count. Note that new_count may be less than
134 * Count even though the remapping function is identity. In that case,
135 * the constants have been removed at the end of the array. */
136 c
->Program
.Constants
.Count
= new_count
;
138 if (are_externals_remapped
) {
139 *out_remap_table
= remap_table
;
141 *out_remap_table
= NULL
;
146 free(inv_remap_table
);
148 if (c
->Debug
& RC_DBG_LOG
)
149 rc_constants_print(&c
->Program
.Constants
);