• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Muchnick's Advanced Compiler Design and Implementation, section
32  * 13.1 (p378).
33  */
34 
35 using namespace brw;
36 
37 namespace {
38 struct aeb_entry : public exec_node {
39    /** The instruction that generates the expression value. */
40    fs_inst *generator;
41 
42    /** The temporary where the value is stored. */
43    fs_reg tmp;
44 };
45 }
46 
47 static bool
is_expression(const fs_visitor * v,const fs_inst * const inst)48 is_expression(const fs_visitor *v, const fs_inst *const inst)
49 {
50    switch (inst->opcode) {
51    case BRW_OPCODE_MOV:
52    case BRW_OPCODE_SEL:
53    case BRW_OPCODE_NOT:
54    case BRW_OPCODE_AND:
55    case BRW_OPCODE_OR:
56    case BRW_OPCODE_XOR:
57    case BRW_OPCODE_SHR:
58    case BRW_OPCODE_SHL:
59    case BRW_OPCODE_ASR:
60    case BRW_OPCODE_CMP:
61    case BRW_OPCODE_CMPN:
62    case BRW_OPCODE_ADD:
63    case BRW_OPCODE_MUL:
64    case SHADER_OPCODE_MULH:
65    case BRW_OPCODE_FRC:
66    case BRW_OPCODE_RNDU:
67    case BRW_OPCODE_RNDD:
68    case BRW_OPCODE_RNDE:
69    case BRW_OPCODE_RNDZ:
70    case BRW_OPCODE_LINE:
71    case BRW_OPCODE_PLN:
72    case BRW_OPCODE_MAD:
73    case BRW_OPCODE_LRP:
74    case FS_OPCODE_FB_READ_LOGICAL:
75    case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
76    case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_LOGICAL:
77    case FS_OPCODE_LINTERP:
78    case SHADER_OPCODE_FIND_LIVE_CHANNEL:
79    case SHADER_OPCODE_FIND_LAST_LIVE_CHANNEL:
80    case FS_OPCODE_LOAD_LIVE_CHANNELS:
81    case SHADER_OPCODE_BROADCAST:
82    case SHADER_OPCODE_MOV_INDIRECT:
83    case SHADER_OPCODE_TEX_LOGICAL:
84    case SHADER_OPCODE_TXD_LOGICAL:
85    case SHADER_OPCODE_TXF_LOGICAL:
86    case SHADER_OPCODE_TXL_LOGICAL:
87    case SHADER_OPCODE_TXS_LOGICAL:
88    case FS_OPCODE_TXB_LOGICAL:
89    case SHADER_OPCODE_TXF_CMS_LOGICAL:
90    case SHADER_OPCODE_TXF_CMS_W_LOGICAL:
91    case SHADER_OPCODE_TXF_UMS_LOGICAL:
92    case SHADER_OPCODE_TXF_MCS_LOGICAL:
93    case SHADER_OPCODE_LOD_LOGICAL:
94    case SHADER_OPCODE_TG4_LOGICAL:
95    case SHADER_OPCODE_TG4_OFFSET_LOGICAL:
96    case FS_OPCODE_PACK:
97       return true;
98    case SHADER_OPCODE_RCP:
99    case SHADER_OPCODE_RSQ:
100    case SHADER_OPCODE_SQRT:
101    case SHADER_OPCODE_EXP2:
102    case SHADER_OPCODE_LOG2:
103    case SHADER_OPCODE_POW:
104    case SHADER_OPCODE_INT_QUOTIENT:
105    case SHADER_OPCODE_INT_REMAINDER:
106    case SHADER_OPCODE_SIN:
107    case SHADER_OPCODE_COS:
108       return inst->mlen < 2;
109    case SHADER_OPCODE_LOAD_PAYLOAD:
110       return !is_coalescing_payload(v->alloc, inst);
111    default:
112       return inst->is_send_from_grf() && !inst->has_side_effects() &&
113          !inst->is_volatile();
114    }
115 }
116 
117 static bool
operands_match(const fs_inst * a,const fs_inst * b,bool * negate)118 operands_match(const fs_inst *a, const fs_inst *b, bool *negate)
119 {
120    fs_reg *xs = a->src;
121    fs_reg *ys = b->src;
122 
123    if (a->opcode == BRW_OPCODE_MAD) {
124       return xs[0].equals(ys[0]) &&
125              ((xs[1].equals(ys[1]) && xs[2].equals(ys[2])) ||
126               (xs[2].equals(ys[1]) && xs[1].equals(ys[2])));
127    } else if (a->opcode == BRW_OPCODE_MUL && a->dst.type == BRW_REGISTER_TYPE_F) {
128       bool xs0_negate = xs[0].negate;
129       bool xs1_negate = xs[1].file == IMM ? xs[1].f < 0.0f
130                                           : xs[1].negate;
131       bool ys0_negate = ys[0].negate;
132       bool ys1_negate = ys[1].file == IMM ? ys[1].f < 0.0f
133                                           : ys[1].negate;
134       float xs1_imm = xs[1].f;
135       float ys1_imm = ys[1].f;
136 
137       xs[0].negate = false;
138       xs[1].negate = false;
139       ys[0].negate = false;
140       ys[1].negate = false;
141       xs[1].f = fabsf(xs[1].f);
142       ys[1].f = fabsf(ys[1].f);
143 
144       bool ret = (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
145                  (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
146 
147       xs[0].negate = xs0_negate;
148       xs[1].negate = xs[1].file == IMM ? false : xs1_negate;
149       ys[0].negate = ys0_negate;
150       ys[1].negate = ys[1].file == IMM ? false : ys1_negate;
151       xs[1].f = xs1_imm;
152       ys[1].f = ys1_imm;
153 
154       *negate = (xs0_negate != xs1_negate) != (ys0_negate != ys1_negate);
155       if (*negate && (a->saturate || b->saturate))
156          return false;
157       return ret;
158    } else if (!a->is_commutative()) {
159       bool match = true;
160       for (int i = 0; i < a->sources; i++) {
161          if (!xs[i].equals(ys[i])) {
162             match = false;
163             break;
164          }
165       }
166       return match;
167    } else {
168       return (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
169              (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
170    }
171 }
172 
173 static bool
instructions_match(fs_inst * a,fs_inst * b,bool * negate)174 instructions_match(fs_inst *a, fs_inst *b, bool *negate)
175 {
176    return a->opcode == b->opcode &&
177           a->force_writemask_all == b->force_writemask_all &&
178           a->exec_size == b->exec_size &&
179           a->group == b->group &&
180           a->saturate == b->saturate &&
181           a->predicate == b->predicate &&
182           a->predicate_inverse == b->predicate_inverse &&
183           a->conditional_mod == b->conditional_mod &&
184           a->flag_subreg == b->flag_subreg &&
185           a->dst.type == b->dst.type &&
186           a->offset == b->offset &&
187           a->mlen == b->mlen &&
188           a->ex_mlen == b->ex_mlen &&
189           a->sfid == b->sfid &&
190           a->desc == b->desc &&
191           a->size_written == b->size_written &&
192           a->base_mrf == b->base_mrf &&
193           a->check_tdr == b->check_tdr &&
194           a->send_has_side_effects == b->send_has_side_effects &&
195           a->eot == b->eot &&
196           a->header_size == b->header_size &&
197           a->shadow_compare == b->shadow_compare &&
198           a->pi_noperspective == b->pi_noperspective &&
199           a->target == b->target &&
200           a->sources == b->sources &&
201           operands_match(a, b, negate);
202 }
203 
204 static void
create_copy_instr(const fs_builder & bld,fs_inst * inst,fs_reg src,bool negate)205 create_copy_instr(const fs_builder &bld, fs_inst *inst, fs_reg src, bool negate)
206 {
207    unsigned written = regs_written(inst);
208    unsigned dst_width =
209       DIV_ROUND_UP(inst->dst.component_size(inst->exec_size), REG_SIZE);
210    fs_inst *copy;
211 
212    if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD) {
213       assert(src.file == VGRF);
214       fs_reg *payload = ralloc_array(bld.shader->mem_ctx, fs_reg,
215                                      inst->sources);
216       for (int i = 0; i < inst->header_size; i++) {
217          payload[i] = src;
218          src.offset += REG_SIZE;
219       }
220       for (int i = inst->header_size; i < inst->sources; i++) {
221          src.type = inst->src[i].type;
222          payload[i] = src;
223          src = offset(src, bld, 1);
224       }
225       copy = bld.LOAD_PAYLOAD(inst->dst, payload, inst->sources,
226                               inst->header_size);
227    } else if (written != dst_width) {
228       assert(src.file == VGRF);
229       assert(written % dst_width == 0);
230       const int sources = written / dst_width;
231       fs_reg *payload = ralloc_array(bld.shader->mem_ctx, fs_reg, sources);
232       for (int i = 0; i < sources; i++) {
233          payload[i] = src;
234          src = offset(src, bld, 1);
235       }
236       copy = bld.LOAD_PAYLOAD(inst->dst, payload, sources, 0);
237    } else {
238       copy = bld.MOV(inst->dst, src);
239       copy->group = inst->group;
240       copy->force_writemask_all = inst->force_writemask_all;
241       copy->src[0].negate = negate;
242    }
243    assert(regs_written(copy) == written);
244 }
245 
246 bool
opt_cse_local(const fs_live_variables & live,bblock_t * block,int & ip)247 fs_visitor::opt_cse_local(const fs_live_variables &live, bblock_t *block, int &ip)
248 {
249    bool progress = false;
250    exec_list aeb;
251 
252    void *cse_ctx = ralloc_context(NULL);
253 
254    foreach_inst_in_block(fs_inst, inst, block) {
255       /* Skip some cases. */
256       if (is_expression(this, inst) && !inst->is_partial_write() &&
257           ((inst->dst.file != ARF && inst->dst.file != FIXED_GRF) ||
258            inst->dst.is_null()))
259       {
260          bool found = false;
261          bool negate = false;
262 
263          foreach_in_list_use_after(aeb_entry, entry, &aeb) {
264             /* Match current instruction's expression against those in AEB. */
265             if (!(entry->generator->dst.is_null() && !inst->dst.is_null()) &&
266                 instructions_match(inst, entry->generator, &negate)) {
267                found = true;
268                progress = true;
269                break;
270             }
271          }
272 
273          if (!found) {
274             if (inst->opcode != BRW_OPCODE_MOV ||
275                 (inst->opcode == BRW_OPCODE_MOV &&
276                  inst->src[0].file == IMM &&
277                  inst->src[0].type == BRW_REGISTER_TYPE_VF)) {
278                /* Our first sighting of this expression.  Create an entry. */
279                aeb_entry *entry = ralloc(cse_ctx, aeb_entry);
280                entry->tmp = reg_undef;
281                entry->generator = inst;
282                aeb.push_tail(entry);
283             }
284          } else {
285             /* This is at least our second sighting of this expression.
286              * If we don't have a temporary already, make one.
287              */
288             bool no_existing_temp = entry->tmp.file == BAD_FILE;
289             if (no_existing_temp && !entry->generator->dst.is_null()) {
290                const fs_builder ibld = fs_builder(this, block, entry->generator)
291                                        .at(block, entry->generator->next);
292                int written = regs_written(entry->generator);
293 
294                entry->tmp = fs_reg(VGRF, alloc.allocate(written),
295                                    entry->generator->dst.type);
296 
297                create_copy_instr(ibld, entry->generator, entry->tmp, false);
298 
299                entry->generator->dst = entry->tmp;
300             }
301 
302             /* dest <- temp */
303             if (!inst->dst.is_null()) {
304                assert(inst->size_written == entry->generator->size_written);
305                assert(inst->dst.type == entry->tmp.type);
306                const fs_builder ibld(this, block, inst);
307 
308                create_copy_instr(ibld, inst, entry->tmp, negate);
309             }
310 
311             /* Set our iterator so that next time through the loop inst->next
312              * will get the instruction in the basic block after the one we've
313              * removed.
314              */
315             fs_inst *prev = (fs_inst *)inst->prev;
316 
317             inst->remove(block);
318             inst = prev;
319          }
320       }
321 
322       /* Discard jumps aren't represented in the CFG unfortunately, so we need
323        * to make sure that they behave as a CSE barrier, since we lack global
324        * dataflow information.  This is particularly likely to cause problems
325        * with instructions dependent on the current execution mask like
326        * SHADER_OPCODE_FIND_LIVE_CHANNEL.
327        */
328       if (inst->opcode == BRW_OPCODE_HALT ||
329           inst->opcode == SHADER_OPCODE_HALT_TARGET)
330          aeb.make_empty();
331 
332       foreach_in_list_safe(aeb_entry, entry, &aeb) {
333          /* Kill all AEB entries that write a different value to or read from
334           * the flag register if we just wrote it.
335           */
336          if (inst->flags_written(devinfo)) {
337             bool negate; /* dummy */
338             if (entry->generator->flags_read(devinfo) ||
339                 (entry->generator->flags_written(devinfo) &&
340                  !instructions_match(inst, entry->generator, &negate))) {
341                entry->remove();
342                ralloc_free(entry);
343                continue;
344             }
345          }
346 
347          for (int i = 0; i < entry->generator->sources; i++) {
348             fs_reg *src_reg = &entry->generator->src[i];
349 
350             /* Kill all AEB entries that use the destination we just
351              * overwrote.
352              */
353             if (regions_overlap(inst->dst, inst->size_written,
354                                 entry->generator->src[i],
355                                 entry->generator->size_read(i))) {
356                entry->remove();
357                ralloc_free(entry);
358                break;
359             }
360 
361             /* Kill any AEB entries using registers that don't get reused any
362              * more -- a sure sign they'll fail operands_match().
363              */
364             if (src_reg->file == VGRF && live.vgrf_end[src_reg->nr] < ip) {
365                entry->remove();
366                ralloc_free(entry);
367                break;
368             }
369          }
370       }
371 
372       ip++;
373    }
374 
375    ralloc_free(cse_ctx);
376 
377    return progress;
378 }
379 
380 bool
opt_cse()381 fs_visitor::opt_cse()
382 {
383    const fs_live_variables &live = live_analysis.require();
384    bool progress = false;
385    int ip = 0;
386 
387    foreach_block (block, cfg) {
388       progress = opt_cse_local(live, block, ip) || progress;
389    }
390 
391    if (progress)
392       invalidate_analysis(DEPENDENCY_INSTRUCTIONS | DEPENDENCY_VARIABLES);
393 
394    return progress;
395 }
396