2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Write vcg representation of firm to file.
23 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Hubert Schmidt,
38 #include "irgraph_t.h"
52 #include "callgraph.h"
53 #include "irextbb_t.h"
55 #include "dbginfo_t.h"
69 /** Dump only irgs with names that start with this prefix. */
70 static ident *dump_file_filter_id = NULL;
72 static ir_dump_flags_t flags =
73 ir_dump_flag_blocks_as_subgraphs |
74 ir_dump_flag_keepalive_edges |
75 ir_dump_flag_ld_names |
76 ir_dump_flag_back_edges |
77 ir_dump_flag_consts_local |
78 ir_dump_flag_analysed_types |
79 ir_dump_flag_entities_in_hierarchy;
81 static ird_color_t overrule_nodecolor = ird_color_default_node;
83 /** The vcg node attribute hook. */
84 static dump_node_vcgattr_func dump_node_vcgattr_hook = NULL;
85 /** The vcg edge attribute hook. */
86 static dump_edge_vcgattr_func dump_edge_vcgattr_hook = NULL;
87 /** The vcg dump block edge hook */
88 static dump_node_edge_func dump_block_edge_hook = NULL;
89 /** The vcg dump node edge hook. */
90 static dump_node_edge_func dump_node_edge_hook = NULL;
92 void set_dump_node_edge_hook(dump_node_edge_func func)
94 dump_node_edge_hook = func;
97 dump_node_edge_func get_dump_node_edge_hook(void)
99 return dump_node_edge_hook;
102 void set_dump_block_edge_hook(dump_node_edge_func func)
104 dump_block_edge_hook = func;
107 dump_node_edge_func get_dump_block_edge_hook(void)
109 return dump_node_edge_hook;
112 void set_dump_node_vcgattr_hook(dump_node_vcgattr_func hook)
114 dump_node_vcgattr_hook = hook;
117 void set_dump_edge_vcgattr_hook(dump_edge_vcgattr_func hook)
119 dump_edge_vcgattr_hook = hook;
122 void ir_set_dump_flags(ir_dump_flags_t new_flags)
127 void ir_add_dump_flags(ir_dump_flags_t new_flags)
132 void ir_remove_dump_flags(ir_dump_flags_t to_remove)
137 ir_dump_flags_t ir_get_dump_flags(void)
142 /** Returns 0 if dump_out_edge_flag or dump_loop_information_flag
143 * are set, else returns dump_const_local_flag.
145 static bool get_opt_dump_const_local(void)
147 return (flags & ir_dump_flag_out_edges)
148 || (flags & ir_dump_flag_loops)
149 || (flags & ir_dump_flag_consts_local)
150 || (flags & ir_dump_flag_iredges);
153 static char *dump_filter;
155 void ir_set_dump_filter(const char *new_filter)
158 dump_filter = xstrdup(new_filter);
161 const char *ir_get_dump_filter(void)
166 int ir_should_dump(const char *name)
170 if (dump_filter == NULL || dump_filter[0] == '\0')
173 for (n = name, f = dump_filter; *f != '\0' && *n != '\0';
181 /* -------------- some extended helper functions ----------------- */
183 const char *get_mode_name_ex(const ir_mode *mode, int *bad)
186 return get_mode_name(mode);
192 #define CUSTOM_COLOR_BASE 100
193 static const char *color_names[ird_color_count];
194 static const char *color_rgb[ird_color_count];
195 static struct obstack color_obst;
197 /** define a custom color. */
198 static void custom_color(int num, const char *rgb_def)
200 assert(num < ird_color_count);
201 obstack_printf(&color_obst, "%d", CUSTOM_COLOR_BASE + num);
202 obstack_1grow(&color_obst, '\0');
204 color_rgb[num] = rgb_def;
205 color_names[num] = obstack_finish(&color_obst);
208 /** Define a named color. */
209 static void named_color(int num, const char *name)
211 assert(num < ird_color_count);
212 color_rgb[num] = NULL;
213 color_names[num] = name;
216 /** Initializes the used colors. */
217 static void init_colors(void)
219 static int initialized = 0;
223 obstack_init(&color_obst);
225 custom_color(ird_color_prog_background, "204 204 204");
226 custom_color(ird_color_block_background, "255 255 0");
227 custom_color(ird_color_dead_block_background, "190 150 150");
228 named_color(ird_color_block_inout, "lightblue");
229 named_color(ird_color_default_node, "white");
230 custom_color(ird_color_memory, "153 153 255");
231 custom_color(ird_color_controlflow, "255 153 153");
232 custom_color(ird_color_const, "204 255 255");
233 custom_color(ird_color_proj, "255 255 153");
234 custom_color(ird_color_uses_memory, "153 153 255");
235 custom_color(ird_color_phi, "105 255 105");
236 custom_color(ird_color_anchor, "100 100 255");
237 named_color(ird_color_error, "red");
238 custom_color(ird_color_entity, "204 204 255");
244 * Prints the VCG color to a file.
246 static void print_vcg_color(FILE *out, ird_color_t color)
248 assert(color < ird_color_count);
249 fprintf(out, "color:%s", color_names[color]);
253 * Prints the edge kind of a given IR node.
255 * Projs should be dumped near their predecessor, so they get "nearedge".
257 static void print_node_edge_kind(FILE *out, ir_node *node)
260 fprintf(out, "nearedge: ");
262 fprintf(out, "edge: ");
267 * Prints the edge from a type S to a type T with additional info fmt, ...
270 static void print_type_type_edge(FILE *F, const ir_type *S, const ir_type *T, const char *fmt, ...)
275 fprintf(F, "edge: { sourcename: "); PRINT_TYPEID(S);
276 fprintf(F, " targetname: "); PRINT_TYPEID(T);
277 vfprintf(F, fmt, ap);
283 * Prints the edge from a type tp to an entity ent with additional info fmt, ...
286 static void print_type_ent_edge(FILE *F, const ir_type *tp, const ir_entity *ent, const char *fmt, ...)
291 fprintf(F, "edge: { sourcename: "); PRINT_TYPEID(tp);
292 fprintf(F, " targetname: \""); PRINT_ENTID(ent); fprintf(F, "\"");
293 vfprintf(F, fmt, ap);
299 * Prints the edge from an entity ent1 to an entity ent2 with additional info fmt, ...
302 static void print_ent_ent_edge(FILE *F, const ir_entity *ent1, const ir_entity *ent2, int backedge, ird_color_t color, const char *fmt, ...)
308 fprintf(F, "backedge: { sourcename: \"");
310 fprintf(F, "edge: { sourcename: \"");
312 fprintf(F, "\" targetname: \""); PRINT_ENTID(ent2); fprintf(F, "\"");
313 vfprintf(F, fmt, ap);
315 if (color != (ird_color_t) -1)
316 print_vcg_color(F, color);
322 * Prints the edge from an entity ent to a type tp with additional info fmt, ...
325 static void print_ent_type_edge(FILE *F, const ir_entity *ent, const ir_type *tp, const char *fmt, ...)
330 fprintf(F, "edge: { sourcename: \""); PRINT_ENTID(ent);
331 fprintf(F, "\" targetname: "); PRINT_TYPEID(tp);
332 vfprintf(F, fmt, ap);
338 * Prints the edge from a node irn to a type tp with additional info fmt, ...
341 static void print_node_type_edge(FILE *F, const ir_node *irn, ir_type *tp, const char *fmt, ...)
346 fprintf(F, "edge: { sourcename: \""); PRINT_NODEID(irn);
347 fprintf(F, "\" targetname: "); PRINT_TYPEID(tp);
348 vfprintf(F, fmt, ap);
354 * Prints the edge from a node irn to an entity ent with additional info fmt, ...
357 static void print_node_ent_edge(FILE *F, const ir_node *irn, const ir_entity *ent, const char *fmt, ...)
362 fprintf(F, "edge: { sourcename: \""); PRINT_NODEID(irn);
363 fprintf(F, "\" targetname: \""); PRINT_ENTID(ent);
365 vfprintf(F, fmt, ap);
371 * Prints the edge from an entity ent to a node irn with additional info fmt, ...
374 static void print_ent_node_edge(FILE *F, const ir_entity *ent, const ir_node *irn, const char *fmt, ...)
379 fprintf(F, "edge: { sourcename: \""); PRINT_ENTID(ent);
380 fprintf(F, "\" targetname: \""); PRINT_NODEID(irn); fprintf(F, "\"");
381 vfprintf(F, fmt, ap);
387 * Prints the edge from a type tp to an enumeration item item with additional info fmt, ...
390 static void print_enum_item_edge(FILE *F, const ir_type *tp, int item, const char *fmt, ...)
395 fprintf(F, "edge: { sourcename: "); PRINT_TYPEID(tp);
396 fprintf(F, " targetname: \""); PRINT_ITEMID(tp, item); fprintf(F, "\" ");
397 vfprintf(F, fmt, ap);
402 /*-----------------------------------------------------------------*/
403 /* global and ahead declarations */
404 /*-----------------------------------------------------------------*/
406 static void dump_node_with_edges(ir_node *n, void *env);
407 static void dump_loop_nodes_into_graph(FILE *F, ir_graph *irg);
409 /*-----------------------------------------------------------------*/
410 /* Helper functions. */
411 /*-----------------------------------------------------------------*/
414 * This map is used as a private link attr to be able to call dumper
415 * anywhere without destroying link fields.
417 static pmap *irdump_link_map = NULL;
419 /** NOT A STANDARD LIBFIRM INIT METHOD
421 * We do not want to integrate dumping into libfirm, i.e., if the dumpers
422 * are off, we want to have as few interferences as possible. Therefore the
423 * initialization is performed lazily and not called from within init_firm.
425 * Creates the link attribute map. */
426 static void init_irdump(void)
428 /* We need a new, empty map. */
429 if (irdump_link_map) pmap_destroy(irdump_link_map);
430 irdump_link_map = pmap_create();
431 if (!dump_file_filter_id)
432 dump_file_filter_id = new_id_from_str("");
436 * Returns the private link field.
438 static void *ird_get_irn_link(const ir_node *n)
441 if (irdump_link_map == NULL)
444 if (pmap_contains(irdump_link_map, n))
445 res = pmap_get(irdump_link_map, n);
450 * Sets the private link field.
452 static void ird_set_irn_link(const ir_node *n, void *x)
454 if (irdump_link_map == NULL)
456 pmap_insert(irdump_link_map, n, x);
460 * Gets the private link field of an irg.
462 static void *ird_get_irg_link(const ir_graph *irg)
465 if (irdump_link_map == NULL)
468 if (pmap_contains(irdump_link_map, irg))
469 res = pmap_get(irdump_link_map, irg);
474 * Sets the private link field of an irg.
476 static void ird_set_irg_link(const ir_graph *irg, void *x)
478 if (irdump_link_map == NULL)
480 pmap_insert(irdump_link_map, irg, x);
484 * Walker, clears the private link field.
486 static void clear_link(ir_node *node, void *env)
489 ird_set_irn_link(node, NULL);
493 * If the entity has a ld_name, returns it if the dump_ld_name is set,
494 * else returns the name of the entity.
496 static const char *_get_ent_dump_name(const ir_entity *ent, bool dump_ld_name)
499 return "<NULL entity>";
501 /* Don't use get_entity_ld_ident (ent) as it computes the mangled name! */
502 if (ent->ld_name != NULL)
503 return get_id_str(ent->ld_name);
505 return get_id_str(ent->name);
508 const char *get_ent_dump_name(const ir_entity *ent)
510 return _get_ent_dump_name(ent, flags & ir_dump_flag_ld_names);
513 const char *get_irg_dump_name(const ir_graph *irg)
515 /* Don't use get_entity_ld_ident (ent) as it computes the mangled name! */
516 return _get_ent_dump_name(get_irg_entity(irg), true);
520 * Returns non-zero if a node is in floating state.
522 static int node_floats(const ir_node *n)
524 return ((get_irn_pinned(n) == op_pin_state_floats) &&
525 (get_irg_pinned(current_ir_graph) == op_pin_state_floats));
529 * Walker that visits the anchors
531 static void ird_walk_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env)
533 if ((flags & ir_dump_flag_all_anchors)
534 || ((flags & ir_dump_flag_iredges) && edges_activated(irg))) {
535 irg_walk_anchors(irg, pre, post, env);
537 irg_walk_graph(irg, pre, post, env);
542 * Walker, allocates an array for all blocks and puts it's nodes non-floating
543 * nodes into this array.
545 static void collect_node(ir_node *node, void *env)
550 || (get_op_flags(get_irn_op(node)) & irop_flag_dump_noblock)) {
551 ir_node ** arr = (ir_node **) ird_get_irg_link(get_irn_irg(node));
552 if (!arr) arr = NEW_ARR_F(ir_node *, 0);
553 ARR_APP1(ir_node *, arr, node);
554 ird_set_irg_link(get_irn_irg(node), arr); /* arr is an l-value, APP_ARR might change it! */
556 ir_node * block = get_nodes_block(node);
559 /* this node is in a Bad block, so we must place it into the graph's list */
560 ir_node ** arr = (ir_node **) ird_get_irg_link(get_irn_irg(node));
561 if (!arr) arr = NEW_ARR_F(ir_node *, 0);
562 ARR_APP1(ir_node *, arr, node);
563 ird_set_irg_link(get_irn_irg(node), arr); /* arr is an l-value, APP_ARR might change it! */
565 ird_set_irn_link(node, ird_get_irn_link(block));
566 ird_set_irn_link(block, node);
571 /** Construct lists to walk ir block-wise.
573 * Collects all blocks, nodes not op_pin_state_pinned,
574 * Bad, NoMem and Unknown into a flexible array in link field of
575 * irg they belong to. Sets the irg link field to NULL in all
576 * graphs not visited.
577 * Free the list with DEL_ARR_F().
579 static ir_node **construct_block_lists(ir_graph *irg)
582 #ifdef INTERPROCEDURAL_VIEW
583 int rem_view = get_interprocedural_view();
585 int walk_flag = ir_resources_reserved(irg) & IR_RESOURCE_IRN_VISITED;
586 ir_graph *rem = current_ir_graph;
588 current_ir_graph = irg;
591 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
594 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
595 ird_set_irg_link(get_irp_irg(i), NULL);
597 ird_walk_graph(current_ir_graph, clear_link, collect_node, current_ir_graph);
599 #ifdef INTERPROCEDURAL_VIEW
600 /* Collect also EndReg and EndExcept. We do not want to change the walker. */
601 set_interprocedural_view(0);
604 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
605 irg_walk(get_irg_end_reg(current_ir_graph), clear_link, collect_node, current_ir_graph);
606 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
607 irg_walk(get_irg_end_except(current_ir_graph), clear_link, collect_node, current_ir_graph);
609 #ifdef INTERPROCEDURAL_VIEW
610 set_interprocedural_view(rem_view);
614 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
617 current_ir_graph = rem;
618 return ird_get_irg_link(irg);
621 typedef struct _list_tuple {
623 ir_extblk **extbb_list;
626 /** Construct lists to walk IR extended block-wise.
627 * Free the lists in the tuple with DEL_ARR_F(). Sets the irg link field to
628 * NULL in all graphs not visited.
630 static list_tuple *construct_extblock_lists(ir_graph *irg)
632 ir_node **blk_list = construct_block_lists(irg);
634 ir_graph *rem = current_ir_graph;
635 list_tuple *lists = XMALLOC(list_tuple);
637 current_ir_graph = irg;
639 lists->blk_list = NEW_ARR_F(ir_node *, 0);
640 lists->extbb_list = NEW_ARR_F(ir_extblk *, 0);
642 inc_irg_block_visited(irg);
643 for (i = ARR_LEN(blk_list) - 1; i >= 0; --i) {
646 if (is_Block(blk_list[i])) {
647 ext = get_Block_extbb(blk_list[i]);
649 if (extbb_not_visited(ext)) {
650 ARR_APP1(ir_extblk *, lists->extbb_list, ext);
651 mark_extbb_visited(ext);
654 ARR_APP1(ir_node *, lists->blk_list, blk_list[i]);
658 current_ir_graph = rem;
659 ird_set_irg_link(irg, lists);
663 void dump_node_opcode(FILE *F, ir_node *n)
665 const ir_op_ops *ops = get_op_ops(get_irn_op(n));
667 /* call the dump_node operation if available */
668 if (ops->dump_node) {
669 ops->dump_node(F, n, dump_node_opcode_txt);
673 /* implementation for default nodes */
674 switch (get_irn_opcode(n)) {
676 switch (get_SymConst_kind(n)) {
677 case symconst_addr_ent:
678 fprintf(F, "SymC &%s", get_entity_name(get_SymConst_entity(n)));
680 case symconst_ofs_ent:
681 fprintf(F, "SymC %s offset", get_entity_name(get_SymConst_entity(n)));
683 case symconst_type_tag:
684 ir_fprintf(F, "SymC %+F tag", get_SymConst_type(n));
686 case symconst_type_size:
687 ir_fprintf(F, "SymC %+F size", get_SymConst_type(n));
689 case symconst_type_align:
690 ir_fprintf(F, "SymC %+F align", get_SymConst_type(n));
692 case symconst_enum_const:
693 fprintf(F, "SymC %s enum", get_enumeration_const_name(get_SymConst_enum(n)));
698 #ifdef INTERPROCEDURAL_VIEW
700 if (!get_interprocedural_view())
708 ir_node *pred = get_Proj_pred(n);
710 if (get_irn_opcode(pred) == iro_Cond
711 && get_Proj_proj(n) == get_Cond_default_proj(pred)
712 && get_irn_mode(get_Cond_selector(pred)) != mode_b)
713 fprintf(F, "defProj");
718 #ifdef INTERPROCEDURAL_VIEW
723 if (get_interprocedural_view()) {
724 fprintf(F, "%s %s", get_irn_opname(n), get_ent_dump_name(get_irg_entity(get_irn_irg(n))));
730 case iro_CallBegin: {
731 ir_node *addr = get_CallBegin_ptr(n);
732 ir_entity *ent = NULL;
734 ent = get_Sel_entity(addr);
735 else if (is_Global(addr))
736 ent = get_Global_entity(addr);
737 fprintf(F, "%s", get_irn_opname(n));
738 if (ent) fprintf(F, " %s", get_entity_name(ent));
742 if (get_Load_align(n) == align_non_aligned)
744 fprintf(F, "%s[%s]", get_irn_opname(n), get_mode_name_ex(get_Load_mode(n), NULL));
747 if (get_Store_align(n) == align_non_aligned)
749 fprintf(F, "%s", get_irn_opname(n));
752 if (is_Block_dead(n))
754 if (n == get_irg_start_block(get_irn_irg(n)))
756 if (n == get_irg_end_block(get_irn_irg(n)))
758 fprintf(F, "%s%s", get_irn_opname(n),
759 (flags & ir_dump_flag_show_marks) ? (get_Block_mark(n) ? "*" : "") : "");
762 if (get_Conv_strict(n))
763 fprintf(F, "strict");
764 fprintf(F, "%s", get_irn_opname(n));
767 fprintf(F, "%s", get_irn_opname(n));
768 if (get_Div_no_remainder(n))
770 fprintf(F, "[%s]", get_mode_name_ex(get_Div_resmode(n), NULL));
773 fprintf(F, "%s[%s]", get_irn_opname(n), get_mode_name_ex(get_Mod_resmode(n), NULL));
776 fprintf(F, "%s[%s]", get_irn_opname(n), get_mode_name_ex(get_DivMod_resmode(n), NULL));
779 fprintf(F, "%s[%s]", get_irn_opname(n), get_builtin_kind_name(get_Builtin_kind(n)));
784 fprintf(F, "%s", get_irn_opname(n));
789 * Dump the mode of a node n to a file F.
790 * Ignore modes that are "always known".
792 static void dump_node_mode(FILE *F, ir_node *n)
794 const ir_op_ops *ops = get_op_ops(get_irn_op(n));
798 /* call the dump_node operation if available */
799 if (ops->dump_node) {
800 ops->dump_node(F, n, dump_node_mode_txt);
804 /* default implementation */
805 iro = get_irn_opcode(n);
817 mode = get_irn_mode(n);
819 if (mode != NULL && mode != mode_BB && mode != mode_ANY && mode != mode_BAD &&
820 (mode != mode_T || iro == iro_Proj))
821 fprintf(F, "%s", get_mode_name_ex(mode, NULL));
826 * Dump the type of a node n to a file F if it's known.
828 static int dump_node_typeinfo(FILE *F, ir_node *n)
832 if (ir_get_dump_flags() & ir_dump_flag_analysed_types) {
833 if (get_irg_typeinfo_state(current_ir_graph) == ir_typeinfo_consistent ||
834 get_irg_typeinfo_state(current_ir_graph) == ir_typeinfo_inconsistent) {
835 ir_type *tp = get_irn_typeinfo_type(n);
836 if (tp != firm_none_type) {
837 ir_fprintf(F, "[%+F]", tp);
846 typedef struct _pns_lookup {
847 long nr; /**< the proj number */
848 const char *name; /**< the name of the Proj */
851 typedef struct _proj_lookup {
852 ir_opcode code; /**< the opcode of the Proj predecessor */
853 unsigned num_data; /**< number of data entries */
854 const pns_lookup_t *data; /**< the data */
857 /** the lookup table for Proj(Start) names */
858 static const pns_lookup_t start_lut[] = {
859 #define X(a) { pn_Start_##a, #a }
867 /** the lookup table for Proj(Cond) names */
868 static const pns_lookup_t cond_lut[] = {
869 #define X(a) { pn_Cond_##a, #a }
875 /** the lookup table for Proj(Call) names */
876 static const pns_lookup_t call_lut[] = {
877 #define X(a) { pn_Call_##a, #a }
886 /** the lookup table for Proj(Quot) names */
887 static const pns_lookup_t quot_lut[] = {
888 #define X(a) { pn_Quot_##a, #a }
896 /** the lookup table for Proj(DivMod) names */
897 static const pns_lookup_t divmod_lut[] = {
898 #define X(a) { pn_DivMod_##a, #a }
907 /** the lookup table for Proj(Div) names */
908 static const pns_lookup_t div_lut[] = {
909 #define X(a) { pn_Div_##a, #a }
917 /** the lookup table for Proj(Mod) names */
918 static const pns_lookup_t mod_lut[] = {
919 #define X(a) { pn_Mod_##a, #a }
927 /** the lookup table for Proj(Load) names */
928 static const pns_lookup_t load_lut[] = {
929 #define X(a) { pn_Load_##a, #a }
937 /** the lookup table for Proj(Store) names */
938 static const pns_lookup_t store_lut[] = {
939 #define X(a) { pn_Store_##a, #a }
946 /** the lookup table for Proj(Alloc) names */
947 static const pns_lookup_t alloc_lut[] = {
948 #define X(a) { pn_Alloc_##a, #a }
956 /** the lookup table for Proj(CopyB) names */
957 static const pns_lookup_t copyb_lut[] = {
958 #define X(a) { pn_CopyB_##a, #a }
965 /** the lookup table for Proj(InstOf) names */
966 static const pns_lookup_t instof_lut[] = {
967 #define X(a) { pn_InstOf_##a, #a }
975 /** the lookup table for Proj(Raise) names */
976 static const pns_lookup_t raise_lut[] = {
977 #define X(a) { pn_Raise_##a, #a }
983 /** the lookup table for Proj(Bound) names */
984 static const pns_lookup_t bound_lut[] = {
985 #define X(a) { pn_Bound_##a, #a }
993 /** the Proj lookup table */
994 static const proj_lookup_t proj_lut[] = {
995 #define E(a) ARRAY_SIZE(a), a
996 { iro_Start, E(start_lut) },
997 { iro_Cond, E(cond_lut) },
998 { iro_Call, E(call_lut) },
999 { iro_Quot, E(quot_lut) },
1000 { iro_DivMod, E(divmod_lut) },
1001 { iro_Div, E(div_lut) },
1002 { iro_Mod, E(mod_lut) },
1003 { iro_Load, E(load_lut) },
1004 { iro_Store, E(store_lut) },
1005 { iro_Alloc, E(alloc_lut) },
1006 { iro_CopyB, E(copyb_lut) },
1007 { iro_InstOf, E(instof_lut) },
1008 { iro_Raise, E(raise_lut) },
1009 { iro_Bound, E(bound_lut) }
1014 * Dump additional node attributes of some nodes to a file F.
1016 static void dump_node_nodeattr(FILE *F, ir_node *n)
1021 const ir_op_ops *ops = get_op_ops(get_irn_op(n));
1023 /* call the dump_node operation if available */
1024 if (ops->dump_node) {
1025 ops->dump_node(F, n, dump_node_nodeattr_txt);
1029 switch (get_irn_opcode(n)) {
1031 ir_fprintf(F, "%T ", get_Const_tarval(n));
1035 pred = get_Proj_pred(n);
1036 proj_nr = get_Proj_proj(n);
1037 #ifdef INTERPROCEDURAL_VIEW
1040 code = get_irn_opcode(pred);
1042 if (code == iro_Cmp)
1043 fprintf(F, "%s ", get_pnc_string(get_Proj_proj(n)));
1044 else if (code == iro_Proj && get_irn_opcode(get_Proj_pred(pred)) == iro_Start)
1045 fprintf(F, "Arg %ld ", proj_nr);
1046 else if (code == iro_Cond && get_irn_mode(get_Cond_selector(pred)) != mode_b)
1047 fprintf(F, "%ld ", proj_nr);
1049 unsigned i, j, f = 0;
1051 for (i = 0; i < ARRAY_SIZE(proj_lut); ++i) {
1052 if (code == proj_lut[i].code) {
1053 for (j = 0; j < proj_lut[i].num_data; ++j) {
1054 if (proj_nr == proj_lut[i].data[j].nr) {
1055 fprintf(F, "%s ", proj_lut[i].data[j].name);
1064 fprintf(F, "%ld ", proj_nr);
1065 if (code == iro_Cond && get_Cond_jmp_pred(pred) != COND_JMP_PRED_NONE) {
1066 if (proj_nr == pn_Cond_false && get_Cond_jmp_pred(pred) == COND_JMP_PRED_FALSE)
1067 fprintf(F, "PRED ");
1068 if (proj_nr == pn_Cond_true && get_Cond_jmp_pred(pred) == COND_JMP_PRED_TRUE)
1069 fprintf(F, "PRED ");
1074 proj_nr = get_Filter_proj(n);
1075 #ifdef INTERPROCEDURAL_VIEW
1076 if (! get_interprocedural_view()) {
1078 pred = get_Filter_pred(n);
1082 fprintf(F, "%ld ", proj_nr);
1085 fprintf(F, "%s ", get_ent_dump_name(get_Sel_entity(n)));
1088 ir_fprintf(F, "(%+F)", get_Cast_type(n));
1091 fprintf(F, "%s ", get_pnc_string(get_Confirm_cmp(n)));
1094 ir_fprintf(F, "(%+F)", get_CopyB_type(n));
1103 #include "execution_frequency.h"
1105 static void dump_node_ana_vals(FILE *F, ir_node *n)
1109 #ifdef INTERPROCEDURAL_VIEW
1110 fprintf(F, " %lf*(%2.0lf + %2.0lf) = %2.0lf ",
1111 get_irn_exec_freq(n),
1112 get_irg_method_execution_frequency(get_irn_irg(n)),
1113 pow(5, get_irg_recursion_depth(get_irn_irg(n))),
1114 get_irn_exec_freq(n) * (get_irg_method_execution_frequency(get_irn_irg(n)) + pow(5, get_irg_recursion_depth(get_irn_irg(n))))
1120 void dump_node_label(FILE *F, ir_node *n)
1122 dump_node_opcode(F, n);
1124 dump_node_mode(F, n);
1126 dump_node_typeinfo(F, n);
1127 dump_node_nodeattr(F, n);
1128 if (flags & ir_dump_flag_number_label) {
1129 fprintf(F, "%ld", get_irn_node_nr(n));
1131 if (flags & ir_dump_flag_idx_label) {
1132 fprintf(F, ":%u", get_irn_idx(n));
1136 void dump_vrp_info(FILE *F, ir_node *n)
1138 vrp_attr *vrp = vrp_get_info(n);
1143 fprintf(F, "range_type: %d\n", (int) vrp->range_type);
1144 if (vrp->range_type == VRP_RANGE || vrp->range_type ==
1146 ir_fprintf(F, "range_bottom: %F\n",vrp->range_bottom);
1147 ir_fprintf(F, "range_top: %F\n", vrp->range_top);
1149 ir_fprintf(F, "bits_set: %T\n", vrp->bits_set);
1150 ir_fprintf(F, "bits_not_set: %T\n", vrp->bits_not_set);
1154 * Dumps the attributes of a node n into the file F.
1155 * Currently this is only the color of a node.
1157 static void dump_node_vcgattr(FILE *F, ir_node *node, ir_node *local, int bad)
1163 print_vcg_color(F, ird_color_error);
1167 if (dump_node_vcgattr_hook != NULL) {
1168 dump_node_vcgattr_hook(F, node, local);
1172 n = local ? local : node;
1174 if (overrule_nodecolor != ird_color_default_node) {
1175 print_vcg_color(F, overrule_nodecolor);
1179 mode = get_irn_mode(n);
1180 if (mode == mode_M) {
1181 print_vcg_color(F, ird_color_memory);
1184 if (mode == mode_X) {
1185 print_vcg_color(F, ird_color_controlflow);
1189 switch (get_irn_opcode(n)) {
1194 print_vcg_color(F, ird_color_anchor);
1197 print_vcg_color(F, ird_color_error);
1200 if (is_Block_dead(n))
1201 print_vcg_color(F, ird_color_dead_block_background);
1203 print_vcg_color(F, ird_color_block_background);
1206 print_vcg_color(F, ird_color_phi);
1209 print_vcg_color(F, ird_color_memory);
1213 print_vcg_color(F, ird_color_const);
1216 print_vcg_color(F, ird_color_proj);
1219 ir_op *op = get_irn_op(node);
1221 if (is_op_constlike(op)) {
1222 print_vcg_color(F, ird_color_const);
1223 } else if (is_op_uses_memory(op)) {
1224 print_vcg_color(F, ird_color_uses_memory);
1225 } else if (is_op_cfopcode(op) || is_op_forking(op)) {
1226 print_vcg_color(F, ird_color_controlflow);
1232 void *dump_add_node_info_callback(dump_node_info_cb_t *cb, void *data)
1234 hook_entry_t *info = XMALLOC(hook_entry_t);
1236 info->hook._hook_node_info = cb;
1237 info->context = data;
1238 register_hook(hook_node_info, info);
1243 void dump_remove_node_info_callback(void *handle)
1245 hook_entry_t *info = handle;
1246 unregister_hook(hook_node_info, info);
1251 * Dump the node information of a node n to a file F.
1253 static void dump_node_info(FILE *F, ir_node *n)
1255 const ir_op_ops *ops = get_op_ops(get_irn_op(n));
1257 fprintf(F, " info1: \"");
1258 dump_irnode_to_file(F, n);
1259 /* call the dump_node operation if available */
1261 ops->dump_node(F, n, dump_node_info_txt);
1263 /* allow additional info to be added */
1264 hook_node_info(F, n);
1268 static int is_constlike_node(const ir_node *node)
1270 const ir_op *op = get_irn_op(node);
1271 return is_op_constlike(op);
1275 /** outputs the predecessors of n, that are constants, local. I.e.,
1276 generates a copy of the constant predecessors for each node called with. */
1277 static void dump_const_node_local(FILE *F, ir_node *n)
1280 if (!get_opt_dump_const_local()) return;
1282 /* Use visited flag to avoid outputting nodes twice.
1283 initialize it first. */
1284 for (i = 0; i < get_irn_arity(n); i++) {
1285 ir_node *con = get_irn_n(n, i);
1286 if (is_constlike_node(con)) {
1287 set_irn_visited(con, get_irg_visited(current_ir_graph) - 1);
1291 for (i = 0; i < get_irn_arity(n); i++) {
1292 ir_node *con = get_irn_n(n, i);
1293 if (is_constlike_node(con) && !irn_visited(con)) {
1294 mark_irn_visited(con);
1295 /* Generate a new name for the node by appending the names of
1297 fprintf(F, "node: {title: "); PRINT_CONSTID(n, con);
1298 fprintf(F, " label: \"");
1299 dump_node_label(F, con);
1301 dump_node_info(F, con);
1302 dump_node_vcgattr(F, n, con, 0);
1308 /** If the block of an edge is a const_like node, dump it local with an edge */
1309 static void dump_const_block_local(FILE *F, ir_node *n)
1313 if (!get_opt_dump_const_local()) return;
1315 blk = get_nodes_block(n);
1316 if (is_constlike_node(blk)) {
1317 /* Generate a new name for the node by appending the names of
1319 fprintf(F, "node: {title: \""); PRINT_CONSTBLKID(n, blk);
1320 fprintf(F, "\" label: \"");
1321 dump_node_label(F, blk);
1323 dump_node_info(F, blk);
1324 dump_node_vcgattr(F, n, blk, 0);
1327 fprintf(F, "edge: { sourcename: \"");
1329 fprintf(F, "\" targetname: \""); PRINT_CONSTBLKID(n,blk);
1331 if (dump_edge_vcgattr_hook) {
1333 if (dump_edge_vcgattr_hook(F, n, -1)) {
1337 fprintf(F, " " BLOCK_EDGE_ATTR "}\n");
1342 fprintf(F, "\" " BLOCK_EDGE_ATTR "}\n");
1347 * prints the error message of a node to a file F as info2.
1349 static void print_node_error(FILE *F, const char *err_msg)
1354 fprintf(F, " info2: \"%s\"", err_msg);
1358 * prints debug messages of a node to file F as info3.
1360 static void print_dbg_info(FILE *F, dbg_info *dbg)
1364 ir_dbg_info_snprint(buf, sizeof(buf), dbg);
1366 fprintf(F, " info3: \"%s\"\n", buf);
1370 static void print_type_dbg_info(FILE *F, type_dbg_info *dbg)
1380 static void dump_node(FILE *F, ir_node *n)
1385 if (get_opt_dump_const_local() && is_constlike_node(n))
1388 /* dump this node */
1389 fputs("node: {title: \"", F);
1393 fputs(" label: \"", F);
1394 bad = ! irn_vrfy_irg_dump(n, current_ir_graph, &p);
1395 dump_node_label(F, n);
1396 dump_node_ana_vals(F, n);
1397 //dump_node_ana_info(F, n);
1400 if (get_op_flags(get_irn_op(n)) & irop_flag_dump_noinput) {
1401 //fputs(" node_class:23", F);
1404 dump_node_info(F, n);
1405 print_node_error(F, p);
1406 print_dbg_info(F, get_irn_dbg_info(n));
1407 dump_node_vcgattr(F, n, NULL, bad);
1409 dump_const_node_local(F, n);
1411 if (dump_node_edge_hook)
1412 dump_node_edge_hook(F, n);
1415 /** dump the edge to the block this node belongs to */
1416 static void dump_ir_block_edge(FILE *F, ir_node *n)
1418 if (get_opt_dump_const_local() && is_constlike_node(n)) return;
1419 if (is_no_Block(n)) {
1420 ir_node *block = get_nodes_block(n);
1422 if (get_opt_dump_const_local() && is_constlike_node(block)) {
1423 dump_const_block_local(F, n);
1425 fprintf(F, "edge: { sourcename: \"");
1427 fprintf(F, "\" targetname: ");
1428 fprintf(F, "\""); PRINT_NODEID(block); fprintf(F, "\"");
1430 if (dump_edge_vcgattr_hook) {
1432 if (dump_edge_vcgattr_hook(F, n, -1)) {
1436 fprintf(F, " " BLOCK_EDGE_ATTR "}\n");
1441 fprintf(F, " " BLOCK_EDGE_ATTR "}\n");
1446 static void print_data_edge_vcgattr(FILE *F, ir_node *from, int to)
1449 * do not use get_nodes_block() here, will fail
1450 * if the irg is not pinned.
1452 if (get_irn_n(from, -1) == get_irn_n(get_irn_n(from, to), -1))
1453 fprintf(F, INTRA_DATA_EDGE_ATTR);
1455 fprintf(F, INTER_DATA_EDGE_ATTR);
1458 static void print_mem_edge_vcgattr(FILE *F, ir_node *from, int to)
1461 * do not use get_nodes_block() here, will fail
1462 * if the irg is not pinned.
1464 if (get_irn_n(from, -1) == get_irn_n(get_irn_n(from, to), -1))
1465 fprintf(F, INTRA_MEM_EDGE_ATTR);
1467 fprintf(F, INTER_MEM_EDGE_ATTR);
1470 /** Print the vcg attributes for the edge from node from to it's to's input */
1471 static void print_edge_vcgattr(FILE *F, ir_node *from, int to)
1475 if (dump_edge_vcgattr_hook)
1476 if (dump_edge_vcgattr_hook(F, from, to))
1479 if ((flags & ir_dump_flag_back_edges) && is_backedge(from, to))
1480 fprintf(F, BACK_EDGE_ATTR);
1482 switch (get_irn_opcode(from)) {
1484 fprintf(F, CF_EDGE_ATTR);
1486 case iro_Start: break;
1489 if (get_irn_mode(get_End_keepalive(from, to)) == mode_BB)
1490 fprintf(F, KEEP_ALIVE_CF_EDGE_ATTR);
1492 fprintf(F, KEEP_ALIVE_DF_EDGE_ATTR);
1496 if (is_Proj(from)) {
1497 if (get_irn_mode(from) == mode_M)
1498 print_mem_edge_vcgattr(F, from, to);
1499 else if (get_irn_mode(from) == mode_X)
1500 fprintf(F, CF_EDGE_ATTR);
1502 print_data_edge_vcgattr(F, from, to);
1504 else if (get_irn_mode(get_irn_n(from, to)) == mode_M)
1505 print_mem_edge_vcgattr(F, from, to);
1506 else if (get_irn_mode(get_irn_n(from, to)) == mode_X)
1507 fprintf(F, CF_EDGE_ATTR);
1509 print_data_edge_vcgattr(F, from, to);
1513 /** dump edges to our inputs */
1514 static void dump_ir_data_edges(FILE *F, ir_node *n)
1518 if (!(flags & ir_dump_flag_keepalive_edges) && is_End(n)) {
1519 /* the End node has only keep-alive edges */
1523 /* dump the dependency edges. */
1524 num = get_irn_deps(n);
1525 for (i = 0; i < num; ++i) {
1526 ir_node *dep = get_irn_dep(n, i);
1529 print_node_edge_kind(F, n);
1530 fprintf(F, "{sourcename: \"");
1532 fprintf(F, "\" targetname: ");
1533 if ((get_opt_dump_const_local()) && is_constlike_node(dep)) {
1534 PRINT_CONSTID(n, dep);
1540 fprintf(F, " label: \"%d\" ", i);
1541 fprintf(F, " color: darkgreen}\n");
1545 num = get_irn_arity(n);
1546 for (i = 0; i < num; i++) {
1547 ir_node *pred = get_irn_n(n, i);
1550 #ifdef INTERPROCEDURAL_VIEW
1551 if ((get_interprocedural_view() && get_irn_visited(pred) < get_irn_visited(n)))
1552 continue; /* pred not dumped */
1555 if ((flags & ir_dump_flag_back_edges) && is_backedge(n, i)) {
1556 fprintf(F, "backedge: {sourcename: \"");
1558 print_node_edge_kind(F, n);
1559 fprintf(F, "{sourcename: \"");
1562 fprintf(F, "\" targetname: ");
1563 if ((get_opt_dump_const_local()) && is_constlike_node(pred)) {
1564 PRINT_CONSTID(n, pred);
1566 fprintf(F, "\""); PRINT_NODEID(pred); fprintf(F, "\"");
1568 fprintf(F, " label: \"%d\" ", i);
1569 print_edge_vcgattr(F, n, i);
1573 if ((flags & ir_dump_flag_macroblock_edges) && is_Block(n)) {
1574 ir_node *mb = get_Block_MacroBlock(n);
1575 fprintf(F, "edge: {sourcename: \"");
1577 fprintf(F, "\" targetname: \"");
1579 fprintf(F, "\" label: \"mb\" " MACROBLOCK_EDGE_ATTR);
1587 static void dump_ir_edges(ir_node *node, void *env)
1591 const ir_edge_t *edge;
1593 foreach_out_edge(node, edge) {
1594 ir_node *succ = get_edge_src_irn(edge);
1596 print_node_edge_kind(F, succ);
1597 fprintf(F, "{sourcename: \"");
1599 fprintf(F, "\" targetname: \"");
1603 fprintf(F, " label: \"%d\" ", i);
1604 fprintf(F, OUT_EDGE_ATTR);
1611 /** Dumps a node and its edges but not the block edge */
1612 static void dump_node_wo_blockedge(ir_node *n, void *env)
1616 dump_ir_data_edges(F, n);
1619 /** Dumps a node and its edges. */
1620 static void dump_node_with_edges(ir_node *n, void *env)
1623 dump_node_wo_blockedge(n, env);
1624 if (!node_floats(n))
1625 dump_ir_block_edge(F, n);
1628 /** Dumps a const-like node. */
1629 static void dump_const_node(ir_node *n, void *env)
1631 if (is_Block(n)) return;
1632 dump_node_wo_blockedge(n, env);
1635 /***********************************************************************/
1636 /* the following routines dump the nodes/irgs bracketed to graphs. */
1637 /***********************************************************************/
1639 /** Dumps a constant expression as entity initializer, array bound ... */
1640 static void dump_const_expression(FILE *F, ir_node *value)
1642 ir_graph *rem = current_ir_graph;
1643 ir_dump_flags_t old_flags = ir_get_dump_flags();
1644 ir_remove_dump_flags(ir_dump_flag_consts_local);
1646 current_ir_graph = get_const_code_irg();
1647 irg_walk(value, dump_const_node, NULL, F);
1648 /* Decrease visited flag so that we walk with the same flag for the next
1649 expression. This guarantees that we don't dump the same node twice,
1650 as for const expressions cse is performed to save memory. */
1651 set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph) -1);
1653 ir_set_dump_flags(old_flags);
1654 current_ir_graph = rem;
1657 /** Dump a block as graph containing its nodes.
1659 * Expects to find nodes belonging to the block as list in its
1661 * Dumps the edges of all nodes including itself. */
1662 static void dump_whole_block(FILE *F, ir_node *block)
1665 ird_color_t color = ird_color_block_background;
1667 assert(is_Block(block));
1669 fprintf(F, "graph: { title: \"");
1670 PRINT_NODEID(block);
1671 fprintf(F, "\" label: \"");
1672 dump_node_label(F, block);
1674 /* colorize blocks */
1675 if (! get_Block_matured(block))
1676 color = ird_color_block_background;
1677 if (is_Block_dead(block))
1678 color = ird_color_dead_block_background;
1680 fprintf(F, "\" status:clustered ");
1681 print_vcg_color(F, color);
1684 /* yComp can show attributes for blocks, XVCG parses but ignores them */
1685 dump_node_info(F, block);
1686 print_dbg_info(F, get_irn_dbg_info(block));
1688 /* dump the blocks edges */
1689 dump_ir_data_edges(F, block);
1691 if (dump_block_edge_hook)
1692 dump_block_edge_hook(F, block);
1694 /* dump the nodes that go into the block */
1695 for (node = ird_get_irn_link(block); node; node = ird_get_irn_link(node)) {
1697 dump_ir_data_edges(F, node);
1700 /* Close the vcg information for the block */
1702 dump_const_node_local(F, block);
1706 /** dumps a graph block-wise. Expects all blockless nodes in arr in irgs link.
1707 * The outermost nodes: blocks and nodes not op_pin_state_pinned, Bad, Unknown. */
1708 static void dump_block_graph(FILE *F, ir_graph *irg)
1711 ir_graph *rem = current_ir_graph;
1712 ir_node **arr = ird_get_irg_link(irg);
1713 current_ir_graph = irg;
1715 for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
1716 ir_node * node = arr[i];
1717 if (is_Block(node)) {
1718 /* Dumps the block and all the nodes in the block, which are to
1719 be found in Block->link. */
1720 dump_whole_block(F, node);
1722 /* Nodes that are not in a Block. */
1724 if (!node_floats(node) && is_Bad(get_nodes_block(node))) {
1725 dump_const_block_local(F, node);
1727 dump_ir_data_edges(F, node);
1729 if ((flags & ir_dump_flag_iredges) && edges_activated(irg))
1730 dump_ir_edges(node, F);
1733 if ((flags & ir_dump_flag_loops) && (get_irg_loopinfo_state(irg) & loopinfo_valid))
1734 dump_loop_nodes_into_graph(F, irg);
1736 current_ir_graph = rem;
1740 * Dump the info for an irg.
1741 * Parsed by XVCG but not shown. use yComp.
1743 static void dump_graph_info(FILE *F, ir_graph *irg)
1745 fprintf(F, "info1: \"");
1746 dump_entity_to_file(F, get_irg_entity(irg));
1750 /** Dumps an irg as a graph clustered by block nodes.
1751 * If interprocedural view edges can point to nodes out of this graph.
1753 static void dump_graph_from_list(FILE *F, ir_graph *irg)
1755 ir_entity *ent = get_irg_entity(irg);
1757 fprintf(F, "graph: { title: \"");
1759 fprintf(F, "\" label: \"%s\" status:clustered color:%s \n",
1760 get_ent_dump_name(ent), color_names[ird_color_prog_background]);
1762 dump_graph_info(F, irg);
1763 print_dbg_info(F, get_entity_dbg_info(ent));
1765 dump_block_graph(F, irg);
1767 /* Close the vcg information for the irg */
1768 fprintf(F, "}\n\n");
1771 /*******************************************************************/
1772 /* Basic type and entity nodes and edges. */
1773 /*******************************************************************/
1775 /** dumps the edges between nodes and their type or entity attributes. */
1776 static void dump_node2type_edges(ir_node *n, void *env)
1781 switch (get_irn_opcode(n)) {
1783 /* @@@ some consts have an entity */
1786 if (SYMCONST_HAS_TYPE(get_SymConst_kind(n)))
1787 print_node_type_edge(F,n,get_SymConst_type(n),NODE2TYPE_EDGE_ATTR);
1790 print_node_ent_edge(F,n,get_Sel_entity(n),NODE2TYPE_EDGE_ATTR);
1793 print_node_type_edge(F,n,get_Call_type(n),NODE2TYPE_EDGE_ATTR);
1796 print_node_type_edge(F,n,get_Alloc_type(n),NODE2TYPE_EDGE_ATTR);
1799 print_node_type_edge(F,n,get_Free_type(n),NODE2TYPE_EDGE_ATTR);
1802 print_node_type_edge(F,n,get_Cast_type(n),NODE2TYPE_EDGE_ATTR);
1810 static int print_type_info(FILE *F, ir_type *tp)
1814 if (get_type_state(tp) == layout_undefined) {
1815 fprintf(F, "state: layout_undefined\n");
1817 fprintf(F, "state: layout_fixed,\n");
1819 if (get_type_mode(tp))
1820 fprintf(F, "mode: %s,\n", get_mode_name_ex(get_type_mode(tp), &bad));
1821 fprintf(F, "size: %db,\n", get_type_size_bits(tp));
1826 static void print_typespecific_info(FILE *F, ir_type *tp)
1828 switch (get_type_tpop_code(tp)) {
1830 fprintf(F, "peculiarity: %s\n", get_peculiarity_string(get_class_peculiarity(tp)));
1835 fprintf(F, "variadicity: %s\n", get_variadicity_name(get_method_variadicity(tp)));
1836 fprintf(F, "params: %d\n", get_method_n_params(tp));
1837 fprintf(F, "results: %d\n", get_method_n_ress(tp));
1843 case tpo_enumeration:
1855 static void print_typespecific_vcgattr(FILE *F, ir_type *tp)
1857 switch (get_type_tpop_code(tp)) {
1859 fprintf(F, " " TYPE_CLASS_NODE_ATTR);
1862 fprintf(F, " " TYPE_METH_NODE_ATTR);
1870 case tpo_enumeration:
1881 void dump_type_node(FILE *F, ir_type *tp)
1883 fprintf(F, "node: {title: ");
1885 fprintf(F, " label: \"%s ", get_type_tpop_name(tp));
1886 if (tp->dbi != NULL) {
1888 ir_print_type(buf, sizeof(buf), tp);
1889 fprintf(F, "'%s'", buf);
1891 ir_fprintf(F, "%+F", tp);
1893 fputs("\" info1: \"", F);
1894 dump_type_to_file(F, tp);
1896 print_type_dbg_info(F, get_type_dbg_info(tp));
1897 print_typespecific_vcgattr(F, tp);
1901 static void dump_entity_node(FILE *F, ir_entity *ent)
1903 fprintf(F, "node: {title: \"");
1904 PRINT_ENTID(ent); fprintf(F, "\"");
1905 fprintf(F, " label: ");
1906 fprintf(F, "\"%s\" ", get_ent_dump_name(ent));
1908 print_vcg_color(F, ird_color_entity);
1909 fprintf(F, "\n info1: \"");
1911 dump_entity_to_file(F, ent);
1914 print_dbg_info(F, get_entity_dbg_info(ent));
1918 static void dump_enum_item(FILE *F, ir_type *tp, int pos)
1921 ir_enum_const *ec = get_enumeration_const(tp, pos);
1922 ident *id = get_enumeration_const_nameid(ec);
1923 tarval *tv = get_enumeration_value(ec);
1926 tarval_snprintf(buf, sizeof(buf), tv);
1928 strncpy(buf, "<not set>", sizeof(buf));
1929 fprintf(F, "node: {title: \"");
1930 PRINT_ITEMID(tp, pos); fprintf(F, "\"");
1931 fprintf(F, " label: ");
1932 fprintf(F, "\"enum item %s\" " ENUM_ITEM_NODE_ATTR, get_id_str(id));
1933 fprintf(F, "\n info1: \"value: %s\"}\n", buf);
1937 * Dumps a new style initializer.
1939 static void dump_entity_initializer(FILE *F, const ir_entity *ent)
1946 /** Dumps a type or entity and it's edges. */
1947 static void dump_type_info(type_or_ent tore, void *env)
1950 int i = 0; /* to shutup gcc */
1952 /* dump this type or entity */
1954 switch (get_kind(tore.ent)) {
1956 ir_entity *ent = tore.ent;
1959 dump_entity_node(F, ent);
1961 /* skip this to reduce graph. Member edge of type is parallel to this edge. *
1962 fprintf(F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
1963 ENT_OWN_EDGE_ATTR "}\n", ent, get_entity_owner(ent));*/
1964 print_ent_type_edge(F,ent, get_entity_type(ent), ENT_TYPE_EDGE_ATTR);
1965 if (is_Class_type(get_entity_owner(ent))) {
1966 for (i = get_entity_n_overwrites(ent) - 1; i >= 0; --i)
1967 print_ent_ent_edge(F,ent, get_entity_overwrites(ent, i), 0, -1, ENT_OVERWRITES_EDGE_ATTR);
1969 /* attached subgraphs */
1970 if (! (flags & ir_dump_flag_no_entity_values)) {
1971 if (ent->initializer != NULL) {
1972 /* new style initializers */
1973 dump_entity_initializer(F, ent);
1974 } else if (entity_has_compound_ent_values(ent)) {
1975 /* old style compound entity values */
1976 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
1977 value = get_compound_ent_value(ent, i);
1979 print_ent_node_edge(F, ent, value, ENT_VALUE_EDGE_ATTR, i);
1980 dump_const_expression(F, value);
1981 print_ent_ent_edge(F, ent, get_compound_ent_value_member(ent, i), 0, -1, ENT_CORR_EDGE_ATTR, i);
1983 fprintf(F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
1984 ENT_CORR_EDGE_ATTR "}\n", GET_ENTID(ent),
1985 get_compound_ent_value_member(ent, i), i);
1994 ir_type *tp = tore.typ;
1995 dump_type_node(F, tp);
1996 /* and now the edges */
1997 switch (get_type_tpop_code(tp)) {
1999 for (i = get_class_n_supertypes(tp) - 1; i >= 0; --i)
2000 print_type_type_edge(F, tp, get_class_supertype(tp, i), TYPE_SUPER_EDGE_ATTR);
2001 for (i = get_class_n_members(tp) - 1; i >= 0; --i)
2002 print_type_ent_edge(F, tp, get_class_member(tp, i), TYPE_MEMBER_EDGE_ATTR);
2005 for (i = get_struct_n_members(tp) - 1; i >= 0; --i)
2006 print_type_ent_edge(F, tp, get_struct_member(tp, i), TYPE_MEMBER_EDGE_ATTR);
2009 for (i = get_method_n_params(tp) - 1; i >= 0; --i)
2010 print_type_type_edge(F, tp, get_method_param_type(tp, i), METH_PAR_EDGE_ATTR,i);
2011 for (i = get_method_n_ress(tp) - 1; i >= 0; --i)
2012 print_type_type_edge(F, tp, get_method_res_type(tp, i), METH_RES_EDGE_ATTR,i);
2015 for (i = get_union_n_members(tp) - 1; i >= 0; --i)
2016 print_type_ent_edge(F, tp, get_union_member(tp, i), UNION_EDGE_ATTR);
2019 print_type_type_edge(F, tp, get_array_element_type(tp), ARR_ELT_TYPE_EDGE_ATTR);
2020 print_type_ent_edge(F, tp, get_array_element_entity(tp), ARR_ENT_EDGE_ATTR);
2021 for (i = get_array_n_dimensions(tp) - 1; i >= 0; --i) {
2022 ir_node *upper = get_array_upper_bound(tp, i);
2023 ir_node *lower = get_array_lower_bound(tp, i);
2024 print_node_type_edge(F, upper, tp, "label: \"upper %d\"", get_array_order(tp, i));
2025 print_node_type_edge(F, lower, tp, "label: \"lower %d\"", get_array_order(tp, i));
2026 dump_const_expression(F, upper);
2027 dump_const_expression(F, lower);
2030 case tpo_enumeration:
2031 for (i = get_enumeration_n_enums(tp) - 1; i >= 0; --i) {
2032 dump_enum_item(F, tp, i);
2033 print_enum_item_edge(F, tp, i, "label: \"item %d\"", i);
2037 print_type_type_edge(F, tp, get_pointer_points_to_type(tp), PTR_PTS_TO_EDGE_ATTR);
2044 break; /* case k_type */
2047 printf(" *** irdump, dump_type_info(l.%i), faulty type.\n", __LINE__);
2048 } /* switch kind_or_entity */
2051 /** For dumping class hierarchies.
2052 * Dumps a class type node and a superclass edge.
2054 static void dump_class_hierarchy_node(type_or_ent tore, void *ctx)
2057 int i = 0; /* to shutup gcc */
2059 /* dump this type or entity */
2060 switch (get_kind(tore.ent)) {
2062 ir_entity *ent = tore.ent;
2063 if (get_entity_owner(ent) == get_glob_type()) break;
2064 if (!is_Method_type(get_entity_type(ent)))
2066 if (flags & ir_dump_flag_entities_in_hierarchy
2067 && is_Class_type(get_entity_owner(ent))) {
2069 dump_entity_node(F, ent);
2071 print_type_ent_edge(F, get_entity_owner(ent), ent, TYPE_MEMBER_EDGE_ATTR);
2072 for (i = get_entity_n_overwrites(ent) - 1; i >= 0; --i)
2073 print_ent_ent_edge(F, get_entity_overwrites(ent, i), ent, 0, -1, ENT_OVERWRITES_EDGE_ATTR);
2078 ir_type *tp = tore.typ;
2079 if (tp == get_glob_type())
2081 switch (get_type_tpop_code(tp)) {
2083 dump_type_node(F, tp);
2084 /* and now the edges */
2085 for (i = get_class_n_supertypes(tp) - 1; i >= 0; --i) {
2086 print_type_type_edge(F,tp,get_class_supertype(tp, i),TYPE_SUPER_EDGE_ATTR);
2091 break; /* case k_type */
2094 printf(" *** irdump, dump_class_hierarchy_node(l.%i), faulty type.\n", __LINE__);
2095 } /* switch kind_or_entity */
2098 /*******************************************************************/
2099 /* dump analysis information that is expressed in graph terms. */
2100 /*******************************************************************/
2102 /* dump out edges */
2103 static void dump_out_edge(ir_node *n, void *env)
2107 for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
2108 ir_node *succ = get_irn_out(n, i);
2110 print_node_edge_kind(F, succ);
2111 fprintf(F, "{sourcename: \"");
2113 fprintf(F, "\" targetname: \"");
2115 fprintf(F, "\" color: red linestyle: dashed");
2120 static void dump_loop_label(FILE *F, ir_loop *loop)
2122 fprintf(F, "loop %d, %d sons, %d nodes",
2123 get_loop_depth(loop), get_loop_n_sons(loop), get_loop_n_nodes(loop));
2126 static void dump_loop_info(FILE *F, ir_loop *loop)
2128 fprintf(F, " info1: \"");
2129 fprintf(F, " loop nr: %d", get_loop_loop_nr(loop));
2130 #ifdef DEBUG_libfirm /* GL @@@ debug analyses */
2131 fprintf(F, "\n The loop was analyzed %d times.", PTR_TO_INT(get_loop_link(loop)));
2136 static void dump_loop_node(FILE *F, ir_loop *loop)
2138 fprintf(F, "node: {title: \"");
2140 fprintf(F, "\" label: \"");
2141 dump_loop_label(F, loop);
2143 dump_loop_info(F, loop);
2147 static void dump_loop_node_edge(FILE *F, ir_loop *loop, int i)
2150 fprintf(F, "edge: {sourcename: \"");
2152 fprintf(F, "\" targetname: \"");
2153 PRINT_NODEID(get_loop_node(loop, i));
2154 fprintf(F, "\" color: green");
2158 static void dump_loop_son_edge(FILE *F, ir_loop *loop, int i)
2161 fprintf(F, "edge: {sourcename: \"");
2163 fprintf(F, "\" targetname: \"");
2164 PRINT_LOOPID(get_loop_son(loop, i));
2165 fprintf(F, "\" color: darkgreen label: \"%d\"}\n",
2166 get_loop_element_pos(loop, get_loop_son(loop, i)));
2169 static void dump_loops(FILE *F, ir_loop *loop)
2172 /* dump this loop node */
2173 dump_loop_node(F, loop);
2175 /* dump edges to nodes in loop -- only if it is a real loop */
2176 if (get_loop_depth(loop) != 0) {
2177 for (i = get_loop_n_nodes(loop) - 1; i >= 0; --i) {
2178 dump_loop_node_edge(F, loop, i);
2181 for (i = get_loop_n_sons(loop) - 1; i >= 0; --i) {
2182 dump_loops(F, get_loop_son(loop, i));
2183 dump_loop_son_edge(F, loop, i);
2187 static void dump_loop_nodes_into_graph(FILE *F, ir_graph *irg)
2189 ir_loop *loop = get_irg_loop(irg);
2192 ir_graph *rem = current_ir_graph;
2193 current_ir_graph = irg;
2195 dump_loops(F, loop);
2197 current_ir_graph = rem;
2203 * dumps the VCG header
2205 void dump_vcg_header(FILE *F, const char *name, const char *layout, const char *orientation)
2209 = (flags & ir_dump_flag_disable_edge_labels) ? "no" : "yes";
2213 if (! layout) layout = "Compilergraph";
2214 if (!orientation) orientation = "bottom_to_top";
2218 "graph: { title: \"ir graph of %s\"\n"
2219 "display_edge_labels: %s\n"
2220 "layoutalgorithm: mindepth //$ \"%s\"\n"
2221 "manhattan_edges: yes\n"
2222 "port_sharing: no\n"
2224 "classname 1: \"intrablock Data\"\n"
2225 "classname 2: \"Block\"\n"
2226 "classname 3: \"Entity type\"\n"
2227 "classname 4: \"Entity owner\"\n"
2228 "classname 5: \"Method Param\"\n"
2229 "classname 6: \"Method Res\"\n"
2230 "classname 7: \"Super\"\n"
2231 "classname 8: \"Union\"\n"
2232 "classname 9: \"Points-to\"\n"
2233 "classname 10: \"Array Element Type\"\n"
2234 "classname 11: \"Overwrites\"\n"
2235 "classname 12: \"Member\"\n"
2236 "classname 13: \"Control Flow\"\n"
2237 "classname 14: \"intrablock Memory\"\n"
2238 "classname 15: \"Dominators\"\n"
2239 "classname 16: \"interblock Data\"\n"
2240 "classname 17: \"interblock Memory\"\n"
2241 "classname 18: \"Exception Control Flow for Interval Analysis\"\n"
2242 "classname 19: \"Postdominators\"\n"
2243 "classname 20: \"Keep Alive\"\n"
2244 "classname 21: \"Out Edges\"\n"
2245 "classname 22: \"Macro Block Edges\"\n"
2246 //"classname 23: \"NoInput Nodes\"\n"
2247 "infoname 1: \"Attribute\"\n"
2248 "infoname 2: \"Verification errors\"\n"
2249 "infoname 3: \"Debug info\"\n",
2250 name, label, layout, orientation);
2252 for (i = 0; i < ird_color_count; ++i) {
2253 if (color_rgb[i] != NULL) {
2254 fprintf(F, "colorentry %s: %s\n", color_names[i], color_rgb[i]);
2261 * Dumps the vcg file footer
2263 void dump_vcg_footer(FILE *F)
2270 static void dump_blocks_as_subgraphs(FILE *out, ir_graph *irg)
2274 construct_block_lists(irg);
2277 * If we are in the interprocedural view, we dump not
2278 * only the requested irg but also all irgs that can be reached
2281 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
2282 ir_graph *irg = get_irp_irg(i);
2283 ir_node **arr = ird_get_irg_link(irg);
2287 dump_graph_from_list(out, irg);
2292 /** dumps a graph extended block-wise. Expects all blockless nodes in arr in irgs link.
2293 * The outermost nodes: blocks and nodes not op_pin_state_pinned, Bad, Unknown. */
2294 static void dump_extblock_graph(FILE *F, ir_graph *irg)
2297 ir_graph *rem = current_ir_graph;
2298 ir_extblk **arr = ird_get_irg_link(irg);
2299 current_ir_graph = irg;
2301 for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
2302 ir_extblk *extbb = arr[i];
2303 ir_node *leader = get_extbb_leader(extbb);
2306 fprintf(F, "graph: { title: \"");
2307 PRINT_EXTBBID(leader);
2308 fprintf(F, "\" label: \"ExtBB %ld\" status:clustered color:lightgreen\n",
2309 get_irn_node_nr(leader));
2311 for (j = ARR_LEN(extbb->blks) - 1; j >= 0; --j) {
2312 ir_node * node = extbb->blks[j];
2313 if (is_Block(node)) {
2314 /* Dumps the block and all the nodes in the block, which are to
2315 be found in Block->link. */
2316 dump_whole_block(F, node);
2318 /* Nodes that are not in a Block. */
2320 if (is_Bad(get_nodes_block(node)) && !node_floats(node)) {
2321 dump_const_block_local(F, node);
2323 dump_ir_data_edges(F, node);
2329 if ((flags & ir_dump_flag_loops)
2330 && (get_irg_loopinfo_state(irg) & loopinfo_valid))
2331 dump_loop_nodes_into_graph(F, irg);
2333 current_ir_graph = rem;
2337 static void dump_blocks_extbb_grouped(FILE *F, ir_graph *irg)
2340 ir_entity *ent = get_irg_entity(irg);
2342 if (get_irg_extblk_state(irg) != extblk_valid)
2345 construct_extblock_lists(irg);
2347 fprintf(F, "graph: { title: \"");
2349 fprintf(F, "\" label: \"%s\" status:clustered color: white \n",
2350 get_ent_dump_name(ent));
2352 dump_graph_info(F, irg);
2353 print_dbg_info(F, get_entity_dbg_info(ent));
2355 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
2356 ir_graph *irg = get_irp_irg(i);
2357 list_tuple *lists = ird_get_irg_link(irg);
2360 /* dump the extended blocks first */
2361 if (ARR_LEN(lists->extbb_list)) {
2362 ird_set_irg_link(irg, lists->extbb_list);
2363 dump_extblock_graph(F, irg);
2366 /* we may have blocks without extended blocks, bad for instance */
2367 if (ARR_LEN(lists->blk_list)) {
2368 ird_set_irg_link(irg, lists->blk_list);
2369 dump_block_graph(F, irg);
2372 DEL_ARR_F(lists->extbb_list);
2373 DEL_ARR_F(lists->blk_list);
2378 /* Close the vcg information for the irg */
2379 fprintf(F, "}\n\n");
2384 void dump_ir_graph_file(FILE *out, ir_graph *irg)
2386 dump_vcg_header(out, get_irg_dump_name(irg), NULL, NULL);
2389 if (flags & ir_dump_flag_blocks_as_subgraphs) {
2390 if (flags & ir_dump_flag_group_extbb) {
2391 dump_blocks_extbb_grouped(out, irg);
2393 dump_blocks_as_subgraphs(out, irg);
2396 /* dump_node_with_edges must be called in post visiting predecessors */
2397 ird_walk_graph(irg, NULL, dump_node_with_edges, out);
2400 /* dump type info */
2401 if (flags & ir_dump_flag_with_typegraph) {
2402 ir_graph *rem = current_ir_graph;
2403 current_ir_graph = irg;
2405 type_walk_irg(irg, dump_type_info, NULL, out);
2406 inc_irg_visited(get_const_code_irg());
2407 /* dump edges from graph to type info */
2408 irg_walk(get_irg_end(irg), dump_node2type_edges, NULL, out);
2410 current_ir_graph = rem;
2413 /* dump iredges out edges */
2414 if ((flags & ir_dump_flag_iredges) && edges_activated(current_ir_graph)) {
2415 irg_walk_edges(get_irg_start_block(irg), dump_ir_edges, NULL, out);
2418 /* dump the out edges in a separate walk */
2419 if ((flags & ir_dump_flag_out_edges)
2420 && (get_irg_outs_state(irg) != outs_none)) {
2421 irg_out_walk(get_irg_start(irg), dump_out_edge, NULL, out);
2424 dump_vcg_footer(out);
2427 static void dump_block_to_cfg(ir_node *block, void *env)
2433 if (is_Block(block)) {
2434 /* This is a block. Dump a node for the block. */
2435 fprintf(F, "node: {title: \""); PRINT_NODEID(block);
2436 fprintf(F, "\" label: \"");
2437 if (block == get_irg_start_block(get_irn_irg(block)))
2438 fprintf(F, "Start ");
2439 if (block == get_irg_end_block(get_irn_irg(block)))
2442 fprintf(F, "%s ", get_op_name(get_irn_op(block)));
2443 PRINT_NODEID(block);
2445 fprintf(F, "info1:\"");
2447 /* the generic version. */
2448 dump_irnode_to_file(F, block);
2450 /* Check whether we have bad predecessors to color the block. */
2451 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i)
2452 if ((fl = is_Bad(get_Block_cfgpred(block, i))))
2455 fprintf(F, "\""); /* closing quote of info */
2457 if ((block == get_irg_start_block(get_irn_irg(block))) ||
2458 (block == get_irg_end_block(get_irn_irg(block))) )
2459 fprintf(F, " color:blue ");
2461 fprintf(F, " color:yellow ");
2464 /* Dump the edges */
2465 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i)
2466 if (!is_Bad(skip_Proj(get_Block_cfgpred(block, i)))) {
2467 pred = get_nodes_block(skip_Proj(get_Block_cfgpred(block, i)));
2468 fprintf(F, "edge: { sourcename: \"");
2469 PRINT_NODEID(block);
2470 fprintf(F, "\" targetname: \"");
2472 fprintf(F, "\"}\n");
2475 /* Dump dominator/postdominator edge */
2476 if (ir_get_dump_flags() & ir_dump_flag_dominance) {
2477 if (get_irg_dom_state(current_ir_graph) == dom_consistent && get_Block_idom(block)) {
2478 pred = get_Block_idom(block);
2479 fprintf(F, "edge: { sourcename: \"");
2480 PRINT_NODEID(block);
2481 fprintf(F, "\" targetname: \"");
2483 fprintf(F, "\" " DOMINATOR_EDGE_ATTR "}\n");
2485 if (get_irg_postdom_state(current_ir_graph) == dom_consistent && get_Block_ipostdom(block)) {
2486 pred = get_Block_ipostdom(block);
2487 fprintf(F, "edge: { sourcename: \"");
2488 PRINT_NODEID(block);
2489 fprintf(F, "\" targetname: \"");
2491 fprintf(F, "\" " POSTDOMINATOR_EDGE_ATTR "}\n");
2497 void dump_cfg(FILE *F, ir_graph *irg)
2499 #ifdef INTERPROCEDURAL_VIEW
2500 int ipv = get_interprocedural_view();
2503 dump_vcg_header(F, get_irg_dump_name(irg), NULL, NULL);
2505 #ifdef INTERPROCEDURAL_VIEW
2507 printf("Warning: dumping cfg not in interprocedural view!\n");
2508 set_interprocedural_view(0);
2512 /* walk over the blocks in the graph */
2513 irg_block_walk(get_irg_end(irg), dump_block_to_cfg, NULL, F);
2514 dump_node(F, get_irg_bad(irg));
2516 #ifdef INTERPROCEDURAL_VIEW
2517 set_interprocedural_view(ipv);
2522 void dump_callgraph(FILE *F)
2525 ir_dump_flags_t old_flags = ir_get_dump_flags();
2527 ir_remove_dump_flags(ir_dump_flag_disable_edge_labels);
2528 dump_vcg_header(F, "Callgraph", "Hierarchic", NULL);
2530 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
2531 ir_graph *irg = get_irp_irg(i);
2532 ir_entity *ent = get_irg_entity(irg);
2534 int n_callees = get_irg_n_callees(irg);
2536 dump_entity_node(F, ent);
2537 for (j = 0; j < n_callees; ++j) {
2538 ir_entity *c = get_irg_entity(get_irg_callee(irg, j));
2539 int be = is_irg_callee_backedge(irg, j);
2540 const char *attr = be
2541 ? "label:\"recursion %d\""
2542 : "label:\"calls %d\"";
2543 print_ent_ent_edge(F, ent, c, be, ird_color_entity, attr,
2544 get_irg_callee_loop_depth(irg, j));
2548 ir_set_dump_flags(old_flags);
2552 void dump_typegraph(FILE *out)
2554 dump_vcg_header(out, "All_types", "Hierarchic", NULL);
2555 type_walk(dump_type_info, NULL, out);
2556 dump_vcg_footer(out);
2559 void dump_class_hierarchy(FILE *out)
2561 dump_vcg_header(out, "class_hierarchy", "Hierarchic", NULL);
2562 type_walk(dump_class_hierarchy_node, NULL, out);
2563 dump_vcg_footer(out);
2566 static void dump_loops_standalone(FILE *F, ir_loop *loop)
2568 int i = 0, loop_node_started = 0, son_number = 0, first = 0;
2570 ir_loop *son = NULL;
2572 /* Dump a new loop node. */
2573 dump_loop_node(F, loop);
2575 /* Dump the loop elements. */
2576 for (i = 0; i < get_loop_n_elements(loop); i++) {
2577 le = get_loop_element(loop, i);
2579 if (get_kind(son) == k_ir_loop) {
2581 /* We are a loop son -> Recurse */
2583 if (loop_node_started) { /* Close the "firm-nodes" node first if we started one. */
2584 fprintf(F, "\" }\n");
2585 fprintf(F, "edge: {sourcename: \"");
2587 fprintf(F, "\" targetname: \"");
2589 fprintf(F, "-%d-nodes\" label:\"%d...%d\"}\n", first, first, i-1);
2590 loop_node_started = 0;
2592 dump_loop_son_edge(F, loop, son_number++);
2593 dump_loops_standalone(F, son);
2594 } else if (get_kind(son) == k_ir_node) {
2595 /* We are a loop node -> Collect firm nodes */
2597 ir_node *n = le.node;
2598 if (!loop_node_started) {
2599 /* Start a new node which contains all firm nodes of the current loop */
2600 fprintf(F, "node: { title: \"");
2602 fprintf(F, "-%d-nodes\" color: lightyellow label: \"", i);
2603 loop_node_started = 1;
2608 dump_node_label(F, n);
2609 /* Causes indeterministic output: if (is_Block(n)) fprintf(F, "\t ->%d", (int)get_irn_link(n)); */
2610 if (has_backedges(n)) fprintf(F, "\t loop head!");
2611 } else { /* for callgraph loop tree */
2613 assert(get_kind(son) == k_ir_graph);
2615 /* We are a loop node -> Collect firm graphs */
2617 if (!loop_node_started) {
2618 /* Start a new node which contains all firm nodes of the current loop */
2619 fprintf(F, "node: { title: \"");
2621 fprintf(F, "-%d-nodes\" color: lightyellow label: \"", i);
2622 loop_node_started = 1;
2626 fprintf(F, " %s", get_irg_dump_name(n));
2627 /* fprintf(F, " %s (depth %d)", get_irg_dump_name(n), n->callgraph_weighted_loop_depth); */
2631 if (loop_node_started) {
2632 fprintf(F, "\" }\n");
2633 fprintf(F, "edge: {sourcename: \"");
2635 fprintf(F, "\" targetname: \"");
2637 fprintf(F, "-%d-nodes\" label:\"%d...%d\"}\n", first, first, i-1);
2638 loop_node_started = 0;
2642 void dump_loop_tree(FILE *out, ir_graph *irg)
2644 ir_graph *rem = current_ir_graph;
2645 ir_dump_flags_t old_flags = ir_get_dump_flags();
2647 current_ir_graph = irg;
2648 ir_remove_dump_flags(ir_dump_flag_disable_edge_labels);
2650 dump_vcg_header(out, get_irg_dump_name(irg), "Tree", "top_to_bottom");
2652 if (get_irg_loop(irg))
2653 dump_loops_standalone(out, get_irg_loop(irg));
2655 dump_vcg_footer(out);
2657 ir_set_dump_flags(old_flags);
2658 current_ir_graph = rem;
2661 void dump_callgraph_loop_tree(FILE *out)
2663 dump_vcg_header(out, "callgraph looptree", "Tree", "top_to_bottom");
2664 dump_loops_standalone(out, irp->outermost_cg_loop);
2665 dump_vcg_footer(out);
2668 static void collect_nodeloop(FILE *F, ir_loop *loop, eset *loopnodes)
2670 int i, son_number = 0, node_number = 0;
2672 if (flags & ir_dump_flag_loops)
2673 dump_loop_node(F, loop);
2675 for (i = 0; i < get_loop_n_elements(loop); i++) {
2676 loop_element le = get_loop_element(loop, i);
2677 if (*(le.kind) == k_ir_loop) {
2678 if (flags & ir_dump_flag_loops)
2679 dump_loop_son_edge(F, loop, son_number++);
2681 collect_nodeloop(F, le.son, loopnodes);
2683 if (flags & ir_dump_flag_loops)
2684 dump_loop_node_edge(F, loop, node_number++);
2685 eset_insert(loopnodes, le.node);
2690 static void collect_nodeloop_external_nodes(ir_loop *loop, eset *loopnodes,
2695 for (i = 0; i < get_loop_n_elements(loop); i++) {
2696 loop_element le = get_loop_element(loop, i);
2697 if (*(le.kind) == k_ir_loop) {
2699 collect_nodeloop_external_nodes(le.son, loopnodes, extnodes);
2701 if (is_Block(le.node)) start = 0; else start = -1;
2702 for (j = start; j < get_irn_arity(le.node); j++) {
2703 ir_node *pred = get_irn_n(le.node, j);
2704 if (!eset_contains(loopnodes, pred)) {
2705 eset_insert(extnodes, pred);
2706 if (!is_Block(pred)) {
2707 pred = get_nodes_block(pred);
2708 if (!eset_contains(loopnodes, pred)) eset_insert(extnodes, pred);
2716 void dump_loop(FILE *F, ir_loop *l)
2719 snprintf(name, sizeof(name), "loop_%d", get_loop_loop_nr(l));
2721 eset *loopnodes = eset_create();
2722 eset *extnodes = eset_create();
2725 dump_vcg_header(F, name, NULL, NULL);
2727 /* collect all nodes to dump */
2728 collect_nodeloop(F, l, loopnodes);
2729 collect_nodeloop_external_nodes(l, loopnodes, extnodes);
2731 /* build block lists */
2732 for (n = eset_first(loopnodes); n != NULL; n = eset_next(loopnodes))
2733 set_irn_link(n, NULL);
2734 for (n = eset_first(extnodes); n != NULL; n = eset_next(extnodes))
2735 set_irn_link(n, NULL);
2736 for (n = eset_first(loopnodes); n != NULL; n = eset_next(loopnodes)) {
2738 b = get_nodes_block(n);
2739 set_irn_link(n, get_irn_link(b));
2743 for (n = eset_first(extnodes); n != NULL; n = eset_next(extnodes)) {
2745 b = get_nodes_block(n);
2746 set_irn_link(n, get_irn_link(b));
2751 for (b = eset_first(loopnodes); b != NULL; b = eset_next(loopnodes)) {
2753 fprintf(F, "graph: { title: \"");
2755 fprintf(F, "\" label: \"");
2756 dump_node_opcode(F, b);
2757 fprintf(F, " %ld:%d", get_irn_node_nr(b), get_irn_idx(b));
2758 fprintf(F, "\" status:clustered color:yellow\n");
2760 /* dump the blocks edges */
2761 dump_ir_data_edges(F, b);
2763 /* dump the nodes that go into the block */
2764 for (n = get_irn_link(b); n; n = get_irn_link(n)) {
2765 if (eset_contains(extnodes, n))
2766 overrule_nodecolor = ird_color_block_inout;
2768 overrule_nodecolor = ird_color_default_node;
2769 if (!eset_contains(extnodes, n)) dump_ir_data_edges(F, n);
2772 /* Close the vcg information for the block */
2774 dump_const_node_local(F, b);
2778 for (b = eset_first(extnodes); b != NULL; b = eset_next(extnodes)) {
2780 fprintf(F, "graph: { title: \"");
2782 fprintf(F, "\" label: \"");
2783 dump_node_opcode(F, b);
2784 fprintf(F, " %ld:%d", get_irn_node_nr(b), get_irn_idx(b));
2785 fprintf(F, "\" status:clustered color:lightblue\n");
2787 /* dump the nodes that go into the block */
2788 for (n = get_irn_link(b); n; n = get_irn_link(n)) {
2789 if (!eset_contains(loopnodes, n))
2790 overrule_nodecolor = ird_color_block_inout;
2792 overrule_nodecolor = ird_color_default_node;
2793 if (eset_contains(loopnodes, n)) dump_ir_data_edges(F, n);
2796 /* Close the vcg information for the block */
2798 dump_const_node_local(F, b);
2802 eset_destroy(loopnodes);
2803 eset_destroy(extnodes);
2808 static bool obstack_init;
2809 static struct obstack obst;
2810 static char *dump_path;
2812 void ir_set_dump_path(const char *path)
2815 dump_path = xstrdup(path);
2818 static void add_string_escaped(const char *string)
2821 for (p = string; *p != '\0'; ++p) {
2824 obstack_1grow(&obst, '@');
2825 obstack_1grow(&obst, '1');
2826 } else if (c == '@') {
2827 obstack_1grow(&obst, '@');
2828 obstack_1grow(&obst, '2');
2830 obstack_1grow(&obst, c);
2835 static void add_dump_path(void)
2837 if (!obstack_init) {
2838 obstack_init(&obst);
2839 obstack_init = true;
2842 if (dump_path != NULL) {
2843 size_t len = strlen(dump_path);
2844 obstack_grow(&obst, dump_path, len);
2845 if (len > 0 && dump_path[len-1] != '/')
2846 obstack_1grow(&obst, '/');
2850 void dump_ir_graph_ext(ir_graph_dump_func func, ir_graph *graph,
2853 const char *dump_name = get_irg_dump_name(graph);
2857 if (!ir_should_dump(dump_name))
2862 add_string_escaped(dump_name);
2863 obstack_printf(&obst, "-%02u", graph->dump_nr++);
2865 if (suffix != NULL) {
2866 if (suffix[0] != '.')
2867 obstack_1grow(&obst, '-');
2868 add_string_escaped(suffix);
2870 obstack_1grow(&obst, '\0');
2872 file_name = obstack_finish(&obst);
2873 /* xvcg expects only <CR> so we need "b"inary mode (for win32) */
2874 out = fopen(file_name, "wb");
2875 obstack_free(&obst, file_name);
2878 fprintf(stderr, "Couldn't open '%s': %s\n", file_name, strerror(errno));
2886 void dump_ir_prog_ext(ir_prog_dump_func func, const char *suffix)
2893 obstack_printf(&obst, "%02u", irp->dump_nr++);
2894 if (suffix != NULL) {
2895 if (suffix[0] != '.')
2896 obstack_1grow(&obst, '-');
2897 add_string_escaped(suffix);
2899 obstack_1grow(&obst, '\0');
2901 file_name = obstack_finish(&obst);
2902 out = fopen(file_name, "wb");
2903 obstack_free(&obst, file_name);
2906 fprintf(stderr, "Couldn't open '%s': %s\n", file_name, strerror(errno));
2913 void dump_ir_graph(ir_graph *graph, const char *suffix)
2917 snprintf(buf, sizeof(buf), "%s.vcg", suffix);
2918 dump_ir_graph_ext(dump_ir_graph_file, graph, buf);
2921 void dump_all_ir_graphs(const char *suffix)
2923 int n_irgs = get_irp_n_irgs();
2926 for (i = 0; i < n_irgs; ++i) {
2927 ir_graph *irg = get_irp_irg(i);
2928 dump_ir_graph(irg, suffix);
2933 ir_prog_pass_t pass;
2938 * Wrapper around dump_all_ir_graphs().
2940 static int dump_all_ir_graphs_wrapper(ir_prog *irp, void *context)
2942 struct pass_t *pass = context;
2945 dump_all_ir_graphs(pass->suffix);
2949 ir_prog_pass_t *dump_all_ir_graph_pass(const char *name, const char *suffix)
2951 size_t len = strlen(suffix);
2952 struct pass_t *pass = xmalloc(sizeof(*pass) + len);
2953 ir_prog_pass_t *res = def_prog_pass_constructor(
2954 &pass->pass, name ? name : "dump_all_graphs", dump_all_ir_graphs_wrapper);
2956 /* this pass does not change anything, so neither dump nor verify is needed. */
2957 res->dump_irprog = ir_prog_no_dump;
2958 res->verify_irprog = ir_prog_no_verify;
2960 memcpy(pass->suffix, suffix, len+1);