adding assertion to prevent recursive compound types
[libfirm] / ir / opt / tropt.c
1 /**
2  * @file tropt.c
3  *
4  * Project:     libFIRM
5  * File name:   ir/opt/tropt.c
6  * Purpose:     Optimize the type representation.
7  * Author:      Goetz Lindenmaier
8  * Modified by:
9  * Created:     20.4.2005
10  * CVS-ID:      $Id$
11  * Copyright:   (c) 2005 Universität Karlsruhe
12  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
13  *
14  * Perform optimizations of the type representation.
15  */
16
17 #include <alloca.h>
18
19 #include "tropt.h"
20
21 #include "irprog.h"
22 #include "mangle.h"
23
24 #include "tr_inheritance.h"
25 #include "irtypeinfo.h"
26 #include "irgwalk.h"
27 #include "irsimpletype.h"
28 #include "trouts.h"
29 #include "ircons.h"
30 #include "irgmod.h"
31 #include "irflag.h"
32
33 /* - statistics ---------------------------------------------- */
34
35 static int n_casts_normalized = 0;
36 static int n_casts_removed    = 0;
37 static int n_sels_concretized = 0;
38
39 /* - Cast normalization. ------------------------------------- */
40
41 static type *default_gen_pointer_type_to(type *tp);
42
43 #define PTR_TYPE_SUFFIX "cc_ptr_tp"  /* class cast pointer type. */
44 static ident *ptr_type_suffix = NULL;
45 static gen_pointer_type_to_func gen_pointer_type_to = default_gen_pointer_type_to;
46
47 static type *default_gen_pointer_type_to(type *tp) {
48   type *res = NULL;
49   if (get_trouts_state() == outs_consistent) {
50     if (get_type_n_pointertypes_to(tp) > 0) {
51       res = get_type_pointertype_to(tp, 0);
52     } else {
53       res = new_type_pointer(mangle_u(get_type_ident(tp), ptr_type_suffix), tp);
54       /* Update trout for pointertypes, so we can use it in next call. */
55       add_type_pointertype_to(tp, res);
56     }
57   } else {
58     res = find_pointer_type_to_type(tp);
59     if (res == firm_unknown_type)
60       res = new_type_pointer(mangle_u(get_type_ident(tp), ptr_type_suffix), tp);
61   }
62
63   return res;
64 }
65
66 /* Return a type that is a depth times pointer to type. */
67 static type *pointerize_type(type *tp, int depth) {
68   for (; depth > 0; --depth) {
69     tp = gen_pointer_type_to(tp);
70   }
71   return tp;
72 }
73
74 static void normalize_irn_class_cast(ir_node *n, void *env) {
75   if (get_irn_op(n) != op_Cast) return;
76
77
78   type *fromtype = get_irn_typeinfo_type(get_Cast_op(n));
79   type *totype   = get_Cast_type(n);
80   int ref_depth = 0;
81
82   if (totype == fromtype) return;   /* Case for optimization! */
83
84   while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
85     totype   = get_pointer_points_to_type(totype);
86     fromtype = get_pointer_points_to_type(fromtype);
87     ref_depth++;
88   }
89
90   if (!is_Class_type(totype))   return;
91   if (!is_Class_type(fromtype)) return;
92
93   if ((get_class_supertype_index(totype, fromtype) != -1) ||
94       (get_class_supertype_index(fromtype, totype) != -1) ) {
95     /* It's just what we want ... */
96     return;
97   }
98
99   set_cur_block(get_nodes_block(n));
100
101   if (is_subclass_of(totype, fromtype)) {
102     /* downcast */
103     while (get_class_subtype_index(fromtype, totype) == -1) {
104       /* Insert a cast to a subtype of fromtype. */
105       type *new_type = NULL;
106       ir_node *new_cast;
107       int i, n_subtypes = get_class_n_subtypes(fromtype);
108       for (i = 0; i < n_subtypes && !new_type; ++i) {
109         type *new_sub = get_class_subtype(fromtype, i);
110         if (is_superclass_of(new_sub, totype))
111           new_type = new_sub;
112       }
113       assert(new_type);
114       fromtype = new_type;
115       new_type = pointerize_type(new_type, ref_depth);
116       new_cast = new_Cast(get_Cast_op(n), new_type);
117       n_casts_normalized ++;
118       set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
119       set_Cast_op(n, new_cast);
120       if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
121     }
122   }
123   else {
124     assert(is_superclass_of(totype, fromtype));
125     /* upcast */
126     while (get_class_supertype_index(fromtype, totype) == -1) {
127       /* Insert a cast to a supertype of fromtype. */
128       type *new_type = NULL;
129       ir_node *new_cast;
130       int i, n_supertypes = get_class_n_supertypes(fromtype);
131       for (i = 0; i < n_supertypes && !new_type; ++i) {
132         type *new_super = get_class_supertype(fromtype, i);
133         if (is_subclass_of(new_super, totype))
134           new_type = new_super;
135       }
136       assert(new_type);
137       fromtype = new_type;
138       new_type = pointerize_type(new_type, ref_depth);
139       new_cast = new_Cast(get_Cast_op(n), new_type);
140       n_casts_normalized ++;
141       set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
142       if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
143       set_Cast_op(n, new_cast);
144     }
145   }
146 }
147
148
149 static void pure_normalize_irg_class_casts(ir_graph *irg) {
150   assert(get_irg_class_cast_state(irg) != ir_class_casts_any &&
151          "Cannot normalize irregular casts.");
152   if (get_irg_class_cast_state(irg) == ir_class_casts_normalized) {
153     verify_irg_class_cast_state(irg);
154     return;
155   }
156
157   irg_walk_graph(irg, NULL, normalize_irn_class_cast, NULL);
158   set_irg_class_cast_state(irg, ir_class_casts_normalized);
159 }
160
161
162 void normalize_irg_class_casts(ir_graph *irg, gen_pointer_type_to_func gppt_fct) {
163   assert(get_irp_typeinfo_state() == ir_typeinfo_consistent);
164
165   if (gppt_fct) gen_pointer_type_to = gppt_fct;
166
167   if (!ptr_type_suffix)
168     ptr_type_suffix = new_id_from_str(PTR_TYPE_SUFFIX);
169
170   pure_normalize_irg_class_casts(irg);
171
172   gen_pointer_type_to = default_gen_pointer_type_to;
173 }
174
175 void normalize_irp_class_casts(gen_pointer_type_to_func gppt_fct) {
176   int i, n_irgs = get_irp_n_irgs();
177   if (gppt_fct) gen_pointer_type_to = gppt_fct;
178
179   if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
180     simple_analyse_types();
181
182   for (i = 0; i < n_irgs; ++i) {
183     pure_normalize_irg_class_casts(get_irp_irg(i));
184   }
185
186   set_irp_class_cast_state(ir_class_casts_normalized);
187   gen_pointer_type_to = default_gen_pointer_type_to;
188
189   if (get_opt_optimize_class_casts_verbose() && get_firm_verbosity()) {
190     printf(" Cast normalization: %d Casts inserted.\n", n_casts_normalized);
191   }
192 }
193
194
195
196 /* - Cast optimization. ------------------------------------------- */
197
198 static void cancel_out_casts(ir_node *cast) {
199   ir_node *orig, *pred = get_Cast_op(cast);
200   type *tp_cast, *tp_pred, *tp_orig;
201   int ref_depth = 0;
202
203   if (get_irn_op(pred) != op_Cast) return;
204   orig = get_Cast_op(pred);
205
206   tp_cast = get_Cast_type(cast);
207   tp_pred = get_Cast_type(pred);
208   tp_orig = get_irn_typeinfo_type(orig);
209
210   while (is_Pointer_type(tp_cast) &&
211          is_Pointer_type(tp_pred) &&
212          is_Pointer_type(tp_orig)   ) {
213     tp_cast = get_pointer_points_to_type(tp_cast);
214     tp_pred = get_pointer_points_to_type(tp_pred);
215     tp_orig = get_pointer_points_to_type(tp_orig);
216     ref_depth++;
217   }
218
219   if (!is_Class_type(tp_cast)) return;
220   if (!is_Class_type(tp_pred)) return;
221   if (!is_Class_type(tp_orig)) return;
222
223   if (is_subclass_of(tp_pred, tp_cast) && get_opt_suppress_downcast_optimization())
224     return;
225
226   if (tp_cast == tp_orig) {
227     exchange(cast, orig);
228     n_casts_removed += 2;
229     return;
230   }
231
232   if (!(is_subclass_of  (tp_cast, tp_orig) || is_subclass_of  (tp_orig, tp_cast)))
233     /* Avoid (B2)(A)(new B1()) --> (B2)(new B1()) */
234     return;
235
236   if ((is_subclass_of  (tp_cast, tp_pred) && is_superclass_of(tp_pred, tp_orig)) ||
237       (is_superclass_of(tp_cast, tp_pred) && is_subclass_of  (tp_pred, tp_orig))   ) {
238     set_Cast_op (cast, orig);
239     n_casts_removed ++;
240   }
241 }
242
243 static void concretize_selected_entity(ir_node *sel) {
244   ir_node *cast, *ptr = get_Sel_ptr(sel);
245   type *orig_tp, *cast_tp;
246   entity *new_ent, *sel_ent;
247
248   sel_ent = get_Sel_entity(sel);
249   cast = get_Sel_ptr(sel);
250
251   while (get_irn_op(cast) == op_Cast) {
252     cast_tp = get_Cast_type(cast);
253     ptr = get_Cast_op(cast);
254     orig_tp = get_irn_typeinfo_type(ptr);
255
256     if (!is_Pointer_type(orig_tp)) return;
257     if (!is_Pointer_type(cast_tp)) return;
258     orig_tp = get_pointer_points_to_type(orig_tp);
259     cast_tp = get_pointer_points_to_type(cast_tp);
260     if (!is_Class_type(orig_tp)) return;
261     if (!is_Class_type(cast_tp)) return;
262
263     /* We only want to contretize, but not generalize. */
264     if (!is_superclass_of(cast_tp, orig_tp)) return;
265
266     /* Hmm, we are not properly typed. */
267     if (get_class_member_index(cast_tp, sel_ent) == -1) return;
268
269     new_ent = resolve_ent_polymorphy(orig_tp, sel_ent);
270
271     /* New ent must be member of orig_tp. */
272     if (get_class_member_index(orig_tp, new_ent) == -1) return;
273
274     set_Sel_entity(sel, new_ent);
275     set_Sel_ptr(sel, ptr);
276     n_sels_concretized++;
277
278     sel_ent = new_ent;
279     cast = ptr;
280   }
281 }
282
283 static void concretize_Phi_type(ir_node *phi) {
284   int i, n_preds = get_Phi_n_preds(phi);
285   ir_node **pred = alloca(n_preds * sizeof(ir_node *));
286   ir_node *new;
287   type *totype, *fromtype;
288
289   if (n_preds == 0) return;
290   pred[0] = get_Phi_pred(phi, 0);
291
292   if (get_irn_op(pred[0]) != op_Cast) return;
293
294   if (!is_Cast_upcast(pred[0])) return;
295
296   fromtype = get_irn_typeinfo_type(get_Cast_op(pred[0]));
297   totype   = get_Cast_type(pred[0]);
298
299   pred[0] = get_Cast_op(pred[0]);
300   for (i = 1; i < n_preds; ++i) {
301     pred[i] = get_Phi_pred(phi, i);
302     if (get_irn_op(pred[i]) != op_Cast) return;
303     if (get_irn_typeinfo_type(get_Cast_op(pred[i])) != fromtype) return;
304     pred[i] = get_Cast_op(pred[i]);
305   }
306
307   /* Transform Phi */
308   set_cur_block(get_nodes_block(phi));
309   new = new_Phi(n_preds, pred, get_irn_mode(phi));
310   set_irn_typeinfo_type(new, fromtype);
311   new = new_Cast(new, totype);
312   set_irn_typeinfo_type(new, totype);
313   exchange(phi, new);
314 }
315
316 void remove_Cmp_Null_cast(ir_node *cmp) {
317   ir_node *cast, *null, *new_null;
318   int cast_pos, null_pos;
319   type *fromtype;
320
321   cast = get_Cmp_left(cmp);
322   cast_pos = 0;
323   if (get_irn_op(cast) != op_Cast) {
324     null = cast;
325     null_pos = cast_pos;
326     cast = get_Cmp_right(cmp);
327     cast_pos = 1;
328     if (get_irn_op(cast) != op_Cast) return;
329   } else {
330     null = get_Cmp_right(cmp);
331     null_pos = 1;
332   }
333
334   if (get_irn_op(null) != op_Const) return;
335   if (!mode_is_reference(get_irn_mode(null))) return;
336   if (get_Const_tarval(null) != get_mode_null(get_irn_mode(null))) return;
337
338   /* Transform Cmp */
339   set_irn_n(cmp, cast_pos, get_Cast_op(cast));
340   fromtype = get_irn_typeinfo_type(get_Cast_op(cast));
341   new_null = new_Const_type(get_Const_tarval(null), fromtype);
342   set_irn_typeinfo_type(new_null, fromtype);
343   set_irn_n(cmp, null_pos, new_null);
344   n_casts_removed ++;
345 }
346
347 static void irn_optimize_class_cast(ir_node *n, void *env) {
348   if (get_irn_op(n) == op_Cast)
349     cancel_out_casts(n);
350   else if (get_irn_op(n) == op_Sel)
351     concretize_selected_entity(n);
352   else if (get_irn_op(n) == op_Phi)
353     concretize_Phi_type(n);
354   else if (get_irn_op(n) == op_Cmp)
355     remove_Cmp_Null_cast(n);
356 }
357
358 void optimize_class_casts(void) {
359   int i, n_irgs = get_irp_n_irgs();
360
361   if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
362     simple_analyse_types();
363
364   all_irg_walk(NULL, irn_optimize_class_cast, NULL);
365
366   set_trouts_inconsistent();
367   for (i = 0; i < n_irgs; ++i)
368     set_irg_outs_inconsistent(get_irp_irg(i));
369
370   if (get_opt_optimize_class_casts_verbose() && get_firm_verbosity()) {
371     printf(" Cast optimization: %d Casts removed, %d sels concretized.\n",
372            n_casts_removed, n_sels_concretized);
373   }
374 }