1b8e80941Smrg/* 2b8e80941Smrg * Copyright © 2010 Intel Corporation 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the 9b8e80941Smrg * Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the next 12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the 13b8e80941Smrg * Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21b8e80941Smrg * DEALINGS IN THE SOFTWARE. 22b8e80941Smrg */ 23b8e80941Smrg 24b8e80941Smrg/** 25b8e80941Smrg * \file ir_variable_refcount.cpp 26b8e80941Smrg * 27b8e80941Smrg * Provides a visitor which produces a list of variables referenced, 28b8e80941Smrg * how many times they were referenced and assigned, and whether they 29b8e80941Smrg * were defined in the scope. 30b8e80941Smrg */ 31b8e80941Smrg 32b8e80941Smrg#include "ir.h" 33b8e80941Smrg#include "ir_visitor.h" 34b8e80941Smrg#include "ir_variable_refcount.h" 35b8e80941Smrg#include "compiler/glsl_types.h" 36b8e80941Smrg#include "util/hash_table.h" 37b8e80941Smrg 38b8e80941Smrgir_variable_refcount_visitor::ir_variable_refcount_visitor() 39b8e80941Smrg{ 40b8e80941Smrg this->mem_ctx = ralloc_context(NULL); 41b8e80941Smrg this->ht = _mesa_pointer_hash_table_create(NULL); 42b8e80941Smrg} 43b8e80941Smrg 44b8e80941Smrgstatic void 45b8e80941Smrgfree_entry(struct hash_entry *entry) 46b8e80941Smrg{ 47b8e80941Smrg ir_variable_refcount_entry *ivre = (ir_variable_refcount_entry *) entry->data; 48b8e80941Smrg 49b8e80941Smrg /* Free assignment list */ 50b8e80941Smrg exec_node *n; 51b8e80941Smrg while ((n = ivre->assign_list.pop_head()) != NULL) { 52b8e80941Smrg struct assignment_entry *assignment_entry = 53b8e80941Smrg exec_node_data(struct assignment_entry, n, link); 54b8e80941Smrg free(assignment_entry); 55b8e80941Smrg } 56b8e80941Smrg 57b8e80941Smrg delete ivre; 58b8e80941Smrg} 59b8e80941Smrg 60b8e80941Smrgir_variable_refcount_visitor::~ir_variable_refcount_visitor() 61b8e80941Smrg{ 62b8e80941Smrg ralloc_free(this->mem_ctx); 63b8e80941Smrg _mesa_hash_table_destroy(this->ht, free_entry); 64b8e80941Smrg} 65b8e80941Smrg 66b8e80941Smrg// constructor 67b8e80941Smrgir_variable_refcount_entry::ir_variable_refcount_entry(ir_variable *var) 68b8e80941Smrg{ 69b8e80941Smrg this->var = var; 70b8e80941Smrg assigned_count = 0; 71b8e80941Smrg declaration = false; 72b8e80941Smrg referenced_count = 0; 73b8e80941Smrg} 74b8e80941Smrg 75b8e80941Smrg 76b8e80941Smrgir_variable_refcount_entry * 77b8e80941Smrgir_variable_refcount_visitor::get_variable_entry(ir_variable *var) 78b8e80941Smrg{ 79b8e80941Smrg assert(var); 80b8e80941Smrg 81b8e80941Smrg struct hash_entry *e = _mesa_hash_table_search(this->ht, var); 82b8e80941Smrg if (e) 83b8e80941Smrg return (ir_variable_refcount_entry *)e->data; 84b8e80941Smrg 85b8e80941Smrg ir_variable_refcount_entry *entry = new ir_variable_refcount_entry(var); 86b8e80941Smrg assert(entry->referenced_count == 0); 87b8e80941Smrg _mesa_hash_table_insert(this->ht, var, entry); 88b8e80941Smrg 89b8e80941Smrg return entry; 90b8e80941Smrg} 91b8e80941Smrg 92b8e80941Smrg 93b8e80941Smrgir_visitor_status 94b8e80941Smrgir_variable_refcount_visitor::visit(ir_variable *ir) 95b8e80941Smrg{ 96b8e80941Smrg ir_variable_refcount_entry *entry = this->get_variable_entry(ir); 97b8e80941Smrg if (entry) 98b8e80941Smrg entry->declaration = true; 99b8e80941Smrg 100b8e80941Smrg return visit_continue; 101b8e80941Smrg} 102b8e80941Smrg 103b8e80941Smrg 104b8e80941Smrgir_visitor_status 105b8e80941Smrgir_variable_refcount_visitor::visit(ir_dereference_variable *ir) 106b8e80941Smrg{ 107b8e80941Smrg ir_variable *const var = ir->variable_referenced(); 108b8e80941Smrg ir_variable_refcount_entry *entry = this->get_variable_entry(var); 109b8e80941Smrg 110b8e80941Smrg if (entry) 111b8e80941Smrg entry->referenced_count++; 112b8e80941Smrg 113b8e80941Smrg return visit_continue; 114b8e80941Smrg} 115b8e80941Smrg 116b8e80941Smrg 117b8e80941Smrgir_visitor_status 118b8e80941Smrgir_variable_refcount_visitor::visit_enter(ir_function_signature *ir) 119b8e80941Smrg{ 120b8e80941Smrg /* We don't want to descend into the function parameters and 121b8e80941Smrg * dead-code eliminate them, so just accept the body here. 122b8e80941Smrg */ 123b8e80941Smrg visit_list_elements(this, &ir->body); 124b8e80941Smrg return visit_continue_with_parent; 125b8e80941Smrg} 126b8e80941Smrg 127b8e80941Smrg 128b8e80941Smrgir_visitor_status 129b8e80941Smrgir_variable_refcount_visitor::visit_leave(ir_assignment *ir) 130b8e80941Smrg{ 131b8e80941Smrg ir_variable_refcount_entry *entry; 132b8e80941Smrg entry = this->get_variable_entry(ir->lhs->variable_referenced()); 133b8e80941Smrg if (entry) { 134b8e80941Smrg entry->assigned_count++; 135b8e80941Smrg 136b8e80941Smrg /* Build a list for dead code optimisation. Don't add assignment if it 137b8e80941Smrg * was declared out of scope (outside the instruction stream). Also don't 138b8e80941Smrg * bother adding any more to the list if there are more references than 139b8e80941Smrg * assignments as this means the variable is used and won't be optimised 140b8e80941Smrg * out. 141b8e80941Smrg */ 142b8e80941Smrg assert(entry->referenced_count >= entry->assigned_count); 143b8e80941Smrg if (entry->referenced_count == entry->assigned_count) { 144b8e80941Smrg struct assignment_entry *assignment_entry = 145b8e80941Smrg (struct assignment_entry *)calloc(1, sizeof(*assignment_entry)); 146b8e80941Smrg assignment_entry->assign = ir; 147b8e80941Smrg entry->assign_list.push_head(&assignment_entry->link); 148b8e80941Smrg } 149b8e80941Smrg } 150b8e80941Smrg 151b8e80941Smrg return visit_continue; 152b8e80941Smrg} 153