ir_constant: Support constant structures in clone
[mesa.git] / ir_constant_variable.cpp
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 ir_constant_variable.cpp
26 *
27 * Marks variables assigned a single constant value over the course
28 * of the program as constant.
29 *
30 * The goal here is to trigger further constant folding and then dead
31 * code elimination. This is common with vector/matrix constructors
32 * and calls to builtin functions.
33 */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include "ir.h"
38 #include "ir_print_visitor.h"
39 #include "ir_visitor.h"
40 #include "ir_optimization.h"
41 #include "glsl_types.h"
42
43 struct assignment_entry {
44 exec_node link;
45 int assignment_count;
46 ir_variable *var;
47 ir_constant *constval;
48 };
49
50 class ir_constant_variable_visitor : public ir_hierarchical_visitor {
51 public:
52 virtual ir_visitor_status visit_enter(ir_assignment *);
53
54 exec_list list;
55 };
56
57 static struct assignment_entry *
58 get_assignment_entry(ir_variable *var, exec_list *list)
59 {
60 struct assignment_entry *entry;
61
62 foreach_list_typed(struct assignment_entry, entry, link, list) {
63 if (entry->var == var)
64 return entry;
65 }
66
67 entry = (struct assignment_entry *)calloc(1, sizeof(*entry));
68 entry->var = var;
69 list->push_head(&entry->link);
70 return entry;
71 }
72
73 ir_visitor_status
74 ir_constant_variable_visitor::visit_enter(ir_assignment *ir)
75 {
76 ir_constant *constval;
77 struct assignment_entry *entry;
78
79 entry = get_assignment_entry(ir->lhs->variable_referenced(), &this->list);
80 assert(entry);
81 entry->assignment_count++;
82
83 /* If it's already constant, don't do the work. */
84 if (entry->var->constant_value)
85 return visit_continue;
86
87 /* OK, now find if we actually have all the right conditions for
88 * this to be a constant value assigned to the var.
89 */
90 if (ir->condition) {
91 constval = ir->condition->constant_expression_value();
92 if (!constval || !constval->value.b[0])
93 return visit_continue;
94 }
95
96 ir_variable *var = ir->lhs->whole_variable_referenced();
97 if (!var)
98 return visit_continue;
99
100 constval = ir->rhs->constant_expression_value();
101 if (!constval)
102 return visit_continue;
103
104 /* Mark this entry as having a constant assignment (if the
105 * assignment count doesn't go >1). do_constant_variable will fix
106 * up the variable with the constant value later.
107 */
108 entry->constval = constval;
109
110 return visit_continue;
111 }
112
113 /**
114 * Does a copy propagation pass on the code present in the instruction stream.
115 */
116 bool
117 do_constant_variable(exec_list *instructions)
118 {
119 bool progress = false;
120 ir_constant_variable_visitor v;
121
122 v.run(instructions);
123
124 while (!v.list.is_empty()) {
125
126 struct assignment_entry *entry;
127 entry = exec_node_data(struct assignment_entry, v.list.head, link);
128
129 if (entry->assignment_count == 1 && entry->constval) {
130 entry->var->constant_value = entry->constval;
131 progress = true;
132 }
133 entry->link.remove();
134 free(entry);
135 }
136
137 return progress;
138 }
139
140 bool
141 do_constant_variable_unlinked(exec_list *instructions)
142 {
143 bool progress = false;
144
145 foreach_iter(exec_list_iterator, iter, *instructions) {
146 ir_instruction *ir = (ir_instruction *)iter.get();
147 ir_function *f = ir->as_function();
148 if (f) {
149 foreach_iter(exec_list_iterator, sigiter, *f) {
150 ir_function_signature *sig =
151 (ir_function_signature *) sigiter.get();
152 if (do_constant_variable(&sig->body))
153 progress = true;
154 }
155 }
156 }
157
158 return progress;
159 }