2 * Copyright © 2012, 2013, 2014 Intel Corporation
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:
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
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
29 /** @file brw_vec4_cse.cpp
31 * Support for local common subexpression elimination.
33 * See Muchnick's Advanced Compiler Design and Implementation, section
38 struct aeb_entry : public exec_node {
39 /** The instruction that generates the expression value. */
40 vec4_instruction *generator;
42 /** The temporary where the value is stored. */
48 is_expression(const vec4_instruction *const inst)
50 switch (inst->opcode) {
73 case VEC4_OPCODE_UNPACK_UNIFORM:
75 case SHADER_OPCODE_RCP:
76 case SHADER_OPCODE_RSQ:
77 case SHADER_OPCODE_SQRT:
78 case SHADER_OPCODE_EXP2:
79 case SHADER_OPCODE_LOG2:
80 case SHADER_OPCODE_POW:
81 case SHADER_OPCODE_INT_QUOTIENT:
82 case SHADER_OPCODE_INT_REMAINDER:
83 case SHADER_OPCODE_SIN:
84 case SHADER_OPCODE_COS:
85 return inst->mlen == 0;
92 is_expression_commutative(enum opcode op)
107 operands_match(enum opcode op, src_reg *xs, src_reg *ys)
109 if (op == BRW_OPCODE_MAD) {
110 return xs[0].equals(ys[0]) &&
111 ((xs[1].equals(ys[1]) && xs[2].equals(ys[2])) ||
112 (xs[2].equals(ys[1]) && xs[1].equals(ys[2])));
113 } else if (!is_expression_commutative(op)) {
114 return xs[0].equals(ys[0]) && xs[1].equals(ys[1]) && xs[2].equals(ys[2]);
116 return (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
117 (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
122 instructions_match(vec4_instruction *a, vec4_instruction *b)
124 return a->opcode == b->opcode &&
125 a->saturate == b->saturate &&
126 a->conditional_mod == b->conditional_mod &&
127 a->dst.type == b->dst.type &&
128 a->dst.writemask == b->dst.writemask &&
129 operands_match(a->opcode, a->src, b->src);
133 vec4_visitor::opt_cse_local(bblock_t *block)
135 bool progress = false;
138 void *cse_ctx = ralloc_context(NULL);
140 int ip = block->start_ip;
141 foreach_inst_in_block (vec4_instruction, inst, block) {
142 /* Skip some cases. */
143 if (is_expression(inst) && !inst->predicate && inst->mlen == 0 &&
144 (inst->dst.file != HW_REG || inst->dst.is_null()))
148 foreach_in_list_use_after(aeb_entry, entry, &aeb) {
149 /* Match current instruction's expression against those in AEB. */
150 if (instructions_match(inst, entry->generator)) {
158 if (inst->opcode != BRW_OPCODE_MOV ||
159 (inst->opcode == BRW_OPCODE_MOV &&
160 inst->src[0].file == IMM &&
161 inst->src[0].type == BRW_REGISTER_TYPE_VF)) {
162 /* Our first sighting of this expression. Create an entry. */
163 aeb_entry *entry = ralloc(cse_ctx, aeb_entry);
164 entry->tmp = src_reg(); /* file will be BAD_FILE */
165 entry->generator = inst;
166 aeb.push_tail(entry);
169 /* This is at least our second sighting of this expression.
170 * If we don't have a temporary already, make one.
172 bool no_existing_temp = entry->tmp.file == BAD_FILE;
173 if (no_existing_temp && !entry->generator->dst.is_null()) {
174 entry->tmp = src_reg(this, glsl_type::float_type);
175 entry->tmp.type = inst->dst.type;
176 entry->tmp.swizzle = BRW_SWIZZLE_XYZW;
178 vec4_instruction *copy = MOV(entry->generator->dst, entry->tmp);
179 entry->generator->insert_after(block, copy);
180 entry->generator->dst = dst_reg(entry->tmp);
184 if (!inst->dst.is_null()) {
185 assert(inst->dst.type == entry->tmp.type);
186 vec4_instruction *copy = MOV(inst->dst, entry->tmp);
187 copy->force_writemask_all = inst->force_writemask_all;
188 inst->insert_before(block, copy);
191 /* Set our iterator so that next time through the loop inst->next
192 * will get the instruction in the basic block after the one we've
195 vec4_instruction *prev = (vec4_instruction *)inst->prev;
202 foreach_in_list_safe(aeb_entry, entry, &aeb) {
203 /* Kill all AEB entries that write a different value to or read from
204 * the flag register if we just wrote it.
206 if (inst->writes_flag()) {
207 if (entry->generator->reads_flag() ||
208 (entry->generator->writes_flag() &&
209 !instructions_match(inst, entry->generator))) {
216 for (int i = 0; i < 3; i++) {
217 src_reg *src = &entry->generator->src[i];
219 /* Kill all AEB entries that use the destination we just
222 if (inst->dst.file == entry->generator->src[i].file &&
223 inst->dst.reg == entry->generator->src[i].reg) {
229 /* Kill any AEB entries using registers that don't get reused any
230 * more -- a sure sign they'll fail operands_match().
232 if (src->file == GRF) {
233 assert((src->reg * 4 + 3) < (virtual_grf_count * 4));
235 int last_reg_use = MAX2(MAX2(virtual_grf_end[src->reg * 4 + 0],
236 virtual_grf_end[src->reg * 4 + 1]),
237 MAX2(virtual_grf_end[src->reg * 4 + 2],
238 virtual_grf_end[src->reg * 4 + 3]));
239 if (last_reg_use < ip) {
251 ralloc_free(cse_ctx);
257 vec4_visitor::opt_cse()
259 bool progress = false;
261 calculate_live_intervals();
263 foreach_block (block, cfg) {
264 progress = opt_cse_local(block) || progress;
268 invalidate_live_intervals();