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