freedreno/ir3: add SSBO get_buffer_size() support
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_cp.c
index 2076b62acb8d3bf89b1dfc411556c00d98d3138a..8c907eb5a534d9be15b7d6849d6793b1c78d1741 100644 (file)
 #include "freedreno_util.h"
 
 #include "ir3.h"
+#include "ir3_shader.h"
 
 /*
  * Copy Propagate:
- *
  */
 
-static void block_cp(struct ir3_block *block);
-static struct ir3_instruction * instr_cp(struct ir3_instruction *instr, bool keep);
+struct ir3_cp_ctx {
+       struct ir3 *shader;
+       struct ir3_shader_variant *so;
+       unsigned immediate_idx;
+};
 
-/* XXX move this somewhere useful (and rename?) */
-static struct ir3_instruction *ssa(struct ir3_register *reg)
+/* is it a type preserving mov, with ok flags? */
+static bool is_eligible_mov(struct ir3_instruction *instr, bool allow_flags)
 {
-       if (reg->flags & IR3_REG_SSA)
-               return reg->instr;
-       return NULL;
-}
+       if (is_same_type_mov(instr)) {
+               struct ir3_register *dst = instr->regs[0];
+               struct ir3_register *src = instr->regs[1];
+               struct ir3_instruction *src_instr = ssa(src);
 
-static bool conflicts(struct ir3_instruction *a, struct ir3_instruction *b)
-{
-       return (a && b) && (a != b);
-}
+               /* only if mov src is SSA (not const/immed): */
+               if (!src_instr)
+                       return false;
 
-static void set_neighbors(struct ir3_instruction *instr,
-               struct ir3_instruction *left, struct ir3_instruction *right)
-{
-       debug_assert(!conflicts(instr->cp.left, left));
-       if (left) {
-               instr->cp.left_cnt++;
-               instr->cp.left = left;
-       }
-       debug_assert(!conflicts(instr->cp.right, right));
-       if (right) {
-               instr->cp.right_cnt++;
-               instr->cp.right = right;
+               /* no indirect: */
+               if (dst->flags & IR3_REG_RELATIV)
+                       return false;
+               if (src->flags & IR3_REG_RELATIV)
+                       return false;
+
+               if (!allow_flags)
+                       if (src->flags & (IR3_REG_FABS | IR3_REG_FNEG |
+                                       IR3_REG_SABS | IR3_REG_SNEG | IR3_REG_BNOT))
+                               return false;
+
+               /* TODO: remove this hack: */
+               if (src_instr->opc == OPC_META_FO)
+                       return false;
+               /* TODO: we currently don't handle left/right neighbors
+                * very well when inserting parallel-copies into phi..
+                * to avoid problems don't eliminate a mov coming out
+                * of phi..
+                */
+               if (src_instr->opc == OPC_META_PHI)
+                       return false;
+               return true;
        }
+       return false;
 }
 
-/* remove neighbor reference, clearing left/right neighbor ptrs when
- * there are no more references:
- */
-static void remove_neighbors(struct ir3_instruction *instr)
+static unsigned cp_flags(unsigned flags)
 {
-       if (instr->cp.left) {
-               if (--instr->cp.left_cnt == 0)
-                       instr->cp.left = NULL;
-       }
-       if (instr->cp.right) {
-               if (--instr->cp.right_cnt == 0)
-                       instr->cp.right = NULL;
-       }
+       /* only considering these flags (at least for now): */
+       flags &= (IR3_REG_CONST | IR3_REG_IMMED |
+                       IR3_REG_FNEG | IR3_REG_FABS |
+                       IR3_REG_SNEG | IR3_REG_SABS |
+                       IR3_REG_BNOT | IR3_REG_RELATIV);
+       return flags;
 }
 
-/* stop condition for iteration: */
-static bool check_stop(struct ir3_instruction *instr)
+static bool valid_flags(struct ir3_instruction *instr, unsigned n,
+               unsigned flags)
 {
-       if (ir3_instr_check_mark(instr))
-               return true;
+       unsigned valid_flags;
+       flags = cp_flags(flags);
 
-       /* stay within the block.. don't try to operate across
-        * basic block boundaries or we'll have problems when
-        * dealing with multiple basic blocks:
+       /* If destination is indirect, then source cannot be.. at least
+        * I don't think so..
         */
-       if (is_meta(instr) && (instr->opc == OPC_META_INPUT))
-               return true;
+       if ((instr->regs[0]->flags & IR3_REG_RELATIV) &&
+                       (flags & IR3_REG_RELATIV))
+               return false;
+
+       /* TODO it seems to *mostly* work to cp RELATIV, except we get some
+        * intermittent piglit variable-indexing fails.  Newer blob driver
+        * doesn't seem to cp these.  Possibly this is hw workaround?  Not
+        * sure, but until that is understood better, lets just switch off
+        * cp for indirect src's:
+        */
+       if (flags & IR3_REG_RELATIV)
+               return false;
 
-       return false;
-}
+       switch (opc_cat(instr->opc)) {
+       case 1:
+               valid_flags = IR3_REG_IMMED | IR3_REG_CONST | IR3_REG_RELATIV;
+               if (flags & ~valid_flags)
+                       return false;
+               break;
+       case 2:
+               valid_flags = ir3_cat2_absneg(instr->opc) |
+                               IR3_REG_CONST | IR3_REG_RELATIV;
 
-static bool is_eligible_mov(struct ir3_instruction *instr)
-{
-       if ((instr->category == 1) &&
-                       (instr->cat1.src_type == instr->cat1.dst_type)) {
-               struct ir3_register *dst = instr->regs[0];
-               struct ir3_register *src = instr->regs[1];
-               struct ir3_instruction *src_instr = ssa(src);
-               if (dst->flags & IR3_REG_ADDR)
+               if (ir3_cat2_int(instr->opc))
+                       valid_flags |= IR3_REG_IMMED;
+
+               if (flags & ~valid_flags)
                        return false;
-               /* TODO: propagate abs/neg modifiers if possible */
-               if (src->flags & (IR3_REG_ABS | IR3_REG_NEGATE | IR3_REG_RELATIV))
+
+               if (flags & (IR3_REG_CONST | IR3_REG_IMMED)) {
+                       unsigned m = (n ^ 1) + 1;
+                       /* cannot deal w/ const in both srcs:
+                        * (note that some cat2 actually only have a single src)
+                        */
+                       if (m < instr->regs_count) {
+                               struct ir3_register *reg = instr->regs[m];
+                               if ((flags & IR3_REG_CONST) && (reg->flags & IR3_REG_CONST))
+                                       return false;
+                               if ((flags & IR3_REG_IMMED) && (reg->flags & IR3_REG_IMMED))
+                                       return false;
+                       }
+                       /* cannot be const + ABS|NEG: */
+                       if (flags & (IR3_REG_FABS | IR3_REG_FNEG |
+                                       IR3_REG_SABS | IR3_REG_SNEG | IR3_REG_BNOT))
+                               return false;
+               }
+               break;
+       case 3:
+               valid_flags = ir3_cat3_absneg(instr->opc) |
+                               IR3_REG_CONST | IR3_REG_RELATIV;
+
+               if (flags & ~valid_flags)
                        return false;
-               if (src_instr) {
-                       /* check that eliminating the move won't result in
-                        * a neighbor conflict, ie. if an instruction feeds
-                        * into multiple fanins it can still only have at
-                        * most one left and one right neighbor:
+
+               if (flags & (IR3_REG_CONST | IR3_REG_RELATIV)) {
+                       /* cannot deal w/ const/relativ in 2nd src: */
+                       if (n == 1)
+                               return false;
+               }
+
+               if (flags & IR3_REG_CONST) {
+                       /* cannot be const + ABS|NEG: */
+                       if (flags & (IR3_REG_FABS | IR3_REG_FNEG |
+                                       IR3_REG_SABS | IR3_REG_SNEG | IR3_REG_BNOT))
+                               return false;
+               }
+               break;
+       case 4:
+               /* seems like blob compiler avoids const as src.. */
+               /* TODO double check if this is still the case on a4xx */
+               if (flags & (IR3_REG_CONST | IR3_REG_IMMED))
+                       return false;
+               if (flags & (IR3_REG_SABS | IR3_REG_SNEG))
+                       return false;
+               break;
+       case 5:
+               /* no flags allowed */
+               if (flags)
+                       return false;
+               break;
+       case 6:
+               valid_flags = IR3_REG_IMMED;
+               if (flags & ~valid_flags)
+                       return false;
+
+               if (flags & IR3_REG_IMMED) {
+                       /* doesn't seem like we can have immediate src for store
+                        * instructions:
+                        *
+                        * TODO this restriction could also apply to load instructions,
+                        * but for load instructions this arg is the address (and not
+                        * really sure any good way to test a hard-coded immed addr src)
                         */
-                       if (conflicts(instr->cp.left, src_instr->cp.left))
+                       if (is_store(instr) && (n == 1))
                                return false;
-                       if (conflicts(instr->cp.right, src_instr->cp.right))
+
+                       /* disallow CP into anything but the SSBO slot argument for
+                        * atomics:
+                        */
+                       if (is_atomic(instr->opc) && (n != 0))
                                return false;
-                       return true;
                }
+
+               break;
        }
-       return false;
+
+       return true;
 }
 
-static void walk_children(struct ir3_instruction *instr, bool keep)
+/* propagate register flags from src to dst.. negates need special
+ * handling to cancel each other out.
+ */
+static void combine_flags(unsigned *dstflags, struct ir3_instruction *src)
 {
-       unsigned i;
+       unsigned srcflags = src->regs[1]->flags;
 
-       /* walk down the graph from each src: */
-       for (i = 1; i < instr->regs_count; i++) {
-               struct ir3_register *src = instr->regs[i];
-               if (src->flags & IR3_REG_SSA)
-                       src->instr = instr_cp(src->instr, keep);
-       }
+       /* if what we are combining into already has (abs) flags,
+        * we can drop (neg) from src:
+        */
+       if (*dstflags & IR3_REG_FABS)
+               srcflags &= ~IR3_REG_FNEG;
+       if (*dstflags & IR3_REG_SABS)
+               srcflags &= ~IR3_REG_SNEG;
+
+       if (srcflags & IR3_REG_FABS)
+               *dstflags |= IR3_REG_FABS;
+       if (srcflags & IR3_REG_SABS)
+               *dstflags |= IR3_REG_SABS;
+       if (srcflags & IR3_REG_FNEG)
+               *dstflags ^= IR3_REG_FNEG;
+       if (srcflags & IR3_REG_SNEG)
+               *dstflags ^= IR3_REG_SNEG;
+       if (srcflags & IR3_REG_BNOT)
+               *dstflags ^= IR3_REG_BNOT;
+
+       *dstflags &= ~IR3_REG_SSA;
+       *dstflags |= srcflags & IR3_REG_SSA;
+       *dstflags |= srcflags & IR3_REG_CONST;
+       *dstflags |= srcflags & IR3_REG_IMMED;
+       *dstflags |= srcflags & IR3_REG_RELATIV;
+       *dstflags |= srcflags & IR3_REG_ARRAY;
+
+       /* if src of the src is boolean we can drop the (abs) since we know
+        * the source value is already a postitive integer.  This cleans
+        * up the absnegs that get inserted when converting between nir and
+        * native boolean (see ir3_b2n/n2b)
+        */
+       struct ir3_instruction *srcsrc = ssa(src->regs[1]);
+       if (srcsrc && is_bool(srcsrc))
+               *dstflags &= ~IR3_REG_SABS;
 }
 
-static struct ir3_instruction *
-instr_cp_fanin(struct ir3_instruction *instr)
+static struct ir3_register *
+lower_immed(struct ir3_cp_ctx *ctx, struct ir3_register *reg, unsigned new_flags)
 {
-       unsigned i, j;
+       unsigned swiz, idx, i;
 
-       /* we need to handle fanin specially, to detect cases
-        * when we need to keep a mov
+       reg = ir3_reg_clone(ctx->shader, reg);
+
+       /* in some cases, there are restrictions on (abs)/(neg) plus const..
+        * so just evaluate those and clear the flags:
         */
+       if (new_flags & IR3_REG_SABS) {
+               reg->iim_val = abs(reg->iim_val);
+               new_flags &= ~IR3_REG_SABS;
+       }
 
-       for (i = 1; i < instr->regs_count; i++) {
-               struct ir3_register *src = instr->regs[i];
-               if (src->flags & IR3_REG_SSA) {
-                       struct ir3_instruction *cand =
-                                       instr_cp(src->instr, false);
+       if (new_flags & IR3_REG_FABS) {
+               reg->fim_val = fabs(reg->fim_val);
+               new_flags &= ~IR3_REG_FABS;
+       }
 
-                       /* if the candidate is a fanout, then keep
-                        * the move.
-                        *
-                        * This is a bit, um, fragile, but it should
-                        * catch the extra mov's that the front-end
-                        * puts in for us already in these cases.
-                        */
-                       if (is_meta(cand) && (cand->opc == OPC_META_FO))
-                               cand = instr_cp(src->instr, true);
+       if (new_flags & IR3_REG_SNEG) {
+               reg->iim_val = -reg->iim_val;
+               new_flags &= ~IR3_REG_SNEG;
+       }
 
-                       /* we can't have 2 registers referring to the same instruction, so
-                        * go through and check if any already refer to the candidate
-                        * instruction. if so, don't do the propagation.
-                        *
-                        * NOTE: we need to keep this, despite the neighbor
-                        * conflict checks, to avoid A<->B<->A..
-                        */
-                       for (j = 1; j < instr->regs_count; j++)
-                               if (instr->regs[j]->instr == cand)
-                                       break;
-                       if (j == instr->regs_count)
-                               src->instr = cand;
-               }
+       if (new_flags & IR3_REG_FNEG) {
+               reg->fim_val = -reg->fim_val;
+               new_flags &= ~IR3_REG_FNEG;
        }
 
-       walk_children(instr, false);
+       for (i = 0; i < ctx->immediate_idx; i++) {
+               swiz = i % 4;
+               idx  = i / 4;
 
-       return instr;
-}
+               if (ctx->so->immediates[idx].val[swiz] == reg->uim_val) {
+                       break;
+               }
+       }
 
-static struct ir3_instruction *
-instr_cp(struct ir3_instruction *instr, bool keep)
-{
-       /* if we've already visited this instruction, bail now: */
-       if (check_stop(instr))
-               return instr;
-
-       if (is_meta(instr) && (instr->opc == OPC_META_FI))
-               return instr_cp_fanin(instr);
-
-       if (!keep && is_eligible_mov(instr)) {
-               struct ir3_instruction *src_instr = ssa(instr->regs[1]);
-               set_neighbors(src_instr, instr->cp.left, instr->cp.right);
-               remove_neighbors(instr);
-               return instr_cp(src_instr, false);
+       if (i == ctx->immediate_idx) {
+               /* need to generate a new immediate: */
+               swiz = i % 4;
+               idx  = i / 4;
+               ctx->so->immediates[idx].val[swiz] = reg->uim_val;
+               ctx->so->immediates_count = idx + 1;
+               ctx->immediate_idx++;
        }
 
-       walk_children(instr, false);
+       new_flags &= ~IR3_REG_IMMED;
+       new_flags |= IR3_REG_CONST;
+       reg->flags = new_flags;
+       reg->num = i + (4 * ctx->so->constbase.immediate);
 
-       return instr;
+       return reg;
 }
 
-static void block_cp(struct ir3_block *block)
+/**
+ * Handle cp for a given src register.  This additionally handles
+ * the cases of collapsing immedate/const (which replace the src
+ * register with a non-ssa src) or collapsing mov's from relative
+ * src (which needs to also fixup the address src reference by the
+ * instruction).
+ */
+static void
+reg_cp(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr,
+               struct ir3_register *reg, unsigned n)
 {
-       unsigned i, j;
+       struct ir3_instruction *src = ssa(reg);
 
-       for (i = 0; i < block->noutputs; i++) {
-               if (block->outputs[i]) {
-                       struct ir3_instruction *out =
-                                       instr_cp(block->outputs[i], false);
+       /* don't propagate copies into a PHI, since we don't know if the
+        * src block executed:
+        */
+       if (instr->opc == OPC_META_PHI)
+               return;
 
-                       /* To deal with things like this:
-                        *
-                        *   43: MOV OUT[2], TEMP[5]
-                        *   44: MOV OUT[0], TEMP[5]
+       if (is_eligible_mov(src, true)) {
+               /* simple case, no immed/const/relativ, only mov's w/ ssa src: */
+               struct ir3_register *src_reg = src->regs[1];
+               unsigned new_flags = reg->flags;
+
+               combine_flags(&new_flags, src);
+
+               if (valid_flags(instr, n, new_flags)) {
+                       if (new_flags & IR3_REG_ARRAY) {
+                               debug_assert(!(reg->flags & IR3_REG_ARRAY));
+                               reg->array = src_reg->array;
+                       }
+                       reg->flags = new_flags;
+                       reg->instr = ssa(src_reg);
+               }
+
+               src = ssa(reg);      /* could be null for IR3_REG_ARRAY case */
+               if (!src)
+                       return;
+       } else if (is_same_type_mov(src) &&
+                       /* cannot collapse const/immed/etc into meta instrs: */
+                       !is_meta(instr)) {
+               /* immed/const/etc cases, which require some special handling: */
+               struct ir3_register *src_reg = src->regs[1];
+               unsigned new_flags = reg->flags;
+
+               combine_flags(&new_flags, src);
+
+               if (!valid_flags(instr, n, new_flags)) {
+                       /* See if lowering an immediate to const would help. */
+                       if (valid_flags(instr, n, (new_flags & ~IR3_REG_IMMED) | IR3_REG_CONST)) {
+                               debug_assert(new_flags & IR3_REG_IMMED);
+                               instr->regs[n + 1] = lower_immed(ctx, src_reg, new_flags);
+                               return;
+                       }
+
+                       /* special case for "normal" mad instructions, we can
+                        * try swapping the first two args if that fits better.
                         *
-                        * we need to ensure that no two outputs point to
-                        * the same instruction
+                        * the "plain" MAD's (ie. the ones that don't shift first
+                        * src prior to multiply) can swap their first two srcs if
+                        * src[0] is !CONST and src[1] is CONST:
                         */
-                       for (j = 0; j < i; j++) {
-                               if (block->outputs[j] == out) {
-                                       out = instr_cp(block->outputs[i], true);
-                                       break;
-                               }
+                       if ((n == 1) && is_mad(instr->opc) &&
+                                       !(instr->regs[0 + 1]->flags & (IR3_REG_CONST | IR3_REG_RELATIV)) &&
+                                       valid_flags(instr, 0, new_flags)) {
+                               /* swap src[0] and src[1]: */
+                               struct ir3_register *tmp;
+                               tmp = instr->regs[0 + 1];
+                               instr->regs[0 + 1] = instr->regs[1 + 1];
+                               instr->regs[1 + 1] = tmp;
+                               n = 0;
+                       } else {
+                               return;
                        }
-
-                       block->outputs[i] = out;
                }
-       }
-}
 
-/*
- * Find instruction neighbors:
- */
+               /* Here we handle the special case of mov from
+                * CONST and/or RELATIV.  These need to be handled
+                * specially, because in the case of move from CONST
+                * there is no src ir3_instruction so we need to
+                * replace the ir3_register.  And in the case of
+                * RELATIV we need to handle the address register
+                * dependency.
+                */
+               if (src_reg->flags & IR3_REG_CONST) {
+                       /* an instruction cannot reference two different
+                        * address registers:
+                        */
+                       if ((src_reg->flags & IR3_REG_RELATIV) &&
+                                       conflicts(instr->address, reg->instr->address))
+                               return;
 
-static void instr_find_neighbors(struct ir3_instruction *instr)
-{
-       unsigned i;
+                       /* This seems to be a hw bug, or something where the timings
+                        * just somehow don't work out.  This restriction may only
+                        * apply if the first src is also CONST.
+                        */
+                       if ((opc_cat(instr->opc) == 3) && (n == 2) &&
+                                       (src_reg->flags & IR3_REG_RELATIV) &&
+                                       (src_reg->array.offset == 0))
+                               return;
 
-       if (check_stop(instr))
-               return;
+                       src_reg = ir3_reg_clone(instr->block->shader, src_reg);
+                       src_reg->flags = new_flags;
+                       instr->regs[n+1] = src_reg;
 
-       if (is_meta(instr) && (instr->opc == OPC_META_FI)) {
-               unsigned n = instr->regs_count;
-               for (i = 1; i < n; i++) {
-                       struct ir3_instruction *src_instr = ssa(instr->regs[i]);
-                       if (src_instr) {
-                               struct ir3_instruction *left = (i > 1) ?
-                                               ssa(instr->regs[i-1]) : NULL;
-                               struct ir3_instruction *right = (i < (n - 1)) ?
-                                               ssa(instr->regs[i+1]) : NULL;
-                               set_neighbors(src_instr, left, right);
-                               instr_find_neighbors(src_instr);
-                       }
+                       if (src_reg->flags & IR3_REG_RELATIV)
+                               ir3_instr_set_address(instr, reg->instr->address);
+
+                       return;
                }
-       } else {
-               for (i = 1; i < instr->regs_count; i++) {
-                       struct ir3_instruction *src_instr = ssa(instr->regs[i]);
-                       if (src_instr)
-                               instr_find_neighbors(src_instr);
+
+               if ((src_reg->flags & IR3_REG_RELATIV) &&
+                               !conflicts(instr->address, reg->instr->address)) {
+                       src_reg = ir3_reg_clone(instr->block->shader, src_reg);
+                       src_reg->flags = new_flags;
+                       instr->regs[n+1] = src_reg;
+                       ir3_instr_set_address(instr, reg->instr->address);
+
+                       return;
+               }
+
+               /* NOTE: seems we can only do immed integers, so don't
+                * need to care about float.  But we do need to handle
+                * abs/neg *before* checking that the immediate requires
+                * few enough bits to encode:
+                *
+                * TODO: do we need to do something to avoid accidentally
+                * catching a float immed?
+                */
+               if (src_reg->flags & IR3_REG_IMMED) {
+                       int32_t iim_val = src_reg->iim_val;
+
+                       debug_assert((opc_cat(instr->opc) == 1) ||
+                                       (opc_cat(instr->opc) == 6) ||
+                                       ir3_cat2_int(instr->opc));
+
+                       if (new_flags & IR3_REG_SABS)
+                               iim_val = abs(iim_val);
+
+                       if (new_flags & IR3_REG_SNEG)
+                               iim_val = -iim_val;
+
+                       if (new_flags & IR3_REG_BNOT)
+                               iim_val = ~iim_val;
+
+                       /* other than category 1 (mov) we can only encode up to 10 bits: */
+                       if ((instr->opc == OPC_MOV) ||
+                                       !((iim_val & ~0x3ff) && (-iim_val & ~0x3ff))) {
+                               new_flags &= ~(IR3_REG_SABS | IR3_REG_SNEG | IR3_REG_BNOT);
+                               src_reg = ir3_reg_clone(instr->block->shader, src_reg);
+                               src_reg->flags = new_flags;
+                               src_reg->iim_val = iim_val;
+                               instr->regs[n+1] = src_reg;
+                       } else if (valid_flags(instr, n, (new_flags & ~IR3_REG_IMMED) | IR3_REG_CONST)) {
+                               /* See if lowering an immediate to const would help. */
+                               instr->regs[n+1] = lower_immed(ctx, src_reg, new_flags);
+                       }
+
+                       return;
                }
        }
 }
 
-static void block_find_neighbors(struct ir3_block *block)
+/* Handle special case of eliminating output mov, and similar cases where
+ * there isn't a normal "consuming" instruction.  In this case we cannot
+ * collapse flags (ie. output mov from const, or w/ abs/neg flags, cannot
+ * be eliminated)
+ */
+static struct ir3_instruction *
+eliminate_output_mov(struct ir3_instruction *instr)
 {
-       unsigned i;
-
-       for (i = 0; i < block->noutputs; i++) {
-               if (block->outputs[i]) {
-                       struct ir3_instruction *instr = block->outputs[i];
-                       instr_find_neighbors(instr);
+       if (is_eligible_mov(instr, false)) {
+               struct ir3_register *reg = instr->regs[1];
+               if (!(reg->flags & IR3_REG_ARRAY)) {
+                       struct ir3_instruction *src_instr = ssa(reg);
+                       debug_assert(src_instr);
+                       return src_instr;
                }
        }
+       return instr;
 }
 
-static void instr_clear_neighbors(struct ir3_instruction *instr)
+/**
+ * Find instruction src's which are mov's that can be collapsed, replacing
+ * the mov dst with the mov src
+ */
+static void
+instr_cp(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr)
 {
-       unsigned i;
+       struct ir3_register *reg;
+
+       if (instr->regs_count == 0)
+               return;
 
-       if (check_stop(instr))
+       if (ir3_instr_check_mark(instr))
                return;
 
-       instr->cp.left_cnt = 0;
-       instr->cp.left = NULL;
-       instr->cp.right_cnt = 0;
-       instr->cp.right = NULL;
+       /* walk down the graph from each src: */
+       foreach_src_n(reg, n, instr) {
+               struct ir3_instruction *src = ssa(reg);
+
+               if (!src)
+                       continue;
+
+               instr_cp(ctx, src);
 
-       for (i = 1; i < instr->regs_count; i++) {
-               struct ir3_instruction *src_instr = ssa(instr->regs[i]);
-               if (src_instr)
-                       instr_clear_neighbors(src_instr);
+               /* TODO non-indirect access we could figure out which register
+                * we actually want and allow cp..
+                */
+               if (reg->flags & IR3_REG_ARRAY)
+                       continue;
+
+               reg_cp(ctx, instr, reg, n);
        }
-}
 
-static void block_clear_neighbors(struct ir3_block *block)
-{
-       unsigned i;
+       if (instr->regs[0]->flags & IR3_REG_ARRAY) {
+               struct ir3_instruction *src = ssa(instr->regs[0]);
+               if (src)
+                       instr_cp(ctx, src);
+       }
 
-       for (i = 0; i < block->noutputs; i++) {
-               if (block->outputs[i]) {
-                       struct ir3_instruction *instr = block->outputs[i];
-                       instr_clear_neighbors(instr);
+       if (instr->address) {
+               instr_cp(ctx, instr->address);
+               ir3_instr_set_address(instr, eliminate_output_mov(instr->address));
+       }
+
+       /* we can end up with extra cmps.s from frontend, which uses a
+        *
+        *    cmps.s p0.x, cond, 0
+        *
+        * as a way to mov into the predicate register.  But frequently 'cond'
+        * is itself a cmps.s/cmps.f/cmps.u.  So detect this special case and
+        * just re-write the instruction writing predicate register to get rid
+        * of the double cmps.
+        */
+       if ((instr->opc == OPC_CMPS_S) &&
+                       (instr->regs[0]->num == regid(REG_P0, 0)) &&
+                       ssa(instr->regs[1]) &&
+                       (instr->regs[2]->flags & IR3_REG_IMMED) &&
+                       (instr->regs[2]->iim_val == 0)) {
+               struct ir3_instruction *cond = ssa(instr->regs[1]);
+               switch (cond->opc) {
+               case OPC_CMPS_S:
+               case OPC_CMPS_F:
+               case OPC_CMPS_U:
+                       instr->opc   = cond->opc;
+                       instr->flags = cond->flags;
+                       instr->cat2  = cond->cat2;
+                       instr->address = cond->address;
+                       instr->regs[1] = cond->regs[1];
+                       instr->regs[2] = cond->regs[2];
+                       break;
+               default:
+                       break;
                }
        }
 }
 
-void ir3_block_cp(struct ir3_block *block)
+void
+ir3_cp(struct ir3 *ir, struct ir3_shader_variant *so)
 {
-       ir3_clear_mark(block->shader);
-       block_clear_neighbors(block);
-       ir3_clear_mark(block->shader);
-       block_find_neighbors(block);
-       ir3_clear_mark(block->shader);
-       block_cp(block);
+       struct ir3_cp_ctx ctx = {
+                       .shader = ir,
+                       .so = so,
+       };
+
+       ir3_clear_mark(ir);
+
+       for (unsigned i = 0; i < ir->noutputs; i++) {
+               if (ir->outputs[i]) {
+                       instr_cp(&ctx, ir->outputs[i]);
+                       ir->outputs[i] = eliminate_output_mov(ir->outputs[i]);
+               }
+       }
+
+       list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
+               if (block->condition) {
+                       instr_cp(&ctx, block->condition);
+                       block->condition = eliminate_output_mov(block->condition);
+               }
+
+               for (unsigned i = 0; i < block->keeps_count; i++) {
+                       instr_cp(&ctx, block->keeps[i]);
+                       block->keeps[i] = eliminate_output_mov(block->keeps[i]);
+               }
+       }
 }