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