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