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