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_expression_flattening.cpp
27 * Takes the leaves of expression trees and makes them dereferences of
28 * assignments of the leaves to temporaries, according to a predicate.
30 * This is used for automatic function inlining, where we want to take
31 * an expression containing a call and move the call out to its own
32 * assignment so that we can inline it at the appropriate place in the
37 #include "ir_visitor.h"
38 #include "ir_expression_flattening.h"
39 #include "glsl_types.h"
41 class ir_expression_flattening_visitor
: public ir_hierarchical_visitor
{
43 ir_expression_flattening_visitor(ir_instruction
*base_ir
,
44 bool (*predicate
)(ir_instruction
*ir
))
46 this->base_ir
= base_ir
;
47 this->predicate
= predicate
;
50 virtual ~ir_expression_flattening_visitor()
55 virtual ir_visitor_status
visit_enter(ir_call
*);
56 virtual ir_visitor_status
visit_enter(ir_return
*);
57 virtual ir_visitor_status
visit_enter(ir_function_signature
*);
58 virtual ir_visitor_status
visit_enter(ir_if
*);
59 virtual ir_visitor_status
visit_enter(ir_loop
*);
60 virtual ir_visitor_status
visit_leave(ir_expression
*);
61 virtual ir_visitor_status
visit_leave(ir_swizzle
*);
63 bool (*predicate
)(ir_instruction
*ir
);
64 ir_instruction
*base_ir
;
68 do_expression_flattening(exec_list
*instructions
,
69 bool (*predicate
)(ir_instruction
*ir
))
71 foreach_iter(exec_list_iterator
, iter
, *instructions
) {
72 ir_instruction
*ir
= (ir_instruction
*)iter
.get();
74 ir_expression_flattening_visitor
v(ir
, predicate
);
81 operand_to_temp(ir_instruction
*base_ir
, ir_rvalue
*ir
)
84 ir_assignment
*assign
;
86 var
= new ir_variable(ir
->type
, "flattening_tmp");
87 base_ir
->insert_before(var
);
89 assign
= new ir_assignment(new ir_dereference_variable(var
),
92 base_ir
->insert_before(assign
);
94 return new ir_dereference_variable(var
);
98 ir_expression_flattening_visitor::visit_enter(ir_function_signature
*ir
)
100 do_expression_flattening(&ir
->body
, this->predicate
);
102 return visit_continue_with_parent
;
106 ir_expression_flattening_visitor::visit_enter(ir_loop
*ir
)
108 do_expression_flattening(&ir
->body_instructions
, this->predicate
);
110 return visit_continue_with_parent
;
114 ir_expression_flattening_visitor::visit_enter(ir_if
*ir
)
116 ir
->condition
->accept(this);
118 do_expression_flattening(&ir
->then_instructions
, this->predicate
);
119 do_expression_flattening(&ir
->else_instructions
, this->predicate
);
121 return visit_continue_with_parent
;
125 ir_expression_flattening_visitor::visit_leave(ir_expression
*ir
)
127 unsigned int operand
;
129 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
130 /* If the operand matches the predicate, then we'll assign its
131 * value to a temporary and deref the temporary as the operand.
133 if (this->predicate(ir
->operands
[operand
])) {
134 ir
->operands
[operand
] = operand_to_temp(base_ir
,
135 ir
->operands
[operand
]);
139 return visit_continue
;
143 ir_expression_flattening_visitor::visit_leave(ir_swizzle
*ir
)
145 if (this->predicate(ir
->val
)) {
146 ir
->val
= operand_to_temp(this->base_ir
, ir
->val
);
149 return visit_continue
;
153 ir_expression_flattening_visitor::visit_enter(ir_call
*ir
)
155 /* FINISHME: Why not process the call parameters? (Same behavior as original
159 return visit_continue_with_parent
;
164 ir_expression_flattening_visitor::visit_enter(ir_return
*ir
)
166 /* FINISHME: Why not process the return value? (Same behavior as original
170 return visit_continue_with_parent
;