1 /*
2  * Copyright © 2016 Intel Corporation
3  * Copyright © 2019 Valve Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #include "nir.h"
26 
27 /**
28  * \file nir_opt_move.c
29  *
30  * This pass can move various operations just before their first use inside the
31  * same basic block. Usually this is to reduce register usage. It's probably
32  * not a good idea to use this in an optimization loop.
33  *
34  * Moving comparisons is useful because many GPUs generate condition codes
35  * for comparisons, and use predication for conditional selects and control
36  * flow.  In a sequence such as:
37  *
38  *     vec1 32 ssa_1 = flt a b
39  *     <some other operations>
40  *     vec1 32 ssa_2 = bcsel ssa_1 c d
41  *
42  * the backend would likely do the comparison, producing condition codes,
43  * then save those to a boolean value.  The intervening operations might
44  * trash the condition codes.  Then, in order to do the bcsel, it would
45  * need to re-populate the condition code register based on the boolean.
46  *
47  * By moving the comparison just before the bcsel, the condition codes could
48  * be used directly.  This eliminates the need to reload them from the boolean
49  * (generally eliminating an instruction).  It may also eliminate the need to
50  * create a boolean value altogether (unless it's used elsewhere), which could
51  * lower register pressure.
52  */
53 
54 static bool
move_source(nir_src * src,nir_block * block,nir_instr * before,nir_move_options options)55 move_source(nir_src *src, nir_block *block, nir_instr *before, nir_move_options options)
56 {
57    if (!src->is_ssa)
58       return false;
59 
60    nir_instr *src_instr = src->ssa->parent_instr;
61 
62    if (src_instr->block == block && nir_can_move_instr(src_instr, options)) {
63       exec_node_remove(&src_instr->node);
64 
65       if (before)
66          exec_node_insert_node_before(&before->node, &src_instr->node);
67       else
68          exec_list_push_tail(&block->instr_list, &src_instr->node);
69 
70       return true;
71    }
72    return false;
73 }
74 
75 struct source_cb_data {
76    bool *progress;
77    nir_move_options options;
78 };
79 
80 static bool
move_source_cb(nir_src * src,void * data_ptr)81 move_source_cb(nir_src *src, void *data_ptr)
82 {
83    struct source_cb_data data = *(struct source_cb_data*)data_ptr;
84 
85    nir_instr *instr = src->parent_instr;
86    if (move_source(src, instr->block, instr, data.options))
87       *data.progress = true;
88 
89    return true; /* nir_foreach_src should keep going */
90 }
91 
92 static bool
move(nir_block * block,nir_move_options options)93 move(nir_block *block, nir_move_options options)
94 {
95    bool progress = false;
96 
97    /* We use a simple approach: walk instructions backwards.
98     *
99     * If the instruction's source is a comparison from the same block,
100     * simply move it here.  This may break SSA if it's used earlier in
101     * the block as well.  However, as we walk backwards, we'll find the
102     * earlier use and move it again, further up.  It eventually ends up
103     * dominating all uses again, restoring SSA form.
104     *
105     * Before walking instructions, we consider the if-condition at the
106     * end of the block, if one exists.  It's effectively a use at the
107     * bottom of the block.
108     */
109    nir_if *iff = nir_block_get_following_if(block);
110    if (iff) {
111       progress |= move_source(&iff->condition, block, NULL, options);
112    }
113 
114    nir_foreach_instr_reverse(instr, block) {
115       /* The sources of phi instructions happen after the predecessor block
116        * but before this block.  (Yes, that's between blocks).  This means
117        * that we don't need to move them in order for them to be correct.
118        * We could move them to encourage comparisons that are used in a phi to
119        * the end of the block, doing so correctly would make the pass
120        * substantially more complicated and wouldn't gain us anything since
121        * the phi can't use a flag value anyway.
122        */
123 
124       if (instr->type == nir_instr_type_phi) {
125          /* We're going backwards so everything else is a phi too */
126          break;
127       } else if (instr->type == nir_instr_type_alu) {
128          /* Walk ALU instruction sources backwards so that bcsel's boolean
129           * condition is processed last for when comparisons are being moved.
130           */
131          nir_alu_instr *alu = nir_instr_as_alu(instr);
132          for (int i = nir_op_infos[alu->op].num_inputs - 1; i >= 0; i--) {
133             progress |= move_source(&alu->src[i].src, block, instr, options);
134          }
135       } else {
136          struct source_cb_data data;
137          data.progress = &progress;
138          data.options = options;
139          nir_foreach_src(instr, move_source_cb, &data);
140       }
141    }
142 
143    return progress;
144 }
145 
146 bool
nir_opt_move(nir_shader * shader,nir_move_options options)147 nir_opt_move(nir_shader *shader, nir_move_options options)
148 {
149    bool progress = false;
150 
151    nir_foreach_function(func, shader) {
152       if (!func->impl)
153          continue;
154 
155       bool impl_progress = false;
156       nir_foreach_block(block, func->impl) {
157          if (move(block, options))
158             impl_progress = true;
159       }
160 
161       if (impl_progress) {
162          nir_metadata_preserve(func->impl, nir_metadata_block_index |
163                                            nir_metadata_dominance |
164                                            nir_metadata_live_ssa_defs);
165          progress = true;
166       } else {
167          nir_metadata_preserve(func->impl, nir_metadata_all);
168       }
169    }
170 
171    return progress;
172 }
173