1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2016 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_phi_builder.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_cistruct repair_ssa_state {
28bf215546Sopenharmony_ci   nir_function_impl *impl;
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci   BITSET_WORD *def_set;
31bf215546Sopenharmony_ci   struct nir_phi_builder *phi_builder;
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci   bool progress;
34bf215546Sopenharmony_ci};
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci/* Get ready to build a phi and return the builder */
37bf215546Sopenharmony_cistatic struct nir_phi_builder *
38bf215546Sopenharmony_ciprep_build_phi(struct repair_ssa_state *state)
39bf215546Sopenharmony_ci{
40bf215546Sopenharmony_ci   const unsigned num_words = BITSET_WORDS(state->impl->num_blocks);
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci   /* We create the phi builder on-demand. */
43bf215546Sopenharmony_ci   if (state->phi_builder == NULL) {
44bf215546Sopenharmony_ci      state->phi_builder = nir_phi_builder_create(state->impl);
45bf215546Sopenharmony_ci      state->def_set = ralloc_array(NULL, BITSET_WORD, num_words);
46bf215546Sopenharmony_ci   }
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   /* We're going to build a phi.  That's progress. */
49bf215546Sopenharmony_ci   state->progress = true;
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci   /* Set the defs set to empty */
52bf215546Sopenharmony_ci   memset(state->def_set, 0, num_words * sizeof(*state->def_set));
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci   return state->phi_builder;
55bf215546Sopenharmony_ci}
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_cistatic nir_block *
58bf215546Sopenharmony_ciget_src_block(nir_src *src)
59bf215546Sopenharmony_ci{
60bf215546Sopenharmony_ci   if (src->parent_instr->type == nir_instr_type_phi) {
61bf215546Sopenharmony_ci      return exec_node_data(nir_phi_src, src, src)->pred;
62bf215546Sopenharmony_ci   } else {
63bf215546Sopenharmony_ci      return src->parent_instr->block;
64bf215546Sopenharmony_ci   }
65bf215546Sopenharmony_ci}
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_cistatic bool
68bf215546Sopenharmony_cirepair_ssa_def(nir_ssa_def *def, void *void_state)
69bf215546Sopenharmony_ci{
70bf215546Sopenharmony_ci   struct repair_ssa_state *state = void_state;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   bool is_valid = true;
73bf215546Sopenharmony_ci   nir_foreach_use(src, def) {
74bf215546Sopenharmony_ci      if (nir_block_is_unreachable(get_src_block(src)) ||
75bf215546Sopenharmony_ci          !nir_block_dominates(def->parent_instr->block, get_src_block(src))) {
76bf215546Sopenharmony_ci         is_valid = false;
77bf215546Sopenharmony_ci         break;
78bf215546Sopenharmony_ci      }
79bf215546Sopenharmony_ci   }
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   nir_foreach_if_use(src, def) {
82bf215546Sopenharmony_ci      nir_block *block_before_if =
83bf215546Sopenharmony_ci         nir_cf_node_as_block(nir_cf_node_prev(&src->parent_if->cf_node));
84bf215546Sopenharmony_ci      if (nir_block_is_unreachable(block_before_if) ||
85bf215546Sopenharmony_ci          !nir_block_dominates(def->parent_instr->block, block_before_if)) {
86bf215546Sopenharmony_ci         is_valid = false;
87bf215546Sopenharmony_ci         break;
88bf215546Sopenharmony_ci      }
89bf215546Sopenharmony_ci   }
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   if (is_valid)
92bf215546Sopenharmony_ci      return true;
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   struct nir_phi_builder *pb = prep_build_phi(state);
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   BITSET_SET(state->def_set, def->parent_instr->block->index);
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci   struct nir_phi_builder_value *val =
99bf215546Sopenharmony_ci      nir_phi_builder_add_value(pb, def->num_components, def->bit_size,
100bf215546Sopenharmony_ci                                state->def_set);
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci   nir_phi_builder_value_set_block_def(val, def->parent_instr->block, def);
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   nir_foreach_use_safe(src, def) {
105bf215546Sopenharmony_ci      nir_block *src_block = get_src_block(src);
106bf215546Sopenharmony_ci      if (src_block == def->parent_instr->block) {
107bf215546Sopenharmony_ci         assert(nir_phi_builder_value_get_block_def(val, src_block) == def);
108bf215546Sopenharmony_ci         continue;
109bf215546Sopenharmony_ci      }
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci      nir_ssa_def *block_def =
112bf215546Sopenharmony_ci         nir_phi_builder_value_get_block_def(val, src_block);
113bf215546Sopenharmony_ci      if (block_def == def)
114bf215546Sopenharmony_ci         continue;
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci      /* If def was a deref and the use we're looking at is a deref that
117bf215546Sopenharmony_ci       * isn't a cast, we need to wrap it in a cast so we don't loose any
118bf215546Sopenharmony_ci       * deref information.
119bf215546Sopenharmony_ci       */
120bf215546Sopenharmony_ci      if (def->parent_instr->type == nir_instr_type_deref &&
121bf215546Sopenharmony_ci          src->parent_instr->type == nir_instr_type_deref &&
122bf215546Sopenharmony_ci          nir_instr_as_deref(src->parent_instr)->deref_type != nir_deref_type_cast) {
123bf215546Sopenharmony_ci         nir_deref_instr *cast =
124bf215546Sopenharmony_ci            nir_deref_instr_create(state->impl->function->shader,
125bf215546Sopenharmony_ci                                   nir_deref_type_cast);
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci         nir_deref_instr *deref = nir_instr_as_deref(def->parent_instr);
128bf215546Sopenharmony_ci         cast->modes = deref->modes;
129bf215546Sopenharmony_ci         cast->type = deref->type;
130bf215546Sopenharmony_ci         cast->parent = nir_src_for_ssa(block_def);
131bf215546Sopenharmony_ci         cast->cast.ptr_stride = nir_deref_instr_array_stride(deref);
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci         nir_ssa_dest_init(&cast->instr, &cast->dest,
134bf215546Sopenharmony_ci                           def->num_components, def->bit_size, NULL);
135bf215546Sopenharmony_ci         nir_instr_insert(nir_before_instr(src->parent_instr),
136bf215546Sopenharmony_ci                          &cast->instr);
137bf215546Sopenharmony_ci         block_def = &cast->dest.ssa;
138bf215546Sopenharmony_ci      }
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci      nir_instr_rewrite_src(src->parent_instr, src, nir_src_for_ssa(block_def));
141bf215546Sopenharmony_ci   }
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci   nir_foreach_if_use_safe(src, def) {
144bf215546Sopenharmony_ci      nir_block *block_before_if =
145bf215546Sopenharmony_ci         nir_cf_node_as_block(nir_cf_node_prev(&src->parent_if->cf_node));
146bf215546Sopenharmony_ci      if (block_before_if == def->parent_instr->block) {
147bf215546Sopenharmony_ci         assert(nir_phi_builder_value_get_block_def(val, block_before_if) == def);
148bf215546Sopenharmony_ci         continue;
149bf215546Sopenharmony_ci      }
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci      nir_ssa_def *block_def =
152bf215546Sopenharmony_ci         nir_phi_builder_value_get_block_def(val, block_before_if);
153bf215546Sopenharmony_ci      if (block_def == def)
154bf215546Sopenharmony_ci         continue;
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci      nir_if_rewrite_condition(src->parent_if, nir_src_for_ssa(block_def));
157bf215546Sopenharmony_ci   }
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci   return true;
160bf215546Sopenharmony_ci}
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_cibool
163bf215546Sopenharmony_cinir_repair_ssa_impl(nir_function_impl *impl)
164bf215546Sopenharmony_ci{
165bf215546Sopenharmony_ci   struct repair_ssa_state state;
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci   state.impl = impl;
168bf215546Sopenharmony_ci   state.phi_builder = NULL;
169bf215546Sopenharmony_ci   state.progress = false;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   nir_metadata_require(impl, nir_metadata_block_index |
172bf215546Sopenharmony_ci                              nir_metadata_dominance);
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   nir_foreach_block(block, impl) {
175bf215546Sopenharmony_ci      nir_foreach_instr_safe(instr, block) {
176bf215546Sopenharmony_ci         nir_foreach_ssa_def(instr, repair_ssa_def, &state);
177bf215546Sopenharmony_ci      }
178bf215546Sopenharmony_ci   }
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci   if (state.progress)
181bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_block_index |
182bf215546Sopenharmony_ci                                  nir_metadata_dominance);
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci   if (state.phi_builder) {
185bf215546Sopenharmony_ci      nir_phi_builder_finish(state.phi_builder);
186bf215546Sopenharmony_ci      ralloc_free(state.def_set);
187bf215546Sopenharmony_ci   }
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci   return state.progress;
190bf215546Sopenharmony_ci}
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci/** This pass can be used to repair SSA form in a shader.
193bf215546Sopenharmony_ci *
194bf215546Sopenharmony_ci * Sometimes a transformation (such as return lowering) will have to make
195bf215546Sopenharmony_ci * changes to a shader which, while still correct, break some of NIR's SSA
196bf215546Sopenharmony_ci * invariants.  This pass will insert ssa_undefs and phi nodes as needed to
197bf215546Sopenharmony_ci * get the shader back into SSA that the validator will like.
198bf215546Sopenharmony_ci */
199bf215546Sopenharmony_cibool
200bf215546Sopenharmony_cinir_repair_ssa(nir_shader *shader)
201bf215546Sopenharmony_ci{
202bf215546Sopenharmony_ci   bool progress = false;
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
205bf215546Sopenharmony_ci      if (function->impl)
206bf215546Sopenharmony_ci         progress = nir_repair_ssa_impl(function->impl) || progress;
207bf215546Sopenharmony_ci   }
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ci   return progress;
210bf215546Sopenharmony_ci}
211