i965/fs: Split fs_visitor::register_coalesce() into its own file.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_register_coalesce.cpp
1 /*
2 * Copyright © 2012 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 /** @file brw_fs_register_coalesce.cpp
25 *
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.
29 *
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:
32 *
33 * add vgrf3:F, vgrf1:F, vgrf2:F
34 * mov vgrf4:F, vgrf3:F
35 * mul vgrf5:F, vgrf5:F, vgrf4:F
36 *
37 * becomes
38 *
39 * add vgrf4:F, vgrf1:F, vgrf2:F
40 * mul vgrf5:F, vgrf5:F, vgrf4:F
41 */
42
43 #include "brw_fs.h"
44 #include "brw_fs_live_variables.h"
45
46 bool
47 fs_visitor::register_coalesce()
48 {
49 bool progress = false;
50
51 calculate_live_intervals();
52
53 int src_size = 0;
54 int channels_remaining = 0;
55 int reg_from = -1, reg_to = -1;
56 int reg_to_offset[MAX_SAMPLER_MESSAGE_SIZE];
57 fs_inst *mov[MAX_SAMPLER_MESSAGE_SIZE];
58
59 foreach_list(node, &this->instructions) {
60 fs_inst *inst = (fs_inst *)node;
61
62 if (inst->opcode != BRW_OPCODE_MOV ||
63 inst->is_partial_write() ||
64 inst->saturate ||
65 inst->src[0].file != GRF ||
66 inst->src[0].negate ||
67 inst->src[0].abs ||
68 !inst->src[0].is_contiguous() ||
69 inst->dst.file != GRF ||
70 inst->dst.type != inst->src[0].type) {
71 continue;
72 }
73
74 if (virtual_grf_sizes[inst->src[0].reg] >
75 virtual_grf_sizes[inst->dst.reg])
76 continue;
77
78 int var_from = live_intervals->var_from_reg(&inst->src[0]);
79 int var_to = live_intervals->var_from_reg(&inst->dst);
80
81 if (live_intervals->vars_interfere(var_from, var_to) &&
82 !inst->dst.equals(inst->src[0])) {
83
84 /* We know that the live ranges of A (var_from) and B (var_to)
85 * interfere because of the ->vars_interfere() call above. If the end
86 * of B's live range is after the end of A's range, then we know two
87 * things:
88 * - the start of B's live range must be in A's live range (since we
89 * already know the two ranges interfere, this is the only remaining
90 * possibility)
91 * - the interference isn't of the form we're looking for (where B is
92 * entirely inside A)
93 */
94 if (live_intervals->end[var_to] > live_intervals->end[var_from])
95 continue;
96
97 bool overwritten = false;
98 int scan_ip = -1;
99
100 foreach_list(n, &this->instructions) {
101 fs_inst *scan_inst = (fs_inst *)n;
102 scan_ip++;
103
104 if (scan_inst->is_control_flow()) {
105 overwritten = true;
106 break;
107 }
108
109 if (scan_ip <= live_intervals->start[var_to])
110 continue;
111
112 if (scan_ip > live_intervals->end[var_to])
113 break;
114
115 if (scan_inst->dst.equals(inst->dst) ||
116 scan_inst->dst.equals(inst->src[0])) {
117 overwritten = true;
118 break;
119 }
120 }
121
122 if (overwritten)
123 continue;
124 }
125
126 if (reg_from != inst->src[0].reg) {
127 reg_from = inst->src[0].reg;
128
129 src_size = virtual_grf_sizes[inst->src[0].reg];
130 assert(src_size <= MAX_SAMPLER_MESSAGE_SIZE);
131
132 channels_remaining = src_size;
133 memset(mov, 0, sizeof(mov));
134
135 reg_to = inst->dst.reg;
136 }
137
138 if (reg_to != inst->dst.reg)
139 continue;
140
141 const int offset = inst->src[0].reg_offset;
142 reg_to_offset[offset] = inst->dst.reg_offset;
143 mov[offset] = inst;
144 channels_remaining--;
145
146 if (channels_remaining)
147 continue;
148
149 bool removed = false;
150 for (int i = 0; i < src_size; i++) {
151 if (mov[i]) {
152 removed = true;
153
154 mov[i]->opcode = BRW_OPCODE_NOP;
155 mov[i]->conditional_mod = BRW_CONDITIONAL_NONE;
156 mov[i]->dst = reg_undef;
157 mov[i]->src[0] = reg_undef;
158 mov[i]->src[1] = reg_undef;
159 mov[i]->src[2] = reg_undef;
160 }
161 }
162
163 foreach_list(node, &this->instructions) {
164 fs_inst *scan_inst = (fs_inst *)node;
165
166 for (int i = 0; i < src_size; i++) {
167 if (mov[i]) {
168 if (scan_inst->dst.file == GRF &&
169 scan_inst->dst.reg == reg_from &&
170 scan_inst->dst.reg_offset == i) {
171 scan_inst->dst.reg = reg_to;
172 scan_inst->dst.reg_offset = reg_to_offset[i];
173 }
174 for (int j = 0; j < 3; j++) {
175 if (scan_inst->src[j].file == GRF &&
176 scan_inst->src[j].reg == reg_from &&
177 scan_inst->src[j].reg_offset == i) {
178 scan_inst->src[j].reg = reg_to;
179 scan_inst->src[j].reg_offset = reg_to_offset[i];
180 }
181 }
182 }
183 }
184 }
185
186 if (removed) {
187 live_intervals->start[var_to] = MIN2(live_intervals->start[var_to],
188 live_intervals->start[var_from]);
189 live_intervals->end[var_to] = MAX2(live_intervals->end[var_to],
190 live_intervals->end[var_from]);
191 reg_from = -1;
192 }
193 }
194
195 foreach_list_safe(node, &this->instructions) {
196 fs_inst *inst = (fs_inst *)node;
197
198 if (inst->opcode == BRW_OPCODE_NOP) {
199 inst->remove();
200 progress = true;
201 }
202 }
203
204 if (progress)
205 invalidate_live_intervals();
206
207 return progress;
208 }