ir_expression_flattening: Fix breakage from hierarchichal visitor.
[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_hierarchical_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
51 virtual ~ir_expression_flattening_visitor()
52 {
53 /* empty */
54 }
55
56 virtual ir_visitor_status visit_enter(ir_call *);
57 virtual ir_visitor_status visit_enter(ir_return *);
58 virtual ir_visitor_status visit_enter(ir_function_signature *);
59 virtual ir_visitor_status visit_enter(ir_if *);
60 virtual ir_visitor_status visit_enter(ir_loop *);
61 virtual ir_visitor_status visit_leave(ir_expression *);
62
63 bool (*predicate)(ir_instruction *ir);
64 ir_instruction *base_ir;
65 };
66
67 void
68 do_expression_flattening(exec_list *instructions,
69 bool (*predicate)(ir_instruction *ir))
70 {
71 foreach_iter(exec_list_iterator, iter, *instructions) {
72 ir_instruction *ir = (ir_instruction *)iter.get();
73
74 ir_expression_flattening_visitor v(ir, predicate);
75 ir->accept(&v);
76 }
77 }
78
79 ir_visitor_status
80 ir_expression_flattening_visitor::visit_enter(ir_function_signature *ir)
81 {
82 do_expression_flattening(&ir->body, this->predicate);
83
84 return visit_continue_with_parent;
85 }
86
87 ir_visitor_status
88 ir_expression_flattening_visitor::visit_enter(ir_loop *ir)
89 {
90 do_expression_flattening(&ir->body_instructions, this->predicate);
91
92 return visit_continue_with_parent;
93 }
94
95 ir_visitor_status
96 ir_expression_flattening_visitor::visit_enter(ir_if *ir)
97 {
98 ir->condition->accept(this);
99
100 do_expression_flattening(&ir->then_instructions, this->predicate);
101 do_expression_flattening(&ir->else_instructions, this->predicate);
102
103 return visit_continue_with_parent;
104 }
105
106 ir_visitor_status
107 ir_expression_flattening_visitor::visit_leave(ir_expression *ir)
108 {
109 unsigned int operand;
110
111 for (operand = 0; operand < ir->get_num_operands(); operand++) {
112 /* If the operand matches the predicate, then we'll assign its
113 * value to a temporary and deref the temporary as the operand.
114 */
115 if (this->predicate(ir->operands[operand])) {
116 ir_variable *var;
117 ir_assignment *assign;
118
119 var = new ir_variable(ir->operands[operand]->type, "flattening_tmp");
120 this->base_ir->insert_before(var);
121
122 assign = new ir_assignment(new ir_dereference_variable(var),
123 ir->operands[operand],
124 NULL);
125 this->base_ir->insert_before(assign);
126
127 ir->operands[operand] = new ir_dereference_variable(var);
128 }
129 }
130
131 return visit_continue;
132 }
133
134
135 ir_visitor_status
136 ir_expression_flattening_visitor::visit_enter(ir_call *ir)
137 {
138 /* FINISHME: Why not process the call parameters? (Same behavior as original
139 * FINISHME: code.)
140 */
141 (void) ir;
142 return visit_continue_with_parent;
143 }
144
145
146 ir_visitor_status
147 ir_expression_flattening_visitor::visit_enter(ir_return *ir)
148 {
149 /* FINISHME: Why not process the return value? (Same behavior as original
150 * FINISHME: code.)
151 */
152 (void) ir;
153 return visit_continue_with_parent;
154 }