i965/fs: Invalidate live intervals after copy propagation.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_copy_propagation.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 #include "brw_fs.h"
25 #include "brw_fs_cfg.h"
26
27 namespace { /* avoid conflict with opt_copy_propagation_elements */
28 struct acp_entry : public exec_node {
29 fs_reg dst;
30 fs_reg src;
31 };
32 }
33
34 bool
35 fs_visitor::try_copy_propagate(fs_inst *inst, int arg, acp_entry *entry)
36 {
37 if (inst->src[arg].file != entry->dst.file ||
38 inst->src[arg].reg != entry->dst.reg ||
39 inst->src[arg].reg_offset != entry->dst.reg_offset) {
40 return false;
41 }
42
43 /* See resolve_ud_negate() and comment in brw_fs_emit.cpp. */
44 if (inst->conditional_mod &&
45 inst->src[arg].type == BRW_REGISTER_TYPE_UD &&
46 entry->src.negate)
47 return false;
48
49 bool has_source_modifiers = entry->src.abs || entry->src.negate;
50
51 if (intel->gen == 6 && inst->is_math() &&
52 (has_source_modifiers || entry->src.file == UNIFORM))
53 return false;
54
55 inst->src[arg].file = entry->src.file;
56 inst->src[arg].reg = entry->src.reg;
57 inst->src[arg].reg_offset = entry->src.reg_offset;
58
59 if (!inst->src[arg].abs) {
60 inst->src[arg].abs = entry->src.abs;
61 inst->src[arg].negate ^= entry->src.negate;
62 }
63
64 return true;
65 }
66
67 /** @file brw_fs_copy_propagation.cpp
68 *
69 * Support for local copy propagation by walking the list of instructions
70 * and maintaining the ACP table of available copies for propagation.
71 *
72 * See Muchnik's Advanced Compiler Design and Implementation, section
73 * 12.5 (p356).
74 */
75
76 /* Walks a basic block and does copy propagation on it using the acp
77 * list.
78 */
79 bool
80 fs_visitor::opt_copy_propagate_local(void *mem_ctx,
81 fs_bblock *block, exec_list *acp)
82 {
83 bool progress = false;
84
85 for (fs_inst *inst = block->start;
86 inst != block->end->next;
87 inst = (fs_inst *)inst->next) {
88
89 /* Try propagating into this instruction. */
90 foreach_list(entry_node, acp) {
91 acp_entry *entry = (acp_entry *)entry_node;
92
93 for (int i = 0; i < 3; i++) {
94 if (try_copy_propagate(inst, i, entry))
95 progress = true;
96 }
97 }
98
99 /* kill the destination from the ACP */
100 if (inst->dst.file == GRF) {
101 int start_offset = inst->dst.reg_offset;
102 int end_offset = start_offset + inst->regs_written();
103
104 foreach_list_safe(entry_node, acp) {
105 acp_entry *entry = (acp_entry *)entry_node;
106
107 if (entry->dst.file == GRF &&
108 entry->dst.reg == inst->dst.reg &&
109 entry->dst.reg_offset >= start_offset &&
110 entry->dst.reg_offset < end_offset) {
111 entry->remove();
112 continue;
113 }
114 if (entry->src.file == GRF &&
115 entry->src.reg == inst->dst.reg &&
116 entry->src.reg_offset >= start_offset &&
117 entry->src.reg_offset < end_offset) {
118 entry->remove();
119 }
120 }
121 }
122
123 /* If this instruction is a raw copy, add it to the ACP. */
124 if (inst->opcode == BRW_OPCODE_MOV &&
125 inst->dst.file == GRF &&
126 ((inst->src[0].file == GRF &&
127 (inst->src[0].reg != inst->dst.reg ||
128 inst->src[0].reg_offset != inst->dst.reg_offset)) ||
129 inst->src[0].file == UNIFORM) &&
130 inst->src[0].type == inst->dst.type &&
131 !inst->saturate &&
132 !inst->predicated &&
133 !inst->force_uncompressed &&
134 !inst->force_sechalf &&
135 inst->src[0].smear == -1) {
136 acp_entry *entry = ralloc(mem_ctx, acp_entry);
137 entry->dst = inst->dst;
138 entry->src = inst->src[0];
139 acp->push_tail(entry);
140 }
141 }
142
143 return progress;
144 }
145
146 bool
147 fs_visitor::opt_copy_propagate()
148 {
149 bool progress = false;
150 void *mem_ctx = ralloc_context(this->mem_ctx);
151
152 fs_cfg cfg(this);
153
154 for (int b = 0; b < cfg.num_blocks; b++) {
155 fs_bblock *block = cfg.blocks[b];
156 exec_list acp;
157
158 progress = opt_copy_propagate_local(mem_ctx, block, &acp) || progress;
159 }
160
161 ralloc_free(mem_ctx);
162
163 if (progress)
164 live_intervals_valid = false;
165
166 return progress;
167 }