Path: blob/21.2-virgl/src/gallium/drivers/r300/compiler/radeon_program.c
4574 views
/*1* Copyright (C) 2008 Nicolai Haehnle.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 "radeon_program.h"2829#include <stdio.h>3031#include "radeon_compiler.h"32#include "radeon_dataflow.h"333435/**36* Transform the given clause in the following way:37* 1. Replace it with an empty clause38* 2. For every instruction in the original clause, try the given39* transformations in order.40* 3. If one of the transformations returns GL_TRUE, assume that it41* has emitted the appropriate instruction(s) into the new clause;42* otherwise, copy the instruction verbatim.43*44* \note The transformation is currently not recursive; in other words,45* instructions emitted by transformations are not transformed.46*47* \note The transform is called 'local' because it can only look at48* one instruction at a time.49*/50void rc_local_transform(51struct radeon_compiler * c,52void *user)53{54struct radeon_program_transformation *transformations =55(struct radeon_program_transformation*)user;56struct rc_instruction * inst = c->Program.Instructions.Next;5758while(inst != &c->Program.Instructions) {59struct rc_instruction * current = inst;60int i;6162inst = inst->Next;6364for(i = 0; transformations[i].function; ++i) {65struct radeon_program_transformation* t = transformations + i;6667if (t->function(c, current, t->userData))68break;69}70}71}7273struct get_used_temporaries_data {74unsigned char * Used;75unsigned int UsedLength;76};7778static void get_used_temporaries_cb(79void * userdata,80struct rc_instruction * inst,81rc_register_file file,82unsigned int index,83unsigned int mask)84{85struct get_used_temporaries_data * d = userdata;8687if (file != RC_FILE_TEMPORARY)88return;8990if (index >= d->UsedLength)91return;9293d->Used[index] |= mask;94}9596/**97* This function fills in the parameter 'used' with a writemask that98* represent which components of each temporary register are used by the99* program. This is meant to be combined with rc_find_free_temporary_list as a100* more efficient version of rc_find_free_temporary.101* @param used The function does not initialize this parameter.102*/103void rc_get_used_temporaries(104struct radeon_compiler * c,105unsigned char * used,106unsigned int used_length)107{108struct rc_instruction * inst;109struct get_used_temporaries_data d;110d.Used = used;111d.UsedLength = used_length;112113for(inst = c->Program.Instructions.Next;114inst != &c->Program.Instructions; inst = inst->Next) {115116rc_for_all_reads_mask(inst, get_used_temporaries_cb, &d);117rc_for_all_writes_mask(inst, get_used_temporaries_cb, &d);118}119}120121/* Search a list of used temporaries for a free one122* \sa rc_get_used_temporaries123* @note If this functions finds a free temporary, it will mark it as used124* in the used temporary list (param 'used')125* @param used list of used temporaries126* @param used_length number of items in param 'used'127* @param mask which components must be free in the temporary index that is128* returned.129* @return -1 If there are no more free temporaries, otherwise the index of130* a temporary register where the components specified in param 'mask' are131* not being used.132*/133int rc_find_free_temporary_list(134struct radeon_compiler * c,135unsigned char * used,136unsigned int used_length,137unsigned int mask)138{139int i;140for(i = 0; i < used_length; i++) {141if ((~used[i] & mask) == mask) {142used[i] |= mask;143return i;144}145}146return -1;147}148149unsigned int rc_find_free_temporary(struct radeon_compiler * c)150{151unsigned char used[RC_REGISTER_MAX_INDEX];152int free;153154memset(used, 0, sizeof(used));155156rc_get_used_temporaries(c, used, RC_REGISTER_MAX_INDEX);157158free = rc_find_free_temporary_list(c, used, RC_REGISTER_MAX_INDEX,159RC_MASK_XYZW);160if (free < 0) {161rc_error(c, "Ran out of temporary registers\n");162return 0;163}164return free;165}166167168struct rc_instruction *rc_alloc_instruction(struct radeon_compiler * c)169{170struct rc_instruction * inst = memory_pool_malloc(&c->Pool, sizeof(struct rc_instruction));171172memset(inst, 0, sizeof(struct rc_instruction));173174inst->U.I.Opcode = RC_OPCODE_ILLEGAL_OPCODE;175inst->U.I.DstReg.WriteMask = RC_MASK_XYZW;176inst->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XYZW;177inst->U.I.SrcReg[1].Swizzle = RC_SWIZZLE_XYZW;178inst->U.I.SrcReg[2].Swizzle = RC_SWIZZLE_XYZW;179180return inst;181}182183void rc_insert_instruction(struct rc_instruction * after, struct rc_instruction * inst)184{185inst->Prev = after;186inst->Next = after->Next;187188inst->Prev->Next = inst;189inst->Next->Prev = inst;190}191192struct rc_instruction *rc_insert_new_instruction(struct radeon_compiler * c, struct rc_instruction * after)193{194struct rc_instruction * inst = rc_alloc_instruction(c);195196rc_insert_instruction(after, inst);197198return inst;199}200201void rc_remove_instruction(struct rc_instruction * inst)202{203inst->Prev->Next = inst->Next;204inst->Next->Prev = inst->Prev;205}206207/**208* Return the number of instructions in the program.209*/210unsigned int rc_recompute_ips(struct radeon_compiler * c)211{212unsigned int ip = 0;213struct rc_instruction * inst;214215for(inst = c->Program.Instructions.Next;216inst != &c->Program.Instructions;217inst = inst->Next) {218inst->IP = ip++;219}220221c->Program.Instructions.IP = 0xcafedead;222223return ip;224}225226227