| 1 |
/* |
| 2 |
* Copyright (C) 2018 Alyssa Rosenzweig |
| 3 |
* Copyright (C) 2019 Collabora, Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "util/bitscan.h" |
| 8 |
#include "compiler.h" |
| 9 |
#include "midgard_ops.h" |
| 10 |
|
| 11 |
static bool |
| 12 |
identity_swizzle(midgard_instruction *mov) |
| 13 |
{ |
| 14 |
/* Write mask should be minimal in SSA form */ |
| 15 |
assert(mir_is_ssa(mov->src[1])); |
| 16 |
assert(mov->mask == BITFIELD_MASK(util_last_bit(mov->mask))); |
| 17 |
|
| 18 |
for (unsigned i = 0; i < util_last_bit(mov->mask); ++i) { |
| 19 |
if (mov->swizzle[1][i] != i) |
| 20 |
return false; |
| 21 |
} |
| 22 |
|
| 23 |
return true; |
| 24 |
} |
| 25 |
|
| 26 |
bool |
| 27 |
midgard_opt_copy_prop(compiler_context *ctx, midgard_block *block) |
| 28 |
{ |
| 29 |
bool progress = false; |
| 30 |
|
| 31 |
mir_foreach_instr_in_block_safe(block, ins) { |
| 32 |
if (ins->type != TAG_ALU_4) |
| 33 |
continue; |
| 34 |
if (!OP_IS_MOVE(ins->op)) |
| 35 |
continue; |
| 36 |
if (ins->is_pack) |
| 37 |
continue; |
| 38 |
|
| 39 |
unsigned from = ins->src[1]; |
| 40 |
unsigned to = ins->dest; |
| 41 |
|
| 42 |
/* We only work on pure SSA */ |
| 43 |
|
| 44 |
if (to & PAN_IS_REG) |
| 45 |
continue; |
| 46 |
if (from & PAN_IS_REG) |
| 47 |
continue; |
| 48 |
|
| 49 |
/* Constant propagation is not handled here, either */ |
| 50 |
if (ins->has_inline_constant) |
| 51 |
continue; |
| 52 |
if (ins->has_constants) |
| 53 |
continue; |
| 54 |
|
| 55 |
/* Modifier propagation is not handled here */ |
| 56 |
if (mir_nontrivial_mod(ins, 1, false)) |
| 57 |
continue; |
| 58 |
if (mir_nontrivial_outmod(ins)) |
| 59 |
continue; |
| 60 |
|
| 61 |
/* Shortened arguments (bias for textures, extra load/store |
| 62 |
* arguments, etc.) do not get a swizzle, only a start |
| 63 |
* component and even that is restricted. Fragment writeout |
| 64 |
* doesn't even get that much */ |
| 65 |
|
| 66 |
bool no_swizzle = false; |
| 67 |
|
| 68 |
mir_foreach_instr_global(ctx, q) { |
| 69 |
bool is_tex = q->type == TAG_TEXTURE_4; |
| 70 |
bool is_ldst = q->type == TAG_LOAD_STORE_4; |
| 71 |
bool is_branch = q->compact_branch; |
| 72 |
|
| 73 |
if (!(is_tex || is_ldst || is_branch)) |
| 74 |
continue; |
| 75 |
|
| 76 |
/* For textures, we get a real swizzle for the |
| 77 |
* coordinate and the content. For stores, we get one. |
| 78 |
* For loads, we get none. */ |
| 79 |
|
| 80 |
unsigned start = is_tex ? 2 : OP_IS_STORE(q->op) ? 1 : 0; |
| 81 |
|
| 82 |
mir_foreach_src(q, s) { |
| 83 |
if ((s >= start) && q->src[s] == to) { |
| 84 |
no_swizzle = true; |
| 85 |
break; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if (no_swizzle && !identity_swizzle(ins)) |
| 91 |
continue; |
| 92 |
|
| 93 |
if (ctx->blend_src1 == to) |
| 94 |
ctx->blend_src1 = from; |
| 95 |
|
| 96 |
/* We're clear -- rewrite, composing the swizzle */ |
| 97 |
mir_rewrite_index_src_swizzle(ctx, to, from, ins->swizzle[1]); |
| 98 |
mir_remove_instruction(ins); |
| 99 |
progress |= true; |
| 100 |
} |
| 101 |
|
| 102 |
return progress; |
| 103 |
} |