lower_vec_index_to_swizzle.cpp revision d3073f58c17d8675a2ecdd5dfa83e5520c78e1a8
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"
36b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
37b145e903694fa932ab1e0d955e889555193ab604Eric Anholt/**
38b145e903694fa932ab1e0d955e889555193ab604Eric Anholt * Visitor class for replacing expressions with ir_constant values.
39b145e903694fa932ab1e0d955e889555193ab604Eric Anholt */
40b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
41b145e903694fa932ab1e0d955e889555193ab604Eric Anholtclass ir_vec_index_to_swizzle_visitor : public ir_hierarchical_visitor {
42b145e903694fa932ab1e0d955e889555193ab604Eric Anholtpublic:
43b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir_vec_index_to_swizzle_visitor()
44b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   {
45b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      progress = false;
46b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   }
47b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
48b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir_rvalue *convert_vec_index_to_swizzle(ir_rvalue *val);
49b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
50b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   virtual ir_visitor_status visit_enter(ir_expression *);
51b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   virtual ir_visitor_status visit_enter(ir_swizzle *);
52b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   virtual ir_visitor_status visit_enter(ir_assignment *);
53b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   virtual ir_visitor_status visit_enter(ir_return *);
54b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   virtual ir_visitor_status visit_enter(ir_call *);
55b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   virtual ir_visitor_status visit_enter(ir_if *);
56b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
57b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   bool progress;
58b145e903694fa932ab1e0d955e889555193ab604Eric Anholt};
59b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
60b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_rvalue *
61b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::convert_vec_index_to_swizzle(ir_rvalue *ir)
62b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
63b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir_dereference_array *deref = ir->as_dereference_array();
64b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir_constant *ir_constant;
65b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
66b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   if (!deref)
67b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      return ir;
68b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
69b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   if (deref->array->type->is_matrix() || deref->array->type->is_array())
70b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      return ir;
71b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
72b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   assert(deref->array_index->type->base_type == GLSL_TYPE_INT);
73b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir_constant = deref->array_index->constant_expression_value();
74b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   if (!ir_constant)
75b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      return ir;
76b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
77d3073f58c17d8675a2ecdd5dfa83e5520c78e1a8Kenneth Graunke   void *ctx = ralloc_parent(ir);
78b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   this->progress = true;
791660a2954797e056caba319c5d6c70b0d4be22feCarl Worth   return new(ctx) ir_swizzle(deref->array,
801660a2954797e056caba319c5d6c70b0d4be22feCarl Worth			      ir_constant->value.i[0], 0, 0, 0, 1);
81b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
82b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
83b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_visitor_status
84b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::visit_enter(ir_expression *ir)
85b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
86b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   unsigned int i;
87b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
88b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   for (i = 0; i < ir->get_num_operands(); i++) {
89b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      ir->operands[i] = convert_vec_index_to_swizzle(ir->operands[i]);
90b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   }
91b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
92b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   return visit_continue;
93b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
94b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
95b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_visitor_status
96b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::visit_enter(ir_swizzle *ir)
97b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
98b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   /* Can't be hit from normal GLSL, since you can't swizzle a scalar (which
99b145e903694fa932ab1e0d955e889555193ab604Eric Anholt    * the result of indexing a vector is.  But maybe at some point we'll end up
100b145e903694fa932ab1e0d955e889555193ab604Eric Anholt    * using swizzling of scalars for vector construction.
101b145e903694fa932ab1e0d955e889555193ab604Eric Anholt    */
102b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir->val = convert_vec_index_to_swizzle(ir->val);
103b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
104b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   return visit_continue;
105b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
106b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
107b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_visitor_status
108b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::visit_enter(ir_assignment *ir)
109b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
1105a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick   ir->set_lhs(convert_vec_index_to_swizzle(ir->lhs));
111b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir->rhs = convert_vec_index_to_swizzle(ir->rhs);
112b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
113b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   return visit_continue;
114b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
115b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
116b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_visitor_status
117b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::visit_enter(ir_call *ir)
118b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
119b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   foreach_iter(exec_list_iterator, iter, *ir) {
120b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      ir_rvalue *param = (ir_rvalue *)iter.get();
121b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      ir_rvalue *new_param = convert_vec_index_to_swizzle(param);
122b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
123b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      if (new_param != param) {
124c7a18da69022d3f9b05c21ff2473e8ea390f77f1Kenneth Graunke	 param->replace_with(new_param);
125b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      }
126b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   }
127b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
128b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   return visit_continue;
129b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
130b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
131b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_visitor_status
132b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::visit_enter(ir_return *ir)
133b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
134b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   if (ir->value) {
135b145e903694fa932ab1e0d955e889555193ab604Eric Anholt      ir->value = convert_vec_index_to_swizzle(ir->value);
136b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   }
137b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
138b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   return visit_continue;
139b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
140b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
141b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_visitor_status
142b145e903694fa932ab1e0d955e889555193ab604Eric Anholtir_vec_index_to_swizzle_visitor::visit_enter(ir_if *ir)
143b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
144b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir->condition = convert_vec_index_to_swizzle(ir->condition);
145b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
146b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   return visit_continue;
147b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
148b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
149b145e903694fa932ab1e0d955e889555193ab604Eric Anholtbool
150b145e903694fa932ab1e0d955e889555193ab604Eric Anholtdo_vec_index_to_swizzle(exec_list *instructions)
151b145e903694fa932ab1e0d955e889555193ab604Eric Anholt{
152b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   ir_vec_index_to_swizzle_visitor v;
153b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
154b145e903694fa932ab1e0d955e889555193ab604Eric Anholt   v.run(instructions);
155b145e903694fa932ab1e0d955e889555193ab604Eric Anholt
156022f79e49648d465d2db0240554f58ac42754584Eric Anholt   return v.progress;
157b145e903694fa932ab1e0d955e889555193ab604Eric Anholt}
158