5 * File name: ir/ana2/pto.c
9 * Created: Mon 18 Oct 2004
11 * Copyright: (c) 1999-2004 Universität Karlsruhe
12 * Licence: This file is protected by GPL - GNU GENERAL PUBLIC LICENSE.
20 # include "typalise.h"
25 # endif /* not defined TRUE */
38 static long ta_id = 0;
43 static typalise_t *typalise_proj (ir_node*);
47 Ctors, Dtors for typalise_t-s
49 static typalise_t *ta_exact (type *tp)
51 typalise_t *ta = (typalise_t*) xmalloc (sizeof (typalise_t));
52 ta->kind = type_exact;
56 assert (is_class_type (tp));
61 static typalise_t *ta_types (lset_t *set)
63 typalise_t *ta = (typalise_t*) xmalloc (sizeof (typalise_t));
64 ta->kind = type_types;
71 static typalise_t *ta_type (type *tp)
73 typalise_t *ta = (typalise_t*) xmalloc (sizeof (typalise_t));
78 assert (is_class_type (tp));
83 static void ta_delete (typalise_t *ta)
85 if (type_types == ta->kind) {
86 lset_destroy (ta->res.types);
92 ta->kind = type_invalid;
98 Helpers for inheritance, overwriting and stuff
101 Find out whether otype is a subtype of stype.
102 Return non-zero iff otype is a subtype of stype.
104 static int is_subtype (type *otype, type *stype)
106 int n_sub = get_class_n_subtypes (stype);
110 if (otype == stype) {
114 for (i = 0; (!is_sub) && (i < n_sub); i ++) {
115 type *sub = get_class_subtype (stype, i);
117 is_sub |= is_subtype (otype, sub);
126 Compute the closure of all subtypes of otype (including otype
129 static void _collect_subtypes (type *otype, lset_t *set)
131 lset_insert (set, otype);
133 int n_sub = get_class_n_subtypes (otype);
136 for (i = 0; i < n_sub; i ++) {
137 type *sub = get_class_subtype (otype, i);
139 _collect_subtypes (sub, set);
143 static lset_t *subtype_closure (type *otype)
145 lset_t *set = lset_create ();
147 _collect_subtypes (otype, set);
153 Helper method for get_owner_types
155 static void _collect_owner_types (entity *method, ir_graph *graph, lset_t *tps)
157 /* search DOWNwards in clazz hierarchy */
159 if ((peculiarity_description == get_entity_peculiarity (method)) ||
160 (peculiarity_inherited == get_entity_peculiarity (method))) {
161 lset_insert (tps, get_entity_owner (method));
162 } else if (peculiarity_existent == get_entity_peculiarity (method)) {
163 ir_graph *ex_graph = get_entity_irg (method);
165 if ((NULL == ex_graph) || (ex_graph == graph)) {
166 /* wtf? they define the same graph again? well, whatever: */
167 lset_insert (tps, get_entity_owner (method));
169 /* aha: they define a new graph. can't have that, so bail out */
174 int n_over = get_entity_n_overwrittenby (method);
177 for (i = 0; i < n_over; i ++) {
178 entity *ometh = get_entity_overwrittenby (method, i);
180 _collect_owner_types (ometh, graph, tps);
186 Collect all classes that use the given implementation of a method.
188 static lset_t *get_owner_types (ir_graph *graph)
190 lset_t *tps = lset_create ();
191 entity *meth = get_irg_entity (graph);
193 _collect_owner_types (meth, graph, tps);
199 Return a list containing all types of 'set' which are a subtype of 'type'.
201 static lset_t *filter_for_type (lset_t *set, type *stype)
203 type *curs = (type*) lset_first (set);
204 lset_t *lset = lset_create ();
206 while (NULL != curs) {
207 if (is_subtype (curs, stype)) {
208 lset_insert (lset, curs);
211 curs = lset_next (set);
221 Join 'one' and 'two'; both args are deallocated, result is freshly
224 static typalise_t *ta_join (typalise_t *one, typalise_t *two)
226 typalise_t *res = NULL;
229 case (type_invalid): { /* shut up, gcc */ }
232 case (type_invalid): { /* shut up, gcc */ }
234 if (one->res.type == two->res.type) {
237 lset_t *set = lset_create ();
238 lset_insert (set, one->res.type);
239 lset_insert (set, two->res.type);
240 res = ta_types (set);
248 lset_insert (two->res.types, one->res.type);
254 if (is_subtype (one->res.type, two->res.type)) {
258 lset_t *closure = subtype_closure (two->res.type);
259 lset_insert (closure, one->res.type);
270 case (type_invalid): { /* shut up, gcc */ }
272 res = ta_join (two, one);
275 lset_insert_all (one->res.types, two->res.types);
281 lset_t *closure = subtype_closure (two->res.type);
282 lset_append (one->res.types, closure);
292 case (type_invalid): { /* shut up, gcc */ }
294 res = ta_join (two, one);
297 res = ta_join (two, one);
300 type *one_type = one->res.type;
301 type *two_type = two->res.type;
303 if (is_subtype (one_type, two_type)) {
306 } else if (is_subtype (two_type, one_type)) {
310 lset_t *one_closure = subtype_closure (one->res.type);
311 lset_t *two_closure = subtype_closure (two->res.type);
313 lset_append (one_closure, two_closure);
318 res = ta_types (one_closure);
325 assert (res && "no result");
332 static const char *ta_name (typalise_t *ta)
334 /* # define BUF_SIZE 1024 */
335 static char buf [BUF_SIZE];
337 int len = sprintf (buf, "[%d] ", ta->id);
340 case (type_invalid): { /* shut up, gcc */ }
342 len += sprintf (buf+len, "only ");
343 strncat (buf, get_type_name (ta->res.type), BUF_SIZE);
346 len += sprintf (buf+len, "one_of ");
348 type *iter = lset_first (ta->res.types);
350 int size = BUF_SIZE - len - 1;
351 while ((NULL != iter) && (0 < size)) {
352 char *dest = strncat (buf, get_type_name (iter), size);
355 iter = lset_next (ta->res.types);
359 len += sprintf (buf+len, "poly ");
360 strncat (buf, get_type_name (ta->res.type), BUF_SIZE);
365 /* # undef BUF_SIZE */
367 # endif /* SHUT_UP_GCC */
370 Find out whether the given clazz uses the given implementation of a
371 method. Presumably, this is because clazz inherits the graph as
372 the implementation for a method.
374 static int uses_graph (type *clazz, entity *meth, ir_graph *graph)
376 type *g_clazz = get_entity_owner (meth);
378 if (g_clazz == clazz) {
382 if (peculiarity_existent == get_entity_peculiarity (meth)) {
383 ir_graph *g_graph = get_entity_irg (meth);
385 if (g_graph != graph) {
390 /* else inherited or description */
393 int n_over = get_entity_n_overwrittenby (meth); /* DOWN-wards */
395 for (i = 0; (i < n_over) && (!use); i ++) {
396 entity *over = get_entity_overwrittenby (meth, i);
398 use |= uses_graph (clazz, over, graph);
405 Check whether the given typalise_t includes the given type.
407 static int ta_supports (typalise_t *ta, ir_graph *graph)
410 case (type_invalid): { /* shut up, gcc */ }
413 lset_t *tps = get_owner_types (graph);
415 if (lset_contains (tps, ta->res.type)) {
424 entity *meth = get_irg_entity (graph);
425 type *tp = get_entity_owner (meth);
426 int res = is_subtype (tp, ta->res.type);
431 res = uses_graph (ta->res.type, meth, graph);
437 type *tp = get_entity_owner (get_irg_entity (graph));
439 return (lset_contains (ta->res.types, tp));
443 assert (0 && "invalid ta");
447 /* =========== WHAT ELSE ? =========== */
450 Helper to typalise (ir_node*)
453 Find an approximation to the given proj node's value's types
455 static typalise_t *typalise_proj (ir_node *proj)
457 typalise_t *res = NULL;
458 ir_node *proj_in = get_Proj_pred (proj);
460 if (iro_Proj == get_irn_opcode (proj_in)) {
461 /* fprintf (stdout, "\tProj (Proj)\n"); */
463 proj_in = get_Proj_pred (proj_in);
464 if (iro_Start == get_irn_opcode (proj_in)) {
465 long n = get_Proj_proj (proj);
468 ir_graph *graph = get_irn_irg (proj);
469 entity *meth = get_irg_entity (graph);
470 type *tp = get_entity_owner (meth);
472 /* res = ta_exact (tp); */
473 res = ta_type (tp); /* TODO */
476 /* hey, even 'filtering' this NULL by the select of the actual
477 call is probably as "precise" as anything: */
480 } else if (iro_Call == get_irn_opcode (proj_in)) {
481 /* call result ... 'whatever' */
482 ir_node *call_ptr = get_Call_ptr (proj_in);
484 res = typalise (call_ptr);
486 fprintf (stdout, "\n Proj (Proj (%s)) not handled\n",
487 get_op_name (get_irn_op (proj_in)));
491 opcode op = get_irn_opcode (proj_in);
492 if ((iro_Load != op) && (iro_Alloc != op) && (iro_Call != op)) {
493 fprintf (stdout, "\n Proj (%s) not handled\n",
494 get_op_name (get_irn_op (proj_in)));
497 res = typalise (proj_in); /* everything else */
498 /* Proj (Load), Proj (New), Proj (Call) */
510 Given a set of graphs and a typalise_t, return the method (s) in
511 the set that are supported by the typalise_t. Also, deallocates
514 lset_t *filter_for_ta (lset_t *set, typalise_t *ta)
516 lset_t *res = lset_create ();
517 ir_graph *curs = (ir_graph*) lset_first (set);
519 while (NULL != curs) {
520 if (ta_supports (ta, curs)) {
521 lset_insert (res, curs);
524 curs = lset_next (set);
533 For the given ptr, do a quick check about what (class) types may be
536 typalise_t *typalise (ir_node *node)
538 opcode op = get_irn_opcode (node);
539 typalise_t *res = NULL;
543 /* casts always succeed */
544 typalise_t *ta = NULL;
545 type *tp = get_Cast_type (node);
547 if (is_pointer_type (tp)) {
548 tp = get_pointer_points_to_type (tp);
550 assert (is_class_type (tp));
552 ta = typalise (get_Cast_op (node));
554 if (NULL == ta) { /* no type found */
556 } else if (type_exact == ta->kind) { /* one type found */
557 /* nothing (maybe check cast? */
558 } else if (type_type == ta->kind) { /* some types found */
559 if (is_subtype (tp, ta->res.type)) {
560 ta->res.type = tp; /* assume cast is correct */
562 /* should assert (is_subtype (ta->res.type, tp)) */
564 } else if (type_types == ta->kind) {
565 lset_t *ftp = filter_for_type (ta->res.types, tp);
566 lset_destroy (ta->res.types);
574 res = typalise_proj (node);
578 /* presumably it's call (load (ptr)) we're analyzing. */
579 ir_node *load_ptr = get_Load_ptr (node);
581 res = typalise (load_ptr);
586 /* it's call (sel (ptr)) or load (sel (ptr)) */
587 entity *ent = get_Sel_entity (node);
588 type *tp = get_entity_type (ent);
590 if (is_method_type (tp)) {
591 tp = get_entity_type (ent);
592 tp = get_method_res_type (tp, 0);
594 if (is_pointer_type (tp)) {
595 tp = get_pointer_points_to_type (tp);
599 } else if (is_class_type (tp)) {
600 tp = get_entity_type (ent);
602 if (is_pointer_type (tp)) {
603 tp = get_pointer_points_to_type (tp);
607 } else if (is_pointer_type (tp)) {
608 tp = get_pointer_points_to_type (tp);
611 assert (0 && "select not handled");
616 int n_ins = get_irn_arity (node);
618 ir_node *phi_in = NULL;
619 typalise_t *ta = NULL;
620 /* assert (0 && "Do we ever get here?"); */ /* apparently, we do. */
622 for (i = 0; i < n_ins; i ++) {
623 phi_in = get_irn_n (node, i);
624 ta = (NULL == ta) ? typalise (phi_in) : ta_join (ta, typalise (phi_in));
631 type *type = get_Alloc_type (node);
632 res = ta_exact (type);
636 /* presumably call (sel (proj (call))) */
637 ir_node *ptr = get_Call_ptr (node);
639 if (iro_Sel == get_irn_opcode (ptr)) {
640 meth = get_Sel_entity (ptr);
641 } else if (iro_SymConst == get_irn_opcode (ptr)) {
642 if (get_SymConst_kind (ptr) == symconst_addr_ent) {
643 meth = get_SymConst_entity (ptr);
645 meth = NULL; /* WTF? */
650 type *tp = get_method_res_type ((type*) meth, 0);
653 /* could be anything */
654 /* fprintf (stdout, "meth= (null)"); */
658 fprintf (stdout, "]\n");
662 case (iro_SymConst): {
663 if (get_SymConst_kind (node) == symconst_type_tag) {
664 type *tp = get_SymConst_type (node);
667 } else if (get_SymConst_kind (node) == symconst_addr_ent) {
668 entity *ent = get_SymConst_entity (node);
669 type *tp = get_entity_type (ent);
670 tp = get_pointer_points_to_type (tp);
671 assert (is_class_type (tp));
673 res = ta_type (tp); /* can't use ta_exact */
675 fprintf (stdout, "can't handle SymConst %s?\n",
676 get_op_name (get_irn_op (node)));
687 fprintf (stdout, "what's with %s?\n", get_op_name (get_irn_op (node)));
701 Revision 1.1 2004/10/21 11:09:37 liekweg
702 Moved memwalk stuf into irmemwalk
703 Moved lset stuff into lset
704 Moved typalise stuff into typalise