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