blob: 806027b280ea9d39e80f8b681a0e0bcf2d06460a [file] [log] [blame]
Eric Anholt5c89f0e2010-05-04 13:04:40 -07001/*
2 * Copyright © 2010 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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24/**
Chad Versacedf883eb2010-11-17 10:43:10 -080025 * \file opt_copy_propagation.cpp
Eric Anholt5c89f0e2010-05-04 13:04:40 -070026 *
Eric Anholtaef0aae2010-05-05 09:38:09 -070027 * Moves usage of recently-copied variables to the previous copy of
Eric Anholt2dd3ae02010-08-08 17:56:34 -070028 * the variable.
Eric Anholtaef0aae2010-05-05 09:38:09 -070029 *
30 * This should reduce the number of MOV instructions in the generated
31 * programs unless copy propagation is also done on the LIR, and may
32 * help anyway by triggering other optimizations that live in the HIR.
Eric Anholt5c89f0e2010-05-04 13:04:40 -070033 */
34
Eric Anholt5c89f0e2010-05-04 13:04:40 -070035#include "ir.h"
36#include "ir_visitor.h"
37#include "ir_basic_block.h"
Eric Anholtbdd9b1f2010-05-05 11:45:30 -070038#include "ir_optimization.h"
Eric Anholt5c89f0e2010-05-04 13:04:40 -070039#include "glsl_types.h"
40
Eric Anholt337d9c92012-05-29 16:18:37 -070041namespace {
42
Eric Anholt5c89f0e2010-05-04 13:04:40 -070043class acp_entry : public exec_node
44{
45public:
46 acp_entry(ir_variable *lhs, ir_variable *rhs)
47 {
48 assert(lhs);
49 assert(rhs);
50 this->lhs = lhs;
51 this->rhs = rhs;
52 }
53
54 ir_variable *lhs;
55 ir_variable *rhs;
56};
57
Eric Anholt2dd3ae02010-08-08 17:56:34 -070058
59class kill_entry : public exec_node
60{
61public:
62 kill_entry(ir_variable *var)
63 {
64 assert(var);
65 this->var = var;
66 }
67
68 ir_variable *var;
69};
70
Ian Romanick2fd22482010-05-26 17:04:19 -070071class ir_copy_propagation_visitor : public ir_hierarchical_visitor {
Eric Anholt5c89f0e2010-05-04 13:04:40 -070072public:
Eric Anholt2dd3ae02010-08-08 17:56:34 -070073 ir_copy_propagation_visitor()
Eric Anholt5c89f0e2010-05-04 13:04:40 -070074 {
75 progress = false;
Kenneth Graunked3073f52011-01-21 14:32:31 -080076 mem_ctx = ralloc_context(0);
Eric Anholt2dd3ae02010-08-08 17:56:34 -070077 this->acp = new(mem_ctx) exec_list;
78 this->kills = new(mem_ctx) exec_list;
79 }
80 ~ir_copy_propagation_visitor()
81 {
Kenneth Graunked3073f52011-01-21 14:32:31 -080082 ralloc_free(mem_ctx);
Eric Anholt5c89f0e2010-05-04 13:04:40 -070083 }
84
Ian Romanick2fd22482010-05-26 17:04:19 -070085 virtual ir_visitor_status visit(class ir_dereference_variable *);
86 virtual ir_visitor_status visit_enter(class ir_loop *);
87 virtual ir_visitor_status visit_enter(class ir_function_signature *);
88 virtual ir_visitor_status visit_enter(class ir_function *);
Ian Romanick4e5b41c2010-08-27 16:22:36 -070089 virtual ir_visitor_status visit_leave(class ir_assignment *);
Ian Romanick2fd22482010-05-26 17:04:19 -070090 virtual ir_visitor_status visit_enter(class ir_call *);
91 virtual ir_visitor_status visit_enter(class ir_if *);
Eric Anholt5c89f0e2010-05-04 13:04:40 -070092
Eric Anholt2dd3ae02010-08-08 17:56:34 -070093 void add_copy(ir_assignment *ir);
94 void kill(ir_variable *ir);
95 void handle_if_block(exec_list *instructions);
96
97 /** List of acp_entry: The available copies to propagate */
Eric Anholt5c89f0e2010-05-04 13:04:40 -070098 exec_list *acp;
Eric Anholt2dd3ae02010-08-08 17:56:34 -070099 /**
100 * List of kill_entry: The variables whose values were killed in this
101 * block.
102 */
103 exec_list *kills;
104
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700105 bool progress;
Ian Romanick2fd22482010-05-26 17:04:19 -0700106
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700107 bool killed_all;
108
109 void *mem_ctx;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700110};
111
Eric Anholt337d9c92012-05-29 16:18:37 -0700112} /* unnamed namespace */
113
Ian Romanick2fd22482010-05-26 17:04:19 -0700114ir_visitor_status
115ir_copy_propagation_visitor::visit_enter(ir_function_signature *ir)
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700116{
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700117 /* Treat entry into a function signature as a completely separate
118 * block. Any instructions at global scope will be shuffled into
119 * main() at link time, so they're irrelevant to us.
120 */
121 exec_list *orig_acp = this->acp;
122 exec_list *orig_kills = this->kills;
123 bool orig_killed_all = this->killed_all;
124
125 this->acp = new(mem_ctx) exec_list;
126 this->kills = new(mem_ctx) exec_list;
127 this->killed_all = false;
128
129 visit_list_elements(this, &ir->body);
130
Kenneth Graunke76960a52014-12-16 18:33:39 -0800131 ralloc_free(this->acp);
132 ralloc_free(this->kills);
133
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700134 this->kills = orig_kills;
135 this->acp = orig_acp;
136 this->killed_all = orig_killed_all;
137
Ian Romanick2fd22482010-05-26 17:04:19 -0700138 return visit_continue_with_parent;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700139}
140
Ian Romanick2fd22482010-05-26 17:04:19 -0700141ir_visitor_status
Ian Romanick4e5b41c2010-08-27 16:22:36 -0700142ir_copy_propagation_visitor::visit_leave(ir_assignment *ir)
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700143{
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700144 kill(ir->lhs->variable_referenced());
145
146 add_copy(ir);
147
Ian Romanick4e5b41c2010-08-27 16:22:36 -0700148 return visit_continue;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700149}
150
Ian Romanick2fd22482010-05-26 17:04:19 -0700151ir_visitor_status
152ir_copy_propagation_visitor::visit_enter(ir_function *ir)
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700153{
154 (void) ir;
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700155 return visit_continue;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700156}
157
158/**
159 * Replaces dereferences of ACP RHS variables with ACP LHS variables.
160 *
161 * This is where the actual copy propagation occurs. Note that the
162 * rewriting of ir_dereference means that the ir_dereference instance
163 * must not be shared by multiple IR operations!
164 */
Ian Romanick2fd22482010-05-26 17:04:19 -0700165ir_visitor_status
Ian Romanickc7b10462010-05-19 13:20:12 +0200166ir_copy_propagation_visitor::visit(ir_dereference_variable *ir)
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700167{
Ian Romanick4e5b41c2010-08-27 16:22:36 -0700168 if (this->in_assignee)
169 return visit_continue;
170
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700171 ir_variable *var = ir->var;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700172
Matt Turner4d784462014-06-24 21:34:05 -0700173 foreach_in_list(acp_entry, entry, this->acp) {
Ian Romanickc7b10462010-05-19 13:20:12 +0200174 if (var == entry->lhs) {
175 ir->var = entry->rhs;
176 this->progress = true;
177 break;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700178 }
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700179 }
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700180
Ian Romanick2fd22482010-05-26 17:04:19 -0700181 return visit_continue;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700182}
183
184
Ian Romanick2fd22482010-05-26 17:04:19 -0700185ir_visitor_status
186ir_copy_propagation_visitor::visit_enter(ir_call *ir)
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700187{
Aras Pranckevicius63cddb22010-08-06 16:09:02 +0200188 /* Do copy propagation on call parameters, but skip any out params */
Kenneth Graunke48d0faa2014-01-10 16:39:17 -0800189 foreach_two_lists(formal_node, &ir->callee->parameters,
190 actual_node, &ir->actual_parameters) {
191 ir_variable *sig_param = (ir_variable *) formal_node;
192 ir_rvalue *ir = (ir_rvalue *) actual_node;
Tapani Pälli33ee2c62013-12-12 13:51:01 +0200193 if (sig_param->data.mode != ir_var_function_out
194 && sig_param->data.mode != ir_var_function_inout) {
Aras Pranckevicius63cddb22010-08-06 16:09:02 +0200195 ir->accept(this);
196 }
Aras Pranckevicius63cddb22010-08-06 16:09:02 +0200197 }
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700198
Eric Anholtb6d49ab2011-01-30 07:50:21 +1000199 /* Since we're unlinked, we don't (necessarily) know the side effects of
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700200 * this call. So kill all copies.
201 */
202 acp->make_empty();
203 this->killed_all = true;
204
Ian Romanick2fd22482010-05-26 17:04:19 -0700205 return visit_continue_with_parent;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700206}
207
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700208void
209ir_copy_propagation_visitor::handle_if_block(exec_list *instructions)
210{
211 exec_list *orig_acp = this->acp;
212 exec_list *orig_kills = this->kills;
213 bool orig_killed_all = this->killed_all;
214
215 this->acp = new(mem_ctx) exec_list;
216 this->kills = new(mem_ctx) exec_list;
217 this->killed_all = false;
218
219 /* Populate the initial acp with a copy of the original */
Matt Turner4d784462014-06-24 21:34:05 -0700220 foreach_in_list(acp_entry, a, orig_acp) {
Kenneth Graunke76960a52014-12-16 18:33:39 -0800221 this->acp->push_tail(new(this->acp) acp_entry(a->lhs, a->rhs));
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700222 }
223
224 visit_list_elements(this, instructions);
225
226 if (this->killed_all) {
227 orig_acp->make_empty();
228 }
229
230 exec_list *new_kills = this->kills;
231 this->kills = orig_kills;
Kenneth Graunke76960a52014-12-16 18:33:39 -0800232 ralloc_free(this->acp);
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700233 this->acp = orig_acp;
234 this->killed_all = this->killed_all || orig_killed_all;
235
Matt Turner4d784462014-06-24 21:34:05 -0700236 foreach_in_list(kill_entry, k, new_kills) {
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700237 kill(k->var);
238 }
Kenneth Graunke76960a52014-12-16 18:33:39 -0800239
240 ralloc_free(new_kills);
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700241}
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700242
Ian Romanick2fd22482010-05-26 17:04:19 -0700243ir_visitor_status
244ir_copy_propagation_visitor::visit_enter(ir_if *ir)
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700245{
246 ir->condition->accept(this);
Ian Romanick2fd22482010-05-26 17:04:19 -0700247
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700248 handle_if_block(&ir->then_instructions);
249 handle_if_block(&ir->else_instructions);
250
251 /* handle_if_block() already descended into the children. */
Ian Romanick2fd22482010-05-26 17:04:19 -0700252 return visit_continue_with_parent;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700253}
254
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700255ir_visitor_status
256ir_copy_propagation_visitor::visit_enter(ir_loop *ir)
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700257{
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700258 exec_list *orig_acp = this->acp;
259 exec_list *orig_kills = this->kills;
260 bool orig_killed_all = this->killed_all;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700261
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700262 /* FINISHME: For now, the initial acp for loops is totally empty.
263 * We could go through once, then go through again with the acp
264 * cloned minus the killed entries after the first run through.
265 */
266 this->acp = new(mem_ctx) exec_list;
267 this->kills = new(mem_ctx) exec_list;
268 this->killed_all = false;
Eric Anholt8e75de32010-05-05 09:31:53 -0700269
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700270 visit_list_elements(this, &ir->body_instructions);
271
272 if (this->killed_all) {
273 orig_acp->make_empty();
274 }
275
276 exec_list *new_kills = this->kills;
277 this->kills = orig_kills;
Kenneth Graunke76960a52014-12-16 18:33:39 -0800278 ralloc_free(this->acp);
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700279 this->acp = orig_acp;
280 this->killed_all = this->killed_all || orig_killed_all;
281
Matt Turner4d784462014-06-24 21:34:05 -0700282 foreach_in_list(kill_entry, k, new_kills) {
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700283 kill(k->var);
284 }
285
Kenneth Graunke76960a52014-12-16 18:33:39 -0800286 ralloc_free(new_kills);
287
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700288 /* already descended into the children. */
289 return visit_continue_with_parent;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700290}
291
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700292void
293ir_copy_propagation_visitor::kill(ir_variable *var)
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700294{
Ian Romanick5d82e232010-05-14 17:36:00 -0700295 assert(var != NULL);
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700296
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700297 /* Remove any entries currently in the ACP for this kill. */
Matt Turnerc6a16f62014-06-24 21:58:35 -0700298 foreach_in_list_safe(acp_entry, entry, acp) {
Ian Romanick5d82e232010-05-14 17:36:00 -0700299 if (entry->lhs == var || entry->rhs == var) {
300 entry->remove();
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700301 }
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700302 }
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700303
304 /* Add the LHS variable to the list of killed variables in this block.
305 */
Kenneth Graunke76960a52014-12-16 18:33:39 -0800306 this->kills->push_tail(new(this->kills) kill_entry(var));
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700307}
308
309/**
310 * Adds an entry to the available copy list if it's a plain assignment
311 * of a variable to a variable.
312 */
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700313void
314ir_copy_propagation_visitor::add_copy(ir_assignment *ir)
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700315{
316 acp_entry *entry;
317
Eric Anholt29a2e912011-01-30 07:59:14 +1000318 if (ir->condition)
319 return;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700320
Ian Romanick5a7758e2010-08-02 18:48:25 -0700321 ir_variable *lhs_var = ir->whole_variable_written();
Ian Romanickb067db22010-05-26 11:32:52 -0700322 ir_variable *rhs_var = ir->rhs->whole_variable_referenced();
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700323
Ian Romanickb067db22010-05-26 11:32:52 -0700324 if ((lhs_var != NULL) && (rhs_var != NULL)) {
Eric Anholtc5b9cab2010-08-04 23:23:15 -0700325 if (lhs_var == rhs_var) {
326 /* This is a dumb assignment, but we've conveniently noticed
327 * it here. Removing it now would mess up the loop iteration
328 * calling us. Just flag it to not execute, and someone else
329 * will clean up the mess.
330 */
Kenneth Graunked3073f52011-01-21 14:32:31 -0800331 ir->condition = new(ralloc_parent(ir)) ir_constant(false);
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700332 this->progress = true;
Eric Anholtc5b9cab2010-08-04 23:23:15 -0700333 } else {
Kenneth Graunke76960a52014-12-16 18:33:39 -0800334 entry = new(this->acp) acp_entry(lhs_var, rhs_var);
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700335 this->acp->push_tail(entry);
Eric Anholtc5b9cab2010-08-04 23:23:15 -0700336 }
Ian Romanickb067db22010-05-26 11:32:52 -0700337 }
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700338}
339
340/**
341 * Does a copy propagation pass on the code present in the instruction stream.
342 */
343bool
344do_copy_propagation(exec_list *instructions)
345{
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700346 ir_copy_propagation_visitor v;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700347
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700348 visit_list_elements(&v, instructions);
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700349
Eric Anholt2dd3ae02010-08-08 17:56:34 -0700350 return v.progress;
Eric Anholt5c89f0e2010-05-04 13:04:40 -0700351}