Eric Anholt | 65122e9 | 2010-05-12 12:10:41 -0700 | [diff] [blame^] | 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 | #include "linux_list.h" |
| 43 | |
| 44 | struct assignment_entry { |
| 45 | struct list link; |
| 46 | int assignment_count; |
| 47 | ir_variable *var; |
| 48 | ir_constant *constval; |
| 49 | }; |
| 50 | |
| 51 | class ir_constant_variable_visitor : public ir_hierarchical_visitor { |
| 52 | public: |
| 53 | ir_constant_variable_visitor() |
| 54 | { |
| 55 | list_init(&list); |
| 56 | } |
| 57 | |
| 58 | virtual ir_visitor_status visit_enter(ir_assignment *); |
| 59 | |
| 60 | struct list list; |
| 61 | }; |
| 62 | |
| 63 | static struct assignment_entry * |
| 64 | get_assignment_entry(ir_variable *var, struct list *list) |
| 65 | { |
| 66 | struct assignment_entry *entry; |
| 67 | |
| 68 | list_foreach_entry(entry, struct assignment_entry, list, link) { |
| 69 | if (entry->var == var) |
| 70 | return entry; |
| 71 | } |
| 72 | |
| 73 | entry = (struct assignment_entry *)calloc(1, sizeof(*entry)); |
| 74 | entry->var = var; |
| 75 | list_add(&entry->link, list); |
| 76 | return entry; |
| 77 | } |
| 78 | |
| 79 | ir_visitor_status |
| 80 | ir_constant_variable_visitor::visit_enter(ir_assignment *ir) |
| 81 | { |
| 82 | ir_constant *constval; |
| 83 | struct assignment_entry *entry; |
| 84 | |
| 85 | entry = get_assignment_entry(ir->lhs->variable_referenced(), &this->list); |
| 86 | assert(entry); |
| 87 | entry->assignment_count++; |
| 88 | |
| 89 | /* If it's already constant, don't do the work. */ |
| 90 | if (entry->var->constant_value) |
| 91 | return visit_continue; |
| 92 | |
| 93 | /* OK, now find if we actually have all the right conditions for |
| 94 | * this to be a constant value assigned to the var. |
| 95 | */ |
| 96 | if (ir->condition) { |
| 97 | constval = ir->condition->constant_expression_value(); |
| 98 | if (!constval || !constval->value.b[0]) |
| 99 | return visit_continue; |
| 100 | } |
| 101 | |
| 102 | ir_variable *var = ir->lhs->whole_variable_referenced(); |
| 103 | if (!var) |
| 104 | return visit_continue; |
| 105 | |
| 106 | constval = ir->rhs->constant_expression_value(); |
| 107 | if (!constval) |
| 108 | return visit_continue; |
| 109 | |
| 110 | /* Mark this entry as having a constant assignment (if the |
| 111 | * assignment count doesn't go >1). do_constant_variable will fix |
| 112 | * up the variable with the constant value later. |
| 113 | */ |
| 114 | entry->constval = constval; |
| 115 | |
| 116 | return visit_continue; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Does a copy propagation pass on the code present in the instruction stream. |
| 121 | */ |
| 122 | bool |
| 123 | do_constant_variable(exec_list *instructions) |
| 124 | { |
| 125 | bool progress = false; |
| 126 | ir_constant_variable_visitor v; |
| 127 | |
| 128 | v.run(instructions); |
| 129 | |
| 130 | while (!list_is_empty(&v.list)) { |
| 131 | |
| 132 | struct assignment_entry *entry; |
| 133 | entry = list_entry(v.list.next, struct assignment_entry, link); |
| 134 | |
| 135 | if (entry->assignment_count == 1 && entry->constval) { |
| 136 | entry->var->constant_value = entry->constval; |
| 137 | progress = true; |
| 138 | } |
| 139 | list_del(&entry->link); |
| 140 | free(entry); |
| 141 | } |
| 142 | |
| 143 | return progress; |
| 144 | } |
| 145 | |
| 146 | bool |
| 147 | do_constant_variable_unlinked(exec_list *instructions) |
| 148 | { |
| 149 | bool progress = false; |
| 150 | |
| 151 | foreach_iter(exec_list_iterator, iter, *instructions) { |
| 152 | ir_instruction *ir = (ir_instruction *)iter.get(); |
| 153 | ir_function *f = ir->as_function(); |
| 154 | if (f) { |
| 155 | foreach_iter(exec_list_iterator, sigiter, *f) { |
| 156 | ir_function_signature *sig = |
| 157 | (ir_function_signature *) sigiter.get(); |
| 158 | if (do_constant_variable(&sig->body)) |
| 159 | progress = true; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return progress; |
| 165 | } |