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/nir_builder.h"
26bf215546Sopenharmony_ci#include "nir_constant_expressions.h"
27bf215546Sopenharmony_ci#include "nir_control_flow.h"
28bf215546Sopenharmony_ci#include "nir_loop_analyze.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_cistatic nir_ssa_def *clone_alu_and_replace_src_defs(nir_builder *b,
31bf215546Sopenharmony_ci                                                   const nir_alu_instr *alu,
32bf215546Sopenharmony_ci                                                   nir_ssa_def **src_defs);
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci/**
35bf215546Sopenharmony_ci * Gets the single block that jumps back to the loop header. Already assumes
36bf215546Sopenharmony_ci * there is exactly one such block.
37bf215546Sopenharmony_ci */
38bf215546Sopenharmony_cistatic nir_block*
39bf215546Sopenharmony_cifind_continue_block(nir_loop *loop)
40bf215546Sopenharmony_ci{
41bf215546Sopenharmony_ci   nir_block *header_block = nir_loop_first_block(loop);
42bf215546Sopenharmony_ci   nir_block *prev_block =
43bf215546Sopenharmony_ci      nir_cf_node_as_block(nir_cf_node_prev(&loop->cf_node));
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci   assert(header_block->predecessors->entries == 2);
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   set_foreach(header_block->predecessors, pred_entry) {
48bf215546Sopenharmony_ci      if (pred_entry->key != prev_block)
49bf215546Sopenharmony_ci         return (nir_block*)pred_entry->key;
50bf215546Sopenharmony_ci   }
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   unreachable("Continue block not found!");
53bf215546Sopenharmony_ci}
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci/**
56bf215546Sopenharmony_ci * Does a phi have one constant value from outside a loop and one from inside?
57bf215546Sopenharmony_ci */
58bf215546Sopenharmony_cistatic bool
59bf215546Sopenharmony_ciphi_has_constant_from_outside_and_one_from_inside_loop(nir_phi_instr *phi,
60bf215546Sopenharmony_ci                                                       const nir_block *entry_block,
61bf215546Sopenharmony_ci                                                       bool *entry_val,
62bf215546Sopenharmony_ci                                                       bool *continue_val)
63bf215546Sopenharmony_ci{
64bf215546Sopenharmony_ci   /* We already know we have exactly one continue */
65bf215546Sopenharmony_ci   assert(exec_list_length(&phi->srcs) == 2);
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci   *entry_val = false;
68bf215546Sopenharmony_ci   *continue_val = false;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci    nir_foreach_phi_src(src, phi) {
71bf215546Sopenharmony_ci       if (!nir_src_is_const(src->src))
72bf215546Sopenharmony_ci         return false;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci       if (src->pred != entry_block) {
75bf215546Sopenharmony_ci          *continue_val = nir_src_as_bool(src->src);
76bf215546Sopenharmony_ci       } else {
77bf215546Sopenharmony_ci          *entry_val = nir_src_as_bool(src->src);
78bf215546Sopenharmony_ci       }
79bf215546Sopenharmony_ci    }
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci    return true;
82bf215546Sopenharmony_ci}
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci/**
85bf215546Sopenharmony_ci * This optimization detects if statements at the tops of loops where the
86bf215546Sopenharmony_ci * condition is a phi node of two constants and moves half of the if to above
87bf215546Sopenharmony_ci * the loop and the other half of the if to the end of the loop.  A simple for
88bf215546Sopenharmony_ci * loop "for (int i = 0; i < 4; i++)", when run through the SPIR-V front-end,
89bf215546Sopenharmony_ci * ends up looking something like this:
90bf215546Sopenharmony_ci *
91bf215546Sopenharmony_ci * vec1 32 ssa_0 = load_const (0x00000000)
92bf215546Sopenharmony_ci * vec1 32 ssa_1 = load_const (0xffffffff)
93bf215546Sopenharmony_ci * loop {
94bf215546Sopenharmony_ci *    block block_1:
95bf215546Sopenharmony_ci *    vec1 32 ssa_2 = phi block_0: ssa_0, block_7: ssa_5
96bf215546Sopenharmony_ci *    vec1 32 ssa_3 = phi block_0: ssa_0, block_7: ssa_1
97bf215546Sopenharmony_ci *    if ssa_3 {
98bf215546Sopenharmony_ci *       block block_2:
99bf215546Sopenharmony_ci *       vec1 32 ssa_4 = load_const (0x00000001)
100bf215546Sopenharmony_ci *       vec1 32 ssa_5 = iadd ssa_2, ssa_4
101bf215546Sopenharmony_ci *    } else {
102bf215546Sopenharmony_ci *       block block_3:
103bf215546Sopenharmony_ci *    }
104bf215546Sopenharmony_ci *    block block_4:
105bf215546Sopenharmony_ci *    vec1 32 ssa_6 = load_const (0x00000004)
106bf215546Sopenharmony_ci *    vec1 32 ssa_7 = ilt ssa_5, ssa_6
107bf215546Sopenharmony_ci *    if ssa_7 {
108bf215546Sopenharmony_ci *       block block_5:
109bf215546Sopenharmony_ci *    } else {
110bf215546Sopenharmony_ci *       block block_6:
111bf215546Sopenharmony_ci *       break
112bf215546Sopenharmony_ci *    }
113bf215546Sopenharmony_ci *    block block_7:
114bf215546Sopenharmony_ci * }
115bf215546Sopenharmony_ci *
116bf215546Sopenharmony_ci * This turns it into something like this:
117bf215546Sopenharmony_ci *
118bf215546Sopenharmony_ci * // Stuff from block 1
119bf215546Sopenharmony_ci * // Stuff from block 3
120bf215546Sopenharmony_ci * loop {
121bf215546Sopenharmony_ci *    block block_1:
122bf215546Sopenharmony_ci *    vec1 32 ssa_2 = phi block_0: ssa_0, block_7: ssa_5
123bf215546Sopenharmony_ci *    vec1 32 ssa_6 = load_const (0x00000004)
124bf215546Sopenharmony_ci *    vec1 32 ssa_7 = ilt ssa_2, ssa_6
125bf215546Sopenharmony_ci *    if ssa_7 {
126bf215546Sopenharmony_ci *       block block_5:
127bf215546Sopenharmony_ci *    } else {
128bf215546Sopenharmony_ci *       block block_6:
129bf215546Sopenharmony_ci *       break
130bf215546Sopenharmony_ci *    }
131bf215546Sopenharmony_ci *    block block_7:
132bf215546Sopenharmony_ci *    // Stuff from block 1
133bf215546Sopenharmony_ci *    // Stuff from block 2
134bf215546Sopenharmony_ci *    vec1 32 ssa_4 = load_const (0x00000001)
135bf215546Sopenharmony_ci *    vec1 32 ssa_5 = iadd ssa_2, ssa_4
136bf215546Sopenharmony_ci * }
137bf215546Sopenharmony_ci */
138bf215546Sopenharmony_cistatic bool
139bf215546Sopenharmony_ciopt_peel_loop_initial_if(nir_loop *loop)
140bf215546Sopenharmony_ci{
141bf215546Sopenharmony_ci   nir_block *header_block = nir_loop_first_block(loop);
142bf215546Sopenharmony_ci   nir_block *const prev_block =
143bf215546Sopenharmony_ci      nir_cf_node_as_block(nir_cf_node_prev(&loop->cf_node));
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   /* It would be insane if this were not true */
146bf215546Sopenharmony_ci   assert(_mesa_set_search(header_block->predecessors, prev_block));
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci   /* The loop must have exactly one continue block which could be a block
149bf215546Sopenharmony_ci    * ending in a continue instruction or the "natural" continue from the
150bf215546Sopenharmony_ci    * last block in the loop back to the top.
151bf215546Sopenharmony_ci    */
152bf215546Sopenharmony_ci   if (header_block->predecessors->entries != 2)
153bf215546Sopenharmony_ci      return false;
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci   nir_cf_node *if_node = nir_cf_node_next(&header_block->cf_node);
156bf215546Sopenharmony_ci   if (!if_node || if_node->type != nir_cf_node_if)
157bf215546Sopenharmony_ci      return false;
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci   nir_if *nif = nir_cf_node_as_if(if_node);
160bf215546Sopenharmony_ci   if (!nif->condition.is_ssa)
161bf215546Sopenharmony_ci      return false;
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci   nir_ssa_def *cond = nif->condition.ssa;
164bf215546Sopenharmony_ci   if (cond->parent_instr->type != nir_instr_type_phi)
165bf215546Sopenharmony_ci      return false;
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci   nir_phi_instr *cond_phi = nir_instr_as_phi(cond->parent_instr);
168bf215546Sopenharmony_ci   if (cond->parent_instr->block != header_block)
169bf215546Sopenharmony_ci      return false;
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   bool entry_val = false, continue_val = false;
172bf215546Sopenharmony_ci   if (!phi_has_constant_from_outside_and_one_from_inside_loop(cond_phi,
173bf215546Sopenharmony_ci                                                               prev_block,
174bf215546Sopenharmony_ci                                                               &entry_val,
175bf215546Sopenharmony_ci                                                               &continue_val))
176bf215546Sopenharmony_ci      return false;
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci   /* If they both execute or both don't execute, this is a job for
179bf215546Sopenharmony_ci    * nir_dead_cf, not this pass.
180bf215546Sopenharmony_ci    */
181bf215546Sopenharmony_ci   if ((entry_val && continue_val) || (!entry_val && !continue_val))
182bf215546Sopenharmony_ci      return false;
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci   struct exec_list *continue_list, *entry_list;
185bf215546Sopenharmony_ci   if (continue_val) {
186bf215546Sopenharmony_ci      continue_list = &nif->then_list;
187bf215546Sopenharmony_ci      entry_list = &nif->else_list;
188bf215546Sopenharmony_ci   } else {
189bf215546Sopenharmony_ci      continue_list = &nif->else_list;
190bf215546Sopenharmony_ci      entry_list = &nif->then_list;
191bf215546Sopenharmony_ci   }
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci   /* We want to be moving the contents of entry_list to above the loop so it
194bf215546Sopenharmony_ci    * can't contain any break or continue instructions.
195bf215546Sopenharmony_ci    */
196bf215546Sopenharmony_ci   foreach_list_typed(nir_cf_node, cf_node, node, entry_list) {
197bf215546Sopenharmony_ci      nir_foreach_block_in_cf_node(block, cf_node) {
198bf215546Sopenharmony_ci         nir_instr *last_instr = nir_block_last_instr(block);
199bf215546Sopenharmony_ci         if (last_instr && last_instr->type == nir_instr_type_jump)
200bf215546Sopenharmony_ci            return false;
201bf215546Sopenharmony_ci      }
202bf215546Sopenharmony_ci   }
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci   /* We're about to re-arrange a bunch of blocks so make sure that we don't
205bf215546Sopenharmony_ci    * have deref uses which cross block boundaries.  We don't want a deref
206bf215546Sopenharmony_ci    * accidentally ending up in a phi.
207bf215546Sopenharmony_ci    */
208bf215546Sopenharmony_ci   nir_rematerialize_derefs_in_use_blocks_impl(
209bf215546Sopenharmony_ci      nir_cf_node_get_function(&loop->cf_node));
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci   /* Before we do anything, convert the loop to LCSSA.  We're about to
212bf215546Sopenharmony_ci    * replace a bunch of SSA defs with registers and this will prevent any of
213bf215546Sopenharmony_ci    * it from leaking outside the loop.
214bf215546Sopenharmony_ci    */
215bf215546Sopenharmony_ci   nir_convert_loop_to_lcssa(loop);
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci   nir_block *after_if_block =
218bf215546Sopenharmony_ci      nir_cf_node_as_block(nir_cf_node_next(&nif->cf_node));
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ci   /* Get rid of phis in the header block since we will be duplicating it */
221bf215546Sopenharmony_ci   nir_lower_phis_to_regs_block(header_block);
222bf215546Sopenharmony_ci   /* Get rid of phis after the if since dominance will change */
223bf215546Sopenharmony_ci   nir_lower_phis_to_regs_block(after_if_block);
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_ci   /* Get rid of SSA defs in the pieces we're about to move around */
226bf215546Sopenharmony_ci   nir_lower_ssa_defs_to_regs_block(header_block);
227bf215546Sopenharmony_ci   nir_foreach_block_in_cf_node(block, &nif->cf_node)
228bf215546Sopenharmony_ci      nir_lower_ssa_defs_to_regs_block(block);
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci   nir_cf_list header, tmp;
231bf215546Sopenharmony_ci   nir_cf_extract(&header, nir_before_block(header_block),
232bf215546Sopenharmony_ci                           nir_after_block(header_block));
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci   nir_cf_list_clone(&tmp, &header, &loop->cf_node, NULL);
235bf215546Sopenharmony_ci   nir_cf_reinsert(&tmp, nir_before_cf_node(&loop->cf_node));
236bf215546Sopenharmony_ci   nir_cf_extract(&tmp, nir_before_cf_list(entry_list),
237bf215546Sopenharmony_ci                        nir_after_cf_list(entry_list));
238bf215546Sopenharmony_ci   nir_cf_reinsert(&tmp, nir_before_cf_node(&loop->cf_node));
239bf215546Sopenharmony_ci
240bf215546Sopenharmony_ci   nir_cf_reinsert(&header,
241bf215546Sopenharmony_ci                   nir_after_block_before_jump(find_continue_block(loop)));
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci   bool continue_list_jumps =
244bf215546Sopenharmony_ci      nir_block_ends_in_jump(exec_node_data(nir_block,
245bf215546Sopenharmony_ci                                            exec_list_get_tail(continue_list),
246bf215546Sopenharmony_ci                                            cf_node.node));
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ci   nir_cf_extract(&tmp, nir_before_cf_list(continue_list),
249bf215546Sopenharmony_ci                        nir_after_cf_list(continue_list));
250bf215546Sopenharmony_ci
251bf215546Sopenharmony_ci   /* Get continue block again as the previous reinsert might have removed the
252bf215546Sopenharmony_ci    * block.  Also, if both the continue list and the continue block ends in
253bf215546Sopenharmony_ci    * jump instructions, removes the jump from the latter, as it will not be
254bf215546Sopenharmony_ci    * executed if we insert the continue list before it. */
255bf215546Sopenharmony_ci
256bf215546Sopenharmony_ci   nir_block *continue_block = find_continue_block(loop);
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_ci   if (continue_list_jumps) {
259bf215546Sopenharmony_ci      nir_instr *last_instr = nir_block_last_instr(continue_block);
260bf215546Sopenharmony_ci      if (last_instr && last_instr->type == nir_instr_type_jump)
261bf215546Sopenharmony_ci         nir_instr_remove(last_instr);
262bf215546Sopenharmony_ci   }
263bf215546Sopenharmony_ci
264bf215546Sopenharmony_ci   nir_cf_reinsert(&tmp,
265bf215546Sopenharmony_ci                   nir_after_block_before_jump(continue_block));
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_ci   nir_cf_node_remove(&nif->cf_node);
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci   return true;
270bf215546Sopenharmony_ci}
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_cistatic bool
273bf215546Sopenharmony_cialu_instr_is_comparison(const nir_alu_instr *alu)
274bf215546Sopenharmony_ci{
275bf215546Sopenharmony_ci   switch (alu->op) {
276bf215546Sopenharmony_ci   case nir_op_flt32:
277bf215546Sopenharmony_ci   case nir_op_fge32:
278bf215546Sopenharmony_ci   case nir_op_feq32:
279bf215546Sopenharmony_ci   case nir_op_fneu32:
280bf215546Sopenharmony_ci   case nir_op_ilt32:
281bf215546Sopenharmony_ci   case nir_op_ult32:
282bf215546Sopenharmony_ci   case nir_op_ige32:
283bf215546Sopenharmony_ci   case nir_op_uge32:
284bf215546Sopenharmony_ci   case nir_op_ieq32:
285bf215546Sopenharmony_ci   case nir_op_ine32:
286bf215546Sopenharmony_ci      return true;
287bf215546Sopenharmony_ci   default:
288bf215546Sopenharmony_ci      return nir_alu_instr_is_comparison(alu);
289bf215546Sopenharmony_ci   }
290bf215546Sopenharmony_ci}
291bf215546Sopenharmony_ci
292bf215546Sopenharmony_cistatic bool
293bf215546Sopenharmony_cialu_instr_is_type_conversion(const nir_alu_instr *alu)
294bf215546Sopenharmony_ci{
295bf215546Sopenharmony_ci   return nir_op_infos[alu->op].num_inputs == 1 &&
296bf215546Sopenharmony_ci          nir_op_infos[alu->op].output_type != nir_op_infos[alu->op].input_types[0];
297bf215546Sopenharmony_ci}
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_cistatic bool
300bf215546Sopenharmony_ciis_trivial_bcsel(const nir_instr *instr, bool allow_non_phi_src)
301bf215546Sopenharmony_ci{
302bf215546Sopenharmony_ci   if (instr->type != nir_instr_type_alu)
303bf215546Sopenharmony_ci      return false;
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_ci   nir_alu_instr *const bcsel = nir_instr_as_alu(instr);
306bf215546Sopenharmony_ci   if (!nir_op_is_selection(bcsel->op))
307bf215546Sopenharmony_ci      return false;
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_ci   for (unsigned i = 0; i < 3; i++) {
310bf215546Sopenharmony_ci      if (!nir_alu_src_is_trivial_ssa(bcsel, i) ||
311bf215546Sopenharmony_ci          bcsel->src[i].src.ssa->parent_instr->block != instr->block)
312bf215546Sopenharmony_ci         return false;
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_ci      if (bcsel->src[i].src.ssa->parent_instr->type != nir_instr_type_phi) {
315bf215546Sopenharmony_ci         /* opt_split_alu_of_phi() is able to peel that src from the loop */
316bf215546Sopenharmony_ci         if (i == 0 || !allow_non_phi_src)
317bf215546Sopenharmony_ci            return false;
318bf215546Sopenharmony_ci         allow_non_phi_src = false;
319bf215546Sopenharmony_ci      }
320bf215546Sopenharmony_ci   }
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci   nir_foreach_phi_src(src, nir_instr_as_phi(bcsel->src[0].src.ssa->parent_instr)) {
323bf215546Sopenharmony_ci      if (!nir_src_is_const(src->src))
324bf215546Sopenharmony_ci         return false;
325bf215546Sopenharmony_ci   }
326bf215546Sopenharmony_ci
327bf215546Sopenharmony_ci   return true;
328bf215546Sopenharmony_ci}
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci/**
331bf215546Sopenharmony_ci * Splits ALU instructions that have a source that is a phi node
332bf215546Sopenharmony_ci *
333bf215546Sopenharmony_ci * ALU instructions in the header block of a loop that meet the following
334bf215546Sopenharmony_ci * criteria can be split.
335bf215546Sopenharmony_ci *
336bf215546Sopenharmony_ci * - The loop has no continue instructions other than the "natural" continue
337bf215546Sopenharmony_ci *   at the bottom of the loop.
338bf215546Sopenharmony_ci *
339bf215546Sopenharmony_ci * - At least one source of the instruction is a phi node from the header block.
340bf215546Sopenharmony_ci *
341bf215546Sopenharmony_ci * - Any non-phi sources of the ALU instruction come from a block that
342bf215546Sopenharmony_ci *   dominates the block before the loop.  The most common failure mode for
343bf215546Sopenharmony_ci *   this check is sources that are generated in the loop header block.
344bf215546Sopenharmony_ci *
345bf215546Sopenharmony_ci * - The phi node selects a constant or undef from the block before the loop or
346bf215546Sopenharmony_ci *   the only ALU user is a trivial bcsel that gets removed by peeling the ALU
347bf215546Sopenharmony_ci *
348bf215546Sopenharmony_ci * The split process splits the original ALU instruction into two, one at the
349bf215546Sopenharmony_ci * bottom of the loop and one at the block before the loop. The instruction
350bf215546Sopenharmony_ci * before the loop computes the value on the first iteration, and the
351bf215546Sopenharmony_ci * instruction at the bottom computes the value on the second, third, and so
352bf215546Sopenharmony_ci * on. A new phi node is added to the header block that selects either the
353bf215546Sopenharmony_ci * instruction before the loop or the one at the end, and uses of the original
354bf215546Sopenharmony_ci * instruction are replaced by this phi.
355bf215546Sopenharmony_ci *
356bf215546Sopenharmony_ci * The splitting transforms a loop like:
357bf215546Sopenharmony_ci *
358bf215546Sopenharmony_ci *    vec1 32 ssa_8 = load_const (0x00000001)
359bf215546Sopenharmony_ci *    vec1 32 ssa_10 = load_const (0x00000000)
360bf215546Sopenharmony_ci *    // succs: block_1
361bf215546Sopenharmony_ci *    loop {
362bf215546Sopenharmony_ci *            block block_1:
363bf215546Sopenharmony_ci *            // preds: block_0 block_4
364bf215546Sopenharmony_ci *            vec1 32 ssa_11 = phi block_0: ssa_10, block_4: ssa_15
365bf215546Sopenharmony_ci *            vec1 32 ssa_12 = phi block_0: ssa_1, block_4: ssa_15
366bf215546Sopenharmony_ci *            vec1 32 ssa_13 = phi block_0: ssa_10, block_4: ssa_16
367bf215546Sopenharmony_ci *            vec1 32 ssa_14 = iadd ssa_11, ssa_8
368bf215546Sopenharmony_ci *            vec1 32 ssa_15 = b32csel ssa_13, ssa_14, ssa_12
369bf215546Sopenharmony_ci *            ...
370bf215546Sopenharmony_ci *            // succs: block_1
371bf215546Sopenharmony_ci *    }
372bf215546Sopenharmony_ci *
373bf215546Sopenharmony_ci * into:
374bf215546Sopenharmony_ci *
375bf215546Sopenharmony_ci *    vec1 32 ssa_8 = load_const (0x00000001)
376bf215546Sopenharmony_ci *    vec1 32 ssa_10 = load_const (0x00000000)
377bf215546Sopenharmony_ci *    vec1 32 ssa_22 = iadd ssa_10, ssa_8
378bf215546Sopenharmony_ci *    // succs: block_1
379bf215546Sopenharmony_ci *    loop {
380bf215546Sopenharmony_ci *            block block_1:
381bf215546Sopenharmony_ci *            // preds: block_0 block_4
382bf215546Sopenharmony_ci *            vec1 32 ssa_11 = phi block_0: ssa_10, block_4: ssa_15
383bf215546Sopenharmony_ci *            vec1 32 ssa_12 = phi block_0: ssa_1, block_4: ssa_15
384bf215546Sopenharmony_ci *            vec1 32 ssa_13 = phi block_0: ssa_10, block_4: ssa_16
385bf215546Sopenharmony_ci *            vec1 32 ssa_21 = phi block_0: ssa_22, block_4: ssa_20
386bf215546Sopenharmony_ci *            vec1 32 ssa_15 = b32csel ssa_13, ssa_21, ssa_12
387bf215546Sopenharmony_ci *            ...
388bf215546Sopenharmony_ci *            vec1 32 ssa_20 = iadd ssa_15, ssa_8
389bf215546Sopenharmony_ci *            // succs: block_1
390bf215546Sopenharmony_ci *    }
391bf215546Sopenharmony_ci */
392bf215546Sopenharmony_cistatic bool
393bf215546Sopenharmony_ciopt_split_alu_of_phi(nir_builder *b, nir_loop *loop)
394bf215546Sopenharmony_ci{
395bf215546Sopenharmony_ci   bool progress = false;
396bf215546Sopenharmony_ci   nir_block *header_block = nir_loop_first_block(loop);
397bf215546Sopenharmony_ci   nir_block *const prev_block =
398bf215546Sopenharmony_ci      nir_cf_node_as_block(nir_cf_node_prev(&loop->cf_node));
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_ci   /* It would be insane if this were not true */
401bf215546Sopenharmony_ci   assert(_mesa_set_search(header_block->predecessors, prev_block));
402bf215546Sopenharmony_ci
403bf215546Sopenharmony_ci   /* The loop must have exactly one continue block which could be a block
404bf215546Sopenharmony_ci    * ending in a continue instruction or the "natural" continue from the
405bf215546Sopenharmony_ci    * last block in the loop back to the top.
406bf215546Sopenharmony_ci    */
407bf215546Sopenharmony_ci   if (header_block->predecessors->entries != 2)
408bf215546Sopenharmony_ci      return false;
409bf215546Sopenharmony_ci
410bf215546Sopenharmony_ci   nir_block *continue_block = find_continue_block(loop);
411bf215546Sopenharmony_ci   if (continue_block == header_block)
412bf215546Sopenharmony_ci      return false;
413bf215546Sopenharmony_ci
414bf215546Sopenharmony_ci   nir_foreach_instr_safe(instr, header_block) {
415bf215546Sopenharmony_ci      if (instr->type != nir_instr_type_alu)
416bf215546Sopenharmony_ci         continue;
417bf215546Sopenharmony_ci
418bf215546Sopenharmony_ci      nir_alu_instr *const alu = nir_instr_as_alu(instr);
419bf215546Sopenharmony_ci
420bf215546Sopenharmony_ci      /* nir_op_vec{2,3,4} and nir_op_mov are excluded because they can easily
421bf215546Sopenharmony_ci       * lead to infinite optimization loops. Splitting comparisons can lead
422bf215546Sopenharmony_ci       * to loop unrolling not recognizing loop termintators, and type
423bf215546Sopenharmony_ci       * conversions also lead to regressions.
424bf215546Sopenharmony_ci       */
425bf215546Sopenharmony_ci      if (nir_op_is_vec(alu->op) ||
426bf215546Sopenharmony_ci          alu_instr_is_comparison(alu) ||
427bf215546Sopenharmony_ci          alu_instr_is_type_conversion(alu))
428bf215546Sopenharmony_ci         continue;
429bf215546Sopenharmony_ci
430bf215546Sopenharmony_ci      bool has_phi_src_from_prev_block = false;
431bf215546Sopenharmony_ci      bool all_non_phi_exist_in_prev_block = true;
432bf215546Sopenharmony_ci      bool is_prev_result_undef = true;
433bf215546Sopenharmony_ci      bool is_prev_result_const = true;
434bf215546Sopenharmony_ci      nir_ssa_def *prev_srcs[8];     // FINISHME: Array size?
435bf215546Sopenharmony_ci      nir_ssa_def *continue_srcs[8]; // FINISHME: Array size?
436bf215546Sopenharmony_ci
437bf215546Sopenharmony_ci      for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
438bf215546Sopenharmony_ci         nir_instr *const src_instr = alu->src[i].src.ssa->parent_instr;
439bf215546Sopenharmony_ci
440bf215546Sopenharmony_ci         /* If the source is a phi in the loop header block, then the
441bf215546Sopenharmony_ci          * prev_srcs and continue_srcs will come from the different sources
442bf215546Sopenharmony_ci          * of the phi.
443bf215546Sopenharmony_ci          */
444bf215546Sopenharmony_ci         if (src_instr->type == nir_instr_type_phi &&
445bf215546Sopenharmony_ci             src_instr->block == header_block) {
446bf215546Sopenharmony_ci            nir_phi_instr *const phi = nir_instr_as_phi(src_instr);
447bf215546Sopenharmony_ci
448bf215546Sopenharmony_ci            /* Only strictly need to NULL out the pointers when the assertions
449bf215546Sopenharmony_ci             * (below) are compiled in.  Debugging a NULL pointer deref in the
450bf215546Sopenharmony_ci             * wild is easier than debugging a random pointer deref, so set
451bf215546Sopenharmony_ci             * NULL unconditionally just to be safe.
452bf215546Sopenharmony_ci             */
453bf215546Sopenharmony_ci            prev_srcs[i] = NULL;
454bf215546Sopenharmony_ci            continue_srcs[i] = NULL;
455bf215546Sopenharmony_ci
456bf215546Sopenharmony_ci            nir_foreach_phi_src(src_of_phi, phi) {
457bf215546Sopenharmony_ci               if (src_of_phi->pred == prev_block) {
458bf215546Sopenharmony_ci                  if (src_of_phi->src.ssa->parent_instr->type !=
459bf215546Sopenharmony_ci                      nir_instr_type_ssa_undef) {
460bf215546Sopenharmony_ci                     is_prev_result_undef = false;
461bf215546Sopenharmony_ci                  }
462bf215546Sopenharmony_ci
463bf215546Sopenharmony_ci                  if (src_of_phi->src.ssa->parent_instr->type !=
464bf215546Sopenharmony_ci                      nir_instr_type_load_const) {
465bf215546Sopenharmony_ci                     is_prev_result_const = false;
466bf215546Sopenharmony_ci                  }
467bf215546Sopenharmony_ci
468bf215546Sopenharmony_ci                  prev_srcs[i] = src_of_phi->src.ssa;
469bf215546Sopenharmony_ci                  has_phi_src_from_prev_block = true;
470bf215546Sopenharmony_ci               } else
471bf215546Sopenharmony_ci                  continue_srcs[i] = src_of_phi->src.ssa;
472bf215546Sopenharmony_ci            }
473bf215546Sopenharmony_ci
474bf215546Sopenharmony_ci            assert(prev_srcs[i] != NULL);
475bf215546Sopenharmony_ci            assert(continue_srcs[i] != NULL);
476bf215546Sopenharmony_ci         } else {
477bf215546Sopenharmony_ci            /* If the source is not a phi (or a phi in a block other than the
478bf215546Sopenharmony_ci             * loop header), then the value must exist in prev_block.
479bf215546Sopenharmony_ci             */
480bf215546Sopenharmony_ci            if (!nir_block_dominates(src_instr->block, prev_block)) {
481bf215546Sopenharmony_ci               all_non_phi_exist_in_prev_block = false;
482bf215546Sopenharmony_ci               break;
483bf215546Sopenharmony_ci            }
484bf215546Sopenharmony_ci
485bf215546Sopenharmony_ci            prev_srcs[i] = alu->src[i].src.ssa;
486bf215546Sopenharmony_ci            continue_srcs[i] = alu->src[i].src.ssa;
487bf215546Sopenharmony_ci         }
488bf215546Sopenharmony_ci      }
489bf215546Sopenharmony_ci
490bf215546Sopenharmony_ci      if (!has_phi_src_from_prev_block || !all_non_phi_exist_in_prev_block)
491bf215546Sopenharmony_ci         continue;
492bf215546Sopenharmony_ci
493bf215546Sopenharmony_ci      if (!is_prev_result_undef && !is_prev_result_const) {
494bf215546Sopenharmony_ci         /* check if the only user is a trivial bcsel */
495bf215546Sopenharmony_ci         if (!list_is_empty(&alu->dest.dest.ssa.if_uses) ||
496bf215546Sopenharmony_ci             !list_is_singular(&alu->dest.dest.ssa.uses))
497bf215546Sopenharmony_ci            continue;
498bf215546Sopenharmony_ci
499bf215546Sopenharmony_ci         nir_src *use = list_first_entry(&alu->dest.dest.ssa.uses, nir_src, use_link);
500bf215546Sopenharmony_ci         if (!is_trivial_bcsel(use->parent_instr, true))
501bf215546Sopenharmony_ci            continue;
502bf215546Sopenharmony_ci      }
503bf215546Sopenharmony_ci
504bf215546Sopenharmony_ci      /* Split ALU of Phi */
505bf215546Sopenharmony_ci      b->cursor = nir_after_block(prev_block);
506bf215546Sopenharmony_ci      nir_ssa_def *prev_value = clone_alu_and_replace_src_defs(b, alu, prev_srcs);
507bf215546Sopenharmony_ci
508bf215546Sopenharmony_ci      /* Make a copy of the original ALU instruction.  Replace the sources
509bf215546Sopenharmony_ci       * of the new instruction that read a phi with an undef source from
510bf215546Sopenharmony_ci       * prev_block with the non-undef source of that phi.
511bf215546Sopenharmony_ci       *
512bf215546Sopenharmony_ci       * Insert the new instruction at the end of the continue block.
513bf215546Sopenharmony_ci       */
514bf215546Sopenharmony_ci      b->cursor = nir_after_block_before_jump(continue_block);
515bf215546Sopenharmony_ci
516bf215546Sopenharmony_ci      nir_ssa_def *const alu_copy =
517bf215546Sopenharmony_ci         clone_alu_and_replace_src_defs(b, alu, continue_srcs);
518bf215546Sopenharmony_ci
519bf215546Sopenharmony_ci      /* Make a new phi node that selects a value from prev_block and the
520bf215546Sopenharmony_ci       * result of the new instruction from continue_block.
521bf215546Sopenharmony_ci       */
522bf215546Sopenharmony_ci      nir_phi_instr *const phi = nir_phi_instr_create(b->shader);
523bf215546Sopenharmony_ci      nir_phi_instr_add_src(phi, prev_block, nir_src_for_ssa(prev_value));
524bf215546Sopenharmony_ci      nir_phi_instr_add_src(phi, continue_block, nir_src_for_ssa(alu_copy));
525bf215546Sopenharmony_ci
526bf215546Sopenharmony_ci      nir_ssa_dest_init(&phi->instr, &phi->dest,
527bf215546Sopenharmony_ci                        alu_copy->num_components, alu_copy->bit_size, NULL);
528bf215546Sopenharmony_ci
529bf215546Sopenharmony_ci      b->cursor = nir_after_phis(header_block);
530bf215546Sopenharmony_ci      nir_builder_instr_insert(b, &phi->instr);
531bf215546Sopenharmony_ci
532bf215546Sopenharmony_ci      /* Modify all readers of the original ALU instruction to read the
533bf215546Sopenharmony_ci       * result of the phi.
534bf215546Sopenharmony_ci       */
535bf215546Sopenharmony_ci      nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa,
536bf215546Sopenharmony_ci                               &phi->dest.ssa);
537bf215546Sopenharmony_ci
538bf215546Sopenharmony_ci      /* Since the original ALU instruction no longer has any readers, just
539bf215546Sopenharmony_ci       * remove it.
540bf215546Sopenharmony_ci       */
541bf215546Sopenharmony_ci      nir_instr_remove_v(&alu->instr);
542bf215546Sopenharmony_ci      nir_instr_free(&alu->instr);
543bf215546Sopenharmony_ci
544bf215546Sopenharmony_ci      progress = true;
545bf215546Sopenharmony_ci   }
546bf215546Sopenharmony_ci
547bf215546Sopenharmony_ci   return progress;
548bf215546Sopenharmony_ci}
549bf215546Sopenharmony_ci
550bf215546Sopenharmony_ci/**
551bf215546Sopenharmony_ci * Simplify a bcsel whose sources are all phi nodes from the loop header block
552bf215546Sopenharmony_ci *
553bf215546Sopenharmony_ci * bcsel instructions in a loop that meet the following criteria can be
554bf215546Sopenharmony_ci * converted to phi nodes:
555bf215546Sopenharmony_ci *
556bf215546Sopenharmony_ci * - The loop has no continue instructions other than the "natural" continue
557bf215546Sopenharmony_ci *   at the bottom of the loop.
558bf215546Sopenharmony_ci *
559bf215546Sopenharmony_ci * - All of the sources of the bcsel are phi nodes in the header block of the
560bf215546Sopenharmony_ci *   loop.
561bf215546Sopenharmony_ci *
562bf215546Sopenharmony_ci * - The phi node representing the condition of the bcsel instruction chooses
563bf215546Sopenharmony_ci *   only constant values.
564bf215546Sopenharmony_ci *
565bf215546Sopenharmony_ci * The contant value from the condition will select one of the other sources
566bf215546Sopenharmony_ci * when entered from outside the loop and the remaining source when entered
567bf215546Sopenharmony_ci * from the continue block.  Since each of these sources is also a phi node in
568bf215546Sopenharmony_ci * the header block, the value of the phi node can be "evaluated."  These
569bf215546Sopenharmony_ci * evaluated phi nodes provide the sources for a new phi node.  All users of
570bf215546Sopenharmony_ci * the bcsel result are updated to use the phi node result.
571bf215546Sopenharmony_ci *
572bf215546Sopenharmony_ci * The replacement transforms loops like:
573bf215546Sopenharmony_ci *
574bf215546Sopenharmony_ci *    vec1 32 ssa_7 = undefined
575bf215546Sopenharmony_ci *    vec1 32 ssa_8 = load_const (0x00000001)
576bf215546Sopenharmony_ci *    vec1 32 ssa_9 = load_const (0x000000c8)
577bf215546Sopenharmony_ci *    vec1 32 ssa_10 = load_const (0x00000000)
578bf215546Sopenharmony_ci *    // succs: block_1
579bf215546Sopenharmony_ci *    loop {
580bf215546Sopenharmony_ci *            block block_1:
581bf215546Sopenharmony_ci *            // preds: block_0 block_4
582bf215546Sopenharmony_ci *            vec1 32 ssa_11 = phi block_0: ssa_1, block_4: ssa_14
583bf215546Sopenharmony_ci *            vec1 32 ssa_12 = phi block_0: ssa_10, block_4: ssa_15
584bf215546Sopenharmony_ci *            vec1 32 ssa_13 = phi block_0: ssa_7, block_4: ssa_25
585bf215546Sopenharmony_ci *            vec1 32 ssa_14 = b32csel ssa_12, ssa_13, ssa_11
586bf215546Sopenharmony_ci *            vec1 32 ssa_16 = ige32 ssa_14, ssa_9
587bf215546Sopenharmony_ci *            ...
588bf215546Sopenharmony_ci *            vec1 32 ssa_15 = load_const (0xffffffff)
589bf215546Sopenharmony_ci *            ...
590bf215546Sopenharmony_ci *            vec1 32 ssa_25 = iadd ssa_14, ssa_8
591bf215546Sopenharmony_ci *            // succs: block_1
592bf215546Sopenharmony_ci *    }
593bf215546Sopenharmony_ci *
594bf215546Sopenharmony_ci * into:
595bf215546Sopenharmony_ci *
596bf215546Sopenharmony_ci *    vec1 32 ssa_7 = undefined
597bf215546Sopenharmony_ci *    vec1 32 ssa_8 = load_const (0x00000001)
598bf215546Sopenharmony_ci *    vec1 32 ssa_9 = load_const (0x000000c8)
599bf215546Sopenharmony_ci *    vec1 32 ssa_10 = load_const (0x00000000)
600bf215546Sopenharmony_ci *    // succs: block_1
601bf215546Sopenharmony_ci *    loop {
602bf215546Sopenharmony_ci *            block block_1:
603bf215546Sopenharmony_ci *            // preds: block_0 block_4
604bf215546Sopenharmony_ci *            vec1 32 ssa_11 = phi block_0: ssa_1, block_4: ssa_14
605bf215546Sopenharmony_ci *            vec1 32 ssa_12 = phi block_0: ssa_10, block_4: ssa_15
606bf215546Sopenharmony_ci *            vec1 32 ssa_13 = phi block_0: ssa_7, block_4: ssa_25
607bf215546Sopenharmony_ci *            vec1 32 sss_26 = phi block_0: ssa_1, block_4: ssa_25
608bf215546Sopenharmony_ci *            vec1 32 ssa_16 = ige32 ssa_26, ssa_9
609bf215546Sopenharmony_ci *            ...
610bf215546Sopenharmony_ci *            vec1 32 ssa_15 = load_const (0xffffffff)
611bf215546Sopenharmony_ci *            ...
612bf215546Sopenharmony_ci *            vec1 32 ssa_25 = iadd ssa_26, ssa_8
613bf215546Sopenharmony_ci *            // succs: block_1
614bf215546Sopenharmony_ci *    }
615bf215546Sopenharmony_ci *
616bf215546Sopenharmony_ci * \note
617bf215546Sopenharmony_ci * It may be possible modify this function to not require a phi node as the
618bf215546Sopenharmony_ci * source of the bcsel that is selected when entering from outside the loop.
619bf215546Sopenharmony_ci * The only restriction is that the source must be geneated outside the loop
620bf215546Sopenharmony_ci * (since it will become the source of a phi node in the header block of the
621bf215546Sopenharmony_ci * loop).
622bf215546Sopenharmony_ci */
623bf215546Sopenharmony_cistatic bool
624bf215546Sopenharmony_ciopt_simplify_bcsel_of_phi(nir_builder *b, nir_loop *loop)
625bf215546Sopenharmony_ci{
626bf215546Sopenharmony_ci   bool progress = false;
627bf215546Sopenharmony_ci   nir_block *header_block = nir_loop_first_block(loop);
628bf215546Sopenharmony_ci   nir_block *const prev_block =
629bf215546Sopenharmony_ci      nir_cf_node_as_block(nir_cf_node_prev(&loop->cf_node));
630bf215546Sopenharmony_ci
631bf215546Sopenharmony_ci   /* It would be insane if this were not true */
632bf215546Sopenharmony_ci   assert(_mesa_set_search(header_block->predecessors, prev_block));
633bf215546Sopenharmony_ci
634bf215546Sopenharmony_ci   /* The loop must have exactly one continue block which could be a block
635bf215546Sopenharmony_ci    * ending in a continue instruction or the "natural" continue from the
636bf215546Sopenharmony_ci    * last block in the loop back to the top.
637bf215546Sopenharmony_ci    */
638bf215546Sopenharmony_ci   if (header_block->predecessors->entries != 2)
639bf215546Sopenharmony_ci      return false;
640bf215546Sopenharmony_ci
641bf215546Sopenharmony_ci   /* We can move any bcsel that can guaranteed to execut on every iteration
642bf215546Sopenharmony_ci    * of a loop.  For now this is accomplished by only taking bcsels from the
643bf215546Sopenharmony_ci    * header_block.  In the future, this could be expanced to include any
644bf215546Sopenharmony_ci    * bcsel that must come before any break.
645bf215546Sopenharmony_ci    *
646bf215546Sopenharmony_ci    * For more details, see
647bf215546Sopenharmony_ci    * https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/170#note_110305
648bf215546Sopenharmony_ci    */
649bf215546Sopenharmony_ci   nir_foreach_instr_safe(instr, header_block) {
650bf215546Sopenharmony_ci      if (!is_trivial_bcsel(instr, false))
651bf215546Sopenharmony_ci         continue;
652bf215546Sopenharmony_ci
653bf215546Sopenharmony_ci      nir_alu_instr *const bcsel = nir_instr_as_alu(instr);
654bf215546Sopenharmony_ci      nir_phi_instr *const cond_phi =
655bf215546Sopenharmony_ci         nir_instr_as_phi(bcsel->src[0].src.ssa->parent_instr);
656bf215546Sopenharmony_ci
657bf215546Sopenharmony_ci      bool entry_val = false, continue_val = false;
658bf215546Sopenharmony_ci      if (!phi_has_constant_from_outside_and_one_from_inside_loop(cond_phi,
659bf215546Sopenharmony_ci                                                                  prev_block,
660bf215546Sopenharmony_ci                                                                  &entry_val,
661bf215546Sopenharmony_ci                                                                  &continue_val))
662bf215546Sopenharmony_ci         continue;
663bf215546Sopenharmony_ci
664bf215546Sopenharmony_ci      /* If they both execute or both don't execute, this is a job for
665bf215546Sopenharmony_ci       * nir_dead_cf, not this pass.
666bf215546Sopenharmony_ci       */
667bf215546Sopenharmony_ci      if ((entry_val && continue_val) || (!entry_val && !continue_val))
668bf215546Sopenharmony_ci         continue;
669bf215546Sopenharmony_ci
670bf215546Sopenharmony_ci      const unsigned entry_src = entry_val ? 1 : 2;
671bf215546Sopenharmony_ci      const unsigned continue_src = entry_val ? 2 : 1;
672bf215546Sopenharmony_ci
673bf215546Sopenharmony_ci      /* Create a new phi node that selects the value for prev_block from
674bf215546Sopenharmony_ci       * the bcsel source that is selected by entry_val and the value for
675bf215546Sopenharmony_ci       * continue_block from the other bcsel source.  Both sources have
676bf215546Sopenharmony_ci       * already been verified to be phi nodes.
677bf215546Sopenharmony_ci       */
678bf215546Sopenharmony_ci      nir_block *continue_block = find_continue_block(loop);
679bf215546Sopenharmony_ci      nir_phi_instr *const phi = nir_phi_instr_create(b->shader);
680bf215546Sopenharmony_ci      nir_phi_instr_add_src(phi, prev_block,
681bf215546Sopenharmony_ci                            nir_phi_get_src_from_block(nir_instr_as_phi(bcsel->src[entry_src].src.ssa->parent_instr),
682bf215546Sopenharmony_ci                                                       prev_block)->src);
683bf215546Sopenharmony_ci
684bf215546Sopenharmony_ci      nir_phi_instr_add_src(phi, continue_block,
685bf215546Sopenharmony_ci                            nir_phi_get_src_from_block(nir_instr_as_phi(bcsel->src[continue_src].src.ssa->parent_instr),
686bf215546Sopenharmony_ci                                    continue_block)->src);
687bf215546Sopenharmony_ci
688bf215546Sopenharmony_ci      nir_ssa_dest_init(&phi->instr,
689bf215546Sopenharmony_ci                        &phi->dest,
690bf215546Sopenharmony_ci                        nir_dest_num_components(bcsel->dest.dest),
691bf215546Sopenharmony_ci                        nir_dest_bit_size(bcsel->dest.dest),
692bf215546Sopenharmony_ci                        NULL);
693bf215546Sopenharmony_ci
694bf215546Sopenharmony_ci      b->cursor = nir_after_phis(header_block);
695bf215546Sopenharmony_ci      nir_builder_instr_insert(b, &phi->instr);
696bf215546Sopenharmony_ci
697bf215546Sopenharmony_ci      /* Modify all readers of the bcsel instruction to read the result of
698bf215546Sopenharmony_ci       * the phi.
699bf215546Sopenharmony_ci       */
700bf215546Sopenharmony_ci      nir_ssa_def_rewrite_uses(&bcsel->dest.dest.ssa,
701bf215546Sopenharmony_ci                               &phi->dest.ssa);
702bf215546Sopenharmony_ci
703bf215546Sopenharmony_ci      /* Since the original bcsel instruction no longer has any readers,
704bf215546Sopenharmony_ci       * just remove it.
705bf215546Sopenharmony_ci       */
706bf215546Sopenharmony_ci      nir_instr_remove_v(&bcsel->instr);
707bf215546Sopenharmony_ci      nir_instr_free(&bcsel->instr);
708bf215546Sopenharmony_ci
709bf215546Sopenharmony_ci      progress = true;
710bf215546Sopenharmony_ci   }
711bf215546Sopenharmony_ci
712bf215546Sopenharmony_ci   return progress;
713bf215546Sopenharmony_ci}
714bf215546Sopenharmony_ci
715bf215546Sopenharmony_cistatic bool
716bf215546Sopenharmony_ciis_block_empty(nir_block *block)
717bf215546Sopenharmony_ci{
718bf215546Sopenharmony_ci   return nir_cf_node_is_last(&block->cf_node) &&
719bf215546Sopenharmony_ci          exec_list_is_empty(&block->instr_list);
720bf215546Sopenharmony_ci}
721bf215546Sopenharmony_ci
722bf215546Sopenharmony_cistatic bool
723bf215546Sopenharmony_cinir_block_ends_in_continue(nir_block *block)
724bf215546Sopenharmony_ci{
725bf215546Sopenharmony_ci   if (exec_list_is_empty(&block->instr_list))
726bf215546Sopenharmony_ci      return false;
727bf215546Sopenharmony_ci
728bf215546Sopenharmony_ci   nir_instr *instr = nir_block_last_instr(block);
729bf215546Sopenharmony_ci   return instr->type == nir_instr_type_jump &&
730bf215546Sopenharmony_ci      nir_instr_as_jump(instr)->type == nir_jump_continue;
731bf215546Sopenharmony_ci}
732bf215546Sopenharmony_ci
733bf215546Sopenharmony_ci/**
734bf215546Sopenharmony_ci * This optimization turns:
735bf215546Sopenharmony_ci *
736bf215546Sopenharmony_ci *     loop {
737bf215546Sopenharmony_ci *        ...
738bf215546Sopenharmony_ci *        if (cond) {
739bf215546Sopenharmony_ci *           do_work_1();
740bf215546Sopenharmony_ci *           continue;
741bf215546Sopenharmony_ci *        } else {
742bf215546Sopenharmony_ci *        }
743bf215546Sopenharmony_ci *        do_work_2();
744bf215546Sopenharmony_ci *     }
745bf215546Sopenharmony_ci *
746bf215546Sopenharmony_ci * into:
747bf215546Sopenharmony_ci *
748bf215546Sopenharmony_ci *     loop {
749bf215546Sopenharmony_ci *        ...
750bf215546Sopenharmony_ci *        if (cond) {
751bf215546Sopenharmony_ci *           do_work_1();
752bf215546Sopenharmony_ci *           continue;
753bf215546Sopenharmony_ci *        } else {
754bf215546Sopenharmony_ci *           do_work_2();
755bf215546Sopenharmony_ci *        }
756bf215546Sopenharmony_ci *     }
757bf215546Sopenharmony_ci *
758bf215546Sopenharmony_ci * The continue should then be removed by nir_opt_trivial_continues() and the
759bf215546Sopenharmony_ci * loop can potentially be unrolled.
760bf215546Sopenharmony_ci *
761bf215546Sopenharmony_ci * Note: Unless the function param aggressive_last_continue==true do_work_2()
762bf215546Sopenharmony_ci * is only ever blocks and nested loops. We avoid nesting other if-statments
763bf215546Sopenharmony_ci * in the branch as this can result in increased register pressure, and in
764bf215546Sopenharmony_ci * the i965 driver it causes a large amount of spilling in shader-db.
765bf215546Sopenharmony_ci * For RADV however nesting these if-statements allows further continues to be
766bf215546Sopenharmony_ci * remove and provides a significant FPS boost in Doom, which is why we have
767bf215546Sopenharmony_ci * opted for this special bool to enable more aggresive optimisations.
768bf215546Sopenharmony_ci * TODO: The GCM pass solves most of the spilling regressions in i965, if it
769bf215546Sopenharmony_ci * is ever enabled we should consider removing the aggressive_last_continue
770bf215546Sopenharmony_ci * param.
771bf215546Sopenharmony_ci */
772bf215546Sopenharmony_cistatic bool
773bf215546Sopenharmony_ciopt_if_loop_last_continue(nir_loop *loop, bool aggressive_last_continue)
774bf215546Sopenharmony_ci{
775bf215546Sopenharmony_ci   nir_if *nif = NULL;
776bf215546Sopenharmony_ci   bool then_ends_in_continue = false;
777bf215546Sopenharmony_ci   bool else_ends_in_continue = false;
778bf215546Sopenharmony_ci
779bf215546Sopenharmony_ci   /* Scan the control flow of the loop from the last to the first node
780bf215546Sopenharmony_ci    * looking for an if-statement we can optimise.
781bf215546Sopenharmony_ci    */
782bf215546Sopenharmony_ci   nir_block *last_block = nir_loop_last_block(loop);
783bf215546Sopenharmony_ci   nir_cf_node *if_node = nir_cf_node_prev(&last_block->cf_node);
784bf215546Sopenharmony_ci   while (if_node) {
785bf215546Sopenharmony_ci      if (if_node->type == nir_cf_node_if) {
786bf215546Sopenharmony_ci         nif = nir_cf_node_as_if(if_node);
787bf215546Sopenharmony_ci         nir_block *then_block = nir_if_last_then_block(nif);
788bf215546Sopenharmony_ci         nir_block *else_block = nir_if_last_else_block(nif);
789bf215546Sopenharmony_ci
790bf215546Sopenharmony_ci         then_ends_in_continue = nir_block_ends_in_continue(then_block);
791bf215546Sopenharmony_ci         else_ends_in_continue = nir_block_ends_in_continue(else_block);
792bf215546Sopenharmony_ci
793bf215546Sopenharmony_ci         /* If both branches end in a jump do nothing, this should be handled
794bf215546Sopenharmony_ci          * by nir_opt_dead_cf().
795bf215546Sopenharmony_ci          */
796bf215546Sopenharmony_ci         if ((then_ends_in_continue || nir_block_ends_in_break(then_block)) &&
797bf215546Sopenharmony_ci             (else_ends_in_continue || nir_block_ends_in_break(else_block)))
798bf215546Sopenharmony_ci            return false;
799bf215546Sopenharmony_ci
800bf215546Sopenharmony_ci         /* If continue found stop scanning and attempt optimisation, or
801bf215546Sopenharmony_ci          */
802bf215546Sopenharmony_ci         if (then_ends_in_continue || else_ends_in_continue ||
803bf215546Sopenharmony_ci             !aggressive_last_continue)
804bf215546Sopenharmony_ci            break;
805bf215546Sopenharmony_ci      }
806bf215546Sopenharmony_ci
807bf215546Sopenharmony_ci      if_node = nir_cf_node_prev(if_node);
808bf215546Sopenharmony_ci   }
809bf215546Sopenharmony_ci
810bf215546Sopenharmony_ci   /* If we didn't find an if to optimise return */
811bf215546Sopenharmony_ci   if (!nif || (!then_ends_in_continue && !else_ends_in_continue))
812bf215546Sopenharmony_ci      return false;
813bf215546Sopenharmony_ci
814bf215546Sopenharmony_ci   /* If there is nothing after the if-statement we bail */
815bf215546Sopenharmony_ci   if (&nif->cf_node == nir_cf_node_prev(&last_block->cf_node) &&
816bf215546Sopenharmony_ci       exec_list_is_empty(&last_block->instr_list))
817bf215546Sopenharmony_ci      return false;
818bf215546Sopenharmony_ci
819bf215546Sopenharmony_ci   /* If there are single-source phis in the last block,
820bf215546Sopenharmony_ci    * get rid of them first
821bf215546Sopenharmony_ci    */
822bf215546Sopenharmony_ci   nir_opt_remove_phis_block(last_block);
823bf215546Sopenharmony_ci
824bf215546Sopenharmony_ci   /* Move the last block of the loop inside the last if-statement */
825bf215546Sopenharmony_ci   nir_cf_list tmp;
826bf215546Sopenharmony_ci   nir_cf_extract(&tmp, nir_after_cf_node(if_node),
827bf215546Sopenharmony_ci                        nir_after_block(last_block));
828bf215546Sopenharmony_ci   if (then_ends_in_continue)
829bf215546Sopenharmony_ci      nir_cf_reinsert(&tmp, nir_after_cf_list(&nif->else_list));
830bf215546Sopenharmony_ci   else
831bf215546Sopenharmony_ci      nir_cf_reinsert(&tmp, nir_after_cf_list(&nif->then_list));
832bf215546Sopenharmony_ci
833bf215546Sopenharmony_ci   /* In order to avoid running nir_lower_regs_to_ssa_impl() every time an if
834bf215546Sopenharmony_ci    * opt makes progress we leave nir_opt_trivial_continues() to remove the
835bf215546Sopenharmony_ci    * continue now that the end of the loop has been simplified.
836bf215546Sopenharmony_ci    */
837bf215546Sopenharmony_ci
838bf215546Sopenharmony_ci   return true;
839bf215546Sopenharmony_ci}
840bf215546Sopenharmony_ci
841bf215546Sopenharmony_ci/* Walk all the phis in the block immediately following the if statement and
842bf215546Sopenharmony_ci * swap the blocks.
843bf215546Sopenharmony_ci */
844bf215546Sopenharmony_cistatic void
845bf215546Sopenharmony_cirewrite_phi_predecessor_blocks(nir_if *nif,
846bf215546Sopenharmony_ci                               nir_block *old_then_block,
847bf215546Sopenharmony_ci                               nir_block *old_else_block,
848bf215546Sopenharmony_ci                               nir_block *new_then_block,
849bf215546Sopenharmony_ci                               nir_block *new_else_block)
850bf215546Sopenharmony_ci{
851bf215546Sopenharmony_ci   nir_block *after_if_block =
852bf215546Sopenharmony_ci      nir_cf_node_as_block(nir_cf_node_next(&nif->cf_node));
853bf215546Sopenharmony_ci
854bf215546Sopenharmony_ci   nir_foreach_instr(instr, after_if_block) {
855bf215546Sopenharmony_ci      if (instr->type != nir_instr_type_phi)
856bf215546Sopenharmony_ci         continue;
857bf215546Sopenharmony_ci
858bf215546Sopenharmony_ci      nir_phi_instr *phi = nir_instr_as_phi(instr);
859bf215546Sopenharmony_ci
860bf215546Sopenharmony_ci      nir_foreach_phi_src(src, phi) {
861bf215546Sopenharmony_ci         if (src->pred == old_then_block) {
862bf215546Sopenharmony_ci            src->pred = new_then_block;
863bf215546Sopenharmony_ci         } else if (src->pred == old_else_block) {
864bf215546Sopenharmony_ci            src->pred = new_else_block;
865bf215546Sopenharmony_ci         }
866bf215546Sopenharmony_ci      }
867bf215546Sopenharmony_ci   }
868bf215546Sopenharmony_ci}
869bf215546Sopenharmony_ci
870bf215546Sopenharmony_ci/**
871bf215546Sopenharmony_ci * This optimization turns:
872bf215546Sopenharmony_ci *
873bf215546Sopenharmony_ci *     if (cond) {
874bf215546Sopenharmony_ci *     } else {
875bf215546Sopenharmony_ci *         do_work();
876bf215546Sopenharmony_ci *     }
877bf215546Sopenharmony_ci *
878bf215546Sopenharmony_ci * into:
879bf215546Sopenharmony_ci *
880bf215546Sopenharmony_ci *     if (!cond) {
881bf215546Sopenharmony_ci *         do_work();
882bf215546Sopenharmony_ci *     } else {
883bf215546Sopenharmony_ci *     }
884bf215546Sopenharmony_ci */
885bf215546Sopenharmony_cistatic bool
886bf215546Sopenharmony_ciopt_if_simplification(nir_builder *b, nir_if *nif)
887bf215546Sopenharmony_ci{
888bf215546Sopenharmony_ci   /* Only simplify if the then block is empty and the else block is not. */
889bf215546Sopenharmony_ci   if (!is_block_empty(nir_if_first_then_block(nif)) ||
890bf215546Sopenharmony_ci       is_block_empty(nir_if_first_else_block(nif)))
891bf215546Sopenharmony_ci      return false;
892bf215546Sopenharmony_ci
893bf215546Sopenharmony_ci   /* Make sure the condition is a comparison operation. */
894bf215546Sopenharmony_ci   nir_instr *src_instr = nif->condition.ssa->parent_instr;
895bf215546Sopenharmony_ci   if (src_instr->type != nir_instr_type_alu)
896bf215546Sopenharmony_ci      return false;
897bf215546Sopenharmony_ci
898bf215546Sopenharmony_ci   nir_alu_instr *alu_instr = nir_instr_as_alu(src_instr);
899bf215546Sopenharmony_ci   if (!nir_alu_instr_is_comparison(alu_instr))
900bf215546Sopenharmony_ci      return false;
901bf215546Sopenharmony_ci
902bf215546Sopenharmony_ci   /* Insert the inverted instruction and rewrite the condition. */
903bf215546Sopenharmony_ci   b->cursor = nir_after_instr(&alu_instr->instr);
904bf215546Sopenharmony_ci
905bf215546Sopenharmony_ci   nir_ssa_def *new_condition =
906bf215546Sopenharmony_ci      nir_inot(b, &alu_instr->dest.dest.ssa);
907bf215546Sopenharmony_ci
908bf215546Sopenharmony_ci   nir_if_rewrite_condition(nif, nir_src_for_ssa(new_condition));
909bf215546Sopenharmony_ci
910bf215546Sopenharmony_ci   /* Grab pointers to the last then/else blocks for fixing up the phis. */
911bf215546Sopenharmony_ci   nir_block *then_block = nir_if_last_then_block(nif);
912bf215546Sopenharmony_ci   nir_block *else_block = nir_if_last_else_block(nif);
913bf215546Sopenharmony_ci
914bf215546Sopenharmony_ci   if (nir_block_ends_in_jump(else_block)) {
915bf215546Sopenharmony_ci      /* Even though this if statement has a jump on one side, we may still have
916bf215546Sopenharmony_ci       * phis afterwards.  Single-source phis can be produced by loop unrolling
917bf215546Sopenharmony_ci       * or dead control-flow passes and are perfectly legal.  Run a quick phi
918bf215546Sopenharmony_ci       * removal on the block after the if to clean up any such phis.
919bf215546Sopenharmony_ci       */
920bf215546Sopenharmony_ci      nir_block *const next_block =
921bf215546Sopenharmony_ci         nir_cf_node_as_block(nir_cf_node_next(&nif->cf_node));
922bf215546Sopenharmony_ci      nir_opt_remove_phis_block(next_block);
923bf215546Sopenharmony_ci   }
924bf215546Sopenharmony_ci
925bf215546Sopenharmony_ci   rewrite_phi_predecessor_blocks(nif, then_block, else_block, else_block,
926bf215546Sopenharmony_ci                                  then_block);
927bf215546Sopenharmony_ci
928bf215546Sopenharmony_ci   /* Finally, move the else block to the then block. */
929bf215546Sopenharmony_ci   nir_cf_list tmp;
930bf215546Sopenharmony_ci   nir_cf_extract(&tmp, nir_before_cf_list(&nif->else_list),
931bf215546Sopenharmony_ci                        nir_after_cf_list(&nif->else_list));
932bf215546Sopenharmony_ci   nir_cf_reinsert(&tmp, nir_before_cf_list(&nif->then_list));
933bf215546Sopenharmony_ci
934bf215546Sopenharmony_ci   return true;
935bf215546Sopenharmony_ci}
936bf215546Sopenharmony_ci
937bf215546Sopenharmony_ci/* Find phi statements after an if that choose between true and false, and
938bf215546Sopenharmony_ci * replace them with the if statement's condition (or an inot of it).
939bf215546Sopenharmony_ci */
940bf215546Sopenharmony_cistatic bool
941bf215546Sopenharmony_ciopt_if_phi_is_condition(nir_builder *b, nir_if *nif)
942bf215546Sopenharmony_ci{
943bf215546Sopenharmony_ci   /* Grab pointers to the last then/else blocks for looking in the phis. */
944bf215546Sopenharmony_ci   nir_block *then_block = nir_if_last_then_block(nif);
945bf215546Sopenharmony_ci   nir_block *else_block = nir_if_last_else_block(nif);
946bf215546Sopenharmony_ci   nir_ssa_def *cond = nif->condition.ssa;
947bf215546Sopenharmony_ci   bool progress = false;
948bf215546Sopenharmony_ci
949bf215546Sopenharmony_ci   nir_block *after_if_block = nir_cf_node_as_block(nir_cf_node_next(&nif->cf_node));
950bf215546Sopenharmony_ci   nir_foreach_instr_safe(instr, after_if_block) {
951bf215546Sopenharmony_ci      if (instr->type != nir_instr_type_phi)
952bf215546Sopenharmony_ci         break;
953bf215546Sopenharmony_ci
954bf215546Sopenharmony_ci      nir_phi_instr *phi = nir_instr_as_phi(instr);
955bf215546Sopenharmony_ci      if (phi->dest.ssa.bit_size != cond->bit_size ||
956bf215546Sopenharmony_ci          phi->dest.ssa.num_components != 1)
957bf215546Sopenharmony_ci         continue;
958bf215546Sopenharmony_ci
959bf215546Sopenharmony_ci      enum opt_bool {
960bf215546Sopenharmony_ci         T, F, UNKNOWN
961bf215546Sopenharmony_ci      } then_val = UNKNOWN, else_val = UNKNOWN;
962bf215546Sopenharmony_ci
963bf215546Sopenharmony_ci      nir_foreach_phi_src(src, phi) {
964bf215546Sopenharmony_ci         assert(src->pred == then_block || src->pred == else_block);
965bf215546Sopenharmony_ci         enum opt_bool *pred_val = src->pred == then_block ? &then_val : &else_val;
966bf215546Sopenharmony_ci
967bf215546Sopenharmony_ci         nir_ssa_scalar val = nir_ssa_scalar_resolved(src->src.ssa, 0);
968bf215546Sopenharmony_ci         if (!nir_ssa_scalar_is_const(val))
969bf215546Sopenharmony_ci            break;
970bf215546Sopenharmony_ci
971bf215546Sopenharmony_ci         if (nir_ssa_scalar_as_int(val) == -1)
972bf215546Sopenharmony_ci            *pred_val = T;
973bf215546Sopenharmony_ci         else if (nir_ssa_scalar_as_uint(val) == 0)
974bf215546Sopenharmony_ci            *pred_val = F;
975bf215546Sopenharmony_ci         else
976bf215546Sopenharmony_ci            break;
977bf215546Sopenharmony_ci      }
978bf215546Sopenharmony_ci      if (then_val == T && else_val == F) {
979bf215546Sopenharmony_ci         nir_ssa_def_rewrite_uses(&phi->dest.ssa, cond);
980bf215546Sopenharmony_ci         progress = true;
981bf215546Sopenharmony_ci      } else if (then_val == F && else_val == T) {
982bf215546Sopenharmony_ci         b->cursor = nir_before_cf_node(&nif->cf_node);
983bf215546Sopenharmony_ci         nir_ssa_def_rewrite_uses(&phi->dest.ssa, nir_inot(b, cond));
984bf215546Sopenharmony_ci         progress = true;
985bf215546Sopenharmony_ci      }
986bf215546Sopenharmony_ci   }
987bf215546Sopenharmony_ci
988bf215546Sopenharmony_ci   return progress;
989bf215546Sopenharmony_ci}
990bf215546Sopenharmony_ci
991bf215546Sopenharmony_ci/**
992bf215546Sopenharmony_ci * This optimization tries to merge two break statements into a single break.
993bf215546Sopenharmony_ci * For this purpose, it checks if both branch legs end in a break or
994bf215546Sopenharmony_ci * if one branch leg ends in a break, and the other one does so after the
995bf215546Sopenharmony_ci * branch.
996bf215546Sopenharmony_ci *
997bf215546Sopenharmony_ci * This optimization turns
998bf215546Sopenharmony_ci *
999bf215546Sopenharmony_ci *     loop {
1000bf215546Sopenharmony_ci *        ...
1001bf215546Sopenharmony_ci *        if (cond) {
1002bf215546Sopenharmony_ci *           do_work_1();
1003bf215546Sopenharmony_ci *           break;
1004bf215546Sopenharmony_ci *        } else {
1005bf215546Sopenharmony_ci *           do_work_2();
1006bf215546Sopenharmony_ci *           break;
1007bf215546Sopenharmony_ci *        }
1008bf215546Sopenharmony_ci *     }
1009bf215546Sopenharmony_ci *
1010bf215546Sopenharmony_ci * into:
1011bf215546Sopenharmony_ci *
1012bf215546Sopenharmony_ci *     loop {
1013bf215546Sopenharmony_ci *        ...
1014bf215546Sopenharmony_ci *        if (cond) {
1015bf215546Sopenharmony_ci *           do_work_1();
1016bf215546Sopenharmony_ci *        } else {
1017bf215546Sopenharmony_ci *           do_work_2();
1018bf215546Sopenharmony_ci *        }
1019bf215546Sopenharmony_ci *        break;
1020bf215546Sopenharmony_ci *     }
1021bf215546Sopenharmony_ci *
1022bf215546Sopenharmony_ci * but also situations like
1023bf215546Sopenharmony_ci *
1024bf215546Sopenharmony_ci *     loop {
1025bf215546Sopenharmony_ci *        ...
1026bf215546Sopenharmony_ci *        if (cond1) {
1027bf215546Sopenharmony_ci *           if (cond2) {
1028bf215546Sopenharmony_ci *              do_work_1();
1029bf215546Sopenharmony_ci *              break;
1030bf215546Sopenharmony_ci *           } else {
1031bf215546Sopenharmony_ci *              do_work_2();
1032bf215546Sopenharmony_ci *           }
1033bf215546Sopenharmony_ci *           do_work_3();
1034bf215546Sopenharmony_ci *           break;
1035bf215546Sopenharmony_ci *        } else {
1036bf215546Sopenharmony_ci *           ...
1037bf215546Sopenharmony_ci *        }
1038bf215546Sopenharmony_ci *     }
1039bf215546Sopenharmony_ci *
1040bf215546Sopenharmony_ci *  into:
1041bf215546Sopenharmony_ci *
1042bf215546Sopenharmony_ci *     loop {
1043bf215546Sopenharmony_ci *        ...
1044bf215546Sopenharmony_ci *        if (cond1) {
1045bf215546Sopenharmony_ci *           if (cond2) {
1046bf215546Sopenharmony_ci *              do_work_1();
1047bf215546Sopenharmony_ci *           } else {
1048bf215546Sopenharmony_ci *              do_work_2();
1049bf215546Sopenharmony_ci *              do_work_3();
1050bf215546Sopenharmony_ci *           }
1051bf215546Sopenharmony_ci *           break;
1052bf215546Sopenharmony_ci *        } else {
1053bf215546Sopenharmony_ci *           ...
1054bf215546Sopenharmony_ci *        }
1055bf215546Sopenharmony_ci *     }
1056bf215546Sopenharmony_ci */
1057bf215546Sopenharmony_cistatic bool
1058bf215546Sopenharmony_ciopt_merge_breaks(nir_if *nif)
1059bf215546Sopenharmony_ci{
1060bf215546Sopenharmony_ci   nir_block *last_then = nir_if_last_then_block(nif);
1061bf215546Sopenharmony_ci   nir_block *last_else = nir_if_last_else_block(nif);
1062bf215546Sopenharmony_ci   bool then_break = nir_block_ends_in_break(last_then);
1063bf215546Sopenharmony_ci   bool else_break = nir_block_ends_in_break(last_else);
1064bf215546Sopenharmony_ci
1065bf215546Sopenharmony_ci   /* If both branch legs end in a break, merge the break after the branch */
1066bf215546Sopenharmony_ci   if (then_break && else_break) {
1067bf215546Sopenharmony_ci      nir_block *after_if = nir_cf_node_cf_tree_next(&nif->cf_node);
1068bf215546Sopenharmony_ci      /* Make sure that the successor is empty.
1069bf215546Sopenharmony_ci       * If not we let nir_opt_dead_cf() clean it up first.
1070bf215546Sopenharmony_ci       */
1071bf215546Sopenharmony_ci      if (!is_block_empty(after_if))
1072bf215546Sopenharmony_ci         return false;
1073bf215546Sopenharmony_ci
1074bf215546Sopenharmony_ci      nir_lower_phis_to_regs_block(last_then->successors[0]);
1075bf215546Sopenharmony_ci      nir_instr_remove_v(nir_block_last_instr(last_then));
1076bf215546Sopenharmony_ci      nir_instr *jump = nir_block_last_instr(last_else);
1077bf215546Sopenharmony_ci      nir_instr_remove_v(jump);
1078bf215546Sopenharmony_ci      nir_instr_insert(nir_after_block(after_if), jump);
1079bf215546Sopenharmony_ci      return true;
1080bf215546Sopenharmony_ci    }
1081bf215546Sopenharmony_ci
1082bf215546Sopenharmony_ci   /* Single break: If there's a break after the branch and the non-breaking
1083bf215546Sopenharmony_ci    * side of the if falls through to it, then hoist that code after up into
1084bf215546Sopenharmony_ci    * the if and leave just a single break there.
1085bf215546Sopenharmony_ci    */
1086bf215546Sopenharmony_ci   if (then_break || else_break) {
1087bf215546Sopenharmony_ci
1088bf215546Sopenharmony_ci      /* At least one branch leg must fall-through */
1089bf215546Sopenharmony_ci      if (nir_block_ends_in_jump(last_then) && nir_block_ends_in_jump(last_else))
1090bf215546Sopenharmony_ci         return false;
1091bf215546Sopenharmony_ci
1092bf215546Sopenharmony_ci      /* Check if there is a single break after the IF */
1093bf215546Sopenharmony_ci      nir_cf_node *first = nir_cf_node_next(&nif->cf_node);
1094bf215546Sopenharmony_ci      nir_cf_node *last = first;
1095bf215546Sopenharmony_ci      while (!nir_cf_node_is_last(last)) {
1096bf215546Sopenharmony_ci         if (contains_other_jump (last, NULL))
1097bf215546Sopenharmony_ci            return false;
1098bf215546Sopenharmony_ci         last = nir_cf_node_next(last);
1099bf215546Sopenharmony_ci      }
1100bf215546Sopenharmony_ci
1101bf215546Sopenharmony_ci      assert(last->type == nir_cf_node_block);
1102bf215546Sopenharmony_ci      if (!nir_block_ends_in_break(nir_cf_node_as_block(last)))
1103bf215546Sopenharmony_ci         return false;
1104bf215546Sopenharmony_ci
1105bf215546Sopenharmony_ci      /* Hoist the code from after the IF into the falling-through branch leg */
1106bf215546Sopenharmony_ci      nir_opt_remove_phis_block(nir_cf_node_as_block(first));
1107bf215546Sopenharmony_ci      nir_block *break_block = then_break ? last_then : last_else;
1108bf215546Sopenharmony_ci      nir_lower_phis_to_regs_block(break_block->successors[0]);
1109bf215546Sopenharmony_ci
1110bf215546Sopenharmony_ci      nir_cf_list tmp;
1111bf215546Sopenharmony_ci      nir_cf_extract(&tmp, nir_before_cf_node(first),
1112bf215546Sopenharmony_ci                           nir_after_block_before_jump(nir_cf_node_as_block(last)));
1113bf215546Sopenharmony_ci      if (then_break)
1114bf215546Sopenharmony_ci         nir_cf_reinsert(&tmp, nir_after_block(last_else));
1115bf215546Sopenharmony_ci      else
1116bf215546Sopenharmony_ci         nir_cf_reinsert(&tmp, nir_after_block(last_then));
1117bf215546Sopenharmony_ci
1118bf215546Sopenharmony_ci      nir_instr_remove_v(nir_block_last_instr(break_block));
1119bf215546Sopenharmony_ci      return true;
1120bf215546Sopenharmony_ci   }
1121bf215546Sopenharmony_ci
1122bf215546Sopenharmony_ci   return false;
1123bf215546Sopenharmony_ci}
1124bf215546Sopenharmony_ci
1125bf215546Sopenharmony_ci/**
1126bf215546Sopenharmony_ci * This optimization simplifies potential loop terminators which then allows
1127bf215546Sopenharmony_ci * other passes such as opt_if_simplification() and loop unrolling to progress
1128bf215546Sopenharmony_ci * further:
1129bf215546Sopenharmony_ci *
1130bf215546Sopenharmony_ci *     if (cond) {
1131bf215546Sopenharmony_ci *        ... then block instructions ...
1132bf215546Sopenharmony_ci *     } else {
1133bf215546Sopenharmony_ci *         ...
1134bf215546Sopenharmony_ci *        break;
1135bf215546Sopenharmony_ci *     }
1136bf215546Sopenharmony_ci *
1137bf215546Sopenharmony_ci * into:
1138bf215546Sopenharmony_ci *
1139bf215546Sopenharmony_ci *     if (cond) {
1140bf215546Sopenharmony_ci *     } else {
1141bf215546Sopenharmony_ci *         ...
1142bf215546Sopenharmony_ci *        break;
1143bf215546Sopenharmony_ci *     }
1144bf215546Sopenharmony_ci *     ... then block instructions ...
1145bf215546Sopenharmony_ci */
1146bf215546Sopenharmony_cistatic bool
1147bf215546Sopenharmony_ciopt_if_loop_terminator(nir_if *nif)
1148bf215546Sopenharmony_ci{
1149bf215546Sopenharmony_ci   nir_block *break_blk = NULL;
1150bf215546Sopenharmony_ci   nir_block *continue_from_blk = NULL;
1151bf215546Sopenharmony_ci   bool continue_from_then = true;
1152bf215546Sopenharmony_ci
1153bf215546Sopenharmony_ci   nir_block *last_then = nir_if_last_then_block(nif);
1154bf215546Sopenharmony_ci   nir_block *last_else = nir_if_last_else_block(nif);
1155bf215546Sopenharmony_ci
1156bf215546Sopenharmony_ci   if (nir_block_ends_in_break(last_then)) {
1157bf215546Sopenharmony_ci      break_blk = last_then;
1158bf215546Sopenharmony_ci      continue_from_blk = last_else;
1159bf215546Sopenharmony_ci      continue_from_then = false;
1160bf215546Sopenharmony_ci   } else if (nir_block_ends_in_break(last_else)) {
1161bf215546Sopenharmony_ci      break_blk = last_else;
1162bf215546Sopenharmony_ci      continue_from_blk = last_then;
1163bf215546Sopenharmony_ci   }
1164bf215546Sopenharmony_ci
1165bf215546Sopenharmony_ci   /* Continue if the if-statement contained no jumps at all */
1166bf215546Sopenharmony_ci   if (!break_blk)
1167bf215546Sopenharmony_ci      return false;
1168bf215546Sopenharmony_ci
1169bf215546Sopenharmony_ci   /* If the continue from block is empty then return as there is nothing to
1170bf215546Sopenharmony_ci    * move.
1171bf215546Sopenharmony_ci    */
1172bf215546Sopenharmony_ci   nir_block *first_continue_from_blk = continue_from_then ?
1173bf215546Sopenharmony_ci      nir_if_first_then_block(nif) :
1174bf215546Sopenharmony_ci      nir_if_first_else_block(nif);
1175bf215546Sopenharmony_ci   if (is_block_empty(first_continue_from_blk))
1176bf215546Sopenharmony_ci      return false;
1177bf215546Sopenharmony_ci
1178bf215546Sopenharmony_ci   if (nir_block_ends_in_jump(continue_from_blk))
1179bf215546Sopenharmony_ci      return false;
1180bf215546Sopenharmony_ci
1181bf215546Sopenharmony_ci   /* Even though this if statement has a jump on one side, we may still have
1182bf215546Sopenharmony_ci    * phis afterwards.  Single-source phis can be produced by loop unrolling
1183bf215546Sopenharmony_ci    * or dead control-flow passes and are perfectly legal.  Run a quick phi
1184bf215546Sopenharmony_ci    * removal on the block after the if to clean up any such phis.
1185bf215546Sopenharmony_ci    */
1186bf215546Sopenharmony_ci   nir_opt_remove_phis_block(nir_cf_node_as_block(nir_cf_node_next(&nif->cf_node)));
1187bf215546Sopenharmony_ci
1188bf215546Sopenharmony_ci   /* Finally, move the continue from branch after the if-statement. */
1189bf215546Sopenharmony_ci   nir_cf_list tmp;
1190bf215546Sopenharmony_ci   nir_cf_extract(&tmp, nir_before_block(first_continue_from_blk),
1191bf215546Sopenharmony_ci                        nir_after_block(continue_from_blk));
1192bf215546Sopenharmony_ci   nir_cf_reinsert(&tmp, nir_after_cf_node(&nif->cf_node));
1193bf215546Sopenharmony_ci
1194bf215546Sopenharmony_ci   return true;
1195bf215546Sopenharmony_ci}
1196bf215546Sopenharmony_ci
1197bf215546Sopenharmony_cistatic bool
1198bf215546Sopenharmony_cievaluate_if_condition(nir_if *nif, nir_cursor cursor, bool *value)
1199bf215546Sopenharmony_ci{
1200bf215546Sopenharmony_ci   nir_block *use_block = nir_cursor_current_block(cursor);
1201bf215546Sopenharmony_ci   if (nir_block_dominates(nir_if_first_then_block(nif), use_block)) {
1202bf215546Sopenharmony_ci      *value = true;
1203bf215546Sopenharmony_ci      return true;
1204bf215546Sopenharmony_ci   } else if (nir_block_dominates(nir_if_first_else_block(nif), use_block)) {
1205bf215546Sopenharmony_ci      *value = false;
1206bf215546Sopenharmony_ci      return true;
1207bf215546Sopenharmony_ci   } else {
1208bf215546Sopenharmony_ci      return false;
1209bf215546Sopenharmony_ci   }
1210bf215546Sopenharmony_ci}
1211bf215546Sopenharmony_ci
1212bf215546Sopenharmony_cistatic nir_ssa_def *
1213bf215546Sopenharmony_ciclone_alu_and_replace_src_defs(nir_builder *b, const nir_alu_instr *alu,
1214bf215546Sopenharmony_ci                               nir_ssa_def **src_defs)
1215bf215546Sopenharmony_ci{
1216bf215546Sopenharmony_ci   nir_alu_instr *nalu = nir_alu_instr_create(b->shader, alu->op);
1217bf215546Sopenharmony_ci   nalu->exact = alu->exact;
1218bf215546Sopenharmony_ci
1219bf215546Sopenharmony_ci   nir_ssa_dest_init(&nalu->instr, &nalu->dest.dest,
1220bf215546Sopenharmony_ci                     alu->dest.dest.ssa.num_components,
1221bf215546Sopenharmony_ci                     alu->dest.dest.ssa.bit_size, NULL);
1222bf215546Sopenharmony_ci
1223bf215546Sopenharmony_ci   nalu->dest.saturate = alu->dest.saturate;
1224bf215546Sopenharmony_ci   nalu->dest.write_mask = alu->dest.write_mask;
1225bf215546Sopenharmony_ci
1226bf215546Sopenharmony_ci   for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
1227bf215546Sopenharmony_ci      assert(alu->src[i].src.is_ssa);
1228bf215546Sopenharmony_ci      nalu->src[i].src = nir_src_for_ssa(src_defs[i]);
1229bf215546Sopenharmony_ci      nalu->src[i].negate = alu->src[i].negate;
1230bf215546Sopenharmony_ci      nalu->src[i].abs = alu->src[i].abs;
1231bf215546Sopenharmony_ci      memcpy(nalu->src[i].swizzle, alu->src[i].swizzle,
1232bf215546Sopenharmony_ci             sizeof(nalu->src[i].swizzle));
1233bf215546Sopenharmony_ci   }
1234bf215546Sopenharmony_ci
1235bf215546Sopenharmony_ci   nir_builder_instr_insert(b, &nalu->instr);
1236bf215546Sopenharmony_ci
1237bf215546Sopenharmony_ci   return &nalu->dest.dest.ssa;;
1238bf215546Sopenharmony_ci}
1239bf215546Sopenharmony_ci
1240bf215546Sopenharmony_ci/*
1241bf215546Sopenharmony_ci * This propagates if condition evaluation down the chain of some alu
1242bf215546Sopenharmony_ci * instructions. For example by checking the use of some of the following alu
1243bf215546Sopenharmony_ci * instruction we can eventually replace ssa_107 with NIR_TRUE.
1244bf215546Sopenharmony_ci *
1245bf215546Sopenharmony_ci *   loop {
1246bf215546Sopenharmony_ci *      block block_1:
1247bf215546Sopenharmony_ci *      vec1 32 ssa_85 = load_const (0x00000002)
1248bf215546Sopenharmony_ci *      vec1 32 ssa_86 = ieq ssa_48, ssa_85
1249bf215546Sopenharmony_ci *      vec1 32 ssa_87 = load_const (0x00000001)
1250bf215546Sopenharmony_ci *      vec1 32 ssa_88 = ieq ssa_48, ssa_87
1251bf215546Sopenharmony_ci *      vec1 32 ssa_89 = ior ssa_86, ssa_88
1252bf215546Sopenharmony_ci *      vec1 32 ssa_90 = ieq ssa_48, ssa_0
1253bf215546Sopenharmony_ci *      vec1 32 ssa_91 = ior ssa_89, ssa_90
1254bf215546Sopenharmony_ci *      if ssa_86 {
1255bf215546Sopenharmony_ci *         block block_2:
1256bf215546Sopenharmony_ci *             ...
1257bf215546Sopenharmony_ci *            break
1258bf215546Sopenharmony_ci *      } else {
1259bf215546Sopenharmony_ci *            block block_3:
1260bf215546Sopenharmony_ci *      }
1261bf215546Sopenharmony_ci *      block block_4:
1262bf215546Sopenharmony_ci *      if ssa_88 {
1263bf215546Sopenharmony_ci *            block block_5:
1264bf215546Sopenharmony_ci *             ...
1265bf215546Sopenharmony_ci *            break
1266bf215546Sopenharmony_ci *      } else {
1267bf215546Sopenharmony_ci *            block block_6:
1268bf215546Sopenharmony_ci *      }
1269bf215546Sopenharmony_ci *      block block_7:
1270bf215546Sopenharmony_ci *      if ssa_90 {
1271bf215546Sopenharmony_ci *            block block_8:
1272bf215546Sopenharmony_ci *             ...
1273bf215546Sopenharmony_ci *            break
1274bf215546Sopenharmony_ci *      } else {
1275bf215546Sopenharmony_ci *            block block_9:
1276bf215546Sopenharmony_ci *      }
1277bf215546Sopenharmony_ci *      block block_10:
1278bf215546Sopenharmony_ci *      vec1 32 ssa_107 = inot ssa_91
1279bf215546Sopenharmony_ci *      if ssa_107 {
1280bf215546Sopenharmony_ci *            block block_11:
1281bf215546Sopenharmony_ci *            break
1282bf215546Sopenharmony_ci *      } else {
1283bf215546Sopenharmony_ci *            block block_12:
1284bf215546Sopenharmony_ci *      }
1285bf215546Sopenharmony_ci *   }
1286bf215546Sopenharmony_ci */
1287bf215546Sopenharmony_cistatic bool
1288bf215546Sopenharmony_cipropagate_condition_eval(nir_builder *b, nir_if *nif, nir_src *use_src,
1289bf215546Sopenharmony_ci                         nir_src *alu_use, nir_alu_instr *alu,
1290bf215546Sopenharmony_ci                         bool is_if_condition)
1291bf215546Sopenharmony_ci{
1292bf215546Sopenharmony_ci   bool bool_value;
1293bf215546Sopenharmony_ci   b->cursor = nir_before_src(alu_use, is_if_condition);
1294bf215546Sopenharmony_ci   if (!evaluate_if_condition(nif, b->cursor, &bool_value))
1295bf215546Sopenharmony_ci      return false;
1296bf215546Sopenharmony_ci
1297bf215546Sopenharmony_ci   nir_ssa_def *def[NIR_MAX_VEC_COMPONENTS] = {0};
1298bf215546Sopenharmony_ci   for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
1299bf215546Sopenharmony_ci      if (alu->src[i].src.ssa == use_src->ssa) {
1300bf215546Sopenharmony_ci         def[i] = nir_imm_bool(b, bool_value);
1301bf215546Sopenharmony_ci      } else {
1302bf215546Sopenharmony_ci         def[i] = alu->src[i].src.ssa;
1303bf215546Sopenharmony_ci      }
1304bf215546Sopenharmony_ci   }
1305bf215546Sopenharmony_ci
1306bf215546Sopenharmony_ci   nir_ssa_def *nalu = clone_alu_and_replace_src_defs(b, alu, def);
1307bf215546Sopenharmony_ci
1308bf215546Sopenharmony_ci   /* Rewrite use to use new alu instruction */
1309bf215546Sopenharmony_ci   nir_src new_src = nir_src_for_ssa(nalu);
1310bf215546Sopenharmony_ci
1311bf215546Sopenharmony_ci   if (is_if_condition)
1312bf215546Sopenharmony_ci      nir_if_rewrite_condition(alu_use->parent_if, new_src);
1313bf215546Sopenharmony_ci   else
1314bf215546Sopenharmony_ci      nir_instr_rewrite_src(alu_use->parent_instr, alu_use, new_src);
1315bf215546Sopenharmony_ci
1316bf215546Sopenharmony_ci   return true;
1317bf215546Sopenharmony_ci}
1318bf215546Sopenharmony_ci
1319bf215546Sopenharmony_cistatic bool
1320bf215546Sopenharmony_cican_propagate_through_alu(nir_src *src)
1321bf215546Sopenharmony_ci{
1322bf215546Sopenharmony_ci   if (src->parent_instr->type != nir_instr_type_alu)
1323bf215546Sopenharmony_ci      return false;
1324bf215546Sopenharmony_ci
1325bf215546Sopenharmony_ci   nir_alu_instr *alu = nir_instr_as_alu(src->parent_instr);
1326bf215546Sopenharmony_ci   switch (alu->op) {
1327bf215546Sopenharmony_ci      case nir_op_ior:
1328bf215546Sopenharmony_ci      case nir_op_iand:
1329bf215546Sopenharmony_ci      case nir_op_inot:
1330bf215546Sopenharmony_ci      case nir_op_b2i32:
1331bf215546Sopenharmony_ci         return true;
1332bf215546Sopenharmony_ci      case nir_op_bcsel:
1333bf215546Sopenharmony_ci         return src == &alu->src[0].src;
1334bf215546Sopenharmony_ci      default:
1335bf215546Sopenharmony_ci         return false;
1336bf215546Sopenharmony_ci   }
1337bf215546Sopenharmony_ci}
1338bf215546Sopenharmony_ci
1339bf215546Sopenharmony_cistatic bool
1340bf215546Sopenharmony_cievaluate_condition_use(nir_builder *b, nir_if *nif, nir_src *use_src,
1341bf215546Sopenharmony_ci                       bool is_if_condition)
1342bf215546Sopenharmony_ci{
1343bf215546Sopenharmony_ci   bool progress = false;
1344bf215546Sopenharmony_ci
1345bf215546Sopenharmony_ci   b->cursor = nir_before_src(use_src, is_if_condition);
1346bf215546Sopenharmony_ci
1347bf215546Sopenharmony_ci   bool bool_value;
1348bf215546Sopenharmony_ci   if (evaluate_if_condition(nif, b->cursor, &bool_value)) {
1349bf215546Sopenharmony_ci      /* Rewrite use to use const */
1350bf215546Sopenharmony_ci      nir_src imm_src = nir_src_for_ssa(nir_imm_bool(b, bool_value));
1351bf215546Sopenharmony_ci      if (is_if_condition)
1352bf215546Sopenharmony_ci         nir_if_rewrite_condition(use_src->parent_if, imm_src);
1353bf215546Sopenharmony_ci      else
1354bf215546Sopenharmony_ci         nir_instr_rewrite_src(use_src->parent_instr, use_src, imm_src);
1355bf215546Sopenharmony_ci
1356bf215546Sopenharmony_ci      progress = true;
1357bf215546Sopenharmony_ci   }
1358bf215546Sopenharmony_ci
1359bf215546Sopenharmony_ci   if (!is_if_condition && can_propagate_through_alu(use_src)) {
1360bf215546Sopenharmony_ci      nir_alu_instr *alu = nir_instr_as_alu(use_src->parent_instr);
1361bf215546Sopenharmony_ci
1362bf215546Sopenharmony_ci      nir_foreach_use_safe(alu_use, &alu->dest.dest.ssa) {
1363bf215546Sopenharmony_ci         progress |= propagate_condition_eval(b, nif, use_src, alu_use, alu,
1364bf215546Sopenharmony_ci                                              false);
1365bf215546Sopenharmony_ci      }
1366bf215546Sopenharmony_ci
1367bf215546Sopenharmony_ci      nir_foreach_if_use_safe(alu_use, &alu->dest.dest.ssa) {
1368bf215546Sopenharmony_ci         progress |= propagate_condition_eval(b, nif, use_src, alu_use, alu,
1369bf215546Sopenharmony_ci                                              true);
1370bf215546Sopenharmony_ci      }
1371bf215546Sopenharmony_ci   }
1372bf215546Sopenharmony_ci
1373bf215546Sopenharmony_ci   return progress;
1374bf215546Sopenharmony_ci}
1375bf215546Sopenharmony_ci
1376bf215546Sopenharmony_cistatic bool
1377bf215546Sopenharmony_ciopt_if_evaluate_condition_use(nir_builder *b, nir_if *nif)
1378bf215546Sopenharmony_ci{
1379bf215546Sopenharmony_ci   bool progress = false;
1380bf215546Sopenharmony_ci
1381bf215546Sopenharmony_ci   /* Evaluate any uses of the if condition inside the if branches */
1382bf215546Sopenharmony_ci   assert(nif->condition.is_ssa);
1383bf215546Sopenharmony_ci   nir_foreach_use_safe(use_src, nif->condition.ssa) {
1384bf215546Sopenharmony_ci      progress |= evaluate_condition_use(b, nif, use_src, false);
1385bf215546Sopenharmony_ci   }
1386bf215546Sopenharmony_ci
1387bf215546Sopenharmony_ci   nir_foreach_if_use_safe(use_src, nif->condition.ssa) {
1388bf215546Sopenharmony_ci      if (use_src->parent_if != nif)
1389bf215546Sopenharmony_ci         progress |= evaluate_condition_use(b, nif, use_src, true);
1390bf215546Sopenharmony_ci   }
1391bf215546Sopenharmony_ci
1392bf215546Sopenharmony_ci   return progress;
1393bf215546Sopenharmony_ci}
1394bf215546Sopenharmony_ci
1395bf215546Sopenharmony_cistatic bool
1396bf215546Sopenharmony_cirewrite_comp_uses_within_if(nir_builder *b, nir_if *nif, bool invert,
1397bf215546Sopenharmony_ci                            nir_ssa_scalar scalar, nir_ssa_scalar new_scalar)
1398bf215546Sopenharmony_ci{
1399bf215546Sopenharmony_ci   bool progress = false;
1400bf215546Sopenharmony_ci
1401bf215546Sopenharmony_ci   nir_block *first = invert ? nir_if_first_else_block(nif) : nir_if_first_then_block(nif);
1402bf215546Sopenharmony_ci   nir_block *last = invert ? nir_if_last_else_block(nif) : nir_if_last_then_block(nif);
1403bf215546Sopenharmony_ci
1404bf215546Sopenharmony_ci   nir_ssa_def *new_ssa = NULL;
1405bf215546Sopenharmony_ci   nir_foreach_use_safe(use, scalar.def) {
1406bf215546Sopenharmony_ci      if (use->parent_instr->block->index < first->index ||
1407bf215546Sopenharmony_ci          use->parent_instr->block->index > last->index)
1408bf215546Sopenharmony_ci         continue;
1409bf215546Sopenharmony_ci
1410bf215546Sopenharmony_ci      /* Only rewrite users which use only the new component. This is to avoid a
1411bf215546Sopenharmony_ci       * situation where copy propagation will undo the rewrite and we risk an infinite
1412bf215546Sopenharmony_ci       * loop.
1413bf215546Sopenharmony_ci       *
1414bf215546Sopenharmony_ci       * We could rewrite users which use a mix of the old and new components, but if
1415bf215546Sopenharmony_ci       * nir_src_components_read() is incomplete, then we risk the new component actually being
1416bf215546Sopenharmony_ci       * unused and some optimization later undoing the rewrite.
1417bf215546Sopenharmony_ci       */
1418bf215546Sopenharmony_ci      if (nir_src_components_read(use) != BITFIELD64_BIT(scalar.comp))
1419bf215546Sopenharmony_ci         continue;
1420bf215546Sopenharmony_ci
1421bf215546Sopenharmony_ci      if (!new_ssa) {
1422bf215546Sopenharmony_ci         b->cursor = nir_before_cf_node(&nif->cf_node);
1423bf215546Sopenharmony_ci         new_ssa = nir_channel(b, new_scalar.def, new_scalar.comp);
1424bf215546Sopenharmony_ci         if (scalar.def->num_components > 1) {
1425bf215546Sopenharmony_ci            nir_ssa_def *vec = nir_ssa_undef(b, scalar.def->num_components, scalar.def->bit_size);
1426bf215546Sopenharmony_ci            new_ssa = nir_vector_insert_imm(b, vec, new_ssa, scalar.comp);
1427bf215546Sopenharmony_ci         }
1428bf215546Sopenharmony_ci      }
1429bf215546Sopenharmony_ci
1430bf215546Sopenharmony_ci      nir_instr_rewrite_src_ssa(use->parent_instr, use, new_ssa);
1431bf215546Sopenharmony_ci      progress = true;
1432bf215546Sopenharmony_ci   }
1433bf215546Sopenharmony_ci
1434bf215546Sopenharmony_ci   return progress;
1435bf215546Sopenharmony_ci}
1436bf215546Sopenharmony_ci
1437bf215546Sopenharmony_ci/*
1438bf215546Sopenharmony_ci * This optimization turns:
1439bf215546Sopenharmony_ci *
1440bf215546Sopenharmony_ci *     if (a == (b=readfirstlane(a)))
1441bf215546Sopenharmony_ci *        use(a)
1442bf215546Sopenharmony_ci *     if (c == (d=load_const))
1443bf215546Sopenharmony_ci *        use(c)
1444bf215546Sopenharmony_ci *
1445bf215546Sopenharmony_ci * into:
1446bf215546Sopenharmony_ci *
1447bf215546Sopenharmony_ci *     if (a == (b=readfirstlane(a)))
1448bf215546Sopenharmony_ci *        use(b)
1449bf215546Sopenharmony_ci *     if (c == (d=load_const))
1450bf215546Sopenharmony_ci *        use(d)
1451bf215546Sopenharmony_ci*/
1452bf215546Sopenharmony_cistatic bool
1453bf215546Sopenharmony_ciopt_if_rewrite_uniform_uses(nir_builder *b, nir_if *nif, nir_ssa_scalar cond, bool accept_ine)
1454bf215546Sopenharmony_ci{
1455bf215546Sopenharmony_ci   bool progress = false;
1456bf215546Sopenharmony_ci
1457bf215546Sopenharmony_ci   if (!nir_ssa_scalar_is_alu(cond))
1458bf215546Sopenharmony_ci      return false;
1459bf215546Sopenharmony_ci
1460bf215546Sopenharmony_ci   nir_op op = nir_ssa_scalar_alu_op(cond);
1461bf215546Sopenharmony_ci   if (op == nir_op_iand) {
1462bf215546Sopenharmony_ci      progress |= opt_if_rewrite_uniform_uses(b, nif, nir_ssa_scalar_chase_alu_src(cond, 0), false);
1463bf215546Sopenharmony_ci      progress |= opt_if_rewrite_uniform_uses(b, nif, nir_ssa_scalar_chase_alu_src(cond, 1), false);
1464bf215546Sopenharmony_ci      return progress;
1465bf215546Sopenharmony_ci   }
1466bf215546Sopenharmony_ci
1467bf215546Sopenharmony_ci   if (op != nir_op_ieq && (op != nir_op_ine || !accept_ine))
1468bf215546Sopenharmony_ci      return false;
1469bf215546Sopenharmony_ci
1470bf215546Sopenharmony_ci   for (unsigned i = 0; i < 2; i++) {
1471bf215546Sopenharmony_ci      nir_ssa_scalar src_uni = nir_ssa_scalar_chase_alu_src(cond, i);
1472bf215546Sopenharmony_ci      nir_ssa_scalar src_div = nir_ssa_scalar_chase_alu_src(cond, !i);
1473bf215546Sopenharmony_ci
1474bf215546Sopenharmony_ci      if (src_uni.def->parent_instr->type == nir_instr_type_load_const && src_div.def != src_uni.def)
1475bf215546Sopenharmony_ci         return rewrite_comp_uses_within_if(b, nif, op == nir_op_ine, src_div, src_uni);
1476bf215546Sopenharmony_ci
1477bf215546Sopenharmony_ci      if (src_uni.def->parent_instr->type != nir_instr_type_intrinsic)
1478bf215546Sopenharmony_ci         continue;
1479bf215546Sopenharmony_ci      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(src_uni.def->parent_instr);
1480bf215546Sopenharmony_ci      if (intrin->intrinsic != nir_intrinsic_read_first_invocation &&
1481bf215546Sopenharmony_ci          (intrin->intrinsic != nir_intrinsic_reduce || nir_intrinsic_cluster_size(intrin)))
1482bf215546Sopenharmony_ci         continue;
1483bf215546Sopenharmony_ci
1484bf215546Sopenharmony_ci      nir_ssa_scalar intrin_src = {intrin->src[0].ssa, src_uni.comp};
1485bf215546Sopenharmony_ci      nir_ssa_scalar resolved_intrin_src = nir_ssa_scalar_resolved(intrin_src.def, intrin_src.comp);
1486bf215546Sopenharmony_ci
1487bf215546Sopenharmony_ci      if (resolved_intrin_src.comp != src_div.comp || resolved_intrin_src.def != src_div.def)
1488bf215546Sopenharmony_ci         continue;
1489bf215546Sopenharmony_ci
1490bf215546Sopenharmony_ci      progress |= rewrite_comp_uses_within_if(b, nif, op == nir_op_ine, resolved_intrin_src, src_uni);
1491bf215546Sopenharmony_ci      if (intrin_src.comp != resolved_intrin_src.comp || intrin_src.def != resolved_intrin_src.def)
1492bf215546Sopenharmony_ci         progress |= rewrite_comp_uses_within_if(b, nif, op == nir_op_ine, intrin_src, src_uni);
1493bf215546Sopenharmony_ci
1494bf215546Sopenharmony_ci      return progress;
1495bf215546Sopenharmony_ci   }
1496bf215546Sopenharmony_ci
1497bf215546Sopenharmony_ci   return false;
1498bf215546Sopenharmony_ci}
1499bf215546Sopenharmony_ci
1500bf215546Sopenharmony_cistatic void
1501bf215546Sopenharmony_cisimple_merge_if(nir_if *dest_if, nir_if *src_if, bool dest_if_then,
1502bf215546Sopenharmony_ci                bool src_if_then)
1503bf215546Sopenharmony_ci{
1504bf215546Sopenharmony_ci   /* Now merge the if branch */
1505bf215546Sopenharmony_ci   nir_block *dest_blk = dest_if_then ? nir_if_last_then_block(dest_if)
1506bf215546Sopenharmony_ci                                      : nir_if_last_else_block(dest_if);
1507bf215546Sopenharmony_ci
1508bf215546Sopenharmony_ci   struct exec_list *list = src_if_then ? &src_if->then_list
1509bf215546Sopenharmony_ci                                        : &src_if->else_list;
1510bf215546Sopenharmony_ci
1511bf215546Sopenharmony_ci   nir_cf_list if_cf_list;
1512bf215546Sopenharmony_ci   nir_cf_extract(&if_cf_list, nir_before_cf_list(list),
1513bf215546Sopenharmony_ci                  nir_after_cf_list(list));
1514bf215546Sopenharmony_ci   nir_cf_reinsert(&if_cf_list, nir_after_block(dest_blk));
1515bf215546Sopenharmony_ci}
1516bf215546Sopenharmony_ci
1517bf215546Sopenharmony_cistatic bool
1518bf215546Sopenharmony_ciopt_if_merge(nir_if *nif)
1519bf215546Sopenharmony_ci{
1520bf215546Sopenharmony_ci   bool progress = false;
1521bf215546Sopenharmony_ci
1522bf215546Sopenharmony_ci   nir_block *next_blk = nir_cf_node_cf_tree_next(&nif->cf_node);
1523bf215546Sopenharmony_ci   if (!next_blk || !nif->condition.is_ssa)
1524bf215546Sopenharmony_ci      return false;
1525bf215546Sopenharmony_ci
1526bf215546Sopenharmony_ci   nir_if *next_if = nir_block_get_following_if(next_blk);
1527bf215546Sopenharmony_ci   if (!next_if || !next_if->condition.is_ssa)
1528bf215546Sopenharmony_ci      return false;
1529bf215546Sopenharmony_ci
1530bf215546Sopenharmony_ci   /* Here we merge two consecutive ifs that have the same condition e.g:
1531bf215546Sopenharmony_ci    *
1532bf215546Sopenharmony_ci    *   if ssa_12 {
1533bf215546Sopenharmony_ci    *      ...
1534bf215546Sopenharmony_ci    *   } else {
1535bf215546Sopenharmony_ci    *      ...
1536bf215546Sopenharmony_ci    *   }
1537bf215546Sopenharmony_ci    *   if ssa_12 {
1538bf215546Sopenharmony_ci    *      ...
1539bf215546Sopenharmony_ci    *   } else {
1540bf215546Sopenharmony_ci    *      ...
1541bf215546Sopenharmony_ci    *   }
1542bf215546Sopenharmony_ci    *
1543bf215546Sopenharmony_ci    * Note: This only merges if-statements when the block between them is
1544bf215546Sopenharmony_ci    * empty. The reason we don't try to merge ifs that just have phis between
1545bf215546Sopenharmony_ci    * them is because this can result in increased register pressure. For
1546bf215546Sopenharmony_ci    * example when merging if ladders created by indirect indexing.
1547bf215546Sopenharmony_ci    */
1548bf215546Sopenharmony_ci   if (nif->condition.ssa == next_if->condition.ssa &&
1549bf215546Sopenharmony_ci       exec_list_is_empty(&next_blk->instr_list)) {
1550bf215546Sopenharmony_ci
1551bf215546Sopenharmony_ci      /* This optimization isn't made to work in this case and
1552bf215546Sopenharmony_ci       * opt_if_evaluate_condition_use will optimize it later.
1553bf215546Sopenharmony_ci       */
1554bf215546Sopenharmony_ci      if (nir_block_ends_in_jump(nir_if_last_then_block(nif)) ||
1555bf215546Sopenharmony_ci          nir_block_ends_in_jump(nir_if_last_else_block(nif)))
1556bf215546Sopenharmony_ci         return false;
1557bf215546Sopenharmony_ci
1558bf215546Sopenharmony_ci      simple_merge_if(nif, next_if, true, true);
1559bf215546Sopenharmony_ci      simple_merge_if(nif, next_if, false, false);
1560bf215546Sopenharmony_ci
1561bf215546Sopenharmony_ci      nir_block *new_then_block = nir_if_last_then_block(nif);
1562bf215546Sopenharmony_ci      nir_block *new_else_block = nir_if_last_else_block(nif);
1563bf215546Sopenharmony_ci
1564bf215546Sopenharmony_ci      nir_block *old_then_block = nir_if_last_then_block(next_if);
1565bf215546Sopenharmony_ci      nir_block *old_else_block = nir_if_last_else_block(next_if);
1566bf215546Sopenharmony_ci
1567bf215546Sopenharmony_ci      /* Rewrite the predecessor block for any phis following the second
1568bf215546Sopenharmony_ci       * if-statement.
1569bf215546Sopenharmony_ci       */
1570bf215546Sopenharmony_ci      rewrite_phi_predecessor_blocks(next_if, old_then_block,
1571bf215546Sopenharmony_ci                                     old_else_block,
1572bf215546Sopenharmony_ci                                     new_then_block,
1573bf215546Sopenharmony_ci                                     new_else_block);
1574bf215546Sopenharmony_ci
1575bf215546Sopenharmony_ci      /* Move phis after merged if to avoid them being deleted when we remove
1576bf215546Sopenharmony_ci       * the merged if-statement.
1577bf215546Sopenharmony_ci       */
1578bf215546Sopenharmony_ci      nir_block *after_next_if_block =
1579bf215546Sopenharmony_ci         nir_cf_node_as_block(nir_cf_node_next(&next_if->cf_node));
1580bf215546Sopenharmony_ci
1581bf215546Sopenharmony_ci      nir_foreach_instr_safe(instr, after_next_if_block) {
1582bf215546Sopenharmony_ci         if (instr->type != nir_instr_type_phi)
1583bf215546Sopenharmony_ci            break;
1584bf215546Sopenharmony_ci
1585bf215546Sopenharmony_ci         exec_node_remove(&instr->node);
1586bf215546Sopenharmony_ci         exec_list_push_tail(&next_blk->instr_list, &instr->node);
1587bf215546Sopenharmony_ci         instr->block = next_blk;
1588bf215546Sopenharmony_ci      }
1589bf215546Sopenharmony_ci
1590bf215546Sopenharmony_ci      nir_cf_node_remove(&next_if->cf_node);
1591bf215546Sopenharmony_ci
1592bf215546Sopenharmony_ci      progress = true;
1593bf215546Sopenharmony_ci   }
1594bf215546Sopenharmony_ci
1595bf215546Sopenharmony_ci   return progress;
1596bf215546Sopenharmony_ci}
1597bf215546Sopenharmony_ci
1598bf215546Sopenharmony_cistatic bool
1599bf215546Sopenharmony_ciopt_if_cf_list(nir_builder *b, struct exec_list *cf_list,
1600bf215546Sopenharmony_ci               nir_opt_if_options options)
1601bf215546Sopenharmony_ci{
1602bf215546Sopenharmony_ci   bool progress = false;
1603bf215546Sopenharmony_ci   foreach_list_typed(nir_cf_node, cf_node, node, cf_list) {
1604bf215546Sopenharmony_ci      switch (cf_node->type) {
1605bf215546Sopenharmony_ci      case nir_cf_node_block:
1606bf215546Sopenharmony_ci         break;
1607bf215546Sopenharmony_ci
1608bf215546Sopenharmony_ci      case nir_cf_node_if: {
1609bf215546Sopenharmony_ci         nir_if *nif = nir_cf_node_as_if(cf_node);
1610bf215546Sopenharmony_ci         progress |= opt_if_cf_list(b, &nif->then_list,
1611bf215546Sopenharmony_ci                                    options);
1612bf215546Sopenharmony_ci         progress |= opt_if_cf_list(b, &nif->else_list,
1613bf215546Sopenharmony_ci                                    options);
1614bf215546Sopenharmony_ci         progress |= opt_if_loop_terminator(nif);
1615bf215546Sopenharmony_ci         progress |= opt_if_merge(nif);
1616bf215546Sopenharmony_ci         progress |= opt_if_simplification(b, nif);
1617bf215546Sopenharmony_ci         if (options & nir_opt_if_optimize_phi_true_false)
1618bf215546Sopenharmony_ci            progress |= opt_if_phi_is_condition(b, nif);
1619bf215546Sopenharmony_ci         break;
1620bf215546Sopenharmony_ci      }
1621bf215546Sopenharmony_ci
1622bf215546Sopenharmony_ci      case nir_cf_node_loop: {
1623bf215546Sopenharmony_ci         nir_loop *loop = nir_cf_node_as_loop(cf_node);
1624bf215546Sopenharmony_ci         progress |= opt_if_cf_list(b, &loop->body,
1625bf215546Sopenharmony_ci                                    options);
1626bf215546Sopenharmony_ci         progress |= opt_simplify_bcsel_of_phi(b, loop);
1627bf215546Sopenharmony_ci         progress |= opt_if_loop_last_continue(loop,
1628bf215546Sopenharmony_ci                                               options & nir_opt_if_aggressive_last_continue);
1629bf215546Sopenharmony_ci         break;
1630bf215546Sopenharmony_ci      }
1631bf215546Sopenharmony_ci
1632bf215546Sopenharmony_ci      case nir_cf_node_function:
1633bf215546Sopenharmony_ci         unreachable("Invalid cf type");
1634bf215546Sopenharmony_ci      }
1635bf215546Sopenharmony_ci   }
1636bf215546Sopenharmony_ci
1637bf215546Sopenharmony_ci   return progress;
1638bf215546Sopenharmony_ci}
1639bf215546Sopenharmony_ci
1640bf215546Sopenharmony_ci/**
1641bf215546Sopenharmony_ci * Optimizations which can create registers are done after other optimizations
1642bf215546Sopenharmony_ci * which require SSA.
1643bf215546Sopenharmony_ci */
1644bf215546Sopenharmony_cistatic bool
1645bf215546Sopenharmony_ciopt_if_regs_cf_list(struct exec_list *cf_list)
1646bf215546Sopenharmony_ci{
1647bf215546Sopenharmony_ci   bool progress = false;
1648bf215546Sopenharmony_ci   foreach_list_typed(nir_cf_node, cf_node, node, cf_list) {
1649bf215546Sopenharmony_ci      switch (cf_node->type) {
1650bf215546Sopenharmony_ci      case nir_cf_node_block:
1651bf215546Sopenharmony_ci         break;
1652bf215546Sopenharmony_ci
1653bf215546Sopenharmony_ci      case nir_cf_node_if: {
1654bf215546Sopenharmony_ci         nir_if *nif = nir_cf_node_as_if(cf_node);
1655bf215546Sopenharmony_ci         progress |= opt_if_regs_cf_list(&nif->then_list);
1656bf215546Sopenharmony_ci         progress |= opt_if_regs_cf_list(&nif->else_list);
1657bf215546Sopenharmony_ci         if (opt_merge_breaks(nif)) {
1658bf215546Sopenharmony_ci            /* This optimization might move blocks
1659bf215546Sopenharmony_ci             * from after the NIF into the NIF */
1660bf215546Sopenharmony_ci            progress = true;
1661bf215546Sopenharmony_ci            opt_if_regs_cf_list(&nif->then_list);
1662bf215546Sopenharmony_ci            opt_if_regs_cf_list(&nif->else_list);
1663bf215546Sopenharmony_ci         }
1664bf215546Sopenharmony_ci         break;
1665bf215546Sopenharmony_ci      }
1666bf215546Sopenharmony_ci
1667bf215546Sopenharmony_ci      case nir_cf_node_loop: {
1668bf215546Sopenharmony_ci         nir_loop *loop = nir_cf_node_as_loop(cf_node);
1669bf215546Sopenharmony_ci         progress |= opt_if_regs_cf_list(&loop->body);
1670bf215546Sopenharmony_ci         progress |= opt_peel_loop_initial_if(loop);
1671bf215546Sopenharmony_ci         break;
1672bf215546Sopenharmony_ci      }
1673bf215546Sopenharmony_ci
1674bf215546Sopenharmony_ci      case nir_cf_node_function:
1675bf215546Sopenharmony_ci         unreachable("Invalid cf type");
1676bf215546Sopenharmony_ci      }
1677bf215546Sopenharmony_ci   }
1678bf215546Sopenharmony_ci
1679bf215546Sopenharmony_ci   return progress;
1680bf215546Sopenharmony_ci}
1681bf215546Sopenharmony_ci
1682bf215546Sopenharmony_ci/**
1683bf215546Sopenharmony_ci * These optimisations depend on nir_metadata_block_index and therefore must
1684bf215546Sopenharmony_ci * not do anything to cause the metadata to become invalid.
1685bf215546Sopenharmony_ci */
1686bf215546Sopenharmony_cistatic bool
1687bf215546Sopenharmony_ciopt_if_safe_cf_list(nir_builder *b, struct exec_list *cf_list)
1688bf215546Sopenharmony_ci{
1689bf215546Sopenharmony_ci   bool progress = false;
1690bf215546Sopenharmony_ci   foreach_list_typed(nir_cf_node, cf_node, node, cf_list) {
1691bf215546Sopenharmony_ci      switch (cf_node->type) {
1692bf215546Sopenharmony_ci      case nir_cf_node_block:
1693bf215546Sopenharmony_ci         break;
1694bf215546Sopenharmony_ci
1695bf215546Sopenharmony_ci      case nir_cf_node_if: {
1696bf215546Sopenharmony_ci         nir_if *nif = nir_cf_node_as_if(cf_node);
1697bf215546Sopenharmony_ci         progress |= opt_if_safe_cf_list(b, &nif->then_list);
1698bf215546Sopenharmony_ci         progress |= opt_if_safe_cf_list(b, &nif->else_list);
1699bf215546Sopenharmony_ci         progress |= opt_if_evaluate_condition_use(b, nif);
1700bf215546Sopenharmony_ci         nir_ssa_scalar cond = nir_ssa_scalar_resolved(nif->condition.ssa, 0);
1701bf215546Sopenharmony_ci         progress |= opt_if_rewrite_uniform_uses(b, nif, cond, true);
1702bf215546Sopenharmony_ci         break;
1703bf215546Sopenharmony_ci      }
1704bf215546Sopenharmony_ci
1705bf215546Sopenharmony_ci      case nir_cf_node_loop: {
1706bf215546Sopenharmony_ci         nir_loop *loop = nir_cf_node_as_loop(cf_node);
1707bf215546Sopenharmony_ci         progress |= opt_if_safe_cf_list(b, &loop->body);
1708bf215546Sopenharmony_ci         progress |= opt_split_alu_of_phi(b, loop);
1709bf215546Sopenharmony_ci         break;
1710bf215546Sopenharmony_ci      }
1711bf215546Sopenharmony_ci
1712bf215546Sopenharmony_ci      case nir_cf_node_function:
1713bf215546Sopenharmony_ci         unreachable("Invalid cf type");
1714bf215546Sopenharmony_ci      }
1715bf215546Sopenharmony_ci   }
1716bf215546Sopenharmony_ci
1717bf215546Sopenharmony_ci   return progress;
1718bf215546Sopenharmony_ci}
1719bf215546Sopenharmony_ci
1720bf215546Sopenharmony_cibool
1721bf215546Sopenharmony_cinir_opt_if(nir_shader *shader, nir_opt_if_options options)
1722bf215546Sopenharmony_ci{
1723bf215546Sopenharmony_ci   bool progress = false;
1724bf215546Sopenharmony_ci
1725bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
1726bf215546Sopenharmony_ci      if (function->impl == NULL)
1727bf215546Sopenharmony_ci         continue;
1728bf215546Sopenharmony_ci
1729bf215546Sopenharmony_ci      nir_builder b;
1730bf215546Sopenharmony_ci      nir_builder_init(&b, function->impl);
1731bf215546Sopenharmony_ci
1732bf215546Sopenharmony_ci      nir_metadata_require(function->impl, nir_metadata_block_index |
1733bf215546Sopenharmony_ci                           nir_metadata_dominance);
1734bf215546Sopenharmony_ci      progress = opt_if_safe_cf_list(&b, &function->impl->body);
1735bf215546Sopenharmony_ci      nir_metadata_preserve(function->impl, nir_metadata_block_index |
1736bf215546Sopenharmony_ci                            nir_metadata_dominance);
1737bf215546Sopenharmony_ci
1738bf215546Sopenharmony_ci      bool preserve = true;
1739bf215546Sopenharmony_ci
1740bf215546Sopenharmony_ci      if (opt_if_cf_list(&b, &function->impl->body, options)) {
1741bf215546Sopenharmony_ci         preserve = false;
1742bf215546Sopenharmony_ci         progress = true;
1743bf215546Sopenharmony_ci      }
1744bf215546Sopenharmony_ci
1745bf215546Sopenharmony_ci      if (opt_if_regs_cf_list(&function->impl->body)) {
1746bf215546Sopenharmony_ci         preserve = false;
1747bf215546Sopenharmony_ci         progress = true;
1748bf215546Sopenharmony_ci
1749bf215546Sopenharmony_ci         /* If that made progress, we're no longer really in SSA form.  We
1750bf215546Sopenharmony_ci          * need to convert registers back into SSA defs and clean up SSA defs
1751bf215546Sopenharmony_ci          * that don't dominate their uses.
1752bf215546Sopenharmony_ci          */
1753bf215546Sopenharmony_ci         nir_lower_regs_to_ssa_impl(function->impl);
1754bf215546Sopenharmony_ci      }
1755bf215546Sopenharmony_ci
1756bf215546Sopenharmony_ci      if (preserve) {
1757bf215546Sopenharmony_ci         nir_metadata_preserve(function->impl, nir_metadata_none);
1758bf215546Sopenharmony_ci      } else {
1759bf215546Sopenharmony_ci         nir_metadata_preserve(function->impl, nir_metadata_all);
1760bf215546Sopenharmony_ci      }
1761bf215546Sopenharmony_ci   }
1762bf215546Sopenharmony_ci
1763bf215546Sopenharmony_ci   return progress;
1764bf215546Sopenharmony_ci}
1765