ir_reader: rvalues are instructions too!
[mesa.git] / ir_function_can_inline.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_function_can_inline.cpp
26 *
27 * Determines if we can inline a function call using ir_function_inlining.cpp.
28 *
29 * The primary restriction is that we can't return from the function
30 * other than as the last instruction. We could potentially work
31 * around this for some constructs by flattening control flow and
32 * moving the return to the end, or by using breaks from a do {} while
33 * (0) loop surrounding the function body.
34 */
35
36 #define NULL 0
37 #include "ir.h"
38 #include "ir_visitor.h"
39 #include "ir_function_inlining.h"
40 #include "ir_expression_flattening.h"
41 #include "glsl_types.h"
42
43 class ir_function_can_inline_visitor : public ir_visitor {
44 public:
45 ir_function_can_inline_visitor()
46 {
47 this->can_inline = true;
48 this->num_returns = 0;
49 }
50
51 /**
52 * \name Visit methods
53 *
54 * As typical for the visitor pattern, there must be one \c visit method for
55 * each concrete subclass of \c ir_instruction. Virtual base classes within
56 * the hierarchy should not have \c visit methods.
57 */
58 /*@{*/
59 virtual void visit(ir_variable *);
60 virtual void visit(ir_label *);
61 virtual void visit(ir_loop *);
62 virtual void visit(ir_loop_jump *);
63 virtual void visit(ir_function_signature *);
64 virtual void visit(ir_function *);
65 virtual void visit(ir_expression *);
66 virtual void visit(ir_swizzle *);
67 virtual void visit(ir_dereference *);
68 virtual void visit(ir_assignment *);
69 virtual void visit(ir_constant *);
70 virtual void visit(ir_call *);
71 virtual void visit(ir_return *);
72 virtual void visit(ir_if *);
73 /*@}*/
74
75 bool can_inline;
76 int num_returns;
77 };
78
79 void
80 ir_function_can_inline_visitor::visit(ir_variable *ir)
81 {
82 (void)ir;
83 }
84
85 void
86 ir_function_can_inline_visitor::visit(ir_label *ir)
87 {
88 (void)ir;
89 }
90
91 void
92 ir_function_can_inline_visitor::visit(ir_loop *ir)
93 {
94 /* FINISHME: Implement loop cloning in ir_function_inlining.cpp */
95 this->can_inline = false;
96
97 if (ir->from)
98 ir->from->accept(this);
99 if (ir->to)
100 ir->to->accept(this);
101 if (ir->increment)
102 ir->increment->accept(this);
103
104 foreach_iter(exec_list_iterator, iter, ir->body_instructions) {
105 ir_instruction *inner_ir = (ir_instruction *)iter.get();
106 inner_ir->accept(this);
107 }
108 }
109
110 void
111 ir_function_can_inline_visitor::visit(ir_loop_jump *ir)
112 {
113 (void) ir;
114 }
115
116
117 void
118 ir_function_can_inline_visitor::visit(ir_function_signature *ir)
119 {
120 (void)ir;
121 }
122
123
124 void
125 ir_function_can_inline_visitor::visit(ir_function *ir)
126 {
127 (void) ir;
128 }
129
130 void
131 ir_function_can_inline_visitor::visit(ir_expression *ir)
132 {
133 unsigned int operand;
134
135 for (operand = 0; operand < ir->get_num_operands(); operand++) {
136 ir->operands[operand]->accept(this);
137 }
138 }
139
140
141 void
142 ir_function_can_inline_visitor::visit(ir_swizzle *ir)
143 {
144 ir->val->accept(this);
145 }
146
147 void
148 ir_function_can_inline_visitor::visit(ir_dereference *ir)
149 {
150 ir->var->accept(this);
151 if (ir->mode == ir_dereference::ir_reference_array)
152 ir->selector.array_index->accept(this);
153 }
154
155 void
156 ir_function_can_inline_visitor::visit(ir_assignment *ir)
157 {
158 ir->lhs->accept(this);
159 ir->rhs->accept(this);
160 if (ir->condition)
161 ir->condition->accept(this);
162 }
163
164
165 void
166 ir_function_can_inline_visitor::visit(ir_constant *ir)
167 {
168 (void)ir;
169 }
170
171
172 void
173 ir_function_can_inline_visitor::visit(ir_call *ir)
174 {
175 foreach_iter(exec_list_iterator, iter, *ir) {
176 ir_rvalue *param = (ir_rvalue *)iter.get();
177
178 param->accept(this);
179 }
180 }
181
182
183 void
184 ir_function_can_inline_visitor::visit(ir_return *ir)
185 {
186 ir->get_value()->accept(this);
187
188 this->num_returns++;
189 }
190
191
192 void
193 ir_function_can_inline_visitor::visit(ir_if *ir)
194 {
195 /* FINISHME: Implement if cloning in ir_function_inlining.cpp. */
196 this->can_inline = false;
197
198 ir->condition->accept(this);
199
200 foreach_iter(exec_list_iterator, iter, ir->then_instructions) {
201 ir_instruction *inner_ir = (ir_instruction *)iter.get();
202 inner_ir->accept(this);
203 }
204
205 foreach_iter(exec_list_iterator, iter, ir->else_instructions) {
206 ir_instruction *inner_ir = (ir_instruction *)iter.get();
207 inner_ir->accept(this);
208 }
209 }
210
211 bool
212 can_inline(ir_call *call)
213 {
214 ir_function_can_inline_visitor v;
215 const ir_function_signature *callee = call->get_callee();
216
217 foreach_iter(exec_list_iterator, iter, callee->body) {
218 ir_instruction *ir = (ir_instruction *)iter.get();
219 ir->accept(&v);
220 }
221
222 ir_instruction *last = (ir_instruction *)callee->body.get_tail();
223 if (last && !last->as_return())
224 v.num_returns++;
225
226 return v.can_inline && v.num_returns == 1;
227 }