blob: ef5e1e418e54071010e2bd3dd917796b0437ef80 [file] [log] [blame]
Eric Anholt65122e92010-05-12 12:10:41 -07001/*
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
Eric Anholt65122e92010-05-12 12:10:41 -070035#include "ir.h"
Eric Anholt65122e92010-05-12 12:10:41 -070036#include "ir_visitor.h"
37#include "ir_optimization.h"
38#include "glsl_types.h"
Eric Anholt65122e92010-05-12 12:10:41 -070039
40struct assignment_entry {
Kenneth Graunke332920a2010-06-09 17:05:14 -070041 exec_node link;
Eric Anholt65122e92010-05-12 12:10:41 -070042 int assignment_count;
43 ir_variable *var;
44 ir_constant *constval;
45};
46
47class ir_constant_variable_visitor : public ir_hierarchical_visitor {
48public:
Eric Anholt65122e92010-05-12 12:10:41 -070049 virtual ir_visitor_status visit_enter(ir_assignment *);
50
Kenneth Graunke332920a2010-06-09 17:05:14 -070051 exec_list list;
Eric Anholt65122e92010-05-12 12:10:41 -070052};
53
54static struct assignment_entry *
Kenneth Graunke332920a2010-06-09 17:05:14 -070055get_assignment_entry(ir_variable *var, exec_list *list)
Eric Anholt65122e92010-05-12 12:10:41 -070056{
57 struct assignment_entry *entry;
58
Kenneth Graunke332920a2010-06-09 17:05:14 -070059 foreach_list_typed(struct assignment_entry, entry, link, list) {
Eric Anholt65122e92010-05-12 12:10:41 -070060 if (entry->var == var)
61 return entry;
62 }
63
64 entry = (struct assignment_entry *)calloc(1, sizeof(*entry));
65 entry->var = var;
Kenneth Graunke332920a2010-06-09 17:05:14 -070066 list->push_head(&entry->link);
Eric Anholt65122e92010-05-12 12:10:41 -070067 return entry;
68}
69
70ir_visitor_status
71ir_constant_variable_visitor::visit_enter(ir_assignment *ir)
72{
73 ir_constant *constval;
74 struct assignment_entry *entry;
75
76 entry = get_assignment_entry(ir->lhs->variable_referenced(), &this->list);
77 assert(entry);
78 entry->assignment_count++;
79
80 /* If it's already constant, don't do the work. */
81 if (entry->var->constant_value)
82 return visit_continue;
83
84 /* OK, now find if we actually have all the right conditions for
85 * this to be a constant value assigned to the var.
86 */
87 if (ir->condition) {
88 constval = ir->condition->constant_expression_value();
89 if (!constval || !constval->value.b[0])
90 return visit_continue;
91 }
92
93 ir_variable *var = ir->lhs->whole_variable_referenced();
94 if (!var)
95 return visit_continue;
96
97 constval = ir->rhs->constant_expression_value();
98 if (!constval)
99 return visit_continue;
100
101 /* Mark this entry as having a constant assignment (if the
102 * assignment count doesn't go >1). do_constant_variable will fix
103 * up the variable with the constant value later.
104 */
105 entry->constval = constval;
106
107 return visit_continue;
108}
109
110/**
111 * Does a copy propagation pass on the code present in the instruction stream.
112 */
113bool
114do_constant_variable(exec_list *instructions)
115{
116 bool progress = false;
117 ir_constant_variable_visitor v;
118
119 v.run(instructions);
120
Kenneth Graunke332920a2010-06-09 17:05:14 -0700121 while (!v.list.is_empty()) {
Eric Anholt65122e92010-05-12 12:10:41 -0700122
123 struct assignment_entry *entry;
Kenneth Graunke332920a2010-06-09 17:05:14 -0700124 entry = exec_node_data(struct assignment_entry, v.list.head, link);
Eric Anholt65122e92010-05-12 12:10:41 -0700125
126 if (entry->assignment_count == 1 && entry->constval) {
127 entry->var->constant_value = entry->constval;
128 progress = true;
129 }
Kenneth Graunke332920a2010-06-09 17:05:14 -0700130 entry->link.remove();
Eric Anholt65122e92010-05-12 12:10:41 -0700131 free(entry);
132 }
133
134 return progress;
135}
136
137bool
138do_constant_variable_unlinked(exec_list *instructions)
139{
140 bool progress = false;
141
142 foreach_iter(exec_list_iterator, iter, *instructions) {
143 ir_instruction *ir = (ir_instruction *)iter.get();
144 ir_function *f = ir->as_function();
145 if (f) {
146 foreach_iter(exec_list_iterator, sigiter, *f) {
147 ir_function_signature *sig =
148 (ir_function_signature *) sigiter.get();
149 if (do_constant_variable(&sig->body))
150 progress = true;
151 }
152 }
153 }
154
155 return progress;
156}