1 /*
2  * Copyright © 2013 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 #include "brw_fs.h"
25 #include "brw_cfg.h"
26 
27 /** @file brw_fs_sel_peephole.cpp
28  *
29  * This file contains the opt_peephole_sel() optimization pass that replaces
30  * MOV instructions to the same destination in the "then" and "else" bodies of
31  * an if statement with SEL instructions.
32  */
33 
34 /* Four MOVs seems to be pretty typical, so I picked the next power of two in
35  * the hopes that it would handle almost anything possible in a single
36  * pass.
37  */
38 #define MAX_MOVS 8 /**< The maximum number of MOVs to attempt to match. */
39 
40 using namespace brw;
41 
42 /**
43  * Scans forwards from an IF counting consecutive MOV instructions in the
44  * "then" and "else" blocks of the if statement.
45  *
46  * A pointer to the bblock_t following the IF is passed as the <then_block>
47  * argument. The function stores pointers to the MOV instructions in the
48  * <then_mov> and <else_mov> arrays.
49  *
50  * \return the minimum number of MOVs found in the two branches or zero if
51  *         an error occurred.
52  *
53  * E.g.:
54  *                  IF ...
55  *    then_mov[0] = MOV g4, ...
56  *    then_mov[1] = MOV g5, ...
57  *    then_mov[2] = MOV g6, ...
58  *                  ELSE ...
59  *    else_mov[0] = MOV g4, ...
60  *    else_mov[1] = MOV g5, ...
61  *    else_mov[2] = MOV g7, ...
62  *                  ENDIF
63  *    returns 3.
64  */
65 static int
count_movs_from_if(fs_inst * then_mov[MAX_MOVS],fs_inst * else_mov[MAX_MOVS],bblock_t * then_block,bblock_t * else_block)66 count_movs_from_if(fs_inst *then_mov[MAX_MOVS], fs_inst *else_mov[MAX_MOVS],
67                    bblock_t *then_block, bblock_t *else_block)
68 {
69    int then_movs = 0;
70    foreach_inst_in_block(fs_inst, inst, then_block) {
71       if (then_movs == MAX_MOVS || inst->opcode != BRW_OPCODE_MOV ||
72           inst->flags_written())
73          break;
74 
75       then_mov[then_movs] = inst;
76       then_movs++;
77    }
78 
79    int else_movs = 0;
80    foreach_inst_in_block(fs_inst, inst, else_block) {
81       if (else_movs == MAX_MOVS || inst->opcode != BRW_OPCODE_MOV ||
82           inst->flags_written())
83          break;
84 
85       else_mov[else_movs] = inst;
86       else_movs++;
87    }
88 
89    return MIN2(then_movs, else_movs);
90 }
91 
92 /**
93  * Try to replace IF/MOV+/ELSE/MOV+/ENDIF with SEL.
94  *
95  * Many GLSL shaders contain the following pattern:
96  *
97  *    x = condition ? foo : bar
98  *
99  * or
100  *
101  *    if (...) a.xyzw = foo.xyzw;
102  *    else     a.xyzw = bar.xyzw;
103  *
104  * The compiler emits an ir_if tree for this, since each subexpression might be
105  * a complex tree that could have side-effects or short-circuit logic.
106  *
107  * However, the common case is to simply select one of two constants or
108  * variable values---which is exactly what SEL is for.  In this case, the
109  * assembly looks like:
110  *
111  *    (+f0) IF
112  *    MOV dst src0
113  *    ...
114  *    ELSE
115  *    MOV dst src1
116  *    ...
117  *    ENDIF
118  *
119  * where each pair of MOVs to a common destination and can be easily translated
120  * into
121  *
122  *    (+f0) SEL dst src0 src1
123  *
124  * If src0 is an immediate value, we promote it to a temporary GRF.
125  */
126 bool
opt_peephole_sel()127 fs_visitor::opt_peephole_sel()
128 {
129    bool progress = false;
130 
131    foreach_block (block, cfg) {
132       /* IF instructions, by definition, can only be found at the ends of
133        * basic blocks.
134        */
135       fs_inst *if_inst = (fs_inst *)block->end();
136       if (if_inst->opcode != BRW_OPCODE_IF)
137          continue;
138 
139       fs_inst *else_mov[MAX_MOVS] = { NULL };
140       fs_inst *then_mov[MAX_MOVS] = { NULL };
141 
142       bblock_t *then_block = block->next();
143       bblock_t *else_block = NULL;
144       foreach_list_typed(bblock_link, child, link, &block->children) {
145          if (child->block != then_block) {
146             if (child->block->prev()->end()->opcode == BRW_OPCODE_ELSE) {
147                else_block = child->block;
148             }
149             break;
150          }
151       }
152       if (else_block == NULL)
153          continue;
154 
155       int movs = count_movs_from_if(then_mov, else_mov, then_block, else_block);
156 
157       if (movs == 0)
158          continue;
159 
160       /* Generate SEL instructions for pairs of MOVs to a common destination. */
161       for (int i = 0; i < movs; i++) {
162          if (!then_mov[i] || !else_mov[i])
163             break;
164 
165          /* Check that the MOVs are the right form. */
166          if (!then_mov[i]->dst.equals(else_mov[i]->dst) ||
167              then_mov[i]->exec_size != else_mov[i]->exec_size ||
168              then_mov[i]->group != else_mov[i]->group ||
169              then_mov[i]->force_writemask_all != else_mov[i]->force_writemask_all ||
170              then_mov[i]->is_partial_write() ||
171              else_mov[i]->is_partial_write() ||
172              then_mov[i]->conditional_mod != BRW_CONDITIONAL_NONE ||
173              else_mov[i]->conditional_mod != BRW_CONDITIONAL_NONE) {
174             movs = i;
175             break;
176          }
177 
178          /* Check that source types for mov operations match. */
179          if (then_mov[i]->src[0].type != else_mov[i]->src[0].type) {
180             movs = i;
181             break;
182          }
183       }
184 
185       if (movs == 0)
186          continue;
187 
188       for (int i = 0; i < movs; i++) {
189          const fs_builder ibld = fs_builder(this, then_block, then_mov[i])
190                                  .at(block, if_inst);
191 
192          if (then_mov[i]->src[0].equals(else_mov[i]->src[0])) {
193             ibld.MOV(then_mov[i]->dst, then_mov[i]->src[0]);
194          } else {
195             /* Only the last source register can be a constant, so if the MOV
196              * in the "then" clause uses a constant, we need to put it in a
197              * temporary.
198              */
199             fs_reg src0(then_mov[i]->src[0]);
200             if (src0.file == IMM) {
201                src0 = ibld.vgrf(then_mov[i]->src[0].type);
202                ibld.MOV(src0, then_mov[i]->src[0]);
203             }
204 
205             /* 64-bit immediates can't be placed in src1. */
206             fs_reg src1(else_mov[i]->src[0]);
207             if (src1.file == IMM && type_sz(src1.type) == 8) {
208                src1 = ibld.vgrf(else_mov[i]->src[0].type);
209                ibld.MOV(src1, else_mov[i]->src[0]);
210             }
211 
212             set_predicate_inv(if_inst->predicate, if_inst->predicate_inverse,
213                               ibld.SEL(then_mov[i]->dst, src0, src1));
214          }
215 
216          then_mov[i]->remove(then_block);
217          else_mov[i]->remove(else_block);
218       }
219 
220       progress = true;
221    }
222 
223    if (progress)
224       invalidate_analysis(DEPENDENCY_INSTRUCTIONS | DEPENDENCY_VARIABLES);
225 
226    return progress;
227 }
228