Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  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. /** @file brw_fs_copy_propagation.cpp
  25.  *
  26.  * Support for global copy propagation in two passes: A local pass that does
  27.  * intra-block copy (and constant) propagation, and a global pass that uses
  28.  * dataflow analysis on the copies available at the end of each block to re-do
  29.  * local copy propagation with more copies available.
  30.  *
  31.  * See Muchnik's Advanced Compiler Design and Implementation, section
  32.  * 12.5 (p356).
  33.  */
  34.  
  35. #define ACP_HASH_SIZE 16
  36.  
  37. #include "main/bitset.h"
  38. #include "brw_fs.h"
  39. #include "brw_cfg.h"
  40.  
  41. namespace { /* avoid conflict with opt_copy_propagation_elements */
  42. struct acp_entry : public exec_node {
  43.    fs_reg dst;
  44.    fs_reg src;
  45. };
  46.  
  47. struct block_data {
  48.    /**
  49.     * Which entries in the fs_copy_prop_dataflow acp table are live at the
  50.     * start of this block.  This is the useful output of the analysis, since
  51.     * it lets us plug those into the local copy propagation on the second
  52.     * pass.
  53.     */
  54.    BITSET_WORD *livein;
  55.  
  56.    /**
  57.     * Which entries in the fs_copy_prop_dataflow acp table are live at the end
  58.     * of this block.  This is done in initial setup from the per-block acps
  59.     * returned by the first local copy prop pass.
  60.     */
  61.    BITSET_WORD *liveout;
  62.  
  63.    /**
  64.     * Which entries in the fs_copy_prop_dataflow acp table are killed over the
  65.     * course of this block.
  66.     */
  67.    BITSET_WORD *kill;
  68. };
  69.  
  70. class fs_copy_prop_dataflow
  71. {
  72. public:
  73.    fs_copy_prop_dataflow(void *mem_ctx, cfg_t *cfg,
  74.                          exec_list *out_acp[ACP_HASH_SIZE]);
  75.  
  76.    void setup_kills();
  77.    void run();
  78.  
  79.    void *mem_ctx;
  80.    cfg_t *cfg;
  81.  
  82.    acp_entry **acp;
  83.    int num_acp;
  84.    int bitset_words;
  85.  
  86.   struct block_data *bd;
  87. };
  88. } /* anonymous namespace */
  89.  
  90. fs_copy_prop_dataflow::fs_copy_prop_dataflow(void *mem_ctx, cfg_t *cfg,
  91.                                              exec_list *out_acp[ACP_HASH_SIZE])
  92.    : mem_ctx(mem_ctx), cfg(cfg)
  93. {
  94.    bd = rzalloc_array(mem_ctx, struct block_data, cfg->num_blocks);
  95.  
  96.    num_acp = 0;
  97.    for (int b = 0; b < cfg->num_blocks; b++) {
  98.       for (int i = 0; i < ACP_HASH_SIZE; i++) {
  99.          foreach_list(entry_node, &out_acp[b][i]) {
  100.             num_acp++;
  101.          }
  102.       }
  103.    }
  104.  
  105.    acp = rzalloc_array(mem_ctx, struct acp_entry *, num_acp);
  106.  
  107.    bitset_words = BITSET_WORDS(num_acp);
  108.  
  109.    int next_acp = 0;
  110.    for (int b = 0; b < cfg->num_blocks; b++) {
  111.       bd[b].livein = rzalloc_array(bd, BITSET_WORD, bitset_words);
  112.       bd[b].liveout = rzalloc_array(bd, BITSET_WORD, bitset_words);
  113.       bd[b].kill = rzalloc_array(bd, BITSET_WORD, bitset_words);
  114.  
  115.       for (int i = 0; i < ACP_HASH_SIZE; i++) {
  116.          foreach_list(entry_node, &out_acp[b][i]) {
  117.             acp_entry *entry = (acp_entry *)entry_node;
  118.  
  119.             acp[next_acp] = entry;
  120.             BITSET_SET(bd[b].liveout, next_acp);
  121.             next_acp++;
  122.          }
  123.       }
  124.    }
  125.  
  126.    assert(next_acp == num_acp);
  127.  
  128.    setup_kills();
  129.    run();
  130. }
  131.  
  132. /**
  133.  * Walk the set of instructions in the block, marking which entries in the acp
  134.  * are killed by the block.
  135.  */
  136. void
  137. fs_copy_prop_dataflow::setup_kills()
  138. {
  139.    for (int b = 0; b < cfg->num_blocks; b++) {
  140.       bblock_t *block = cfg->blocks[b];
  141.  
  142.       for (fs_inst *inst = (fs_inst *)block->start;
  143.            inst != block->end->next;
  144.            inst = (fs_inst *)inst->next) {
  145.          if (inst->dst.file != GRF)
  146.             continue;
  147.  
  148.          for (int i = 0; i < num_acp; i++) {
  149.             if (inst->overwrites_reg(acp[i]->dst) ||
  150.                 inst->overwrites_reg(acp[i]->src)) {
  151.                BITSET_SET(bd[b].kill, i);
  152.             }
  153.          }
  154.       }
  155.    }
  156. }
  157.  
  158. /**
  159.  * Walk the set of instructions in the block, marking which entries in the acp
  160.  * are killed by the block.
  161.  */
  162. void
  163. fs_copy_prop_dataflow::run()
  164. {
  165.    bool cont = true;
  166.  
  167.    while (cont) {
  168.       cont = false;
  169.  
  170.       for (int b = 0; b < cfg->num_blocks; b++) {
  171.          for (int i = 0; i < bitset_words; i++) {
  172.             BITSET_WORD new_liveout = (bd[b].livein[i] &
  173.                                        ~bd[b].kill[i] &
  174.                                        ~bd[b].liveout[i]);
  175.             if (new_liveout) {
  176.                bd[b].liveout[i] |= new_liveout;
  177.                cont = true;
  178.             }
  179.  
  180.             /* Update livein: if it's live at the end of all parents, it's
  181.              * live at our start.
  182.              */
  183.             BITSET_WORD new_livein = ~bd[b].livein[i];
  184.             foreach_list(block_node, &cfg->blocks[b]->parents) {
  185.                bblock_link *link = (bblock_link *)block_node;
  186.                bblock_t *block = link->block;
  187.                new_livein &= bd[block->block_num].liveout[i];
  188.                if (!new_livein)
  189.                   break;
  190.             }
  191.             if (new_livein) {
  192.                bd[b].livein[i] |= new_livein;
  193.                cont = true;
  194.             }
  195.          }
  196.       }
  197.    }
  198. }
  199.  
  200. bool
  201. fs_visitor::try_copy_propagate(fs_inst *inst, int arg, acp_entry *entry)
  202. {
  203.    if (entry->src.file == IMM)
  204.       return false;
  205.  
  206.    if (inst->src[arg].file != entry->dst.file ||
  207.        inst->src[arg].reg != entry->dst.reg ||
  208.        inst->src[arg].reg_offset != entry->dst.reg_offset) {
  209.       return false;
  210.    }
  211.  
  212.    /* See resolve_ud_negate() and comment in brw_fs_emit.cpp. */
  213.    if (inst->conditional_mod &&
  214.        inst->src[arg].type == BRW_REGISTER_TYPE_UD &&
  215.        entry->src.negate)
  216.       return false;
  217.  
  218.    bool has_source_modifiers = entry->src.abs || entry->src.negate;
  219.  
  220.    if ((has_source_modifiers || entry->src.file == UNIFORM ||
  221.         entry->src.smear != -1) && !can_do_source_mods(inst))
  222.       return false;
  223.  
  224.    if (has_source_modifiers && entry->dst.type != inst->src[arg].type)
  225.       return false;
  226.  
  227.    inst->src[arg].file = entry->src.file;
  228.    inst->src[arg].reg = entry->src.reg;
  229.    inst->src[arg].reg_offset = entry->src.reg_offset;
  230.    if (entry->src.smear != -1)
  231.       inst->src[arg].smear = entry->src.smear;
  232.  
  233.    if (!inst->src[arg].abs) {
  234.       inst->src[arg].abs = entry->src.abs;
  235.       inst->src[arg].negate ^= entry->src.negate;
  236.    }
  237.  
  238.    return true;
  239. }
  240.  
  241.  
  242. bool
  243. fs_visitor::try_constant_propagate(fs_inst *inst, acp_entry *entry)
  244. {
  245.    bool progress = false;
  246.  
  247.    if (entry->src.file != IMM)
  248.       return false;
  249.  
  250.    for (int i = 2; i >= 0; i--) {
  251.       if (inst->src[i].file != entry->dst.file ||
  252.           inst->src[i].reg != entry->dst.reg ||
  253.           inst->src[i].reg_offset != entry->dst.reg_offset)
  254.          continue;
  255.  
  256.       /* Don't bother with cases that should have been taken care of by the
  257.        * GLSL compiler's constant folding pass.
  258.        */
  259.       if (inst->src[i].negate || inst->src[i].abs)
  260.          continue;
  261.  
  262.       switch (inst->opcode) {
  263.       case BRW_OPCODE_MOV:
  264.          inst->src[i] = entry->src;
  265.          progress = true;
  266.          break;
  267.  
  268.       case BRW_OPCODE_MACH:
  269.       case BRW_OPCODE_MUL:
  270.       case BRW_OPCODE_ADD:
  271.          if (i == 1) {
  272.             inst->src[i] = entry->src;
  273.             progress = true;
  274.          } else if (i == 0 && inst->src[1].file != IMM) {
  275.             /* Fit this constant in by commuting the operands.
  276.              * Exception: we can't do this for 32-bit integer MUL/MACH
  277.              * because it's asymmetric.
  278.              */
  279.             if ((inst->opcode == BRW_OPCODE_MUL ||
  280.                  inst->opcode == BRW_OPCODE_MACH) &&
  281.                 (inst->src[1].type == BRW_REGISTER_TYPE_D ||
  282.                  inst->src[1].type == BRW_REGISTER_TYPE_UD))
  283.                break;
  284.             inst->src[0] = inst->src[1];
  285.             inst->src[1] = entry->src;
  286.             progress = true;
  287.          }
  288.          break;
  289.  
  290.       case BRW_OPCODE_CMP:
  291.       case BRW_OPCODE_IF:
  292.          if (i == 1) {
  293.             inst->src[i] = entry->src;
  294.             progress = true;
  295.          } else if (i == 0 && inst->src[1].file != IMM) {
  296.             uint32_t new_cmod;
  297.  
  298.             new_cmod = brw_swap_cmod(inst->conditional_mod);
  299.             if (new_cmod != ~0u) {
  300.                /* Fit this constant in by swapping the operands and
  301.                 * flipping the test
  302.                 */
  303.                inst->src[0] = inst->src[1];
  304.                inst->src[1] = entry->src;
  305.                inst->conditional_mod = new_cmod;
  306.                progress = true;
  307.             }
  308.          }
  309.          break;
  310.  
  311.       case BRW_OPCODE_SEL:
  312.          if (i == 1) {
  313.             inst->src[i] = entry->src;
  314.             progress = true;
  315.          } else if (i == 0 && inst->src[1].file != IMM) {
  316.             inst->src[0] = inst->src[1];
  317.             inst->src[1] = entry->src;
  318.  
  319.             /* If this was predicated, flipping operands means
  320.              * we also need to flip the predicate.
  321.              */
  322.             if (inst->conditional_mod == BRW_CONDITIONAL_NONE) {
  323.                inst->predicate_inverse =
  324.                   !inst->predicate_inverse;
  325.             }
  326.             progress = true;
  327.          }
  328.          break;
  329.  
  330.       case SHADER_OPCODE_RCP:
  331.          /* The hardware doesn't do math on immediate values
  332.           * (because why are you doing that, seriously?), but
  333.           * the correct answer is to just constant fold it
  334.           * anyway.
  335.           */
  336.          assert(i == 0);
  337.          if (inst->src[0].imm.f != 0.0f) {
  338.             inst->opcode = BRW_OPCODE_MOV;
  339.             inst->src[0] = entry->src;
  340.             inst->src[0].imm.f = 1.0f / inst->src[0].imm.f;
  341.             progress = true;
  342.          }
  343.          break;
  344.  
  345.       case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
  346.          inst->src[i] = entry->src;
  347.          progress = true;
  348.          break;
  349.  
  350.       default:
  351.          break;
  352.       }
  353.    }
  354.  
  355.    return progress;
  356. }
  357. /* Walks a basic block and does copy propagation on it using the acp
  358.  * list.
  359.  */
  360. bool
  361. fs_visitor::opt_copy_propagate_local(void *mem_ctx, bblock_t *block,
  362.                                      exec_list *acp)
  363. {
  364.    bool progress = false;
  365.  
  366.    for (fs_inst *inst = (fs_inst *)block->start;
  367.         inst != block->end->next;
  368.         inst = (fs_inst *)inst->next) {
  369.  
  370.       /* Try propagating into this instruction. */
  371.       for (int i = 0; i < 3; i++) {
  372.          if (inst->src[i].file != GRF)
  373.             continue;
  374.  
  375.          foreach_list(entry_node, &acp[inst->src[i].reg % ACP_HASH_SIZE]) {
  376.             acp_entry *entry = (acp_entry *)entry_node;
  377.  
  378.             if (try_constant_propagate(inst, entry))
  379.                progress = true;
  380.  
  381.             if (try_copy_propagate(inst, i, entry))
  382.                progress = true;
  383.          }
  384.       }
  385.  
  386.       /* kill the destination from the ACP */
  387.       if (inst->dst.file == GRF) {
  388.          foreach_list_safe(entry_node, &acp[inst->dst.reg % ACP_HASH_SIZE]) {
  389.             acp_entry *entry = (acp_entry *)entry_node;
  390.  
  391.             if (inst->overwrites_reg(entry->dst)) {
  392.                entry->remove();
  393.             }
  394.          }
  395.  
  396.          /* Oops, we only have the chaining hash based on the destination, not
  397.           * the source, so walk across the entire table.
  398.           */
  399.          for (int i = 0; i < ACP_HASH_SIZE; i++) {
  400.             foreach_list_safe(entry_node, &acp[i]) {
  401.                acp_entry *entry = (acp_entry *)entry_node;
  402.                if (inst->overwrites_reg(entry->src))
  403.                   entry->remove();
  404.             }
  405.          }
  406.       }
  407.  
  408.       /* If this instruction's source could potentially be folded into the
  409.        * operand of another instruction, add it to the ACP.
  410.        */
  411.       if (inst->opcode == BRW_OPCODE_MOV &&
  412.           inst->dst.file == GRF &&
  413.           ((inst->src[0].file == GRF &&
  414.             (inst->src[0].reg != inst->dst.reg ||
  415.              inst->src[0].reg_offset != inst->dst.reg_offset)) ||
  416.            inst->src[0].file == UNIFORM ||
  417.            inst->src[0].file == IMM) &&
  418.           inst->src[0].type == inst->dst.type &&
  419.           !inst->saturate &&
  420.           !inst->is_partial_write()) {
  421.          acp_entry *entry = ralloc(mem_ctx, acp_entry);
  422.          entry->dst = inst->dst;
  423.          entry->src = inst->src[0];
  424.          acp[entry->dst.reg % ACP_HASH_SIZE].push_tail(entry);
  425.       }
  426.    }
  427.  
  428.    return progress;
  429. }
  430.  
  431. bool
  432. fs_visitor::opt_copy_propagate()
  433. {
  434.    bool progress = false;
  435.    void *mem_ctx = ralloc_context(this->mem_ctx);
  436.    cfg_t cfg(this);
  437.    exec_list *out_acp[cfg.num_blocks];
  438.    for (int i = 0; i < cfg.num_blocks; i++)
  439.       out_acp[i] = new exec_list [ACP_HASH_SIZE];
  440.  
  441.    /* First, walk through each block doing local copy propagation and getting
  442.     * the set of copies available at the end of the block.
  443.     */
  444.    for (int b = 0; b < cfg.num_blocks; b++) {
  445.       bblock_t *block = cfg.blocks[b];
  446.  
  447.       progress = opt_copy_propagate_local(mem_ctx, block,
  448.                                           out_acp[b]) || progress;
  449.    }
  450.  
  451.    /* Do dataflow analysis for those available copies. */
  452.    fs_copy_prop_dataflow dataflow(mem_ctx, &cfg, out_acp);
  453.  
  454.    /* Next, re-run local copy propagation, this time with the set of copies
  455.     * provided by the dataflow analysis available at the start of a block.
  456.     */
  457.    for (int b = 0; b < cfg.num_blocks; b++) {
  458.       bblock_t *block = cfg.blocks[b];
  459.       exec_list in_acp[ACP_HASH_SIZE];
  460.  
  461.       for (int i = 0; i < dataflow.num_acp; i++) {
  462.          if (BITSET_TEST(dataflow.bd[b].livein, i)) {
  463.             struct acp_entry *entry = dataflow.acp[i];
  464.             in_acp[entry->dst.reg % ACP_HASH_SIZE].push_tail(entry);
  465.          }
  466.       }
  467.  
  468.       progress = opt_copy_propagate_local(mem_ctx, block, in_acp) || progress;
  469.    }
  470.  
  471.    for (int i = 0; i < cfg.num_blocks; i++)
  472.       delete [] out_acp[i];
  473.    ralloc_free(mem_ctx);
  474.  
  475.    if (progress)
  476.       live_intervals_valid = false;
  477.  
  478.    return progress;
  479. }
  480.