• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2010 Tom Stellard <tstellar@gmail.com>
3  *
4  * All Rights Reserved.
5  *
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:
13  *
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.
17  *
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.
25  *
26  */
27 
28 /**
29  * \file
30  */
31 
32 #include "util/u_math.h"
33 
34 #include "radeon_rename_regs.h"
35 
36 #include "radeon_compiler.h"
37 #include "radeon_list.h"
38 #include "radeon_program.h"
39 #include "radeon_variable.h"
40 
41 /**
42  * This function renames registers in an attempt to get the code close to
43  * SSA form.  After this function has completed, most of the register are only
44  * written to one time, with a few exceptions.
45  *
46  * This function assumes all the instructions are still of type
47  * RC_INSTRUCTION_NORMAL.
48  */
rc_rename_regs(struct radeon_compiler * c,void * user)49 void rc_rename_regs(struct radeon_compiler *c, void *user)
50 {
51 	unsigned int used_length;
52 	struct rc_instruction * inst;
53 	unsigned char * used;
54 	struct rc_list * variables;
55 	struct rc_list * var_ptr;
56 
57 	/* XXX Remove this once the register allocation works with flow control. */
58 	for(inst = c->Program.Instructions.Next;
59 					inst != &c->Program.Instructions;
60 					inst = inst->Next) {
61 		if (inst->U.I.Opcode == RC_OPCODE_BGNLOOP)
62 			return;
63 	}
64 
65 	used_length = MIN2(2 * rc_recompute_ips(c), RC_REGISTER_MAX_INDEX);
66 	used = memory_pool_malloc(&c->Pool, sizeof(unsigned char) * used_length);
67 	memset(used, 0, sizeof(unsigned char) * used_length);
68 
69 	rc_get_used_temporaries(c, used, used_length);
70 	variables = rc_get_variables(c);
71 
72 	for (var_ptr = variables; var_ptr; var_ptr = var_ptr->Next) {
73 		int new_index;
74 		unsigned writemask;
75 		struct rc_variable * var = var_ptr->Item;
76 
77 		if (var->Inst->U.I.DstReg.File != RC_FILE_TEMPORARY) {
78 			continue;
79 		}
80 
81 		new_index = rc_find_free_temporary_list(c, used, used_length,
82 						RC_MASK_XYZW);
83 		if (new_index < 0) {
84 			rc_error(c, "Ran out of temporary registers\n");
85 			return;
86 		}
87 
88 		writemask = rc_variable_writemask_sum(var);
89 		rc_variable_change_dst(var, new_index, writemask);
90 	}
91 }
92