1 /*
2  * Copyright (C) 2020 Collabora, Ltd.
3  * Copyright (C) 2014 Intel Corporation
4  *
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:
11  *
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
14  * Software.
15  *
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
22  * IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Alyssa Rosenzweig <alyssa@collabora.com>
26  *    Jason Ekstrand (jason@jlekstrand.net)
27  *
28  */
29 
30 #include "nir.h"
31 #include "pan_ir.h"
32 
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.
37 */
38 
39 bool
pan_has_source_mod(nir_alu_src * src,nir_op op)40 pan_has_source_mod(nir_alu_src *src, nir_op op)
41 {
42    if (!src->src.is_ssa || src->src.ssa->parent_instr->type != nir_instr_type_alu)
43       return false;
44 
45    nir_alu_instr *alu = nir_instr_as_alu(src->src.ssa->parent_instr);
46 
47    if (alu->op != op)
48       return false;
49 
50    /* This only works for unary ops */
51    assert(nir_op_infos[op].num_inputs == 1);
52 
53    /* If the copied source is not SSA, moving it might not be valid */
54    if (!alu->src[0].src.is_ssa)
55       return false;
56 
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.
60     */
61 
62    nir_alu_src nsrc = {
63       .src = alu->src[0].src,
64    };
65 
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]];
69    }
70 
71    *src = nsrc;
72    return true;
73 }
74 
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. */
80 
81 bool
pan_has_dest_mod(nir_dest ** odest,nir_op op)82 pan_has_dest_mod(nir_dest **odest, nir_op op)
83 {
84    /* This only works for unary ops */
85    assert(nir_op_infos[op].num_inputs == 1);
86 
87    /* If not SSA, this might not be legal */
88    nir_dest *dest = *odest;
89    if (!dest->is_ssa)
90       return false;
91 
92    /* Check the uses. We want a single use, with the op `op` */
93    if (!list_is_empty(&dest->ssa.if_uses))
94       return false;
95 
96    if (!list_is_singular(&dest->ssa.uses))
97       return false;
98 
99    nir_src *use = list_first_entry(&dest->ssa.uses, nir_src, use_link);
100    nir_instr *parent = use->parent_instr;
101 
102    /* Check if the op is `op` */
103    if (parent->type != nir_instr_type_alu)
104       return false;
105 
106    nir_alu_instr *alu = nir_instr_as_alu(parent);
107    if (alu->op != op)
108       return false;
109 
110    /* We can't do expansions without a move in the middle */
111    unsigned nr_components = nir_dest_num_components(alu->dest.dest);
112 
113    if (nir_dest_num_components(*dest) != nr_components)
114       return false;
115 
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)
119          return false;
120    }
121 
122    if (!alu->dest.dest.is_ssa)
123       return false;
124 
125    /* Otherwise, we're good */
126    *odest = &alu->dest.dest;
127    return true;
128 }
129