2 * Copyright © 2012 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
24 /** @file brw_fs_register_coalesce.cpp
26 * Implements register coalescing: Checks if the two registers involved in a
27 * raw move don't interfere, in which case they can both be stored in the same
28 * place and the MOV removed.
30 * To do this, all uses of the source of the MOV in the shader are replaced
31 * with the destination of the MOV. For example:
33 * add vgrf3:F, vgrf1:F, vgrf2:F
34 * mov vgrf4:F, vgrf3:F
35 * mul vgrf5:F, vgrf5:F, vgrf4:F
39 * add vgrf4:F, vgrf1:F, vgrf2:F
40 * mul vgrf5:F, vgrf5:F, vgrf4:F
45 #include "brw_fs_live_variables.h"
48 is_nop_mov(const fs_inst
*inst
)
50 if (inst
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
) {
51 fs_reg dst
= inst
->dst
;
52 for (int i
= 0; i
< inst
->sources
; i
++) {
53 dst
.offset
= i
* REG_SIZE
+ dst
.offset
% REG_SIZE
;
54 if (!dst
.equals(inst
->src
[i
])) {
59 } else if (inst
->opcode
== BRW_OPCODE_MOV
) {
60 return inst
->dst
.equals(inst
->src
[0]);
67 is_coalesce_candidate(const fs_visitor
*v
, const fs_inst
*inst
)
69 if ((inst
->opcode
!= BRW_OPCODE_MOV
&&
70 inst
->opcode
!= SHADER_OPCODE_LOAD_PAYLOAD
) ||
71 inst
->is_partial_write() ||
73 inst
->src
[0].file
!= VGRF
||
74 inst
->src
[0].negate
||
76 !inst
->src
[0].is_contiguous() ||
77 inst
->dst
.file
!= VGRF
||
78 inst
->dst
.type
!= inst
->src
[0].type
) {
82 if (v
->alloc
.sizes
[inst
->src
[0].nr
] >
83 v
->alloc
.sizes
[inst
->dst
.nr
])
86 if (inst
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
) {
87 if (!inst
->is_copy_payload(v
->alloc
)) {
96 can_coalesce_vars(brw::fs_live_variables
*live_intervals
,
97 const cfg_t
*cfg
, const fs_inst
*inst
,
98 int dst_var
, int src_var
)
100 if (!live_intervals
->vars_interfere(src_var
, dst_var
))
103 int dst_start
= live_intervals
->start
[dst_var
];
104 int dst_end
= live_intervals
->end
[dst_var
];
105 int src_start
= live_intervals
->start
[src_var
];
106 int src_end
= live_intervals
->end
[src_var
];
108 /* Variables interfere and one line range isn't a subset of the other. */
109 if ((dst_end
> src_end
&& src_start
< dst_start
) ||
110 (src_end
> dst_end
&& dst_start
< src_start
))
113 /* Check for a write to either register in the intersection of their live
116 int start_ip
= MAX2(dst_start
, src_start
);
117 int end_ip
= MIN2(dst_end
, src_end
);
119 foreach_block(block
, cfg
) {
120 if (block
->end_ip
< start_ip
)
123 int scan_ip
= block
->start_ip
- 1;
125 foreach_inst_in_block(fs_inst
, scan_inst
, block
) {
128 /* Ignore anything before the intersection of the live ranges */
129 if (scan_ip
< start_ip
)
132 /* Ignore the copying instruction itself */
133 if (scan_inst
== inst
)
136 if (scan_ip
> end_ip
)
137 return true; /* registers do not interfere */
139 if (scan_inst
->overwrites_reg(inst
->dst
) ||
140 scan_inst
->overwrites_reg(inst
->src
[0]))
141 return false; /* registers interfere */
149 fs_visitor::register_coalesce()
151 bool progress
= false;
153 calculate_live_intervals();
156 int channels_remaining
= 0;
157 int src_reg
= -1, dst_reg
= -1;
158 int dst_reg_offset
[MAX_VGRF_SIZE
];
159 fs_inst
*mov
[MAX_VGRF_SIZE
];
160 int dst_var
[MAX_VGRF_SIZE
];
161 int src_var
[MAX_VGRF_SIZE
];
163 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
164 if (!is_coalesce_candidate(this, inst
))
167 if (is_nop_mov(inst
)) {
168 inst
->opcode
= BRW_OPCODE_NOP
;
173 if (src_reg
!= inst
->src
[0].nr
) {
174 src_reg
= inst
->src
[0].nr
;
176 src_size
= alloc
.sizes
[inst
->src
[0].nr
];
177 assert(src_size
<= MAX_VGRF_SIZE
);
179 channels_remaining
= src_size
;
180 memset(mov
, 0, sizeof(mov
));
182 dst_reg
= inst
->dst
.nr
;
185 if (dst_reg
!= inst
->dst
.nr
)
188 if (inst
->opcode
== SHADER_OPCODE_LOAD_PAYLOAD
) {
189 for (int i
= 0; i
< src_size
; i
++) {
190 dst_reg_offset
[i
] = i
;
193 channels_remaining
-= inst
->regs_written
;
195 const int offset
= inst
->src
[0].offset
/ REG_SIZE
;
197 /* This is the second time that this offset in the register has
198 * been set. This means, in particular, that inst->dst was
199 * live before this instruction and that the live ranges of
200 * inst->dst and inst->src[0] overlap and we can't coalesce the
201 * two variables. Let's ensure that doesn't happen.
203 channels_remaining
= -1;
206 dst_reg_offset
[offset
] = inst
->dst
.offset
/ REG_SIZE
;
207 if (inst
->regs_written
> 1)
208 dst_reg_offset
[offset
+ 1] = inst
->dst
.offset
/ REG_SIZE
+ 1;
210 channels_remaining
-= inst
->regs_written
;
213 if (channels_remaining
)
216 bool can_coalesce
= true;
217 for (int i
= 0; i
< src_size
; i
++) {
218 if (dst_reg_offset
[i
] != dst_reg_offset
[0] + i
) {
219 /* Registers are out-of-order. */
220 can_coalesce
= false;
225 dst_var
[i
] = live_intervals
->var_from_vgrf
[dst_reg
] + dst_reg_offset
[i
];
226 src_var
[i
] = live_intervals
->var_from_vgrf
[src_reg
] + i
;
228 if (!can_coalesce_vars(live_intervals
, cfg
, inst
,
229 dst_var
[i
], src_var
[i
])) {
230 can_coalesce
= false;
241 for (int i
= 0; i
< src_size
; i
++) {
243 mov
[i
]->opcode
= BRW_OPCODE_NOP
;
244 mov
[i
]->conditional_mod
= BRW_CONDITIONAL_NONE
;
245 mov
[i
]->dst
= reg_undef
;
246 for (int j
= 0; j
< mov
[i
]->sources
; j
++) {
247 mov
[i
]->src
[j
] = reg_undef
;
252 foreach_block_and_inst(block
, fs_inst
, scan_inst
, cfg
) {
253 if (scan_inst
->dst
.file
== VGRF
&&
254 scan_inst
->dst
.nr
== src_reg
) {
255 scan_inst
->dst
.nr
= dst_reg
;
256 scan_inst
->dst
.offset
= scan_inst
->dst
.offset
% REG_SIZE
+
257 dst_reg_offset
[scan_inst
->dst
.offset
/ REG_SIZE
] * REG_SIZE
;
260 for (int j
= 0; j
< scan_inst
->sources
; j
++) {
261 if (scan_inst
->src
[j
].file
== VGRF
&&
262 scan_inst
->src
[j
].nr
== src_reg
) {
263 scan_inst
->src
[j
].nr
= dst_reg
;
264 scan_inst
->src
[j
].offset
= scan_inst
->src
[j
].offset
% REG_SIZE
+
265 dst_reg_offset
[scan_inst
->src
[j
].offset
/ REG_SIZE
] * REG_SIZE
;
270 for (int i
= 0; i
< src_size
; i
++) {
271 live_intervals
->start
[dst_var
[i
]] =
272 MIN2(live_intervals
->start
[dst_var
[i
]],
273 live_intervals
->start
[src_var
[i
]]);
274 live_intervals
->end
[dst_var
[i
]] =
275 MAX2(live_intervals
->end
[dst_var
[i
]],
276 live_intervals
->end
[src_var
[i
]]);
282 foreach_block_and_inst_safe (block
, backend_instruction
, inst
, cfg
) {
283 if (inst
->opcode
== BRW_OPCODE_NOP
) {
288 invalidate_live_intervals();