1/*
2 * Copyright 2011 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#include "radeon_compiler.h"
29#include "radeon_compiler_util.h"
30#include "radeon_opcodes.h"
31#include "radeon_program_pair.h"
32
33static void mark_used_presub(struct rc_pair_sub_instruction * sub)
34{
35	if (sub->Src[RC_PAIR_PRESUB_SRC].Used) {
36		unsigned int presub_reg_count = rc_presubtract_src_reg_count(
37					sub->Src[RC_PAIR_PRESUB_SRC].Index);
38		unsigned int i;
39		for (i = 0; i < presub_reg_count; i++) {
40			sub->Src[i].Used = 1;
41		}
42	}
43}
44
45static void mark_used(
46	struct rc_instruction * inst,
47	struct rc_pair_sub_instruction * sub)
48{
49	unsigned int i;
50	const struct rc_opcode_info * info = rc_get_opcode_info(sub->Opcode);
51	for (i = 0; i < info->NumSrcRegs; i++) {
52		unsigned int src_type = rc_source_type_swz(sub->Arg[i].Swizzle);
53		if (src_type & RC_SOURCE_RGB) {
54			inst->U.P.RGB.Src[sub->Arg[i].Source].Used = 1;
55		}
56
57		if (src_type & RC_SOURCE_ALPHA) {
58			inst->U.P.Alpha.Src[sub->Arg[i].Source].Used = 1;
59		}
60	}
61}
62
63/**
64 * This pass finds sources that are not used by their instruction and marks
65 * them as unused.
66 */
67void rc_pair_remove_dead_sources(struct radeon_compiler * c, void *user)
68{
69	struct rc_instruction * inst;
70	for (inst = c->Program.Instructions.Next;
71					inst != &c->Program.Instructions;
72					inst = inst->Next) {
73		unsigned int i;
74		if (inst->Type == RC_INSTRUCTION_NORMAL)
75			continue;
76
77		/* Mark all sources as unused */
78		for (i = 0; i < 4; i++) {
79			inst->U.P.RGB.Src[i].Used = 0;
80			inst->U.P.Alpha.Src[i].Used = 0;
81		}
82		mark_used(inst, &inst->U.P.RGB);
83		mark_used(inst, &inst->U.P.Alpha);
84
85		mark_used_presub(&inst->U.P.RGB);
86		mark_used_presub(&inst->U.P.Alpha);
87	}
88}
89