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 is_channel_updated(vec4_instruction
*inst
, src_reg
*values
[4], int ch
)
63 const src_reg
*src
= values
[ch
];
65 /* consider GRF only */
66 assert(inst
->dst
.file
== GRF
);
67 if (!src
|| src
->file
!= GRF
)
70 return (src
->reg
== inst
->dst
.reg
&&
71 src
->reg_offset
== inst
->dst
.reg_offset
&&
72 inst
->dst
.writemask
& (1 << BRW_GET_SWZ(src
->swizzle
, ch
)));
76 try_constant_propagation(vec4_instruction
*inst
, int arg
, src_reg
*values
[4])
78 /* For constant propagation, we only handle the same constant
79 * across all 4 channels. Some day, we should handle the 8-bit
80 * float vector format, which would let us constant propagate
83 src_reg value
= *values
[0];
84 for (int i
= 1; i
< 4; i
++) {
85 if (!value
.equals(values
[i
]))
89 if (value
.file
!= IMM
)
92 if (inst
->src
[arg
].abs
) {
93 if (value
.type
== BRW_REGISTER_TYPE_F
) {
94 value
.imm
.f
= fabs(value
.imm
.f
);
95 } else if (value
.type
== BRW_REGISTER_TYPE_D
) {
97 value
.imm
.i
= -value
.imm
.i
;
101 if (inst
->src
[arg
].negate
) {
102 if (value
.type
== BRW_REGISTER_TYPE_F
)
103 value
.imm
.f
= -value
.imm
.f
;
105 value
.imm
.u
= -value
.imm
.u
;
108 switch (inst
->opcode
) {
110 inst
->src
[arg
] = value
;
117 case BRW_OPCODE_BFI1
:
121 case BRW_OPCODE_SUBB
:
123 inst
->src
[arg
] = value
;
128 case BRW_OPCODE_MACH
:
134 case BRW_OPCODE_ADDC
:
136 inst
->src
[arg
] = value
;
138 } else if (arg
== 0 && inst
->src
[1].file
!= IMM
) {
139 /* Fit this constant in by commuting the operands. Exception: we
140 * can't do this for 32-bit integer MUL/MACH because it's asymmetric.
142 if ((inst
->opcode
== BRW_OPCODE_MUL
||
143 inst
->opcode
== BRW_OPCODE_MACH
) &&
144 (inst
->src
[1].type
== BRW_REGISTER_TYPE_D
||
145 inst
->src
[1].type
== BRW_REGISTER_TYPE_UD
))
147 inst
->src
[0] = inst
->src
[1];
148 inst
->src
[1] = value
;
155 inst
->src
[arg
] = value
;
157 } else if (arg
== 0 && inst
->src
[1].file
!= IMM
) {
160 new_cmod
= brw_swap_cmod(inst
->conditional_mod
);
161 if (new_cmod
!= ~0u) {
162 /* Fit this constant in by swapping the operands and
165 inst
->src
[0] = inst
->src
[1];
166 inst
->src
[1] = value
;
167 inst
->conditional_mod
= new_cmod
;
175 inst
->src
[arg
] = value
;
177 } else if (arg
== 0 && inst
->src
[1].file
!= IMM
) {
178 inst
->src
[0] = inst
->src
[1];
179 inst
->src
[1] = value
;
181 /* If this was predicated, flipping operands means
182 * we also need to flip the predicate.
184 if (inst
->conditional_mod
== BRW_CONDITIONAL_NONE
) {
185 inst
->predicate_inverse
= !inst
->predicate_inverse
;
199 vec4_visitor::try_copy_propagation(vec4_instruction
*inst
, int arg
,
202 /* For constant propagation, we only handle the same constant
203 * across all 4 channels. Some day, we should handle the 8-bit
204 * float vector format, which would let us constant propagate
207 src_reg value
= *values
[0];
208 for (int i
= 1; i
< 4; i
++) {
209 /* This is equals() except we don't care about the swizzle. */
210 if (value
.file
!= values
[i
]->file
||
211 value
.reg
!= values
[i
]->reg
||
212 value
.reg_offset
!= values
[i
]->reg_offset
||
213 value
.type
!= values
[i
]->type
||
214 value
.negate
!= values
[i
]->negate
||
215 value
.abs
!= values
[i
]->abs
) {
220 /* Compute the swizzle of the original register by swizzling the
221 * component loaded from each value according to the swizzle of
222 * operand we're going to change.
225 for (int i
= 0; i
< 4; i
++) {
226 s
[i
] = BRW_GET_SWZ(values
[i
]->swizzle
,
227 BRW_GET_SWZ(inst
->src
[arg
].swizzle
, i
));
229 value
.swizzle
= BRW_SWIZZLE4(s
[0], s
[1], s
[2], s
[3]);
231 if (value
.file
!= UNIFORM
&&
236 if (inst
->src
[arg
].abs
) {
237 value
.negate
= false;
240 if (inst
->src
[arg
].negate
)
241 value
.negate
= !value
.negate
;
243 bool has_source_modifiers
= value
.negate
|| value
.abs
;
245 /* gen6 math and gen7+ SENDs from GRFs ignore source modifiers on
248 if ((has_source_modifiers
|| value
.file
== UNIFORM
||
249 value
.swizzle
!= BRW_SWIZZLE_XYZW
) && !can_do_source_mods(inst
))
252 if (has_source_modifiers
&& value
.type
!= inst
->src
[arg
].type
)
255 bool is_3src_inst
= (inst
->opcode
== BRW_OPCODE_LRP
||
256 inst
->opcode
== BRW_OPCODE_MAD
||
257 inst
->opcode
== BRW_OPCODE_BFE
||
258 inst
->opcode
== BRW_OPCODE_BFI2
);
259 if (is_3src_inst
&& value
.file
== UNIFORM
)
262 if (inst
->is_send_from_grf())
265 /* We can't copy-propagate a UD negation into a condmod
266 * instruction, because the condmod ends up looking at the 33-bit
267 * signed accumulator value instead of the 32-bit value we wanted
269 if (inst
->conditional_mod
&&
271 value
.type
== BRW_REGISTER_TYPE_UD
)
274 /* Don't report progress if this is a noop. */
275 if (value
.equals(&inst
->src
[arg
]))
278 value
.type
= inst
->src
[arg
].type
;
279 inst
->src
[arg
] = value
;
284 vec4_visitor::opt_copy_propagation()
286 bool progress
= false;
287 src_reg
*cur_value
[virtual_grf_reg_count
][4];
289 memset(&cur_value
, 0, sizeof(cur_value
));
291 foreach_list(node
, &this->instructions
) {
292 vec4_instruction
*inst
= (vec4_instruction
*)node
;
294 /* This pass only works on basic blocks. If there's flow
295 * control, throw out all our information and start from
298 * This should really be fixed by using a structure like in
299 * src/glsl/opt_copy_propagation.cpp to track available copies.
301 if (!is_dominated_by_previous_instruction(inst
)) {
302 memset(cur_value
, 0, sizeof(cur_value
));
306 /* For each source arg, see if each component comes from a copy
307 * from the same type file (IMM, GRF, UNIFORM), and try
308 * optimizing out access to the copy result
310 for (int i
= 2; i
>= 0; i
--) {
311 /* Copied values end up in GRFs, and we don't track reladdr
314 if (inst
->src
[i
].file
!= GRF
||
315 inst
->src
[i
].reladdr
)
318 int reg
= (virtual_grf_reg_map
[inst
->src
[i
].reg
] +
319 inst
->src
[i
].reg_offset
);
321 /* Find the regs that each swizzle component came from.
325 for (c
= 0; c
< 4; c
++) {
326 values
[c
] = cur_value
[reg
][BRW_GET_SWZ(inst
->src
[i
].swizzle
, c
)];
328 /* If there's no available copy for this channel, bail.
329 * We could be more aggressive here -- some channels might
330 * not get used based on the destination writemask.
335 /* We'll only be able to copy propagate if the sources are
336 * all from the same file -- there's no ability to swizzle
337 * 0 or 1 constants in with source registers like in i915.
339 if (c
> 0 && values
[c
- 1]->file
!= values
[c
]->file
)
346 if (try_constant_propagation(inst
, i
, values
) ||
347 try_copy_propagation(inst
, i
, values
))
351 /* Track available source registers. */
352 if (inst
->dst
.file
== GRF
) {
354 virtual_grf_reg_map
[inst
->dst
.reg
] + inst
->dst
.reg_offset
;
356 /* Update our destination's current channel values. For a direct copy,
357 * the value is the newly propagated source. Otherwise, we don't know
358 * the new value, so clear it.
360 bool direct_copy
= is_direct_copy(inst
);
361 for (int i
= 0; i
< 4; i
++) {
362 if (inst
->dst
.writemask
& (1 << i
)) {
363 cur_value
[reg
][i
] = direct_copy
? &inst
->src
[0] : NULL
;
367 /* Clear the records for any registers whose current value came from
368 * our destination's updated channels, as the two are no longer equal.
370 if (inst
->dst
.reladdr
)
371 memset(cur_value
, 0, sizeof(cur_value
));
373 for (int i
= 0; i
< virtual_grf_reg_count
; i
++) {
374 for (int j
= 0; j
< 4; j
++) {
375 if (is_channel_updated(inst
, cur_value
[i
], j
)){
376 cur_value
[i
][j
] = NULL
;
385 invalidate_live_intervals();
390 } /* namespace brw */