1b145e903694fa932ab1e0d955e889555193ab604Eric Anholt/*
2b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * Copyright © 2010 Intel Corporation
3b145e903694fa932ab1e0d955e889555193ab604Eric Anholt *
4b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * Permission is hereby granted, free of charge, to any person obtaining a
5b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * copy of this software and associated documentation files (the "Software"),
6b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * to deal in the Software without restriction, including without limitation
7b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * and/or sell copies of the Software, and to permit persons to whom the
9b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * Software is furnished to do so, subject to the following conditions:
10b145e903694fa932ab1e0d955e889555193ab604Eric Anholt *
11b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * The above copyright notice and this permission notice (including the next
12b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * paragraph) shall be included in all copies or substantial portions of the
13b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * Software.
14b145e903694fa932ab1e0d955e889555193ab604Eric Anholt *
15b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * DEALINGS IN THE SOFTWARE.
22b145e903694fa932ab1e0d955e889555193ab604Eric Anholt */
23b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
24b145e903694fa932ab1e0d955e889555193ab604Eric Anholt/**
25df883eb1575a740bf91e01cbe2eaa4dbc1f9f154Chad Versace * \file lower_vec_index_to_swizzle.cpp
26b145e903694fa932ab1e0d955e889555193ab604Eric Anholt *
27b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * Turns constant indexing into vector types to swizzles.  This will
28b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * let other swizzle-aware optimization passes catch these constructs,
29b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * and codegen backends not have to worry about this case.
30b145e903694fa932ab1e0d955e889555193ab604Eric Anholt */
31b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
32b145e903694fa932ab1e0d955e889555193ab604Eric Anholt#include "ir.h"
33b145e903694fa932ab1e0d955e889555193ab604Eric Anholt#include "ir_visitor.h"
34b145e903694fa932ab1e0d955e889555193ab604Eric Anholt#include "ir_optimization.h"
35b145e903694fa932ab1e0d955e889555193ab604Eric Anholt#include "glsl_types.h"
366f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick#include "main/macros.h"
37b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
38b145e903694fa932ab1e0d955e889555193ab604Eric Anholt/**
39b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * Visitor class for replacing expressions with ir_constant values.
40b145e903694fa932ab1e0d955e889555193ab604Eric Anholt */
41b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
42b145e903694fa932ab1e0d955e889555193ab604Eric Anholtclass ir_vec_index_to_swizzle_visitor : public ir_hierarchical_visitor {
43b145e903694fa932ab1e0d955e889555193ab604Eric Anholtpublic:
44b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir_vec_index_to_swizzle_visitor()
45b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   {
46b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      progress = false;
47b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   }
48b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
49b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir_rvalue *convert_vec_index_to_swizzle(ir_rvalue *val);
50b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
51b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   virtual ir_visitor_status visit_enter(ir_expression *);
52b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   virtual ir_visitor_status visit_enter(ir_swizzle *);
53b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   virtual ir_visitor_status visit_enter(ir_assignment *);
54b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   virtual ir_visitor_status visit_enter(ir_return *);
55b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   virtual ir_visitor_status visit_enter(ir_call *);
56b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   virtual ir_visitor_status visit_enter(ir_if *);
57b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
58b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   bool progress;
59b145e903694fa932ab1e0d955e889555193ab604Eric Anholt};
60b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
61b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_rvalue *
62b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::convert_vec_index_to_swizzle(ir_rvalue *ir)
63b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
64b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir_dereference_array *deref = ir->as_dereference_array();
65b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir_constant *ir_constant;
66b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
67b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   if (!deref)
68b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      return ir;
69b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
70b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   if (deref->array->type->is_matrix() || deref->array->type->is_array())
71b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      return ir;
72b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
73b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   assert(deref->array_index->type->base_type == GLSL_TYPE_INT);
74b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir_constant = deref->array_index->constant_expression_value();
75b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   if (!ir_constant)
76b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      return ir;
77b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
78d3073f58c17d8675a2ecdd5dfa83e5520c78e1a8Kenneth Graunke   void *ctx = ralloc_parent(ir);
79b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   this->progress = true;
806f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick
816f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick   /* Page 40 of the GLSL 1.20 spec says:
826f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick    *
836f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick    *     "When indexing with non-constant expressions, behavior is undefined
846f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick    *     if the index is negative, or greater than or equal to the size of
856f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick    *     the vector."
866f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick    *
876f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick    * The quoted spec text mentions non-constant expressions, but this code
886f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick    * operates on constants.  These constants are the result of non-constant
896f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick    * expressions that have been optimized to constants.  The common case here
906f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick    * is a loop counter from an unrolled loop that is used to index a vector.
916f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick    *
926f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick    * The ir_swizzle constructor gets angry if the index is negative or too
936f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick    * large.  For simplicity sake, just clamp the index to [0, size-1].
946f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick    */
956f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick   const int i = MIN2(MAX2(ir_constant->value.i[0], 0),
966f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick		      (deref->array->type->vector_elements - 1));
976f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick
986f5c73797087c6e7842665f84e41caedea59bb65Ian Romanick   return new(ctx) ir_swizzle(deref->array, i, 0, 0, 0, 1);
99b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
100b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
101b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_visitor_status
102b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::visit_enter(ir_expression *ir)
103b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
104b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   unsigned int i;
105b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
106b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   for (i = 0; i < ir->get_num_operands(); i++) {
107b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      ir->operands[i] = convert_vec_index_to_swizzle(ir->operands[i]);
108b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   }
109b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
110b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   return visit_continue;
111b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
112b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
113b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_visitor_status
114b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::visit_enter(ir_swizzle *ir)
115b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
116b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   /* Can't be hit from normal GLSL, since you can't swizzle a scalar (which
117b145e903694fa932ab1e0d955e889555193ab604Eric Anholt    * the result of indexing a vector is.  But maybe at some point we'll end up
118b145e903694fa932ab1e0d955e889555193ab604Eric Anholt    * using swizzling of scalars for vector construction.
119b145e903694fa932ab1e0d955e889555193ab604Eric Anholt    */
120b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir->val = convert_vec_index_to_swizzle(ir->val);
121b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
122b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   return visit_continue;
123b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
124b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
125b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_visitor_status
126b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::visit_enter(ir_assignment *ir)
127b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
1285a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick   ir->set_lhs(convert_vec_index_to_swizzle(ir->lhs));
129b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir->rhs = convert_vec_index_to_swizzle(ir->rhs);
130b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
131b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   return visit_continue;
132b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
133b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
134b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_visitor_status
135b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::visit_enter(ir_call *ir)
136b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
137b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   foreach_iter(exec_list_iterator, iter, *ir) {
138b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      ir_rvalue *param = (ir_rvalue *)iter.get();
139b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      ir_rvalue *new_param = convert_vec_index_to_swizzle(param);
140b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
141b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      if (new_param != param) {
142c7a18da69022d3f9b05c21ff2473e8ea390f77f1Kenneth Graunke	 param->replace_with(new_param);
143b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      }
144b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   }
145b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
146b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   return visit_continue;
147b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
148b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
149b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_visitor_status
150b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::visit_enter(ir_return *ir)
151b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
152b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   if (ir->value) {
153b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      ir->value = convert_vec_index_to_swizzle(ir->value);
154b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   }
155b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
156b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   return visit_continue;
157b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
158b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
159b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_visitor_status
160b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::visit_enter(ir_if *ir)
161b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
162b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir->condition = convert_vec_index_to_swizzle(ir->condition);
163b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
164b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   return visit_continue;
165b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
166b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
167b145e903694fa932ab1e0d955e889555193ab604Eric Anholtbool
168b145e903694fa932ab1e0d955e889555193ab604Eric Anholtdo_vec_index_to_swizzle(exec_list *instructions)
169b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
170b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir_vec_index_to_swizzle_visitor v;
171b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
172b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   v.run(instructions);
173b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
174022f79e49648d465d2db0240554f58ac42754584Eric Anholt   return v.progress;
175b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
176