1 /*
2 * Copyright 2008 Nicolai Haehnle.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "radeon_program.h"
7
8 #include <stdio.h>
9
10 #include "radeon_compiler.h"
11 #include "radeon_dataflow.h"
12
13
14 /**
15 * Transform the given clause in the following way:
16 * 1. Replace it with an empty clause
17 * 2. For every instruction in the original clause, try the given
18 * transformations in order.
19 * 3. If one of the transformations returns GL_TRUE, assume that it
20 * has emitted the appropriate instruction(s) into the new clause;
21 * otherwise, copy the instruction verbatim.
22 *
23 * \note The transformation is currently not recursive; in other words,
24 * instructions emitted by transformations are not transformed.
25 *
26 * \note The transform is called 'local' because it can only look at
27 * one instruction at a time.
28 */
rc_local_transform(struct radeon_compiler * c,void * user)29 void rc_local_transform(
30 struct radeon_compiler * c,
31 void *user)
32 {
33 struct radeon_program_transformation *transformations =
34 (struct radeon_program_transformation*)user;
35 struct rc_instruction * inst = c->Program.Instructions.Next;
36
37 while(inst != &c->Program.Instructions) {
38 struct rc_instruction * current = inst;
39 int i;
40
41 inst = inst->Next;
42
43 for(i = 0; transformations[i].function; ++i) {
44 struct radeon_program_transformation* t = transformations + i;
45
46 if (t->function(c, current, t->userData))
47 break;
48 }
49 }
50 }
51
rc_find_free_temporary(struct radeon_compiler * c)52 unsigned int rc_find_free_temporary(struct radeon_compiler * c)
53 {
54 /* Find the largest used temp index when called for the first time. */
55 if (c->max_temp_index == -1) {
56 for (struct rc_instruction * inst = c->Program.Instructions.Next;
57 inst != &c->Program.Instructions; inst = inst->Next) {
58 const struct rc_opcode_info * opcode =
59 rc_get_opcode_info(inst->U.I.Opcode);
60 if (opcode->HasDstReg &&
61 inst->U.I.DstReg.File == RC_FILE_TEMPORARY &&
62 inst->U.I.WriteALUResult == RC_ALURESULT_NONE &&
63 inst->U.I.DstReg.Index > c->max_temp_index)
64 c->max_temp_index = inst->U.I.DstReg.Index;
65 }
66 }
67
68 c->max_temp_index++;
69 if (c->max_temp_index > RC_REGISTER_MAX_INDEX) {
70 rc_error(c, "Ran out of temporary registers\n");
71 return 0;
72 }
73 return c->max_temp_index;
74 }
75
76
rc_alloc_instruction(struct radeon_compiler * c)77 struct rc_instruction *rc_alloc_instruction(struct radeon_compiler * c)
78 {
79 struct rc_instruction * inst = memory_pool_malloc(&c->Pool, sizeof(struct rc_instruction));
80
81 memset(inst, 0, sizeof(struct rc_instruction));
82
83 inst->U.I.Opcode = RC_OPCODE_ILLEGAL_OPCODE;
84 inst->U.I.DstReg.WriteMask = RC_MASK_XYZW;
85 inst->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XYZW;
86 inst->U.I.SrcReg[1].Swizzle = RC_SWIZZLE_XYZW;
87 inst->U.I.SrcReg[2].Swizzle = RC_SWIZZLE_XYZW;
88
89 return inst;
90 }
91
rc_insert_instruction(struct rc_instruction * after,struct rc_instruction * inst)92 void rc_insert_instruction(struct rc_instruction * after, struct rc_instruction * inst)
93 {
94 inst->Prev = after;
95 inst->Next = after->Next;
96
97 inst->Prev->Next = inst;
98 inst->Next->Prev = inst;
99 }
100
rc_insert_new_instruction(struct radeon_compiler * c,struct rc_instruction * after)101 struct rc_instruction *rc_insert_new_instruction(struct radeon_compiler * c, struct rc_instruction * after)
102 {
103 struct rc_instruction * inst = rc_alloc_instruction(c);
104
105 rc_insert_instruction(after, inst);
106
107 return inst;
108 }
109
rc_remove_instruction(struct rc_instruction * inst)110 void rc_remove_instruction(struct rc_instruction * inst)
111 {
112 inst->Prev->Next = inst->Next;
113 inst->Next->Prev = inst->Prev;
114 }
115
116 /**
117 * Return the number of instructions in the program.
118 */
rc_recompute_ips(struct radeon_compiler * c)119 unsigned int rc_recompute_ips(struct radeon_compiler * c)
120 {
121 unsigned int ip = 0;
122 struct rc_instruction * inst;
123
124 for(inst = c->Program.Instructions.Next;
125 inst != &c->Program.Instructions;
126 inst = inst->Next) {
127 inst->IP = ip++;
128 }
129
130 c->Program.Instructions.IP = 0xcafedead;
131
132 return ip;
133 }
134