Fix the swizzling of vector constructors from scalars.
[mesa.git] / ir_expression_flattening.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_expression_flattening.cpp
26 *
27 * Takes the leaves of expression trees and makes them dereferences of
28 * assignments of the leaves to temporaries, according to a predicate.
29 *
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
33 * instruction stream.
34 */
35
36 #define NULL 0
37 #include "ir.h"
38 #include "ir_visitor.h"
39 #include "ir_expression_flattening.h"
40 #include "glsl_types.h"
41
42 class ir_expression_flattening_visitor : public ir_visitor {
43 public:
44 ir_expression_flattening_visitor(ir_instruction *base_ir,
45 bool (*predicate)(ir_instruction *ir))
46 {
47 this->base_ir = base_ir;
48 this->predicate = predicate;
49
50 /* empty */
51 }
52
53 virtual ~ir_expression_flattening_visitor()
54 {
55 /* empty */
56 }
57
58 /**
59 * \name Visit methods
60 *
61 * As typical for the visitor pattern, there must be one \c visit method for
62 * each concrete subclass of \c ir_instruction. Virtual base classes within
63 * the hierarchy should not have \c visit methods.
64 */
65 /*@{*/
66 virtual void visit(ir_variable *);
67 virtual void visit(ir_loop *);
68 virtual void visit(ir_loop_jump *);
69 virtual void visit(ir_function_signature *);
70 virtual void visit(ir_function *);
71 virtual void visit(ir_expression *);
72 virtual void visit(ir_swizzle *);
73 virtual void visit(ir_dereference *);
74 virtual void visit(ir_assignment *);
75 virtual void visit(ir_constant *);
76 virtual void visit(ir_call *);
77 virtual void visit(ir_return *);
78 virtual void visit(ir_if *);
79 /*@}*/
80
81 bool (*predicate)(ir_instruction *ir);
82 ir_instruction *base_ir;
83 };
84
85 void
86 do_expression_flattening(exec_list *instructions,
87 bool (*predicate)(ir_instruction *ir))
88 {
89 foreach_iter(exec_list_iterator, iter, *instructions) {
90 ir_instruction *ir = (ir_instruction *)iter.get();
91
92 ir_expression_flattening_visitor v(ir, predicate);
93 ir->accept(&v);
94 }
95 }
96
97 void
98 ir_expression_flattening_visitor::visit(ir_variable *ir)
99 {
100 (void) ir;
101 }
102
103 void
104 ir_expression_flattening_visitor::visit(ir_loop *ir)
105 {
106 do_expression_flattening(&ir->body_instructions, this->predicate);
107 }
108
109 void
110 ir_expression_flattening_visitor::visit(ir_loop_jump *ir)
111 {
112 (void) ir;
113 }
114
115
116 void
117 ir_expression_flattening_visitor::visit(ir_function_signature *ir)
118 {
119 do_expression_flattening(&ir->body, this->predicate);
120 }
121
122 void
123 ir_expression_flattening_visitor::visit(ir_function *ir)
124 {
125 (void) ir;
126 }
127
128 void
129 ir_expression_flattening_visitor::visit(ir_expression *ir)
130 {
131 unsigned int operand;
132
133 for (operand = 0; operand < ir->get_num_operands(); operand++) {
134 ir->operands[operand]->accept(this);
135
136 /* If the operand matches the predicate, then we'll assign its
137 * value to a temporary and deref the temporary as the operand.
138 */
139 if (this->predicate(ir->operands[operand])) {
140 ir_variable *var;
141 ir_assignment *assign;
142
143 var = new ir_variable(ir->operands[operand]->type, "flattening_tmp");
144 this->base_ir->insert_before(var);
145
146 assign = new ir_assignment(new ir_dereference(var),
147 ir->operands[operand],
148 NULL);
149 this->base_ir->insert_before(assign);
150
151 ir->operands[operand] = new ir_dereference(var);
152 }
153 }
154 }
155
156
157 void
158 ir_expression_flattening_visitor::visit(ir_swizzle *ir)
159 {
160 ir->val->accept(this);
161 }
162
163
164 void
165 ir_expression_flattening_visitor::visit(ir_dereference *ir)
166 {
167 if (ir->mode == ir_dereference::ir_reference_array) {
168 ir->selector.array_index->accept(this);
169 }
170 ir->var->accept(this);
171 }
172
173 void
174 ir_expression_flattening_visitor::visit(ir_assignment *ir)
175 {
176 ir->rhs->accept(this);
177 }
178
179
180 void
181 ir_expression_flattening_visitor::visit(ir_constant *ir)
182 {
183 (void) ir;
184 }
185
186
187 void
188 ir_expression_flattening_visitor::visit(ir_call *ir)
189 {
190 (void) ir;
191 }
192
193
194 void
195 ir_expression_flattening_visitor::visit(ir_return *ir)
196 {
197 (void) ir;
198 }
199
200
201 void
202 ir_expression_flattening_visitor::visit(ir_if *ir)
203 {
204 ir->condition->accept(this);
205
206 do_expression_flattening(&ir->then_instructions, this->predicate);
207 do_expression_flattening(&ir->else_instructions, this->predicate);
208 }