Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2013 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_sel_peephole.cpp
  28.  *
  29.  * This file contains the opt_peephole_sel() optimization pass that replaces
  30.  * MOV instructions to the same destination in the "then" and "else" bodies of
  31.  * an if statement with SEL instructions.
  32.  */
  33.  
  34. /* Four MOVs seems to be pretty typical, so I picked the next power of two in
  35.  * the hopes that it would handle almost anything possible in a single
  36.  * pass.
  37.  */
  38. #define MAX_MOVS 8 /**< The maximum number of MOVs to attempt to match. */
  39.  
  40. /**
  41.  * Scans forwards from an IF counting consecutive MOV instructions in the
  42.  * "then" and "else" blocks of the if statement.
  43.  *
  44.  * A pointer to the bblock_t following the IF is passed as the <then_block>
  45.  * argument. The function stores pointers to the MOV instructions in the
  46.  * <then_mov> and <else_mov> arrays.
  47.  *
  48.  * \return the minimum number of MOVs found in the two branches or zero if
  49.  *         an error occurred.
  50.  *
  51.  * E.g.:
  52.  *                  IF ...
  53.  *    then_mov[0] = MOV g4, ...
  54.  *    then_mov[1] = MOV g5, ...
  55.  *    then_mov[2] = MOV g6, ...
  56.  *                  ELSE ...
  57.  *    else_mov[0] = MOV g4, ...
  58.  *    else_mov[1] = MOV g5, ...
  59.  *    else_mov[2] = MOV g7, ...
  60.  *                  ENDIF
  61.  *    returns 3.
  62.  */
  63. static int
  64. count_movs_from_if(fs_inst *then_mov[MAX_MOVS], fs_inst *else_mov[MAX_MOVS],
  65.                    bblock_t *then_block, bblock_t *else_block)
  66. {
  67.    int then_movs = 0;
  68.    foreach_inst_in_block(fs_inst, inst, then_block) {
  69.       if (then_movs == MAX_MOVS || inst->opcode != BRW_OPCODE_MOV)
  70.          break;
  71.  
  72.       then_mov[then_movs] = inst;
  73.       then_movs++;
  74.    }
  75.  
  76.    int else_movs = 0;
  77.    foreach_inst_in_block(fs_inst, inst, else_block) {
  78.       if (else_movs == MAX_MOVS || inst->opcode != BRW_OPCODE_MOV)
  79.          break;
  80.  
  81.       else_mov[else_movs] = inst;
  82.       else_movs++;
  83.    }
  84.  
  85.    return MIN2(then_movs, else_movs);
  86. }
  87.  
  88. /**
  89.  * Try to replace IF/MOV+/ELSE/MOV+/ENDIF with SEL.
  90.  *
  91.  * Many GLSL shaders contain the following pattern:
  92.  *
  93.  *    x = condition ? foo : bar
  94.  *
  95.  * or
  96.  *
  97.  *    if (...) a.xyzw = foo.xyzw;
  98.  *    else     a.xyzw = bar.xyzw;
  99.  *
  100.  * The compiler emits an ir_if tree for this, since each subexpression might be
  101.  * a complex tree that could have side-effects or short-circuit logic.
  102.  *
  103.  * However, the common case is to simply select one of two constants or
  104.  * variable values---which is exactly what SEL is for.  In this case, the
  105.  * assembly looks like:
  106.  *
  107.  *    (+f0) IF
  108.  *    MOV dst src0
  109.  *    ...
  110.  *    ELSE
  111.  *    MOV dst src1
  112.  *    ...
  113.  *    ENDIF
  114.  *
  115.  * where each pair of MOVs to a common destination and can be easily translated
  116.  * into
  117.  *
  118.  *    (+f0) SEL dst src0 src1
  119.  *
  120.  * If src0 is an immediate value, we promote it to a temporary GRF.
  121.  */
  122. bool
  123. fs_visitor::opt_peephole_sel()
  124. {
  125.    bool progress = false;
  126.  
  127.    foreach_block (block, cfg) {
  128.       /* IF instructions, by definition, can only be found at the ends of
  129.        * basic blocks.
  130.        */
  131.       fs_inst *if_inst = (fs_inst *)block->end();
  132.       if (if_inst->opcode != BRW_OPCODE_IF)
  133.          continue;
  134.  
  135.       fs_inst *else_mov[MAX_MOVS] = { NULL };
  136.       fs_inst *then_mov[MAX_MOVS] = { NULL };
  137.  
  138.       bblock_t *then_block = block->next();
  139.       bblock_t *else_block = NULL;
  140.       foreach_list_typed(bblock_link, child, link, &block->children) {
  141.          if (child->block != then_block) {
  142.             if (child->block->prev()->end()->opcode == BRW_OPCODE_ELSE) {
  143.                else_block = child->block;
  144.             }
  145.             break;
  146.          }
  147.       }
  148.       if (else_block == NULL)
  149.          continue;
  150.  
  151.       int movs = count_movs_from_if(then_mov, else_mov, then_block, else_block);
  152.  
  153.       if (movs == 0)
  154.          continue;
  155.  
  156.       fs_inst *sel_inst[MAX_MOVS] = { NULL };
  157.       fs_inst *mov_imm_inst[MAX_MOVS] = { NULL };
  158.  
  159.       enum brw_predicate predicate;
  160.       bool predicate_inverse;
  161.       if (devinfo->gen == 6 && if_inst->conditional_mod) {
  162.          /* For Sandybridge with IF with embedded comparison */
  163.          predicate = BRW_PREDICATE_NORMAL;
  164.          predicate_inverse = false;
  165.       } else {
  166.          /* Separate CMP and IF instructions */
  167.          predicate = if_inst->predicate;
  168.          predicate_inverse = if_inst->predicate_inverse;
  169.       }
  170.  
  171.       /* Generate SEL instructions for pairs of MOVs to a common destination. */
  172.       for (int i = 0; i < movs; i++) {
  173.          if (!then_mov[i] || !else_mov[i])
  174.             break;
  175.  
  176.          /* Check that the MOVs are the right form. */
  177.          if (!then_mov[i]->dst.equals(else_mov[i]->dst) ||
  178.              then_mov[i]->is_partial_write() ||
  179.              else_mov[i]->is_partial_write() ||
  180.              then_mov[i]->conditional_mod != BRW_CONDITIONAL_NONE ||
  181.              else_mov[i]->conditional_mod != BRW_CONDITIONAL_NONE) {
  182.             movs = i;
  183.             break;
  184.          }
  185.  
  186.          /* Check that source types for mov operations match. */
  187.          if (then_mov[i]->src[0].type != else_mov[i]->src[0].type) {
  188.             movs = i;
  189.             break;
  190.          }
  191.  
  192.          if (then_mov[i]->src[0].equals(else_mov[i]->src[0])) {
  193.             sel_inst[i] = MOV(then_mov[i]->dst, then_mov[i]->src[0]);
  194.          } else {
  195.             /* Only the last source register can be a constant, so if the MOV
  196.              * in the "then" clause uses a constant, we need to put it in a
  197.              * temporary.
  198.              */
  199.             fs_reg src0(then_mov[i]->src[0]);
  200.             if (src0.file == IMM) {
  201.                src0 = vgrf(glsl_type::float_type);
  202.                src0.type = then_mov[i]->src[0].type;
  203.                mov_imm_inst[i] = MOV(src0, then_mov[i]->src[0]);
  204.             }
  205.  
  206.             sel_inst[i] = SEL(then_mov[i]->dst, src0, else_mov[i]->src[0]);
  207.             sel_inst[i]->predicate = predicate;
  208.             sel_inst[i]->predicate_inverse = predicate_inverse;
  209.          }
  210.       }
  211.  
  212.       if (movs == 0)
  213.          continue;
  214.  
  215.       /* Emit a CMP if our IF used the embedded comparison */
  216.       if (devinfo->gen == 6 && if_inst->conditional_mod) {
  217.          fs_inst *cmp_inst = CMP(reg_null_d, if_inst->src[0], if_inst->src[1],
  218.                                  if_inst->conditional_mod);
  219.          if_inst->insert_before(block, cmp_inst);
  220.       }
  221.  
  222.       for (int i = 0; i < movs; i++) {
  223.          if (mov_imm_inst[i])
  224.             if_inst->insert_before(block, mov_imm_inst[i]);
  225.          if_inst->insert_before(block, sel_inst[i]);
  226.  
  227.          then_mov[i]->remove(then_block);
  228.          else_mov[i]->remove(else_block);
  229.       }
  230.  
  231.       progress = true;
  232.    }
  233.  
  234.    if (progress)
  235.       invalidate_live_intervals();
  236.  
  237.    return progress;
  238. }
  239.