i965/gs: Add a case to brwNewProgram() for geometry shaders.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_cse.cpp
1 /*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "brw_fs.h"
25 #include "brw_cfg.h"
26
27 /** @file brw_fs_cse.cpp
28 *
29 * Support for local common subexpression elimination.
30 *
31 * See Muchnik's Advanced Compiler Design and Implementation, section
32 * 13.1 (p378).
33 */
34
35 namespace {
36 struct aeb_entry : public exec_node {
37 /** The instruction that generates the expression value. */
38 fs_inst *generator;
39
40 /** The temporary where the value is stored. */
41 fs_reg tmp;
42 };
43 }
44
45 static bool
46 is_expression(const fs_inst *const inst)
47 {
48 switch (inst->opcode) {
49 case BRW_OPCODE_SEL:
50 case BRW_OPCODE_NOT:
51 case BRW_OPCODE_AND:
52 case BRW_OPCODE_OR:
53 case BRW_OPCODE_XOR:
54 case BRW_OPCODE_SHR:
55 case BRW_OPCODE_SHL:
56 case BRW_OPCODE_RSR:
57 case BRW_OPCODE_RSL:
58 case BRW_OPCODE_ASR:
59 case BRW_OPCODE_ADD:
60 case BRW_OPCODE_MUL:
61 case BRW_OPCODE_FRC:
62 case BRW_OPCODE_RNDU:
63 case BRW_OPCODE_RNDD:
64 case BRW_OPCODE_RNDE:
65 case BRW_OPCODE_RNDZ:
66 case BRW_OPCODE_LINE:
67 case BRW_OPCODE_PLN:
68 case BRW_OPCODE_MAD:
69 case BRW_OPCODE_LRP:
70 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
71 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7:
72 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD:
73 case FS_OPCODE_CINTERP:
74 case FS_OPCODE_LINTERP:
75 return true;
76 default:
77 return false;
78 }
79 }
80
81 static bool
82 operands_match(fs_reg *xs, fs_reg *ys)
83 {
84 return xs[0].equals(ys[0]) && xs[1].equals(ys[1]) && xs[2].equals(ys[2]);
85 }
86
87 bool
88 fs_visitor::opt_cse_local(bblock_t *block, exec_list *aeb)
89 {
90 bool progress = false;
91
92 void *mem_ctx = ralloc_context(this->mem_ctx);
93
94 int ip = block->start_ip;
95 for (fs_inst *inst = (fs_inst *)block->start;
96 inst != block->end->next;
97 inst = (fs_inst *) inst->next) {
98
99 /* Skip some cases. */
100 if (is_expression(inst) &&
101 !inst->predicate &&
102 !inst->is_partial_write() &&
103 !inst->conditional_mod)
104 {
105 bool found = false;
106
107 aeb_entry *entry;
108 foreach_list(entry_node, aeb) {
109 entry = (aeb_entry *) entry_node;
110
111 /* Match current instruction's expression against those in AEB. */
112 if (inst->opcode == entry->generator->opcode &&
113 inst->saturate == entry->generator->saturate &&
114 inst->dst.type == entry->generator->dst.type &&
115 operands_match(entry->generator->src, inst->src)) {
116
117 found = true;
118 progress = true;
119 break;
120 }
121 }
122
123 if (!found) {
124 /* Our first sighting of this expression. Create an entry. */
125 aeb_entry *entry = ralloc(mem_ctx, aeb_entry);
126 entry->tmp = reg_undef;
127 entry->generator = inst;
128 aeb->push_tail(entry);
129 } else {
130 /* This is at least our second sighting of this expression.
131 * If we don't have a temporary already, make one.
132 */
133 bool no_existing_temp = entry->tmp.file == BAD_FILE;
134 if (no_existing_temp) {
135 int written = entry->generator->regs_written;
136
137 fs_reg orig_dst = entry->generator->dst;
138 fs_reg tmp = fs_reg(GRF, virtual_grf_alloc(written),
139 orig_dst.type);
140 entry->tmp = tmp;
141 entry->generator->dst = tmp;
142
143 for (int i = 0; i < written; i++) {
144 fs_inst *copy = MOV(orig_dst, tmp);
145 copy->force_writemask_all =
146 entry->generator->force_writemask_all;
147 entry->generator->insert_after(copy);
148
149 orig_dst.reg_offset++;
150 tmp.reg_offset++;
151 }
152 }
153
154 /* dest <- temp */
155 int written = inst->regs_written;
156 assert(written == entry->generator->regs_written);
157 assert(inst->dst.type == entry->tmp.type);
158 fs_reg dst = inst->dst;
159 fs_reg tmp = entry->tmp;
160 fs_inst *copy = NULL;
161 for (int i = 0; i < written; i++) {
162 copy = MOV(dst, tmp);
163 copy->force_writemask_all = inst->force_writemask_all;
164 inst->insert_before(copy);
165
166 dst.reg_offset++;
167 tmp.reg_offset++;
168 }
169 inst->remove();
170
171 /* Appending an instruction may have changed our bblock end. */
172 if (inst == block->end) {
173 block->end = copy;
174 }
175
176 /* Continue iteration with copy->next */
177 inst = copy;
178 }
179 }
180
181 foreach_list_safe(entry_node, aeb) {
182 aeb_entry *entry = (aeb_entry *)entry_node;
183
184 for (int i = 0; i < 3; i++) {
185 fs_reg *src_reg = &entry->generator->src[i];
186
187 /* Kill all AEB entries that use the destination we just
188 * overwrote.
189 */
190 if (inst->overwrites_reg(entry->generator->src[i])) {
191 entry->remove();
192 ralloc_free(entry);
193 break;
194 }
195
196 /* Kill any AEB entries using registers that don't get reused any
197 * more -- a sure sign they'll fail operands_match().
198 */
199 if (src_reg->file == GRF && virtual_grf_end[src_reg->reg] < ip) {
200 entry->remove();
201 ralloc_free(entry);
202 break;
203 }
204 }
205 }
206
207 ip++;
208 }
209
210 ralloc_free(mem_ctx);
211
212 if (progress)
213 this->live_intervals_valid = false;
214
215 return progress;
216 }
217
218 bool
219 fs_visitor::opt_cse()
220 {
221 bool progress = false;
222
223 calculate_live_intervals();
224
225 cfg_t cfg(this);
226
227 for (int b = 0; b < cfg.num_blocks; b++) {
228 bblock_t *block = cfg.blocks[b];
229 exec_list aeb;
230
231 progress = opt_cse_local(block, &aeb) || progress;
232 }
233
234 return progress;
235 }