1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2008 Nicolai Haehnle. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * All Rights Reserved. 5bf215546Sopenharmony_ci * 6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining 7bf215546Sopenharmony_ci * a copy of this software and associated documentation files (the 8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 10bf215546Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to 11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 12bf215546Sopenharmony_ci * the following conditions: 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the 15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial 16bf215546Sopenharmony_ci * portions of the Software. 17bf215546Sopenharmony_ci * 18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19bf215546Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 21bf215546Sopenharmony_ci * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 22bf215546Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23bf215546Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24bf215546Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci */ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci#include "radeon_program.h" 29bf215546Sopenharmony_ci 30bf215546Sopenharmony_ci#include <stdio.h> 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci#include "radeon_compiler.h" 33bf215546Sopenharmony_ci#include "radeon_dataflow.h" 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ci 36bf215546Sopenharmony_ci/** 37bf215546Sopenharmony_ci * Transform the given clause in the following way: 38bf215546Sopenharmony_ci * 1. Replace it with an empty clause 39bf215546Sopenharmony_ci * 2. For every instruction in the original clause, try the given 40bf215546Sopenharmony_ci * transformations in order. 41bf215546Sopenharmony_ci * 3. If one of the transformations returns GL_TRUE, assume that it 42bf215546Sopenharmony_ci * has emitted the appropriate instruction(s) into the new clause; 43bf215546Sopenharmony_ci * otherwise, copy the instruction verbatim. 44bf215546Sopenharmony_ci * 45bf215546Sopenharmony_ci * \note The transformation is currently not recursive; in other words, 46bf215546Sopenharmony_ci * instructions emitted by transformations are not transformed. 47bf215546Sopenharmony_ci * 48bf215546Sopenharmony_ci * \note The transform is called 'local' because it can only look at 49bf215546Sopenharmony_ci * one instruction at a time. 50bf215546Sopenharmony_ci */ 51bf215546Sopenharmony_civoid rc_local_transform( 52bf215546Sopenharmony_ci struct radeon_compiler * c, 53bf215546Sopenharmony_ci void *user) 54bf215546Sopenharmony_ci{ 55bf215546Sopenharmony_ci struct radeon_program_transformation *transformations = 56bf215546Sopenharmony_ci (struct radeon_program_transformation*)user; 57bf215546Sopenharmony_ci struct rc_instruction * inst = c->Program.Instructions.Next; 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci while(inst != &c->Program.Instructions) { 60bf215546Sopenharmony_ci struct rc_instruction * current = inst; 61bf215546Sopenharmony_ci int i; 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci inst = inst->Next; 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ci for(i = 0; transformations[i].function; ++i) { 66bf215546Sopenharmony_ci struct radeon_program_transformation* t = transformations + i; 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci if (t->function(c, current, t->userData)) 69bf215546Sopenharmony_ci break; 70bf215546Sopenharmony_ci } 71bf215546Sopenharmony_ci } 72bf215546Sopenharmony_ci} 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_cistruct get_used_temporaries_data { 75bf215546Sopenharmony_ci unsigned char * Used; 76bf215546Sopenharmony_ci unsigned int UsedLength; 77bf215546Sopenharmony_ci}; 78bf215546Sopenharmony_ci 79bf215546Sopenharmony_cistatic void get_used_temporaries_cb( 80bf215546Sopenharmony_ci void * userdata, 81bf215546Sopenharmony_ci struct rc_instruction * inst, 82bf215546Sopenharmony_ci rc_register_file file, 83bf215546Sopenharmony_ci unsigned int index, 84bf215546Sopenharmony_ci unsigned int mask) 85bf215546Sopenharmony_ci{ 86bf215546Sopenharmony_ci struct get_used_temporaries_data * d = userdata; 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci if (file != RC_FILE_TEMPORARY) 89bf215546Sopenharmony_ci return; 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci if (index >= d->UsedLength) 92bf215546Sopenharmony_ci return; 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_ci d->Used[index] |= mask; 95bf215546Sopenharmony_ci} 96bf215546Sopenharmony_ci 97bf215546Sopenharmony_ci/** 98bf215546Sopenharmony_ci * This function fills in the parameter 'used' with a writemask that 99bf215546Sopenharmony_ci * represent which components of each temporary register are used by the 100bf215546Sopenharmony_ci * program. This is meant to be combined with rc_find_free_temporary_list as a 101bf215546Sopenharmony_ci * more efficient version of rc_find_free_temporary. 102bf215546Sopenharmony_ci * @param used The function does not initialize this parameter. 103bf215546Sopenharmony_ci */ 104bf215546Sopenharmony_civoid rc_get_used_temporaries( 105bf215546Sopenharmony_ci struct radeon_compiler * c, 106bf215546Sopenharmony_ci unsigned char * used, 107bf215546Sopenharmony_ci unsigned int used_length) 108bf215546Sopenharmony_ci{ 109bf215546Sopenharmony_ci struct rc_instruction * inst; 110bf215546Sopenharmony_ci struct get_used_temporaries_data d; 111bf215546Sopenharmony_ci d.Used = used; 112bf215546Sopenharmony_ci d.UsedLength = used_length; 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci for(inst = c->Program.Instructions.Next; 115bf215546Sopenharmony_ci inst != &c->Program.Instructions; inst = inst->Next) { 116bf215546Sopenharmony_ci 117bf215546Sopenharmony_ci rc_for_all_reads_mask(inst, get_used_temporaries_cb, &d); 118bf215546Sopenharmony_ci rc_for_all_writes_mask(inst, get_used_temporaries_cb, &d); 119bf215546Sopenharmony_ci } 120bf215546Sopenharmony_ci} 121bf215546Sopenharmony_ci 122bf215546Sopenharmony_ci/* Search a list of used temporaries for a free one 123bf215546Sopenharmony_ci * \sa rc_get_used_temporaries 124bf215546Sopenharmony_ci * @note If this functions finds a free temporary, it will mark it as used 125bf215546Sopenharmony_ci * in the used temporary list (param 'used') 126bf215546Sopenharmony_ci * @param used list of used temporaries 127bf215546Sopenharmony_ci * @param used_length number of items in param 'used' 128bf215546Sopenharmony_ci * @param mask which components must be free in the temporary index that is 129bf215546Sopenharmony_ci * returned. 130bf215546Sopenharmony_ci * @return -1 If there are no more free temporaries, otherwise the index of 131bf215546Sopenharmony_ci * a temporary register where the components specified in param 'mask' are 132bf215546Sopenharmony_ci * not being used. 133bf215546Sopenharmony_ci */ 134bf215546Sopenharmony_ciint rc_find_free_temporary_list( 135bf215546Sopenharmony_ci struct radeon_compiler * c, 136bf215546Sopenharmony_ci unsigned char * used, 137bf215546Sopenharmony_ci unsigned int used_length, 138bf215546Sopenharmony_ci unsigned int mask) 139bf215546Sopenharmony_ci{ 140bf215546Sopenharmony_ci int i; 141bf215546Sopenharmony_ci for(i = 0; i < used_length; i++) { 142bf215546Sopenharmony_ci if ((~used[i] & mask) == mask) { 143bf215546Sopenharmony_ci used[i] |= mask; 144bf215546Sopenharmony_ci return i; 145bf215546Sopenharmony_ci } 146bf215546Sopenharmony_ci } 147bf215546Sopenharmony_ci return -1; 148bf215546Sopenharmony_ci} 149bf215546Sopenharmony_ci 150bf215546Sopenharmony_ciunsigned int rc_find_free_temporary(struct radeon_compiler * c) 151bf215546Sopenharmony_ci{ 152bf215546Sopenharmony_ci unsigned char used[RC_REGISTER_MAX_INDEX]; 153bf215546Sopenharmony_ci int free; 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_ci memset(used, 0, sizeof(used)); 156bf215546Sopenharmony_ci 157bf215546Sopenharmony_ci rc_get_used_temporaries(c, used, RC_REGISTER_MAX_INDEX); 158bf215546Sopenharmony_ci 159bf215546Sopenharmony_ci free = rc_find_free_temporary_list(c, used, RC_REGISTER_MAX_INDEX, 160bf215546Sopenharmony_ci RC_MASK_XYZW); 161bf215546Sopenharmony_ci if (free < 0) { 162bf215546Sopenharmony_ci rc_error(c, "Ran out of temporary registers\n"); 163bf215546Sopenharmony_ci return 0; 164bf215546Sopenharmony_ci } 165bf215546Sopenharmony_ci return free; 166bf215546Sopenharmony_ci} 167bf215546Sopenharmony_ci 168bf215546Sopenharmony_ci 169bf215546Sopenharmony_cistruct rc_instruction *rc_alloc_instruction(struct radeon_compiler * c) 170bf215546Sopenharmony_ci{ 171bf215546Sopenharmony_ci struct rc_instruction * inst = memory_pool_malloc(&c->Pool, sizeof(struct rc_instruction)); 172bf215546Sopenharmony_ci 173bf215546Sopenharmony_ci memset(inst, 0, sizeof(struct rc_instruction)); 174bf215546Sopenharmony_ci 175bf215546Sopenharmony_ci inst->U.I.Opcode = RC_OPCODE_ILLEGAL_OPCODE; 176bf215546Sopenharmony_ci inst->U.I.DstReg.WriteMask = RC_MASK_XYZW; 177bf215546Sopenharmony_ci inst->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XYZW; 178bf215546Sopenharmony_ci inst->U.I.SrcReg[1].Swizzle = RC_SWIZZLE_XYZW; 179bf215546Sopenharmony_ci inst->U.I.SrcReg[2].Swizzle = RC_SWIZZLE_XYZW; 180bf215546Sopenharmony_ci 181bf215546Sopenharmony_ci return inst; 182bf215546Sopenharmony_ci} 183bf215546Sopenharmony_ci 184bf215546Sopenharmony_civoid rc_insert_instruction(struct rc_instruction * after, struct rc_instruction * inst) 185bf215546Sopenharmony_ci{ 186bf215546Sopenharmony_ci inst->Prev = after; 187bf215546Sopenharmony_ci inst->Next = after->Next; 188bf215546Sopenharmony_ci 189bf215546Sopenharmony_ci inst->Prev->Next = inst; 190bf215546Sopenharmony_ci inst->Next->Prev = inst; 191bf215546Sopenharmony_ci} 192bf215546Sopenharmony_ci 193bf215546Sopenharmony_cistruct rc_instruction *rc_insert_new_instruction(struct radeon_compiler * c, struct rc_instruction * after) 194bf215546Sopenharmony_ci{ 195bf215546Sopenharmony_ci struct rc_instruction * inst = rc_alloc_instruction(c); 196bf215546Sopenharmony_ci 197bf215546Sopenharmony_ci rc_insert_instruction(after, inst); 198bf215546Sopenharmony_ci 199bf215546Sopenharmony_ci return inst; 200bf215546Sopenharmony_ci} 201bf215546Sopenharmony_ci 202bf215546Sopenharmony_civoid rc_remove_instruction(struct rc_instruction * inst) 203bf215546Sopenharmony_ci{ 204bf215546Sopenharmony_ci inst->Prev->Next = inst->Next; 205bf215546Sopenharmony_ci inst->Next->Prev = inst->Prev; 206bf215546Sopenharmony_ci} 207bf215546Sopenharmony_ci 208bf215546Sopenharmony_ci/** 209bf215546Sopenharmony_ci * Return the number of instructions in the program. 210bf215546Sopenharmony_ci */ 211bf215546Sopenharmony_ciunsigned int rc_recompute_ips(struct radeon_compiler * c) 212bf215546Sopenharmony_ci{ 213bf215546Sopenharmony_ci unsigned int ip = 0; 214bf215546Sopenharmony_ci struct rc_instruction * inst; 215bf215546Sopenharmony_ci 216bf215546Sopenharmony_ci for(inst = c->Program.Instructions.Next; 217bf215546Sopenharmony_ci inst != &c->Program.Instructions; 218bf215546Sopenharmony_ci inst = inst->Next) { 219bf215546Sopenharmony_ci inst->IP = ip++; 220bf215546Sopenharmony_ci } 221bf215546Sopenharmony_ci 222bf215546Sopenharmony_ci c->Program.Instructions.IP = 0xcafedead; 223bf215546Sopenharmony_ci 224bf215546Sopenharmony_ci return ip; 225bf215546Sopenharmony_ci} 226