Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
  2.  
  3. /*
  4.  * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice (including the next
  14.  * paragraph) shall be included in all copies or substantial portions of the
  15.  * Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  20.  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23.  * SOFTWARE.
  24.  *
  25.  * Authors:
  26.  *    Rob Clark <robclark@freedesktop.org>
  27.  */
  28.  
  29. #include "freedreno_util.h"
  30.  
  31. #include "ir3.h"
  32.  
  33. /*
  34.  * Copy Propagate:
  35.  */
  36.  
  37. /* is it a type preserving mov, with ok flags? */
  38. static bool is_eligible_mov(struct ir3_instruction *instr, bool allow_flags)
  39. {
  40.         if (is_same_type_mov(instr)) {
  41.                 struct ir3_register *dst = instr->regs[0];
  42.                 struct ir3_register *src = instr->regs[1];
  43.                 struct ir3_instruction *src_instr = ssa(src);
  44.                 if (dst->flags & (IR3_REG_ADDR | IR3_REG_RELATIV))
  45.                         return false;
  46.                 if (src->flags & IR3_REG_RELATIV)
  47.                         return false;
  48.                 if (!allow_flags)
  49.                         if (src->flags & (IR3_REG_FABS | IR3_REG_FNEG |
  50.                                         IR3_REG_SABS | IR3_REG_SNEG | IR3_REG_BNOT))
  51.                                 return false;
  52.                 if (!src_instr)
  53.                         return false;
  54.                 /* TODO: remove this hack: */
  55.                 if (is_meta(src_instr) && (src_instr->opc == OPC_META_FO))
  56.                         return false;
  57.                 return true;
  58.         }
  59.         return false;
  60. }
  61.  
  62. static unsigned cp_flags(unsigned flags)
  63. {
  64.         /* only considering these flags (at least for now): */
  65.         flags &= (IR3_REG_CONST | IR3_REG_IMMED |
  66.                         IR3_REG_FNEG | IR3_REG_FABS |
  67.                         IR3_REG_SNEG | IR3_REG_SABS |
  68.                         IR3_REG_BNOT | IR3_REG_RELATIV);
  69.         return flags;
  70. }
  71.  
  72. static bool valid_flags(struct ir3_instruction *instr, unsigned n,
  73.                 unsigned flags)
  74. {
  75.         unsigned valid_flags;
  76.         flags = cp_flags(flags);
  77.  
  78.         /* clear flags that are 'ok' */
  79.         switch (instr->category) {
  80.         case 1:
  81.                 valid_flags = IR3_REG_IMMED | IR3_REG_RELATIV;
  82.                 if (flags & ~valid_flags)
  83.                         return false;
  84.                 break;
  85.         case 5:
  86.                 /* no flags allowed */
  87.                 if (flags)
  88.                         return false;
  89.                 break;
  90.         case 6:
  91.                 valid_flags = IR3_REG_IMMED;
  92.                 if (flags & ~valid_flags)
  93.                         return false;
  94.                 break;
  95.         case 2:
  96.                 valid_flags = ir3_cat2_absneg(instr->opc) |
  97.                                 IR3_REG_CONST | IR3_REG_RELATIV;
  98.  
  99.                 if (ir3_cat2_int(instr->opc))
  100.                         valid_flags |= IR3_REG_IMMED;
  101.  
  102.                 if (flags & ~valid_flags)
  103.                         return false;
  104.  
  105.                 if (flags & (IR3_REG_CONST | IR3_REG_IMMED)) {
  106.                         unsigned m = (n ^ 1) + 1;
  107.                         /* cannot deal w/ const in both srcs:
  108.                          * (note that some cat2 actually only have a single src)
  109.                          */
  110.                         if (m < instr->regs_count) {
  111.                                 struct ir3_register *reg = instr->regs[m];
  112.                                 if ((flags & IR3_REG_CONST) && (reg->flags & IR3_REG_CONST))
  113.                                         return false;
  114.                                 if ((flags & IR3_REG_IMMED) && (reg->flags & IR3_REG_IMMED))
  115.                                         return false;
  116.                         }
  117.                         /* cannot be const + ABS|NEG: */
  118.                         if (flags & (IR3_REG_FABS | IR3_REG_FNEG |
  119.                                         IR3_REG_SABS | IR3_REG_SNEG | IR3_REG_BNOT))
  120.                                 return false;
  121.                 }
  122.                 break;
  123.         case 3:
  124.                 valid_flags = ir3_cat3_absneg(instr->opc) |
  125.                                 IR3_REG_CONST | IR3_REG_RELATIV;
  126.  
  127.                 if (flags & ~valid_flags)
  128.                         return false;
  129.  
  130.                 if (flags & (IR3_REG_CONST | IR3_REG_RELATIV)) {
  131.                         /* cannot deal w/ const/relativ in 2nd src: */
  132.                         if (n == 1)
  133.                                 return false;
  134.                 }
  135.  
  136.                 if (flags & IR3_REG_CONST) {
  137.                         /* cannot be const + ABS|NEG: */
  138.                         if (flags & (IR3_REG_FABS | IR3_REG_FNEG |
  139.                                         IR3_REG_SABS | IR3_REG_SNEG | IR3_REG_BNOT))
  140.                                 return false;
  141.                 }
  142.                 break;
  143.         case 4:
  144.                 /* seems like blob compiler avoids const as src.. */
  145.                 /* TODO double check if this is still the case on a4xx */
  146.                 if (flags & IR3_REG_CONST)
  147.                         return false;
  148.                 if (flags & (IR3_REG_SABS | IR3_REG_SNEG))
  149.                         return false;
  150.                 break;
  151.         }
  152.  
  153.         return true;
  154. }
  155.  
  156. /* propagate register flags from src to dst.. negates need special
  157.  * handling to cancel each other out.
  158.  */
  159. static void combine_flags(unsigned *dstflags, unsigned srcflags)
  160. {
  161.         /* if what we are combining into already has (abs) flags,
  162.          * we can drop (neg) from src:
  163.          */
  164.         if (*dstflags & IR3_REG_FABS)
  165.                 srcflags &= ~IR3_REG_FNEG;
  166.         if (*dstflags & IR3_REG_SABS)
  167.                 srcflags &= ~IR3_REG_SNEG;
  168.  
  169.         if (srcflags & IR3_REG_FABS)
  170.                 *dstflags |= IR3_REG_FABS;
  171.         if (srcflags & IR3_REG_SABS)
  172.                 *dstflags |= IR3_REG_SABS;
  173.         if (srcflags & IR3_REG_FNEG)
  174.                 *dstflags ^= IR3_REG_FNEG;
  175.         if (srcflags & IR3_REG_SNEG)
  176.                 *dstflags ^= IR3_REG_SNEG;
  177.         if (srcflags & IR3_REG_BNOT)
  178.                 *dstflags ^= IR3_REG_BNOT;
  179. }
  180.  
  181. static struct ir3_instruction * instr_cp(struct ir3_instruction *instr, unsigned *flags);
  182.  
  183. /* the "plain" MAD's (ie. the ones that don't shift first src prior to
  184.  * multiply) can swap their first two srcs if src[0] is !CONST and
  185.  * src[1] is CONST:
  186.  */
  187. static bool is_valid_mad(struct ir3_instruction *instr)
  188. {
  189.         return (instr->category == 3) && is_mad(instr->opc);
  190. }
  191.  
  192. /**
  193.  * Handle cp for a given src register.  This additionally handles
  194.  * the cases of collapsing immedate/const (which replace the src
  195.  * register with a non-ssa src) or collapsing mov's from relative
  196.  * src (which needs to also fixup the address src reference by the
  197.  * instruction).
  198.  */
  199. static void
  200. reg_cp(struct ir3_instruction *instr, struct ir3_register *reg, unsigned n)
  201. {
  202.         unsigned src_flags = 0, new_flags;
  203.         struct ir3_instruction *src_instr;
  204.  
  205.         if (is_meta(instr)) {
  206.                 /* meta instructions cannot fold up register
  207.                  * flags.. they are usually src for texture
  208.                  * fetch, etc, where we cannot specify abs/neg
  209.                  */
  210.                 reg->instr = instr_cp(reg->instr, NULL);
  211.                 return;
  212.         }
  213.  
  214.         src_instr = instr_cp(reg->instr, &src_flags);
  215.  
  216.         new_flags = reg->flags;
  217.         combine_flags(&new_flags, src_flags);
  218.  
  219.         reg->flags = new_flags;
  220.         reg->instr = src_instr;
  221.  
  222.         if (!valid_flags(instr, n, reg->flags)) {
  223.                 /* insert an absneg.f */
  224.                 if (reg->flags & (IR3_REG_SNEG | IR3_REG_SABS | IR3_REG_BNOT)) {
  225.                         debug_assert(!(reg->flags & (IR3_REG_FNEG | IR3_REG_FABS)));
  226.                         reg->instr = ir3_ABSNEG_S(instr->block,
  227.                                         reg->instr, cp_flags(src_flags));
  228.                 } else {
  229.                         debug_assert(!(reg->flags & (IR3_REG_SNEG | IR3_REG_SABS | IR3_REG_BNOT)));
  230.                         reg->instr = ir3_ABSNEG_F(instr->block,
  231.                                         reg->instr, cp_flags(src_flags));
  232.                 }
  233.                 reg->flags &= ~cp_flags(src_flags);
  234.                 debug_assert(valid_flags(instr, n, reg->flags));
  235.                 /* send it through instr_cp() again since
  236.                  * the absneg src might be a mov from const
  237.                  * that could be cleaned up:
  238.                  */
  239.                 reg->instr = instr_cp(reg->instr, NULL);
  240.                 return;
  241.         }
  242.  
  243.         if (is_same_type_mov(reg->instr)) {
  244.                 struct ir3_register *src_reg = reg->instr->regs[1];
  245.                 unsigned new_flags = src_reg->flags;
  246.  
  247.                 combine_flags(&new_flags, reg->flags);
  248.  
  249.                 if (!valid_flags(instr, n, new_flags)) {
  250.                         /* special case for "normal" mad instructions, we can
  251.                          * try swapping the first two args if that fits better.
  252.                          */
  253.                         if ((n == 1) && is_valid_mad(instr) &&
  254.                                         !(instr->regs[0 + 1]->flags & (IR3_REG_CONST | IR3_REG_RELATIV)) &&
  255.                                         valid_flags(instr, 0, new_flags)) {
  256.                                 /* swap src[0] and src[1]: */
  257.                                 struct ir3_register *tmp;
  258.                                 tmp = instr->regs[0 + 1];
  259.                                 instr->regs[0 + 1] = instr->regs[1 + 1];
  260.                                 instr->regs[1 + 1] = tmp;
  261.                                 n = 0;
  262.                         } else {
  263.                                 return;
  264.                         }
  265.                 }
  266.  
  267.                 /* Here we handle the special case of mov from
  268.                  * CONST and/or RELATIV.  These need to be handled
  269.                  * specially, because in the case of move from CONST
  270.                  * there is no src ir3_instruction so we need to
  271.                  * replace the ir3_register.  And in the case of
  272.                  * RELATIV we need to handle the address register
  273.                  * dependency.
  274.                  */
  275.                 if (src_reg->flags & IR3_REG_CONST) {
  276.                         /* an instruction cannot reference two different
  277.                          * address registers:
  278.                          */
  279.                         if ((src_reg->flags & IR3_REG_RELATIV) &&
  280.                                         conflicts(instr->address, reg->instr->address))
  281.                                 return;
  282.  
  283.                         src_reg->flags = new_flags;
  284.                         instr->regs[n+1] = src_reg;
  285.  
  286.                         if (src_reg->flags & IR3_REG_RELATIV)
  287.                                 instr->address = reg->instr->address;
  288.  
  289.                         return;
  290.                 }
  291.  
  292.                 if ((src_reg->flags & IR3_REG_RELATIV) &&
  293.                                 !conflicts(instr->address, reg->instr->address)) {
  294.                         src_reg->flags = new_flags;
  295.                         instr->regs[n+1] = src_reg;
  296.                         instr->address = reg->instr->address;
  297.  
  298.                         return;
  299.                 }
  300.  
  301.                 /* NOTE: seems we can only do immed integers, so don't
  302.                  * need to care about float.  But we do need to handle
  303.                  * abs/neg *before* checking that the immediate requires
  304.                  * few enough bits to encode:
  305.                  *
  306.                  * TODO: do we need to do something to avoid accidentally
  307.                  * catching a float immed?
  308.                  */
  309.                 if (src_reg->flags & IR3_REG_IMMED) {
  310.                         int32_t iim_val = src_reg->iim_val;
  311.  
  312.                         debug_assert((instr->category == 1) ||
  313.                                         (instr->category == 6) ||
  314.                                         ((instr->category == 2) &&
  315.                                                 ir3_cat2_int(instr->opc)));
  316.  
  317.                         if (new_flags & IR3_REG_SABS)
  318.                                 iim_val = abs(iim_val);
  319.  
  320.                         if (new_flags & IR3_REG_SNEG)
  321.                                 iim_val = -iim_val;
  322.  
  323.                         if (new_flags & IR3_REG_BNOT)
  324.                                 iim_val = ~iim_val;
  325.  
  326.                         if (!(iim_val & ~0x3ff)) {
  327.                                 new_flags &= ~(IR3_REG_SABS | IR3_REG_SNEG | IR3_REG_BNOT);
  328.                                 src_reg->flags = new_flags;
  329.                                 src_reg->iim_val = iim_val;
  330.                                 instr->regs[n+1] = src_reg;
  331.                         }
  332.  
  333.                         return;
  334.                 }
  335.         }
  336. }
  337.  
  338. /**
  339.  * Given an SSA src (instruction), return the one with extraneous
  340.  * mov's removed, ie, for (to copy NIR syntax):
  341.  *
  342.  *   vec1 ssa1 = fadd <something>, <somethingelse>
  343.  *   vec1 ssa2 = fabs ssa1
  344.  *   vec1 ssa3 = fneg ssa1
  345.  *
  346.  * then calling instr_cp(ssa3, &flags) would return ssa1 with
  347.  * (IR3_REG_ABS | IR3_REG_NEGATE) in flags.  If flags is NULL,
  348.  * then disallow eliminating copies which would require flag
  349.  * propagation (for example, we cannot propagate abs/neg into
  350.  * an output).
  351.  */
  352. static struct ir3_instruction *
  353. instr_cp(struct ir3_instruction *instr, unsigned *flags)
  354. {
  355.         struct ir3_register *reg;
  356.  
  357.         /* stay within the block.. don't try to operate across
  358.          * basic block boundaries or we'll have problems when
  359.          * dealing with multiple basic blocks:
  360.          */
  361.         if (is_meta(instr) && (instr->opc == OPC_META_INPUT))
  362.                 return instr;
  363.  
  364.         if (is_eligible_mov(instr, !!flags)) {
  365.                 struct ir3_register *reg = instr->regs[1];
  366.                 struct ir3_instruction *src_instr = ssa(reg);
  367.                 if (flags)
  368.                         combine_flags(flags, reg->flags);
  369.                 return instr_cp(src_instr, flags);
  370.         }
  371.  
  372.         /* Check termination condition before walking children (rather
  373.          * than before checking eligible-mov).  A mov instruction may
  374.          * appear as ssa-src for multiple other instructions, and we
  375.          * want to consider it for removal for each, rather than just
  376.          * the first one.  (But regardless of how many places it shows
  377.          * up as a src, we only need to recursively walk the children
  378.          * once.)
  379.          */
  380.         if (ir3_instr_check_mark(instr))
  381.                 return instr;
  382.  
  383.         /* walk down the graph from each src: */
  384.         foreach_src_n(reg, n, instr) {
  385.                 if (!(reg->flags & IR3_REG_SSA))
  386.                         continue;
  387.  
  388.                 reg_cp(instr, reg, n);
  389.         }
  390.  
  391.         if (instr->address)
  392.                 instr->address = instr_cp(instr->address, NULL);
  393.  
  394.         return instr;
  395. }
  396.  
  397. static void block_cp(struct ir3_block *block)
  398. {
  399.         unsigned i;
  400.  
  401.         for (i = 0; i < block->noutputs; i++) {
  402.                 if (block->outputs[i]) {
  403.                         struct ir3_instruction *out =
  404.                                         instr_cp(block->outputs[i], NULL);
  405.  
  406.                         block->outputs[i] = out;
  407.                 }
  408.         }
  409. }
  410.  
  411. void ir3_block_cp(struct ir3_block *block)
  412. {
  413.         ir3_clear_mark(block->shader);
  414.         block_cp(block);
  415. }
  416.