• 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 "radeon_compiler_util.h"
33 
34 #include "radeon_compiler.h"
35 #include "radeon_dataflow.h"
36 /**
37  */
rc_swizzle_to_writemask(unsigned int swz)38 unsigned int rc_swizzle_to_writemask(unsigned int swz)
39 {
40 	unsigned int mask = 0;
41 	unsigned int i;
42 
43 	for(i = 0; i < 4; i++) {
44 		mask |= 1 << GET_SWZ(swz, i);
45 	}
46 	mask &= RC_MASK_XYZW;
47 
48 	return mask;
49 }
50 
get_swz(unsigned int swz,rc_swizzle idx)51 rc_swizzle get_swz(unsigned int swz, rc_swizzle idx)
52 {
53 	if (idx & 0x4)
54 		return idx;
55 	return GET_SWZ(swz, idx);
56 }
57 
58 /**
59  * The purpose of this function is to standardize the number channels used by
60  * swizzles.  All swizzles regardless of what instruction they are a part of
61  * should have 4 channels initialized with values.
62  * @param channels The number of channels in initial_value that have a
63  * meaningful value.
64  * @return An initialized swizzle that has all of the unused channels set to
65  * RC_SWIZZLE_UNUSED.
66  */
rc_init_swizzle(unsigned int initial_value,unsigned int channels)67 unsigned int rc_init_swizzle(unsigned int initial_value, unsigned int channels)
68 {
69 	unsigned int i;
70 	for (i = channels; i < 4; i++) {
71 		SET_SWZ(initial_value, i, RC_SWIZZLE_UNUSED);
72 	}
73 	return initial_value;
74 }
75 
combine_swizzles4(unsigned int src,rc_swizzle swz_x,rc_swizzle swz_y,rc_swizzle swz_z,rc_swizzle swz_w)76 unsigned int combine_swizzles4(unsigned int src,
77 		rc_swizzle swz_x, rc_swizzle swz_y, rc_swizzle swz_z, rc_swizzle swz_w)
78 {
79 	unsigned int ret = 0;
80 
81 	ret |= get_swz(src, swz_x);
82 	ret |= get_swz(src, swz_y) << 3;
83 	ret |= get_swz(src, swz_z) << 6;
84 	ret |= get_swz(src, swz_w) << 9;
85 
86 	return ret;
87 }
88 
combine_swizzles(unsigned int src,unsigned int swz)89 unsigned int combine_swizzles(unsigned int src, unsigned int swz)
90 {
91 	unsigned int ret = 0;
92 
93 	ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_X));
94 	ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_Y)) << 3;
95 	ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_Z)) << 6;
96 	ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_W)) << 9;
97 
98 	return ret;
99 }
100 
101 /**
102  * @param mask Must be either RC_MASK_X, RC_MASK_Y, RC_MASK_Z, or RC_MASK_W
103  */
rc_mask_to_swizzle(unsigned int mask)104 rc_swizzle rc_mask_to_swizzle(unsigned int mask)
105 {
106 	switch (mask) {
107 	case RC_MASK_X: return RC_SWIZZLE_X;
108 	case RC_MASK_Y: return RC_SWIZZLE_Y;
109 	case RC_MASK_Z: return RC_SWIZZLE_Z;
110 	case RC_MASK_W: return RC_SWIZZLE_W;
111 	}
112 	return RC_SWIZZLE_UNUSED;
113 }
114 
115 /* Reorder mask bits according to swizzle. */
swizzle_mask(unsigned swizzle,unsigned mask)116 unsigned swizzle_mask(unsigned swizzle, unsigned mask)
117 {
118 	unsigned ret = 0;
119 	for (unsigned chan = 0; chan < 4; ++chan) {
120 		unsigned swz = GET_SWZ(swizzle, chan);
121 		if (swz < 4)
122 			ret |= GET_BIT(mask, swz) << chan;
123 	}
124 	return ret;
125 }
126 
srcs_need_rewrite(const struct rc_opcode_info * info)127 static unsigned int srcs_need_rewrite(const struct rc_opcode_info * info)
128 {
129 	if (info->HasTexture) {
130 		return 0;
131 	}
132 	switch (info->Opcode) {
133 		case RC_OPCODE_DP2:
134 		case RC_OPCODE_DP3:
135 		case RC_OPCODE_DP4:
136 		case RC_OPCODE_DDX:
137 		case RC_OPCODE_DDY:
138 			return 0;
139 		default:
140 			return 1;
141 	}
142 }
143 
144 /**
145  * This function moves the old swizzles to new channels using the values
146  * in the conversion swizzle. For example if the instruction writemask is
147  * changed from x to y, then conversion_swizzle should be y___ and this
148  * function will adjust the old argument swizzles (of the same instruction)
149  * to the new channels, so x___ will become _x__, etc...
150  *
151  * @param old_swizzle The swizzle to change
152  * @param conversion_swizzle Describes the conversion to perform on the swizzle
153  * @return A new swizzle
154  */
rc_adjust_channels(unsigned int old_swizzle,unsigned int conversion_swizzle)155 unsigned int rc_adjust_channels(
156 	unsigned int old_swizzle,
157 	unsigned int conversion_swizzle)
158 {
159 	unsigned int i;
160 	unsigned int new_swizzle = rc_init_swizzle(RC_SWIZZLE_UNUSED, 0);
161 	for (i = 0; i < 4; i++) {
162 		unsigned int new_chan = get_swz(conversion_swizzle, i);
163 		if (new_chan == RC_SWIZZLE_UNUSED) {
164 			continue;
165 		}
166 		SET_SWZ(new_swizzle, new_chan, GET_SWZ(old_swizzle, i));
167 	}
168 	return new_swizzle;
169 }
170 
rewrite_writemask(unsigned int old_mask,unsigned int conversion_swizzle)171 static unsigned int rewrite_writemask(
172 	unsigned int old_mask,
173 	unsigned int conversion_swizzle)
174 {
175 	unsigned int new_mask = 0;
176 	unsigned int i;
177 
178 	for (i = 0; i < 4; i++) {
179 		if (!GET_BIT(old_mask, i)
180 		   || GET_SWZ(conversion_swizzle, i) == RC_SWIZZLE_UNUSED) {
181 			continue;
182 		}
183 		new_mask |= (1 << GET_SWZ(conversion_swizzle, i));
184 	}
185 
186 	return new_mask;
187 }
188 
189 /**
190  * This function rewrites the writemask of sub and adjusts the swizzles
191  * of all its source registers based on the conversion_swizzle.
192  * conversion_swizzle represents a mapping of the old writemask to the
193  * new writemask.  For a detailed description of how conversion swizzles
194  * work see rc_rewrite_swizzle().
195  */
rc_pair_rewrite_writemask(struct rc_pair_sub_instruction * sub,unsigned int conversion_swizzle)196 void rc_pair_rewrite_writemask(
197 	struct rc_pair_sub_instruction * sub,
198 	unsigned int conversion_swizzle)
199 {
200 	const struct rc_opcode_info * info = rc_get_opcode_info(sub->Opcode);
201 	unsigned int i;
202 
203 	sub->WriteMask = rewrite_writemask(sub->WriteMask, conversion_swizzle);
204 
205 	if (!srcs_need_rewrite(info)) {
206 		return ;
207 	}
208 
209 	for (i = 0; i < info->NumSrcRegs; i++) {
210 		sub->Arg[i].Swizzle =
211 			rc_adjust_channels(sub->Arg[i].Swizzle,
212 						conversion_swizzle);
213 	}
214 }
215 
normal_rewrite_writemask_cb(void * userdata,struct rc_instruction * inst,struct rc_src_register * src)216 static void normal_rewrite_writemask_cb(
217 	void * userdata,
218 	struct rc_instruction * inst,
219 	struct rc_src_register * src)
220 {
221 	unsigned int * conversion_swizzle = (unsigned int *)userdata;
222 	src->Swizzle = rc_adjust_channels(src->Swizzle, *conversion_swizzle);
223 }
224 
225 /**
226  * This function is the same as rc_pair_rewrite_writemask() except it
227  * operates on normal instructions.
228  */
rc_normal_rewrite_writemask(struct rc_instruction * inst,unsigned int conversion_swizzle)229 void rc_normal_rewrite_writemask(
230 	struct rc_instruction * inst,
231 	unsigned int conversion_swizzle)
232 {
233 	struct rc_sub_instruction * sub = &inst->U.I;
234 	const struct rc_opcode_info * info = rc_get_opcode_info(sub->Opcode);
235 	sub->DstReg.WriteMask =
236 		rewrite_writemask(sub->DstReg.WriteMask, conversion_swizzle);
237 
238 	if (info->HasTexture) {
239 		unsigned int i;
240 		assert(sub->TexSwizzle == RC_SWIZZLE_XYZW);
241 		for (i = 0; i < 4; i++) {
242 			unsigned int swz = GET_SWZ(conversion_swizzle, i);
243 			if (swz > 3)
244 				continue;
245 			SET_SWZ(sub->TexSwizzle, swz, i);
246 		}
247 	}
248 
249 	if (!srcs_need_rewrite(info)) {
250 		return;
251 	}
252 
253 	rc_for_all_reads_src(inst, normal_rewrite_writemask_cb,
254 							&conversion_swizzle);
255 }
256 
257 /**
258  * This function replaces each value 'swz' in swizzle with the value of
259  * GET_SWZ(conversion_swizzle, swz).  So, if you want to change all the X's
260  * in swizzle to Y, then conversion_swizzle should be Y___ (0xff9).  If you want
261  * to change all the Y's in swizzle to X, then conversion_swizzle should be
262  * _X__ (0xfc7).  If you want to change the Y's to X and the X's to Y, then
263  * conversion swizzle should be YX__ (0xfc1).
264  * @param swizzle The swizzle to change
265  * @param conversion_swizzle Describes the conversion to perform on the swizzle
266  * @return A converted swizzle
267  */
rc_rewrite_swizzle(unsigned int swizzle,unsigned int conversion_swizzle)268 unsigned int rc_rewrite_swizzle(
269 	unsigned int swizzle,
270 	unsigned int conversion_swizzle)
271 {
272 	unsigned int chan;
273 	unsigned int out_swizzle = swizzle;
274 
275 	for (chan = 0; chan < 4; chan++) {
276 		unsigned int swz = GET_SWZ(swizzle, chan);
277 		unsigned int new_swz;
278 		if (swz > 3) {
279 			SET_SWZ(out_swizzle, chan, swz);
280 		} else {
281 			new_swz = GET_SWZ(conversion_swizzle, swz);
282 			if (new_swz != RC_SWIZZLE_UNUSED) {
283 				SET_SWZ(out_swizzle, chan, new_swz);
284 			} else {
285 				SET_SWZ(out_swizzle, chan, swz);
286 			}
287 		}
288 	}
289 	return out_swizzle;
290 }
291 
292 /**
293  * Left multiplication of a register with a swizzle
294  */
lmul_swizzle(unsigned int swizzle,struct rc_src_register srcreg)295 struct rc_src_register lmul_swizzle(unsigned int swizzle, struct rc_src_register srcreg)
296 {
297 	struct rc_src_register tmp = srcreg;
298 	int i;
299 	tmp.Swizzle = 0;
300 	tmp.Negate = 0;
301 	for(i = 0; i < 4; ++i) {
302 		rc_swizzle swz = GET_SWZ(swizzle, i);
303 		if (swz < 4) {
304 			tmp.Swizzle |= GET_SWZ(srcreg.Swizzle, swz) << (i*3);
305 			tmp.Negate |= GET_BIT(srcreg.Negate, swz) << i;
306 		} else {
307 			tmp.Swizzle |= swz << (i*3);
308 		}
309 	}
310 	return tmp;
311 }
312 
reset_srcreg(struct rc_src_register * reg)313 void reset_srcreg(struct rc_src_register* reg)
314 {
315 	memset(reg, 0, sizeof(struct rc_src_register));
316 	reg->Swizzle = RC_SWIZZLE_XYZW;
317 }
318 
rc_src_reads_dst_mask(rc_register_file src_file,unsigned int src_idx,unsigned int src_swz,rc_register_file dst_file,unsigned int dst_idx,unsigned int dst_mask)319 unsigned int rc_src_reads_dst_mask(
320 		rc_register_file src_file,
321 		unsigned int src_idx,
322 		unsigned int src_swz,
323 		rc_register_file dst_file,
324 		unsigned int dst_idx,
325 		unsigned int dst_mask)
326 {
327 	if (src_file != dst_file || src_idx != dst_idx) {
328 		return RC_MASK_NONE;
329 	}
330 	return dst_mask & rc_swizzle_to_writemask(src_swz);
331 }
332 
333 /**
334  * @return A bit mask specifying whether this swizzle will select from an RGB
335  * source, an Alpha source, or both.
336  */
rc_source_type_swz(unsigned int swizzle)337 unsigned int rc_source_type_swz(unsigned int swizzle)
338 {
339 	unsigned int chan;
340 	unsigned int swz = RC_SWIZZLE_UNUSED;
341 	unsigned int ret = RC_SOURCE_NONE;
342 
343 	for(chan = 0; chan < 4; chan++) {
344 		swz = GET_SWZ(swizzle, chan);
345 		if (swz == RC_SWIZZLE_W) {
346 			ret |= RC_SOURCE_ALPHA;
347 		} else if (swz == RC_SWIZZLE_X || swz == RC_SWIZZLE_Y
348 						|| swz == RC_SWIZZLE_Z) {
349 			ret |= RC_SOURCE_RGB;
350 		}
351 	}
352 	return ret;
353 }
354 
rc_source_type_mask(unsigned int mask)355 unsigned int rc_source_type_mask(unsigned int mask)
356 {
357 	unsigned int ret = RC_SOURCE_NONE;
358 
359 	if (mask & RC_MASK_XYZ)
360 		ret |= RC_SOURCE_RGB;
361 
362 	if (mask & RC_MASK_W)
363 		ret |= RC_SOURCE_ALPHA;
364 
365 	return ret;
366 }
367 
368 struct src_select {
369 	rc_register_file File;
370 	int Index;
371 	unsigned int SrcType;
372 };
373 
374 struct can_use_presub_data {
375 	struct src_select Selects[5];
376 	unsigned int SelectCount;
377 	const struct rc_src_register * ReplaceReg;
378 	unsigned int ReplaceRemoved;
379 };
380 
can_use_presub_data_add_select(struct can_use_presub_data * data,rc_register_file file,unsigned int index,unsigned int src_type)381 static void can_use_presub_data_add_select(
382 	struct can_use_presub_data * data,
383 	rc_register_file file,
384 	unsigned int index,
385 	unsigned int src_type)
386 {
387 	struct src_select * select;
388 
389 	select = &data->Selects[data->SelectCount++];
390 	select->File = file;
391 	select->Index = index;
392 	select->SrcType = src_type;
393 }
394 
395 /**
396  * This callback function counts the number of sources in inst that are
397  * different from the sources in can_use_presub_data->RemoveSrcs.
398  */
can_use_presub_read_cb(void * userdata,struct rc_instruction * inst,struct rc_src_register * src)399 static void can_use_presub_read_cb(
400 	void * userdata,
401 	struct rc_instruction * inst,
402 	struct rc_src_register * src)
403 {
404 	struct can_use_presub_data * d = userdata;
405 
406 	if (!d->ReplaceRemoved && src == d->ReplaceReg) {
407 		d->ReplaceRemoved = 1;
408 		return;
409 	}
410 
411 	if (src->File == RC_FILE_NONE)
412 		return;
413 
414 	can_use_presub_data_add_select(d, src->File, src->Index,
415 					rc_source_type_swz(src->Swizzle));
416 }
417 
rc_inst_can_use_presub(struct rc_instruction * inst,rc_presubtract_op presub_op,unsigned int presub_writemask,const struct rc_src_register * replace_reg,const struct rc_src_register * presub_src0,const struct rc_src_register * presub_src1)418 unsigned int rc_inst_can_use_presub(
419 	struct rc_instruction * inst,
420 	rc_presubtract_op presub_op,
421 	unsigned int presub_writemask,
422 	const struct rc_src_register * replace_reg,
423 	const struct rc_src_register * presub_src0,
424 	const struct rc_src_register * presub_src1)
425 {
426 	struct can_use_presub_data d;
427 	unsigned int num_presub_srcs;
428 	unsigned int i;
429 	const struct rc_opcode_info * info =
430 					rc_get_opcode_info(inst->U.I.Opcode);
431 	int rgb_count = 0, alpha_count = 0;
432 	unsigned int src_type0, src_type1;
433 
434 	if (presub_op == RC_PRESUB_NONE) {
435 		return 1;
436 	}
437 
438 	if (info->HasTexture) {
439 		return 0;
440 	}
441 
442 	/* We can't use more than one presubtract value in an
443 	 * instruction, unless the two prsubtract operations
444 	 * are the same and read from the same registers.
445 	 * XXX For now we will limit instructions to only one presubtract
446 	 * value.*/
447 	if (inst->U.I.PreSub.Opcode != RC_PRESUB_NONE) {
448 		return 0;
449 	}
450 
451 	memset(&d, 0, sizeof(d));
452 	d.ReplaceReg = replace_reg;
453 
454 	rc_for_all_reads_src(inst, can_use_presub_read_cb, &d);
455 
456 	num_presub_srcs = rc_presubtract_src_reg_count(presub_op);
457 
458 	src_type0 = rc_source_type_swz(presub_src0->Swizzle);
459 	can_use_presub_data_add_select(&d,
460 		presub_src0->File,
461 		presub_src0->Index,
462 		src_type0);
463 
464 	if (num_presub_srcs > 1) {
465 		src_type1 = rc_source_type_swz(presub_src1->Swizzle);
466 		can_use_presub_data_add_select(&d,
467 			presub_src1->File,
468 			presub_src1->Index,
469 			src_type1);
470 
471 		/* Even if both of the presub sources read from the same
472 		 * register, we still need to use 2 different source selects
473 		 * for them, so we need to increment the count to compensate.
474 		 */
475 		if (presub_src0->File == presub_src1->File
476 		    && presub_src0->Index == presub_src1->Index) {
477 			if (src_type0 & src_type1 & RC_SOURCE_RGB) {
478 				rgb_count++;
479 			}
480 			if (src_type0 & src_type1 & RC_SOURCE_ALPHA) {
481 				alpha_count++;
482 			}
483 		}
484 	}
485 
486 	/* Count the number of source selects for Alpha and RGB.  If we
487 	 * encounter two of the same source selects then we can ignore the
488 	 * first one. */
489 	for (i = 0; i < d.SelectCount; i++) {
490 		unsigned int j;
491 		unsigned int src_type = d.Selects[i].SrcType;
492 		for (j = i + 1; j < d.SelectCount; j++) {
493 			if (d.Selects[i].File == d.Selects[j].File
494 			    && d.Selects[i].Index == d.Selects[j].Index) {
495 				src_type &= ~d.Selects[j].SrcType;
496 			}
497 		}
498 		if (src_type & RC_SOURCE_RGB) {
499 			rgb_count++;
500 		}
501 
502 		if (src_type & RC_SOURCE_ALPHA) {
503 			alpha_count++;
504 		}
505 	}
506 
507 	if (rgb_count > 3 || alpha_count > 3) {
508 		return 0;
509 	}
510 
511 	return 1;
512 }
513 
514 struct max_data {
515 	unsigned int Max;
516 	unsigned int HasFileType;
517 	rc_register_file File;
518 };
519 
max_callback(void * userdata,struct rc_instruction * inst,rc_register_file file,unsigned int index,unsigned int mask)520 static void max_callback(
521 	void * userdata,
522 	struct rc_instruction * inst,
523 	rc_register_file file,
524 	unsigned int index,
525 	unsigned int mask)
526 {
527 	struct max_data * d = (struct max_data*)userdata;
528 	if (file == d->File && (!d->HasFileType || index > d->Max)) {
529 		d->Max = index;
530 		d->HasFileType = 1;
531 	}
532 }
533 
534 /**
535  * @return The maximum index of the specified register file used by the
536  * program.
537  */
rc_get_max_index(struct radeon_compiler * c,rc_register_file file)538 int rc_get_max_index(
539 	struct radeon_compiler * c,
540 	rc_register_file file)
541 {
542 	struct max_data data;
543 	struct rc_instruction * inst;
544 	data.Max = 0;
545 	data.HasFileType = 0;
546 	data.File = file;
547 	for (inst = c->Program.Instructions.Next;
548 					inst != &c->Program.Instructions;
549 					inst = inst->Next) {
550 		rc_for_all_reads_mask(inst, max_callback, &data);
551 		rc_for_all_writes_mask(inst, max_callback, &data);
552 	}
553 	if (!data.HasFileType) {
554 		return -1;
555 	} else {
556 		return data.Max;
557 	}
558 }
559 
560 /**
561  * This function removes a source from a pair instructions.
562  * @param inst
563  * @param src_type RC_SOURCE_RGB, RC_SOURCE_ALPHA, or both bitwise or'd
564  * @param source The index of the source to remove
565 
566  */
rc_pair_remove_src(struct rc_instruction * inst,unsigned int src_type,unsigned int source)567 void rc_pair_remove_src(
568 	struct rc_instruction * inst,
569 	unsigned int src_type,
570 	unsigned int source)
571 {
572 	if (src_type & RC_SOURCE_RGB) {
573 		memset(&inst->U.P.RGB.Src[source], 0,
574 			sizeof(struct rc_pair_instruction_source));
575 	}
576 
577 	if (src_type & RC_SOURCE_ALPHA) {
578 		memset(&inst->U.P.Alpha.Src[source], 0,
579 			sizeof(struct rc_pair_instruction_source));
580 	}
581 }
582 
583 /**
584  * @return RC_OPCODE_NOOP if inst is not a flow control instruction.
585  * @return The opcode of inst if it is a flow control instruction.
586  */
rc_get_flow_control_inst(struct rc_instruction * inst)587 rc_opcode rc_get_flow_control_inst(struct rc_instruction * inst)
588 {
589 	const struct rc_opcode_info * info;
590 	if (inst->Type == RC_INSTRUCTION_NORMAL) {
591 		info = rc_get_opcode_info(inst->U.I.Opcode);
592 	} else {
593 		info = rc_get_opcode_info(inst->U.P.RGB.Opcode);
594 		/*A flow control instruction shouldn't have an alpha
595 		 * instruction.*/
596 		assert(!info->IsFlowControl ||
597 				inst->U.P.Alpha.Opcode == RC_OPCODE_NOP);
598 	}
599 
600 	if (info->IsFlowControl)
601 		return info->Opcode;
602 	else
603 		return RC_OPCODE_NOP;
604 
605 }
606 
607 /**
608  * @return The BGNLOOP instruction that starts the loop ended by endloop.
609  */
rc_match_endloop(struct rc_instruction * endloop)610 struct rc_instruction * rc_match_endloop(struct rc_instruction * endloop)
611 {
612 	unsigned int endloop_count = 0;
613 	struct rc_instruction * inst;
614 	for (inst = endloop->Prev; inst != endloop; inst = inst->Prev) {
615 		rc_opcode op = rc_get_flow_control_inst(inst);
616 		if (op == RC_OPCODE_ENDLOOP) {
617 			endloop_count++;
618 		} else if (op == RC_OPCODE_BGNLOOP) {
619 			if (endloop_count == 0) {
620 				return inst;
621 			} else {
622 				endloop_count--;
623 			}
624 		}
625 	}
626 	return NULL;
627 }
628 
629 /**
630  * @return The ENDLOOP instruction that ends the loop started by bgnloop.
631  */
rc_match_bgnloop(struct rc_instruction * bgnloop)632 struct rc_instruction * rc_match_bgnloop(struct rc_instruction * bgnloop)
633 {
634 	unsigned int bgnloop_count = 0;
635 	struct rc_instruction * inst;
636 	for (inst = bgnloop->Next; inst!=bgnloop; inst = inst->Next) {
637 		rc_opcode op = rc_get_flow_control_inst(inst);
638 		if (op == RC_OPCODE_BGNLOOP) {
639 			bgnloop_count++;
640 		} else if (op == RC_OPCODE_ENDLOOP) {
641 			if (bgnloop_count == 0) {
642 				return inst;
643 			} else {
644 				bgnloop_count--;
645 			}
646 		}
647 	}
648 	return NULL;
649 }
650 
651 /**
652  * @return A conversion swizzle for converting from old_mask->new_mask
653  */
rc_make_conversion_swizzle(unsigned int old_mask,unsigned int new_mask)654 unsigned int rc_make_conversion_swizzle(
655 	unsigned int old_mask,
656 	unsigned int new_mask)
657 {
658 	unsigned int conversion_swizzle = rc_init_swizzle(RC_SWIZZLE_UNUSED, 0);
659 	unsigned int old_idx;
660 	unsigned int new_idx = 0;
661 	for (old_idx = 0; old_idx < 4; old_idx++) {
662 		if (!GET_BIT(old_mask, old_idx))
663 			continue;
664 		for ( ; new_idx < 4; new_idx++) {
665 			if (GET_BIT(new_mask, new_idx)) {
666 				SET_SWZ(conversion_swizzle, old_idx, new_idx);
667 				new_idx++;
668 				break;
669 			}
670 		}
671 	}
672 	return conversion_swizzle;
673 }
674 
675 /**
676  * @return 1 if the register contains an immediate value, 0 otherwise.
677  */
rc_src_reg_is_immediate(struct radeon_compiler * c,unsigned int file,unsigned int index)678 unsigned int rc_src_reg_is_immediate(
679 	struct radeon_compiler * c,
680 	unsigned int file,
681 	unsigned int index)
682 {
683 	return file == RC_FILE_CONSTANT &&
684 	c->Program.Constants.Constants[index].Type == RC_CONSTANT_IMMEDIATE;
685 }
686 
687 /**
688  * @return The immediate value in the specified register.
689  */
rc_get_constant_value(struct radeon_compiler * c,unsigned int index,unsigned int swizzle,unsigned int negate,unsigned int chan)690 float rc_get_constant_value(
691 	struct radeon_compiler * c,
692 	unsigned int index,
693 	unsigned int swizzle,
694 	unsigned int negate,
695 	unsigned int chan)
696 {
697 	float base = 1.0f;
698 	int swz = GET_SWZ(swizzle, chan);
699 	if(swz >= 4 || index >= c->Program.Constants.Count ){
700 		rc_error(c, "get_constant_value: Can't find a value.\n");
701 		return 0.0f;
702 	}
703 	if(GET_BIT(negate, chan)){
704 		base = -1.0f;
705 	}
706 	return base *
707 		c->Program.Constants.Constants[index].u.Immediate[swz];
708 }
709 
710 /**
711  * This function returns the component value (RC_SWIZZLE_*) of the first used
712  * channel in the swizzle.  This is only useful for scalar instructions that are
713  * known to use only one channel of the swizzle.
714  */
rc_get_scalar_src_swz(unsigned int swizzle)715 unsigned int rc_get_scalar_src_swz(unsigned int swizzle)
716 {
717 	unsigned int swz, chan;
718 	for (chan = 0; chan < 4; chan++) {
719 		swz = GET_SWZ(swizzle, chan);
720 		if (swz != RC_SWIZZLE_UNUSED) {
721 			break;
722 		}
723 	}
724 	assert(swz != RC_SWIZZLE_UNUSED);
725 	return swz;
726 }
727