d8ed4be7b0abb1cd8ac9d137505755755418da0b
[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_ASR:
57 case BRW_OPCODE_ADD:
58 case BRW_OPCODE_MUL:
59 case BRW_OPCODE_FRC:
60 case BRW_OPCODE_RNDU:
61 case BRW_OPCODE_RNDD:
62 case BRW_OPCODE_RNDE:
63 case BRW_OPCODE_RNDZ:
64 case BRW_OPCODE_LINE:
65 case BRW_OPCODE_PLN:
66 case BRW_OPCODE_MAD:
67 case BRW_OPCODE_LRP:
68 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
69 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7:
70 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD:
71 case FS_OPCODE_CINTERP:
72 case FS_OPCODE_LINTERP:
73 return true;
74 default:
75 return false;
76 }
77 }
78
79 static bool
80 operands_match(fs_reg *xs, fs_reg *ys)
81 {
82 return xs[0].equals(ys[0]) && xs[1].equals(ys[1]) && xs[2].equals(ys[2]);
83 }
84
85 bool
86 fs_visitor::opt_cse_local(bblock_t *block, exec_list *aeb)
87 {
88 bool progress = false;
89
90 void *mem_ctx = ralloc_context(this->mem_ctx);
91
92 int ip = block->start_ip;
93 for (fs_inst *inst = (fs_inst *)block->start;
94 inst != block->end->next;
95 inst = (fs_inst *) inst->next) {
96
97 /* Skip some cases. */
98 if (is_expression(inst) &&
99 !inst->predicate &&
100 !inst->is_partial_write() &&
101 !inst->conditional_mod &&
102 inst->dst.file != HW_REG)
103 {
104 bool found = false;
105
106 aeb_entry *entry;
107 foreach_list(entry_node, aeb) {
108 entry = (aeb_entry *) entry_node;
109
110 /* Match current instruction's expression against those in AEB. */
111 if (inst->opcode == entry->generator->opcode &&
112 inst->saturate == entry->generator->saturate &&
113 inst->dst.type == entry->generator->dst.type &&
114 operands_match(entry->generator->src, inst->src)) {
115
116 found = true;
117 progress = true;
118 break;
119 }
120 }
121
122 if (!found) {
123 /* Our first sighting of this expression. Create an entry. */
124 aeb_entry *entry = ralloc(mem_ctx, aeb_entry);
125 entry->tmp = reg_undef;
126 entry->generator = inst;
127 aeb->push_tail(entry);
128 } else {
129 /* This is at least our second sighting of this expression.
130 * If we don't have a temporary already, make one.
131 */
132 bool no_existing_temp = entry->tmp.file == BAD_FILE;
133 if (no_existing_temp) {
134 int written = entry->generator->regs_written;
135
136 fs_reg orig_dst = entry->generator->dst;
137 fs_reg tmp = fs_reg(GRF, virtual_grf_alloc(written),
138 orig_dst.type);
139 entry->tmp = tmp;
140 entry->generator->dst = tmp;
141
142 for (int i = 0; i < written; i++) {
143 fs_inst *copy = MOV(orig_dst, tmp);
144 copy->force_writemask_all =
145 entry->generator->force_writemask_all;
146 entry->generator->insert_after(copy);
147
148 orig_dst.reg_offset++;
149 tmp.reg_offset++;
150 }
151 }
152
153 /* dest <- temp */
154 int written = inst->regs_written;
155 assert(written == entry->generator->regs_written);
156 assert(inst->dst.type == entry->tmp.type);
157 fs_reg dst = inst->dst;
158 fs_reg tmp = entry->tmp;
159 fs_inst *copy = NULL;
160 for (int i = 0; i < written; i++) {
161 copy = MOV(dst, tmp);
162 copy->force_writemask_all = inst->force_writemask_all;
163 inst->insert_before(copy);
164
165 dst.reg_offset++;
166 tmp.reg_offset++;
167 }
168 inst->remove();
169
170 /* Appending an instruction may have changed our bblock end. */
171 if (inst == block->end) {
172 block->end = copy;
173 }
174
175 /* Continue iteration with copy->next */
176 inst = copy;
177 }
178 }
179
180 foreach_list_safe(entry_node, aeb) {
181 aeb_entry *entry = (aeb_entry *)entry_node;
182
183 for (int i = 0; i < 3; i++) {
184 fs_reg *src_reg = &entry->generator->src[i];
185
186 /* Kill all AEB entries that use the destination we just
187 * overwrote.
188 */
189 if (inst->overwrites_reg(entry->generator->src[i])) {
190 entry->remove();
191 ralloc_free(entry);
192 break;
193 }
194
195 /* Kill any AEB entries using registers that don't get reused any
196 * more -- a sure sign they'll fail operands_match().
197 */
198 if (src_reg->file == GRF && virtual_grf_end[src_reg->reg] < ip) {
199 entry->remove();
200 ralloc_free(entry);
201 break;
202 }
203 }
204 }
205
206 ip++;
207 }
208
209 ralloc_free(mem_ctx);
210
211 if (progress)
212 invalidate_live_intervals();
213
214 return progress;
215 }
216
217 bool
218 fs_visitor::opt_cse()
219 {
220 bool progress = false;
221
222 calculate_live_intervals();
223
224 cfg_t cfg(this);
225
226 for (int b = 0; b < cfg.num_blocks; b++) {
227 bblock_t *block = cfg.blocks[b];
228 exec_list aeb;
229
230 progress = opt_cse_local(block, &aeb) || progress;
231 }
232
233 return progress;
234 }