glsl2: Replace insert_before/remove pairs with exec_node::replace_with.
[mesa.git] / src / glsl / ir_if_return.cpp
1 /*
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 /**
25 * \file ir_if_return.cpp
26 *
27 * If a function includes an if statement that returns from both
28 * branches, then make the branches write the return val to a temp and
29 * return the temp after the if statement.
30 *
31 * This allows inlinining in the common case of short functions that
32 * return one of two values based on a condition. This helps on
33 * hardware with no branching support, and may even be a useful
34 * transform on hardware supporting control flow by masked returns
35 * with normal returns.
36 */
37
38 #include "ir.h"
39
40 class ir_if_return_visitor : public ir_hierarchical_visitor {
41 public:
42 ir_if_return_visitor()
43 {
44 this->progress = false;
45 }
46
47 ir_visitor_status visit_enter(ir_if *);
48
49 bool progress;
50 };
51
52 bool
53 do_if_return(exec_list *instructions)
54 {
55 ir_if_return_visitor v;
56
57 visit_list_elements(&v, instructions);
58
59 return v.progress;
60 }
61
62
63 ir_visitor_status
64 ir_if_return_visitor::visit_enter(ir_if *ir)
65 {
66 ir_return *then_return = NULL;
67 ir_return *else_return = NULL;
68
69 /* Try to find a return statement on both sides. */
70 foreach_iter(exec_list_iterator, then_iter, ir->then_instructions) {
71 ir_instruction *then_ir = (ir_instruction *)then_iter.get();
72 then_return = then_ir->as_return();
73 if (then_return)
74 break;
75 }
76 if (!then_return)
77 return visit_continue;
78
79 foreach_iter(exec_list_iterator, else_iter, ir->else_instructions) {
80 ir_instruction *else_ir = (ir_instruction *)else_iter.get();
81 else_return = else_ir->as_return();
82 if (else_return)
83 break;
84 }
85 if (!else_return)
86 return visit_continue;
87
88 /* Trim off any trailing instructions after the return statements
89 * on both sides.
90 */
91 while (then_return->get_next()->get_next())
92 ((ir_instruction *)then_return->get_next())->remove();
93 while (else_return->get_next()->get_next())
94 ((ir_instruction *)else_return->get_next())->remove();
95
96 this->progress = true;
97
98 if (!then_return->value) {
99 then_return->remove();
100 else_return->remove();
101 ir->insert_after(new(ir) ir_return(NULL));
102 } else {
103 ir_assignment *assign;
104 ir_variable *new_var = new(ir) ir_variable(then_return->value->type,
105 "if_return_tmp",
106 ir_var_temporary);
107 ir->insert_before(new_var);
108
109 assign = new(ir) ir_assignment(new(ir) ir_dereference_variable(new_var),
110 then_return->value, NULL);
111 then_return->replace_with(assign);
112
113 assign = new(ir) ir_assignment(new(ir) ir_dereference_variable(new_var),
114 else_return->value, NULL);
115 else_return->replace_with(assign);
116
117 ir_dereference_variable *deref = new(ir) ir_dereference_variable(new_var);
118 ir->insert_after(new(ir) ir_return(deref));
119 }
120
121 return visit_continue;
122 }