1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2018 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "nir.h"
25bf215546Sopenharmony_ci#include "nir_builder.h"
26bf215546Sopenharmony_ci#include "util/fast_idiv_by_const.h"
27bf215546Sopenharmony_ci#include "util/u_math.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_cistatic nir_ssa_def *
30bf215546Sopenharmony_cibuild_udiv(nir_builder *b, nir_ssa_def *n, uint64_t d)
31bf215546Sopenharmony_ci{
32bf215546Sopenharmony_ci   if (d == 0) {
33bf215546Sopenharmony_ci      return nir_imm_intN_t(b, 0, n->bit_size);
34bf215546Sopenharmony_ci   } else if (util_is_power_of_two_or_zero64(d)) {
35bf215546Sopenharmony_ci      return nir_ushr_imm(b, n, util_logbase2_64(d));
36bf215546Sopenharmony_ci   } else {
37bf215546Sopenharmony_ci      struct util_fast_udiv_info m =
38bf215546Sopenharmony_ci         util_compute_fast_udiv_info(d, n->bit_size, n->bit_size);
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci      if (m.pre_shift)
41bf215546Sopenharmony_ci         n = nir_ushr_imm(b, n, m.pre_shift);
42bf215546Sopenharmony_ci      if (m.increment)
43bf215546Sopenharmony_ci         n = nir_uadd_sat(b, n, nir_imm_intN_t(b, m.increment, n->bit_size));
44bf215546Sopenharmony_ci      n = nir_umul_high(b, n, nir_imm_intN_t(b, m.multiplier, n->bit_size));
45bf215546Sopenharmony_ci      if (m.post_shift)
46bf215546Sopenharmony_ci         n = nir_ushr_imm(b, n, m.post_shift);
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci      return n;
49bf215546Sopenharmony_ci   }
50bf215546Sopenharmony_ci}
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_cistatic nir_ssa_def *
53bf215546Sopenharmony_cibuild_umod(nir_builder *b, nir_ssa_def *n, uint64_t d)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci   if (d == 0) {
56bf215546Sopenharmony_ci      return nir_imm_intN_t(b, 0, n->bit_size);
57bf215546Sopenharmony_ci   } else if (util_is_power_of_two_or_zero64(d)) {
58bf215546Sopenharmony_ci      return nir_iand(b, n, nir_imm_intN_t(b, d - 1, n->bit_size));
59bf215546Sopenharmony_ci   } else {
60bf215546Sopenharmony_ci      return nir_isub(b, n, nir_imul(b, build_udiv(b, n, d),
61bf215546Sopenharmony_ci                                        nir_imm_intN_t(b, d, n->bit_size)));
62bf215546Sopenharmony_ci   }
63bf215546Sopenharmony_ci}
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_cistatic nir_ssa_def *
66bf215546Sopenharmony_cibuild_idiv(nir_builder *b, nir_ssa_def *n, int64_t d)
67bf215546Sopenharmony_ci{
68bf215546Sopenharmony_ci   int64_t int_min = u_intN_min(n->bit_size);
69bf215546Sopenharmony_ci   if (d == int_min)
70bf215546Sopenharmony_ci      return nir_b2i(b, nir_ieq_imm(b, n, int_min), n->bit_size);
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   uint64_t abs_d = d < 0 ? -d : d;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   if (d == 0) {
75bf215546Sopenharmony_ci      return nir_imm_intN_t(b, 0, n->bit_size);
76bf215546Sopenharmony_ci   } else if (d == 1) {
77bf215546Sopenharmony_ci      return n;
78bf215546Sopenharmony_ci   } else if (d == -1) {
79bf215546Sopenharmony_ci      return nir_ineg(b, n);
80bf215546Sopenharmony_ci   } else if (util_is_power_of_two_or_zero64(abs_d)) {
81bf215546Sopenharmony_ci      nir_ssa_def *uq = nir_ushr_imm(b, nir_iabs(b, n), util_logbase2_64(abs_d));
82bf215546Sopenharmony_ci      nir_ssa_def *n_neg = nir_ilt(b, n, nir_imm_intN_t(b, 0, n->bit_size));
83bf215546Sopenharmony_ci      nir_ssa_def *neg = d < 0 ? nir_inot(b, n_neg) : n_neg;
84bf215546Sopenharmony_ci      return nir_bcsel(b, neg, nir_ineg(b, uq), uq);
85bf215546Sopenharmony_ci   } else {
86bf215546Sopenharmony_ci      struct util_fast_sdiv_info m =
87bf215546Sopenharmony_ci         util_compute_fast_sdiv_info(d, n->bit_size);
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci      nir_ssa_def *res =
90bf215546Sopenharmony_ci         nir_imul_high(b, n, nir_imm_intN_t(b, m.multiplier, n->bit_size));
91bf215546Sopenharmony_ci      if (d > 0 && m.multiplier < 0)
92bf215546Sopenharmony_ci         res = nir_iadd(b, res, n);
93bf215546Sopenharmony_ci      if (d < 0 && m.multiplier > 0)
94bf215546Sopenharmony_ci         res = nir_isub(b, res, n);
95bf215546Sopenharmony_ci      if (m.shift)
96bf215546Sopenharmony_ci         res = nir_ishr_imm(b, res, m.shift);
97bf215546Sopenharmony_ci      res = nir_iadd(b, res, nir_ushr_imm(b, res, n->bit_size - 1));
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci      return res;
100bf215546Sopenharmony_ci   }
101bf215546Sopenharmony_ci}
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_cistatic nir_ssa_def *
104bf215546Sopenharmony_cibuild_irem(nir_builder *b, nir_ssa_def *n, int64_t d)
105bf215546Sopenharmony_ci{
106bf215546Sopenharmony_ci   int64_t int_min = u_intN_min(n->bit_size);
107bf215546Sopenharmony_ci   if (d == 0) {
108bf215546Sopenharmony_ci      return nir_imm_intN_t(b, 0, n->bit_size);
109bf215546Sopenharmony_ci   } else if (d == int_min) {
110bf215546Sopenharmony_ci      return nir_bcsel(b, nir_ieq_imm(b, n, int_min), nir_imm_intN_t(b, 0, n->bit_size), n);
111bf215546Sopenharmony_ci   } else {
112bf215546Sopenharmony_ci      d = d < 0 ? -d : d;
113bf215546Sopenharmony_ci      if (util_is_power_of_two_or_zero64(d)) {
114bf215546Sopenharmony_ci         nir_ssa_def *tmp = nir_bcsel(b, nir_ilt(b, n, nir_imm_intN_t(b, 0, n->bit_size)),
115bf215546Sopenharmony_ci                                      nir_iadd_imm(b, n, d - 1), n);
116bf215546Sopenharmony_ci         return nir_isub(b, n, nir_iand_imm(b, tmp, -d));
117bf215546Sopenharmony_ci      } else {
118bf215546Sopenharmony_ci         return nir_isub(b, n, nir_imul(b, build_idiv(b, n, d),
119bf215546Sopenharmony_ci                                        nir_imm_intN_t(b, d, n->bit_size)));
120bf215546Sopenharmony_ci      }
121bf215546Sopenharmony_ci   }
122bf215546Sopenharmony_ci}
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_cistatic nir_ssa_def *
125bf215546Sopenharmony_cibuild_imod(nir_builder *b, nir_ssa_def *n, int64_t d)
126bf215546Sopenharmony_ci{
127bf215546Sopenharmony_ci   int64_t int_min = u_intN_min(n->bit_size);
128bf215546Sopenharmony_ci   if (d == 0) {
129bf215546Sopenharmony_ci      return nir_imm_intN_t(b, 0, n->bit_size);
130bf215546Sopenharmony_ci   } else if (d == int_min) {
131bf215546Sopenharmony_ci      nir_ssa_def *int_min_def = nir_imm_intN_t(b, int_min, n->bit_size);
132bf215546Sopenharmony_ci      nir_ssa_def *is_neg_not_int_min = nir_ult(b, int_min_def, n);
133bf215546Sopenharmony_ci      nir_ssa_def *is_zero = nir_ieq_imm(b, n, 0);
134bf215546Sopenharmony_ci      return nir_bcsel(b, nir_ior(b, is_neg_not_int_min, is_zero), n, nir_iadd(b, int_min_def, n));
135bf215546Sopenharmony_ci   } else if (d > 0 && util_is_power_of_two_or_zero64(d)) {
136bf215546Sopenharmony_ci      return nir_iand(b, n, nir_imm_intN_t(b, d - 1, n->bit_size));
137bf215546Sopenharmony_ci   } else if (d < 0 && util_is_power_of_two_or_zero64(-d)) {
138bf215546Sopenharmony_ci      nir_ssa_def *d_def = nir_imm_intN_t(b, d, n->bit_size);
139bf215546Sopenharmony_ci      nir_ssa_def *res = nir_ior(b, n, d_def);
140bf215546Sopenharmony_ci      return nir_bcsel(b, nir_ieq(b, res, d_def), nir_imm_intN_t(b, 0, n->bit_size), res);
141bf215546Sopenharmony_ci   } else {
142bf215546Sopenharmony_ci      nir_ssa_def *rem = build_irem(b, n, d);
143bf215546Sopenharmony_ci      nir_ssa_def *zero = nir_imm_intN_t(b, 0, n->bit_size);
144bf215546Sopenharmony_ci      nir_ssa_def *sign_same = d < 0 ? nir_ilt(b, n, zero) : nir_ige(b, n, zero);
145bf215546Sopenharmony_ci      nir_ssa_def *rem_zero = nir_ieq(b, rem, zero);
146bf215546Sopenharmony_ci      return nir_bcsel(b, nir_ior(b, rem_zero, sign_same), rem, nir_iadd_imm(b, rem, d));
147bf215546Sopenharmony_ci   }
148bf215546Sopenharmony_ci}
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_cistatic bool
151bf215546Sopenharmony_cinir_opt_idiv_const_instr(nir_builder *b, nir_alu_instr *alu)
152bf215546Sopenharmony_ci{
153bf215546Sopenharmony_ci   assert(alu->dest.dest.is_ssa);
154bf215546Sopenharmony_ci   assert(alu->src[0].src.is_ssa && alu->src[1].src.is_ssa);
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci   if (!nir_src_is_const(alu->src[1].src))
157bf215546Sopenharmony_ci      return false;
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci   unsigned bit_size = alu->src[1].src.ssa->bit_size;
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci   b->cursor = nir_before_instr(&alu->instr);
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci   nir_ssa_def *q[NIR_MAX_VEC_COMPONENTS];
164bf215546Sopenharmony_ci   for (unsigned comp = 0; comp < alu->dest.dest.ssa.num_components; comp++) {
165bf215546Sopenharmony_ci      /* Get the numerator for the channel */
166bf215546Sopenharmony_ci      nir_ssa_def *n = nir_channel(b, alu->src[0].src.ssa,
167bf215546Sopenharmony_ci                                   alu->src[0].swizzle[comp]);
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci      /* Get the denominator for the channel */
170bf215546Sopenharmony_ci      int64_t d = nir_src_comp_as_int(alu->src[1].src,
171bf215546Sopenharmony_ci                                      alu->src[1].swizzle[comp]);
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci      nir_alu_type d_type = nir_op_infos[alu->op].input_types[1];
174bf215546Sopenharmony_ci      if (nir_alu_type_get_base_type(d_type) == nir_type_uint) {
175bf215546Sopenharmony_ci         /* The code above sign-extended.  If we're lowering an unsigned op,
176bf215546Sopenharmony_ci          * we need to mask it off to the correct number of bits so that a
177bf215546Sopenharmony_ci          * cast to uint64_t will do the right thing.
178bf215546Sopenharmony_ci          */
179bf215546Sopenharmony_ci         if (bit_size < 64)
180bf215546Sopenharmony_ci            d &= (1ull << bit_size) - 1;
181bf215546Sopenharmony_ci      }
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci      switch (alu->op) {
184bf215546Sopenharmony_ci      case nir_op_udiv:
185bf215546Sopenharmony_ci         q[comp] = build_udiv(b, n, d);
186bf215546Sopenharmony_ci         break;
187bf215546Sopenharmony_ci      case nir_op_idiv:
188bf215546Sopenharmony_ci         q[comp] = build_idiv(b, n, d);
189bf215546Sopenharmony_ci         break;
190bf215546Sopenharmony_ci      case nir_op_umod:
191bf215546Sopenharmony_ci         q[comp] = build_umod(b, n, d);
192bf215546Sopenharmony_ci         break;
193bf215546Sopenharmony_ci      case nir_op_imod:
194bf215546Sopenharmony_ci         q[comp] = build_imod(b, n, d);
195bf215546Sopenharmony_ci         break;
196bf215546Sopenharmony_ci      case nir_op_irem:
197bf215546Sopenharmony_ci         q[comp] = build_irem(b, n, d);
198bf215546Sopenharmony_ci         break;
199bf215546Sopenharmony_ci      default:
200bf215546Sopenharmony_ci         unreachable("Unknown integer division op");
201bf215546Sopenharmony_ci      }
202bf215546Sopenharmony_ci   }
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci   nir_ssa_def *qvec = nir_vec(b, q, alu->dest.dest.ssa.num_components);
205bf215546Sopenharmony_ci   nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, qvec);
206bf215546Sopenharmony_ci   nir_instr_remove(&alu->instr);
207bf215546Sopenharmony_ci
208bf215546Sopenharmony_ci   return true;
209bf215546Sopenharmony_ci}
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_cistatic bool
212bf215546Sopenharmony_cinir_opt_idiv_const_impl(nir_function_impl *impl, unsigned min_bit_size)
213bf215546Sopenharmony_ci{
214bf215546Sopenharmony_ci   bool progress = false;
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_ci   nir_builder b;
217bf215546Sopenharmony_ci   nir_builder_init(&b, impl);
218bf215546Sopenharmony_ci
219bf215546Sopenharmony_ci   nir_foreach_block(block, impl) {
220bf215546Sopenharmony_ci      nir_foreach_instr_safe(instr, block) {
221bf215546Sopenharmony_ci         if (instr->type != nir_instr_type_alu)
222bf215546Sopenharmony_ci            continue;
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci         nir_alu_instr *alu = nir_instr_as_alu(instr);
225bf215546Sopenharmony_ci         if (alu->op != nir_op_udiv &&
226bf215546Sopenharmony_ci             alu->op != nir_op_idiv &&
227bf215546Sopenharmony_ci             alu->op != nir_op_umod &&
228bf215546Sopenharmony_ci             alu->op != nir_op_imod &&
229bf215546Sopenharmony_ci             alu->op != nir_op_irem)
230bf215546Sopenharmony_ci            continue;
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci         assert(alu->dest.dest.is_ssa);
233bf215546Sopenharmony_ci         if (alu->dest.dest.ssa.bit_size < min_bit_size)
234bf215546Sopenharmony_ci            continue;
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_ci         progress |= nir_opt_idiv_const_instr(&b, alu);
237bf215546Sopenharmony_ci      }
238bf215546Sopenharmony_ci   }
239bf215546Sopenharmony_ci
240bf215546Sopenharmony_ci   if (progress) {
241bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_block_index |
242bf215546Sopenharmony_ci                                  nir_metadata_dominance);
243bf215546Sopenharmony_ci   } else {
244bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_all);
245bf215546Sopenharmony_ci   }
246bf215546Sopenharmony_ci
247bf215546Sopenharmony_ci   return progress;
248bf215546Sopenharmony_ci}
249bf215546Sopenharmony_ci
250bf215546Sopenharmony_cibool
251bf215546Sopenharmony_cinir_opt_idiv_const(nir_shader *shader, unsigned min_bit_size)
252bf215546Sopenharmony_ci{
253bf215546Sopenharmony_ci   bool progress = false;
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
256bf215546Sopenharmony_ci      if (function->impl)
257bf215546Sopenharmony_ci         progress |= nir_opt_idiv_const_impl(function->impl, min_bit_size);
258bf215546Sopenharmony_ci   }
259bf215546Sopenharmony_ci
260bf215546Sopenharmony_ci   return progress;
261bf215546Sopenharmony_ci}
262