2 * Copyright © 2011 Intel Corporation
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:
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
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
25 * @file brw_vec4_copy_propagation.cpp
27 * Implements tracking of values copied between registers, and
28 * optimizations based on that: copy propagation and constant
34 #include "main/macros.h"
40 is_direct_copy(vec4_instruction
*inst
)
42 return (inst
->opcode
== BRW_OPCODE_MOV
&&
44 inst
->dst
.file
== GRF
&&
47 !inst
->src
[0].reladdr
&&
48 inst
->dst
.type
== inst
->src
[0].type
);
52 is_dominated_by_previous_instruction(vec4_instruction
*inst
)
54 return (inst
->opcode
!= BRW_OPCODE_DO
&&
55 inst
->opcode
!= BRW_OPCODE_WHILE
&&
56 inst
->opcode
!= BRW_OPCODE_ELSE
&&
57 inst
->opcode
!= BRW_OPCODE_ENDIF
);
61 try_constant_propagation(vec4_instruction
*inst
, int arg
, src_reg
*values
[4])
63 /* For constant propagation, we only handle the same constant
64 * across all 4 channels. Some day, we should handle the 8-bit
65 * float vector format, which would let us constant propagate
68 src_reg value
= *values
[0];
69 for (int i
= 1; i
< 4; i
++) {
70 if (!value
.equals(values
[i
]))
74 if (value
.file
!= IMM
)
77 if (inst
->src
[arg
].abs
) {
78 if (value
.type
== BRW_REGISTER_TYPE_F
) {
79 value
.imm
.f
= fabs(value
.imm
.f
);
80 } else if (value
.type
== BRW_REGISTER_TYPE_D
) {
82 value
.imm
.i
= -value
.imm
.i
;
86 if (inst
->src
[arg
].negate
) {
87 if (value
.type
== BRW_REGISTER_TYPE_F
)
88 value
.imm
.f
= -value
.imm
.f
;
90 value
.imm
.u
= -value
.imm
.u
;
93 switch (inst
->opcode
) {
95 inst
->src
[arg
] = value
;
101 inst
->src
[arg
] = value
;
103 } else if (arg
== 0 && inst
->src
[1].file
!= IMM
) {
104 /* Fit this constant in by commuting the operands */
105 inst
->src
[0] = inst
->src
[1];
106 inst
->src
[1] = value
;
113 inst
->src
[arg
] = value
;
115 } else if (arg
== 0 && inst
->src
[1].file
!= IMM
) {
118 new_cmod
= brw_swap_cmod(inst
->conditional_mod
);
119 if (new_cmod
!= ~0u) {
120 /* Fit this constant in by swapping the operands and
123 inst
->src
[0] = inst
->src
[1];
124 inst
->src
[1] = value
;
125 inst
->conditional_mod
= new_cmod
;
133 inst
->src
[arg
] = value
;
135 } else if (arg
== 0 && inst
->src
[1].file
!= IMM
) {
136 inst
->src
[0] = inst
->src
[1];
137 inst
->src
[1] = value
;
139 /* If this was predicated, flipping operands means
140 * we also need to flip the predicate.
142 if (inst
->conditional_mod
== BRW_CONDITIONAL_NONE
) {
143 inst
->predicate_inverse
= !inst
->predicate_inverse
;
157 try_copy_propagation(struct intel_context
*intel
,
158 vec4_instruction
*inst
, int arg
, src_reg
*values
[4])
160 /* For constant propagation, we only handle the same constant
161 * across all 4 channels. Some day, we should handle the 8-bit
162 * float vector format, which would let us constant propagate
165 src_reg value
= *values
[0];
166 for (int i
= 1; i
< 4; i
++) {
167 /* This is equals() except we don't care about the swizzle. */
168 if (value
.file
!= values
[i
]->file
||
169 value
.reg
!= values
[i
]->reg
||
170 value
.reg_offset
!= values
[i
]->reg_offset
||
171 value
.type
!= values
[i
]->type
||
172 value
.negate
!= values
[i
]->negate
||
173 value
.abs
!= values
[i
]->abs
) {
178 /* Compute the swizzle of the original register by swizzling the
179 * component loaded from each value according to the swizzle of
180 * operand we're going to change.
183 for (int i
= 0; i
< 4; i
++) {
184 s
[i
] = BRW_GET_SWZ(values
[i
]->swizzle
,
185 BRW_GET_SWZ(inst
->src
[arg
].swizzle
, i
));
187 value
.swizzle
= BRW_SWIZZLE4(s
[0], s
[1], s
[2], s
[3]);
189 if (value
.file
!= UNIFORM
&&
193 if (inst
->src
[arg
].abs
) {
194 value
.negate
= false;
197 if (inst
->src
[arg
].negate
)
200 /* FINISHME: We can't copy-propagate things that aren't normal
201 * vec8s into gen6 math instructions, because of the weird src
202 * handling for those instructions. Just ignore them for now.
204 if (intel
->gen
>= 6 && inst
->is_math())
207 inst
->src
[arg
] = value
;
212 vec4_visitor::opt_copy_propagation()
214 bool progress
= false;
215 src_reg
*cur_value
[virtual_grf_reg_count
][4];
217 memset(&cur_value
, 0, sizeof(cur_value
));
219 foreach_list(node
, &this->instructions
) {
220 vec4_instruction
*inst
= (vec4_instruction
*)node
;
222 /* This pass only works on basic blocks. If there's flow
223 * control, throw out all our information and start from
226 * This should really be fixed by using a structure like in
227 * src/glsl/opt_copy_propagation.cpp to track available copies.
229 if (!is_dominated_by_previous_instruction(inst
)) {
230 memset(cur_value
, 0, sizeof(cur_value
));
234 /* For each source arg, see if each component comes from a copy
235 * from the same type file (IMM, GRF, UNIFORM), and try
236 * optimizing out access to the copy result
238 for (int i
= 2; i
>= 0; i
--) {
239 int reg
= (virtual_grf_reg_map
[inst
->src
[i
].reg
] +
240 inst
->src
[i
].reg_offset
);
242 /* Copied values end up in GRFs, and we don't track reladdr
245 if (inst
->src
[i
].file
!= GRF
||
246 inst
->src
[i
].reladdr
)
249 /* Find the regs that each swizzle component came from.
253 for (c
= 0; c
< 4; c
++) {
254 values
[c
] = cur_value
[reg
][BRW_GET_SWZ(inst
->src
[i
].swizzle
, c
)];
256 /* If there's no available copy for this channel, bail.
257 * We could be more aggressive here -- some channels might
258 * not get used based on the destination writemask.
263 /* We'll only be able to copy propagate if the sources are
264 * all from the same file -- there's no ability to swizzle
265 * 0 or 1 constants in with source registers like in i915.
267 if (c
> 0 && values
[c
- 1]->file
!= values
[c
]->file
)
274 if (try_constant_propagation(inst
, i
, values
) ||
275 try_copy_propagation(intel
, inst
, i
, values
))
279 /* Track available source registers. */
280 if (is_direct_copy(inst
)) {
281 int reg
= virtual_grf_reg_map
[inst
->dst
.reg
] + inst
->dst
.reg_offset
;
282 for (int i
= 0; i
< 4; i
++) {
283 if (inst
->dst
.writemask
& (1 << i
)) {
284 cur_value
[reg
][i
] = &inst
->src
[0];
290 /* For any updated channels, clear tracking of them as a source
293 * FINISHME: Sources aren't handled, which will need to be done
294 * for copy propagation.
296 if (inst
->dst
.file
== GRF
) {
297 if (inst
->dst
.reladdr
)
298 memset(cur_value
, 0, sizeof(cur_value
));
300 int reg
= virtual_grf_reg_map
[inst
->dst
.reg
] + inst
->dst
.reg_offset
;
302 for (int i
= 0; i
< 4; i
++) {
303 if (inst
->dst
.writemask
& (1 << i
))
304 cur_value
[reg
][i
] = NULL
;
311 live_intervals_valid
= false;
316 } /* namespace brw */