2 * Copyright 2010 Tom Stellard <tstellar@gmail.com>
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:
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.
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.
32 #include "radeon_emulate_loops.h"
34 #include "radeon_compiler.h"
35 #include "radeon_dataflow.h"
39 #define DBG(...) do { if (VERBOSE) fprintf(stderr, __VA_ARGS__); } while(0)
42 struct radeon_compiler
* C
;
43 struct rc_src_register
* Src
;
49 struct radeon_compiler
* C
;
56 static float get_constant_value(struct radeon_compiler
* c
,
57 struct rc_src_register
* src
,
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");
66 if(GET_BIT(src
->Negate
, chan
)){
70 c
->Program
.Constants
.Constants
[src
->Index
].u
.Immediate
[swz
];
73 static int src_reg_is_immediate(struct rc_src_register
* src
,
74 struct radeon_compiler
* c
)
76 return src
->File
== RC_FILE_CONSTANT
&&
77 c
->Program
.Constants
.Constants
[src
->Index
].Type
==RC_CONSTANT_IMMEDIATE
;
80 static unsigned int loop_max_possible_iterations(struct radeon_compiler
*c
,
81 struct loop_info
* loop
, unsigned int prog_inst_limit
)
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 + ((prog_inst_limit
- total_i
) / loop_i
);
89 static void unroll_loop(struct radeon_compiler
* c
, struct loop_info
* loop
,
90 unsigned int iterations
)
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);
110 static void update_const_value(void * data
, struct rc_instruction
* inst
,
111 rc_register_file file
, unsigned int index
, unsigned int mask
)
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
)){
119 switch(inst
->U
.I
.Opcode
){
121 if(!src_reg_is_immediate(&inst
->U
.I
.SrcReg
[0], value
->C
)){
126 get_constant_value(value
->C
, &inst
->U
.I
.SrcReg
[0], 0);
131 static void get_incr_amount(void * data
, struct rc_instruction
* inst
,
132 rc_register_file file
, unsigned int index
, unsigned int mask
)
134 struct count_inst
* count_inst
= data
;
136 const struct rc_opcode_info
* opcode
;
139 if(file
!= RC_FILE_TEMPORARY
||
140 count_inst
->Index
!= index
||
141 (1 << GET_SWZ(count_inst
->Swz
,0) != mask
)){
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;
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
){
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
){
160 count_inst
->Unknown
= 1;
163 if(src_reg_is_immediate(&inst
->U
.I
.SrcReg
[amnt_src_index
],
165 amount
= get_constant_value(count_inst
->C
,
166 &inst
->U
.I
.SrcReg
[amnt_src_index
], 0);
169 count_inst
->Unknown
= 1 ;
172 switch(inst
->U
.I
.Opcode
){
174 count_inst
->Amount
+= amount
;
177 if(amnt_src_index
== 0){
178 count_inst
->Unknown
= 0;
181 count_inst
->Amount
-= amount
;
184 count_inst
->Unknown
= 1;
190 * If prog_inst_limit is -1, then all eligible loops will be unrolled regardless
191 * of how many iterations they have.
193 static int try_unroll_loop(struct radeon_compiler
* c
, struct loop_info
* loop
,
194 unsigned int prog_inst_limit
)
198 struct count_inst count_inst
;
200 struct rc_src_register
* counter
;
201 struct rc_src_register
* limit
;
202 struct const_value counter_value
;
203 struct rc_instruction
* inst
;
205 /* Find the counter and the upper limit */
207 if(src_reg_is_immediate(&loop
->Cond
->U
.I
.SrcReg
[0], c
)){
208 limit
= &loop
->Cond
->U
.I
.SrcReg
[0];
209 counter
= &loop
->Cond
->U
.I
.SrcReg
[1];
211 else if(src_reg_is_immediate(&loop
->Cond
->U
.I
.SrcReg
[1], c
)){
212 limit
= &loop
->Cond
->U
.I
.SrcReg
[1];
213 counter
= &loop
->Cond
->U
.I
.SrcReg
[0];
216 DBG("No constant limit.\n");
220 /* Find the initial value of the counter */
221 counter_value
.Src
= counter
;
222 counter_value
.Value
= 0.0f
;
223 counter_value
.HasValue
= 0;
225 for(inst
= c
->Program
.Instructions
.Next
; inst
!= loop
->BeginLoop
;
227 rc_for_all_writes_mask(inst
, update_const_value
, &counter_value
);
229 if(!counter_value
.HasValue
){
230 DBG("Initial counter value cannot be determined.\n");
233 DBG("Initial counter value is %f\n", counter_value
.Value
);
234 /* Determine how the counter is modified each loop */
236 count_inst
.Index
= counter
->Index
;
237 count_inst
.Swz
= counter
->Swizzle
;
238 count_inst
.Amount
= 0.0f
;
239 count_inst
.Unknown
= 0;
241 for(inst
= loop
->BeginLoop
->Next
; end_loops
> 0; inst
= inst
->Next
){
242 switch(inst
->U
.I
.Opcode
){
243 /* XXX In the future we might want to try to unroll nested
245 case RC_OPCODE_BGNLOOP
:
248 case RC_OPCODE_ENDLOOP
:
249 loop
->EndLoop
= inst
;
253 /* Don't unroll loops if it has a BRK instruction
254 * other one used when testing the main conditional
257 /* Make sure we haven't entered a nested loops. */
258 if(inst
!= loop
->Brk
&& end_loops
== 1) {
262 /* XXX Check if the counter is modified within an if statement.
267 rc_for_all_writes_mask(inst
, get_incr_amount
, &count_inst
);
268 if(count_inst
.Unknown
){
275 if(count_inst
.Amount
== 0.0f
){
278 DBG("Counter is increased by %f each iteration.\n", count_inst
.Amount
);
279 /* Calculate the number of iterations of this loop. Keeping this
280 * simple, since we only support increment and decrement loops.
282 limit_value
= get_constant_value(c
, limit
, 0);
283 DBG("Limit is %f.\n", limit_value
);
284 /* The iteration calculations are opposite of what you would expect.
285 * In a normal loop, if the condition is met, then loop continues, but
286 * with our loops, if the condition is met, the is exited. */
287 switch(loop
->Cond
->U
.I
.Opcode
){
290 iterations
= (int) ceilf((limit_value
- counter_value
.Value
) /
296 iterations
= (int) floorf((limit_value
- counter_value
.Value
) /
297 count_inst
.Amount
) + 1;
303 if (prog_inst_limit
> 0
304 && iterations
> loop_max_possible_iterations(c
, loop
,
309 DBG("Loop will have %d iterations.\n", iterations
);
311 /* Prepare loop for unrolling */
312 rc_remove_instruction(loop
->Cond
);
313 rc_remove_instruction(loop
->If
);
314 rc_remove_instruction(loop
->Brk
);
315 rc_remove_instruction(loop
->EndIf
);
317 unroll_loop(c
, loop
, iterations
);
318 loop
->EndLoop
= NULL
;
325 * @param inst A pointer to a BGNLOOP instruction.
326 * @return 1 if all of the members of loop where set.
327 * @return 0 if there was an error and some members of loop are still NULL.
329 static int build_loop_info(struct radeon_compiler
* c
, struct loop_info
* loop
,
330 struct rc_instruction
* inst
)
332 struct rc_instruction
* ptr
;
334 if(inst
->U
.I
.Opcode
!= RC_OPCODE_BGNLOOP
){
335 rc_error(c
, "%s: expected BGNLOOP", __FUNCTION__
);
339 memset(loop
, 0, sizeof(struct loop_info
));
341 loop
->BeginLoop
= inst
;
343 for(ptr
= loop
->BeginLoop
->Next
; !loop
->EndLoop
; ptr
= ptr
->Next
) {
345 if (ptr
== &c
->Program
.Instructions
) {
346 rc_error(c
, "%s: BGNLOOP without an ENDLOOOP.\n",
351 switch(ptr
->U
.I
.Opcode
){
352 case RC_OPCODE_BGNLOOP
:
354 /* Nested loop, skip ahead to the end. */
355 unsigned int loop_depth
= 1;
356 for(ptr
= ptr
->Next
; ptr
!= &c
->Program
.Instructions
;
358 if (ptr
->U
.I
.Opcode
== RC_OPCODE_BGNLOOP
) {
360 } else if (ptr
->U
.I
.Opcode
== RC_OPCODE_ENDLOOP
) {
366 if (ptr
== &c
->Program
.Instructions
) {
367 rc_error(c
, "%s: BGNLOOP without an ENDLOOOP\n",
374 if(ptr
->Next
->U
.I
.Opcode
!= RC_OPCODE_ENDIF
375 || ptr
->Prev
->U
.I
.Opcode
!= RC_OPCODE_IF
380 loop
->If
= ptr
->Prev
;
381 loop
->EndIf
= ptr
->Next
;
382 switch(loop
->If
->Prev
->U
.I
.Opcode
){
391 rc_error(c
, "%s: expected conditional",
395 loop
->Cond
= loop
->If
->Prev
;
398 case RC_OPCODE_ENDLOOP
:
404 if (loop
->BeginLoop
&& loop
->Brk
&& loop
->If
&& loop
->EndIf
405 && loop
->Cond
&& loop
->EndLoop
) {
412 * This function prepares a loop to be unrolled by converting it into an if
413 * statement. Here is an outline of the conversion process:
414 * BGNLOOP; -> BGNLOOP;
415 * <Additional conditional code> -> <Additional conditional code>
416 * SGE/SLT temp[0], temp[1], temp[2]; -> SLT/SGE temp[0], temp[1], temp[2];
417 * IF temp[0]; -> IF temp[0];
419 * ENDIF; -> <Loop Body>
420 * <Loop Body> -> ENDIF;
421 * ENDLOOP; -> ENDLOOP
423 * @param inst A pointer to a BGNLOOP instruction.
424 * @return 1 for success, 0 for failure
426 int transform_loop(struct emulate_loop_state
* s
, struct rc_instruction
* inst
)
428 struct loop_info
* loop
;
430 memory_pool_array_reserve(&s
->C
->Pool
, struct loop_info
,
431 s
->Loops
, s
->LoopCount
, s
->LoopReserved
, 1);
433 loop
= &s
->Loops
[s
->LoopCount
++];
435 if (!build_loop_info(s
->C
, loop
, inst
))
438 if(try_unroll_loop(s
->C
, loop
, -1)){
442 /* Reverse the conditional instruction */
443 switch(loop
->Cond
->U
.I
.Opcode
){
445 loop
->Cond
->U
.I
.Opcode
= RC_OPCODE_SLT
;
448 loop
->Cond
->U
.I
.Opcode
= RC_OPCODE_SGE
;
451 loop
->Cond
->U
.I
.Opcode
= RC_OPCODE_SGT
;
454 loop
->Cond
->U
.I
.Opcode
= RC_OPCODE_SLE
;
457 loop
->Cond
->U
.I
.Opcode
= RC_OPCODE_SNE
;
460 loop
->Cond
->U
.I
.Opcode
= RC_OPCODE_SEQ
;
463 rc_error(s
->C
, "loop->Cond is not a conditional.\n");
467 /* Prepare the loop to be emulated */
468 rc_remove_instruction(loop
->Brk
);
469 rc_remove_instruction(loop
->EndIf
);
470 rc_insert_instruction(loop
->EndLoop
->Prev
, loop
->EndIf
);
474 void rc_transform_loops(struct radeon_compiler
*c
,
475 struct emulate_loop_state
* s
)
477 struct rc_instruction
* ptr
;
479 memset(s
, 0, sizeof(struct emulate_loop_state
));
481 for(ptr
= s
->C
->Program
.Instructions
.Next
;
482 ptr
!= &s
->C
->Program
.Instructions
; ptr
= ptr
->Next
) {
483 if(ptr
->Type
== RC_INSTRUCTION_NORMAL
&&
484 ptr
->U
.I
.Opcode
== RC_OPCODE_BGNLOOP
){
485 if (!transform_loop(s
, ptr
))
491 void rc_unroll_loops(struct radeon_compiler
*c
, int prog_inst_limit
)
493 struct rc_instruction
* inst
;
494 struct loop_info loop
;
496 for(inst
= c
->Program
.Instructions
.Next
;
497 inst
!= &c
->Program
.Instructions
; inst
= inst
->Next
) {
499 if (inst
->U
.I
.Opcode
== RC_OPCODE_BGNLOOP
) {
500 if (build_loop_info(c
, &loop
, inst
)) {
501 try_unroll_loop(c
, &loop
, prog_inst_limit
);
507 void rc_emulate_loops(struct emulate_loop_state
*s
, int prog_inst_limit
)
510 /* Iterate backwards of the list of loops so that loops that nested
511 * loops are unrolled first.
513 for( i
= s
->LoopCount
- 1; i
>= 0; i
-- ){
514 if(!s
->Loops
[i
].EndLoop
){
517 unsigned int iterations
= loop_max_possible_iterations(
518 s
->C
, &s
->Loops
[i
], prog_inst_limit
);
519 unroll_loop(s
->C
, &s
->Loops
[i
], iterations
);