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
27 /** @file brw_fs_cse.cpp
29 * Support for local common subexpression elimination.
31 * See Muchnik's Advanced Compiler Design and Implementation, section
36 struct aeb_entry
: public exec_node
{
37 /** The instruction that generates the expression value. */
40 /** The temporary where the value is stored. */
46 is_expression(const fs_inst
*const inst
)
48 switch (inst
->opcode
) {
68 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD
:
69 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7
:
70 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD
:
71 case FS_OPCODE_CINTERP
:
72 case FS_OPCODE_LINTERP
:
80 operands_match(fs_reg
*xs
, fs_reg
*ys
)
82 return xs
[0].equals(ys
[0]) && xs
[1].equals(ys
[1]) && xs
[2].equals(ys
[2]);
86 fs_visitor::opt_cse_local(bblock_t
*block
, exec_list
*aeb
)
88 bool progress
= false;
90 void *mem_ctx
= ralloc_context(this->mem_ctx
);
92 int ip
= block
->start_ip
;
93 for (fs_inst
*inst
= (fs_inst
*)block
->start
;
94 inst
!= block
->end
->next
;
95 inst
= (fs_inst
*) inst
->next
) {
97 /* Skip some cases. */
98 if (is_expression(inst
) &&
100 !inst
->is_partial_write() &&
101 !inst
->conditional_mod
&&
102 inst
->dst
.file
!= HW_REG
)
107 foreach_list(entry_node
, aeb
) {
108 entry
= (aeb_entry
*) entry_node
;
110 /* Match current instruction's expression against those in AEB. */
111 if (inst
->opcode
== entry
->generator
->opcode
&&
112 inst
->saturate
== entry
->generator
->saturate
&&
113 inst
->dst
.type
== entry
->generator
->dst
.type
&&
114 operands_match(entry
->generator
->src
, inst
->src
)) {
123 /* Our first sighting of this expression. Create an entry. */
124 aeb_entry
*entry
= ralloc(mem_ctx
, aeb_entry
);
125 entry
->tmp
= reg_undef
;
126 entry
->generator
= inst
;
127 aeb
->push_tail(entry
);
129 /* This is at least our second sighting of this expression.
130 * If we don't have a temporary already, make one.
132 bool no_existing_temp
= entry
->tmp
.file
== BAD_FILE
;
133 if (no_existing_temp
) {
134 int written
= entry
->generator
->regs_written
;
136 fs_reg orig_dst
= entry
->generator
->dst
;
137 fs_reg tmp
= fs_reg(GRF
, virtual_grf_alloc(written
),
140 entry
->generator
->dst
= tmp
;
142 for (int i
= 0; i
< written
; i
++) {
143 fs_inst
*copy
= MOV(orig_dst
, tmp
);
144 copy
->force_writemask_all
=
145 entry
->generator
->force_writemask_all
;
146 entry
->generator
->insert_after(copy
);
148 orig_dst
.reg_offset
++;
154 int written
= inst
->regs_written
;
155 assert(written
== entry
->generator
->regs_written
);
156 assert(inst
->dst
.type
== entry
->tmp
.type
);
157 fs_reg dst
= inst
->dst
;
158 fs_reg tmp
= entry
->tmp
;
159 fs_inst
*copy
= NULL
;
160 for (int i
= 0; i
< written
; i
++) {
161 copy
= MOV(dst
, tmp
);
162 copy
->force_writemask_all
= inst
->force_writemask_all
;
163 inst
->insert_before(copy
);
170 /* Appending an instruction may have changed our bblock end. */
171 if (inst
== block
->end
) {
175 /* Continue iteration with copy->next */
180 foreach_list_safe(entry_node
, aeb
) {
181 aeb_entry
*entry
= (aeb_entry
*)entry_node
;
183 for (int i
= 0; i
< 3; i
++) {
184 fs_reg
*src_reg
= &entry
->generator
->src
[i
];
186 /* Kill all AEB entries that use the destination we just
189 if (inst
->overwrites_reg(entry
->generator
->src
[i
])) {
195 /* Kill any AEB entries using registers that don't get reused any
196 * more -- a sure sign they'll fail operands_match().
198 if (src_reg
->file
== GRF
&& virtual_grf_end
[src_reg
->reg
] < ip
) {
209 ralloc_free(mem_ctx
);
212 invalidate_live_intervals();
218 fs_visitor::opt_cse()
220 bool progress
= false;
222 calculate_live_intervals();
226 for (int b
= 0; b
< cfg
.num_blocks
; b
++) {
227 bblock_t
*block
= cfg
.blocks
[b
];
230 progress
= opt_cse_local(block
, &aeb
) || progress
;