xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_instr_set.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2014 Connor Abbott
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 #ifndef NIR_INSTR_SET_H
25 #define NIR_INSTR_SET_H
26 
27 #include "nir.h"
28 
29 /**
30  * This file defines functions for creating, destroying, and manipulating an
31  * "instruction set," which is an abstraction for finding duplicate
32  * instructions using a hash set. Note that the question of whether an
33  * instruction is actually a duplicate (e.g. whether it has any side effects)
34  * is handled transparently. The user can pass any instruction to
35  * nir_instr_set_add_or_rewrite() and nir_instr_set_remove(), and if the
36  * instruction isn't safe to rewrite or isn't supported, it's silently
37  * removed.
38  */
39 
40 /*@{*/
41 
42 /** Creates an instruction set, using a given ralloc mem_ctx */
43 struct set *nir_instr_set_create(void *mem_ctx);
44 
45 /** Destroys an instruction set. */
46 void nir_instr_set_destroy(struct set *instr_set);
47 
48 /**
49  * Adds an instruction to an instruction set if it doesn't exist. If it does
50  * already exist, returns the other already-inserted instruction and rewrites
51  * all uses of it to point to that instruction. Otherwise, it returns NULL.
52  *
53  * If cond_function() is given, only rewrites uses if
54  * cond_function(old_instr, new_instr) returns true.
55  */
56 nir_instr *
57 nir_instr_set_add_or_rewrite(struct set *instr_set, nir_instr *instr,
58                              bool (*cond_function)(const nir_instr *a,
59                                                    const nir_instr *b));
60 
61 /**
62  * Removes an instruction from an instruction set, so that other instructions
63  * won't be merged with it.
64  */
65 void nir_instr_set_remove(struct set *instr_set, nir_instr *instr);
66 
67 /*@}*/
68 
69 #endif /* NIR_INSTR_SET_H */
70