• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "radeon_dataflow.h"
34 
35 
36 /**
37  * Transform the given clause in the following way:
38  *  1. Replace it with an empty clause
39  *  2. For every instruction in the original clause, try the given
40  *     transformations in order.
41  *  3. If one of the transformations returns GL_TRUE, assume that it
42  *     has emitted the appropriate instruction(s) into the new clause;
43  *     otherwise, copy the instruction verbatim.
44  *
45  * \note The transformation is currently not recursive; in other words,
46  * instructions emitted by transformations are not transformed.
47  *
48  * \note The transform is called 'local' because it can only look at
49  * one instruction at a time.
50  */
rc_local_transform(struct radeon_compiler * c,void * user)51 void rc_local_transform(
52 	struct radeon_compiler * c,
53 	void *user)
54 {
55 	struct radeon_program_transformation *transformations =
56 		(struct radeon_program_transformation*)user;
57 	struct rc_instruction * inst = c->Program.Instructions.Next;
58 
59 	while(inst != &c->Program.Instructions) {
60 		struct rc_instruction * current = inst;
61 		int i;
62 
63 		inst = inst->Next;
64 
65 		for(i = 0; transformations[i].function; ++i) {
66 			struct radeon_program_transformation* t = transformations + i;
67 
68 			if (t->function(c, current, t->userData))
69 				break;
70 		}
71 	}
72 }
73 
rc_find_free_temporary(struct radeon_compiler * c)74 unsigned int rc_find_free_temporary(struct radeon_compiler * c)
75 {
76 	/* Find the largest used temp index when called for the first time. */
77 	if (c->max_temp_index == -1) {
78 		for (struct rc_instruction * inst = c->Program.Instructions.Next;
79 			inst != &c->Program.Instructions; inst = inst->Next) {
80 			const struct rc_opcode_info * opcode =
81 				rc_get_opcode_info(inst->U.I.Opcode);
82 			if (opcode->HasDstReg &&
83 				inst->U.I.DstReg.File == RC_FILE_TEMPORARY &&
84 				inst->U.I.WriteALUResult == RC_ALURESULT_NONE &&
85 				inst->U.I.DstReg.Index > c->max_temp_index)
86 				c->max_temp_index = inst->U.I.DstReg.Index;
87 		}
88 	}
89 
90 	c->max_temp_index++;
91 	if (c->max_temp_index > RC_REGISTER_MAX_INDEX) {
92 		rc_error(c, "Ran out of temporary registers\n");
93 		return 0;
94 	}
95 	return c->max_temp_index;
96 }
97 
98 
rc_alloc_instruction(struct radeon_compiler * c)99 struct rc_instruction *rc_alloc_instruction(struct radeon_compiler * c)
100 {
101 	struct rc_instruction * inst = memory_pool_malloc(&c->Pool, sizeof(struct rc_instruction));
102 
103 	memset(inst, 0, sizeof(struct rc_instruction));
104 
105 	inst->U.I.Opcode = RC_OPCODE_ILLEGAL_OPCODE;
106 	inst->U.I.DstReg.WriteMask = RC_MASK_XYZW;
107 	inst->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XYZW;
108 	inst->U.I.SrcReg[1].Swizzle = RC_SWIZZLE_XYZW;
109 	inst->U.I.SrcReg[2].Swizzle = RC_SWIZZLE_XYZW;
110 
111 	return inst;
112 }
113 
rc_insert_instruction(struct rc_instruction * after,struct rc_instruction * inst)114 void rc_insert_instruction(struct rc_instruction * after, struct rc_instruction * inst)
115 {
116 	inst->Prev = after;
117 	inst->Next = after->Next;
118 
119 	inst->Prev->Next = inst;
120 	inst->Next->Prev = inst;
121 }
122 
rc_insert_new_instruction(struct radeon_compiler * c,struct rc_instruction * after)123 struct rc_instruction *rc_insert_new_instruction(struct radeon_compiler * c, struct rc_instruction * after)
124 {
125 	struct rc_instruction * inst = rc_alloc_instruction(c);
126 
127 	rc_insert_instruction(after, inst);
128 
129 	return inst;
130 }
131 
rc_remove_instruction(struct rc_instruction * inst)132 void rc_remove_instruction(struct rc_instruction * inst)
133 {
134 	inst->Prev->Next = inst->Next;
135 	inst->Next->Prev = inst->Prev;
136 }
137 
138 /**
139  * Return the number of instructions in the program.
140  */
rc_recompute_ips(struct radeon_compiler * c)141 unsigned int rc_recompute_ips(struct radeon_compiler * c)
142 {
143 	unsigned int ip = 0;
144 	struct rc_instruction * inst;
145 
146 	for(inst = c->Program.Instructions.Next;
147 	    inst != &c->Program.Instructions;
148 	    inst = inst->Next) {
149 		inst->IP = ip++;
150 	}
151 
152 	c->Program.Instructions.IP = 0xcafedead;
153 
154 	return ip;
155 }
156