6cd9a2d819a3728497abd49bc59bc30e5040a73e
[mesa.git] / src / compiler / glsl / lower_vector_derefs.cpp
1 /*
2 * Copyright © 2013 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 #include "ir.h"
24 #include "ir_builder.h"
25 #include "ir_rvalue_visitor.h"
26 #include "ir_optimization.h"
27 #include "main/mtypes.h"
28
29 using namespace ir_builder;
30
31 namespace {
32
33 class vector_deref_visitor : public ir_rvalue_enter_visitor {
34 public:
35 vector_deref_visitor()
36 : progress(false)
37 {
38 }
39
40 virtual ~vector_deref_visitor()
41 {
42 }
43
44 virtual void handle_rvalue(ir_rvalue **rv);
45 virtual ir_visitor_status visit_enter(ir_assignment *ir);
46
47 bool progress;
48 };
49
50 } /* anonymous namespace */
51
52 ir_visitor_status
53 vector_deref_visitor::visit_enter(ir_assignment *ir)
54 {
55 if (!ir->lhs || ir->lhs->ir_type != ir_type_dereference_array)
56 return ir_rvalue_enter_visitor::visit_enter(ir);
57
58 ir_dereference_array *const deref = (ir_dereference_array *) ir->lhs;
59 if (!deref->array->type->is_vector())
60 return ir_rvalue_enter_visitor::visit_enter(ir);
61
62 ir_rvalue *const new_lhs = deref->array;
63
64 void *mem_ctx = ralloc_parent(ir);
65 ir_constant *old_index_constant =
66 deref->array_index->constant_expression_value(mem_ctx);
67 if (!old_index_constant) {
68 ir->rhs = new(mem_ctx) ir_expression(ir_triop_vector_insert,
69 new_lhs->type,
70 new_lhs->clone(mem_ctx, NULL),
71 ir->rhs,
72 deref->array_index);
73 ir->write_mask = (1 << new_lhs->type->vector_elements) - 1;
74 ir->set_lhs(new_lhs);
75 } else if (new_lhs->ir_type != ir_type_swizzle) {
76 ir->set_lhs(new_lhs);
77 ir->write_mask = 1 << old_index_constant->get_uint_component(0);
78 } else {
79 /* If the "new" LHS is a swizzle, use the set_lhs helper to instead
80 * swizzle the RHS.
81 */
82 unsigned component[1] = { old_index_constant->get_uint_component(0) };
83 ir->set_lhs(new(mem_ctx) ir_swizzle(new_lhs, component, 1));
84 }
85
86 return ir_rvalue_enter_visitor::visit_enter(ir);
87 }
88
89 void
90 vector_deref_visitor::handle_rvalue(ir_rvalue **rv)
91 {
92 if (*rv == NULL || (*rv)->ir_type != ir_type_dereference_array)
93 return;
94
95 ir_dereference_array *const deref = (ir_dereference_array *) *rv;
96 if (!deref->array->type->is_vector())
97 return;
98
99 void *mem_ctx = ralloc_parent(deref);
100 *rv = new(mem_ctx) ir_expression(ir_binop_vector_extract,
101 deref->array,
102 deref->array_index);
103 }
104
105 bool
106 lower_vector_derefs(gl_linked_shader *shader)
107 {
108 vector_deref_visitor v;
109
110 visit_list_elements(&v, shader->ir);
111
112 return v.progress;
113 }