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