Remove the obsolete .cvsignore files.
[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 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif
19
20 #include <assert.h>
21
22 #include "tropt.h"
23
24 #include "irprog.h"
25 #include "mangle.h"
26
27 #include "tr_inheritance.h"
28 #include "irtypeinfo.h"
29 #include "irgwalk.h"
30 #include "irsimpletype.h"
31 #include "trouts.h"
32 #include "ircons.h"
33 #include "irgmod.h"
34 #include "irflag_t.h"
35 #include "xmalloc.h"
36
37 /* - statistics ---------------------------------------------- */
38
39 static int n_casts_normalized = 0;
40 static int n_casts_removed    = 0;
41 static int n_sels_concretized = 0;
42
43 /* - Cast normalization. ------------------------------------- */
44
45 static ir_type *default_gen_pointer_type_to(ir_type *tp);
46
47 #define PTR_TYPE_SUFFIX "cc_ptr_tp"  /* class cast pointer type. */
48 static ident *ptr_type_suffix = NULL;
49 static gen_pointer_type_to_func gen_pointer_type_to = default_gen_pointer_type_to;
50
51 /**
52  * Find a pointer type to a given type.
53  * Uses and updates trouts if available.
54  */
55 static ir_type *default_gen_pointer_type_to(ir_type *tp) {
56   ir_type *res = NULL;
57   if (get_trouts_state() == outs_consistent) {
58     if (get_type_n_pointertypes_to(tp) > 0) {
59       res = get_type_pointertype_to(tp, 0);
60     } else {
61       ir_mode *mode = is_Method_type(tp) ? mode_P_code : mode_P_data;
62
63       res = new_type_pointer(mangle_u(get_type_ident(tp), ptr_type_suffix), tp, mode);
64       /* Update trout for pointer types, so we can use it in next call. */
65       add_type_pointertype_to(tp, res);
66     }
67   }
68   else {
69     res = find_pointer_type_to_type(tp);
70     if (res == firm_unknown_type)
71       res = new_type_pointer(mangle_u(get_type_ident(tp), ptr_type_suffix), tp, mode_P_data);
72   }
73
74   return res;
75 }
76
77 /** Return a type that is a depth times pointer to type. */
78 static ir_type *pointerize_type(ir_type *tp, int depth) {
79   for (; depth > 0; --depth) {
80     tp = gen_pointer_type_to(tp);
81   }
82   return tp;
83 }
84
85
86 static ir_node *normalize_values_type(ir_type *totype, ir_node *pred) {
87   ir_type *fromtype = get_irn_typeinfo_type(pred);
88   ir_node *new_cast = pred;
89   int ref_depth = 0;
90
91   if (totype == fromtype) return pred;   /* Case for optimization! */
92
93   while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
94     totype   = get_pointer_points_to_type(totype);
95     fromtype = get_pointer_points_to_type(fromtype);
96     ref_depth++;
97   }
98
99   if (!is_Class_type(totype))   return pred;
100   if (!is_Class_type(fromtype)) return pred;
101
102   if ((get_class_supertype_index(totype, fromtype) != -1) ||
103       (get_class_supertype_index(fromtype, totype) != -1) ) {
104     /* It's just what we want ... */
105     return pred;
106   }
107
108   set_cur_block(get_nodes_block(pred));
109
110   if (is_SubClass_of(totype, fromtype)) {
111     /* downcast */
112     while (get_class_subtype_index(fromtype, totype) == -1) {
113       /* Insert a cast to a subtype of fromtype. */
114       ir_type *new_type = NULL;
115       ir_node *new_cast;
116       int i, n_subtypes = get_class_n_subtypes(fromtype);
117       for (i = 0; i < n_subtypes && !new_type; ++i) {
118         ir_type *new_sub = get_class_subtype(fromtype, i);
119         if (is_SuperClass_of(new_sub, totype))
120           new_type = new_sub;
121       }
122       assert(new_type);
123       fromtype = new_type;
124       new_type = pointerize_type(new_type, ref_depth);
125       new_cast = new_Cast(pred, new_type);
126       pred = new_cast;
127       n_casts_normalized ++;
128       set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
129       if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
130     }
131   }
132   else {
133     assert(is_SuperClass_of(totype, fromtype));
134     /* upcast */
135     while (get_class_supertype_index(fromtype, totype) == -1) {
136       /* Insert a cast to a supertype of fromtype. */
137       ir_type *new_type = NULL;
138       int i, n_supertypes = get_class_n_supertypes(fromtype);
139       for (i = 0; i < n_supertypes && !new_type; ++i) {
140         ir_type *new_super = get_class_supertype(fromtype, i);
141         if (is_SubClass_of(new_super, totype))
142           new_type = new_super;
143       }
144       assert(new_type);
145       fromtype = new_type;
146       new_type = pointerize_type(new_type, ref_depth);
147       new_cast = new_Cast(pred, new_type);
148       pred = new_cast;
149       n_casts_normalized ++;
150       set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
151       if (get_trouts_state() != outs_none) add_type_cast(new_type, new_cast);
152     }
153   }
154   return new_cast;
155 }
156
157
158 static void normalize_irn_class_cast(ir_node *n, void *env) {
159   ir_node *res;
160   if (get_irn_op(n) == op_Cast) {
161     ir_node *pred  = get_Cast_op(n);
162     ir_type *totype   = get_Cast_type(n);
163     res = normalize_values_type(totype, pred);
164     set_Cast_op(n, res);
165   } else if (get_irn_op(n) == op_Call) {
166     int i, n_params = get_Call_n_params(n);
167     ir_type *tp = get_Call_type(n);
168     for (i = 0; i < n_params; ++i) {
169       res = normalize_values_type(get_method_param_type(tp, i), get_Call_param(n, i));
170       set_Call_param(n, i, res);
171     }
172   }
173 }
174
175
176 static void pure_normalize_irg_class_casts(ir_graph *irg) {
177   assert(get_irg_class_cast_state(irg) != ir_class_casts_any &&
178          "Cannot normalize irregular casts.");
179   if (get_irg_class_cast_state(irg) == ir_class_casts_normalized) {
180     verify_irg_class_cast_state(irg);
181     return;
182   }
183
184   irg_walk_graph(irg, NULL, normalize_irn_class_cast, NULL);
185   set_irg_class_cast_state(irg, ir_class_casts_normalized);
186 }
187
188
189 void normalize_irg_class_casts(ir_graph *irg, gen_pointer_type_to_func gppt_fct) {
190   assert(get_irp_typeinfo_state() == ir_typeinfo_consistent);
191
192   if (gppt_fct) gen_pointer_type_to = gppt_fct;
193
194   if (!ptr_type_suffix)
195     ptr_type_suffix = new_id_from_str(PTR_TYPE_SUFFIX);
196
197   pure_normalize_irg_class_casts(irg);
198
199   gen_pointer_type_to = default_gen_pointer_type_to;
200 }
201
202 void normalize_irp_class_casts(gen_pointer_type_to_func gppt_fct) {
203   int i, n_irgs = get_irp_n_irgs();
204   if (gppt_fct) gen_pointer_type_to = gppt_fct;
205
206   if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
207     simple_analyse_types();
208
209   for (i = 0; i < n_irgs; ++i) {
210     pure_normalize_irg_class_casts(get_irp_irg(i));
211   }
212
213   set_irp_class_cast_state(ir_class_casts_normalized);
214   gen_pointer_type_to = default_gen_pointer_type_to;
215
216   if (get_opt_optimize_class_casts_verbose() && get_firm_verbosity()) {
217     printf(" Cast normalization: %d Casts inserted.\n", n_casts_normalized);
218   }
219 }
220
221
222
223 /* - Cast optimization. ------------------------------------------- */
224
225 /**
226  * Optimizes Casts:
227  *
228  *  (T1)(T2)x<T1> -> x<T1>
229  *
230  *  (T3)(T2)x<T1> -> (T3)x<T1>
231  *
232  * if possible.
233  */
234 static void cancel_out_casts(ir_node *cast) {
235   ir_node *orig, *pred = get_Cast_op(cast);
236   ir_type *tp_cast, *tp_pred, *tp_orig;
237   int ref_depth = 0;
238
239   if (get_irn_op(pred) != op_Cast) return;
240   orig = get_Cast_op(pred);
241
242   tp_cast = get_Cast_type(cast);
243   tp_pred = get_Cast_type(pred);
244   tp_orig = get_irn_typeinfo_type(orig);
245
246   while (is_Pointer_type(tp_cast) &&
247          is_Pointer_type(tp_pred) &&
248          is_Pointer_type(tp_orig)   ) {
249     tp_cast = get_pointer_points_to_type(tp_cast);
250     tp_pred = get_pointer_points_to_type(tp_pred);
251     tp_orig = get_pointer_points_to_type(tp_orig);
252     ref_depth++;
253   }
254
255   if (!is_Class_type(tp_cast)) return;
256   if (!is_Class_type(tp_pred)) return;
257   if (!is_Class_type(tp_orig)) return;
258
259   if (is_SubClass_of(tp_pred, tp_cast) && get_opt_suppress_downcast_optimization())
260     return;
261
262   if (tp_cast == tp_orig) {
263     exchange(cast, orig);
264     n_casts_removed += 2;
265     return;
266   }
267
268   if (!(is_SubClass_of  (tp_cast, tp_orig) || is_SubClass_of  (tp_orig, tp_cast))) {
269     /* Avoid (B2)(A)(new B1()) --> (B2)(new B1())
270      * if B1 =!> B2  and  B2 =!> B1
271      */
272     return;
273   }
274
275   if ((is_SubClass_of  (tp_cast, tp_pred) && is_SuperClass_of(tp_pred, tp_orig)) ||
276       (is_SuperClass_of(tp_cast, tp_pred) && is_SubClass_of  (tp_pred, tp_orig))   ) {
277     /* Cast --> Pred --> Orig */
278     set_Cast_op (cast, orig);
279     n_casts_removed ++;
280   }
281 }
282
283 static void concretize_selected_entity(ir_node *sel) {
284   ir_node *cast, *ptr = get_Sel_ptr(sel);
285   ir_type *orig_tp, *cast_tp;
286   ir_entity *new_ent, *sel_ent;
287
288   sel_ent = get_Sel_entity(sel);
289   cast = get_Sel_ptr(sel);
290
291   while (get_irn_op(cast) == op_Cast) {
292     cast_tp = get_Cast_type(cast);
293     ptr = get_Cast_op(cast);
294     orig_tp = get_irn_typeinfo_type(ptr);
295
296     if (!is_Pointer_type(orig_tp)) return;
297     if (!is_Pointer_type(cast_tp)) return;
298     orig_tp = get_pointer_points_to_type(orig_tp);
299     cast_tp = get_pointer_points_to_type(cast_tp);
300     if (!is_Class_type(orig_tp)) return;
301     if (!is_Class_type(cast_tp)) return;
302
303     /* We only want to concretize, but not generalize. */
304     if (!is_SuperClass_of(cast_tp, orig_tp)) return;
305
306     /* Hmm, we are not properly typed. */
307     if (get_class_member_index(cast_tp, sel_ent) == -1) return;
308
309     new_ent = resolve_ent_polymorphy(orig_tp, sel_ent);
310
311     /* New ent must be member of orig_tp. */
312     if (get_class_member_index(orig_tp, new_ent) == -1) return;
313
314     set_Sel_entity(sel, new_ent);
315     set_Sel_ptr(sel, ptr);
316     n_sels_concretized++;
317
318     sel_ent = new_ent;
319     cast = ptr;
320   }
321 }
322
323 static void concretize_Phi_type(ir_node *phi) {
324   int i, n_preds = get_Phi_n_preds(phi);
325   ir_node **pred = alloca(n_preds * sizeof(ir_node *));
326   ir_node *new;
327   ir_type *totype, *fromtype;
328
329   if (n_preds == 0) return;
330   pred[0] = get_Phi_pred(phi, 0);
331
332   if (get_irn_op(pred[0]) != op_Cast) return;
333
334   if (!is_Cast_upcast(pred[0])) return;
335
336   fromtype = get_irn_typeinfo_type(get_Cast_op(pred[0]));
337   totype   = get_Cast_type(pred[0]);
338
339   pred[0] = get_Cast_op(pred[0]);
340   for (i = 1; i < n_preds; ++i) {
341     pred[i] = get_Phi_pred(phi, i);
342     if (get_irn_op(pred[i]) != op_Cast) return;
343     if (get_irn_typeinfo_type(get_Cast_op(pred[i])) != fromtype) return;
344     pred[i] = get_Cast_op(pred[i]);
345   }
346
347   /* Transform Phi */
348   set_cur_block(get_nodes_block(phi));
349   new = new_Phi(n_preds, pred, get_irn_mode(phi));
350   set_irn_typeinfo_type(new, fromtype);
351   new = new_Cast(new, totype);
352   set_irn_typeinfo_type(new, totype);
353   exchange(phi, new);
354 }
355
356 void remove_Cmp_Null_cast(ir_node *cmp) {
357   ir_node *cast, *null, *new_null;
358   int cast_pos, null_pos;
359   ir_type *fromtype;
360
361   cast = get_Cmp_left(cmp);
362   cast_pos = 0;
363   if (get_irn_op(cast) != op_Cast) {
364     null = cast;
365     null_pos = cast_pos;
366     cast = get_Cmp_right(cmp);
367     cast_pos = 1;
368     if (get_irn_op(cast) != op_Cast) return;
369   } else {
370     null = get_Cmp_right(cmp);
371     null_pos = 1;
372   }
373
374   if (get_irn_op(null) != op_Const) return;
375   if (!mode_is_reference(get_irn_mode(null))) return;
376   if (get_Const_tarval(null) != get_mode_null(get_irn_mode(null))) return;
377
378   /* Transform Cmp */
379   set_irn_n(cmp, cast_pos, get_Cast_op(cast));
380   fromtype = get_irn_typeinfo_type(get_Cast_op(cast));
381   new_null = new_Const_type(get_Const_tarval(null), fromtype);
382   set_irn_typeinfo_type(new_null, fromtype);
383   set_irn_n(cmp, null_pos, new_null);
384   n_casts_removed ++;
385 }
386
387 /**
388  * Post-Walker:
389  */
390 static void irn_optimize_class_cast(ir_node *n, void *env) {
391   if (get_irn_op(n) == op_Cast)
392     cancel_out_casts(n);
393   else if (get_irn_op(n) == op_Sel)
394     concretize_selected_entity(n);
395   else if (get_irn_op(n) == op_Phi)
396     concretize_Phi_type(n);
397   else if (get_irn_op(n) == op_Cmp)
398     remove_Cmp_Null_cast(n);
399 }
400
401 void optimize_class_casts(void) {
402   int i, n_irgs = get_irp_n_irgs();
403
404   if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
405     simple_analyse_types();
406
407   all_irg_walk(NULL, irn_optimize_class_cast, NULL);
408
409   set_trouts_inconsistent();
410   for (i = 0; i < n_irgs; ++i)
411     set_irg_outs_inconsistent(get_irp_irg(i));
412
413   if (get_opt_optimize_class_casts_verbose() && get_firm_verbosity()) {
414     printf(" Cast optimization: %d Casts removed, %d Sels concretized.\n",
415            n_casts_removed, n_sels_concretized);
416   }
417 }