1b8e80941Smrg/* 2b8e80941Smrg * Copyright © 2016 Intel Corporation 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the 9b8e80941Smrg * Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the next 12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the 13b8e80941Smrg * Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21b8e80941Smrg * IN THE SOFTWARE. 22b8e80941Smrg */ 23b8e80941Smrg 24b8e80941Smrg#ifndef NIR_PHI_BUILDER_H 25b8e80941Smrg#define NIR_PHI_BUILDER_H 26b8e80941Smrg 27b8e80941Smrg#include "nir.h" 28b8e80941Smrg 29b8e80941Smrg/** A helper for placing phi nodes in a NIR shader 30b8e80941Smrg * 31b8e80941Smrg * Basic usage goes something like this: 32b8e80941Smrg * 33b8e80941Smrg * each variable, var, has: 34b8e80941Smrg * a bitset var.defs of blocks where the variable is defined 35b8e80941Smrg * a struct nir_phi_builder_value *pb_val 36b8e80941Smrg * 37b8e80941Smrg * // initialize bitsets 38b8e80941Smrg * foreach block: 39b8e80941Smrg * foreach def of variable var: 40b8e80941Smrg * var.defs[def.block] = true; 41b8e80941Smrg * 42b8e80941Smrg * // initialize phi builder 43b8e80941Smrg * pb = nir_phi_builder_create() 44b8e80941Smrg * foreach var: 45b8e80941Smrg * var.pb_val = nir_phi_builder_add_value(pb, var.defs) 46b8e80941Smrg * 47b8e80941Smrg * // Visit each block. This needs to visit dominators first; 48b8e80941Smrg * // nir_foreach_block() will be ok. 49b8e80941Smrg * 50b8e80941Smrg * foreach block: 51b8e80941Smrg * foreach instruction: 52b8e80941Smrg * foreach use of variable var: 53b8e80941Smrg * replace use with nir_phi_builder_get_block_def(var.pb_val) 54b8e80941Smrg * foreach def of variable var: 55b8e80941Smrg * create ssa def, register with 56b8e80941Smrg * nir_phi_builder_set_block_def(var.pb_val) 57b8e80941Smrg * 58b8e80941Smrg * nir_phi_builder_finish(pb) 59b8e80941Smrg */ 60b8e80941Smrgstruct nir_phi_builder; 61b8e80941Smrg 62b8e80941Smrgstruct nir_phi_builder_value; 63b8e80941Smrg 64b8e80941Smrg/* Create a new phi builder. 65b8e80941Smrg * 66b8e80941Smrg * While this is fairly cheap, it does allocate some memory and walk the list 67b8e80941Smrg * of blocks so it's recommended that you only call it once and use it to 68b8e80941Smrg * build phis for several values. 69b8e80941Smrg */ 70b8e80941Smrgstruct nir_phi_builder *nir_phi_builder_create(nir_function_impl *impl); 71b8e80941Smrg 72b8e80941Smrg/* Register a value with the builder. 73b8e80941Smrg * 74b8e80941Smrg * The 'defs' parameter specifies a bitset of blocks in which the given value 75b8e80941Smrg * is defined. This is used to determine where to place the phi nodes. 76b8e80941Smrg */ 77b8e80941Smrgstruct nir_phi_builder_value * 78b8e80941Smrgnir_phi_builder_add_value(struct nir_phi_builder *pb, unsigned num_components, 79b8e80941Smrg unsigned bit_size, const BITSET_WORD *defs); 80b8e80941Smrg 81b8e80941Smrg/* Register a definition for the given value and block. 82b8e80941Smrg * 83b8e80941Smrg * It is safe to call this function as many times as you wish for any given 84b8e80941Smrg * block/value pair. However, it always replaces whatever was there 85b8e80941Smrg * previously even if that definition is from a phi node. The phi builder 86b8e80941Smrg * always uses the latest information it has, so you must be careful about the 87b8e80941Smrg * order in which you register definitions. The final value at the end of the 88b8e80941Smrg * block must be the last value registered. 89b8e80941Smrg */ 90b8e80941Smrgvoid 91b8e80941Smrgnir_phi_builder_value_set_block_def(struct nir_phi_builder_value *val, 92b8e80941Smrg nir_block *block, nir_ssa_def *def); 93b8e80941Smrg 94b8e80941Smrg/* Get the definition for the given value in the given block. 95b8e80941Smrg * 96b8e80941Smrg * This definition will always be the latest definition known for the given 97b8e80941Smrg * block. If no definition is immediately available, it will crawl up the 98b8e80941Smrg * dominance tree and insert phi nodes as needed until it finds one. In the 99b8e80941Smrg * case that no suitable definition is found, it will return the result of a 100b8e80941Smrg * nir_ssa_undef_instr with the correct number of components. 101b8e80941Smrg * 102b8e80941Smrg * Because this function only uses the latest available information for any 103b8e80941Smrg * given block, you must have already finished registering definitions for any 104b8e80941Smrg * blocks that dominate the current block in order to get the correct result. 105b8e80941Smrg */ 106b8e80941Smrgnir_ssa_def * 107b8e80941Smrgnir_phi_builder_value_get_block_def(struct nir_phi_builder_value *val, 108b8e80941Smrg nir_block *block); 109b8e80941Smrg 110b8e80941Smrg/* Finish building phi nodes and free the builder. 111b8e80941Smrg * 112b8e80941Smrg * This function does far more than just free memory. Prior to calling 113b8e80941Smrg * nir_phi_builder_finish, no phi nodes have actually been inserted in the 114b8e80941Smrg * program. This function is what finishes setting up phi node sources and 115b8e80941Smrg * adds the phi nodes to the program. 116b8e80941Smrg */ 117b8e80941Smrgvoid nir_phi_builder_finish(struct nir_phi_builder *pb); 118b8e80941Smrg 119b8e80941Smrg#endif /* NIR_PHI_BUILDER_H */ 120