Home | History | Annotate | Download | only in glsl
      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 opt_dead_code.cpp
     26  *
     27  * Eliminates dead assignments and variable declarations from the code.
     28  */
     29 
     30 #include "ir.h"
     31 #include "ir_visitor.h"
     32 #include "ir_variable_refcount.h"
     33 #include "glsl_types.h"
     34 
     35 static bool debug = false;
     36 
     37 /**
     38  * Do a dead code pass over instructions and everything that instructions
     39  * references.
     40  *
     41  * Note that this will remove assignments to globals, so it is not suitable
     42  * for usage on an unlinked instruction stream.
     43  */
     44 bool
     45 do_dead_code(exec_list *instructions, bool uniform_locations_assigned)
     46 {
     47    ir_variable_refcount_visitor v;
     48    bool progress = false;
     49 
     50    v.run(instructions);
     51 
     52    foreach_iter(exec_list_iterator, iter, v.variable_list) {
     53       ir_variable_refcount_entry *entry = (ir_variable_refcount_entry *)iter.get();
     54 
     55       /* Since each assignment is a reference, the refereneced count must be
     56        * greater than or equal to the assignment count.  If they are equal,
     57        * then all of the references are assignments, and the variable is
     58        * dead.
     59        *
     60        * Note that if the variable is neither assigned nor referenced, both
     61        * counts will be zero and will be caught by the equality test.
     62        */
     63       assert(entry->referenced_count >= entry->assigned_count);
     64 
     65       if (debug) {
     66 	 printf("%s@%p: %d refs, %d assigns, %sdeclared in our scope\n",
     67 		entry->var->name, (void *) entry->var,
     68 		entry->referenced_count, entry->assigned_count,
     69 		entry->declaration ? "" : "not ");
     70       }
     71 
     72       if ((entry->referenced_count > entry->assigned_count)
     73 	  || !entry->declaration)
     74 	 continue;
     75 
     76       if (entry->assign) {
     77 	 /* Remove a single dead assignment to the variable we found.
     78 	  * Don't do so if it's a shader output, though.
     79 	  */
     80 	 if (entry->var->mode != ir_var_out &&
     81 	     entry->var->mode != ir_var_inout) {
     82 	    entry->assign->remove();
     83 	    progress = true;
     84 
     85 	    if (debug) {
     86 	       printf("Removed assignment to %s@%p\n",
     87 		      entry->var->name, (void *) entry->var);
     88 	    }
     89 	 }
     90       } else {
     91 	 /* If there are no assignments or references to the variable left,
     92 	  * then we can remove its declaration.
     93 	  */
     94 
     95 	 /* uniform initializers are precious, and could get used by another
     96 	  * stage.  Also, once uniform locations have been assigned, the
     97 	  * declaration cannot be deleted.
     98 	  *
     99 	  * Also, GL_ARB_uniform_buffer_object says that std140
    100 	  * uniforms will not be eliminated.  Since we always do
    101 	  * std140, just don't eliminate uniforms in UBOs.
    102 	  */
    103 	 if (entry->var->mode == ir_var_uniform &&
    104 	     (uniform_locations_assigned ||
    105 	      entry->var->constant_value ||
    106 	      entry->var->uniform_block != -1))
    107 	    continue;
    108 
    109 	 entry->var->remove();
    110 	 progress = true;
    111 
    112 	 if (debug) {
    113 	    printf("Removed declaration of %s@%p\n",
    114 		   entry->var->name, (void *) entry->var);
    115 	 }
    116       }
    117    }
    118 
    119    return progress;
    120 }
    121 
    122 /**
    123  * Does a dead code pass on the functions present in the instruction stream.
    124  *
    125  * This is suitable for use while the program is not linked, as it will
    126  * ignore variable declarations (and the assignments to them) for variables
    127  * with global scope.
    128  */
    129 bool
    130 do_dead_code_unlinked(exec_list *instructions)
    131 {
    132    bool progress = false;
    133 
    134    foreach_iter(exec_list_iterator, iter, *instructions) {
    135       ir_instruction *ir = (ir_instruction *)iter.get();
    136       ir_function *f = ir->as_function();
    137       if (f) {
    138 	 foreach_iter(exec_list_iterator, sigiter, *f) {
    139 	    ir_function_signature *sig =
    140 	       (ir_function_signature *) sigiter.get();
    141 	    /* The setting of the uniform_locations_assigned flag here is
    142 	     * irrelevent.  If there is a uniform declaration encountered
    143 	     * inside the body of the function, something has already gone
    144 	     * terribly, terribly wrong.
    145 	     */
    146 	    if (do_dead_code(&sig->body, false))
    147 	       progress = true;
    148 	 }
    149       }
    150    }
    151 
    152    return progress;
    153 }
    154