added function to remove a keepalive edge
[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  * Transform Sel[method] to SymC[method] if possible.
83  */
84 ir_node *transform_node_Sel(ir_node *node)
85 {
86   ir_node *new_node, *ptr;
87   ir_type *dyn_tp;
88   entity  *ent = get_Sel_entity(node);
89
90   if (get_irp_phase_state() == phase_building) return node;
91
92   if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
93     return node;
94
95   if (!is_Method_type(get_entity_type(ent)))
96     return node;
97
98   /* If the entity is a leave in the inheritance tree,
99      we can replace the Sel by a constant. */
100   if (get_opt_closed_world() && get_entity_n_overwrittenby(ent) == 0) {
101     /* In dead code, we might call a leave entity that is a description.
102        Do not turn the Sel to a SymConst. */
103     if (get_entity_peculiarity(ent) == peculiarity_description) {
104       /* We could remove the Call depending on this Sel. */
105       new_node = node;
106     } else {
107       ir_node *rem_block = get_cur_block();
108       set_cur_block(get_nodes_block(node));
109       new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(ent));
110       set_cur_block(rem_block);
111       DBG_OPT_POLY(node, new_node);
112     }
113
114     return new_node;
115   }
116
117   /* If we know the dynamic type, we can replace the Sel by a constant. */
118   ptr = get_Sel_ptr(node);      /* The address we select from. */
119   dyn_tp = get_dynamic_type(ptr);  /* The runtime type of ptr. */
120
121   if (dyn_tp != firm_unknown_type) {
122     entity *called_ent;
123     ir_node *rem_block;
124
125     /* We know which method will be called, no dispatch necessary. */
126     called_ent = resolve_ent_polymorphy(dyn_tp, ent);
127     /* called_ent may not be description: has no Address/Const to Call! */
128     assert(get_entity_peculiarity(called_ent) != peculiarity_description);
129
130     rem_block = get_cur_block();
131     set_cur_block(get_nodes_block(node));
132     new_node = copy_const_value(get_irn_dbg_info(node), get_atomic_ent_value(called_ent));
133     set_cur_block(rem_block);
134     DBG_OPT_POLY(node, new_node);
135
136     return new_node;
137   }
138
139   return node;
140 }
141
142 /* Transform  Load(Sel(Alloc)[constant static entity])
143  *  to Const[constant static entity value].
144  *
145  *  This function returns a node replacing the Proj(Load)[Value].
146  *  If this is actually called in transform_node, we must build
147  *  a tuple, or replace the Projs of the load.
148  *  Therefore we call this optimization in ldstopt().
149  */
150 ir_node *transform_node_Load(ir_node *n)
151 {
152   ir_node *field_ptr, *new_node, *ptr;
153   entity  *ent;
154   ir_type *dyn_tp;
155
156   if (!(get_opt_optimize() && get_opt_dyn_meth_dispatch()))
157     return n;
158
159   field_ptr = get_Load_ptr(n);
160
161   if (! is_Sel(field_ptr)) return n;
162
163   ent = get_Sel_entity(field_ptr);
164   if ((get_entity_allocation(ent) != allocation_static)    ||
165       (get_entity_variability(ent) != variability_constant)  )
166     return n;
167
168   /* If the entity is a leave in the inheritance tree,
169      we can replace the Sel by a constant. */
170   if ((get_irp_phase_state() != phase_building) && (get_entity_n_overwrittenby(ent) == 0)) {
171     new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(ent));
172     DBG_OPT_POLY(field_ptr, new_node);
173
174     return new_node;
175   }
176
177   /* If we know the dynamic type, we can replace the Sel by a constant. */
178   ptr = get_Sel_ptr(field_ptr);    /* The address we select from. */
179   dyn_tp = get_dynamic_type(ptr);  /* The runtime type of ptr. */
180
181   if (dyn_tp != firm_unknown_type) {
182     entity *loaded_ent;
183
184     /* We know which method will be called, no dispatch necessary. */
185     loaded_ent = resolve_ent_polymorphy(dyn_tp, ent);
186     /* called_ent may not be description: has no Address/Const to Call! */
187     assert(get_entity_peculiarity(loaded_ent) != peculiarity_description);
188
189     new_node = copy_const_value(get_irn_dbg_info(n), get_atomic_ent_value(loaded_ent));
190     DBG_OPT_POLY(field_ptr, new_node);
191
192     return new_node;
193   }
194
195   return n;
196 }