5 * File name: ir/ana/ecg.c
6 * Purpose: Extended Call Graph
11 * Copyright: (c) 1999-2004 Universität Karlsruhe
12 * Licence: This file is protected by GPL - GNU GENERAL PUBLIC LICENSE.
20 Erweiterter Aufrufgraph.
25 /* #include "eset.h" */
35 # define BUF_SIZE 1024
39 #include <string.h> /* need memset */
45 /* Lists, err, Sets */
46 typedef struct lset_entry
49 struct lset_entry *next;
55 lset_entry_t *last; /* useful for lset_append */
56 lset_entry_t *curs; /* for lset_first/lset_next */
61 typedef enum typalise_kind_enum {
62 type_invalid = 0, /* invalid (only set at deletion time) */
63 type_exact = 1, /* this and only this type (res.type) */
64 type_types = 2, /* these types (res.types) */
65 type_type = 3 /* this type and sub types (res.type) */
68 typedef struct typalise
73 type *type; /* for kind == kind_exact and kind == kind_type */
74 lset_t *types; /* for kind == kind_types */
82 /* static int verbose = 0; */
83 static int do_typalise = 0;
89 /* mapping from method graphs (callR) to method graphs (lset_ts of callEds) */
90 /* static pmap *calls; */
91 static pmap *graph_infos;
93 /** Counters for ecg_ecg and friends */
94 static long _graphs = 0;
95 static long _calls = 0;
96 static long _allocs = 0;
98 static int _depth = 0;
99 static int _max_depth = 0;
101 static int _max_callEds = 0;
102 static entity* _max_callEds_callR = NULL;
104 static long ta_id = 0;
111 /* create a new lset */
112 static lset_t *lset_create (void)
114 lset_t *lset = xmalloc (sizeof (lset_t));
119 /* check whether the lset contains an entry for the given data */
120 static int lset_contains (lset_t *lset, void *data)
122 lset_entry_t *entry = lset->first;
124 while (NULL != entry) {
125 if (data == entry->data) {
135 /* check whether the given lset is empty */
136 static int lset_empty (lset_t *lset)
138 return (NULL == lset->first);
142 /* insert the data into the lset (unless there's an entry for it
144 static void lset_insert (lset_t *lset, void *data)
146 if (! lset_contains (lset, data)) {
147 lset_entry_t *entry = xmalloc (sizeof (lset_entry_t));
149 entry->next = lset->first;
152 if (NULL == lset->last) {
160 /* insert all entries from src into tgt */
161 static void lset_insert_all (lset_t *tgt, lset_t *src)
163 lset_entry_t *curs = src->first;
165 while (NULL != curs) {
166 lset_insert (tgt, curs->data);
172 /* append src to tgt. src is deallocated. */
173 static void lset_append (lset_t *tgt, lset_t *src)
175 assert (! tgt->last->next);
177 tgt->last->next = src->first;
178 tgt->last = src->last;
179 tgt->n_entries += src->n_entries;
181 memset (src, 0x00, sizeof (lset_t));
185 /* remove the entry for the given data element from the lset. return
186 TRUE iff it was on the list in the first place, FALSE else */
188 static int lset_remove (lset_t *lset, void *data)
190 lset_entry_t *entry = lset->first;
191 lset_entry_t *prev = NULL;
193 while (NULL != entry) {
194 if (data == entry->data) {
195 /* ok, dike it out */
197 if (NULL == prev) { /* ok, it's lset->first that needs diking */
198 lset->first = entry->next;
200 prev->next = entry->next;
203 memset (entry, 0x00, sizeof (lset_entry_t));
217 # endif /* def SHUT_UP_GCC */
218 /* prepare the given lset for an iteration. return the first element. */
219 static void *lset_first (lset_t *lset)
221 lset->curs = lset->first;
224 return (lset->first->data);
230 /* after calling lset_first, get the next element, if applicable, or
232 static void *lset_next (lset_t *lset)
234 lset->curs = lset->curs->next;
237 return (lset->curs->data);
243 /* say how many entries there are in the given lset */
244 static int lset_n_entries (lset_t *lset)
246 return (lset->n_entries);
249 /* deallocate the lset and all of its entries */
250 static void lset_destroy (lset_t *lset)
252 lset_entry_t *curs = lset->first;
254 while (NULL != curs) {
255 lset_entry_t *tmp = curs->next;
257 memset (curs, 0x00, sizeof (lset_entry_t));
263 memset (lset, 0x00, sizeof (lset_t));
267 /* ====================
269 ==================== */
271 Find out whether the given clazz uses the given implementation of a
272 method. Presumably, this is because clazz inherits the graph as
273 the implementation for a method.
275 static int uses_graph (type *clazz, entity *meth, ir_graph *graph)
277 type *g_clazz = get_entity_owner (meth);
279 if (g_clazz == clazz) {
283 if (peculiarity_existent == get_entity_peculiarity (meth)) {
284 ir_graph *g_graph = get_entity_irg (meth);
286 if (g_graph != graph) {
291 /* else inherited or description */
294 int n_over = get_entity_n_overwrittenby (meth); /* DOWN-wards */
296 for (i = 0; (i < n_over) && (!use); i ++) {
297 entity *over = get_entity_overwrittenby (meth, i);
299 use |= uses_graph (clazz, over, graph);
307 Find out whether otype is a subtype of stype.
308 Return non-zero iff otype is a subtype of stype.
310 static int is_subtype (type *otype, type *stype)
312 int n_sub = get_class_n_subtypes (stype);
316 if (otype == stype) {
320 for (i = 0; (!is_sub) && (i < n_sub); i ++) {
321 type *sub = get_class_subtype (stype, i);
323 is_sub |= is_subtype (otype, sub);
331 Compute the closure of all subtypes of otype (including otype
334 static void _collect_subtypes (type *otype, lset_t *set)
336 lset_insert (set, otype);
338 int n_sub = get_class_n_subtypes (otype);
341 for (i = 0; i < n_sub; i ++) {
342 type *sub = get_class_subtype (otype, i);
344 _collect_subtypes (sub, set);
348 static lset_t *subtype_closure (type *otype)
350 lset_t *set = lset_create ();
352 _collect_subtypes (otype, set);
358 Helper method for get_owner_types
360 static void _collect_owner_types (entity *method, ir_graph *graph, lset_t *tps)
362 /* search DOWNwards in clazz hierarchy */
364 if ((peculiarity_description == get_entity_peculiarity (method)) ||
365 (peculiarity_inherited == get_entity_peculiarity (method))) {
366 lset_insert (tps, get_entity_owner (method));
367 } else if (peculiarity_existent == get_entity_peculiarity (method)) {
368 ir_graph *ex_graph = get_entity_irg (method);
370 if ((NULL == ex_graph) || (ex_graph == graph)) {
371 /* wtf? they define the same graph again? well, whatever: */
372 lset_insert (tps, get_entity_owner (method));
374 /* aha: they define a new graph. can't have that, so bail out */
379 int n_over = get_entity_n_overwrittenby (method);
382 for (i = 0; i < n_over; i ++) {
383 entity *ometh = get_entity_overwrittenby (method, i);
385 _collect_owner_types (ometh, graph, tps);
391 Collect all classes that use the given implementation of a method.
393 static lset_t *get_owner_types (ir_graph *graph)
395 lset_t *tps = lset_create ();
396 entity *meth = get_irg_entity (graph);
398 _collect_owner_types (meth, graph, tps);
405 Convenience funcs to create a typalise_t
407 static typalise_t *ta_exact (type *tp)
409 typalise_t *ta = (typalise_t*) xmalloc (sizeof (typalise_t));
410 ta->kind = type_exact;
414 assert (is_class_type (tp));
419 static typalise_t *ta_types (lset_t *set)
421 typalise_t *ta = (typalise_t*) xmalloc (sizeof (typalise_t));
422 ta->kind = type_types;
429 static typalise_t *ta_type (type *tp)
431 typalise_t *ta = (typalise_t*) xmalloc (sizeof (typalise_t));
432 ta->kind = type_type;
436 assert (is_class_type (tp));
441 static void ta_delete (typalise_t *ta)
443 if (type_types == ta->kind) {
444 lset_destroy (ta->res.types);
445 ta->res.types = NULL;
450 ta->kind = type_invalid;
456 Join 'one' and 'two'; both args are deallocated, result is freshly
459 static typalise_t *ta_join (typalise_t *one, typalise_t *two)
461 typalise_t *res = NULL;
464 case (type_invalid): { /* shut up, gcc */ }
467 case (type_invalid): { /* shut up, gcc */ }
469 if (one->res.type == two->res.type) {
472 lset_t *set = lset_create ();
473 lset_insert (set, one->res.type);
474 lset_insert (set, two->res.type);
475 res = ta_types (set);
483 lset_insert (two->res.types, one->res.type);
489 if (is_subtype (one->res.type, two->res.type)) {
493 lset_t *closure = subtype_closure (two->res.type);
494 lset_insert (closure, one->res.type);
505 case (type_invalid): { /* shut up, gcc */ }
507 res = ta_join (two, one);
510 lset_insert_all (one->res.types, two->res.types);
516 lset_t *closure = subtype_closure (two->res.type);
517 lset_append (one->res.types, closure);
527 case (type_invalid): { /* shut up, gcc */ }
529 res = ta_join (two, one);
532 res = ta_join (two, one);
535 type *one_type = one->res.type;
536 type *two_type = two->res.type;
538 if (is_subtype (one_type, two_type)) {
541 } else if (is_subtype (two_type, one_type)) {
545 lset_t *one_closure = subtype_closure (one->res.type);
546 lset_t *two_closure = subtype_closure (two->res.type);
548 lset_append (one_closure, two_closure);
553 res = ta_types (one_closure);
560 assert (res && "no result");
567 static const char *ta_name (typalise_t *ta)
569 /* # define BUF_SIZE 1024 */
570 static char buf [BUF_SIZE];
572 int len = sprintf (buf, "[%d] ", ta->id);
575 case (type_invalid): { /* shut up, gcc */ }
577 len += sprintf (buf+len, "only ");
578 strncat (buf, get_type_name (ta->res.type), BUF_SIZE);
581 len += sprintf (buf+len, "one_of ");
583 type *iter = lset_first (ta->res.types);
585 int size = BUF_SIZE - len - 1;
586 while ((NULL != iter) && (0 < size)) {
587 char *dest = strncat (buf, get_type_name (iter), size);
590 iter = lset_next (ta->res.types);
594 len += sprintf (buf+len, "poly ");
595 strncat (buf, get_type_name (ta->res.type), BUF_SIZE);
600 /* # undef BUF_SIZE */
602 # endif /* SHUT_UP_GCC */
605 Check whether the given typalise_t includes the given type.
607 static int ta_supports (typalise_t *ta, ir_graph *graph)
610 case (type_invalid): { /* shut up, gcc */ }
613 lset_t *tps = get_owner_types (graph);
615 if (lset_contains (tps, ta->res.type)) {
624 entity *meth = get_irg_entity (graph);
625 type *tp = get_entity_owner (meth);
626 int res = is_subtype (tp, ta->res.type);
631 res = uses_graph (ta->res.type, meth, graph);
637 type *tp = get_entity_owner (get_irg_entity (graph));
639 return (lset_contains (ta->res.types, tp));
643 assert (0 && "invalid ta");
648 Given a set of graphs and a typalise_t, return the method(s) in
649 the set that are supported by the typalise_t. Also, deallocates
652 static lset_t *filter_for_ta (lset_t *set, typalise_t *ta)
654 lset_t *res = lset_create ();
655 ir_graph *curs = (ir_graph*) lset_first (set);
657 while (NULL != curs) {
658 if (ta_supports (ta, curs)) {
659 lset_insert (res, curs);
662 curs = lset_next (set);
672 Return a list containing all types of 'set' which are a subtype of 'type'.
674 static lset_t *filter_for_type (lset_t *set, type *stype)
676 type *curs = (type*) lset_first (set);
677 lset_t *lset = lset_create ();
679 while (NULL != curs) {
680 if (is_subtype (curs, stype)) {
681 lset_insert (lset, curs);
684 curs = lset_next (set);
691 Find an approximation to the given node's value's types
693 static typalise_t *typalise (ir_node*);
696 Find an approximation to the given proj node's value's types
698 static typalise_t *typalise_proj (ir_node *proj)
700 typalise_t *res = NULL;
701 ir_node *proj_in = get_Proj_pred (proj);
703 if (iro_Proj == get_irn_opcode (proj_in)) {
704 /* fprintf (stdout, "\tProj (Proj)\n"); */
706 proj_in = get_Proj_pred (proj_in);
707 if (iro_Start == get_irn_opcode (proj_in)) {
708 long n = get_Proj_proj (proj);
711 ir_graph *graph = get_irn_irg (proj);
712 entity *meth = get_irg_entity (graph);
713 type *tp = get_entity_owner (meth);
715 /* res = ta_exact (tp); */
716 res = ta_type (tp); /* TODO */
719 /* hey, even 'filtering' this NULL by the select of the actual
720 call is probably as "precise" as anything: */
723 } else if (iro_Call == get_irn_opcode (proj_in)) {
724 /* call result ... 'whatever' */
725 ir_node *call_ptr = get_Call_ptr (proj_in);
727 res = typalise (call_ptr);
729 fprintf (stdout, "\n Proj(Proj(%s)) not handled\n",
730 get_op_name (get_irn_op (proj_in)));
734 opcode op = get_irn_opcode (proj_in);
735 if ((iro_Load != op) && (iro_Alloc != op) && (iro_Call != op)) {
736 fprintf (stdout, "\n Proj(%s) not handled\n",
737 get_op_name (get_irn_op (proj_in)));
740 res = typalise (proj_in); /* everything else */
741 /* Proj(Load), Proj(New), Proj(Call) */
749 For the given ptr, do a quick check about what (class) types may be
752 static typalise_t *typalise (ir_node *node)
754 opcode op = get_irn_opcode (node);
755 typalise_t *res = NULL;
759 typalise_t *ta = NULL;
760 type *tp = get_Cast_type (node);
762 if (is_pointer_type (tp)) {
763 tp = get_pointer_points_to_type (tp);
765 assert (is_class_type (tp));
767 ta = typalise (get_Cast_op (node));
769 if (NULL == ta) { /* no type found */
771 } else if (type_exact == ta->kind) { /* one type found */
772 /* nothing (maybe check cast? */
773 } else if (type_type == ta->kind) { /* some types found */
774 if (is_subtype (tp, ta->res.type)) {
775 ta->res.type = tp; /* assume cast is correct */
777 /* should assert (is_subtype (ta->res.type, tp)) */
779 } else if (type_types == ta->kind) {
780 lset_t *ftp = filter_for_type (ta->res.types, tp);
781 lset_destroy (ta->res.types);
789 res = typalise_proj (node);
793 /* presumably it's call(load(ptr)) we're analyzing. */
794 ir_node *load_ptr = get_Load_ptr (node);
796 res = typalise (load_ptr);
801 /* it's call(sel(ptr)) or load(sel(ptr)) */
802 entity *ent = get_Sel_entity (node);
803 type *tp = get_entity_type (ent);
805 if (is_method_type (tp)) {
806 tp = get_entity_type (ent);
807 tp = get_method_res_type (tp, 0);
809 if (is_pointer_type (tp)) {
810 tp = get_pointer_points_to_type (tp);
814 } else if (is_class_type (tp)) {
815 tp = get_entity_type (ent);
817 if (is_pointer_type (tp)) {
818 tp = get_pointer_points_to_type (tp);
822 } else if (is_pointer_type (tp)) {
823 tp = get_pointer_points_to_type (tp);
826 assert (0 && "select not handled");
831 int n_ins = get_irn_arity (node);
833 ir_node *phi_in = NULL;
834 typalise_t *ta = NULL;
835 /* assert (0 && "Do we ever get here?"); */ /* apparently, we do. */
837 for (i = 0; i < n_ins; i ++) {
838 phi_in = get_irn_n (node, i);
839 ta = (NULL == ta) ? typalise (phi_in) : ta_join (ta, typalise (phi_in));
846 type *type = get_Alloc_type (node);
847 res = ta_exact (type);
851 /* presumably call(sel(proj(call))) */
852 ir_node *ptr = get_Call_ptr (node);
854 if (iro_Sel == get_irn_opcode (ptr)) {
855 meth = get_Sel_entity (ptr);
856 } else if (iro_SymConst == get_irn_opcode (ptr)) {
857 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
858 meth = get_SymConst_entity (ptr);
860 meth = NULL; /* WTF? */
865 type *tp = get_method_res_type ((type*) meth, 0);
868 /* could be anything */
869 /* fprintf (stdout, "meth=(null)"); */
873 fprintf (stdout, "]\n");
877 case (iro_SymConst): {
878 if (get_SymConst_kind(node) == symconst_type_tag) {
879 type *tp = get_SymConst_type (node);
882 } else if (get_SymConst_kind (node) == symconst_addr_ent) {
883 entity *ent = get_SymConst_entity (node);
884 type *tp = get_entity_type (ent);
885 tp = get_pointer_points_to_type (tp);
886 assert (is_class_type (tp));
888 res = ta_type (tp); /* can't use ta_exact */
890 fprintf (stdout, "can't handle SymConst %s?\n",
891 get_op_name (get_irn_op (node)));
902 fprintf (stdout, "what's with %s?\n", get_op_name (get_irn_op (node)));
911 /* ====================
913 ==================== */
914 static void append_alloc (graph_info_t *ginfo, ir_node *alloc, type *tp)
916 alloc_info_t *ainfo = (alloc_info_t*) xmalloc (sizeof (alloc_info_t));
918 ainfo->graph = ginfo->graph;
919 ainfo->alloc = alloc;
922 ainfo->prev = ginfo->allocs;
923 ginfo->allocs = ainfo;
927 /* ====================
929 ==================== */
931 Append the given callEd to the given callEd info.
933 static callEd_info_t *append_callEd_info (callEd_info_t *ced, ir_graph *callEd)
935 callEd_info_t *nced = (callEd_info_t*) xmalloc (sizeof (sizeof (callEd_info_t)));
937 nced->callEd = callEd;
944 Append all callEd methods of the given (call) node to the given graph_info.
946 static void append_calls (graph_info_t *info, ir_node *call, lset_t *callEds)
948 call_info_t *cinfo = (call_info_t*) xmalloc (sizeof (call_info_t));
952 cinfo->prev = info->calls;
954 cinfo->callEds = NULL;
957 ir_graph *callEd = lset_first (callEds);
959 cinfo->callEds = append_callEd_info (cinfo->callEds, callEd);
961 callEd = lset_next (callEds);
966 Append the (single) callEd to the given (call) node of the given graph_info.
968 static void append_call (graph_info_t *info, ir_node *call, ir_graph *callEd)
970 call_info_t *cinfo = (call_info_t*) xmalloc (sizeof (call_info_t));
973 cinfo->prev = info->calls;
976 cinfo->callEds = append_callEd_info (cinfo->callEds, callEd);
980 Given a method, find the firm graph that implements that method.
981 Return NULL for abstract and native methods.
983 static ir_graph *_get_implementing_graph (entity *method)
985 ir_graph *graph = NULL;
987 /* What's up with the fenced out stuff in rta? */
988 if (peculiarity_existent == get_entity_peculiarity (method)) {
989 if (visibility_external_allocated == get_entity_visibility (method)) {
990 /* Todo: native implementation */
994 graph = get_entity_irg (get_SymConst_entity (get_atomic_ent_value (method)));
995 assert (graph && "no graph");
999 } else if (0 && (peculiarity_description == get_entity_peculiarity (method))) {
1000 /* abstract --- can't find an implementation */
1001 graph = get_entity_irg (method);
1002 assert (!graph && "graph in abstract method");
1005 } else if ((peculiarity_description == get_entity_peculiarity (method)) ||
1006 (peculiarity_inherited == get_entity_peculiarity (method))) {
1007 /* search UPWARDS */
1009 int n_over = get_entity_n_overwrites (method);
1013 for (i = 0; (NULL == graph) && (i < n_over); i ++) {
1014 entity *over = get_entity_overwrites (method, i);
1016 graph = _get_implementing_graph (over);
1019 assert (0 && "invalid peculiarity");
1027 Collect all graphs of 'method' in the given set.
1029 static void _collect_implementing_graphs (entity *method, lset_t *set)
1031 /* search DOWN-wards in clazz hierarchy */
1033 int n_over = get_entity_n_overwrittenby (method);
1034 ir_graph *graph = get_entity_irg (method);
1036 if (NULL == graph) {
1037 graph = _get_implementing_graph (method);
1041 lset_insert (set, graph);
1044 for (i = 0; i < n_over; i ++) {
1045 entity *over = get_entity_overwrittenby (method, i);
1047 _collect_implementing_graphs (over, set);
1053 Collect all graphs that could possibly be executed when 'method' is called.
1055 static lset_t *get_implementing_graphs (entity *method, ir_node *select)
1057 lset_t *set = lset_create ();
1059 ir_graph *impl = _get_implementing_graph (method);
1062 lset_insert (set, impl);
1064 /* actually, abstract OR native */
1068 _collect_implementing_graphs (method, set);
1070 if (lset_empty (set)) {
1071 /* then it's a method which is only implemented natively, and we
1072 don' bother to analyse anything */
1076 /* void *tmp = lset_first (set); */
1077 int n_graphs = lset_n_entries (set);
1079 /* typalise select_in */
1081 ir_node *select_in = get_Sel_ptr (select);
1082 typalise_t *ta = typalise (select_in);
1083 assert (ta && "typalise failed (go figure)");
1085 /* const char *res = ta_name (ta); */
1087 /* fprintf (stdout, "typalyse res = %s\n", res); */
1089 if (1 != n_graphs) {
1090 set = filter_for_ta (set, ta);
1092 int n_filtered_graphs = lset_n_entries (set);
1095 fprintf (stdout, "%s: %02d %02d\n",
1099 n_graphs - n_filtered_graphs);
1101 n_graphs = n_filtered_graphs;
1105 if (n_graphs > _max_callEds) {
1106 _max_callEds = n_graphs;
1107 _max_callEds_callR = method;
1111 if (visibility_external_allocated != get_entity_visibility (method)) {
1112 if (0 == n_graphs) {
1113 /* fprintf (stdout, "no graphs for method %s\n", get_entity_name (method)); */
1114 assert (n_graphs && "no graphs for method");
1122 Action for the graph.
1124 static void ecg_calls_act (ir_node *node, void *env)
1126 opcode op = get_irn_opcode (node);
1127 graph_info_t *graph_info = (graph_info_t*) env;
1129 if (iro_Call == op) { /* CALL */
1131 ir_node *ptr = get_Call_ptr (node);
1134 if (iro_Sel == get_irn_opcode (ptr)) {
1135 ent = get_Sel_entity (ptr);
1136 lset_t *graphs = get_implementing_graphs (ent, ptr);
1138 append_calls (graph_info, node, graphs);
1139 } else if (iro_SymConst == get_irn_opcode (ptr)) {
1140 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
1141 ent = get_SymConst_entity (ptr);
1142 ir_graph *graph = get_entity_irg (ent);
1145 append_call (graph_info, node, graph);
1147 /* it's an externally allocated thingy */
1149 } else if (get_SymConst_kind(ptr) == symconst_addr_name) {
1150 /* If this SymConst refers to a method the method is external_visible
1151 and therefore must be considered live anyways. */
1152 if (get_SymConst_name(ptr) != new_id_from_str("iro_Catch")) {
1153 assert (ent && "couldn't determine entity of call to symConst");
1156 /* other symconst. */
1157 assert (0 && "This SymConst can not be an address for a method call.");
1160 /* STRANGE, no less ... */
1163 assert(0 && "Unexpected address expression");
1165 } else if (iro_Alloc == op) {
1166 type *tp = get_Alloc_type (node);
1167 const char *name = get_type_name (tp);
1169 append_alloc (graph_info, node, tp);
1171 fprintf (stdout, "NEW \"%s\"\n", name);
1176 Collect called graphs for the given graph.
1178 static void ecg_fill_graph_calls (ir_graph *graph)
1180 graph_info_t *graph_info = (graph_info_t*) xmalloc (sizeof (graph_info_t));
1182 graph_info->graph = graph;
1183 graph_info->calls = NULL;
1184 graph_info->ecg_seen = 0;
1186 /* entity *method = get_irg_entity (graph); */
1187 /* type *clazz = get_entity_owner (method); */
1189 irg_walk_graph (graph, ecg_calls_act, NULL, graph_info);
1191 pmap_insert (graph_infos, graph, graph_info);
1195 For each graph, collect called graphs, and enter them into calls.
1197 static void ecg_fill_calls (void)
1201 for (i = 0; i < get_irp_n_irgs (); i++) {
1202 ir_graph *graph = get_irp_irg (i);
1204 ecg_fill_graph_calls (graph);
1208 /* ====================
1210 ==================== */
1213 get the call infos for the given graph
1215 graph_info_t *ecg_get_info (ir_graph *graph)
1217 graph_info_t *ginfo = (graph_info_t*) pmap_get (graph_infos, graph);
1219 assert (ginfo && "no info for graph");
1227 Dump the given graph and it's calls and it's calls callEds to the given file.
1229 static int ecg_ecg_graph (FILE *dot, ir_graph *graph)
1231 const char *name = get_irg_entity (graph) ?
1232 get_entity_name (get_irg_entity (graph)) : "noEntity";
1234 (get_entity_stickyness
1235 (get_irg_entity (graph)) == stickyness_sticky) ?
1236 "red" : "lightyellow";
1238 graph_info_t *ginfo = (graph_info_t*) pmap_get (graph_infos, graph);
1240 if (0 != ginfo->ecg_seen) {
1241 fprintf (dot, "\t/* recursive call to \"%s\" (%d) */\n",
1242 name, (int) ginfo->ecg_seen);
1244 fprintf (dot, "\t/* recursive call to \"%s\" (0x%08x) */\n",
1247 return (ginfo->ecg_seen);
1250 assert (0L <= _graphs);
1252 const int graph_no = _graphs ++;
1253 ginfo->ecg_seen = graph_no;
1255 fprintf (dot, "\t/* Graph of \"%s.%s\" */\n",
1256 get_type_name (get_entity_owner (get_irg_entity (graph))),
1258 fprintf (dot, "\tgraph_%i [label=\"%s\\l%s\", color=\"%s\"];\n",
1260 get_type_name (get_entity_owner (get_irg_entity (graph))),
1263 fprintf (dot, "\n");
1265 if (visibility_external_allocated ==
1266 get_entity_visibility (get_irg_entity (graph))) {
1267 fprintf (dot, "\t/* graph \"%s\" is external */\n", name);
1272 call_info_t *cinfo = ginfo->calls;
1273 while (NULL != cinfo) {
1274 ir_node *call = cinfo->call;
1275 callEd_info_t *ced = cinfo->callEds;
1276 const int call_no = _calls ++;
1278 fprintf (dot, "\t/* Call 0x%08x */\n", (int) call);
1279 fprintf (dot, "\tcall_%i [label=\"call\\l0x%08x\"];\n",
1280 call_no, (int) call);
1281 fprintf (dot, "\tgraph_%i -> call_%i [color=\"black\"];\n", graph_no, call_no);
1283 while (NULL != ced) {
1284 ir_graph *callEd_graph = ced->callEd;
1285 const int callEd_no = ecg_ecg_graph (dot, callEd_graph);
1286 const char *callEd_name = get_irg_entity (callEd_graph) ?
1287 get_entity_name (get_irg_entity (callEd_graph)) : "noEntity";
1288 const char *direction = (callEd_no <= graph_no) ? "forward" : "forward";
1289 const char *callEd_color = (callEd_no <= graph_no) ? "red" : "black";
1291 fprintf (dot, "\t/* Call from graph \"%s\" to graph \"%s\" */\n",
1294 /* Check for recursive calls */
1295 /* if (callEd_no > graph_no) */ { /* do recursive calls (for now) */
1296 fprintf (dot, "\tcall_%i -> graph_%i [color=\"%s\", dir=\"%s\"];\n",
1297 call_no, callEd_no, callEd_color, direction);
1302 } /* done all calEds(call) */
1304 cinfo = cinfo->prev;
1305 } /* done all calls(graph) */
1307 /* now the allocs */
1308 alloc_info_t *ainfo = ginfo->allocs;
1310 fprintf (dot, "\t/* now the allocs */\n");
1312 fprintf (dot, "\t/* no allocs */\n");
1315 while (NULL != ainfo) {
1316 ir_node *alloc = ainfo->alloc;
1317 const char *name = get_type_name (ainfo->tp);
1318 const char *color = "red1";
1320 /* if (0 == ginfo->allocs_seen) { */
1322 fprintf (dot, "\talloc_0x%08x_%i [label=\"%s\", color=\"%s\"]\n",
1323 (int) alloc, graph_no, name, color);
1326 fprintf (dot, "\tgraph_%i -> alloc_0x%08x_%i\n", graph_no, (int) alloc, graph_no);
1328 ainfo = ainfo->prev;
1331 if (0 == ginfo->allocs_seen) {
1332 ginfo->allocs_seen = 1;
1335 fprintf (dot, "\t/* done with graph of \"%s\" */\n\n", name);
1338 ginfo->ecg_seen = 0;
1344 Count how many nodes the ECG will have
1346 static char spaces [BUF_SIZE];
1348 static void ecg_ecg_count (ir_graph *graph)
1350 graph_info_t *ginfo = (graph_info_t*) pmap_get (graph_infos, graph);
1352 if (0 != ginfo->ecg_seen) {
1357 if (_depth > _max_depth) {
1358 _max_depth = _depth;
1361 fprintf (stdout, "_max_depth = %i\n", _max_depth);
1362 fprintf (stdout, "\tn_graphs: %i\n", _graphs);
1366 assert (0L <= _graphs);
1369 if (0 == (_graphs % 1000000)) {
1370 fprintf (stdout, "\tn_graphs: %i\n", _graphs);
1371 fprintf (stdout, "_depth = %i\n", _depth);
1375 const int graph_no = _graphs ++;
1376 ginfo->ecg_seen = graph_no;
1378 fprintf (stdout, "%sMethod \"%s.%s\"\n",
1379 spaces + BUF_SIZE - _depth,
1380 get_type_name (get_entity_owner (get_irg_entity (graph))),
1381 get_entity_name (get_irg_entity (graph)));
1383 call_info_t *cinfo = ginfo->calls;
1384 while (NULL != cinfo) {
1386 callEd_info_t *ced = cinfo->callEds;
1388 fprintf (stdout, "%sCall \"0x%08x\"\n",
1389 spaces + BUF_SIZE - _depth,
1392 while (NULL != ced) {
1393 ir_graph *callEd_graph = ced->callEd;
1395 fprintf (stdout, "%sCall Target \"%s.%s\"\n",
1396 spaces + BUF_SIZE - _depth,
1397 get_type_name (get_entity_owner (get_irg_entity (callEd_graph))),
1398 get_entity_name (get_irg_entity (callEd_graph)));
1400 ecg_ecg_count (callEd_graph);
1403 } /* done all calEds(call) */
1404 cinfo = cinfo->prev;
1405 } /* done all calls(graph) */
1407 ginfo->ecg_seen = 0;
1411 /* ====================
1413 ==================== */
1416 Initialise our data structures.
1418 void ecg_init (int typalise)
1420 do_typalise = typalise;
1422 graph_infos = pmap_create ();
1434 for (i = 0; i < get_irp_n_irgs (); i++) {
1435 ir_graph *graph = get_irp_irg (i);
1437 graph_info_t *info = pmap_get (graph_infos, graph);
1438 call_info_t *cinfo = info->calls;
1440 while (NULL != cinfo) {
1441 free (cinfo->callEds);
1444 callEd_info_t *ced = cinfo->callEds;
1446 while (NULL != ced) {
1447 callEd_info_t *nced = ced->prev;
1454 /* Todo: delete callEds */
1455 cinfo->callEds = NULL;
1458 cinfo = cinfo->prev;
1462 pmap_insert (graph_infos, graph, NULL);
1466 pmap_destroy (graph_infos);
1468 /* BEGIN mild paranoia mode */
1470 /* END mild paranoia mode */
1474 Show what we have found.
1480 FILE *dot = fopen ("calls.dot", "w");
1482 fprintf (dot, "digraph \"calls\" {\n");
1483 fprintf (dot, "\tnode [shape = \"record\", style = \"filled\"];\n");
1484 fprintf (dot, "\tedge [color = \"black\"];\n");
1485 fprintf (dot, "\n");
1486 fprintf (dot, "\tsize = \"11, 7\";\n");
1487 fprintf (dot, "\trotate = \"90\";\n");
1488 fprintf (dot, "\tratio = \"fill\";\n");
1489 fprintf (dot, "\trankdir = \"LR\";\n");
1490 fprintf (dot, "\n");
1492 for (i = 0; i < get_irp_n_irgs (); i++) {
1493 ir_graph *graph = get_irp_irg (i);
1494 graph_info_t *info = (graph_info_t*) pmap_get (graph_infos, graph);
1496 const char *name = get_irg_entity (graph) ?
1497 get_entity_name (get_irg_entity (graph)) : "noEntity";
1499 const char *oname = get_type_name
1500 (get_entity_owner (get_irg_entity (graph)));
1503 (get_entity_stickyness
1504 (get_irg_entity (graph)) == stickyness_sticky) ?
1505 "red3" : "lightyellow";
1507 fprintf (dot, "\t/* graph_0x%08x (\"%s\") */\n", (int) graph, name);
1509 "\tgraph_0x%08x [label=\"%s\\l%s\", color=\"%s\"];\n",
1510 (int) graph, oname, name, color);
1511 fprintf (dot, "\n");
1513 call_info_t *cinfo = info->calls;
1515 fprintf (dot, "\t/* now the calls */\n");
1517 fprintf (dot, "\t/* no calls */\n");
1520 while (NULL != cinfo) {
1521 ir_node *call = cinfo->call;
1523 fprintf (dot, "\t/* call_0x%08x */\n", (int) call);
1524 fprintf (dot, "\tcall_0x%08x [label=\"call\\l0x%08x\"];\n",
1525 (int) call, (int) call);
1526 fprintf (dot, "\tgraph_0x%08x -> call_0x%08x;\n",
1527 (int) graph, (int) call);
1529 callEd_info_t *ced = cinfo->callEds;
1530 while (NULL != ced) {
1531 fprintf (dot, "\tcall_0x%08x -> graph_0x%08x;\n",
1532 (int) call, (int) ced->callEd);
1535 fprintf (dot, "\n");
1537 cinfo = cinfo->prev;
1539 fprintf (dot, "\n");
1541 alloc_info_t *ainfo = info->allocs;
1543 fprintf (dot, "\t/* now the allocs */\n");
1545 fprintf (dot, "\t/* no allocs */\n");
1549 while (NULL != ainfo) {
1550 ir_node *alloc = ainfo->alloc;
1551 const char *name = get_type_name (ainfo->tp);
1552 const char *color = "red1";
1554 fprintf (dot, "\talloc_0x%08x [label=\"%s\", color=\"%s\"]\n",
1555 (int) alloc, name, color);
1556 fprintf (dot, "\tgraph_0x%08x -> alloc_0x%08x\n",
1557 (int) graph, (int) alloc);
1559 ainfo = ainfo->prev;
1562 fprintf (dot, "}\n");
1565 fprintf (stdout, " max_callEds: %i\n", _max_callEds);
1566 fprintf (stdout, " max_callEds_callR: \"%s\"\n",
1567 get_entity_name (_max_callEds_callR));
1575 Experimental: Print the ecg
1582 ir_graph *main_graph = get_irp_main_irg ();
1585 memset (spaces, '.', BUF_SIZE);
1586 spaces [BUF_SIZE-1] = '\0';
1588 ecg_ecg_count (main_graph);
1589 fprintf (stdout, "n_graphs: %i\n", _graphs);
1590 fprintf (stdout, "max_depth = %i\n", _max_depth);
1598 FILE *dot = fopen ("ecg.dot", "w");
1600 fprintf (dot, "digraph \"ecg\" {\n");
1601 fprintf (dot, "\tnode [shape = \"record\", style = \"filled\"];\n");
1602 fprintf (dot, "\tedge [color = \"black\"];\n");
1603 fprintf (dot, "\n");
1604 fprintf (dot, "\tsize = \"11, 7\";\n");
1605 fprintf (dot, "\trotate = \"90\";\n");
1606 fprintf (dot, "\tratio = \"fill\";\n");
1607 fprintf (dot, "\trankdir = \"LR\";\n");
1608 fprintf (dot, "\n");
1610 /* ir_graph *main_graph = get_irp_main_irg (); */
1611 ecg_ecg_graph (dot, main_graph);
1613 fprintf (dot, "\t/* Grand Total: */\n");
1614 fprintf (dot, "\t/* calls: %i */\n", (int) _calls);
1615 fprintf (dot, "\t/* graphs: %i */\n", (int) _graphs);
1616 fprintf (dot, "\t/* allocs: %i */\n", (int) _allocs);
1617 fprintf (dot, "\t/* (sales tax not included) */\n");
1619 fprintf (dot, "}\n");
1628 Revision 1.6 2004/10/18 12:47:19 liekweg
1631 Revision 1.5 2004/10/14 11:31:28 liekweg
1634 Revision 1.4 2004/10/12 11:02:01 liekweg