1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/*
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright © 2010 Intel Corporation
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the "Software"),
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * to deal in the Software without restriction, including without limitation
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * and/or sell copies of the Software, and to permit persons to whom the
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software is furnished to do so, subject to the following conditions:
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the next
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * paragraph) shall be included in all copies or substantial portions of the
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Software.
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * DEALINGS IN THE SOFTWARE.
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * \file opt_dead_functions.cpp
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Eliminates unused functions from the linked program.
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir.h"
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir_visitor.h"
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "ir_expression_flattening.h"
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "glsl_types.h"
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgnamespace {
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass signature_entry : public exec_node
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgpublic:
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   signature_entry(ir_function_signature *sig)
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->signature = sig;
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->used = false;
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_function_signature *signature;
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   bool used;
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgclass ir_dead_functions_visitor : public ir_hierarchical_visitor {
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgpublic:
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_dead_functions_visitor()
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      this->mem_ctx = ralloc_context(NULL);
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ~ir_dead_functions_visitor()
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ralloc_free(this->mem_ctx);
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   virtual ir_visitor_status visit_enter(ir_function_signature *);
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   virtual ir_visitor_status visit_enter(ir_call *);
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   signature_entry *get_signature_entry(ir_function_signature *var);
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* List of signature_entry */
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   exec_list signature_list;
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   void *mem_ctx;
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org};
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org} /* unnamed namespace */
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgsignature_entry *
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_dead_functions_visitor::get_signature_entry(ir_function_signature *sig)
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   foreach_iter(exec_list_iterator, iter, this->signature_list) {
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      signature_entry *entry = (signature_entry *)iter.get();
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (entry->signature == sig)
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 return entry;
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   signature_entry *entry = new(mem_ctx) signature_entry(sig);
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   this->signature_list.push_tail(entry);
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return entry;
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_visitor_status
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_dead_functions_visitor::visit_enter(ir_function_signature *ir)
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   signature_entry *entry = this->get_signature_entry(ir);
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (strcmp(ir->function_name(), "main") == 0) {
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      entry->used = true;
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return visit_continue;
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_visitor_status
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgir_dead_functions_visitor::visit_enter(ir_call *ir)
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   signature_entry *entry = this->get_signature_entry(ir->callee);
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   entry->used = true;
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return visit_continue;
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgbool
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgdo_dead_functions(exec_list *instructions)
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ir_dead_functions_visitor v;
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   bool progress = false;
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   visit_list_elements(&v, instructions);
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Now that we've figured out which function signatures are used, remove
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * the unused ones, and remove function definitions that have no more
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * signatures.
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    foreach_iter(exec_list_iterator, iter, v.signature_list) {
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      signature_entry *entry = (signature_entry *)iter.get();
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (!entry->used) {
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 entry->signature->remove();
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 delete entry->signature;
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 progress = true;
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      delete(entry);
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* We don't just do this above when we nuked a signature because of
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * const pointers.
139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   foreach_iter(exec_list_iterator, iter, *instructions) {
141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ir_instruction *ir = (ir_instruction *)iter.get();
142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ir_function *func = ir->as_function();
143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (func && func->signatures.is_empty()) {
145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 /* At this point (post-linking), the symbol table is no
146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  * longer in use, so not removing the function from the
147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  * symbol table should be OK.
148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	  */
149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 func->remove();
150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 delete func;
151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	 progress = true;
152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return progress;
156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
157