1/*
2 * Copyright © 2015 Broadcom
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 "nir.h"
25#include "nir_builder.h"
26
27/** @file nir_opt_undef.c
28 *
29 * Handles optimization of operations involving ssa_undef.
30 */
31
32/**
33 * Turn conditional selects between an undef and some other value into a move
34 * of that other value (on the assumption that the condition's going to be
35 * choosing the defined value).  This reduces work after if flattening when
36 * each side of the if is defining a variable.
37 */
38static bool
39opt_undef_csel(nir_alu_instr *instr)
40{
41   if (!nir_op_is_selection(instr->op))
42      return false;
43
44   assert(instr->dest.dest.is_ssa);
45
46   for (int i = 1; i <= 2; i++) {
47      if (!instr->src[i].src.is_ssa)
48         continue;
49
50      nir_instr *parent = instr->src[i].src.ssa->parent_instr;
51      if (parent->type != nir_instr_type_ssa_undef)
52         continue;
53
54      /* We can't just use nir_alu_src_copy, because we need the def/use
55       * updated.
56       */
57      nir_instr_rewrite_src(&instr->instr, &instr->src[0].src,
58                            instr->src[i == 1 ? 2 : 1].src);
59      nir_alu_src_copy(&instr->src[0], &instr->src[i == 1 ? 2 : 1]);
60
61      nir_src empty_src;
62      memset(&empty_src, 0, sizeof(empty_src));
63      nir_instr_rewrite_src(&instr->instr, &instr->src[1].src, empty_src);
64      nir_instr_rewrite_src(&instr->instr, &instr->src[2].src, empty_src);
65      instr->op = nir_op_mov;
66
67      return true;
68   }
69
70   return false;
71}
72
73/**
74 * Replace vecN(undef, undef, ...) with a single undef.
75 */
76static bool
77opt_undef_vecN(nir_builder *b, nir_alu_instr *alu)
78{
79   if (!nir_op_is_vec(alu->op))
80      return false;
81
82   assert(alu->dest.dest.is_ssa);
83
84   for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
85      if (!alu->src[i].src.is_ssa ||
86          alu->src[i].src.ssa->parent_instr->type != nir_instr_type_ssa_undef)
87         return false;
88   }
89
90   b->cursor = nir_before_instr(&alu->instr);
91   nir_ssa_def *undef = nir_ssa_undef(b, alu->dest.dest.ssa.num_components,
92                                      nir_dest_bit_size(alu->dest.dest));
93   nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, undef);
94
95   return true;
96}
97
98static uint32_t
99nir_get_undef_mask(nir_ssa_def *def)
100{
101   nir_instr *instr = def->parent_instr;
102
103   if (instr->type == nir_instr_type_ssa_undef)
104      return BITSET_MASK(def->num_components);
105
106   if (instr->type != nir_instr_type_alu)
107      return 0;
108
109   nir_alu_instr *alu = nir_instr_as_alu(instr);
110   unsigned undef = 0;
111
112   if (nir_op_is_vec(alu->op)) {
113      for (int i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
114         if (alu->src[i].src.is_ssa &&
115             alu->src[i].src.ssa->parent_instr->type ==
116             nir_instr_type_ssa_undef) {
117            undef |= BITSET_MASK(nir_ssa_alu_instr_src_components(alu, i)) << i;
118         }
119      }
120   }
121
122   return undef;
123}
124
125/**
126 * Remove any store intrinsic writemask channels whose value is undefined (the
127 * existing value is a fine representation of "undefined").
128 */
129static bool
130opt_undef_store(nir_intrinsic_instr *intrin)
131{
132   int arg_index;
133   switch (intrin->intrinsic) {
134   case nir_intrinsic_store_deref:
135      arg_index = 1;
136      break;
137   case nir_intrinsic_store_output:
138   case nir_intrinsic_store_per_vertex_output:
139   case nir_intrinsic_store_per_primitive_output:
140   case nir_intrinsic_store_ssbo:
141   case nir_intrinsic_store_shared:
142   case nir_intrinsic_store_global:
143   case nir_intrinsic_store_scratch:
144      arg_index =  0;
145      break;
146   default:
147      return false;
148   }
149
150   if (!intrin->src[arg_index].is_ssa)
151      return false;
152
153   nir_ssa_def *def = intrin->src[arg_index].ssa;
154
155   unsigned write_mask = nir_intrinsic_write_mask(intrin);
156   unsigned undef_mask = nir_get_undef_mask(def);
157
158   if (!(write_mask & undef_mask))
159      return false;
160
161   write_mask &= ~undef_mask;
162   if (!write_mask)
163      nir_instr_remove(&intrin->instr);
164   else
165      nir_intrinsic_set_write_mask(intrin, write_mask);
166
167   return true;
168}
169
170static bool
171nir_opt_undef_instr(nir_builder *b, nir_instr *instr, void *data)
172{
173   if (instr->type == nir_instr_type_alu) {
174      nir_alu_instr *alu = nir_instr_as_alu(instr);
175      return opt_undef_csel(alu) || opt_undef_vecN(b, alu);
176   } else if (instr->type == nir_instr_type_intrinsic) {
177      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
178      return opt_undef_store(intrin);
179   }
180
181   return false;
182}
183
184bool
185nir_opt_undef(nir_shader *shader)
186{
187   return nir_shader_instructions_pass(shader,
188                                       nir_opt_undef_instr,
189                                       nir_metadata_block_index |
190                                       nir_metadata_dominance,
191                                       NULL);
192}
193