glsl: move to compiler/
[mesa.git] / src / mesa / drivers / dri / i965 / test_vec4_copy_propagation.cpp
1 /*
2 * Copyright © 2014 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 DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <gtest/gtest.h>
25 #include "brw_vec4.h"
26 #include "brw_vs.h"
27 #include "program/program.h"
28
29 using namespace brw;
30
31 int ret = 0;
32
33 class copy_propagation_test : public ::testing::Test {
34 virtual void SetUp();
35
36 public:
37 struct brw_compiler *compiler;
38 struct brw_device_info *devinfo;
39 struct gl_context *ctx;
40 struct gl_shader_program *shader_prog;
41 struct brw_vertex_program *vp;
42 vec4_visitor *v;
43 };
44
45 class copy_propagation_vec4_visitor : public vec4_visitor
46 {
47 public:
48 copy_propagation_vec4_visitor(struct brw_compiler *compiler,
49 nir_shader *shader)
50 : vec4_visitor(compiler, NULL, NULL, NULL, shader, NULL,
51 false /* no_spills */, -1)
52 {
53 }
54
55 protected:
56 virtual dst_reg *make_reg_for_system_value(int location,
57 const glsl_type *type)
58 {
59 unreachable("Not reached");
60 }
61
62 virtual void setup_payload()
63 {
64 unreachable("Not reached");
65 }
66
67 virtual void emit_prolog()
68 {
69 unreachable("Not reached");
70 }
71
72 virtual void emit_thread_end()
73 {
74 unreachable("Not reached");
75 }
76
77 virtual void emit_urb_write_header(int mrf)
78 {
79 unreachable("Not reached");
80 }
81
82 virtual vec4_instruction *emit_urb_write_opcode(bool complete)
83 {
84 unreachable("Not reached");
85 }
86 };
87
88
89 void copy_propagation_test::SetUp()
90 {
91 ctx = (struct gl_context *)calloc(1, sizeof(*ctx));
92 compiler = (struct brw_compiler *)calloc(1, sizeof(*compiler));
93 devinfo = (struct brw_device_info *)calloc(1, sizeof(*devinfo));
94 compiler->devinfo = devinfo;
95
96 vp = ralloc(NULL, struct brw_vertex_program);
97
98 nir_shader *shader = nir_shader_create(NULL, MESA_SHADER_VERTEX, NULL);
99
100 v = new copy_propagation_vec4_visitor(compiler, shader);
101
102 _mesa_init_gl_program(&vp->program.Base, GL_VERTEX_SHADER, 0);
103
104 devinfo->gen = 4;
105 }
106
107 static void
108 copy_propagation(vec4_visitor *v)
109 {
110 bool print = false;
111
112 if (print) {
113 fprintf(stderr, "instructions before:\n");
114 v->dump_instructions();
115 }
116
117 v->calculate_cfg();
118 v->opt_copy_propagation();
119
120 if (print) {
121 fprintf(stderr, "instructions after:\n");
122 v->dump_instructions();
123 }
124 }
125
126 TEST_F(copy_propagation_test, test_swizzle_swizzle)
127 {
128 dst_reg a = dst_reg(v, glsl_type::vec4_type);
129 dst_reg b = dst_reg(v, glsl_type::vec4_type);
130 dst_reg c = dst_reg(v, glsl_type::vec4_type);
131
132 v->emit(v->ADD(a, src_reg(a), src_reg(a)));
133
134 v->emit(v->MOV(b, swizzle(src_reg(a), BRW_SWIZZLE4(SWIZZLE_Y,
135 SWIZZLE_Z,
136 SWIZZLE_W,
137 SWIZZLE_X))));
138
139 vec4_instruction *test_mov =
140 v->MOV(c, swizzle(src_reg(b), BRW_SWIZZLE4(SWIZZLE_Y,
141 SWIZZLE_Z,
142 SWIZZLE_W,
143 SWIZZLE_X)));
144 v->emit(test_mov);
145
146 copy_propagation(v);
147
148 EXPECT_EQ(test_mov->src[0].nr, a.nr);
149 EXPECT_EQ(test_mov->src[0].swizzle, BRW_SWIZZLE4(SWIZZLE_Z,
150 SWIZZLE_W,
151 SWIZZLE_X,
152 SWIZZLE_Y));
153 }
154
155 TEST_F(copy_propagation_test, test_swizzle_writemask)
156 {
157 dst_reg a = dst_reg(v, glsl_type::vec4_type);
158 dst_reg b = dst_reg(v, glsl_type::vec4_type);
159 dst_reg c = dst_reg(v, glsl_type::vec4_type);
160
161 v->emit(v->MOV(b, swizzle(src_reg(a), BRW_SWIZZLE4(SWIZZLE_X,
162 SWIZZLE_Y,
163 SWIZZLE_X,
164 SWIZZLE_Z))));
165
166 v->emit(v->MOV(writemask(a, WRITEMASK_XYZ), brw_imm_f(1.0f)));
167
168 vec4_instruction *test_mov =
169 v->MOV(c, swizzle(src_reg(b), BRW_SWIZZLE4(SWIZZLE_W,
170 SWIZZLE_W,
171 SWIZZLE_W,
172 SWIZZLE_W)));
173 v->emit(test_mov);
174
175 copy_propagation(v);
176
177 /* should not copy propagate */
178 EXPECT_EQ(test_mov->src[0].nr, b.nr);
179 EXPECT_EQ(test_mov->src[0].swizzle, BRW_SWIZZLE4(SWIZZLE_W,
180 SWIZZLE_W,
181 SWIZZLE_W,
182 SWIZZLE_W));
183 }