1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2021 Valve 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 FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "ir3.h"
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci/* Sometimes we can get unreachable blocks from NIR. In particular this happens
27bf215546Sopenharmony_ci * for blocks after an if where both sides end in a break/continue. These blocks
28bf215546Sopenharmony_ci * are then reachable only via the physical CFG. This pass deletes these blocks
29bf215546Sopenharmony_ci * and reroutes the physical edge past it.
30bf215546Sopenharmony_ci */
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_cistatic void
33bf215546Sopenharmony_cidelete_block(struct ir3 *ir, struct ir3_block *block)
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci   struct ir3_instruction *end = NULL;
36bf215546Sopenharmony_ci   foreach_instr (instr, &block->instr_list) {
37bf215546Sopenharmony_ci      if (instr->opc == OPC_END) {
38bf215546Sopenharmony_ci         end = instr;
39bf215546Sopenharmony_ci         break;
40bf215546Sopenharmony_ci      }
41bf215546Sopenharmony_ci   }
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci   /* The end block can be legitimately unreachable if the shader only exits via
44bf215546Sopenharmony_ci    * discarding. ir3_legalize will then insert a branch to the end. Keep the
45bf215546Sopenharmony_ci    * block around but delete all the other instructions and make the end not
46bf215546Sopenharmony_ci    * take any sources, so that we don't have any dangling references to other
47bf215546Sopenharmony_ci    * unreachable blocks.
48bf215546Sopenharmony_ci    */
49bf215546Sopenharmony_ci   if (end) {
50bf215546Sopenharmony_ci      foreach_instr_safe (instr, &block->instr_list) {
51bf215546Sopenharmony_ci         if (instr != end)
52bf215546Sopenharmony_ci            list_delinit(&instr->node);
53bf215546Sopenharmony_ci      }
54bf215546Sopenharmony_ci      end->srcs_count = 0;
55bf215546Sopenharmony_ci      return;
56bf215546Sopenharmony_ci   }
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci   for (unsigned i = 0; i < 2; i++) {
59bf215546Sopenharmony_ci      struct ir3_block *succ = block->successors[i];
60bf215546Sopenharmony_ci      if (!succ)
61bf215546Sopenharmony_ci         continue;
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci      unsigned pred_idx = ir3_block_get_pred_index(succ, block);
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci      /* If this isn't the last predecessor, we swap it with the last before
66bf215546Sopenharmony_ci       * removing it.
67bf215546Sopenharmony_ci       */
68bf215546Sopenharmony_ci      bool swap_pred = pred_idx != succ->predecessors_count - 1;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci      foreach_instr (phi, &succ->instr_list) {
71bf215546Sopenharmony_ci         if (phi->opc != OPC_META_PHI)
72bf215546Sopenharmony_ci            break;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci         if (swap_pred)
75bf215546Sopenharmony_ci            phi->srcs[pred_idx] = phi->srcs[phi->srcs_count - 1];
76bf215546Sopenharmony_ci         phi->srcs_count--;
77bf215546Sopenharmony_ci      }
78bf215546Sopenharmony_ci      if (swap_pred) {
79bf215546Sopenharmony_ci         succ->predecessors[pred_idx] =
80bf215546Sopenharmony_ci            succ->predecessors[succ->predecessors_count - 1];
81bf215546Sopenharmony_ci      }
82bf215546Sopenharmony_ci      succ->predecessors_count--;
83bf215546Sopenharmony_ci   }
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   for (unsigned i = 0; i < 2; i++) {
86bf215546Sopenharmony_ci      struct ir3_block *succ = block->physical_successors[i];
87bf215546Sopenharmony_ci      if (!succ)
88bf215546Sopenharmony_ci         continue;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci      ir3_block_remove_physical_predecessor(succ, block);
91bf215546Sopenharmony_ci   }
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   if (block->physical_predecessors_count != 0) {
94bf215546Sopenharmony_ci      /* There should be only one physical predecessor, for the fallthrough
95bf215546Sopenharmony_ci       * edge.
96bf215546Sopenharmony_ci       */
97bf215546Sopenharmony_ci      assert(block->physical_predecessors_count == 1);
98bf215546Sopenharmony_ci      struct ir3_block *pred = block->physical_predecessors[0];
99bf215546Sopenharmony_ci      assert(block->node.next != &ir->block_list);
100bf215546Sopenharmony_ci      struct ir3_block *next = list_entry(block->node.next, struct ir3_block, node);
101bf215546Sopenharmony_ci      if (pred->physical_successors[1] == block)
102bf215546Sopenharmony_ci         pred->physical_successors[1] = next;
103bf215546Sopenharmony_ci      else
104bf215546Sopenharmony_ci         pred->physical_successors[0] = next;
105bf215546Sopenharmony_ci      ir3_block_add_physical_predecessor(next, pred);
106bf215546Sopenharmony_ci   }
107bf215546Sopenharmony_ci}
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_cibool
110bf215546Sopenharmony_ciir3_remove_unreachable(struct ir3 *ir)
111bf215546Sopenharmony_ci{
112bf215546Sopenharmony_ci   bool progress = false;
113bf215546Sopenharmony_ci   foreach_block_safe (block, &ir->block_list) {
114bf215546Sopenharmony_ci      if (block != ir3_start_block(ir) && block->predecessors_count == 0) {
115bf215546Sopenharmony_ci         delete_block(ir, block);
116bf215546Sopenharmony_ci         list_del(&block->node);
117bf215546Sopenharmony_ci         progress = true;
118bf215546Sopenharmony_ci      }
119bf215546Sopenharmony_ci   }
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   return progress;
122bf215546Sopenharmony_ci}
123