Tizen 2.1 base
[sdk/emulator/qemu.git] / gl / mesa / src / gallium / drivers / r300 / compiler / radeon_rename_regs.c
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 "radeon_rename_regs.h"
33
34 #include "radeon_compiler.h"
35 #include "radeon_dataflow.h"
36 #include "radeon_program.h"
37
38 /**
39  * This function renames registers in an attempt to get the code close to
40  * SSA form.  After this function has completed, most of the register are only
41  * written to one time, with a few exceptions.
42  *
43  * This function assumes all the instructions are still of type
44  * RC_INSTRUCTION_NORMAL.
45  */
46 void rc_rename_regs(struct radeon_compiler *c, void *user)
47 {
48         unsigned int i, used_length;
49         int new_index;
50         struct rc_instruction * inst;
51         struct rc_reader_data reader_data;
52         unsigned char * used;
53
54         /* XXX Remove this once the register allocation works with flow control. */
55         for(inst = c->Program.Instructions.Next;
56                                         inst != &c->Program.Instructions;
57                                         inst = inst->Next) {
58                 if (inst->U.I.Opcode == RC_OPCODE_BGNLOOP)
59                         return;
60         }
61
62         used_length = 2 * rc_recompute_ips(c);
63         used = memory_pool_malloc(&c->Pool, sizeof(unsigned char) * used_length);
64         memset(used, 0, sizeof(unsigned char) * used_length);
65
66         rc_get_used_temporaries(c, used, used_length);
67         for(inst = c->Program.Instructions.Next;
68                                         inst != &c->Program.Instructions;
69                                         inst = inst->Next) {
70
71                 if (inst->U.I.DstReg.File != RC_FILE_TEMPORARY)
72                         continue;
73
74                 reader_data.ExitOnAbort = 1;
75                 rc_get_readers(c, inst, &reader_data, NULL, NULL, NULL);
76
77                 if (reader_data.Abort || reader_data.ReaderCount == 0)
78                         continue;
79
80                 new_index = rc_find_free_temporary_list(c, used, used_length,
81                                                 RC_MASK_XYZW);
82                 if (new_index < 0) {
83                         rc_error(c, "Ran out of temporary registers\n");
84                         return;
85                 }
86
87                 reader_data.Writer->U.I.DstReg.Index = new_index;
88                 for(i = 0; i < reader_data.ReaderCount; i++) {
89                         reader_data.Readers[i].U.I.Src->Index = new_index;
90                 }
91         }
92 }