Merge branch '7.8'
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / radeon_program.c
1 /*
2 * Copyright (C) 2008 Nicolai Haehnle.
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 #include "radeon_program.h"
29
30 #include <stdio.h>
31
32 #include "radeon_compiler.h"
33
34
35 /**
36 * Transform the given clause in the following way:
37 * 1. Replace it with an empty clause
38 * 2. For every instruction in the original clause, try the given
39 * transformations in order.
40 * 3. If one of the transformations returns GL_TRUE, assume that it
41 * 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 at
48 * one instruction at a time.
49 */
50 void radeonLocalTransform(
51 struct radeon_compiler * c,
52 int num_transformations,
53 struct radeon_program_transformation* transformations)
54 {
55 struct rc_instruction * inst = c->Program.Instructions.Next;
56
57 while(inst != &c->Program.Instructions) {
58 struct rc_instruction * current = inst;
59 int i;
60
61 inst = inst->Next;
62
63 for(i = 0; i < num_transformations; ++i) {
64 struct radeon_program_transformation* t = transformations + i;
65
66 if (t->function(c, current, t->userData))
67 break;
68 }
69 }
70 }
71
72 /**
73 * Left multiplication of a register with a swizzle
74 */
75 struct rc_src_register lmul_swizzle(unsigned int swizzle, struct rc_src_register srcreg)
76 {
77 struct rc_src_register tmp = srcreg;
78 int i;
79 tmp.Swizzle = 0;
80 tmp.Negate = 0;
81 for(i = 0; i < 4; ++i) {
82 rc_swizzle swz = GET_SWZ(swizzle, i);
83 if (swz < 4) {
84 tmp.Swizzle |= GET_SWZ(srcreg.Swizzle, swz) << (i*3);
85 tmp.Negate |= GET_BIT(srcreg.Negate, swz) << i;
86 } else {
87 tmp.Swizzle |= swz << (i*3);
88 }
89 }
90 return tmp;
91 }
92
93 unsigned int rc_find_free_temporary(struct radeon_compiler * c)
94 {
95 char used[RC_REGISTER_MAX_INDEX];
96 unsigned int i;
97 struct rc_instruction * rcinst;
98
99 memset(used, 0, sizeof(used));
100
101 for (rcinst = c->Program.Instructions.Next; rcinst != &c->Program.Instructions; rcinst = rcinst->Next) {
102 const struct rc_sub_instruction *inst = &rcinst->U.I;
103 const struct rc_opcode_info *opcode = rc_get_opcode_info(inst->Opcode);
104 unsigned int k;
105
106 for (k = 0; k < opcode->NumSrcRegs; k++) {
107 if (inst->SrcReg[k].File == RC_FILE_TEMPORARY)
108 used[inst->SrcReg[k].Index] = 1;
109 }
110
111 if (opcode->HasDstReg) {
112 if (inst->DstReg.File == RC_FILE_TEMPORARY)
113 used[inst->DstReg.Index] = 1;
114 }
115 }
116
117 for (i = 0; i < RC_REGISTER_MAX_INDEX; i++) {
118 if (!used[i])
119 return i;
120 }
121
122 rc_error(c, "Ran out of temporary registers\n");
123 return 0;
124 }
125
126
127 struct rc_instruction *rc_alloc_instruction(struct radeon_compiler * c)
128 {
129 struct rc_instruction * inst = memory_pool_malloc(&c->Pool, sizeof(struct rc_instruction));
130
131 memset(inst, 0, sizeof(struct rc_instruction));
132
133 inst->U.I.Opcode = RC_OPCODE_ILLEGAL_OPCODE;
134 inst->U.I.DstReg.WriteMask = RC_MASK_XYZW;
135 inst->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XYZW;
136 inst->U.I.SrcReg[1].Swizzle = RC_SWIZZLE_XYZW;
137 inst->U.I.SrcReg[2].Swizzle = RC_SWIZZLE_XYZW;
138
139 return inst;
140 }
141
142 void rc_insert_instruction(struct rc_instruction * after, struct rc_instruction * inst)
143 {
144 inst->Prev = after;
145 inst->Next = after->Next;
146
147 inst->Prev->Next = inst;
148 inst->Next->Prev = inst;
149 }
150
151 struct rc_instruction *rc_insert_new_instruction(struct radeon_compiler * c, struct rc_instruction * after)
152 {
153 struct rc_instruction * inst = rc_alloc_instruction(c);
154
155 rc_insert_instruction(after, inst);
156
157 return inst;
158 }
159
160 void rc_remove_instruction(struct rc_instruction * inst)
161 {
162 inst->Prev->Next = inst->Next;
163 inst->Next->Prev = inst->Prev;
164 }
165
166 /**
167 * Return the number of instructions in the program.
168 */
169 unsigned int rc_recompute_ips(struct radeon_compiler * c)
170 {
171 unsigned int ip = 0;
172 struct rc_instruction * inst;
173
174 for(inst = c->Program.Instructions.Next;
175 inst != &c->Program.Instructions;
176 inst = inst->Next) {
177 inst->IP = ip++;
178 }
179
180 c->Program.Instructions.IP = 0xcafedead;
181
182 return ip;
183 }