Implement "step" builtin.
[mesa.git] / ir_constant_folding.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_constant_folding.cpp
26 * Replace constant-valued expressions with references to constant values.
27 */
28
29 #define NULL 0
30 #include "ir.h"
31 #include "ir_visitor.h"
32 #include "ir_optimization.h"
33 #include "glsl_types.h"
34
35 /**
36 * Visitor class for replacing expressions with ir_constant values.
37 */
38
39 class ir_constant_folding_visitor : public ir_visitor {
40 public:
41 ir_constant_folding_visitor()
42 {
43 /* empty */
44 }
45
46 virtual ~ir_constant_folding_visitor()
47 {
48 /* empty */
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_function_signature *);
61 virtual void visit(ir_function *);
62 virtual void visit(ir_expression *);
63 virtual void visit(ir_swizzle *);
64 virtual void visit(ir_dereference *);
65 virtual void visit(ir_assignment *);
66 virtual void visit(ir_constant *);
67 virtual void visit(ir_call *);
68 virtual void visit(ir_return *);
69 virtual void visit(ir_if *);
70 virtual void visit(ir_loop *);
71 virtual void visit(ir_loop_jump *);
72 /*@}*/
73 };
74
75 void
76 ir_constant_folding_visitor::visit(ir_variable *ir)
77 {
78 (void) ir;
79 }
80
81
82 void
83 ir_constant_folding_visitor::visit(ir_function_signature *ir)
84 {
85 visit_exec_list(&ir->body, this);
86 }
87
88
89 void
90 ir_constant_folding_visitor::visit(ir_function *ir)
91 {
92 (void) ir;
93 }
94
95 void
96 ir_constant_folding_visitor::visit(ir_expression *ir)
97 {
98 ir_constant *op[2];
99 unsigned int operand;
100
101 for (operand = 0; operand < ir->get_num_operands(); operand++) {
102 op[operand] = ir->operands[operand]->constant_expression_value();
103 if (op[operand]) {
104 ir->operands[operand] = op[operand];
105 } else {
106 ir->operands[operand]->accept(this);
107 }
108 }
109 }
110
111
112 void
113 ir_constant_folding_visitor::visit(ir_swizzle *ir)
114 {
115 ir->val->accept(this);
116 }
117
118
119 void
120 ir_constant_folding_visitor::visit(ir_dereference *ir)
121 {
122 if (ir->mode == ir_dereference::ir_reference_array) {
123 ir_constant *const_val = ir->selector.array_index->constant_expression_value();
124 if (const_val)
125 ir->selector.array_index = const_val;
126 else
127 ir->selector.array_index->accept(this);
128 }
129 ir->var->accept(this);
130 }
131
132
133 void
134 ir_constant_folding_visitor::visit(ir_assignment *ir)
135 {
136 ir_constant *const_val = ir->rhs->constant_expression_value();
137 if (const_val)
138 ir->rhs = const_val;
139 else
140 ir->rhs->accept(this);
141 }
142
143
144 void
145 ir_constant_folding_visitor::visit(ir_constant *ir)
146 {
147 (void) ir;
148 }
149
150
151 void
152 ir_constant_folding_visitor::visit(ir_call *ir)
153 {
154 (void) ir;
155 }
156
157
158 void
159 ir_constant_folding_visitor::visit(ir_return *ir)
160 {
161 (void) ir;
162 }
163
164
165 void
166 ir_constant_folding_visitor::visit(ir_if *ir)
167 {
168 ir_constant *const_val = ir->condition->constant_expression_value();
169 if (const_val)
170 ir->condition = const_val;
171 else
172 ir->condition->accept(this);
173
174 visit_exec_list(&ir->then_instructions, this);
175 visit_exec_list(&ir->else_instructions, this);
176 }
177
178
179 void
180 ir_constant_folding_visitor::visit(ir_loop *ir)
181 {
182 (void) ir;
183 }
184
185
186 void
187 ir_constant_folding_visitor::visit(ir_loop_jump *ir)
188 {
189 (void) ir;
190 }
191
192 bool
193 do_constant_folding(exec_list *instructions)
194 {
195 ir_constant_folding_visitor constant_folding;
196
197 visit_exec_list(instructions, &constant_folding);
198
199 /* FINISHME: Return real progress. */
200 return false;
201 }