iropt: cmp(~x & 1, 0) => !cmp(x & 1, 0)
[libfirm] / ir / opt / tropt.c
1 /*
2  * Copyright (C) 1995-2011 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 #include "config.h"
28
29 #include <assert.h>
30
31 #include "iroptimize.h"
32 #include "irprog.h"
33 #include "irtypeinfo.h"
34 #include "irgwalk.h"
35 #include "trouts.h"
36 #include "ircons.h"
37 #include "irgmod.h"
38 #include "irflag_t.h"
39 #include "xmalloc.h"
40 #include "debug.h"
41 #include "opt_init.h"
42
43 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
44
45 /* - statistics ---------------------------------------------- */
46
47 static size_t n_casts_normalized = 0;
48 static size_t n_casts_removed    = 0;
49 static size_t n_sels_concretized = 0;
50
51 /* - Cast normalization. ------------------------------------- */
52
53 static ir_type *default_gen_pointer_type_to(ir_type *tp);
54
55 #define PTR_TYPE_SUFFIX "cc_ptr_tp"  /* class cast pointer type. */
56 static ident *ptr_type_suffix = NULL;
57 static gen_pointer_type_to_func gen_pointer_type_to = default_gen_pointer_type_to;
58
59 /**
60  * Find a pointer type to a given type.
61  * Uses and updates trouts if available.
62  */
63 static ir_type *default_gen_pointer_type_to(ir_type *tp)
64 {
65         ir_type *res = find_pointer_type_to_type(tp);
66         if (res == firm_unknown_type)
67                 res = new_type_pointer(tp);
68
69         return res;
70 }
71
72 /** Return a type that is a depth times pointer to type. */
73 static ir_type *pointerize_type(ir_type *tp, size_t depth)
74 {
75         for (; depth > 0; --depth) {
76                 tp = gen_pointer_type_to(tp);
77         }
78         return tp;
79 }
80
81
82 static ir_node *normalize_values_type(ir_type *totype, ir_node *pred)
83 {
84         ir_type *fromtype = get_irn_typeinfo_type(pred);
85         ir_node *new_cast = pred;
86         ir_node *block;
87         size_t  ref_depth = 0;
88
89         if (totype == fromtype) return pred;   /* Case for optimization! */
90
91         while (is_Pointer_type(totype) && is_Pointer_type(fromtype)) {
92                 totype   = get_pointer_points_to_type(totype);
93                 fromtype = get_pointer_points_to_type(fromtype);
94                 ++ref_depth;
95         }
96
97         if (!is_Class_type(totype))   return pred;
98         if (!is_Class_type(fromtype)) return pred;
99
100         if ((get_class_supertype_index(totype, fromtype) != (size_t)-1) ||
101                 (get_class_supertype_index(fromtype, totype) != (size_t)-1) ) {
102                         /* It's just what we want ... */
103                         return pred;
104         }
105
106         block = get_nodes_block(pred);
107
108         if (is_SubClass_of(totype, fromtype)) {
109                 /* downcast */
110                 while (get_class_subtype_index(fromtype, totype) == (size_t)-1) {
111                         /* Insert a cast to a subtype of fromtype. */
112                         ir_type *new_type = NULL;
113                         ir_node *new_cast;
114                         size_t   i;
115                         size_t   n_subtypes = get_class_n_subtypes(fromtype);
116                         for (i = 0; i < n_subtypes && !new_type; ++i) {
117                                 ir_type *new_sub = get_class_subtype(fromtype, i);
118                                 if (is_SuperClass_of(new_sub, totype))
119                                         new_type = new_sub;
120                         }
121                         assert(new_type);
122                         fromtype = new_type;
123                         new_type = pointerize_type(new_type, ref_depth);
124                         new_cast = new_r_Cast(block, pred, new_type);
125                         pred = new_cast;
126                         ++n_casts_normalized;
127                         set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
128                 }
129         } else {
130                 assert(is_SuperClass_of(totype, fromtype));
131                 /* upcast */
132                 while (get_class_supertype_index(fromtype, totype) == (size_t)-1) {
133                         /* Insert a cast to a supertype of fromtype. */
134                         ir_type *new_type = NULL;
135                         size_t i, n_supertypes = get_class_n_supertypes(fromtype);
136                         for (i = 0; i < n_supertypes && !new_type; ++i) {
137                                 ir_type *new_super = get_class_supertype(fromtype, i);
138                                 if (is_SubClass_of(new_super, totype))
139                                         new_type = new_super;
140                         }
141                         assert(new_type);
142                         fromtype = new_type;
143                         new_type = pointerize_type(new_type, ref_depth);
144                         new_cast = new_r_Cast(block, pred, new_type);
145                         pred = new_cast;
146                         ++n_casts_normalized;
147                         set_irn_typeinfo_type(new_cast, new_type);  /* keep type information up to date. */
148                 }
149         }
150         return new_cast;
151 }
152
153 /**
154  * Post-Walker.
155  */
156 static void normalize_irn_class_cast(ir_node *n, void *env)
157 {
158         ir_node *res;
159         (void) env;
160         if (is_Cast(n)) {
161                 ir_node *pred   = get_Cast_op(n);
162                 ir_type *totype = get_Cast_type(n);
163                 res = normalize_values_type(totype, pred);
164                 set_Cast_op(n, res);
165         } else if (is_Call(n)) {
166                 size_t n_params = get_Call_n_params(n);
167                 size_t i;
168                 ir_type *tp = get_Call_type(n);
169                 for (i = 0; i < n_params; ++i) {
170                         res = normalize_values_type(get_method_param_type(tp, i), get_Call_param(n, i));
171                         set_Call_param(n, i, res);
172                 }
173         }
174 }
175
176
177 static void pure_normalize_irg_class_casts(ir_graph *irg)
178 {
179         assert(get_irg_class_cast_state(irg) != ir_class_casts_any &&
180                 "Cannot normalize irregular casts.");
181         if (get_irg_class_cast_state(irg) == ir_class_casts_normalized) {
182                 verify_irg_class_cast_state(irg);
183                 return;
184         }
185
186         irg_walk_graph(irg, NULL, normalize_irn_class_cast, NULL);
187         set_irg_class_cast_state(irg, ir_class_casts_normalized);
188 }
189
190
191 void normalize_irg_class_casts(ir_graph *irg, gen_pointer_type_to_func gppt_fct)
192 {
193         assert(get_irp_typeinfo_state() == ir_typeinfo_consistent);
194
195         if (gppt_fct) gen_pointer_type_to = gppt_fct;
196
197         if (!ptr_type_suffix)
198                 ptr_type_suffix = new_id_from_str(PTR_TYPE_SUFFIX);
199
200         pure_normalize_irg_class_casts(irg);
201
202         gen_pointer_type_to = default_gen_pointer_type_to;
203 }
204
205 void normalize_irp_class_casts(gen_pointer_type_to_func gppt_fct)
206 {
207         size_t i, n;
208         if (gppt_fct) gen_pointer_type_to = gppt_fct;
209
210 #if 0
211         if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
212                 simple_analyse_types();
213 #endif
214
215         for (i = 0, n = get_irp_n_irgs(); i < n; ++i) {
216                 ir_graph *irg = get_irp_irg(i);
217                 pure_normalize_irg_class_casts(irg);
218         }
219
220         set_irp_class_cast_state(ir_class_casts_normalized);
221         gen_pointer_type_to = default_gen_pointer_type_to;
222
223         DB((dbg, SET_LEVEL_1, " Cast normalization: %zu Casts inserted.\n", n_casts_normalized));
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  * @param cast  the Cast node
240  *
241  * @return 1 if the cast was changed
242  */
243 static int cancel_out_casts(ir_node *cast)
244 {
245         ir_node *orig, *pred = get_Cast_op(cast);
246         ir_type *tp_cast, *tp_pred, *tp_orig;
247
248         if (!is_Cast(pred)) return 0;
249         orig = get_Cast_op(pred);
250
251         tp_cast = get_Cast_type(cast);
252         tp_pred = get_Cast_type(pred);
253         tp_orig = get_irn_typeinfo_type(orig);
254
255         while (is_Pointer_type(tp_cast) &&
256               is_Pointer_type(tp_pred) &&
257               is_Pointer_type(tp_orig)   ) {
258                 tp_cast = get_pointer_points_to_type(tp_cast);
259                 tp_pred = get_pointer_points_to_type(tp_pred);
260                 tp_orig = get_pointer_points_to_type(tp_orig);
261         }
262
263         if (!is_Class_type(tp_cast) || !is_Class_type(tp_pred) || !is_Class_type(tp_orig))
264                 return 0;
265
266         if (is_SubClass_of(tp_pred, tp_cast) && get_opt_suppress_downcast_optimization())
267                 return 0;
268
269         if (tp_cast == tp_orig) {
270                 exchange(cast, orig);
271                 n_casts_removed += 2;
272                 return 1;
273         }
274
275         if (!(is_SubClass_of  (tp_cast, tp_orig) || is_SubClass_of  (tp_orig, tp_cast))) {
276                 /* Avoid (B2)(A)(new B1()) --> (B2)(new B1())
277                 * if B1 =!> B2  and  B2 =!> B1
278                 */
279                 return 0;
280         }
281
282         if ((is_SubClass_of  (tp_cast, tp_pred) && is_SuperClass_of(tp_pred, tp_orig)) ||
283             (is_SuperClass_of(tp_cast, tp_pred) && is_SubClass_of  (tp_pred, tp_orig))) {
284                 /* Cast --> Pred --> Orig */
285                 set_Cast_op(cast, orig);
286                 ++n_casts_removed;
287                 return 1;
288         }
289         return 0;
290 }
291
292 /**
293  * Optimize Sel(Cast(type, ptr), ent) into Sel(ptr_type, ent_type)
294  *
295  * @param sel  the Sel node
296  *
297  * @return 1 if Cast's where removed
298  */
299 static int concretize_selected_entity(ir_node *sel)
300 {
301         ir_node   *cast, *ptr = get_Sel_ptr(sel);
302         ir_type   *orig_tp, *cast_tp;
303         ir_entity *new_ent, *sel_ent;
304         int       res = 0;
305
306         sel_ent = get_Sel_entity(sel);
307         cast    = get_Sel_ptr(sel);
308
309         while (is_Cast(cast)) {
310                 cast_tp = get_Cast_type(cast);
311                 ptr     = get_Cast_op(cast);
312                 orig_tp = get_irn_typeinfo_type(ptr);
313
314                 /* we handle only classes */
315                 if (!is_Pointer_type(orig_tp)|| !is_Pointer_type(cast_tp))
316                         return res;
317                 orig_tp = get_pointer_points_to_type(orig_tp);
318                 cast_tp = get_pointer_points_to_type(cast_tp);
319                 if (!is_Class_type(orig_tp) || !is_Class_type(cast_tp))
320                         return res;
321
322                 /* We only want to concretize, but not generalize. */
323                 if (!is_SuperClass_of(cast_tp, orig_tp))
324                         return res;
325
326                 /* The sel entity should be a member of the cast_tp, else
327                    the graph was not properly typed. */
328                 if (get_class_member_index(cast_tp, sel_ent) == (size_t)-1)
329                         return res;
330
331                 new_ent = resolve_ent_polymorphy(orig_tp, sel_ent);
332
333                 /* New ent must be member of orig_tp. */
334                 if (get_class_member_index(orig_tp, new_ent) == (size_t)-1)
335                         return res;
336
337                 /* all checks done, we can remove the Cast and update the Sel */
338                 set_Sel_entity(sel, new_ent);
339                 set_Sel_ptr(sel, ptr);
340                 ++n_sels_concretized;
341
342                 sel_ent = new_ent;
343                 cast    = ptr;
344                 res     = 1;
345         }
346         return res;
347 }
348
349 /**
350  * Move Casts of the same type through a Phi node, i.e.
351  * Phi(Cast(type, x_0), ..., Cast(type, x_n)) -> Cast(type, Phi(x_0, ..., x_n))
352  *
353  * @param phi  the Phi node
354  *
355  * @return 1 if Cast's where moved
356  */
357 static int concretize_Phi_type(ir_node *phi)
358 {
359         int       n_preds = get_Phi_n_preds(phi);
360         ir_node **pred    = ALLOCAN(ir_node*, n_preds);
361         ir_node  *nn, *blk;
362         ir_type  *totype;
363         ir_type  *fromtype;
364         int       i;
365
366         if (n_preds == 0)
367                 return 0;
368         pred[0] = get_Phi_pred(phi, 0);
369
370         if (!is_Cast(pred[0]))
371                 return 0;
372
373         if (!is_Cast_upcast(pred[0]))
374                 return 0;
375
376         fromtype = get_irn_typeinfo_type(get_Cast_op(pred[0]));
377         totype   = get_Cast_type(pred[0]);
378
379         pred[0] = get_Cast_op(pred[0]);
380         for (i = 1; i < n_preds; ++i) {
381                 pred[i] = get_Phi_pred(phi, i);
382                 if (!is_Cast(pred[i]))
383                         return 0;
384                 if (get_irn_typeinfo_type(get_Cast_op(pred[i])) != fromtype)
385                         return 0;
386                 pred[i] = get_Cast_op(pred[i]);
387         }
388
389         /* Transform Phi */
390         blk = get_nodes_block(phi);
391         nn  = new_r_Phi(blk, n_preds, pred, get_irn_mode(phi));
392         set_irn_typeinfo_type(nn, fromtype);
393         nn  = new_r_Cast(blk, nn, totype);
394         set_irn_typeinfo_type(nn, totype);
395         exchange(phi, nn);
396         return 1;
397 }
398
399 /**
400  * Remove Casted null checks, i.e.
401  *
402  * Cmp(Cast(type, x_orig), NULL_type) -> Cmp(x_orig, NULL_orig)
403  *
404  * @param cmp  the Cmp node
405  *
406  * @return 1 if Cast's where removed
407  */
408 static int remove_Cmp_Null_cast(ir_node *cmp)
409 {
410         ir_graph *irg;
411         ir_node *cast, *null, *new_null;
412         int     cast_pos, null_pos;
413         ir_type *fromtype;
414         ir_mode *mode;
415
416         cast = get_Cmp_left(cmp);
417         if (!is_Cast(cast)) {
418                 null     = cast;
419                 null_pos = 0;
420                 cast     = get_Cmp_right(cmp);
421                 cast_pos = 1;
422                 if (!is_Cast(cast))
423                         return 0;
424         } else {
425                 null = get_Cmp_right(cmp);
426                 cast_pos = 0;
427                 null_pos = 1;
428         }
429
430         if (! is_Const(null))
431                 return 0;
432         mode = get_irn_mode(null);
433         if (!mode_is_reference(mode))
434                 return 0;
435         if (get_Const_tarval(null) != get_mode_null(mode))
436                 return 0;
437
438         /* Transform Cmp */
439         irg = get_irn_irg(cmp);
440         set_irn_n(cmp, cast_pos, get_Cast_op(cast));
441         fromtype = get_irn_typeinfo_type(get_Cast_op(cast));
442         new_null = new_r_Const(irg, get_Const_tarval(null));
443         set_irn_typeinfo_type(new_null, fromtype);
444         set_irn_n(cmp, null_pos, new_null);
445         ++n_casts_removed;
446         return 1;
447 }
448
449 /**
450  * Post-Walker: Optimize class casts (mostly by trying to remove them)
451  */
452 static void irn_optimize_class_cast(ir_node *n, void *env)
453 {
454         int *changed = (int*)env;
455
456         if (is_Cast(n))
457                 *changed |= cancel_out_casts(n);
458         else if (is_Sel(n))
459                 *changed |= concretize_selected_entity(n);
460         else if (is_Phi(n))
461                 *changed |= concretize_Phi_type(n);
462         else if (is_Cmp(n))
463                 *changed |= remove_Cmp_Null_cast(n);
464 }
465
466 void optimize_class_casts(void)
467 {
468         int changed;
469
470 #if 0
471         if (get_irp_typeinfo_state() != ir_typeinfo_consistent)
472                 simple_analyse_types();
473 #endif
474
475         changed = 0;
476         all_irg_walk(NULL, irn_optimize_class_cast, &changed);
477
478         DB((dbg, SET_LEVEL_1, " Cast optimization: %zu Casts removed, %zu Sels concretized.\n",
479                 n_casts_removed, n_sels_concretized));
480 }
481
482 void firm_init_class_casts_opt(void)
483 {
484         FIRM_DBG_REGISTER(dbg, "firm.opt.tropt");
485 }