r300/compiler: Don't use rc_error() unless the error is unrecoverable
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / radeon_emulate_loops.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_emulate_loops.h"
33
34 #include "radeon_compiler.h"
35 #include "radeon_dataflow.h"
36
37 #define VERBOSE 0
38
39 #define DBG(...) do { if (VERBOSE) fprintf(stderr, __VA_ARGS__); } while(0)
40
41 struct const_value {
42 struct radeon_compiler * C;
43 struct rc_src_register * Src;
44 float Value;
45 int HasValue;
46 };
47
48 struct count_inst {
49 struct radeon_compiler * C;
50 int Index;
51 rc_swizzle Swz;
52 float Amount;
53 int Unknown;
54 };
55
56 static float get_constant_value(struct radeon_compiler * c,
57 struct rc_src_register * src,
58 int chan)
59 {
60 float base = 1.0f;
61 int swz = GET_SWZ(src->Swizzle, chan);
62 if(swz >= 4 || src->Index >= c->Program.Constants.Count ){
63 rc_error(c, "get_constant_value: Can't find a value.\n");
64 return 0.0f;
65 }
66 if(GET_BIT(src->Negate, chan)){
67 base = -1.0f;
68 }
69 return base *
70 c->Program.Constants.Constants[src->Index].u.Immediate[swz];
71 }
72
73 static int src_reg_is_immediate(struct rc_src_register * src,
74 struct radeon_compiler * c)
75 {
76 return src->File == RC_FILE_CONSTANT &&
77 c->Program.Constants.Constants[src->Index].Type==RC_CONSTANT_IMMEDIATE;
78 }
79
80 static unsigned int loop_max_possible_iterations(struct radeon_compiler *c,
81 struct loop_info * loop)
82 {
83 unsigned int total_i = rc_recompute_ips(c);
84 unsigned int loop_i = (loop->EndLoop->IP - loop->BeginLoop->IP) - 1;
85 /* +1 because the program already has one iteration of the loop. */
86 return 1 + ((c->max_alu_insts - total_i) / loop_i);
87 }
88
89 static void unroll_loop(struct radeon_compiler * c, struct loop_info * loop,
90 unsigned int iterations)
91 {
92 unsigned int i;
93 struct rc_instruction * ptr;
94 struct rc_instruction * first = loop->BeginLoop->Next;
95 struct rc_instruction * last = loop->EndLoop->Prev;
96 struct rc_instruction * append_to = last;
97 rc_remove_instruction(loop->BeginLoop);
98 rc_remove_instruction(loop->EndLoop);
99 for( i = 1; i < iterations; i++){
100 for(ptr = first; ptr != last->Next; ptr = ptr->Next){
101 struct rc_instruction *new = rc_alloc_instruction(c);
102 memcpy(new, ptr, sizeof(struct rc_instruction));
103 rc_insert_instruction(append_to, new);
104 append_to = new;
105 }
106 }
107 }
108
109
110 static void update_const_value(void * data, struct rc_instruction * inst,
111 rc_register_file file, unsigned int index, unsigned int mask)
112 {
113 struct const_value * value = data;
114 if(value->Src->File != file ||
115 value->Src->Index != index ||
116 !(1 << GET_SWZ(value->Src->Swizzle, 0) & mask)){
117 return;
118 }
119 switch(inst->U.I.Opcode){
120 case RC_OPCODE_MOV:
121 if(!src_reg_is_immediate(&inst->U.I.SrcReg[0], value->C)){
122 return;
123 }
124 value->HasValue = 1;
125 value->Value =
126 get_constant_value(value->C, &inst->U.I.SrcReg[0], 0);
127 break;
128 }
129 }
130
131 static void get_incr_amount(void * data, struct rc_instruction * inst,
132 rc_register_file file, unsigned int index, unsigned int mask)
133 {
134 struct count_inst * count_inst = data;
135 int amnt_src_index;
136 const struct rc_opcode_info * opcode;
137 float amount;
138
139 if(file != RC_FILE_TEMPORARY ||
140 count_inst->Index != index ||
141 (1 << GET_SWZ(count_inst->Swz,0) != mask)){
142 return;
143 }
144 /* Find the index of the counter register. */
145 opcode = rc_get_opcode_info(inst->U.I.Opcode);
146 if(opcode->NumSrcRegs != 2){
147 count_inst->Unknown = 1;
148 return;
149 }
150 if(inst->U.I.SrcReg[0].File == RC_FILE_TEMPORARY &&
151 inst->U.I.SrcReg[0].Index == count_inst->Index &&
152 inst->U.I.SrcReg[0].Swizzle == count_inst->Swz){
153 amnt_src_index = 1;
154 } else if( inst->U.I.SrcReg[1].File == RC_FILE_TEMPORARY &&
155 inst->U.I.SrcReg[1].Index == count_inst->Index &&
156 inst->U.I.SrcReg[1].Swizzle == count_inst->Swz){
157 amnt_src_index = 0;
158 }
159 else{
160 count_inst->Unknown = 1;
161 return;
162 }
163 if(src_reg_is_immediate(&inst->U.I.SrcReg[amnt_src_index],
164 count_inst->C)){
165 amount = get_constant_value(count_inst->C,
166 &inst->U.I.SrcReg[amnt_src_index], 0);
167 }
168 else{
169 count_inst->Unknown = 1 ;
170 return;
171 }
172 switch(inst->U.I.Opcode){
173 case RC_OPCODE_ADD:
174 count_inst->Amount += amount;
175 break;
176 case RC_OPCODE_SUB:
177 if(amnt_src_index == 0){
178 count_inst->Unknown = 0;
179 return;
180 }
181 count_inst->Amount -= amount;
182 break;
183 default:
184 count_inst->Unknown = 1;
185 return;
186 }
187 }
188
189 /**
190 * If c->max_alu_inst is -1, then all eligible loops will be unrolled regardless
191 * of how many iterations they have.
192 */
193 static int try_unroll_loop(struct radeon_compiler * c, struct loop_info * loop)
194 {
195 int end_loops;
196 int iterations;
197 struct count_inst count_inst;
198 float limit_value;
199 struct rc_src_register * counter;
200 struct rc_src_register * limit;
201 struct const_value counter_value;
202 struct rc_instruction * inst;
203
204 /* Find the counter and the upper limit */
205
206 if(src_reg_is_immediate(&loop->Cond->U.I.SrcReg[0], c)){
207 limit = &loop->Cond->U.I.SrcReg[0];
208 counter = &loop->Cond->U.I.SrcReg[1];
209 }
210 else if(src_reg_is_immediate(&loop->Cond->U.I.SrcReg[1], c)){
211 limit = &loop->Cond->U.I.SrcReg[1];
212 counter = &loop->Cond->U.I.SrcReg[0];
213 }
214 else{
215 DBG("No constant limit.\n");
216 return 0;
217 }
218
219 /* Find the initial value of the counter */
220 counter_value.Src = counter;
221 counter_value.Value = 0.0f;
222 counter_value.HasValue = 0;
223 counter_value.C = c;
224 for(inst = c->Program.Instructions.Next; inst != loop->BeginLoop;
225 inst = inst->Next){
226 rc_for_all_writes_mask(inst, update_const_value, &counter_value);
227 }
228 if(!counter_value.HasValue){
229 DBG("Initial counter value cannot be determined.\n");
230 return 0;
231 }
232 DBG("Initial counter value is %f\n", counter_value.Value);
233 /* Determine how the counter is modified each loop */
234 count_inst.C = c;
235 count_inst.Index = counter->Index;
236 count_inst.Swz = counter->Swizzle;
237 count_inst.Amount = 0.0f;
238 count_inst.Unknown = 0;
239 end_loops = 1;
240 for(inst = loop->BeginLoop->Next; end_loops > 0; inst = inst->Next){
241 switch(inst->U.I.Opcode){
242 /* XXX In the future we might want to try to unroll nested
243 * loops here.*/
244 case RC_OPCODE_BGNLOOP:
245 end_loops++;
246 break;
247 case RC_OPCODE_ENDLOOP:
248 loop->EndLoop = inst;
249 end_loops--;
250 break;
251 case RC_OPCODE_BRK:
252 /* Don't unroll loops if it has a BRK instruction
253 * other one used when testing the main conditional
254 * of the loop. */
255
256 /* Make sure we haven't entered a nested loops. */
257 if(inst != loop->Brk && end_loops == 1) {
258 return 0;
259 }
260 break;
261 /* XXX Check if the counter is modified within an if statement.
262 */
263 case RC_OPCODE_IF:
264 break;
265 default:
266 rc_for_all_writes_mask(inst, get_incr_amount, &count_inst);
267 if(count_inst.Unknown){
268 return 0;
269 }
270 break;
271 }
272 }
273 /* Infinite loop */
274 if(count_inst.Amount == 0.0f){
275 return 0;
276 }
277 DBG("Counter is increased by %f each iteration.\n", count_inst.Amount);
278 /* Calculate the number of iterations of this loop. Keeping this
279 * simple, since we only support increment and decrement loops.
280 */
281 limit_value = get_constant_value(c, limit, 0);
282 DBG("Limit is %f.\n", limit_value);
283 /* The iteration calculations are opposite of what you would expect.
284 * In a normal loop, if the condition is met, then loop continues, but
285 * with our loops, if the condition is met, the is exited. */
286 switch(loop->Cond->U.I.Opcode){
287 case RC_OPCODE_SGE:
288 case RC_OPCODE_SLE:
289 iterations = (int) ceilf((limit_value - counter_value.Value) /
290 count_inst.Amount);
291 break;
292
293 case RC_OPCODE_SGT:
294 case RC_OPCODE_SLT:
295 iterations = (int) floorf((limit_value - counter_value.Value) /
296 count_inst.Amount) + 1;
297 break;
298 default:
299 return 0;
300 }
301
302 if (c->max_alu_insts > 0
303 && iterations > loop_max_possible_iterations(c, loop)) {
304 return 0;
305 }
306
307 DBG("Loop will have %d iterations.\n", iterations);
308
309 /* Prepare loop for unrolling */
310 rc_remove_instruction(loop->Cond);
311 rc_remove_instruction(loop->If);
312 rc_remove_instruction(loop->Brk);
313 rc_remove_instruction(loop->EndIf);
314
315 unroll_loop(c, loop, iterations);
316 loop->EndLoop = NULL;
317 return 1;
318 }
319
320 /**
321 * @param c
322 * @param loop
323 * @param inst A pointer to a BGNLOOP instruction.
324 * @return 1 if all of the members of loop where set.
325 * @return 0 if there was an error and some members of loop are still NULL.
326 */
327 static int build_loop_info(struct radeon_compiler * c, struct loop_info * loop,
328 struct rc_instruction * inst)
329 {
330 struct rc_instruction * ptr;
331
332 if(inst->U.I.Opcode != RC_OPCODE_BGNLOOP){
333 rc_error(c, "%s: expected BGNLOOP", __FUNCTION__);
334 return 0;
335 }
336
337 memset(loop, 0, sizeof(struct loop_info));
338
339 loop->BeginLoop = inst;
340
341 for(ptr = loop->BeginLoop->Next; !loop->EndLoop; ptr = ptr->Next) {
342
343 if (ptr == &c->Program.Instructions) {
344 rc_error(c, "%s: BGNLOOP without an ENDLOOOP.\n",
345 __FUNCTION__);
346 return 0;
347 }
348
349 switch(ptr->U.I.Opcode){
350 case RC_OPCODE_BGNLOOP:
351 {
352 /* Nested loop, skip ahead to the end. */
353 unsigned int loop_depth = 1;
354 for(ptr = ptr->Next; ptr != &c->Program.Instructions;
355 ptr = ptr->Next){
356 if (ptr->U.I.Opcode == RC_OPCODE_BGNLOOP) {
357 loop_depth++;
358 } else if (ptr->U.I.Opcode == RC_OPCODE_ENDLOOP) {
359 if (!--loop_depth) {
360 break;
361 }
362 }
363 }
364 if (ptr == &c->Program.Instructions) {
365 rc_error(c, "%s: BGNLOOP without an ENDLOOOP\n",
366 __FUNCTION__);
367 return 0;
368 }
369 break;
370 }
371 case RC_OPCODE_BRK:
372 if(ptr->Next->U.I.Opcode != RC_OPCODE_ENDIF
373 || ptr->Prev->U.I.Opcode != RC_OPCODE_IF
374 || loop->Brk){
375 continue;
376 }
377 loop->Brk = ptr;
378 loop->If = ptr->Prev;
379 loop->EndIf = ptr->Next;
380 switch(loop->If->Prev->U.I.Opcode){
381 case RC_OPCODE_SLT:
382 case RC_OPCODE_SGE:
383 case RC_OPCODE_SGT:
384 case RC_OPCODE_SLE:
385 case RC_OPCODE_SEQ:
386 case RC_OPCODE_SNE:
387 break;
388 default:
389 return 0;
390 }
391 loop->Cond = loop->If->Prev;
392 break;
393
394 case RC_OPCODE_ENDLOOP:
395 loop->EndLoop = ptr;
396 break;
397 }
398 }
399
400 if (loop->BeginLoop && loop->Brk && loop->If && loop->EndIf
401 && loop->Cond && loop->EndLoop) {
402 return 1;
403 }
404 return 0;
405 }
406
407 /**
408 * This function prepares a loop to be unrolled by converting it into an if
409 * statement. Here is an outline of the conversion process:
410 * BGNLOOP; -> BGNLOOP;
411 * <Additional conditional code> -> <Additional conditional code>
412 * SGE/SLT temp[0], temp[1], temp[2]; -> SLT/SGE temp[0], temp[1], temp[2];
413 * IF temp[0]; -> IF temp[0];
414 * BRK; ->
415 * ENDIF; -> <Loop Body>
416 * <Loop Body> -> ENDIF;
417 * ENDLOOP; -> ENDLOOP
418 *
419 * @param inst A pointer to a BGNLOOP instruction.
420 * @return 1 for success, 0 for failure
421 */
422 static int transform_loop(struct emulate_loop_state * s,
423 struct rc_instruction * inst)
424 {
425 struct loop_info * loop;
426
427 memory_pool_array_reserve(&s->C->Pool, struct loop_info,
428 s->Loops, s->LoopCount, s->LoopReserved, 1);
429
430 loop = &s->Loops[s->LoopCount++];
431
432 if (!build_loop_info(s->C, loop, inst)) {
433 rc_error(s->C, "Failed to build loop info\n");
434 return 0;
435 }
436
437 if(try_unroll_loop(s->C, loop)){
438 return 1;
439 }
440
441 /* Reverse the conditional instruction */
442 switch(loop->Cond->U.I.Opcode){
443 case RC_OPCODE_SGE:
444 loop->Cond->U.I.Opcode = RC_OPCODE_SLT;
445 break;
446 case RC_OPCODE_SLT:
447 loop->Cond->U.I.Opcode = RC_OPCODE_SGE;
448 break;
449 case RC_OPCODE_SLE:
450 loop->Cond->U.I.Opcode = RC_OPCODE_SGT;
451 break;
452 case RC_OPCODE_SGT:
453 loop->Cond->U.I.Opcode = RC_OPCODE_SLE;
454 break;
455 case RC_OPCODE_SEQ:
456 loop->Cond->U.I.Opcode = RC_OPCODE_SNE;
457 break;
458 case RC_OPCODE_SNE:
459 loop->Cond->U.I.Opcode = RC_OPCODE_SEQ;
460 break;
461 default:
462 rc_error(s->C, "loop->Cond is not a conditional.\n");
463 return 0;
464 }
465
466 /* Prepare the loop to be emulated */
467 rc_remove_instruction(loop->Brk);
468 rc_remove_instruction(loop->EndIf);
469 rc_insert_instruction(loop->EndLoop->Prev, loop->EndIf);
470 return 1;
471 }
472
473 void rc_transform_loops(struct radeon_compiler *c, void *user)
474 {
475 struct emulate_loop_state * s = &c->loop_state;
476 struct rc_instruction * ptr;
477
478 memset(s, 0, sizeof(struct emulate_loop_state));
479 s->C = c;
480 for(ptr = s->C->Program.Instructions.Next;
481 ptr != &s->C->Program.Instructions; ptr = ptr->Next) {
482 if(ptr->Type == RC_INSTRUCTION_NORMAL &&
483 ptr->U.I.Opcode == RC_OPCODE_BGNLOOP){
484 if (!transform_loop(s, ptr))
485 return;
486 }
487 }
488 }
489
490 void rc_unroll_loops(struct radeon_compiler *c, void *user)
491 {
492 struct rc_instruction * inst;
493 struct loop_info loop;
494
495 for(inst = c->Program.Instructions.Next;
496 inst != &c->Program.Instructions; inst = inst->Next) {
497
498 if (inst->U.I.Opcode == RC_OPCODE_BGNLOOP) {
499 if (build_loop_info(c, &loop, inst)) {
500 try_unroll_loop(c, &loop);
501 }
502 }
503 }
504 }
505
506 void rc_emulate_loops(struct radeon_compiler *c, void *user)
507 {
508 struct emulate_loop_state * s = &c->loop_state;
509 int i;
510 /* Iterate backwards of the list of loops so that loops that nested
511 * loops are unrolled first.
512 */
513 for( i = s->LoopCount - 1; i >= 0; i-- ){
514 if(!s->Loops[i].EndLoop){
515 continue;
516 }
517 unsigned int iterations = loop_max_possible_iterations(
518 s->C, &s->Loops[i]);
519 unroll_loop(s->C, &s->Loops[i], iterations);
520 }
521 }