1/*
2 * Copyright © 2016 Red Hat
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 *    Rob Clark <robclark@freedesktop.org>
25 */
26
27#ifndef _NIR_SEARCH_HELPERS_
28#define _NIR_SEARCH_HELPERS_
29
30#include "nir.h"
31#include "util/bitscan.h"
32#include <math.h>
33
34static inline bool
35is_pos_power_of_two(nir_alu_instr *instr, unsigned src, unsigned num_components,
36                    const uint8_t *swizzle)
37{
38   /* only constant srcs: */
39   if (!nir_src_is_const(instr->src[src].src))
40      return false;
41
42   for (unsigned i = 0; i < num_components; i++) {
43      switch (nir_op_infos[instr->op].input_types[src]) {
44      case nir_type_int: {
45         int64_t val = nir_src_comp_as_int(instr->src[src].src, swizzle[i]);
46         if (val <= 0 || !util_is_power_of_two_or_zero64(val))
47            return false;
48         break;
49      }
50      case nir_type_uint: {
51         uint64_t val = nir_src_comp_as_uint(instr->src[src].src, swizzle[i]);
52         if (val == 0 || !util_is_power_of_two_or_zero64(val))
53            return false;
54         break;
55      }
56      default:
57         return false;
58      }
59   }
60
61   return true;
62}
63
64static inline bool
65is_neg_power_of_two(nir_alu_instr *instr, unsigned src, unsigned num_components,
66                    const uint8_t *swizzle)
67{
68   /* only constant srcs: */
69   if (!nir_src_is_const(instr->src[src].src))
70      return false;
71
72   for (unsigned i = 0; i < num_components; i++) {
73      switch (nir_op_infos[instr->op].input_types[src]) {
74      case nir_type_int: {
75         int64_t val = nir_src_comp_as_int(instr->src[src].src, swizzle[i]);
76         if (val >= 0 || !util_is_power_of_two_or_zero64(-val))
77            return false;
78         break;
79      }
80      default:
81         return false;
82      }
83   }
84
85   return true;
86}
87
88static inline bool
89is_zero_to_one(nir_alu_instr *instr, unsigned src, unsigned num_components,
90               const uint8_t *swizzle)
91{
92   /* only constant srcs: */
93   if (!nir_src_is_const(instr->src[src].src))
94      return false;
95
96   for (unsigned i = 0; i < num_components; i++) {
97      switch (nir_op_infos[instr->op].input_types[src]) {
98      case nir_type_float: {
99         double val = nir_src_comp_as_float(instr->src[src].src, swizzle[i]);
100         if (isnan(val) || val < 0.0f || val > 1.0f)
101            return false;
102         break;
103      }
104      default:
105         return false;
106      }
107   }
108
109   return true;
110}
111
112static inline bool
113is_not_const_zero(nir_alu_instr *instr, unsigned src, unsigned num_components,
114                  const uint8_t *swizzle)
115{
116   if (nir_src_as_const_value(instr->src[src].src) == NULL)
117      return true;
118
119   for (unsigned i = 0; i < num_components; i++) {
120      switch (nir_op_infos[instr->op].input_types[src]) {
121      case nir_type_float:
122         if (nir_src_comp_as_float(instr->src[src].src, swizzle[i]) == 0.0)
123            return false;
124         break;
125      case nir_type_bool:
126      case nir_type_int:
127      case nir_type_uint:
128         if (nir_src_comp_as_uint(instr->src[src].src, swizzle[i]) == 0)
129            return false;
130         break;
131      default:
132         return false;
133      }
134   }
135
136   return true;
137}
138
139static inline bool
140is_not_const(nir_alu_instr *instr, unsigned src, UNUSED unsigned num_components,
141             UNUSED const uint8_t *swizzle)
142{
143   return !nir_src_is_const(instr->src[src].src);
144}
145
146static inline bool
147is_used_once(nir_alu_instr *instr)
148{
149   bool zero_if_use = list_empty(&instr->dest.dest.ssa.if_uses);
150   bool zero_use = list_empty(&instr->dest.dest.ssa.uses);
151
152   if (zero_if_use && zero_use)
153      return false;
154
155   if (!zero_if_use && list_is_singular(&instr->dest.dest.ssa.uses))
156     return false;
157
158   if (!zero_use && list_is_singular(&instr->dest.dest.ssa.if_uses))
159     return false;
160
161   if (!list_is_singular(&instr->dest.dest.ssa.if_uses) &&
162       !list_is_singular(&instr->dest.dest.ssa.uses))
163      return false;
164
165   return true;
166}
167
168static inline bool
169is_used_by_if(nir_alu_instr *instr)
170{
171   return !list_empty(&instr->dest.dest.ssa.if_uses);
172}
173
174static inline bool
175is_not_used_by_if(nir_alu_instr *instr)
176{
177   return list_empty(&instr->dest.dest.ssa.if_uses);
178}
179
180#endif /* _NIR_SEARCH_ */
181