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