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
44 #include "brw_fs_live_variables.h"
47 is_nop_mov(const fs_inst
*inst
)
49 if (inst
->opcode
== BRW_OPCODE_MOV
) {
50 return inst
->dst
.equals(inst
->src
[0]);
57 is_coalesce_candidate(const fs_inst
*inst
, const int *virtual_grf_sizes
)
59 if (inst
->opcode
!= BRW_OPCODE_MOV
||
60 inst
->is_partial_write() ||
62 inst
->src
[0].file
!= GRF
||
63 inst
->src
[0].negate
||
65 !inst
->src
[0].is_contiguous() ||
66 inst
->dst
.file
!= GRF
||
67 inst
->dst
.type
!= inst
->src
[0].type
) {
71 if (virtual_grf_sizes
[inst
->src
[0].reg
] >
72 virtual_grf_sizes
[inst
->dst
.reg
])
79 can_coalesce_vars(brw::fs_live_variables
*live_intervals
,
80 const exec_list
*instructions
, const fs_inst
*inst
, int ip
,
81 int var_to
, int var_from
)
83 if (!live_intervals
->vars_interfere(var_from
, var_to
))
86 /* We know that the live ranges of A (var_from) and B (var_to)
87 * interfere because of the ->vars_interfere() call above. If the end
88 * of B's live range is after the end of A's range, then we know two
90 * - the start of B's live range must be in A's live range (since we
91 * already know the two ranges interfere, this is the only remaining
93 * - the interference isn't of the form we're looking for (where B is
96 if (live_intervals
->end
[var_to
] > live_intervals
->end
[var_from
])
99 assert(ip
>= live_intervals
->start
[var_to
]);
102 for (scan_inst
= (fs_inst
*)inst
->next
;
103 !scan_inst
->is_tail_sentinel() && ip
<= live_intervals
->end
[var_to
];
104 scan_inst
= (fs_inst
*)scan_inst
->next
, ip
++) {
105 if (scan_inst
->is_control_flow())
108 if (scan_inst
->dst
.equals(inst
->dst
) ||
109 scan_inst
->dst
.equals(inst
->src
[0]))
117 fs_visitor::register_coalesce()
119 bool progress
= false;
121 calculate_live_intervals();
124 int channels_remaining
= 0;
125 int reg_from
= -1, reg_to
= -1;
126 int reg_to_offset
[MAX_SAMPLER_MESSAGE_SIZE
];
127 fs_inst
*mov
[MAX_SAMPLER_MESSAGE_SIZE
];
128 int var_to
[MAX_SAMPLER_MESSAGE_SIZE
];
129 int var_from
[MAX_SAMPLER_MESSAGE_SIZE
];
132 foreach_list(node
, &this->instructions
) {
133 fs_inst
*inst
= (fs_inst
*)node
;
136 if (!is_coalesce_candidate(inst
, virtual_grf_sizes
))
139 if (is_nop_mov(inst
)) {
140 inst
->opcode
= BRW_OPCODE_NOP
;
145 if (reg_from
!= inst
->src
[0].reg
) {
146 reg_from
= inst
->src
[0].reg
;
148 src_size
= virtual_grf_sizes
[inst
->src
[0].reg
];
149 assert(src_size
<= MAX_SAMPLER_MESSAGE_SIZE
);
151 channels_remaining
= src_size
;
152 memset(mov
, 0, sizeof(mov
));
154 reg_to
= inst
->dst
.reg
;
157 if (reg_to
!= inst
->dst
.reg
)
160 const int offset
= inst
->src
[0].reg_offset
;
161 reg_to_offset
[offset
] = inst
->dst
.reg_offset
;
163 channels_remaining
--;
165 if (channels_remaining
)
168 bool can_coalesce
= true;
169 for (int i
= 0; i
< src_size
; i
++) {
170 var_to
[i
] = live_intervals
->var_from_vgrf
[reg_to
] + reg_to_offset
[i
];
171 var_from
[i
] = live_intervals
->var_from_vgrf
[reg_from
] + i
;
173 if (!can_coalesce_vars(live_intervals
, &instructions
, inst
, ip
,
174 var_to
[i
], var_from
[i
])) {
175 can_coalesce
= false;
186 for (int i
= 0; i
< src_size
; i
++) {
188 mov
[i
]->opcode
= BRW_OPCODE_NOP
;
189 mov
[i
]->conditional_mod
= BRW_CONDITIONAL_NONE
;
190 mov
[i
]->dst
= reg_undef
;
191 mov
[i
]->src
[0] = reg_undef
;
192 mov
[i
]->src
[1] = reg_undef
;
193 mov
[i
]->src
[2] = reg_undef
;
197 foreach_list(node
, &this->instructions
) {
198 fs_inst
*scan_inst
= (fs_inst
*)node
;
200 for (int i
= 0; i
< src_size
; i
++) {
202 if (scan_inst
->dst
.file
== GRF
&&
203 scan_inst
->dst
.reg
== reg_from
&&
204 scan_inst
->dst
.reg_offset
== i
) {
205 scan_inst
->dst
.reg
= reg_to
;
206 scan_inst
->dst
.reg_offset
= reg_to_offset
[i
];
208 for (int j
= 0; j
< 3; j
++) {
209 if (scan_inst
->src
[j
].file
== GRF
&&
210 scan_inst
->src
[j
].reg
== reg_from
&&
211 scan_inst
->src
[j
].reg_offset
== i
) {
212 scan_inst
->src
[j
].reg
= reg_to
;
213 scan_inst
->src
[j
].reg_offset
= reg_to_offset
[i
];
220 for (int i
= 0; i
< src_size
; i
++) {
221 live_intervals
->start
[var_to
[i
]] =
222 MIN2(live_intervals
->start
[var_to
[i
]],
223 live_intervals
->start
[var_from
[i
]]);
224 live_intervals
->end
[var_to
[i
]] =
225 MAX2(live_intervals
->end
[var_to
[i
]],
226 live_intervals
->end
[var_from
[i
]]);
232 foreach_list_safe(node
, &this->instructions
) {
233 fs_inst
*inst
= (fs_inst
*)node
;
235 if (inst
->opcode
== BRW_OPCODE_NOP
) {
240 invalidate_live_intervals();