101e04c3fSmrg/* 201e04c3fSmrg * Copyright © 2016 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_array_refcount.cpp 2601e04c3fSmrg * 2701e04c3fSmrg * Provides a visitor which produces a list of variables referenced. 2801e04c3fSmrg */ 2901e04c3fSmrg 3001e04c3fSmrg#include "ir.h" 3101e04c3fSmrg#include "ir_visitor.h" 3201e04c3fSmrg#include "ir_array_refcount.h" 3301e04c3fSmrg#include "compiler/glsl_types.h" 3401e04c3fSmrg#include "util/hash_table.h" 3501e04c3fSmrg 3601e04c3fSmrgir_array_refcount_visitor::ir_array_refcount_visitor() 3701e04c3fSmrg : last_array_deref(0), derefs(0), num_derefs(0), derefs_size(0) 3801e04c3fSmrg{ 3901e04c3fSmrg this->mem_ctx = ralloc_context(NULL); 407e102996Smaya this->ht = _mesa_pointer_hash_table_create(NULL); 4101e04c3fSmrg} 4201e04c3fSmrg 4301e04c3fSmrgstatic void 4401e04c3fSmrgfree_entry(struct hash_entry *entry) 4501e04c3fSmrg{ 4601e04c3fSmrg ir_array_refcount_entry *ivre = (ir_array_refcount_entry *) entry->data; 4701e04c3fSmrg delete ivre; 4801e04c3fSmrg} 4901e04c3fSmrg 5001e04c3fSmrgir_array_refcount_visitor::~ir_array_refcount_visitor() 5101e04c3fSmrg{ 5201e04c3fSmrg ralloc_free(this->mem_ctx); 5301e04c3fSmrg _mesa_hash_table_destroy(this->ht, free_entry); 5401e04c3fSmrg} 5501e04c3fSmrg 5601e04c3fSmrgir_array_refcount_entry::ir_array_refcount_entry(ir_variable *var) 5701e04c3fSmrg : var(var), is_referenced(false) 5801e04c3fSmrg{ 5901e04c3fSmrg num_bits = MAX2(1, var->type->arrays_of_arrays_size()); 6001e04c3fSmrg bits = new BITSET_WORD[BITSET_WORDS(num_bits)]; 6101e04c3fSmrg memset(bits, 0, BITSET_WORDS(num_bits) * sizeof(bits[0])); 6201e04c3fSmrg 6301e04c3fSmrg /* Count the "depth" of the arrays-of-arrays. */ 6401e04c3fSmrg array_depth = 0; 6501e04c3fSmrg for (const glsl_type *type = var->type; 6601e04c3fSmrg type->is_array(); 6701e04c3fSmrg type = type->fields.array) { 6801e04c3fSmrg array_depth++; 6901e04c3fSmrg } 7001e04c3fSmrg} 7101e04c3fSmrg 7201e04c3fSmrg 7301e04c3fSmrgir_array_refcount_entry::~ir_array_refcount_entry() 7401e04c3fSmrg{ 7501e04c3fSmrg delete [] bits; 7601e04c3fSmrg} 7701e04c3fSmrg 7801e04c3fSmrgir_array_refcount_entry * 7901e04c3fSmrgir_array_refcount_visitor::get_variable_entry(ir_variable *var) 8001e04c3fSmrg{ 8101e04c3fSmrg assert(var); 8201e04c3fSmrg 8301e04c3fSmrg struct hash_entry *e = _mesa_hash_table_search(this->ht, var); 8401e04c3fSmrg if (e) 8501e04c3fSmrg return (ir_array_refcount_entry *)e->data; 8601e04c3fSmrg 8701e04c3fSmrg ir_array_refcount_entry *entry = new ir_array_refcount_entry(var); 8801e04c3fSmrg _mesa_hash_table_insert(this->ht, var, entry); 8901e04c3fSmrg 9001e04c3fSmrg return entry; 9101e04c3fSmrg} 9201e04c3fSmrg 9301e04c3fSmrg 9401e04c3fSmrgarray_deref_range * 9501e04c3fSmrgir_array_refcount_visitor::get_array_deref() 9601e04c3fSmrg{ 9701e04c3fSmrg if ((num_derefs + 1) * sizeof(array_deref_range) > derefs_size) { 9801e04c3fSmrg void *ptr = reralloc_size(mem_ctx, derefs, derefs_size + 4096); 9901e04c3fSmrg 10001e04c3fSmrg if (ptr == NULL) 10101e04c3fSmrg return NULL; 10201e04c3fSmrg 10301e04c3fSmrg derefs_size += 4096; 10401e04c3fSmrg derefs = (array_deref_range *)ptr; 10501e04c3fSmrg } 10601e04c3fSmrg 10701e04c3fSmrg array_deref_range *d = &derefs[num_derefs]; 10801e04c3fSmrg num_derefs++; 10901e04c3fSmrg 11001e04c3fSmrg return d; 11101e04c3fSmrg} 11201e04c3fSmrg 11301e04c3fSmrgir_visitor_status 11401e04c3fSmrgir_array_refcount_visitor::visit_enter(ir_dereference_array *ir) 11501e04c3fSmrg{ 11601e04c3fSmrg /* It could also be a vector or a matrix. Individual elements of vectors 11701e04c3fSmrg * are natrices are not tracked, so bail. 11801e04c3fSmrg */ 11901e04c3fSmrg if (!ir->array->type->is_array()) 12001e04c3fSmrg return visit_continue; 12101e04c3fSmrg 12201e04c3fSmrg /* If this array dereference is a child of an array dereference that was 12301e04c3fSmrg * already visited, just continue on. Otherwise, for an arrays-of-arrays 12401e04c3fSmrg * dereference like x[1][2][3][4], we'd process the [1][2][3][4] sequence, 12501e04c3fSmrg * the [1][2][3] sequence, the [1][2] sequence, and the [1] sequence. This 12601e04c3fSmrg * ensures that we only process the full sequence. 12701e04c3fSmrg */ 12801e04c3fSmrg if (last_array_deref && last_array_deref->array == ir) { 12901e04c3fSmrg last_array_deref = ir; 13001e04c3fSmrg return visit_continue; 13101e04c3fSmrg } 13201e04c3fSmrg 13301e04c3fSmrg last_array_deref = ir; 13401e04c3fSmrg 13501e04c3fSmrg num_derefs = 0; 13601e04c3fSmrg 13701e04c3fSmrg ir_rvalue *rv = ir; 13801e04c3fSmrg while (rv->ir_type == ir_type_dereference_array) { 13901e04c3fSmrg ir_dereference_array *const deref = rv->as_dereference_array(); 14001e04c3fSmrg 14101e04c3fSmrg assert(deref != NULL); 14201e04c3fSmrg assert(deref->array->type->is_array()); 14301e04c3fSmrg 14401e04c3fSmrg ir_rvalue *const array = deref->array; 14501e04c3fSmrg const ir_constant *const idx = deref->array_index->as_constant(); 14601e04c3fSmrg array_deref_range *const dr = get_array_deref(); 14701e04c3fSmrg 14801e04c3fSmrg dr->size = array->type->array_size(); 14901e04c3fSmrg 15001e04c3fSmrg if (idx != NULL) { 15101e04c3fSmrg dr->index = idx->get_int_component(0); 15201e04c3fSmrg } else { 15301e04c3fSmrg /* An unsized array can occur at the end of an SSBO. We can't track 15401e04c3fSmrg * accesses to such an array, so bail. 15501e04c3fSmrg */ 15601e04c3fSmrg if (array->type->array_size() == 0) 15701e04c3fSmrg return visit_continue; 15801e04c3fSmrg 15901e04c3fSmrg dr->index = dr->size; 16001e04c3fSmrg } 16101e04c3fSmrg 16201e04c3fSmrg rv = array; 16301e04c3fSmrg } 16401e04c3fSmrg 16501e04c3fSmrg ir_dereference_variable *const var_deref = rv->as_dereference_variable(); 16601e04c3fSmrg 16701e04c3fSmrg /* If the array being dereferenced is not a variable, bail. At the very 16801e04c3fSmrg * least, ir_constant and ir_dereference_record are possible. 16901e04c3fSmrg */ 17001e04c3fSmrg if (var_deref == NULL) 17101e04c3fSmrg return visit_continue; 17201e04c3fSmrg 17301e04c3fSmrg ir_array_refcount_entry *const entry = 17401e04c3fSmrg this->get_variable_entry(var_deref->var); 17501e04c3fSmrg 17601e04c3fSmrg if (entry == NULL) 17701e04c3fSmrg return visit_stop; 17801e04c3fSmrg 1797ec681f3Smrg link_util_mark_array_elements_referenced(derefs, num_derefs, 1807ec681f3Smrg entry->array_depth, 1817ec681f3Smrg entry->bits); 18201e04c3fSmrg 18301e04c3fSmrg return visit_continue; 18401e04c3fSmrg} 18501e04c3fSmrg 18601e04c3fSmrg 18701e04c3fSmrgir_visitor_status 18801e04c3fSmrgir_array_refcount_visitor::visit(ir_dereference_variable *ir) 18901e04c3fSmrg{ 19001e04c3fSmrg ir_variable *const var = ir->variable_referenced(); 19101e04c3fSmrg ir_array_refcount_entry *entry = this->get_variable_entry(var); 19201e04c3fSmrg 19301e04c3fSmrg entry->is_referenced = true; 19401e04c3fSmrg 19501e04c3fSmrg return visit_continue; 19601e04c3fSmrg} 19701e04c3fSmrg 19801e04c3fSmrg 19901e04c3fSmrgir_visitor_status 20001e04c3fSmrgir_array_refcount_visitor::visit_enter(ir_function_signature *ir) 20101e04c3fSmrg{ 20201e04c3fSmrg /* We don't want to descend into the function parameters and 20301e04c3fSmrg * dead-code eliminate them, so just accept the body here. 20401e04c3fSmrg */ 20501e04c3fSmrg visit_list_elements(this, &ir->body); 20601e04c3fSmrg return visit_continue_with_parent; 20701e04c3fSmrg} 208