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