ir_expression_flattening.cpp revision 5723e5bb8b73cd2a3b77d750972e3d0b4d0d0ff8
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#include "ir.h"
37#include "ir_visitor.h"
38#include "ir_expression_flattening.h"
39#include "glsl_types.h"
40
41class ir_expression_flattening_visitor : public ir_hierarchical_visitor {
42public:
43   ir_expression_flattening_visitor(ir_instruction *base_ir,
44				    bool (*predicate)(ir_instruction *ir))
45   {
46      this->base_ir = base_ir;
47      this->predicate = predicate;
48   }
49
50   virtual ~ir_expression_flattening_visitor()
51   {
52      /* empty */
53   }
54
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_assignment *);
61   virtual ir_visitor_status visit_leave(ir_expression *);
62   virtual ir_visitor_status visit_leave(ir_swizzle *);
63
64   ir_rvalue *operand_to_temp(ir_rvalue *val);
65   bool (*predicate)(ir_instruction *ir);
66   ir_instruction *base_ir;
67};
68
69void
70do_expression_flattening(exec_list *instructions,
71			 bool (*predicate)(ir_instruction *ir))
72{
73   foreach_iter(exec_list_iterator, iter, *instructions) {
74      ir_instruction *ir = (ir_instruction *)iter.get();
75
76      ir_expression_flattening_visitor v(ir, predicate);
77      ir->accept(&v);
78   }
79}
80
81
82ir_rvalue *
83ir_expression_flattening_visitor::operand_to_temp(ir_rvalue *ir)
84{
85   void *ctx = talloc_parent(base_ir);
86   ir_variable *var;
87   ir_assignment *assign;
88
89   if (!this->predicate(ir))
90      return ir;
91
92   var = new(ctx) ir_variable(ir->type, "flattening_tmp");
93   base_ir->insert_before(var);
94
95   assign = new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
96				   ir,
97				   NULL);
98   base_ir->insert_before(assign);
99
100   return new(ctx) ir_dereference_variable(var);
101}
102
103ir_visitor_status
104ir_expression_flattening_visitor::visit_enter(ir_function_signature *ir)
105{
106   do_expression_flattening(&ir->body, this->predicate);
107
108   return visit_continue_with_parent;
109}
110
111ir_visitor_status
112ir_expression_flattening_visitor::visit_enter(ir_loop *ir)
113{
114   do_expression_flattening(&ir->body_instructions, this->predicate);
115
116   return visit_continue_with_parent;
117}
118
119ir_visitor_status
120ir_expression_flattening_visitor::visit_enter(ir_if *ir)
121{
122   ir->condition->accept(this);
123
124   do_expression_flattening(&ir->then_instructions, this->predicate);
125   do_expression_flattening(&ir->else_instructions, this->predicate);
126
127   return visit_continue_with_parent;
128}
129
130ir_visitor_status
131ir_expression_flattening_visitor::visit_leave(ir_expression *ir)
132{
133   unsigned int operand;
134
135   for (operand = 0; operand < ir->get_num_operands(); operand++) {
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      ir->operands[operand] = operand_to_temp(ir->operands[operand]);
140   }
141
142   return visit_continue;
143}
144
145ir_visitor_status
146ir_expression_flattening_visitor::visit_leave(ir_assignment *ir)
147{
148   ir->rhs = operand_to_temp(ir->rhs);
149   if (ir->condition)
150      ir->condition = operand_to_temp(ir->condition);
151
152   return visit_continue;
153}
154
155ir_visitor_status
156ir_expression_flattening_visitor::visit_leave(ir_swizzle *ir)
157{
158   if (this->predicate(ir->val)) {
159      ir->val = operand_to_temp(ir->val);
160   }
161
162   return visit_continue;
163}
164
165ir_visitor_status
166ir_expression_flattening_visitor::visit_enter(ir_call *ir)
167{
168   /* FINISHME: Why not process the call parameters? (Same behavior as original
169    * FINISHME: code.)
170    */
171   (void) ir;
172   return visit_continue_with_parent;
173}
174
175
176ir_visitor_status
177ir_expression_flattening_visitor::visit_enter(ir_return *ir)
178{
179   /* FINISHME: Why not process the return value? (Same behavior as original
180    * FINISHME: code.)
181    */
182   (void) ir;
183   return visit_continue_with_parent;
184}
185