xref: /aosp_15_r20/external/mesa3d/src/intel/compiler/brw_fs_saturate_propagation.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2013 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "brw_fs.h"
25 #include "brw_fs_live_variables.h"
26 #include "brw_cfg.h"
27 
28 using namespace brw;
29 
30 /** @file
31  *
32  * Implements a pass that propagates the SAT modifier from a MOV.SAT into the
33  * instruction that produced the source of the MOV.SAT, thereby allowing the
34  * MOV's src and dst to be coalesced and the MOV removed.
35  *
36  * For instance,
37  *
38  *    ADD     tmp, src0, src1
39  *    MOV.SAT dst, tmp
40  *
41  * would be transformed into
42  *
43  *    ADD.SAT tmp, src0, src1
44  *    MOV     dst, tmp
45  */
46 
47 static bool
propagate_sat(fs_inst * inst,fs_inst * scan_inst)48 propagate_sat(fs_inst *inst, fs_inst *scan_inst)
49 {
50    if (scan_inst->dst.type != inst->dst.type) {
51       scan_inst->dst.type = inst->dst.type;
52       for (int i = 0; i < scan_inst->sources; i++) {
53          scan_inst->src[i].type = inst->dst.type;
54       }
55    }
56 
57    if (inst->src[0].negate) {
58       if (scan_inst->opcode == BRW_OPCODE_MUL) {
59          scan_inst->src[0].negate = !scan_inst->src[0].negate;
60          inst->src[0].negate = false;
61       } else if (scan_inst->opcode == BRW_OPCODE_MAD) {
62          for (int i = 0; i < 2; i++) {
63             if (scan_inst->src[i].file == IMM) {
64                brw_reg_negate_immediate(&scan_inst->src[i]);
65             } else {
66                scan_inst->src[i].negate = !scan_inst->src[i].negate;
67             }
68          }
69          inst->src[0].negate = false;
70       } else if (scan_inst->opcode == BRW_OPCODE_ADD) {
71          if (scan_inst->src[1].file == IMM) {
72             if (!brw_reg_negate_immediate(&scan_inst->src[1])) {
73                return false;
74             }
75          } else {
76             scan_inst->src[1].negate = !scan_inst->src[1].negate;
77          }
78          scan_inst->src[0].negate = !scan_inst->src[0].negate;
79          inst->src[0].negate = false;
80       } else {
81          return false;
82       }
83    }
84 
85    scan_inst->saturate = true;
86    inst->saturate = false;
87    return true;
88 }
89 
90 static bool
opt_saturate_propagation_local(fs_visitor & s,bblock_t * block)91 opt_saturate_propagation_local(fs_visitor &s, bblock_t *block)
92 {
93    bool progress = false;
94    int ip = block->end_ip + 1;
95 
96    foreach_inst_in_block_reverse(fs_inst, inst, block) {
97       ip--;
98 
99       if (inst->opcode != BRW_OPCODE_MOV ||
100           !inst->saturate ||
101           inst->dst.file != VGRF ||
102           inst->dst.type != inst->src[0].type ||
103           inst->src[0].file != VGRF ||
104           inst->src[0].abs)
105          continue;
106 
107       const brw::def_analysis &defs = s.def_analysis.require();
108       fs_inst *def = defs.get(inst->src[0]);
109 
110       if (def != NULL) {
111          if (def->exec_size != inst->exec_size)
112             continue;
113 
114          if (def->dst.type != inst->dst.type && !def->can_change_types())
115             continue;
116 
117          if (def->flags_written(s.devinfo) != 0)
118             continue;
119 
120          if (def->saturate) {
121             inst->saturate = false;
122             progress = true;
123             continue;
124          } else if (defs.get_use_count(def->dst) == 1 &&
125                     def->can_do_saturate() &&
126                     propagate_sat(inst, def)) {
127             progress = true;
128             continue;
129          }
130 
131          /* If the def is in a different block the liveness based pass will
132           * not be able to make progress, so skip it.
133           */
134          if (block != defs.get_block(inst->src[0]))
135             continue;
136       }
137 
138       const fs_live_variables &live = s.live_analysis.require();
139       int src_var = live.var_from_reg(inst->src[0]);
140       int src_end_ip = live.end[src_var];
141 
142       bool interfered = false;
143       foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst) {
144          if (scan_inst->exec_size == inst->exec_size &&
145              regions_overlap(scan_inst->dst, scan_inst->size_written,
146                              inst->src[0], inst->size_read(0))) {
147             if (scan_inst->is_partial_write() ||
148                 (scan_inst->dst.type != inst->dst.type &&
149                  !scan_inst->can_change_types()))
150                break;
151 
152             if (scan_inst->flags_written(s.devinfo) != 0)
153                break;
154 
155             if (scan_inst->saturate) {
156                inst->saturate = false;
157                progress = true;
158             } else if (src_end_ip == ip || inst->dst.equals(inst->src[0])) {
159                if (scan_inst->can_do_saturate() &&
160                    propagate_sat(inst, scan_inst)) {
161                   progress = true;
162                }
163             }
164             break;
165          }
166          for (int i = 0; i < scan_inst->sources; i++) {
167             if (scan_inst->src[i].file == VGRF &&
168                 scan_inst->src[i].nr == inst->src[0].nr &&
169                 regions_overlap(
170                   scan_inst->src[i], scan_inst->size_read(i),
171                   inst->src[0], inst->size_read(0))) {
172                if (scan_inst->opcode != BRW_OPCODE_MOV ||
173                    !scan_inst->saturate ||
174                    scan_inst->src[0].abs ||
175                    scan_inst->src[0].negate ||
176                    scan_inst->src[0].abs != inst->src[0].abs ||
177                    scan_inst->src[0].negate != inst->src[0].negate) {
178                   interfered = true;
179                   break;
180                }
181             }
182          }
183 
184          if (interfered)
185             break;
186       }
187    }
188 
189    return progress;
190 }
191 
192 bool
brw_fs_opt_saturate_propagation(fs_visitor & s)193 brw_fs_opt_saturate_propagation(fs_visitor &s)
194 {
195    bool progress = false;
196 
197    foreach_block (block, s.cfg) {
198       progress = opt_saturate_propagation_local(s, block) || progress;
199    }
200 
201    /* Live intervals are still valid. */
202 
203    return progress;
204 }
205