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 lower_vector.cpp
26  * IR lowering pass to remove some types of ir_quadop_vector
27  *
28  * \author Ian Romanick <ian.d.romanick@intel.com>
29  */
30 
31 #include "ir.h"
32 #include "ir_rvalue_visitor.h"
33 
34 namespace {
35 
36 class lower_vector_visitor : public ir_rvalue_visitor {
37 public:
lower_vector_visitor()38    lower_vector_visitor() : progress(false)
39    {
40       /* empty */
41    }
42 
43    void handle_rvalue(ir_rvalue **rvalue);
44 
45    bool progress;
46 };
47 
48 } /* anonymous namespace */
49 
50 void
handle_rvalue(ir_rvalue ** rvalue)51 lower_vector_visitor::handle_rvalue(ir_rvalue **rvalue)
52 {
53    if (!*rvalue)
54       return;
55 
56    ir_expression *expr = (*rvalue)->as_expression();
57    if ((expr == NULL) || (expr->operation != ir_quadop_vector))
58       return;
59 
60    /* FINISHME: Is this the right thing to use for the ralloc context?
61     */
62    void *const mem_ctx = expr;
63 
64    assert(expr->type->vector_elements == expr->num_operands);
65 
66    /* Generate a temporary with the same type as the ir_quadop_operation.
67     */
68    ir_variable *const temp =
69       new(mem_ctx) ir_variable(expr->type, "vecop_tmp", ir_var_temporary);
70 
71    this->base_ir->insert_before(temp);
72 
73    /* Counter of the number of components collected so far.
74     */
75    unsigned assigned;
76 
77    /* Write-mask in the destination that receives counted by 'assigned'.
78     */
79    unsigned write_mask;
80 
81 
82    /* Generate upto four assignments to that variable.  Try to group component
83     * assignments together:
84     *
85     * - All constant components can be assigned at once.
86     * - All assigments of components from a single variable with the same
87     *   unary operator can be assigned at once.
88     */
89    ir_constant_data d = { { 0 } };
90 
91    assigned = 0;
92    write_mask = 0;
93    for (unsigned i = 0; i < expr->type->vector_elements; i++) {
94       const ir_constant *const c = expr->operands[i]->as_constant();
95 
96       if (c == NULL)
97 	 continue;
98 
99       switch (expr->type->base_type) {
100       case GLSL_TYPE_UINT:  d.u[assigned] = c->value.u[0]; break;
101       case GLSL_TYPE_INT:   d.i[assigned] = c->value.i[0]; break;
102       case GLSL_TYPE_FLOAT: d.f[assigned] = c->value.f[0]; break;
103       case GLSL_TYPE_BOOL:  d.b[assigned] = c->value.b[0]; break;
104       default:              assert(!"Should not get here."); break;
105       }
106 
107       write_mask |= (1U << i);
108       assigned++;
109    }
110 
111    assert((write_mask == 0) == (assigned == 0));
112 
113    /* If there were constant values, generate an assignment.
114     */
115    if (assigned > 0) {
116       ir_constant *const c =
117 	 new(mem_ctx) ir_constant(glsl_type::get_instance(expr->type->base_type,
118 							  assigned, 1),
119 				  &d);
120       ir_dereference *const lhs = new(mem_ctx) ir_dereference_variable(temp);
121       ir_assignment *const assign =
122 	 new(mem_ctx) ir_assignment(lhs, c, write_mask);
123 
124       this->base_ir->insert_before(assign);
125    }
126 
127    /* FINISHME: This should try to coalesce assignments.
128     */
129    for (unsigned i = 0; i < expr->type->vector_elements; i++) {
130       if (expr->operands[i]->ir_type == ir_type_constant)
131 	 continue;
132 
133       ir_dereference *const lhs = new(mem_ctx) ir_dereference_variable(temp);
134       ir_assignment *const assign =
135 	 new(mem_ctx) ir_assignment(lhs, expr->operands[i], 1U << i);
136 
137       this->base_ir->insert_before(assign);
138       assigned++;
139    }
140 
141    assert(assigned == expr->type->vector_elements);
142 
143    *rvalue = new(mem_ctx) ir_dereference_variable(temp);
144    this->progress = true;
145 }
146 
147 bool
lower_quadop_vector(exec_list * instructions)148 lower_quadop_vector(exec_list *instructions)
149 {
150    lower_vector_visitor v;
151 
152    visit_list_elements(&v, instructions);
153 
154    return v.progress;
155 }
156