101e04c3fSmrg/* 201e04c3fSmrg * Copyright © 2010 Intel Corporation 301e04c3fSmrg * 401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 501e04c3fSmrg * copy of this software and associated documentation files (the "Software"), 601e04c3fSmrg * to deal in the Software without restriction, including without limitation 701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the 901e04c3fSmrg * Software is furnished to do so, subject to the following conditions: 1001e04c3fSmrg * 1101e04c3fSmrg * The above copyright notice and this permission notice (including the next 1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the 1301e04c3fSmrg * Software. 1401e04c3fSmrg * 1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 2101e04c3fSmrg * DEALINGS IN THE SOFTWARE. 2201e04c3fSmrg */ 2301e04c3fSmrg 2401e04c3fSmrg/** 2501e04c3fSmrg * \file ir_variable_refcount.cpp 2601e04c3fSmrg * 2701e04c3fSmrg * Provides a visitor which produces a list of variables referenced, 2801e04c3fSmrg * how many times they were referenced and assigned, and whether they 2901e04c3fSmrg * were defined in the scope. 3001e04c3fSmrg */ 3101e04c3fSmrg 3201e04c3fSmrg#include "ir.h" 3301e04c3fSmrg#include "ir_visitor.h" 3401e04c3fSmrg#include "ir_variable_refcount.h" 3501e04c3fSmrg#include "compiler/glsl_types.h" 3601e04c3fSmrg#include "util/hash_table.h" 3701e04c3fSmrg 3801e04c3fSmrgir_variable_refcount_visitor::ir_variable_refcount_visitor() 3901e04c3fSmrg{ 4001e04c3fSmrg this->mem_ctx = ralloc_context(NULL); 417e102996Smaya this->ht = _mesa_pointer_hash_table_create(NULL); 4201e04c3fSmrg} 4301e04c3fSmrg 4401e04c3fSmrgstatic void 4501e04c3fSmrgfree_entry(struct hash_entry *entry) 4601e04c3fSmrg{ 4701e04c3fSmrg ir_variable_refcount_entry *ivre = (ir_variable_refcount_entry *) entry->data; 4801e04c3fSmrg 4901e04c3fSmrg /* Free assignment list */ 5001e04c3fSmrg exec_node *n; 5101e04c3fSmrg while ((n = ivre->assign_list.pop_head()) != NULL) { 5201e04c3fSmrg struct assignment_entry *assignment_entry = 5301e04c3fSmrg exec_node_data(struct assignment_entry, n, link); 5401e04c3fSmrg free(assignment_entry); 5501e04c3fSmrg } 5601e04c3fSmrg 5701e04c3fSmrg delete ivre; 5801e04c3fSmrg} 5901e04c3fSmrg 6001e04c3fSmrgir_variable_refcount_visitor::~ir_variable_refcount_visitor() 6101e04c3fSmrg{ 6201e04c3fSmrg ralloc_free(this->mem_ctx); 6301e04c3fSmrg _mesa_hash_table_destroy(this->ht, free_entry); 6401e04c3fSmrg} 6501e04c3fSmrg 6601e04c3fSmrg// constructor 6701e04c3fSmrgir_variable_refcount_entry::ir_variable_refcount_entry(ir_variable *var) 6801e04c3fSmrg{ 6901e04c3fSmrg this->var = var; 7001e04c3fSmrg assigned_count = 0; 7101e04c3fSmrg declaration = false; 7201e04c3fSmrg referenced_count = 0; 7301e04c3fSmrg} 7401e04c3fSmrg 7501e04c3fSmrg 7601e04c3fSmrgir_variable_refcount_entry * 7701e04c3fSmrgir_variable_refcount_visitor::get_variable_entry(ir_variable *var) 7801e04c3fSmrg{ 7901e04c3fSmrg assert(var); 8001e04c3fSmrg 8101e04c3fSmrg struct hash_entry *e = _mesa_hash_table_search(this->ht, var); 8201e04c3fSmrg if (e) 8301e04c3fSmrg return (ir_variable_refcount_entry *)e->data; 8401e04c3fSmrg 8501e04c3fSmrg ir_variable_refcount_entry *entry = new ir_variable_refcount_entry(var); 8601e04c3fSmrg assert(entry->referenced_count == 0); 8701e04c3fSmrg _mesa_hash_table_insert(this->ht, var, entry); 8801e04c3fSmrg 8901e04c3fSmrg return entry; 9001e04c3fSmrg} 9101e04c3fSmrg 9201e04c3fSmrg 9301e04c3fSmrgir_visitor_status 9401e04c3fSmrgir_variable_refcount_visitor::visit(ir_variable *ir) 9501e04c3fSmrg{ 9601e04c3fSmrg ir_variable_refcount_entry *entry = this->get_variable_entry(ir); 9701e04c3fSmrg if (entry) 9801e04c3fSmrg entry->declaration = true; 9901e04c3fSmrg 10001e04c3fSmrg return visit_continue; 10101e04c3fSmrg} 10201e04c3fSmrg 10301e04c3fSmrg 10401e04c3fSmrgir_visitor_status 10501e04c3fSmrgir_variable_refcount_visitor::visit(ir_dereference_variable *ir) 10601e04c3fSmrg{ 10701e04c3fSmrg ir_variable *const var = ir->variable_referenced(); 10801e04c3fSmrg ir_variable_refcount_entry *entry = this->get_variable_entry(var); 10901e04c3fSmrg 11001e04c3fSmrg if (entry) 11101e04c3fSmrg entry->referenced_count++; 11201e04c3fSmrg 11301e04c3fSmrg return visit_continue; 11401e04c3fSmrg} 11501e04c3fSmrg 11601e04c3fSmrg 11701e04c3fSmrgir_visitor_status 11801e04c3fSmrgir_variable_refcount_visitor::visit_enter(ir_function_signature *ir) 11901e04c3fSmrg{ 12001e04c3fSmrg /* We don't want to descend into the function parameters and 12101e04c3fSmrg * dead-code eliminate them, so just accept the body here. 12201e04c3fSmrg */ 12301e04c3fSmrg visit_list_elements(this, &ir->body); 12401e04c3fSmrg return visit_continue_with_parent; 12501e04c3fSmrg} 12601e04c3fSmrg 12701e04c3fSmrg 12801e04c3fSmrgir_visitor_status 12901e04c3fSmrgir_variable_refcount_visitor::visit_leave(ir_assignment *ir) 13001e04c3fSmrg{ 13101e04c3fSmrg ir_variable_refcount_entry *entry; 13201e04c3fSmrg entry = this->get_variable_entry(ir->lhs->variable_referenced()); 13301e04c3fSmrg if (entry) { 13401e04c3fSmrg entry->assigned_count++; 13501e04c3fSmrg 13601e04c3fSmrg /* Build a list for dead code optimisation. Don't add assignment if it 13701e04c3fSmrg * was declared out of scope (outside the instruction stream). Also don't 13801e04c3fSmrg * bother adding any more to the list if there are more references than 13901e04c3fSmrg * assignments as this means the variable is used and won't be optimised 14001e04c3fSmrg * out. 14101e04c3fSmrg */ 14201e04c3fSmrg assert(entry->referenced_count >= entry->assigned_count); 14301e04c3fSmrg if (entry->referenced_count == entry->assigned_count) { 14401e04c3fSmrg struct assignment_entry *assignment_entry = 14501e04c3fSmrg (struct assignment_entry *)calloc(1, sizeof(*assignment_entry)); 14601e04c3fSmrg assignment_entry->assign = ir; 14701e04c3fSmrg entry->assign_list.push_head(&assignment_entry->link); 14801e04c3fSmrg } 14901e04c3fSmrg } 15001e04c3fSmrg 15101e04c3fSmrg return visit_continue; 15201e04c3fSmrg} 153