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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include "ir.h"
25#include "ir_visitor.h"
26#include "ir_optimization.h"
27
28/**
29 * Pre-linking, optimize unused built-in variables
30 *
31 * Uniforms, constants, system values, inputs (vertex shader only), and
32 * outputs (fragment shader only) that are not used can be removed.
33 */
34void
35optimize_dead_builtin_variables(exec_list *instructions,
36                                enum ir_variable_mode other)
37{
38   foreach_in_list_safe(ir_variable, var, instructions) {
39      if (var->ir_type != ir_type_variable || var->data.used)
40         continue;
41
42      if (var->data.mode != ir_var_uniform
43          && var->data.mode != ir_var_auto
44          && var->data.mode != ir_var_system_value
45          && var->data.mode != other)
46         continue;
47
48      /* So that linker rules can later be enforced, we cannot elimate
49       * variables that were redeclared in the shader code.
50       */
51      if ((var->data.mode == other || var->data.mode == ir_var_system_value)
52          && var->data.how_declared != ir_var_declared_implicitly)
53         continue;
54
55      if (!is_gl_identifier(var->name))
56         continue;
57
58      /* gl_ModelViewProjectionMatrix and gl_Vertex are special because they
59       * are used by ftransform.  No other built-in variable is used by a
60       * built-in function.  The forward declarations of these variables in
61       * the built-in function shader does not have the "state slot"
62       * information, so removing these variables from the user shader will
63       * cause problems later.
64       *
65       * Matrix uniforms with "Transpose" are not eliminated because there's
66       * an optimization pass that can turn references to the regular matrix
67       * into references to the transpose matrix.  Eliminating the transpose
68       * matrix would cause that pass to generate references to undeclareds
69       * variables (thank you, ir_validate).
70       *
71       * It doesn't seem worth the effort to track when the transpose could be
72       * eliminated (i.e., when the non-transpose was eliminated).
73       */
74      if (strcmp(var->name, "gl_ModelViewProjectionMatrix") == 0
75          || strcmp(var->name, "gl_Vertex") == 0
76          || strstr(var->name, "Transpose") != NULL)
77         continue;
78
79      var->remove();
80   }
81}
82