1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2010 Intel Corporation
3b8e80941Smrg * Copyright © 2018 Broadcom
4b8e80941Smrg *
5b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
7b8e80941Smrg * to deal in the Software without restriction, including without limitation
8b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
10b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
11b8e80941Smrg *
12b8e80941Smrg * The above copyright notice and this permission notice (including the next
13b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
14b8e80941Smrg * Software.
15b8e80941Smrg *
16b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22b8e80941Smrg * DEALINGS IN THE SOFTWARE.
23b8e80941Smrg */
24b8e80941Smrg
25b8e80941Smrg#include "nir.h"
26b8e80941Smrg#include "nir_builder.h"
27b8e80941Smrg
28b8e80941Smrg/** nir_lower_alu.c
29b8e80941Smrg *
30b8e80941Smrg * NIR's home for miscellaneous ALU operation lowering implementations.
31b8e80941Smrg *
32b8e80941Smrg * Most NIR ALU lowering occurs in nir_opt_algebraic.py, since it's generally
33b8e80941Smrg * easy to write them there.  However, if terms appear multiple times in the
34b8e80941Smrg * lowered code, it can get very verbose and cause a lot of work for CSE, so
35b8e80941Smrg * it may end up being easier to write out in C code.
36b8e80941Smrg *
37b8e80941Smrg * The shader must be in SSA for this pass.
38b8e80941Smrg */
39b8e80941Smrg
40b8e80941Smrg#define LOWER_MUL_HIGH (1 << 0)
41b8e80941Smrg
42b8e80941Smrgstatic bool
43b8e80941Smrglower_alu_instr(nir_alu_instr *instr, nir_builder *b)
44b8e80941Smrg{
45b8e80941Smrg   nir_ssa_def *lowered = NULL;
46b8e80941Smrg
47b8e80941Smrg   assert(instr->dest.dest.is_ssa);
48b8e80941Smrg
49b8e80941Smrg   b->cursor = nir_before_instr(&instr->instr);
50b8e80941Smrg   b->exact = instr->exact;
51b8e80941Smrg
52b8e80941Smrg   switch (instr->op) {
53b8e80941Smrg   case nir_op_bitfield_reverse:
54b8e80941Smrg      if (b->shader->options->lower_bitfield_reverse) {
55b8e80941Smrg         /* For more details, see:
56b8e80941Smrg          *
57b8e80941Smrg          * http://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel
58b8e80941Smrg          */
59b8e80941Smrg         nir_ssa_def *c1 = nir_imm_int(b, 1);
60b8e80941Smrg         nir_ssa_def *c2 = nir_imm_int(b, 2);
61b8e80941Smrg         nir_ssa_def *c4 = nir_imm_int(b, 4);
62b8e80941Smrg         nir_ssa_def *c8 = nir_imm_int(b, 8);
63b8e80941Smrg         nir_ssa_def *c16 = nir_imm_int(b, 16);
64b8e80941Smrg         nir_ssa_def *c33333333 = nir_imm_int(b, 0x33333333);
65b8e80941Smrg         nir_ssa_def *c55555555 = nir_imm_int(b, 0x55555555);
66b8e80941Smrg         nir_ssa_def *c0f0f0f0f = nir_imm_int(b, 0x0f0f0f0f);
67b8e80941Smrg         nir_ssa_def *c00ff00ff = nir_imm_int(b, 0x00ff00ff);
68b8e80941Smrg
69b8e80941Smrg         lowered = nir_ssa_for_alu_src(b, instr, 0);
70b8e80941Smrg
71b8e80941Smrg         /* Swap odd and even bits. */
72b8e80941Smrg         lowered = nir_ior(b,
73b8e80941Smrg                           nir_iand(b, nir_ushr(b, lowered, c1), c55555555),
74b8e80941Smrg                           nir_ishl(b, nir_iand(b, lowered, c55555555), c1));
75b8e80941Smrg
76b8e80941Smrg         /* Swap consecutive pairs. */
77b8e80941Smrg         lowered = nir_ior(b,
78b8e80941Smrg                           nir_iand(b, nir_ushr(b, lowered, c2), c33333333),
79b8e80941Smrg                           nir_ishl(b, nir_iand(b, lowered, c33333333), c2));
80b8e80941Smrg
81b8e80941Smrg         /* Swap nibbles. */
82b8e80941Smrg         lowered = nir_ior(b,
83b8e80941Smrg                           nir_iand(b, nir_ushr(b, lowered, c4), c0f0f0f0f),
84b8e80941Smrg                           nir_ishl(b, nir_iand(b, lowered, c0f0f0f0f), c4));
85b8e80941Smrg
86b8e80941Smrg         /* Swap bytes. */
87b8e80941Smrg         lowered = nir_ior(b,
88b8e80941Smrg                           nir_iand(b, nir_ushr(b, lowered, c8), c00ff00ff),
89b8e80941Smrg                           nir_ishl(b, nir_iand(b, lowered, c00ff00ff), c8));
90b8e80941Smrg
91b8e80941Smrg         lowered = nir_ior(b,
92b8e80941Smrg                           nir_ushr(b, lowered, c16),
93b8e80941Smrg                           nir_ishl(b, lowered, c16));
94b8e80941Smrg      }
95b8e80941Smrg      break;
96b8e80941Smrg
97b8e80941Smrg   case nir_op_bit_count:
98b8e80941Smrg      if (b->shader->options->lower_bit_count) {
99b8e80941Smrg         /* For more details, see:
100b8e80941Smrg          *
101b8e80941Smrg          * http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
102b8e80941Smrg          */
103b8e80941Smrg         nir_ssa_def *c1 = nir_imm_int(b, 1);
104b8e80941Smrg         nir_ssa_def *c2 = nir_imm_int(b, 2);
105b8e80941Smrg         nir_ssa_def *c4 = nir_imm_int(b, 4);
106b8e80941Smrg         nir_ssa_def *c24 = nir_imm_int(b, 24);
107b8e80941Smrg         nir_ssa_def *c33333333 = nir_imm_int(b, 0x33333333);
108b8e80941Smrg         nir_ssa_def *c55555555 = nir_imm_int(b, 0x55555555);
109b8e80941Smrg         nir_ssa_def *c0f0f0f0f = nir_imm_int(b, 0x0f0f0f0f);
110b8e80941Smrg         nir_ssa_def *c01010101 = nir_imm_int(b, 0x01010101);
111b8e80941Smrg
112b8e80941Smrg         lowered = nir_ssa_for_alu_src(b, instr, 0);
113b8e80941Smrg
114b8e80941Smrg         lowered = nir_isub(b, lowered,
115b8e80941Smrg                            nir_iand(b, nir_ushr(b, lowered, c1), c55555555));
116b8e80941Smrg
117b8e80941Smrg         lowered = nir_iadd(b,
118b8e80941Smrg                            nir_iand(b, lowered, c33333333),
119b8e80941Smrg                            nir_iand(b, nir_ushr(b, lowered, c2), c33333333));
120b8e80941Smrg
121b8e80941Smrg         lowered = nir_ushr(b,
122b8e80941Smrg                            nir_imul(b,
123b8e80941Smrg                                     nir_iand(b,
124b8e80941Smrg                                              nir_iadd(b,
125b8e80941Smrg                                                       lowered,
126b8e80941Smrg                                                       nir_ushr(b, lowered, c4)),
127b8e80941Smrg                                              c0f0f0f0f),
128b8e80941Smrg                                     c01010101),
129b8e80941Smrg                            c24);
130b8e80941Smrg      }
131b8e80941Smrg      break;
132b8e80941Smrg
133b8e80941Smrg   case nir_op_imul_high:
134b8e80941Smrg   case nir_op_umul_high:
135b8e80941Smrg      if (b->shader->options->lower_mul_high) {
136b8e80941Smrg         nir_ssa_def *c1 = nir_imm_int(b, 1);
137b8e80941Smrg         nir_ssa_def *c16 = nir_imm_int(b, 16);
138b8e80941Smrg
139b8e80941Smrg         nir_ssa_def *src0 = nir_ssa_for_alu_src(b, instr, 0);
140b8e80941Smrg         nir_ssa_def *src1 = nir_ssa_for_alu_src(b, instr, 1);
141b8e80941Smrg         nir_ssa_def *different_signs = NULL;
142b8e80941Smrg         if (instr->op == nir_op_imul_high) {
143b8e80941Smrg            nir_ssa_def *c0 = nir_imm_int(b, 0);
144b8e80941Smrg            different_signs = nir_ixor(b,
145b8e80941Smrg                                       nir_ilt(b, src0, c0),
146b8e80941Smrg                                       nir_ilt(b, src1, c0));
147b8e80941Smrg            src0 = nir_iabs(b, src0);
148b8e80941Smrg            src1 = nir_iabs(b, src1);
149b8e80941Smrg         }
150b8e80941Smrg
151b8e80941Smrg         /*   ABCD
152b8e80941Smrg          * * EFGH
153b8e80941Smrg          * ======
154b8e80941Smrg          * (GH * CD) + (GH * AB) << 16 + (EF * CD) << 16 + (EF * AB) << 32
155b8e80941Smrg          *
156b8e80941Smrg          * Start by splitting into the 4 multiplies.
157b8e80941Smrg          */
158b8e80941Smrg         nir_ssa_def *src0l = nir_iand(b, src0, nir_imm_int(b, 0xffff));
159b8e80941Smrg         nir_ssa_def *src1l = nir_iand(b, src1, nir_imm_int(b, 0xffff));
160b8e80941Smrg         nir_ssa_def *src0h = nir_ushr(b, src0, c16);
161b8e80941Smrg         nir_ssa_def *src1h = nir_ushr(b, src1, c16);
162b8e80941Smrg
163b8e80941Smrg         nir_ssa_def *lo = nir_imul(b, src0l, src1l);
164b8e80941Smrg         nir_ssa_def *m1 = nir_imul(b, src0l, src1h);
165b8e80941Smrg         nir_ssa_def *m2 = nir_imul(b, src0h, src1l);
166b8e80941Smrg         nir_ssa_def *hi = nir_imul(b, src0h, src1h);
167b8e80941Smrg
168b8e80941Smrg         nir_ssa_def *tmp;
169b8e80941Smrg
170b8e80941Smrg         tmp = nir_ishl(b, m1, c16);
171b8e80941Smrg         hi = nir_iadd(b, hi, nir_iand(b, nir_uadd_carry(b, lo, tmp), c1));
172b8e80941Smrg         lo = nir_iadd(b, lo, tmp);
173b8e80941Smrg         hi = nir_iadd(b, hi, nir_ushr(b, m1, c16));
174b8e80941Smrg
175b8e80941Smrg         tmp = nir_ishl(b, m2, c16);
176b8e80941Smrg         hi = nir_iadd(b, hi, nir_iand(b, nir_uadd_carry(b, lo, tmp), c1));
177b8e80941Smrg         lo = nir_iadd(b, lo, tmp);
178b8e80941Smrg         hi = nir_iadd(b, hi, nir_ushr(b, m2, c16));
179b8e80941Smrg
180b8e80941Smrg         if (instr->op == nir_op_imul_high) {
181b8e80941Smrg            /* For channels where different_signs is set we have to perform a
182b8e80941Smrg             * 64-bit negation.  This is *not* the same as just negating the
183b8e80941Smrg             * high 32-bits.  Consider -3 * 2.  The high 32-bits is 0, but the
184b8e80941Smrg             * desired result is -1, not -0!  Recall -x == ~x + 1.
185b8e80941Smrg             */
186b8e80941Smrg            hi = nir_bcsel(b, different_signs,
187b8e80941Smrg                           nir_iadd(b,
188b8e80941Smrg                                    nir_inot(b, hi),
189b8e80941Smrg                                    nir_iand(b,
190b8e80941Smrg                                             nir_uadd_carry(b,
191b8e80941Smrg                                                            nir_inot(b, lo),
192b8e80941Smrg                                                            c1),
193b8e80941Smrg                                             nir_imm_int(b, 1))),
194b8e80941Smrg                           hi);
195b8e80941Smrg         }
196b8e80941Smrg
197b8e80941Smrg         lowered = hi;
198b8e80941Smrg      }
199b8e80941Smrg      break;
200b8e80941Smrg
201b8e80941Smrg   default:
202b8e80941Smrg      break;
203b8e80941Smrg   }
204b8e80941Smrg
205b8e80941Smrg   if (lowered) {
206b8e80941Smrg      nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, nir_src_for_ssa(lowered));
207b8e80941Smrg      nir_instr_remove(&instr->instr);
208b8e80941Smrg      return true;
209b8e80941Smrg   } else {
210b8e80941Smrg      return false;
211b8e80941Smrg   }
212b8e80941Smrg}
213b8e80941Smrg
214b8e80941Smrgbool
215b8e80941Smrgnir_lower_alu(nir_shader *shader)
216b8e80941Smrg{
217b8e80941Smrg   bool progress = false;
218b8e80941Smrg
219b8e80941Smrg   if (!shader->options->lower_bitfield_reverse &&
220b8e80941Smrg       !shader->options->lower_mul_high)
221b8e80941Smrg      return false;
222b8e80941Smrg
223b8e80941Smrg   nir_foreach_function(function, shader) {
224b8e80941Smrg      if (function->impl) {
225b8e80941Smrg         nir_builder builder;
226b8e80941Smrg         nir_builder_init(&builder, function->impl);
227b8e80941Smrg
228b8e80941Smrg         nir_foreach_block(block, function->impl) {
229b8e80941Smrg            nir_foreach_instr_safe(instr, block) {
230b8e80941Smrg               if (instr->type == nir_instr_type_alu) {
231b8e80941Smrg                  progress = lower_alu_instr(nir_instr_as_alu(instr),
232b8e80941Smrg                                             &builder) || progress;
233b8e80941Smrg               }
234b8e80941Smrg            }
235b8e80941Smrg         }
236b8e80941Smrg
237b8e80941Smrg         if (progress) {
238b8e80941Smrg            nir_metadata_preserve(function->impl,
239b8e80941Smrg                                  nir_metadata_block_index |
240b8e80941Smrg                                  nir_metadata_dominance);
241b8e80941Smrg         }
242b8e80941Smrg      }
243b8e80941Smrg   }
244b8e80941Smrg
245b8e80941Smrg   return progress;
246b8e80941Smrg}
247