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 opt_dead_functions.cpp 2601e04c3fSmrg * 2701e04c3fSmrg * Eliminates unused functions from the linked program. 2801e04c3fSmrg */ 2901e04c3fSmrg 3001e04c3fSmrg#include "ir.h" 3101e04c3fSmrg#include "ir_visitor.h" 3201e04c3fSmrg#include "ir_expression_flattening.h" 3301e04c3fSmrg#include "compiler/glsl_types.h" 3401e04c3fSmrg 3501e04c3fSmrgnamespace { 3601e04c3fSmrg 3701e04c3fSmrgclass signature_entry : public exec_node 3801e04c3fSmrg{ 3901e04c3fSmrgpublic: 4001e04c3fSmrg signature_entry(ir_function_signature *sig) 4101e04c3fSmrg { 4201e04c3fSmrg this->signature = sig; 4301e04c3fSmrg this->used = false; 4401e04c3fSmrg } 4501e04c3fSmrg 4601e04c3fSmrg ir_function_signature *signature; 4701e04c3fSmrg bool used; 4801e04c3fSmrg}; 4901e04c3fSmrg 5001e04c3fSmrgclass ir_dead_functions_visitor : public ir_hierarchical_visitor { 5101e04c3fSmrgpublic: 5201e04c3fSmrg ir_dead_functions_visitor() 5301e04c3fSmrg { 5401e04c3fSmrg this->mem_ctx = ralloc_context(NULL); 5501e04c3fSmrg } 5601e04c3fSmrg 5701e04c3fSmrg ~ir_dead_functions_visitor() 5801e04c3fSmrg { 5901e04c3fSmrg ralloc_free(this->mem_ctx); 6001e04c3fSmrg } 6101e04c3fSmrg 6201e04c3fSmrg virtual ir_visitor_status visit_enter(ir_function_signature *); 6301e04c3fSmrg virtual ir_visitor_status visit_enter(ir_call *); 6401e04c3fSmrg 6501e04c3fSmrg signature_entry *get_signature_entry(ir_function_signature *var); 6601e04c3fSmrg 6701e04c3fSmrg /* List of signature_entry */ 6801e04c3fSmrg exec_list signature_list; 6901e04c3fSmrg void *mem_ctx; 7001e04c3fSmrg}; 7101e04c3fSmrg 7201e04c3fSmrg} /* unnamed namespace */ 7301e04c3fSmrg 7401e04c3fSmrgsignature_entry * 7501e04c3fSmrgir_dead_functions_visitor::get_signature_entry(ir_function_signature *sig) 7601e04c3fSmrg{ 7701e04c3fSmrg foreach_in_list(signature_entry, entry, &this->signature_list) { 7801e04c3fSmrg if (entry->signature == sig) 7901e04c3fSmrg return entry; 8001e04c3fSmrg } 8101e04c3fSmrg 8201e04c3fSmrg signature_entry *entry = new(mem_ctx) signature_entry(sig); 8301e04c3fSmrg this->signature_list.push_tail(entry); 8401e04c3fSmrg return entry; 8501e04c3fSmrg} 8601e04c3fSmrg 8701e04c3fSmrg 8801e04c3fSmrgir_visitor_status 8901e04c3fSmrgir_dead_functions_visitor::visit_enter(ir_function_signature *ir) 9001e04c3fSmrg{ 9101e04c3fSmrg signature_entry *entry = this->get_signature_entry(ir); 9201e04c3fSmrg 9301e04c3fSmrg if (strcmp(ir->function_name(), "main") == 0) { 9401e04c3fSmrg entry->used = true; 9501e04c3fSmrg } 9601e04c3fSmrg 9701e04c3fSmrg 9801e04c3fSmrg 9901e04c3fSmrg return visit_continue; 10001e04c3fSmrg} 10101e04c3fSmrg 10201e04c3fSmrg 10301e04c3fSmrgir_visitor_status 10401e04c3fSmrgir_dead_functions_visitor::visit_enter(ir_call *ir) 10501e04c3fSmrg{ 10601e04c3fSmrg signature_entry *entry = this->get_signature_entry(ir->callee); 10701e04c3fSmrg 10801e04c3fSmrg entry->used = true; 10901e04c3fSmrg 11001e04c3fSmrg return visit_continue; 11101e04c3fSmrg} 11201e04c3fSmrg 11301e04c3fSmrgbool 11401e04c3fSmrgdo_dead_functions(exec_list *instructions) 11501e04c3fSmrg{ 11601e04c3fSmrg ir_dead_functions_visitor v; 11701e04c3fSmrg bool progress = false; 11801e04c3fSmrg 11901e04c3fSmrg visit_list_elements(&v, instructions); 12001e04c3fSmrg 12101e04c3fSmrg /* Now that we've figured out which function signatures are used, remove 12201e04c3fSmrg * the unused ones, and remove function definitions that have no more 12301e04c3fSmrg * signatures. 12401e04c3fSmrg */ 12501e04c3fSmrg foreach_in_list_safe(signature_entry, entry, &v.signature_list) { 12601e04c3fSmrg if (!entry->used) { 12701e04c3fSmrg entry->signature->remove(); 12801e04c3fSmrg delete entry->signature; 12901e04c3fSmrg progress = true; 13001e04c3fSmrg } 13101e04c3fSmrg delete(entry); 13201e04c3fSmrg } 13301e04c3fSmrg 13401e04c3fSmrg /* We don't just do this above when we nuked a signature because of 13501e04c3fSmrg * const pointers. 13601e04c3fSmrg */ 13701e04c3fSmrg foreach_in_list_safe(ir_instruction, ir, instructions) { 13801e04c3fSmrg ir_function *func = ir->as_function(); 13901e04c3fSmrg 14001e04c3fSmrg if (func && func->signatures.is_empty()) { 14101e04c3fSmrg /* At this point (post-linking), the symbol table is no 14201e04c3fSmrg * longer in use, so not removing the function from the 14301e04c3fSmrg * symbol table should be OK. 14401e04c3fSmrg */ 14501e04c3fSmrg func->remove(); 14601e04c3fSmrg delete func; 14701e04c3fSmrg progress = true; 14801e04c3fSmrg } 14901e04c3fSmrg } 15001e04c3fSmrg 15101e04c3fSmrg return progress; 15201e04c3fSmrg} 153