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