• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2014 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  * Authors:
24  *    Jason Ekstrand (jason@jlekstrand.net)
25  *
26  */
27 
28 #include "nir.h"
29 
30 /*
31  * This pass lowers the neg, abs, and sat operations to source modifiers on
32  * ALU operations to make things nicer for the backend.  It's just much
33  * easier to not have them when we're doing optimizations.
34  */
35 
36 static bool
nir_lower_to_source_mods_block(nir_block * block)37 nir_lower_to_source_mods_block(nir_block *block)
38 {
39    bool progress = false;
40 
41    nir_foreach_instr(instr, block) {
42       if (instr->type != nir_instr_type_alu)
43          continue;
44 
45       nir_alu_instr *alu = nir_instr_as_alu(instr);
46 
47       for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
48          if (!alu->src[i].src.is_ssa)
49             continue;
50 
51          if (alu->src[i].src.ssa->parent_instr->type != nir_instr_type_alu)
52             continue;
53 
54          nir_alu_instr *parent = nir_instr_as_alu(alu->src[i].src.ssa->parent_instr);
55 
56          if (parent->dest.saturate)
57             continue;
58 
59          switch (nir_alu_type_get_base_type(nir_op_infos[alu->op].input_types[i])) {
60          case nir_type_float:
61             if (parent->op != nir_op_fmov)
62                continue;
63             break;
64          case nir_type_int:
65             if (parent->op != nir_op_imov)
66                continue;
67             break;
68          default:
69             continue;
70          }
71 
72          /* We can only do a rewrite if the source we are copying is SSA.
73           * Otherwise, moving the read might invalidly reorder reads/writes
74           * on a register.
75           */
76          if (!parent->src[0].src.is_ssa)
77             continue;
78 
79          nir_instr_rewrite_src(instr, &alu->src[i].src, parent->src[0].src);
80          if (alu->src[i].abs) {
81             /* abs trumps both neg and abs, do nothing */
82          } else {
83             alu->src[i].negate = (alu->src[i].negate != parent->src[0].negate);
84             alu->src[i].abs |= parent->src[0].abs;
85          }
86 
87          for (int j = 0; j < 4; ++j) {
88             if (!nir_alu_instr_channel_used(alu, i, j))
89                continue;
90             alu->src[i].swizzle[j] = parent->src[0].swizzle[alu->src[i].swizzle[j]];
91          }
92 
93          if (list_empty(&parent->dest.dest.ssa.uses) &&
94              list_empty(&parent->dest.dest.ssa.if_uses))
95             nir_instr_remove(&parent->instr);
96 
97          progress = true;
98       }
99 
100       switch (alu->op) {
101       case nir_op_fsat:
102          alu->op = nir_op_fmov;
103          alu->dest.saturate = true;
104          break;
105       case nir_op_ineg:
106          alu->op = nir_op_imov;
107          alu->src[0].negate = !alu->src[0].negate;
108          break;
109       case nir_op_fneg:
110          alu->op = nir_op_fmov;
111          alu->src[0].negate = !alu->src[0].negate;
112          break;
113       case nir_op_iabs:
114          alu->op = nir_op_imov;
115          alu->src[0].abs = true;
116          alu->src[0].negate = false;
117          break;
118       case nir_op_fabs:
119          alu->op = nir_op_fmov;
120          alu->src[0].abs = true;
121          alu->src[0].negate = false;
122          break;
123       default:
124          break;
125       }
126 
127       /* We've covered sources.  Now we're going to try and saturate the
128        * destination if we can.
129        */
130 
131       if (!alu->dest.dest.is_ssa)
132          continue;
133 
134       /* We can only saturate float destinations */
135       if (nir_alu_type_get_base_type(nir_op_infos[alu->op].output_type) !=
136           nir_type_float)
137          continue;
138 
139       if (!list_empty(&alu->dest.dest.ssa.if_uses))
140          continue;
141 
142       bool all_children_are_sat = true;
143       nir_foreach_use(child_src, &alu->dest.dest.ssa) {
144          assert(child_src->is_ssa);
145          nir_instr *child = child_src->parent_instr;
146          if (child->type != nir_instr_type_alu) {
147             all_children_are_sat = false;
148             continue;
149          }
150 
151          nir_alu_instr *child_alu = nir_instr_as_alu(child);
152          if (child_alu->src[0].negate || child_alu->src[0].abs) {
153             all_children_are_sat = false;
154             continue;
155          }
156 
157          if (child_alu->op != nir_op_fsat &&
158              !(child_alu->op == nir_op_fmov && child_alu->dest.saturate)) {
159             all_children_are_sat = false;
160             continue;
161          }
162       }
163 
164       if (!all_children_are_sat)
165          continue;
166 
167       alu->dest.saturate = true;
168       progress = true;
169 
170       nir_foreach_use(child_src, &alu->dest.dest.ssa) {
171          assert(child_src->is_ssa);
172          nir_alu_instr *child_alu = nir_instr_as_alu(child_src->parent_instr);
173 
174          child_alu->op = nir_op_fmov;
175          child_alu->dest.saturate = false;
176          /* We could propagate the dest of our instruction to the
177           * destinations of the uses here.  However, one quick round of
178           * copy propagation will clean that all up and then we don't have
179           * the complexity.
180           */
181       }
182    }
183 
184    return progress;
185 }
186 
187 static bool
nir_lower_to_source_mods_impl(nir_function_impl * impl)188 nir_lower_to_source_mods_impl(nir_function_impl *impl)
189 {
190    bool progress = false;
191 
192    nir_foreach_block(block, impl) {
193       progress |= nir_lower_to_source_mods_block(block);
194    }
195 
196    if (progress)
197       nir_metadata_preserve(impl, nir_metadata_block_index |
198                                   nir_metadata_dominance);
199 
200    return progress;
201 }
202 
203 bool
nir_lower_to_source_mods(nir_shader * shader)204 nir_lower_to_source_mods(nir_shader *shader)
205 {
206    bool progress = false;
207 
208    nir_foreach_function(function, shader) {
209       if (function->impl) {
210          progress |= nir_lower_to_source_mods_impl(function->impl);
211       }
212    }
213 
214    return progress;
215 }
216