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