2 * Copyright © 2010 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
21 * DEALINGS IN THE SOFTWARE.
25 * \file ir_if_return.cpp
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.
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.
40 class ir_if_return_visitor
: public ir_hierarchical_visitor
{
42 ir_if_return_visitor()
44 this->progress
= false;
47 ir_visitor_status
visit_enter(ir_if
*);
53 do_if_return(exec_list
*instructions
)
55 ir_if_return_visitor v
;
57 visit_list_elements(&v
, instructions
);
64 ir_if_return_visitor::visit_enter(ir_if
*ir
)
66 ir_return
*then_return
= NULL
;
67 ir_return
*else_return
= NULL
;
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();
77 return visit_continue
;
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();
86 return visit_continue
;
88 /* Trim off any trailing instructions after the return statements
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();
96 this->progress
= true;
98 if (!then_return
->value
) {
99 then_return
->remove();
100 else_return
->remove();
101 ir
->insert_after(new(ir
) ir_return(NULL
));
103 ir_assignment
*assign
;
104 ir_variable
*new_var
= new(ir
) ir_variable(then_return
->value
->type
,
107 ir
->insert_before(new_var
);
109 assign
= new(ir
) ir_assignment(new(ir
) ir_dereference_variable(new_var
),
110 then_return
->value
, NULL
);
111 then_return
->replace_with(assign
);
113 assign
= new(ir
) ir_assignment(new(ir
) ir_dereference_variable(new_var
),
114 else_return
->value
, NULL
);
115 else_return
->replace_with(assign
);
117 ir_dereference_variable
*deref
= new(ir
) ir_dereference_variable(new_var
);
118 ir
->insert_after(new(ir
) ir_return(deref
));
121 return visit_continue
;