Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / compiler / nir / nir_phi_builder.h
1 /*
2 * Copyright © 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #pragma once
25
26 #include "nir.h"
27
28 struct nir_phi_builder;
29 struct nir_phi_builder_value;
30
31 /* Create a new phi builder.
32 *
33 * While this is fairly cheap, it does allocate some memory and walk the list
34 * of blocks so it's recommended that you only call it once and use it to
35 * build phis for several values.
36 */
37 struct nir_phi_builder *nir_phi_builder_create(nir_function_impl *impl);
38
39 /* Register a value with the builder.
40 *
41 * The 'defs' parameter specifies a bitset of blocks in which the given value
42 * is defined. This is used to determine where to place the phi nodes.
43 */
44 struct nir_phi_builder_value *
45 nir_phi_builder_add_value(struct nir_phi_builder *pb, unsigned num_components,
46 const BITSET_WORD *defs);
47
48 /* Register a definition for the given value and block.
49 *
50 * It is safe to call this function as many times as you wish for any given
51 * block/value pair. However, it always replaces whatever was there
52 * previously even if that definition is from a phi node. The phi builder
53 * always uses the latest information it has, so you must be careful about the
54 * order in which you register definitions. The final value at the end of the
55 * block must be the last value registered.
56 */
57 void
58 nir_phi_builder_value_set_block_def(struct nir_phi_builder_value *val,
59 nir_block *block, nir_ssa_def *def);
60
61 /* Get the definition for the given value in the given block.
62 *
63 * This definition will always be the latest definition known for the given
64 * block. If no definition is immediately available, it will crawl up the
65 * dominance tree and insert phi nodes as needed until it finds one. In the
66 * case that no suitable definition is found, it will return the result of a
67 * nir_ssa_undef_instr with the correct number of components.
68 *
69 * Because this function only uses the latest available information for any
70 * given block, you must have already finished registering definitions for any
71 * blocks that dominate the current block in order to get the correct result.
72 */
73 nir_ssa_def *
74 nir_phi_builder_value_get_block_def(struct nir_phi_builder_value *val,
75 nir_block *block);
76
77 /* Finish building phi nodes and free the builder.
78 *
79 * This function does far more than just free memory. Prior to calling
80 * nir_phi_builder_finish, no phi nodes have actually been inserted in the
81 * program. This function is what finishes setting up phi node sources and
82 * adds the phi nodes to the program.
83 */
84 void nir_phi_builder_finish(struct nir_phi_builder *pb);