Path: blob/21.2-virgl/src/gallium/drivers/r300/compiler/radeon_remove_constants.c
4574 views
/*1* Copyright (C) 2010 Marek Olšák <[email protected]>2*3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining6* a copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sublicense, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial15* portions of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,18* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.20* IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE21* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION22* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION23* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25*/2627#include <stdlib.h>28#include "radeon_remove_constants.h"29#include "radeon_dataflow.h"3031struct mark_used_data {32unsigned char * const_used;33unsigned * has_rel_addr;34};3536static void remap_regs(void * userdata, struct rc_instruction * inst,37rc_register_file * pfile, unsigned int * pindex)38{39unsigned *inv_remap_table = userdata;4041if (*pfile == RC_FILE_CONSTANT) {42*pindex = inv_remap_table[*pindex];43}44}4546static void mark_used(void * userdata, struct rc_instruction * inst,47struct rc_src_register * src)48{49struct mark_used_data * d = userdata;5051if (src->File == RC_FILE_CONSTANT) {52if (src->RelAddr) {53*d->has_rel_addr = 1;54} else {55d->const_used[src->Index] = 1;56}57}58}5960void rc_remove_unused_constants(struct radeon_compiler *c, void *user)61{62unsigned **out_remap_table = (unsigned**)user;63unsigned char *const_used;64unsigned *remap_table;65unsigned *inv_remap_table;66unsigned has_rel_addr = 0;67unsigned is_identity = 1;68unsigned are_externals_remapped = 0;69struct rc_constant *constants = c->Program.Constants.Constants;70struct mark_used_data d;71unsigned new_count;7273if (!c->Program.Constants.Count) {74*out_remap_table = NULL;75return;76}7778const_used = malloc(c->Program.Constants.Count);79memset(const_used, 0, c->Program.Constants.Count);8081d.const_used = const_used;82d.has_rel_addr = &has_rel_addr;8384/* Pass 1: Mark used constants. */85for (struct rc_instruction *inst = c->Program.Instructions.Next;86inst != &c->Program.Instructions; inst = inst->Next) {87rc_for_all_reads_src(inst, mark_used, &d);88}8990/* Pass 2: If there is relative addressing or dead constant elimination91* is disabled, mark all externals as used. */92if (has_rel_addr || !c->remove_unused_constants) {93for (unsigned i = 0; i < c->Program.Constants.Count; i++)94if (constants[i].Type == RC_CONSTANT_EXTERNAL)95const_used[i] = 1;96}9798/* Pass 3: Make the remapping table and remap constants.99* This pass removes unused constants simply by overwriting them by other constants. */100remap_table = malloc(c->Program.Constants.Count * sizeof(unsigned));101inv_remap_table = malloc(c->Program.Constants.Count * sizeof(unsigned));102new_count = 0;103104for (unsigned i = 0; i < c->Program.Constants.Count; i++) {105if (const_used[i]) {106remap_table[new_count] = i;107inv_remap_table[i] = new_count;108109if (i != new_count) {110if (constants[i].Type == RC_CONSTANT_EXTERNAL)111are_externals_remapped = 1;112113constants[new_count] = constants[i];114is_identity = 0;115}116new_count++;117}118}119120/* is_identity ==> new_count == old_count121* !is_identity ==> new_count < old_count */122assert( is_identity || new_count < c->Program.Constants.Count);123assert(!((has_rel_addr || !c->remove_unused_constants) && are_externals_remapped));124125/* Pass 4: Redirect reads of all constants to their new locations. */126if (!is_identity) {127for (struct rc_instruction *inst = c->Program.Instructions.Next;128inst != &c->Program.Instructions; inst = inst->Next) {129rc_remap_registers(inst, remap_regs, inv_remap_table);130}131}132133/* Set the new constant count. Note that new_count may be less than134* Count even though the remapping function is identity. In that case,135* the constants have been removed at the end of the array. */136c->Program.Constants.Count = new_count;137138if (are_externals_remapped) {139*out_remap_table = remap_table;140} else {141*out_remap_table = NULL;142free(remap_table);143}144145free(const_used);146free(inv_remap_table);147148if (c->Debug & RC_DBG_LOG)149rc_constants_print(&c->Program.Constants);150}151152153