summaryrefslogtreecommitdiffstats
path: root/src/compiler/nir/nir_phi_builder.h
blob: a4dc18a2b22df27826db9db04ba73796e64a6526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright © 2016 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#pragma once

#include "nir.h"

/** A helper for placing phi nodes in a NIR shader
 *
 * Basic usage goes something like this:
 *
 *     each variable, var, has:
 *         a bitset var.defs of blocks where the variable is defined
 *         a struct nir_phi_builder_value *pb_val
 *
 *     // initialize bitsets
 *     foreach block:
 *         foreach def of variable var:
 *             var.defs[def.block] = true;
 *
 *     // initialize phi builder
 *     pb = nir_phi_builder_create()
 *     foreach var:
 *         var.pb_val = nir_phi_builder_add_value(pb, var.defs)
 *
 *     // Visit each block.  This needs to visit dominators first;
 *     // nir_foreach_block() will be ok.
 *
 *     foreach block:
 *         foreach instruction:
 *             foreach use of variable var:
 *                 replace use with nir_phi_builder_get_block_def(var.pb_val)
 *             foreach def of variable var:
 *                 create ssa def, register with
 *     nir_phi_builder_set_block_def(var.pb_val)
 *
 *     nir_phi_builder_finish(pb)
 */
struct nir_phi_builder;

struct nir_phi_builder_value;

/* Create a new phi builder.
 *
 * While this is fairly cheap, it does allocate some memory and walk the list
 * of blocks so it's recommended that you only call it once and use it to
 * build phis for several values.
 */
struct nir_phi_builder *nir_phi_builder_create(nir_function_impl *impl);

/* Register a value with the builder.
 *
 * The 'defs' parameter specifies a bitset of blocks in which the given value
 * is defined.  This is used to determine where to place the phi nodes.
 */
struct nir_phi_builder_value *
nir_phi_builder_add_value(struct nir_phi_builder *pb, unsigned num_components,
                          unsigned bit_size, const BITSET_WORD *defs);

/* Register a definition for the given value and block.
 *
 * It is safe to call this function as many times as you wish for any given
 * block/value pair.  However, it always replaces whatever was there
 * previously even if that definition is from a phi node.  The phi builder
 * always uses the latest information it has, so you must be careful about the
 * order in which you register definitions.  The final value at the end of the
 * block must be the last value registered.
 */
void
nir_phi_builder_value_set_block_def(struct nir_phi_builder_value *val,
                                    nir_block *block, nir_ssa_def *def);

/* Get the definition for the given value in the given block.
 *
 * This definition will always be the latest definition known for the given
 * block.  If no definition is immediately available, it will crawl up the
 * dominance tree and insert phi nodes as needed until it finds one.  In the
 * case that no suitable definition is found, it will return the result of a
 * nir_ssa_undef_instr with the correct number of components.
 *
 * Because this function only uses the latest available information for any
 * given block, you must have already finished registering definitions for any
 * blocks that dominate the current block in order to get the correct result.
 */
nir_ssa_def *
nir_phi_builder_value_get_block_def(struct nir_phi_builder_value *val,
                                    nir_block *block);

/* Finish building phi nodes and free the builder.
 *
 * This function does far more than just free memory.  Prior to calling
 * nir_phi_builder_finish, no phi nodes have actually been inserted in the
 * program.  This function is what finishes setting up phi node sources and
 * adds the phi nodes to the program.
 */
void nir_phi_builder_finish(struct nir_phi_builder *pb);