5c79296f5a8150e1d057a918f91f33f73650f554
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_sel_peephole.cpp
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 /**
41 * Scans forwards from an IF counting consecutive MOV instructions in the
42 * "then" and "else" blocks of the if statement.
43 *
44 * A pointer to the fs_inst* for IF is passed as the <if_inst> argument. The
45 * function stores pointers to the MOV instructions in the <then_mov> and
46 * <else_mov> arrays.
47 *
48 * \return the minimum number of MOVs found in the two branches or zero if
49 * an error occurred.
50 *
51 * E.g.:
52 * IF ...
53 * then_mov[0] = MOV g4, ...
54 * then_mov[1] = MOV g5, ...
55 * then_mov[2] = MOV g6, ...
56 * ELSE ...
57 * else_mov[0] = MOV g4, ...
58 * else_mov[1] = MOV g5, ...
59 * else_mov[2] = MOV g7, ...
60 * ENDIF
61 * returns 3.
62 */
63 static int
64 count_movs_from_if(fs_inst *then_mov[MAX_MOVS], fs_inst *else_mov[MAX_MOVS],
65 fs_inst *if_inst, fs_inst *else_inst)
66 {
67 fs_inst *m = if_inst;
68
69 assert(m->opcode == BRW_OPCODE_IF);
70 m = (fs_inst *) m->next;
71
72 int then_movs = 0;
73 while (then_movs < MAX_MOVS && m->opcode == BRW_OPCODE_MOV) {
74 then_mov[then_movs] = m;
75 m = (fs_inst *) m->next;
76 then_movs++;
77 }
78
79 m = (fs_inst *) else_inst->next;
80
81 int else_movs = 0;
82 while (else_movs < MAX_MOVS && m->opcode == BRW_OPCODE_MOV) {
83 else_mov[else_movs] = m;
84 m = (fs_inst *) m->next;
85 else_movs++;
86 }
87
88 return MIN2(then_movs, else_movs);
89 }
90
91 /**
92 * Try to replace IF/MOV+/ELSE/MOV+/ENDIF with SEL.
93 *
94 * Many GLSL shaders contain the following pattern:
95 *
96 * x = condition ? foo : bar
97 *
98 * or
99 *
100 * if (...) a.xyzw = foo.xyzw;
101 * else a.xyzw = bar.xyzw;
102 *
103 * The compiler emits an ir_if tree for this, since each subexpression might be
104 * a complex tree that could have side-effects or short-circuit logic.
105 *
106 * However, the common case is to simply select one of two constants or
107 * variable values---which is exactly what SEL is for. In this case, the
108 * assembly looks like:
109 *
110 * (+f0) IF
111 * MOV dst src0
112 * ...
113 * ELSE
114 * MOV dst src1
115 * ...
116 * ENDIF
117 *
118 * where each pair of MOVs to a common destination and can be easily translated
119 * into
120 *
121 * (+f0) SEL dst src0 src1
122 *
123 * If src0 is an immediate value, we promote it to a temporary GRF.
124 */
125 bool
126 fs_visitor::opt_peephole_sel()
127 {
128 bool progress = false;
129
130 calculate_cfg();
131
132 foreach_block (block, cfg) {
133 /* IF instructions, by definition, can only be found at the ends of
134 * basic blocks.
135 */
136 fs_inst *if_inst = (fs_inst *) block->end;
137 if (if_inst->opcode != BRW_OPCODE_IF)
138 continue;
139
140 if (!block->else_inst)
141 continue;
142
143 fs_inst *else_inst = (fs_inst *) block->else_inst;
144 assert(else_inst->opcode == BRW_OPCODE_ELSE);
145
146 fs_inst *else_mov[MAX_MOVS] = { NULL };
147 fs_inst *then_mov[MAX_MOVS] = { NULL };
148
149 int movs = count_movs_from_if(then_mov, else_mov, if_inst, else_inst);
150
151 if (movs == 0)
152 continue;
153
154 fs_inst *sel_inst[MAX_MOVS] = { NULL };
155 fs_inst *mov_imm_inst[MAX_MOVS] = { NULL };
156
157 enum brw_predicate predicate;
158 bool predicate_inverse;
159 if (brw->gen == 6 && if_inst->conditional_mod) {
160 /* For Sandybridge with IF with embedded comparison */
161 predicate = BRW_PREDICATE_NORMAL;
162 predicate_inverse = false;
163 } else {
164 /* Separate CMP and IF instructions */
165 predicate = if_inst->predicate;
166 predicate_inverse = if_inst->predicate_inverse;
167 }
168
169 /* Generate SEL instructions for pairs of MOVs to a common destination. */
170 for (int i = 0; i < movs; i++) {
171 if (!then_mov[i] || !else_mov[i])
172 break;
173
174 /* Check that the MOVs are the right form. */
175 if (!then_mov[i]->dst.equals(else_mov[i]->dst) ||
176 then_mov[i]->is_partial_write() ||
177 else_mov[i]->is_partial_write()) {
178 movs = i;
179 break;
180 }
181
182 /* Check that source types for mov operations match. */
183 if (then_mov[i]->src[0].type != else_mov[i]->src[0].type) {
184 movs = i;
185 break;
186 }
187
188 if (then_mov[i]->src[0].equals(else_mov[i]->src[0])) {
189 sel_inst[i] = MOV(then_mov[i]->dst, then_mov[i]->src[0]);
190 } else {
191 /* Only the last source register can be a constant, so if the MOV
192 * in the "then" clause uses a constant, we need to put it in a
193 * temporary.
194 */
195 fs_reg src0(then_mov[i]->src[0]);
196 if (src0.file == IMM) {
197 src0 = fs_reg(this, glsl_type::float_type);
198 src0.type = then_mov[i]->src[0].type;
199 mov_imm_inst[i] = MOV(src0, then_mov[i]->src[0]);
200 }
201
202 sel_inst[i] = SEL(then_mov[i]->dst, src0, else_mov[i]->src[0]);
203 sel_inst[i]->predicate = predicate;
204 sel_inst[i]->predicate_inverse = predicate_inverse;
205 }
206 }
207
208 if (movs == 0)
209 continue;
210
211 /* Emit a CMP if our IF used the embedded comparison */
212 if (brw->gen == 6 && if_inst->conditional_mod) {
213 fs_inst *cmp_inst = CMP(reg_null_d, if_inst->src[0], if_inst->src[1],
214 if_inst->conditional_mod);
215 if_inst->insert_before(cmp_inst);
216 }
217
218 for (int i = 0; i < movs; i++) {
219 if (mov_imm_inst[i])
220 if_inst->insert_before(mov_imm_inst[i]);
221 if_inst->insert_before(sel_inst[i]);
222
223 then_mov[i]->remove();
224 else_mov[i]->remove();
225 }
226
227 progress = true;
228 }
229
230 if (progress)
231 invalidate_live_intervals();
232
233 return progress;
234 }