2 * Copyright (C) 2020 Collabora, Ltd.
3 * Copyright (C) 2014 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * Alyssa Rosenzweig <alyssa@collabora.com>
26 * Jason Ekstrand (jason@jlekstrand.net)
33 /* Check if a given ALU source is the result of a particular componentwise 1-op
34 * ALU source (principally fneg or fabs). If so, return true and rewrite the
35 * source to be the argument, respecting swizzles as needed. If not (or it
36 * cannot be proven), return false and leave the source untouched.
40 pan_has_source_mod(nir_alu_src
*src
, nir_op op
)
42 if (!src
->src
.is_ssa
|| src
->src
.ssa
->parent_instr
->type
!= nir_instr_type_alu
)
45 nir_alu_instr
*alu
= nir_instr_as_alu(src
->src
.ssa
->parent_instr
);
50 /* This only works for unary ops */
51 assert(nir_op_infos
[op
].num_inputs
== 1);
53 /* If the copied source is not SSA, moving it might not be valid */
54 if (!alu
->src
[0].src
.is_ssa
)
57 /* Okay - we've found the modifier we wanted. Let's construct the new ALU
58 * src. In a scalar world, this is just psrc, but for vector archs we need
59 * to respect the swizzle, so we compose.
63 .src
= alu
->src
[0].src
,
66 for (unsigned i
= 0; i
< NIR_MAX_VEC_COMPONENTS
; ++i
) {
67 /* (a o b)(i) = a(b(i)) ... swizzle composition is intense. */
68 nsrc
.swizzle
[i
] = alu
->src
[0].swizzle
[src
->swizzle
[i
]];
75 /* Check if a given instruction's result will be fed into a
76 * componentwise 1-op ALU instruction (principally fsat without
77 * swizzles). If so, return true and rewrite the destination. The
78 * backend will need to track the new destinations to avoid
79 * incorrect double-emits. */
82 pan_has_dest_mod(nir_dest
**odest
, nir_op op
)
84 /* This only works for unary ops */
85 assert(nir_op_infos
[op
].num_inputs
== 1);
87 /* If not SSA, this might not be legal */
88 nir_dest
*dest
= *odest
;
92 /* Check the uses. We want a single use, with the op `op` */
93 if (!list_is_empty(&dest
->ssa
.if_uses
))
96 if (!list_is_singular(&dest
->ssa
.uses
))
99 nir_src
*use
= list_first_entry(&dest
->ssa
.uses
, nir_src
, use_link
);
100 nir_instr
*parent
= use
->parent_instr
;
102 /* Check if the op is `op` */
103 if (parent
->type
!= nir_instr_type_alu
)
106 nir_alu_instr
*alu
= nir_instr_as_alu(parent
);
110 /* We can't do expansions without a move in the middle */
111 unsigned nr_components
= nir_dest_num_components(alu
->dest
.dest
);
113 if (nir_dest_num_components(*dest
) != nr_components
)
116 /* We don't handle swizzles here, so check for the identity */
117 for (unsigned i
= 0; i
< nr_components
; ++i
) {
118 if (alu
->src
[0].swizzle
[i
] != i
)
122 if (!alu
->dest
.dest
.is_ssa
)
125 /* Otherwise, we're good */
126 *odest
= &alu
->dest
.dest
;