db3853f259338576b91e6fa301aca0d03c9c68e7
[libfirm] / ir / opt / opt_polymorphy.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/opt/opt_polymorphy
4  * Purpose:     Optimize polymorphic Sel nodes.
5  * Author:      Goetz Lindenmaier, Michael Beck
6  * Created:
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 2005 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include "irprog_t.h"
16 #include "entity_t.h"
17 #include "type_t.h"
18 #include "irop.h"
19 #include "irnode_t.h"
20 #include "ircons.h"
21
22 #include "iropt_dbg.h"
23 #include "irflag_t.h"
24
25 /**
26  * Checks if a graph allocates new memory and returns the
27  * type of the newly allocated entity.
28  * Returns NULL if the graph did not represent an Allocation.
29  *
30  * The default implementation hecks for Alloc nodes only.
31  */
32 ir_type *default_firm_get_Alloc(ir_node *n) {
33   n = skip_Proj(n);
34   if (get_irn_op(n) == op_Alloc) {
35     return get_Alloc_type(n);
36   }
37   return NULL;
38 }
39
40 typedef ir_type *(*get_Alloc_func)(ir_node *n);
41
42 /** The get_Alloc function */
43 static get_Alloc_func firm_get_Alloc = default_firm_get_Alloc;
44
45 /** Set a new get_Alloc_func and returns the old one. */
46 get_Alloc_func firm_set_Alloc_func(get_Alloc_func newf) {
47   get_Alloc_func old = firm_get_Alloc;
48   firm_get_Alloc = newf;
49   return old;
50 }
51
52 /** Return dynamic type of ptr.
53  *
54  * If we can deduct the dynamic type from the firm nodes
55  * by a limited test we return the dynamic type.  Else
56  * we return unknown_type.
57  *
58  * If we find a dynamic type this means that the pointer always points
59  * to an object of this type during runtime.   We resolved polymorphy.
60  */
61 static ir_type *get_dynamic_type(ir_node *ptr) {
62   ir_type *tp;
63
64   /* skip Cast and Confirm nodes */
65   for (;;) {
66     opcode code = get_irn_opcode(ptr);
67
68     switch (code) {
69     case iro_Cast:
70       ptr = get_Cast_op(ptr);
71       continue;
72     case iro_Confirm:
73       ptr = get_Confirm_value(ptr);
74       continue;
75     default:
76       ;
77     }
78     break;
79   }
80   tp = (*firm_get_Alloc)(ptr);
81   return tp ? tp : firm_unknown_type;
82 }
83
84 /**
85  * Check, if a entity is final, i.e. is not anymore overridden.
86  */
87 static int is_final_ent(ir_entity *ent) {
88   if (get_entity_final(ent)) {
89     /* not possible to override this entity. */
90     return 1;
91   }
92   if (get_opt_closed_world() && get_entity_n_overwrittenby(ent) == 0) {
93     /* we have a closed world, so simply check how often it was
94        overridden. */
95     return 1;
96   }
97   return 0;
98 }
99
100 /*
101  * Transform Sel[method] to SymC[method] if possible.
102  */
103 ir_node *transform_node_Sel(ir_node *node)
104 {
105   ir_node   *new_node, *ptr;
106   ir_type   *dyn_tp;
107   ir_entity *ent = get_Sel_entity(node);
108
109   if (get_irp_phase_state() == phase_building) return node;
110
111   if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
112     return node;
113
114   if (!is_Method_type(get_entity_type(ent)))
115     return node;
116
117   /* If the entity is a leave in the inheritance tree,
118      we can replace the Sel by a constant. */
119   if (is_final_ent(ent)) {
120     /* In dead code, we might call a leave entity that is a description.
121        Do not turn the Sel to a SymConst. */
122     if (get_entity_peculiarity(ent) == peculiarity_description) {
123       /* We could remove the Call depending on this Sel. */
124       new_node = node;
125     } else {
126       ir_node *rem_block = get_cur_block();
127       set_cur_block(get_nodes_block(node));
128       new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(ent));
129       set_cur_block(rem_block);
130       DBG_OPT_POLY(node, new_node);
131     }
132     return new_node;
133   }
134
135   /* If we know the dynamic type, we can replace the Sel by a constant. */
136   ptr = get_Sel_ptr(node);      /* The address we select from. */
137   dyn_tp = get_dynamic_type(ptr);  /* The runtime type of ptr. */
138
139   if (dyn_tp != firm_unknown_type) {
140     ir_entity *called_ent;
141     ir_node *rem_block;
142
143     /* We know which method will be called, no dispatch necessary. */
144     called_ent = resolve_ent_polymorphy(dyn_tp, ent);
145     /* called_ent may not be description: has no Address/Const to Call! */
146     assert(get_entity_peculiarity(called_ent) != peculiarity_description);
147
148     rem_block = get_cur_block();
149     set_cur_block(get_nodes_block(node));
150     new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(called_ent));
151     set_cur_block(rem_block);
152     DBG_OPT_POLY(node, new_node);
153
154     return new_node;
155   }
156
157   return node;
158 }
159
160 /* Transform  Load(Sel(Alloc)[constant static entity])
161  *  to Const[constant static entity value].
162  *
163  *  This function returns a node replacing the Proj(Load)[Value].
164  *  If this is actually called in transform_node, we must build
165  *  a tuple, or replace the Projs of the load.
166  *  Therefore we call this optimization in ldstopt().
167  */
168 ir_node *transform_node_Load(ir_node *n)
169 {
170   ir_node *field_ptr, *new_node, *ptr;
171   ir_entity *ent;
172   ir_type *dyn_tp;
173
174   if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
175     return n;
176
177   field_ptr = get_Load_ptr(n);
178
179   if (! is_Sel(field_ptr)) return n;
180
181   ent = get_Sel_entity(field_ptr);
182   if ((get_entity_allocation(ent) != allocation_static)    ||
183       (get_entity_variability(ent) != variability_constant)  )
184     return n;
185
186   /* If the entity is a leave in the inheritance tree,
187      we can replace the Sel by a constant. */
188   if ((get_irp_phase_state() != phase_building) && is_final_ent(ent)) {
189     new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(ent));
190     DBG_OPT_POLY(field_ptr, new_node);
191
192     return new_node;
193   }
194
195   /* If we know the dynamic type, we can replace the Sel by a constant. */
196   ptr = get_Sel_ptr(field_ptr);    /* The address we select from. */
197   dyn_tp = get_dynamic_type(ptr);  /* The runtime type of ptr. */
198
199   if (dyn_tp != firm_unknown_type) {
200     ir_entity *loaded_ent;
201
202     /* We know which method will be called, no dispatch necessary. */
203     loaded_ent = resolve_ent_polymorphy(dyn_tp, ent);
204     /* called_ent may not be description: has no Address/Const to Call! */
205     assert(get_entity_peculiarity(loaded_ent) != peculiarity_description);
206
207     new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(loaded_ent));
208     DBG_OPT_POLY(field_ptr, new_node);
209
210     return new_node;
211   }
212
213   return n;
214 }