1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014 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 * Authors:
24bf215546Sopenharmony_ci *    Connor Abbott (cwabbott0@gmail.com)
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci */
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#ifndef NIR_CONTROL_FLOW_H
29bf215546Sopenharmony_ci#define NIR_CONTROL_FLOW_H
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "nir.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#ifdef __cplusplus
34bf215546Sopenharmony_ciextern "C" {
35bf215546Sopenharmony_ci#endif
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci/** NIR Control Flow Modification
38bf215546Sopenharmony_ci *
39bf215546Sopenharmony_ci * This file contains various APIs that make modifying control flow in NIR,
40bf215546Sopenharmony_ci * while maintaining the invariants checked by the validator, much easier.
41bf215546Sopenharmony_ci * There are two parts to this:
42bf215546Sopenharmony_ci *
43bf215546Sopenharmony_ci * 1. Inserting control flow (ifs and loops) in various places, for creating
44bf215546Sopenharmony_ci *    IR either from scratch or as part of some lowering pass.
45bf215546Sopenharmony_ci * 2. Taking existing pieces of the IR and either moving them around or
46bf215546Sopenharmony_ci *    deleting them.
47bf215546Sopenharmony_ci */
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci/** Control flow insertion. */
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci/** puts a control flow node where the cursor is */
52bf215546Sopenharmony_civoid nir_cf_node_insert(nir_cursor cursor, nir_cf_node *node);
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci/** puts a control flow node immediately after another control flow node */
55bf215546Sopenharmony_cistatic inline void
56bf215546Sopenharmony_cinir_cf_node_insert_after(nir_cf_node *node, nir_cf_node *after)
57bf215546Sopenharmony_ci{
58bf215546Sopenharmony_ci   nir_cf_node_insert(nir_after_cf_node(node), after);
59bf215546Sopenharmony_ci}
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci/** puts a control flow node immediately before another control flow node */
62bf215546Sopenharmony_cistatic inline void
63bf215546Sopenharmony_cinir_cf_node_insert_before(nir_cf_node *node, nir_cf_node *before)
64bf215546Sopenharmony_ci{
65bf215546Sopenharmony_ci   nir_cf_node_insert(nir_before_cf_node(node), before);
66bf215546Sopenharmony_ci}
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci/** puts a control flow node at the beginning of a list from an if, loop, or function */
69bf215546Sopenharmony_cistatic inline void
70bf215546Sopenharmony_cinir_cf_node_insert_begin(struct exec_list *list, nir_cf_node *node)
71bf215546Sopenharmony_ci{
72bf215546Sopenharmony_ci   nir_cf_node_insert(nir_before_cf_list(list), node);
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci/** puts a control flow node at the end of a list from an if, loop, or function */
76bf215546Sopenharmony_cistatic inline void
77bf215546Sopenharmony_cinir_cf_node_insert_end(struct exec_list *list, nir_cf_node *node)
78bf215546Sopenharmony_ci{
79bf215546Sopenharmony_ci   nir_cf_node_insert(nir_after_cf_list(list), node);
80bf215546Sopenharmony_ci}
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci/** Control flow motion.
84bf215546Sopenharmony_ci *
85bf215546Sopenharmony_ci * These functions let you take a part of a control flow list (basically
86bf215546Sopenharmony_ci * equivalent to a series of statement in GLSL) and "extract" it from the IR,
87bf215546Sopenharmony_ci * so that it's a free-floating piece of IR that can be either re-inserted
88bf215546Sopenharmony_ci * somewhere else or deleted entirely. A few notes on using it:
89bf215546Sopenharmony_ci *
90bf215546Sopenharmony_ci * 1. Phi nodes are considered attached to the piece of control flow that
91bf215546Sopenharmony_ci *    their sources come from. There are three places where phi nodes can
92bf215546Sopenharmony_ci *    occur, which are the three places where a block can have multiple
93bf215546Sopenharmony_ci *    predecessors:
94bf215546Sopenharmony_ci *
95bf215546Sopenharmony_ci *    1) After an if statement, if neither branch ends in a jump.
96bf215546Sopenharmony_ci *    2) After a loop, if there are multiple breaks.
97bf215546Sopenharmony_ci *    3) At the beginning of a loop.
98bf215546Sopenharmony_ci *
99bf215546Sopenharmony_ci *    For #1, the phi node is considered to be part of the if, and for #2 and
100bf215546Sopenharmony_ci *    #3 the phi node is considered to be part of the loop. This allows us to
101bf215546Sopenharmony_ci *    keep phis intact, but it means that phi nodes cannot be separated from
102bf215546Sopenharmony_ci *    the control flow they come from. For example, extracting an if without
103bf215546Sopenharmony_ci *    extracting all the phi nodes after it is not allowed, and neither is
104bf215546Sopenharmony_ci *    extracting only some of the phi nodes at the beginning of a block. It
105bf215546Sopenharmony_ci *    also means that extracting from the beginning of a basic block actually
106bf215546Sopenharmony_ci *    means extracting from the first non-phi instruction, since there's no
107bf215546Sopenharmony_ci *    situation where extracting phi nodes without extracting what comes
108bf215546Sopenharmony_ci *    before them makes any sense.
109bf215546Sopenharmony_ci *
110bf215546Sopenharmony_ci * 2. Phi node sources are guaranteed to remain valid, meaning that they still
111bf215546Sopenharmony_ci *    correspond one-to-one with the predecessors of the basic block they're
112bf215546Sopenharmony_ci *    part of. In addition, the original sources will be preserved unless they
113bf215546Sopenharmony_ci *    correspond to a break or continue that was deleted. However, no attempt
114bf215546Sopenharmony_ci *    is made to ensure that SSA form is maintained. In particular, it is
115bf215546Sopenharmony_ci *    *not* guaranteed that definitions of SSA values will dominate all their
116bf215546Sopenharmony_ci *    uses after all is said and done. Either the caller must ensure that this
117bf215546Sopenharmony_ci *    is the case, or it must insert extra phi nodes to restore SSA.
118bf215546Sopenharmony_ci *
119bf215546Sopenharmony_ci * 3. It is invalid to move a piece of IR with a break/continue outside of the
120bf215546Sopenharmony_ci *    loop it references. Doing this will result in invalid
121bf215546Sopenharmony_ci *    successors/predecessors and phi node sources.
122bf215546Sopenharmony_ci *
123bf215546Sopenharmony_ci * 4. It is invalid to move a piece of IR from one function implementation to
124bf215546Sopenharmony_ci *    another.
125bf215546Sopenharmony_ci *
126bf215546Sopenharmony_ci * 5. Extracting a control flow list will leave lots of dangling references to
127bf215546Sopenharmony_ci *    and from other pieces of the IR. It also leaves things in a not 100%
128bf215546Sopenharmony_ci *    consistent state. This means that some things (e.g. inserting
129bf215546Sopenharmony_ci *    instructions) might not work reliably on the extracted control flow. It
130bf215546Sopenharmony_ci *    also means that extracting control flow without re-inserting it or
131bf215546Sopenharmony_ci *    deleting it is a Bad Thing (tm).
132bf215546Sopenharmony_ci */
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_citypedef struct {
135bf215546Sopenharmony_ci   struct exec_list list;
136bf215546Sopenharmony_ci   nir_function_impl *impl; /* for cleaning up if the list is deleted */
137bf215546Sopenharmony_ci} nir_cf_list;
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_cinir_cursor nir_cf_extract(nir_cf_list *extracted, nir_cursor begin,
140bf215546Sopenharmony_ci                          nir_cursor end);
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_cinir_cursor nir_cf_reinsert(nir_cf_list *cf_list, nir_cursor cursor);
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_civoid nir_cf_delete(nir_cf_list *cf_list);
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_civoid nir_cf_list_clone(nir_cf_list *dst, nir_cf_list *src, nir_cf_node *parent,
147bf215546Sopenharmony_ci                       struct hash_table *remap_table);
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_cistatic inline void
150bf215546Sopenharmony_cinir_cf_list_clone_and_reinsert(nir_cf_list *src_list, nir_cf_node *parent,
151bf215546Sopenharmony_ci                               nir_cursor cursor,
152bf215546Sopenharmony_ci                               struct hash_table *remap_table)
153bf215546Sopenharmony_ci{
154bf215546Sopenharmony_ci   nir_cf_list list;
155bf215546Sopenharmony_ci   nir_cf_list_clone(&list, src_list, parent, remap_table);
156bf215546Sopenharmony_ci   nir_cf_reinsert(&list, cursor);
157bf215546Sopenharmony_ci}
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_cistatic inline void
160bf215546Sopenharmony_cinir_cf_list_extract(nir_cf_list *extracted, struct exec_list *cf_list)
161bf215546Sopenharmony_ci{
162bf215546Sopenharmony_ci   nir_cf_extract(extracted, nir_before_cf_list(cf_list),
163bf215546Sopenharmony_ci                  nir_after_cf_list(cf_list));
164bf215546Sopenharmony_ci}
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci/** removes a control flow node, doing any cleanup necessary */
167bf215546Sopenharmony_cistatic inline void
168bf215546Sopenharmony_cinir_cf_node_remove(nir_cf_node *node)
169bf215546Sopenharmony_ci{
170bf215546Sopenharmony_ci   nir_cf_list list;
171bf215546Sopenharmony_ci   nir_cf_extract(&list, nir_before_cf_node(node), nir_after_cf_node(node));
172bf215546Sopenharmony_ci   nir_cf_delete(&list);
173bf215546Sopenharmony_ci}
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci/** inserts undef phi sources from predcessor into phis of the block */
176bf215546Sopenharmony_civoid nir_insert_phi_undef(nir_block *block, nir_block *pred);
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci#ifdef __cplusplus
179bf215546Sopenharmony_ci}
180bf215546Sopenharmony_ci#endif
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci#endif /* NIR_CONTROL_FLOW_H */
183