brw_vec4_cse.cpp 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright © 2012, 2013, 2014 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "brw_vec4.h"
25
#include "brw_vec4_live_variables.h"
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include "brw_cfg.h"

using namespace brw;

/** @file brw_vec4_cse.cpp
 *
 * Support for local common subexpression elimination.
 *
 * See Muchnick's Advanced Compiler Design and Implementation, section
 * 13.1 (p378).
 */

namespace {
struct aeb_entry : public exec_node {
   /** The instruction that generates the expression value. */
   vec4_instruction *generator;

   /** The temporary where the value is stored. */
   src_reg tmp;
};
}

static bool
is_expression(const vec4_instruction *const inst)
{
   switch (inst->opcode) {
52
   case BRW_OPCODE_MOV:
53 54 55 56 57 58 59 60
   case BRW_OPCODE_SEL:
   case BRW_OPCODE_NOT:
   case BRW_OPCODE_AND:
   case BRW_OPCODE_OR:
   case BRW_OPCODE_XOR:
   case BRW_OPCODE_SHR:
   case BRW_OPCODE_SHL:
   case BRW_OPCODE_ASR:
61 62
   case BRW_OPCODE_CMP:
   case BRW_OPCODE_CMPN:
63 64
   case BRW_OPCODE_ADD:
   case BRW_OPCODE_MUL:
65
   case SHADER_OPCODE_MULH:
66 67 68 69 70 71 72 73 74
   case BRW_OPCODE_FRC:
   case BRW_OPCODE_RNDU:
   case BRW_OPCODE_RNDD:
   case BRW_OPCODE_RNDE:
   case BRW_OPCODE_RNDZ:
   case BRW_OPCODE_LINE:
   case BRW_OPCODE_PLN:
   case BRW_OPCODE_MAD:
   case BRW_OPCODE_LRP:
75
   case VEC4_OPCODE_UNPACK_UNIFORM:
76
   case SHADER_OPCODE_FIND_LIVE_CHANNEL:
77
   case SHADER_OPCODE_BROADCAST:
78 79
   case TCS_OPCODE_SET_INPUT_URB_OFFSETS:
   case TCS_OPCODE_SET_OUTPUT_URB_OFFSETS:
80 81 82 83 84 85 86 87 88 89 90 91 92
      return true;
   case SHADER_OPCODE_RCP:
   case SHADER_OPCODE_RSQ:
   case SHADER_OPCODE_SQRT:
   case SHADER_OPCODE_EXP2:
   case SHADER_OPCODE_LOG2:
   case SHADER_OPCODE_POW:
   case SHADER_OPCODE_INT_QUOTIENT:
   case SHADER_OPCODE_INT_REMAINDER:
   case SHADER_OPCODE_SIN:
   case SHADER_OPCODE_COS:
      return inst->mlen == 0;
   default:
93
      return false;
94 95 96 97
   }
}

static bool
98
operands_match(const vec4_instruction *a, const vec4_instruction *b)
99
{
100 101 102 103
   const src_reg *xs = a->src;
   const src_reg *ys = b->src;

   if (a->opcode == BRW_OPCODE_MAD) {
104 105 106
      return xs[0].equals(ys[0]) &&
             ((xs[1].equals(ys[1]) && xs[2].equals(ys[2])) ||
              (xs[2].equals(ys[1]) && xs[1].equals(ys[2])));
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
   } else if (a->opcode == BRW_OPCODE_MOV &&
              xs[0].file == IMM &&
              xs[0].type == BRW_REGISTER_TYPE_VF) {
      src_reg tmp_x = xs[0];
      src_reg tmp_y = ys[0];

      /* Smash out the values that are not part of the writemask.  Otherwise
       * the equals operator will fail due to mismatches in unused components.
       */
      const unsigned ab_writemask = a->dst.writemask & b->dst.writemask;
      const uint32_t mask = ((ab_writemask & WRITEMASK_X) ? 0x000000ff : 0) |
                            ((ab_writemask & WRITEMASK_Y) ? 0x0000ff00 : 0) |
                            ((ab_writemask & WRITEMASK_Z) ? 0x00ff0000 : 0) |
                            ((ab_writemask & WRITEMASK_W) ? 0xff000000 : 0);

      tmp_x.ud &= mask;
      tmp_y.ud &= mask;

      return tmp_x.equals(tmp_y);
126
   } else if (!a->is_commutative()) {
127 128 129 130 131 132 133
      return xs[0].equals(ys[0]) && xs[1].equals(ys[1]) && xs[2].equals(ys[2]);
   } else {
      return (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
             (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
   }
}

134 135 136 137 138 139 140 141
/**
 * Checks if instructions match, exactly for sources, but loosely for
 * destination writemasks.
 *
 * \param 'a' is the generating expression from the AEB entry.
 * \param 'b' is the second occurrence of the expression that we're
 *        considering eliminating.
 */
142 143 144 145 146
static bool
instructions_match(vec4_instruction *a, vec4_instruction *b)
{
   return a->opcode == b->opcode &&
          a->saturate == b->saturate &&
147 148
          a->predicate == b->predicate &&
          a->predicate_inverse == b->predicate_inverse &&
149
          a->conditional_mod == b->conditional_mod &&
150
          a->flag_subreg == b->flag_subreg &&
151
          a->dst.type == b->dst.type &&
152 153 154 155 156
          a->offset == b->offset &&
          a->mlen == b->mlen &&
          a->base_mrf == b->base_mrf &&
          a->header_size == b->header_size &&
          a->shadow_compare == b->shadow_compare &&
157
          ((a->dst.writemask & b->dst.writemask) == a->dst.writemask) &&
158
          a->force_writemask_all == b->force_writemask_all &&
159
          a->size_written == b->size_written &&
160 161
          a->exec_size == b->exec_size &&
          a->group == b->group &&
162
          operands_match(a, b);
163 164 165
}

bool
166
vec4_visitor::opt_cse_local(bblock_t *block)
167 168
{
   bool progress = false;
169
   exec_list aeb;
170 171 172

   void *cse_ctx = ralloc_context(NULL);

173
   int ip = block->start_ip;
174
   foreach_inst_in_block (vec4_instruction, inst, block) {
175 176
      /* Skip some cases. */
      if (is_expression(inst) && !inst->predicate && inst->mlen == 0 &&
177 178
          ((inst->dst.file != ARF && inst->dst.file != FIXED_GRF) ||
           inst->dst.is_null()))
179 180 181
      {
         bool found = false;

182
         foreach_in_list_use_after(aeb_entry, entry, &aeb) {
183
            /* Match current instruction's expression against those in AEB. */
184 185
            if (!(entry->generator->dst.is_null() && !inst->dst.is_null()) &&
                instructions_match(inst, entry->generator)) {
186 187 188 189 190 191 192
               found = true;
               progress = true;
               break;
            }
         }

         if (!found) {
193 194 195 196 197 198 199 200 201 202
            if (inst->opcode != BRW_OPCODE_MOV ||
                (inst->opcode == BRW_OPCODE_MOV &&
                 inst->src[0].file == IMM &&
                 inst->src[0].type == BRW_REGISTER_TYPE_VF)) {
               /* Our first sighting of this expression.  Create an entry. */
               aeb_entry *entry = ralloc(cse_ctx, aeb_entry);
               entry->tmp = src_reg(); /* file will be BAD_FILE */
               entry->generator = inst;
               aeb.push_tail(entry);
            }
203 204 205 206 207
         } else {
            /* This is at least our second sighting of this expression.
             * If we don't have a temporary already, make one.
             */
            bool no_existing_temp = entry->tmp.file == BAD_FILE;
208
            if (no_existing_temp && !entry->generator->dst.is_null()) {
Matt Turner's avatar
Matt Turner committed
209
               entry->tmp = retype(src_reg(VGRF, alloc.allocate(
210
                                              regs_written(entry->generator)),
211 212
                                           NULL), inst->dst.type);

213 214 215 216 217
               const unsigned width = entry->generator->exec_size;
               unsigned component_size = width * type_sz(entry->tmp.type);
               unsigned num_copy_movs =
                  DIV_ROUND_UP(entry->generator->size_written, component_size);
               for (unsigned i = 0; i < num_copy_movs; ++i) {
218
                  vec4_instruction *copy =
219 220 221 222
                     MOV(offset(entry->generator->dst, width, i),
                         offset(entry->tmp, width, i));
                  copy->exec_size = width;
                  copy->group = entry->generator->group;
223 224
                  copy->force_writemask_all =
                     entry->generator->force_writemask_all;
225 226
                  entry->generator->insert_after(block, copy);
               }
227 228 229 230 231

               entry->generator->dst = dst_reg(entry->tmp);
            }

            /* dest <- temp */
232 233
            if (!inst->dst.is_null()) {
               assert(inst->dst.type == entry->tmp.type);
234 235 236 237 238
               const unsigned width = inst->exec_size;
               unsigned component_size = width * type_sz(inst->dst.type);
               unsigned num_copy_movs =
                  DIV_ROUND_UP(inst->size_written, component_size);
               for (unsigned i = 0; i < num_copy_movs; ++i) {
239
                  vec4_instruction *copy =
240 241 242 243
                     MOV(offset(inst->dst, width, i),
                         offset(entry->tmp, width, i));
                  copy->exec_size = inst->exec_size;
                  copy->group = inst->group;
244 245 246
                  copy->force_writemask_all = inst->force_writemask_all;
                  inst->insert_before(block, copy);
               }
247
            }
248 249 250 251 252 253 254

            /* Set our iterator so that next time through the loop inst->next
             * will get the instruction in the basic block after the one we've
             * removed.
             */
            vec4_instruction *prev = (vec4_instruction *)inst->prev;

255
            inst->remove(block);
256 257 258 259
            inst = prev;
         }
      }

260
      foreach_in_list_safe(aeb_entry, entry, &aeb) {
261 262 263 264 265 266 267 268 269 270 271 272 273
         /* Kill all AEB entries that write a different value to or read from
          * the flag register if we just wrote it.
          */
         if (inst->writes_flag()) {
            if (entry->generator->reads_flag() ||
                (entry->generator->writes_flag() &&
                 !instructions_match(inst, entry->generator))) {
               entry->remove();
               ralloc_free(entry);
               continue;
            }
         }

274
         for (int i = 0; i < 3; i++) {
275 276
            src_reg *src = &entry->generator->src[i];

277 278 279 280
            /* Kill all AEB entries that use the destination we just
             * overwrote.
             */
            if (inst->dst.file == entry->generator->src[i].file &&
281
                inst->dst.nr == entry->generator->src[i].nr) {
282 283 284 285
               entry->remove();
               ralloc_free(entry);
               break;
            }
286 287 288 289

            /* Kill any AEB entries using registers that don't get reused any
             * more -- a sure sign they'll fail operands_match().
             */
Matt Turner's avatar
Matt Turner committed
290
            if (src->file == VGRF) {
291
               if (var_range_end(var_from_reg(alloc, dst_reg(*src)), 8) < ip) {
292 293 294 295
                  entry->remove();
                  ralloc_free(entry);
                  break;
               }
296
            }
297 298
         }
      }
299 300

      ip++;
301 302 303 304 305 306 307 308 309 310 311 312
   }

   ralloc_free(cse_ctx);

   return progress;
}

bool
vec4_visitor::opt_cse()
{
   bool progress = false;

313 314
   calculate_live_intervals();

315
   foreach_block (block, cfg) {
316
      progress = opt_cse_local(block) || progress;
317 318
   }

319
   if (progress)
320
      invalidate_live_intervals();
321

322 323
   return progress;
}