- fix most of the -Wunreachable-code and -Wlogical-op warnings
[libfirm] / ir / ir / irdump.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Write vcg representation of firm to file.
23  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Hubert Schmidt
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31
32 #include "list.h"
33
34 #include "irnode_t.h"
35 #include "irgraph_t.h"
36 #include "irprog_t.h"
37 #include "entity_t.h"
38 #include "irop.h"
39
40 #include "irdump_t.h"
41 #include "irpass_t.h"
42
43 #include "irgwalk.h"
44 #include "tv_t.h"
45 #include "irouts.h"
46 #include "iredges.h"
47 #include "irdom.h"
48 #include "irloop_t.h"
49 #include "callgraph.h"
50 #include "irextbb_t.h"
51 #include "irhooks.h"
52 #include "dbginfo_t.h"
53 #include "irtools.h"
54 #include "irprintf.h"
55
56 #include "irvrfy.h"
57
58 #include "error.h"
59 #include "array.h"
60 #include "pmap.h"
61 #include "eset.h"
62 #include "pset.h"
63
64 /** Dump only irgs with names that start with this prefix. */
65 static ident *dump_file_filter_id = NULL;
66
67 #define ERROR_TXT       "<ERROR>"
68
69 /*******************************************************************/
70 /* flags to steer output                                           */
71 /*******************************************************************/
72
73 /** An option to turn off edge labels */
74 static int edge_label = 1;
75 /** An option to turn off dumping values of constant entities */
76 static int const_entities = 1;
77 /** An option to dump the keep alive edges */
78 static int dump_keepalive = 1;
79 /** An option to dump the new out edges */
80 static int dump_new_edges_flag = 0;
81 /** An option to dump ld_names instead of names. */
82 static int dump_ld_name = 1;
83 /** Compiler options to dump analysis information in dump_ir_graph */
84 static int dump_out_edge_flag = 0;
85 static int dump_loop_information_flag = 0;
86 static int dump_backedge_information_flag = 1;
87 /** An option to dump const-like nodes locally. */
88 static int dump_const_local = 1;
89 /** An option to dump the node index number. */
90 static int dump_node_idx_labels = 0;
91 /** An option to dump all graph anchors */
92 static int dump_anchors = 0;
93 /** An option to dump the macro block edges. */
94 static int dump_macro_block_edges = 0;
95 /** An option to dump block marker in the block title */
96 static int dump_block_marker = 0;
97
98 int dump_dominator_information_flag = 0;
99 int opt_dump_analysed_type_info = 1;
100 int opt_dump_pointer_values_to_info = 0;  /* default off: for test compares!! */
101
102 static ird_color_t overrule_nodecolor = ird_color_default_node;
103
104 /** The vcg node attribute hook. */
105 static DUMP_IR_GRAPH_FUNC dump_ir_graph_hook = NULL;
106 /** The vcg node attribute hook. */
107 static DUMP_NODE_VCGATTR_FUNC dump_node_vcgattr_hook = NULL;
108 /** The vcg edge attribute hook. */
109 static DUMP_EDGE_VCGATTR_FUNC dump_edge_vcgattr_hook = NULL;
110 /** The vcg dump block edge hook */
111 static DUMP_NODE_EDGE_FUNC dump_block_edge_hook = NULL;
112 /** The vcg dump node edge hook. */
113 static DUMP_NODE_EDGE_FUNC dump_node_edge_hook = NULL;
114
115 /* Set the hook to be called to dump additional edges to a node. */
116 void set_dump_node_edge_hook(DUMP_NODE_EDGE_FUNC func)
117 {
118         dump_node_edge_hook = func;
119 }
120
121 /* Get the additional edge dump hook. */
122 DUMP_NODE_EDGE_FUNC get_dump_node_edge_hook(void)
123 {
124         return dump_node_edge_hook;
125 }
126
127 /* Set the hook to be called to dump additional edges to a block. */
128 void set_dump_block_edge_hook(DUMP_NODE_EDGE_FUNC func)
129 {
130         dump_block_edge_hook = func;
131 }
132
133 /* Get the additional block edge dump hook. */
134 DUMP_NODE_EDGE_FUNC get_dump_block_edge_hook(void)
135 {
136         return dump_node_edge_hook;
137 }
138
139 /* set the ir graph hook */
140 void set_dump_ir_graph_hook(DUMP_IR_GRAPH_FUNC hook)
141 {
142         dump_ir_graph_hook = hook;
143 }
144
145 /* set the node attribute hook */
146 void set_dump_node_vcgattr_hook(DUMP_NODE_VCGATTR_FUNC hook)
147 {
148         dump_node_vcgattr_hook = hook;
149 }
150
151 /* set the edge attribute hook */
152 void set_dump_edge_vcgattr_hook(DUMP_EDGE_VCGATTR_FUNC hook)
153 {
154         dump_edge_vcgattr_hook = hook;
155 }
156
157 /** Returns 0 if dump_out_edge_flag or dump_loop_information_flag
158  * are set, else returns dump_const_local_flag.
159  */
160 static int get_opt_dump_const_local(void)
161 {
162         if (dump_out_edge_flag || dump_loop_information_flag || (dump_new_edges_flag && edges_activated(current_ir_graph)))
163                 return 0;
164         return dump_const_local;
165 }
166
167 /* Set a prefix filter for output functions. */
168 void only_dump_method_with_name(ident *name)
169 {
170         dump_file_filter_id = name;
171 }
172
173 /* Returns the prefix filter set with only_dump_method_with_name(). */
174 ident *get_dump_file_filter_ident(void)
175 {
176         return dump_file_filter_id;
177 }
178
179 /* Returns non-zero if dump file filter is not set, or if it is a prefix of name. */
180 int is_filtered_dump_name(ident *name)
181 {
182         if (!dump_file_filter_id) return 1;
183         return id_is_prefix(dump_file_filter_id, name);
184 }
185
186 /* To turn off display of edge labels.  Edge labels often cause xvcg to
187    abort with a segmentation fault. */
188 void turn_off_edge_labels(void)
189 {
190         edge_label = 0;
191 }
192
193 void dump_consts_local(int flag)
194 {
195         dump_const_local = flag;
196 }
197
198 void dump_node_idx_label(int flag)
199 {
200         dump_node_idx_labels = flag;
201 }
202
203 void dump_constant_entity_values(int flag)
204 {
205         const_entities = flag;
206 }
207
208 void dump_keepalive_edges(int flag)
209 {
210         dump_keepalive = flag;
211 }
212
213 void dump_new_edges(int flag)
214 {
215         dump_new_edges_flag = flag;
216 }
217
218 int get_opt_dump_keepalive_edges(void)
219 {
220         return dump_keepalive;
221 }
222
223 void dump_out_edges(int flag)
224 {
225         dump_out_edge_flag = flag;
226 }
227
228 void dump_dominator_information(int flag)
229 {
230         dump_dominator_information_flag = flag;
231 }
232
233 void dump_loop_information(int flag)
234 {
235         dump_loop_information_flag = flag;
236 }
237
238 void dump_backedge_information(int flag)
239 {
240         dump_backedge_information_flag = flag;
241 }
242
243 /* Dump the information of type field specified in ana/irtypeinfo.h.
244  * If the flag is set, the type name is output in [] in the node label,
245  * else it is output as info.
246  */
247 void set_opt_dump_analysed_type_info(int flag)
248 {
249         opt_dump_analysed_type_info = flag;
250 }
251
252 void dump_pointer_values_to_info(int flag)
253 {
254         opt_dump_pointer_values_to_info = flag;
255 }
256
257 void dump_ld_names(int flag)
258 {
259         dump_ld_name = flag;
260 }
261
262 void dump_all_anchors(int flag)
263 {
264         dump_anchors = flag;
265 }
266
267 void dump_macroblock_edges(int flag)
268 {
269         dump_macro_block_edges = flag;
270 }
271
272 void dump_block_marker_in_title(int flag)
273 {
274         dump_block_marker = flag;
275 }
276
277 /* -------------- some extended helper functions ----------------- */
278
279 /**
280  * returns the name of a mode or ERROR_TXT if mode is NOT a mode object.
281  * in the later case, sets bad.
282  */
283 const char *get_mode_name_ex(const ir_mode *mode, int *bad)
284 {
285         if (is_mode(mode))
286                 return get_mode_name(mode);
287         *bad |= 1;
288         return ERROR_TXT;
289 }
290
291 #define CUSTOM_COLOR_BASE    100
292 static const char *color_names[ird_color_count];
293 static const char *color_rgb[ird_color_count];
294 static struct obstack color_obst;
295
296 /** define a custom color. */
297 static void custom_color(int num, const char *rgb_def)
298 {
299         assert(num < ird_color_count);
300         obstack_printf(&color_obst, "%d", CUSTOM_COLOR_BASE + num);
301         obstack_1grow(&color_obst, '\0');
302
303         color_rgb[num]   = rgb_def;
304         color_names[num] = obstack_finish(&color_obst);
305 }
306
307 /** Define a named color. */
308 static void named_color(int num, const char *name)
309 {
310         assert(num < ird_color_count);
311         color_rgb[num]   = NULL;
312         color_names[num] = name;
313 }
314
315 /** Initializes the used colors. */
316 static void init_colors(void)
317 {
318         static int initialized = 0;
319         if (initialized)
320                 return;
321
322         obstack_init(&color_obst);
323
324         custom_color(ird_color_prog_background,       "204 204 204");
325         custom_color(ird_color_block_background,      "255 255 0");
326         custom_color(ird_color_dead_block_background, "190 150 150");
327         named_color(ird_color_block_inout,            "lightblue");
328         named_color(ird_color_default_node,           "white");
329         custom_color(ird_color_memory,                "153 153 255");
330         custom_color(ird_color_controlflow,           "255 153 153");
331         custom_color(ird_color_const,                 "204 255 255");
332         custom_color(ird_color_proj,                  "255 255 153");
333         custom_color(ird_color_uses_memory,           "153 153 255");
334         custom_color(ird_color_phi,                   "105 255 105");
335         custom_color(ird_color_anchor,                "100 100 255");
336         named_color(ird_color_error,                  "red");
337         custom_color(ird_color_entity,                "204 204 255");
338
339         initialized = 1;
340 }
341
342 /**
343  * Prints the VCG color to a file.
344  */
345 static void print_vcg_color(FILE *F, ird_color_t color)
346 {
347         assert(color < ird_color_count);
348         fprintf(F, "color:%s", color_names[color]);
349 }
350
351 /**
352  * Prints the edge kind of a given IR node.
353  *
354  * Projs should be dumped near their predecessor, so they get "nearedge".
355  */
356 static void print_node_edge_kind(FILE *F, ir_node *node)
357 {
358         if (is_Proj(node)) {
359                 fprintf(F, "nearedge: ");
360         } else {
361                 fprintf(F, "edge: ");
362         }
363 }
364
365 /**
366  * Prints the edge from a type S to a type T with additional info fmt, ...
367  * to the file F.
368  */
369 static void print_type_type_edge(FILE *F, const ir_type *S, const ir_type *T, const char *fmt, ...)
370 {
371         va_list ap;
372
373         va_start(ap, fmt);
374         fprintf(F, "edge: { sourcename: "); PRINT_TYPEID(S);
375         fprintf(F, " targetname: "); PRINT_TYPEID(T);
376         vfprintf(F, fmt, ap);
377         fprintf(F,"}\n");
378         va_end(ap);
379 }
380
381 /**
382  * Prints the edge from a type tp to an entity ent with additional info fmt, ...
383  * to the file F.
384  */
385 static void print_type_ent_edge(FILE *F, const ir_type *tp, const ir_entity *ent, const char *fmt, ...)
386 {
387         va_list ap;
388
389         va_start(ap, fmt);
390         fprintf(F, "edge: { sourcename: "); PRINT_TYPEID(tp);
391         fprintf(F, " targetname: \""); PRINT_ENTID(ent); fprintf(F, "\"");
392         vfprintf(F, fmt, ap);
393         fprintf(F, "}\n");
394         va_end(ap);
395 }
396
397 /**
398  * Prints the edge from an entity ent1 to an entity ent2 with additional info fmt, ...
399  * to the file F.
400  */
401 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, ...)
402 {
403         va_list ap;
404
405         va_start(ap, fmt);
406         if (backedge)
407                 fprintf(F, "backedge: { sourcename: \"");
408         else
409                 fprintf(F, "edge: { sourcename: \"");
410         PRINT_ENTID(ent1);
411         fprintf(F, "\" targetname: \""); PRINT_ENTID(ent2);  fprintf(F, "\"");
412         vfprintf(F, fmt, ap);
413         fprintf(F, " ");
414         if (color != (ird_color_t) -1)
415                 print_vcg_color(F, color);
416         fprintf(F, "}\n");
417         va_end(ap);
418 }
419
420 /**
421  * Prints the edge from an entity ent to a type tp with additional info fmt, ...
422  * to the file F.
423  */
424 static void print_ent_type_edge(FILE *F, const ir_entity *ent, const ir_type *tp, const char *fmt, ...)
425 {
426         va_list ap;
427
428         va_start(ap, fmt);
429         fprintf(F, "edge: { sourcename: \""); PRINT_ENTID(ent);
430         fprintf(F, "\" targetname: "); PRINT_TYPEID(tp);
431         vfprintf(F, fmt, ap);
432         fprintf(F,"}\n");
433         va_end(ap);
434 }
435
436 /**
437  * Prints the edge from a node irn to a type tp with additional info fmt, ...
438  * to the file F.
439  */
440 static void print_node_type_edge(FILE *F, const ir_node *irn, ir_type *tp, const char *fmt, ...)
441 {
442         va_list ap;
443
444         va_start(ap, fmt);
445         fprintf(F, "edge: { sourcename: \""); PRINT_NODEID(irn);
446         fprintf(F, "\" targetname: "); PRINT_TYPEID(tp);
447         vfprintf(F, fmt, ap);
448         fprintf(F,"}\n");
449         va_end(ap);
450 }
451
452 /**
453  * Prints the edge from a node irn to an entity ent with additional info fmt, ...
454  * to the file F.
455  */
456 static void print_node_ent_edge(FILE *F, const ir_node *irn, const ir_entity *ent, const char *fmt, ...)
457 {
458         va_list ap;
459
460         va_start(ap, fmt);
461         fprintf(F, "edge: { sourcename: \""); PRINT_NODEID(irn);
462         fprintf(F, "\" targetname: \""); PRINT_ENTID(ent);
463         fprintf(F, "\"");
464         vfprintf(F, fmt, ap);
465         fprintf(F,"}\n");
466         va_end(ap);
467 }
468
469 /**
470  * Prints the edge from an entity ent to a node irn with additional info fmt, ...
471  * to the file F.
472  */
473 static void print_ent_node_edge(FILE *F, const ir_entity *ent, const ir_node *irn, const char *fmt, ...)
474 {
475         va_list ap;
476
477         va_start(ap, fmt);
478         fprintf(F, "edge: { sourcename: \""); PRINT_ENTID(ent);
479         fprintf(F, "\" targetname: \""); PRINT_NODEID(irn); fprintf(F, "\"");
480         vfprintf(F, fmt, ap);
481         fprintf(F,"}\n");
482         va_end(ap);
483 }
484
485 /**
486  * Prints the edge from a type tp to an enumeration item item with additional info fmt, ...
487  * to the file F.
488  */
489 static void print_enum_item_edge(FILE *F, const ir_type *tp, int item, const char *fmt, ...)
490 {
491         va_list ap;
492
493         va_start(ap, fmt);
494         fprintf(F, "edge: { sourcename: "); PRINT_TYPEID(tp);
495         fprintf(F, " targetname: \""); PRINT_ITEMID(tp, item); fprintf(F, "\" ");
496         vfprintf(F, fmt, ap);
497         fprintf(F,"}\n");
498         va_end(ap);
499 }
500
501 /*-----------------------------------------------------------------*/
502 /* global and ahead declarations                                   */
503 /*-----------------------------------------------------------------*/
504
505 static void dump_whole_node(ir_node *n, void *env);
506 static inline void dump_loop_nodes_into_graph(FILE *F, ir_graph *irg);
507
508 /*-----------------------------------------------------------------*/
509 /* Helper functions.                                                */
510 /*-----------------------------------------------------------------*/
511
512 /**
513  * This map is used as a private link attr to be able to call dumper
514  * anywhere without destroying link fields.
515  */
516 static pmap *irdump_link_map = NULL;
517
518 /** NOT A STANDARD LIBFIRM INIT METHOD
519  *
520  * We do not want to integrate dumping into libfirm, i.e., if the dumpers
521  * are off, we want to have as few interferences as possible.  Therefore the
522  * initialization is performed lazily and not called from within init_firm.
523  *
524  * Creates the link attribute map. */
525 static void init_irdump(void)
526 {
527         /* We need a new, empty map. */
528         if (irdump_link_map) pmap_destroy(irdump_link_map);
529         irdump_link_map = pmap_create();
530         if (!dump_file_filter_id)
531                 dump_file_filter_id = new_id_from_str("");
532 }
533
534 /**
535  * Returns the private link field.
536  */
537 static void *ird_get_irn_link(const ir_node *n)
538 {
539         void *res = NULL;
540         if (irdump_link_map == NULL)
541                 return NULL;
542
543         if (pmap_contains(irdump_link_map, n))
544                 res = pmap_get(irdump_link_map, n);
545         return res;
546 }
547
548 /**
549  * Sets the private link field.
550  */
551 static void ird_set_irn_link(const ir_node *n, void *x)
552 {
553         if (irdump_link_map == NULL)
554                 init_irdump();
555         pmap_insert(irdump_link_map, n, x);
556 }
557
558 /**
559  * Gets the private link field of an irg.
560  */
561 static void *ird_get_irg_link(const ir_graph *irg)
562 {
563         void *res = NULL;
564         if (irdump_link_map == NULL)
565                 return NULL;
566
567         if (pmap_contains(irdump_link_map, irg))
568                 res = pmap_get(irdump_link_map, irg);
569         return res;
570 }
571
572 /**
573  * Sets the private link field of an irg.
574  */
575 static void ird_set_irg_link(const ir_graph *irg, void *x)
576 {
577         if (irdump_link_map == NULL)
578                 init_irdump();
579         pmap_insert(irdump_link_map, irg, x);
580 }
581
582 /**
583  * Walker, clears the private link field.
584  */
585 static void clear_link(ir_node *node, void *env)
586 {
587         (void) env;
588         ird_set_irn_link(node, NULL);
589 }
590
591 /**
592  * If the entity has a ld_name, returns it if the dump_ld_name is set,
593  * else returns the name of the entity.
594  */
595 static const char *_get_ent_dump_name(const ir_entity *ent, int dump_ld_name)
596 {
597         if (ent == NULL)
598                 return "<NULL entity>";
599         if (dump_ld_name) {
600                 /* Don't use get_entity_ld_ident (ent) as it computes the mangled name! */
601                 if (ent->ld_name != NULL)
602                         return get_id_str(ent->ld_name);
603         }
604         return get_id_str(ent->name);
605 }
606
607 /**
608  * If the entity has a ld_name, returns it if the option dump_ld_name is set,
609  * else returns the name of the entity.
610  */
611 const char *get_ent_dump_name(const ir_entity *ent)
612 {
613         return _get_ent_dump_name(ent, dump_ld_name);
614 }
615
616 /* Returns the name of an IRG. */
617 const char *get_irg_dump_name(const ir_graph *irg)
618 {
619         /* Don't use get_entity_ld_ident (ent) as it computes the mangled name! */
620         return _get_ent_dump_name(get_irg_entity(irg), 1);
621 }
622
623 /**
624  * Returns non-zero if a node is in floating state.
625  */
626 static int node_floats(const ir_node *n)
627 {
628         return ((get_irn_pinned(n) == op_pin_state_floats) &&
629                 (get_irg_pinned(current_ir_graph) == op_pin_state_floats));
630 }
631
632 /**
633  *  Walker that visits the anchors
634  */
635 static void ird_walk_graph(ir_graph *irg, irg_walk_func *pre, irg_walk_func *post, void *env)
636 {
637         if (dump_anchors || (dump_new_edges_flag && edges_activated(irg))) {
638                 irg_walk_anchors(irg, pre, post, env);
639         } else {
640                 irg_walk_graph(irg, pre, post, env);
641         }
642 }
643
644 /**
645  * Walker, allocates an array for all blocks and puts it's nodes non-floating nodes into this array.
646  */
647 static void collect_node(ir_node *node, void *env)
648 {
649         (void) env;
650         if (is_Block(node)
651             || node_floats(node)
652             || (get_op_flags(get_irn_op(node)) & irop_flag_dump_noblock)) {
653                 ir_node ** arr = (ir_node **) ird_get_irg_link(get_irn_irg(node));
654                 if (!arr) arr = NEW_ARR_F(ir_node *, 0);
655                 ARR_APP1(ir_node *, arr, node);
656                 ird_set_irg_link(get_irn_irg(node), arr);    /* arr is an l-value, APP_ARR might change it! */
657         } else {
658                 ir_node * block = get_nodes_block(node);
659
660                 if (is_Bad(block)) {
661                         /* this node is in a Bad block, so we must place it into the graph's list */
662                         ir_node ** arr = (ir_node **) ird_get_irg_link(get_irn_irg(node));
663                         if (!arr) arr = NEW_ARR_F(ir_node *, 0);
664                         ARR_APP1(ir_node *, arr, node);
665                         ird_set_irg_link(get_irn_irg(node), arr);    /* arr is an l-value, APP_ARR might change it! */
666                 } else {
667                         ird_set_irn_link(node, ird_get_irn_link(block));
668                         ird_set_irn_link(block, node);
669                 }
670         }
671 }
672
673 /** Construct lists to walk ir block-wise.
674  *
675  * Collects all blocks, nodes not op_pin_state_pinned,
676  * Bad, NoMem and Unknown into a flexible array in link field of
677  * irg they belong to.  Sets the irg link field to NULL in all
678  * graphs not visited.
679  * Free the list with DEL_ARR_F().
680  */
681 static ir_node **construct_block_lists(ir_graph *irg)
682 {
683         int      i;
684 #ifdef INTERPROCEDURAL_VIEW
685         int      rem_view  = get_interprocedural_view();
686 #endif
687         int      walk_flag = ir_resources_reserved(irg) & IR_RESOURCE_IRN_VISITED;
688         ir_graph *rem      = current_ir_graph;
689
690         current_ir_graph = irg;
691
692         if (walk_flag) {
693                 ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
694         }
695
696         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
697                 ird_set_irg_link(get_irp_irg(i), NULL);
698
699         ird_walk_graph(current_ir_graph, clear_link, collect_node, current_ir_graph);
700
701 #ifdef INTERPROCEDURAL_VIEW
702         /* Collect also EndReg and EndExcept. We do not want to change the walker. */
703         set_interprocedural_view(0);
704 #endif
705
706         set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
707         irg_walk(get_irg_end_reg(current_ir_graph), clear_link, collect_node, current_ir_graph);
708         set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph)-1);
709         irg_walk(get_irg_end_except(current_ir_graph), clear_link, collect_node, current_ir_graph);
710
711 #ifdef INTERPROCEDURAL_VIEW
712         set_interprocedural_view(rem_view);
713 #endif
714
715         if (walk_flag) {
716                 ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
717         }
718
719         current_ir_graph = rem;
720         return ird_get_irg_link(irg);
721 }
722
723 typedef struct _list_tuple {
724         ir_node **blk_list;
725         ir_extblk **extbb_list;
726 } list_tuple;
727
728 /** Construct lists to walk IR extended block-wise.
729  * Free the lists in the tuple with DEL_ARR_F().
730  * Sets the irg link field to NULL in all
731  * graphs not visited.
732  */
733 static list_tuple *construct_extblock_lists(ir_graph *irg)
734 {
735         ir_node **blk_list = construct_block_lists(irg);
736         int i;
737         ir_graph *rem = current_ir_graph;
738         list_tuple *lists = XMALLOC(list_tuple);
739
740         current_ir_graph = irg;
741
742         lists->blk_list   = NEW_ARR_F(ir_node *, 0);
743         lists->extbb_list = NEW_ARR_F(ir_extblk *, 0);
744
745         inc_irg_block_visited(irg);
746         for (i = ARR_LEN(blk_list) - 1; i >= 0; --i) {
747                 ir_extblk *ext;
748
749                 if (is_Block(blk_list[i])) {
750                         ext = get_Block_extbb(blk_list[i]);
751
752                         if (extbb_not_visited(ext)) {
753                                 ARR_APP1(ir_extblk *, lists->extbb_list, ext);
754                                 mark_extbb_visited(ext);
755                         }
756                 } else
757                         ARR_APP1(ir_node *, lists->blk_list, blk_list[i]);
758         }
759         DEL_ARR_F(blk_list);
760
761         current_ir_graph = rem;
762         ird_set_irg_link(irg, lists);
763         return lists;
764 }
765
766 /*-----------------------------------------------------------------*/
767 /* Routines to dump information about a single ir node.            */
768 /*-----------------------------------------------------------------*/
769
770 /*
771  * dump the name of a node n to the File F.
772  */
773 int dump_node_opcode(FILE *F, ir_node *n)
774 {
775         int bad = 0;
776         const ir_op_ops *ops = get_op_ops(get_irn_op(n));
777
778         /* call the dump_node operation if available */
779         if (ops->dump_node)
780                 return ops->dump_node(n, F, dump_node_opcode_txt);
781
782         /* implementation for default nodes */
783         switch (get_irn_opcode(n)) {
784         case iro_SymConst:
785                 switch (get_SymConst_kind(n)) {
786                 case symconst_addr_ent:
787                         fprintf(F, "SymC &%s", get_entity_name(get_SymConst_entity(n)));
788                         break;
789                 case symconst_ofs_ent:
790                         fprintf(F, "SymC %s offset", get_entity_name(get_SymConst_entity(n)));
791                         break;
792                 case symconst_type_tag:
793                         ir_fprintf(F, "SymC %+F tag", get_SymConst_type(n));
794                         break;
795                 case symconst_type_size:
796                         ir_fprintf(F, "SymC %+F size", get_SymConst_type(n));
797                         break;
798                 case symconst_type_align:
799                         ir_fprintf(F, "SymC %+F align", get_SymConst_type(n));
800                         break;
801                 case symconst_enum_const:
802                         fprintf(F, "SymC %s enum", get_enumeration_const_name(get_SymConst_enum(n)));
803                         break;
804                 }
805                 break;
806
807 #ifdef INTERPROCEDURAL_VIEW
808         case iro_Filter:
809                 if (!get_interprocedural_view())
810                         fprintf(F, "Proj'");
811                 else
812                         goto default_case;
813                 break;
814 #endif
815
816         case iro_Proj: {
817                 ir_node *pred = get_Proj_pred(n);
818
819                 if (get_irn_opcode(pred) == iro_Cond
820                         && get_Proj_proj(n) == get_Cond_default_proj(pred)
821                         && get_irn_mode(get_Cond_selector(pred)) != mode_b)
822                         fprintf(F, "defProj");
823                 else
824                         goto default_case;
825         } break;
826
827 #ifdef INTERPROCEDURAL_VIEW
828         case iro_Start:
829         case iro_End:
830         case iro_EndExcept:
831         case iro_EndReg:
832                 if (get_interprocedural_view()) {
833                         fprintf(F, "%s %s", get_irn_opname(n), get_ent_dump_name(get_irg_entity(get_irn_irg(n))));
834                         break;
835                 } else
836                         goto default_case;
837 #endif
838
839         case iro_CallBegin: {
840                 ir_node *addr = get_CallBegin_ptr(n);
841                 ir_entity *ent = NULL;
842                 if (is_Sel(addr))
843                         ent = get_Sel_entity(addr);
844                 else if (is_Global(addr))
845                         ent = get_Global_entity(addr);
846                 fprintf(F, "%s", get_irn_opname(n));
847                 if (ent) fprintf(F, " %s", get_entity_name(ent));
848                 break;
849         }
850         case iro_Load:
851                 if (get_Load_align(n) == align_non_aligned)
852                         fprintf(F, "ua");
853                 fprintf(F, "%s[%s]", get_irn_opname(n), get_mode_name_ex(get_Load_mode(n), &bad));
854                 break;
855         case iro_Store:
856                 if (get_Store_align(n) == align_non_aligned)
857                         fprintf(F, "ua");
858                 fprintf(F, "%s", get_irn_opname(n));
859                 break;
860         case iro_Block:
861                 if (is_Block_dead(n))
862                         fputs("Dead ", F);
863                 if (n == get_irg_start_block(get_irn_irg(n)))
864                         fputs("Start ", F);
865                 if (n == get_irg_end_block(get_irn_irg(n)))
866                         fputs("End ", F);
867                 fprintf(F, "%s%s", get_irn_opname(n),
868                         dump_block_marker ? (get_Block_mark(n) ? "*" : "") : "");
869                 break;
870         case iro_Conv:
871                 if (get_Conv_strict(n))
872                         fprintf(F, "strict");
873                 fprintf(F, "%s", get_irn_opname(n));
874                 break;
875         case iro_Div:
876                 fprintf(F, "%s", get_irn_opname(n));
877                 if (get_Div_no_remainder(n))
878                         fprintf(F, "RL");
879                 fprintf(F, "[%s]", get_mode_name_ex(get_Div_resmode(n), &bad));
880                 break;
881         case iro_Mod:
882                 fprintf(F, "%s[%s]", get_irn_opname(n), get_mode_name_ex(get_Mod_resmode(n), &bad));
883                 break;
884         case iro_DivMod:
885                 fprintf(F, "%s[%s]", get_irn_opname(n), get_mode_name_ex(get_DivMod_resmode(n), &bad));
886                 break;
887         case iro_Builtin:
888                 fprintf(F, "%s[%s]", get_irn_opname(n), get_builtin_kind_name(get_Builtin_kind(n)));
889                 break;
890
891         default:
892 default_case:
893                 fprintf(F, "%s", get_irn_opname(n));
894
895         }  /* end switch */
896         return bad;
897 }
898
899 /**
900  * Dump the mode of a node n to a file F.
901  * Ignore modes that are "always known".
902  */
903 static int dump_node_mode(FILE *F, ir_node *n)
904 {
905         int             bad = 0;
906         const ir_op_ops *ops = get_op_ops(get_irn_op(n));
907         ir_opcode       iro;
908         ir_mode         *mode;
909
910         /* call the dump_node operation if available */
911         if (ops->dump_node)
912                 return ops->dump_node(n, F, dump_node_mode_txt);
913
914         /* default implementation */
915         iro = get_irn_opcode(n);
916         switch (iro) {
917         case iro_SymConst:
918         case iro_Sel:
919         case iro_End:
920         case iro_Return:
921         case iro_Free:
922         case iro_Sync:
923         case iro_Jmp:
924         case iro_NoMem:
925                 break;
926         default:
927                 mode = get_irn_mode(n);
928
929                 if (mode != NULL && mode != mode_BB && mode != mode_ANY && mode != mode_BAD &&
930                         (mode != mode_T || iro == iro_Proj))
931                         fprintf(F, "%s", get_mode_name_ex(mode, &bad));
932         }
933         return bad;
934 }
935
936 /**
937  * Dump the type of a node n to a file F if it's known.
938  */
939 static int dump_node_typeinfo(FILE *F, ir_node *n)
940 {
941         int bad = 0;
942
943         if (opt_dump_analysed_type_info) {
944                 if (get_irg_typeinfo_state(current_ir_graph) == ir_typeinfo_consistent  ||
945                         get_irg_typeinfo_state(current_ir_graph) == ir_typeinfo_inconsistent) {
946                         ir_type *tp = get_irn_typeinfo_type(n);
947                         if (tp != firm_none_type) {
948                                 ir_fprintf(F, "[%+F]", tp);
949                         } else {
950                                 fprintf(F, "[] ");
951                         }
952                 }
953         }
954         return bad;
955 }
956
957 typedef struct _pns_lookup {
958         long       nr;      /**< the proj number */
959         const char *name;   /**< the name of the Proj */
960 } pns_lookup_t;
961
962 typedef struct _proj_lookup {
963         ir_opcode          code;      /**< the opcode of the Proj predecessor */
964         unsigned           num_data;  /**< number of data entries */
965         const pns_lookup_t *data;     /**< the data */
966 } proj_lookup_t;
967
968 #define ARR_SIZE(a)       (sizeof(a)/sizeof(a[0]))
969
970 /** the lookup table for Proj(Start) names */
971 static const pns_lookup_t start_lut[] = {
972 #define X(a)    { pn_Start_##a, #a }
973         X(X_initial_exec),
974         X(P_frame_base),
975         X(P_tls),
976         X(T_args),
977 #undef X
978 };
979
980 /** the lookup table for Proj(Cond) names */
981 static const pns_lookup_t cond_lut[] = {
982 #define X(a)    { pn_Cond_##a, #a }
983         X(false),
984         X(true)
985 #undef X
986 };
987
988 /** the lookup table for Proj(Call) names */
989 static const pns_lookup_t call_lut[] = {
990 #define X(a)    { pn_Call_##a, #a }
991         X(M),
992         X(X_regular),
993         X(X_except),
994         X(T_result),
995         X(P_value_res_base)
996 #undef X
997 };
998
999 /** the lookup table for Proj(Quot) names */
1000 static const pns_lookup_t quot_lut[] = {
1001 #define X(a)    { pn_Quot_##a, #a }
1002         X(M),
1003         X(X_regular),
1004         X(X_except),
1005         X(res)
1006 #undef X
1007 };
1008
1009 /** the lookup table for Proj(DivMod) names */
1010 static const pns_lookup_t divmod_lut[] = {
1011 #define X(a)    { pn_DivMod_##a, #a }
1012         X(M),
1013         X(X_regular),
1014         X(X_except),
1015         X(res_div),
1016         X(res_mod)
1017 #undef X
1018 };
1019
1020 /** the lookup table for Proj(Div) names */
1021 static const pns_lookup_t div_lut[] = {
1022 #define X(a)    { pn_Div_##a, #a }
1023         X(M),
1024         X(X_regular),
1025         X(X_except),
1026         X(res)
1027 #undef X
1028 };
1029
1030 /** the lookup table for Proj(Mod) names */
1031 static const pns_lookup_t mod_lut[] = {
1032 #define X(a)    { pn_Mod_##a, #a }
1033         X(M),
1034         X(X_regular),
1035         X(X_except),
1036         X(res)
1037 #undef X
1038 };
1039
1040 /** the lookup table for Proj(Load) names */
1041 static const pns_lookup_t load_lut[] = {
1042 #define X(a)    { pn_Load_##a, #a }
1043         X(M),
1044         X(X_regular),
1045         X(X_except),
1046         X(res)
1047 #undef X
1048 };
1049
1050 /** the lookup table for Proj(Store) names */
1051 static const pns_lookup_t store_lut[] = {
1052 #define X(a)    { pn_Store_##a, #a }
1053         X(M),
1054         X(X_regular),
1055         X(X_except)
1056 #undef X
1057 };
1058
1059 /** the lookup table for Proj(Alloc) names */
1060 static const pns_lookup_t alloc_lut[] = {
1061 #define X(a)    { pn_Alloc_##a, #a }
1062         X(M),
1063         X(X_regular),
1064         X(X_except),
1065         X(res)
1066 #undef X
1067 };
1068
1069 /** the lookup table for Proj(CopyB) names */
1070 static const pns_lookup_t copyb_lut[] = {
1071 #define X(a)    { pn_CopyB_##a, #a }
1072         X(M),
1073         X(X_regular),
1074         X(X_except),
1075 #undef X
1076 };
1077
1078 /** the lookup table for Proj(InstOf) names */
1079 static const pns_lookup_t instof_lut[] = {
1080 #define X(a)    { pn_InstOf_##a, #a }
1081         X(M),
1082         X(X_regular),
1083         X(X_except),
1084         X(res),
1085 #undef X
1086 };
1087
1088 /** the lookup table for Proj(Raise) names */
1089 static const pns_lookup_t raise_lut[] = {
1090 #define X(a)    { pn_Raise_##a, #a }
1091         X(M),
1092         X(X),
1093 #undef X
1094 };
1095
1096 /** the lookup table for Proj(Bound) names */
1097 static const pns_lookup_t bound_lut[] = {
1098 #define X(a)    { pn_Bound_##a, #a }
1099         X(M),
1100         X(X_regular),
1101         X(X_except),
1102         X(res),
1103 #undef X
1104 };
1105
1106 /** the Proj lookup table */
1107 static const proj_lookup_t proj_lut[] = {
1108 #define E(a)  ARR_SIZE(a), a
1109         { iro_Start,   E(start_lut) },
1110         { iro_Cond,    E(cond_lut) },
1111         { iro_Call,    E(call_lut) },
1112         { iro_Quot,    E(quot_lut) },
1113         { iro_DivMod,  E(divmod_lut) },
1114         { iro_Div,     E(div_lut) },
1115         { iro_Mod,     E(mod_lut) },
1116         { iro_Load,    E(load_lut) },
1117         { iro_Store,   E(store_lut) },
1118         { iro_Alloc,   E(alloc_lut) },
1119         { iro_CopyB,   E(copyb_lut) },
1120         { iro_InstOf,  E(instof_lut) },
1121         { iro_Raise,   E(raise_lut) },
1122         { iro_Bound,   E(bound_lut) }
1123 #undef E
1124 };
1125
1126 /**
1127  * Dump additional node attributes of some nodes to a file F.
1128  */
1129 static int dump_node_nodeattr(FILE *F, ir_node *n)
1130 {
1131         int bad = 0;
1132         ir_node *pred;
1133         ir_opcode code;
1134         long proj_nr;
1135         const ir_op_ops *ops = get_op_ops(get_irn_op(n));
1136
1137         /* call the dump_node operation if available */
1138         if (ops->dump_node)
1139                 return ops->dump_node(n, F, dump_node_nodeattr_txt);
1140
1141         switch (get_irn_opcode(n)) {
1142         case iro_Const:
1143                 ir_fprintf(F, "%T ", get_Const_tarval(n));
1144                 break;
1145
1146         case iro_Proj:
1147                 pred    = get_Proj_pred(n);
1148                 proj_nr = get_Proj_proj(n);
1149 #ifdef INTERPROCEDURAL_VIEW
1150 handle_lut:
1151 #endif
1152                 code    = get_irn_opcode(pred);
1153
1154                 if (code == iro_Cmp)
1155                         fprintf(F, "%s ", get_pnc_string(get_Proj_proj(n)));
1156                 else if (code == iro_Proj && get_irn_opcode(get_Proj_pred(pred)) == iro_Start)
1157                         fprintf(F, "Arg %ld ", proj_nr);
1158                 else if (code == iro_Cond && get_irn_mode(get_Cond_selector(pred)) != mode_b)
1159                         fprintf(F, "%ld ", proj_nr);
1160                 else {
1161                         unsigned i, j, f = 0;
1162
1163                         for (i = 0; i < ARR_SIZE(proj_lut); ++i) {
1164                                 if (code == proj_lut[i].code) {
1165                                         for (j = 0; j < proj_lut[i].num_data; ++j) {
1166                                                 if (proj_nr == proj_lut[i].data[j].nr) {
1167                                                         fprintf(F, "%s ", proj_lut[i].data[j].name);
1168                                                         f = 1;
1169                                                         break;
1170                                                 }
1171                                         }
1172                                         break;
1173                                 }
1174                         }
1175                         if (! f)
1176                                 fprintf(F, "%ld ", proj_nr);
1177                         if (code == iro_Cond && get_Cond_jmp_pred(pred) != COND_JMP_PRED_NONE) {
1178                                 if (proj_nr == pn_Cond_false && get_Cond_jmp_pred(pred) == COND_JMP_PRED_FALSE)
1179                                         fprintf(F, "PRED ");
1180                                 if (proj_nr == pn_Cond_true && get_Cond_jmp_pred(pred) == COND_JMP_PRED_TRUE)
1181                                         fprintf(F, "PRED ");
1182                         }
1183                 }
1184                 break;
1185         case iro_Filter:
1186                 proj_nr = get_Filter_proj(n);
1187 #ifdef INTERPROCEDURAL_VIEW
1188                 if (! get_interprocedural_view()) {
1189                         /* it's a Proj' */
1190                         pred    = get_Filter_pred(n);
1191                         goto handle_lut;
1192                 } else
1193 #endif
1194                         fprintf(F, "%ld ", proj_nr);
1195                 break;
1196         case iro_Sel:
1197                 fprintf(F, "%s ", get_ent_dump_name(get_Sel_entity(n)));
1198                 break;
1199         case iro_Cast:
1200                 ir_fprintf(F, "(%+F)", get_Cast_type(n));
1201                 break;
1202         case iro_Confirm:
1203                 fprintf(F, "%s ", get_pnc_string(get_Confirm_cmp(n)));
1204                 break;
1205         case iro_CopyB:
1206                 ir_fprintf(F, "(%+F)", get_CopyB_type(n));
1207                 break;
1208
1209         default:
1210                 ;
1211         } /* end switch */
1212
1213         return bad;
1214 }
1215
1216 #include <math.h>
1217 #include "execution_frequency.h"
1218
1219 static void dump_node_ana_vals(FILE *F, ir_node *n)
1220 {
1221         (void) F;
1222         (void) n;
1223         return;
1224 #ifdef INTERPROCEDURAL_VIEW
1225         fprintf(F, " %lf*(%2.0lf + %2.0lf) = %2.0lf ",
1226                 get_irn_exec_freq(n),
1227                 get_irg_method_execution_frequency(get_irn_irg(n)),
1228                 pow(5, get_irg_recursion_depth(get_irn_irg(n))),
1229                 get_irn_exec_freq(n) * (get_irg_method_execution_frequency(get_irn_irg(n)) + pow(5, get_irg_recursion_depth(get_irn_irg(n))))
1230         );
1231 #endif
1232 }
1233
1234
1235 /* Dumps a node label without the enclosing ". */
1236 int dump_node_label(FILE *F, ir_node *n)
1237 {
1238         int bad = 0;
1239
1240         bad |= dump_node_opcode(F, n);
1241         fputs(" ", F);
1242         bad |= dump_node_mode(F, n);
1243         fprintf(F, " ");
1244         bad |= dump_node_typeinfo(F, n);
1245         bad |= dump_node_nodeattr(F, n);
1246         if (dump_node_idx_labels) {
1247                 fprintf(F, "%ld:%d", get_irn_node_nr(n), get_irn_idx(n));
1248         } else {
1249                 fprintf(F, "%ld", get_irn_node_nr(n));
1250         }
1251
1252         return bad;
1253 }
1254
1255 /* Dumps the vrp information of a node to a file */
1256 int dump_vrp_info(FILE *F, ir_node *n)
1257 {
1258         vrp_attr *vrp = vrp_get_info(n);
1259         if (!n) {
1260                 return 1;
1261         }
1262
1263         fprintf(F, "range_type: %d\n", vrp->range_type);
1264         if (vrp->range_type == VRP_RANGE || vrp->range_type ==
1265                         VRP_ANTIRANGE) {
1266                 ir_fprintf(F, "range_bottom: %F\n",vrp->range_bottom);
1267                 ir_fprintf(F, "range_top: %F\n", vrp->range_top);
1268         }
1269         ir_fprintf(F, "bits_set: %T\n", vrp->bits_set);
1270         ir_fprintf(F, "bits_not_set: %T\n", vrp->bits_not_set);
1271
1272         return 0;
1273 }
1274
1275 /**
1276  * Dumps the attributes of a node n into the file F.
1277  * Currently this is only the color of a node.
1278  */
1279 static void dump_node_vcgattr(FILE *F, ir_node *node, ir_node *local, int bad)
1280 {
1281         ir_mode *mode;
1282         ir_node *n;
1283
1284         if (bad) {
1285                 print_vcg_color(F, ird_color_error);
1286                 return;
1287         }
1288
1289         if (dump_node_vcgattr_hook)
1290                 if (dump_node_vcgattr_hook(F, node, local))
1291                         return;
1292
1293         n = local ? local : node;
1294
1295         if (overrule_nodecolor != ird_color_default_node) {
1296                 print_vcg_color(F, overrule_nodecolor);
1297                 return;
1298         }
1299
1300         mode = get_irn_mode(n);
1301         if (mode == mode_M) {
1302                 print_vcg_color(F, ird_color_memory);
1303                 return;
1304         }
1305         if (mode == mode_X) {
1306                 print_vcg_color(F, ird_color_controlflow);
1307                 return;
1308         }
1309
1310         switch (get_irn_opcode(n)) {
1311         case iro_Start:
1312         case iro_EndReg:
1313         case iro_EndExcept:
1314         case iro_End:
1315                 print_vcg_color(F, ird_color_anchor);
1316                 break;
1317         case iro_Bad:
1318                 print_vcg_color(F, ird_color_error);
1319                 break;
1320         case iro_Block:
1321                 if (is_Block_dead(n))
1322                         print_vcg_color(F, ird_color_dead_block_background);
1323                 else
1324                         print_vcg_color(F, ird_color_block_background);
1325                 break;
1326         case iro_Phi:
1327                 print_vcg_color(F, ird_color_phi);
1328                 break;
1329         case iro_Pin:
1330                 print_vcg_color(F, ird_color_memory);
1331                 break;
1332         case iro_SymConst:
1333         case iro_Const:
1334                 print_vcg_color(F, ird_color_const);
1335                 break;
1336         case iro_Proj:
1337                 print_vcg_color(F, ird_color_proj);
1338                 break;
1339         default: {
1340                 ir_op *op = get_irn_op(node);
1341
1342                 if (is_op_constlike(op)) {
1343                         print_vcg_color(F, ird_color_const);
1344                 } else if (is_op_uses_memory(op)) {
1345                         print_vcg_color(F, ird_color_uses_memory);
1346                 } else if (is_op_cfopcode(op) || is_op_forking(op)) {
1347                         print_vcg_color(F, ird_color_controlflow);
1348                 } else {
1349                         PRINT_DEFAULT_NODE_ATTR;
1350                 }
1351         }
1352         }
1353 }
1354
1355 /* Adds a new node info dumper callback. */
1356 void *dump_add_node_info_callback(dump_node_info_cb_t *cb, void *data)
1357 {
1358         hook_entry_t *info = XMALLOC(hook_entry_t);
1359
1360         info->hook._hook_node_info = cb;
1361         info->context              = data;
1362         register_hook(hook_node_info, info);
1363
1364         return info;
1365 }
1366
1367 /* Remove a previously added info dumper callback. */
1368 void dump_remv_node_info_callback(void *handle)
1369 {
1370         hook_entry_t *info = handle;
1371         unregister_hook(hook_node_info, info);
1372         xfree(info);
1373 }
1374
1375 /**
1376  * Dump the node information of a node n to a file F.
1377  */
1378 static inline int dump_node_info(FILE *F, ir_node *n)
1379 {
1380         int bad = 0;
1381         const ir_op_ops *ops = get_op_ops(get_irn_op(n));
1382
1383         fprintf(F, " info1: \"");
1384         bad = dump_irnode_to_file(F, n);
1385         /* call the dump_node operation if available */
1386         if (ops->dump_node)
1387                 bad = ops->dump_node(n, F, dump_node_info_txt);
1388
1389         /* allow additional info to be added */
1390         hook_node_info(F, n);
1391         fprintf(F, "\"\n");
1392
1393         return bad;
1394 }
1395
1396 static inline int is_constlike_node(const ir_node *node)
1397 {
1398         const ir_op *op = get_irn_op(node);
1399         return is_op_constlike(op);
1400 }
1401
1402
1403 /** outputs the predecessors of n, that are constants, local.  I.e.,
1404    generates a copy of the constant predecessors for each node called with. */
1405 static void dump_const_node_local(FILE *F, ir_node *n)
1406 {
1407         int i;
1408         if (!get_opt_dump_const_local()) return;
1409
1410         /* Use visited flag to avoid outputting nodes twice.
1411         initialize it first. */
1412         for (i = 0; i < get_irn_arity(n); i++) {
1413                 ir_node *con = get_irn_n(n, i);
1414                 if (is_constlike_node(con)) {
1415                         set_irn_visited(con, get_irg_visited(current_ir_graph) - 1);
1416                 }
1417         }
1418
1419         for (i = 0; i < get_irn_arity(n); i++) {
1420                 ir_node *con = get_irn_n(n, i);
1421                 if (is_constlike_node(con) && !irn_visited(con)) {
1422                         int bad = 0;
1423
1424                         mark_irn_visited(con);
1425                         /* Generate a new name for the node by appending the names of
1426                         n and const. */
1427                         fprintf(F, "node: {title: "); PRINT_CONSTID(n, con);
1428                         fprintf(F, " label: \"");
1429                         bad |= dump_node_label(F, con);
1430                         fprintf(F, "\" ");
1431                         bad |= dump_node_info(F, con);
1432                         dump_node_vcgattr(F, n, con, bad);
1433                         fprintf(F, "}\n");
1434                 }
1435         }
1436 }
1437
1438 /** If the block of an edge is a const_like node, dump it local with an edge */
1439 static void dump_const_block_local(FILE *F, ir_node *n)
1440 {
1441         ir_node *blk;
1442
1443         if (!get_opt_dump_const_local()) return;
1444
1445         blk = get_nodes_block(n);
1446         if (is_constlike_node(blk)) {
1447                 int bad = 0;
1448
1449                 /* Generate a new name for the node by appending the names of
1450                 n and blk. */
1451                 fprintf(F, "node: {title: \""); PRINT_CONSTBLKID(n, blk);
1452                 fprintf(F, "\" label: \"");
1453                 bad |= dump_node_label(F, blk);
1454                 fprintf(F, "\" ");
1455                 bad |= dump_node_info(F, blk);
1456                 dump_node_vcgattr(F, n, blk, bad);
1457                 fprintf(F, "}\n");
1458
1459                 fprintf(F, "edge: { sourcename: \"");
1460                 PRINT_NODEID(n);
1461                 fprintf(F, "\" targetname: \""); PRINT_CONSTBLKID(n,blk);
1462
1463                 if (dump_edge_vcgattr_hook) {
1464                         fprintf(F, "\" ");
1465                         if (dump_edge_vcgattr_hook(F, n, -1)) {
1466                                 fprintf(F, "}\n");
1467                                 return;
1468                         } else {
1469                                 fprintf(F, " " BLOCK_EDGE_ATTR "}\n");
1470                                 return;
1471                         }
1472                 }
1473
1474                 fprintf(F, "\" "   BLOCK_EDGE_ATTR "}\n");
1475         }
1476 }
1477
1478 /**
1479  * prints the error message of a node to a file F as info2.
1480  */
1481 static void print_node_error(FILE *F, const char *err_msg)
1482 {
1483         if (! err_msg)
1484                 return;
1485
1486         fprintf(F, " info2: \"%s\"", err_msg);
1487 }
1488
1489 /**
1490  * prints debug messages of a node to file F as info3.
1491  */
1492 static void print_dbg_info(FILE *F, dbg_info *dbg)
1493 {
1494         char buf[1024];
1495
1496         ir_dbg_info_snprint(buf, sizeof(buf), dbg);
1497         if (buf[0] != 0) {
1498                 fprintf(F, " info3: \"%s\"\n", buf);
1499         }
1500 }
1501
1502 static void print_type_dbg_info(FILE *F, type_dbg_info *dbg)
1503 {
1504         (void) F;
1505         (void) dbg;
1506         /* TODO */
1507 }
1508
1509 /**
1510  * Dump a node
1511  */
1512 static void dump_node(FILE *F, ir_node *n)
1513 {
1514         int bad = 0;
1515         const char *p;
1516
1517         if (get_opt_dump_const_local() && is_constlike_node(n))
1518                 return;
1519
1520         /* dump this node */
1521         fputs("node: {title: \"", F);
1522         PRINT_NODEID(n);
1523         fputs("\"", F);
1524
1525         fputs(" label: \"", F);
1526         bad = ! irn_vrfy_irg_dump(n, current_ir_graph, &p);
1527         bad |= dump_node_label(F, n);
1528         dump_node_ana_vals(F, n);
1529         //dump_node_ana_info(F, n);
1530         fputs("\" ", F);
1531
1532         if (get_op_flags(get_irn_op(n)) & irop_flag_dump_noinput) {
1533                 //fputs(" node_class:23", F);
1534         }
1535
1536         bad |= dump_node_info(F, n);
1537         print_node_error(F, p);
1538         print_dbg_info(F, get_irn_dbg_info(n));
1539         dump_node_vcgattr(F, n, NULL, bad);
1540         fputs("}\n", F);
1541         dump_const_node_local(F, n);
1542
1543         if (dump_node_edge_hook)
1544                 dump_node_edge_hook(F, n);
1545 }
1546
1547 /** dump the edge to the block this node belongs to */
1548 static void dump_ir_block_edge(FILE *F, ir_node *n)
1549 {
1550         if (get_opt_dump_const_local() && is_constlike_node(n)) return;
1551         if (is_no_Block(n)) {
1552                 ir_node *block = get_nodes_block(n);
1553
1554                 if (get_opt_dump_const_local() && is_constlike_node(block)) {
1555                         dump_const_block_local(F, n);
1556                 } else {
1557                         fprintf(F, "edge: { sourcename: \"");
1558                         PRINT_NODEID(n);
1559                         fprintf(F, "\" targetname: ");
1560                         fprintf(F, "\""); PRINT_NODEID(block); fprintf(F, "\"");
1561
1562                         if (dump_edge_vcgattr_hook) {
1563                                 fprintf(F, " ");
1564                                 if (dump_edge_vcgattr_hook(F, n, -1)) {
1565                                         fprintf(F, "}\n");
1566                                         return;
1567                                 } else {
1568                                         fprintf(F, " "  BLOCK_EDGE_ATTR "}\n");
1569                                         return;
1570                                 }
1571                         }
1572
1573                         fprintf(F, " "   BLOCK_EDGE_ATTR "}\n");
1574                 }
1575         }
1576 }
1577
1578 static void print_data_edge_vcgattr(FILE *F, ir_node *from, int to)
1579 {
1580         /*
1581          * do not use get_nodes_block() here, will fail
1582          * if the irg is not pinned.
1583          */
1584         if (get_irn_n(from, -1) == get_irn_n(get_irn_n(from, to), -1))
1585                 fprintf(F, INTRA_DATA_EDGE_ATTR);
1586         else
1587                 fprintf(F, INTER_DATA_EDGE_ATTR);
1588 }
1589
1590 static void print_mem_edge_vcgattr(FILE *F, ir_node *from, int to)
1591 {
1592         /*
1593          * do not use get_nodes_block() here, will fail
1594          * if the irg is not pinned.
1595          */
1596         if (get_irn_n(from, -1) == get_irn_n(get_irn_n(from, to), -1))
1597                 fprintf(F, INTRA_MEM_EDGE_ATTR);
1598         else
1599                 fprintf(F, INTER_MEM_EDGE_ATTR);
1600 }
1601
1602 /** Print the vcg attributes for the edge from node from to it's to's input */
1603 static void print_edge_vcgattr(FILE *F, ir_node *from, int to)
1604 {
1605         assert(from);
1606
1607         if (dump_edge_vcgattr_hook)
1608                 if (dump_edge_vcgattr_hook(F, from, to))
1609                         return;
1610
1611         if (dump_backedge_information_flag && is_backedge(from, to))
1612                 fprintf(F, BACK_EDGE_ATTR);
1613
1614         switch (get_irn_opcode(from)) {
1615         case iro_Block:
1616                 fprintf(F, CF_EDGE_ATTR);
1617                 break;
1618         case iro_Start:  break;
1619         case iro_End:
1620                 if (to >= 0) {
1621                         if (get_irn_mode(get_End_keepalive(from, to)) == mode_BB)
1622                                 fprintf(F, KEEP_ALIVE_CF_EDGE_ATTR);
1623                         else
1624                                 fprintf(F, KEEP_ALIVE_DF_EDGE_ATTR);
1625                 }
1626                 break;
1627         default:
1628                 if (is_Proj(from)) {
1629                         if (get_irn_mode(from) == mode_M)
1630                                 print_mem_edge_vcgattr(F, from, to);
1631                         else if (get_irn_mode(from) == mode_X)
1632                                 fprintf(F, CF_EDGE_ATTR);
1633                         else
1634                                 print_data_edge_vcgattr(F, from, to);
1635                 }
1636                 else if (get_irn_mode(get_irn_n(from, to)) == mode_M)
1637                         print_mem_edge_vcgattr(F, from, to);
1638                 else if (get_irn_mode(get_irn_n(from, to)) == mode_X)
1639                         fprintf(F, CF_EDGE_ATTR);
1640                 else
1641                         print_data_edge_vcgattr(F, from, to);
1642         }
1643 }
1644
1645 /** dump edges to our inputs */
1646 static void dump_ir_data_edges(FILE *F, ir_node *n)
1647 {
1648         int i, num;
1649
1650         if (!dump_keepalive && is_End(n)) {
1651                 /* the End node has only keep-alive edges */
1652                 return;
1653         }
1654
1655         /* dump the dependency edges. */
1656         num = get_irn_deps(n);
1657         for (i = 0; i < num; ++i) {
1658                 ir_node *dep = get_irn_dep(n, i);
1659
1660                 if (dep) {
1661                         print_node_edge_kind(F, n);
1662                         fprintf(F, "{sourcename: \"");
1663                         PRINT_NODEID(n);
1664                         fprintf(F, "\" targetname: ");
1665                         if ((get_opt_dump_const_local()) && is_constlike_node(dep)) {
1666                                 PRINT_CONSTID(n, dep);
1667                         } else {
1668                                 fprintf(F, "\"");
1669                                 PRINT_NODEID(dep);
1670                                 fprintf(F, "\"");
1671                         }
1672                         fprintf(F, " label: \"%d\" ", i);
1673                         fprintf(F, " color: darkgreen}\n");
1674                 }
1675         }
1676
1677         num = get_irn_arity(n);
1678         for (i = 0; i < num; i++) {
1679                 ir_node *pred = get_irn_n(n, i);
1680                 assert(pred);
1681
1682 #ifdef INTERPROCEDURAL_VIEW
1683                 if ((get_interprocedural_view() && get_irn_visited(pred) < get_irn_visited(n)))
1684                         continue; /* pred not dumped */
1685 #endif
1686
1687                 if (dump_backedge_information_flag && is_backedge(n, i))
1688                         fprintf(F, "backedge: {sourcename: \"");
1689                 else {
1690                         print_node_edge_kind(F, n);
1691                         fprintf(F, "{sourcename: \"");
1692                 }
1693                 PRINT_NODEID(n);
1694                 fprintf(F, "\" targetname: ");
1695                 if ((get_opt_dump_const_local()) && is_constlike_node(pred)) {
1696                         PRINT_CONSTID(n, pred);
1697                 } else {
1698                         fprintf(F, "\""); PRINT_NODEID(pred); fprintf(F, "\"");
1699                 }
1700                 fprintf(F, " label: \"%d\" ", i);
1701                 print_edge_vcgattr(F, n, i);
1702                 fprintf(F, "}\n");
1703         }
1704
1705         if (dump_macro_block_edges && is_Block(n)) {
1706                 ir_node *mb = get_Block_MacroBlock(n);
1707                 fprintf(F, "edge: {sourcename: \"");
1708                 PRINT_NODEID(n);
1709                 fprintf(F, "\" targetname: \"");
1710                 PRINT_NODEID(mb);
1711                 fprintf(F, "\" label: \"mb\" " MACROBLOCK_EDGE_ATTR);
1712                 fprintf(F, "}\n");
1713         }
1714 }
1715
1716 /**
1717  * Dump the ir_edges
1718  */
1719 static void dump_ir_edges(FILE *F, ir_node *n)
1720 {
1721         const ir_edge_t *edge;
1722         int i = 0;
1723
1724         foreach_out_edge(n, edge) {
1725                 ir_node *succ = get_edge_src_irn(edge);
1726
1727                 print_node_edge_kind(F, succ);
1728                 fprintf(F, "{sourcename: \"");
1729                 PRINT_NODEID(n);
1730                 fprintf(F, "\" targetname: \"");
1731                 PRINT_NODEID(succ);
1732                 fprintf(F, "\"");
1733
1734                 fprintf(F, " label: \"%d\" ", i);
1735                 fprintf(F, OUT_EDGE_ATTR);
1736                 fprintf(F, "}\n");
1737                 ++i;
1738         }
1739 }
1740
1741
1742 /** Dumps a node and its edges but not the block edge  */
1743 static void dump_node_wo_blockedge(ir_node *n, void *env)
1744 {
1745         FILE *F = env;
1746         dump_node(F, n);
1747         dump_ir_data_edges(F, n);
1748 }
1749
1750 /** Dumps a node and its edges. */
1751 static void dump_whole_node(ir_node *n, void *env)
1752 {
1753         FILE *F = env;
1754         dump_node_wo_blockedge(n, env);
1755         if (!node_floats(n))
1756                 dump_ir_block_edge(F, n);
1757         if (dump_new_edges_flag && edges_activated(current_ir_graph))
1758                 dump_ir_edges(F, n);
1759 }
1760
1761 /** Dumps a const-like node. */
1762 static void dump_const_node(ir_node *n, void *env)
1763 {
1764         if (is_Block(n)) return;
1765         dump_node_wo_blockedge(n, env);
1766 }
1767
1768 /***********************************************************************/
1769 /* the following routines dump the nodes/irgs bracketed to graphs.     */
1770 /***********************************************************************/
1771
1772 /** Dumps a constant expression as entity initializer, array bound ...
1773  */
1774 static void dump_const_expression(FILE *F, ir_node *value)
1775 {
1776         ir_graph *rem = current_ir_graph;
1777         int rem_dump_const_local = dump_const_local;
1778         dump_const_local = 0;
1779         current_ir_graph = get_const_code_irg();
1780         irg_walk(value, dump_const_node, NULL, F);
1781         /* Decrease visited flag so that we walk with the same flag for the next
1782            expression.  This guarantees that we don't dump the same node twice,
1783            as for const expressions cse is performed to save memory. */
1784         set_irg_visited(current_ir_graph, get_irg_visited(current_ir_graph) -1);
1785         current_ir_graph = rem;
1786         dump_const_local = rem_dump_const_local;
1787 }
1788
1789 /** Dump a block as graph containing its nodes.
1790  *
1791  *  Expects to find nodes belonging to the block as list in its
1792  *  link field.
1793  *  Dumps the edges of all nodes including itself. */
1794 static void dump_whole_block(FILE *F, ir_node *block)
1795 {
1796         ir_node *node;
1797         ird_color_t color = ird_color_block_background;
1798
1799         assert(is_Block(block));
1800
1801         fprintf(F, "graph: { title: \"");
1802         PRINT_NODEID(block);
1803         fprintf(F, "\"  label: \"");
1804         dump_node_label(F, block);
1805
1806         /* colorize blocks */
1807         if (! get_Block_matured(block))
1808                 color = ird_color_block_background;
1809         if (is_Block_dead(block))
1810                 color = ird_color_dead_block_background;
1811
1812         fprintf(F, "\" status:clustered ");
1813         print_vcg_color(F, color);
1814         fprintf(F, "\n");
1815
1816         /* yComp can show attributes for blocks, XVCG parses but ignores them */
1817         dump_node_info(F, block);
1818         print_dbg_info(F, get_irn_dbg_info(block));
1819
1820         /* dump the blocks edges */
1821         dump_ir_data_edges(F, block);
1822
1823         if (dump_block_edge_hook)
1824                 dump_block_edge_hook(F, block);
1825
1826         /* dump the nodes that go into the block */
1827         for (node = ird_get_irn_link(block); node; node = ird_get_irn_link(node)) {
1828                 dump_node(F, node);
1829                 dump_ir_data_edges(F, node);
1830         }
1831
1832         /* Close the vcg information for the block */
1833         fprintf(F, "}\n");
1834         dump_const_node_local(F, block);
1835         fprintf(F, "\n");
1836 }
1837
1838 /** dumps a graph block-wise. Expects all blockless nodes in arr in irgs link.
1839  *  The outermost nodes: blocks and nodes not op_pin_state_pinned, Bad, Unknown. */
1840 static void dump_block_graph(FILE *F, ir_graph *irg)
1841 {
1842         int i;
1843         ir_graph *rem = current_ir_graph;
1844         ir_node **arr = ird_get_irg_link(irg);
1845         current_ir_graph = irg;
1846
1847         for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
1848                 ir_node * node = arr[i];
1849                 if (is_Block(node)) {
1850                 /* Dumps the block and all the nodes in the block, which are to
1851                         be found in Block->link. */
1852                         dump_whole_block(F, node);
1853                 } else {
1854                         /* Nodes that are not in a Block. */
1855                         dump_node(F, node);
1856                         if (!node_floats(node) && is_Bad(get_nodes_block(node))) {
1857                                 dump_const_block_local(F, node);
1858                         }
1859                         dump_ir_data_edges(F, node);
1860                 }
1861                 if (dump_new_edges_flag && edges_activated(irg))
1862                         dump_ir_edges(F, node);
1863         }
1864
1865         if (dump_loop_information_flag && (get_irg_loopinfo_state(irg) & loopinfo_valid))
1866                 dump_loop_nodes_into_graph(F, irg);
1867
1868         current_ir_graph = rem;
1869 }
1870
1871 /**
1872  * Dump the info for an irg.
1873  * Parsed by XVCG but not shown. use yComp.
1874  */
1875 static void dump_graph_info(FILE *F, ir_graph *irg)
1876 {
1877         fprintf(F, "info1: \"");
1878         dump_entity_to_file(F, get_irg_entity(irg), dump_verbosity_entattrs | dump_verbosity_entconsts);
1879         fprintf(F, "\"\n");
1880 }
1881
1882 /** Dumps an irg as a graph clustered by block nodes.
1883  *  If interprocedural view edges can point to nodes out of this graph.
1884  */
1885 static void dump_graph_from_list(FILE *F, ir_graph *irg)
1886 {
1887         ir_entity *ent = get_irg_entity(irg);
1888
1889         fprintf(F, "graph: { title: \"");
1890         PRINT_IRGID(irg);
1891         fprintf(F, "\" label: \"%s\" status:clustered color:%s \n",
1892           get_ent_dump_name(ent), color_names[ird_color_prog_background]);
1893
1894         dump_graph_info(F, irg);
1895         print_dbg_info(F, get_entity_dbg_info(ent));
1896
1897         dump_block_graph(F, irg);
1898
1899         /* Close the vcg information for the irg */
1900         fprintf(F, "}\n\n");
1901 }
1902
1903 /** dumps a graph extended block-wise. Expects all blockless nodes in arr in irgs link.
1904  *  The outermost nodes: blocks and nodes not op_pin_state_pinned, Bad, Unknown. */
1905 static void dump_extblock_graph(FILE *F, ir_graph *irg)
1906 {
1907         int i;
1908         ir_graph *rem = current_ir_graph;
1909         ir_extblk **arr = ird_get_irg_link(irg);
1910         current_ir_graph = irg;
1911
1912         for (i = ARR_LEN(arr) - 1; i >= 0; --i) {
1913                 ir_extblk *extbb = arr[i];
1914                 ir_node *leader = get_extbb_leader(extbb);
1915                 int j;
1916
1917                 fprintf(F, "graph: { title: \"");
1918                 PRINT_EXTBBID(leader);
1919                 fprintf(F, "\"  label: \"ExtBB %ld\" status:clustered color:lightgreen\n",
1920                         get_irn_node_nr(leader));
1921
1922                 for (j = ARR_LEN(extbb->blks) - 1; j >= 0; --j) {
1923                         ir_node * node = extbb->blks[j];
1924                         if (is_Block(node)) {
1925                         /* Dumps the block and all the nodes in the block, which are to
1926                                 be found in Block->link. */
1927                                 dump_whole_block(F, node);
1928                         } else {
1929                                 /* Nodes that are not in a Block. */
1930                                 dump_node(F, node);
1931                                 if (is_Bad(get_nodes_block(node)) && !node_floats(node)) {
1932                                         dump_const_block_local(F, node);
1933                                 }
1934                                 dump_ir_data_edges(F, node);
1935                         }
1936                 }
1937                 fprintf(F, "}\n");
1938         }
1939
1940         if (dump_loop_information_flag && (get_irg_loopinfo_state(irg) & loopinfo_valid))
1941                 dump_loop_nodes_into_graph(F, irg);
1942
1943         current_ir_graph = rem;
1944         free_extbb(irg);
1945 }
1946
1947
1948 /*******************************************************************/
1949 /* Basic type and entity nodes and edges.                          */
1950 /*******************************************************************/
1951
1952 /** dumps the edges between nodes and their type or entity attributes. */
1953 static void dump_node2type_edges(ir_node *n, void *env)
1954 {
1955         FILE *F = env;
1956         assert(n);
1957
1958         switch (get_irn_opcode(n)) {
1959         case iro_Const :
1960                 /* @@@ some consts have an entity */
1961                 break;
1962         case iro_SymConst:
1963                 if (SYMCONST_HAS_TYPE(get_SymConst_kind(n)))
1964                         print_node_type_edge(F,n,get_SymConst_type(n),NODE2TYPE_EDGE_ATTR);
1965                 break;
1966         case iro_Sel:
1967                 print_node_ent_edge(F,n,get_Sel_entity(n),NODE2TYPE_EDGE_ATTR);
1968                 break;
1969         case iro_Call:
1970                 print_node_type_edge(F,n,get_Call_type(n),NODE2TYPE_EDGE_ATTR);
1971                 break;
1972         case iro_Alloc:
1973                 print_node_type_edge(F,n,get_Alloc_type(n),NODE2TYPE_EDGE_ATTR);
1974                 break;
1975         case iro_Free:
1976                 print_node_type_edge(F,n,get_Free_type(n),NODE2TYPE_EDGE_ATTR);
1977                 break;
1978         case iro_Cast:
1979                 print_node_type_edge(F,n,get_Cast_type(n),NODE2TYPE_EDGE_ATTR);
1980                 break;
1981         default:
1982                 break;
1983         }
1984 }
1985
1986 #if 0
1987 static int print_type_info(FILE *F, ir_type *tp)
1988 {
1989         int bad = 0;
1990
1991         if (get_type_state(tp) == layout_undefined) {
1992                 fprintf(F, "state: layout_undefined\n");
1993         } else {
1994                 fprintf(F, "state: layout_fixed,\n");
1995         }
1996         if (get_type_mode(tp))
1997                 fprintf(F, "mode: %s,\n", get_mode_name_ex(get_type_mode(tp), &bad));
1998         fprintf(F, "size: %db,\n", get_type_size_bits(tp));
1999
2000         return bad;
2001 }
2002
2003 static void print_typespecific_info(FILE *F, ir_type *tp)
2004 {
2005         switch (get_type_tpop_code(tp)) {
2006         case tpo_class:
2007                 fprintf(F, "peculiarity: %s\n", get_peculiarity_string(get_class_peculiarity(tp)));
2008                 break;
2009         case tpo_struct:
2010                 break;
2011         case tpo_method:
2012                 fprintf(F, "variadicity: %s\n", get_variadicity_name(get_method_variadicity(tp)));
2013                 fprintf(F, "params: %d\n", get_method_n_params(tp));
2014                 fprintf(F, "results: %d\n", get_method_n_ress(tp));
2015                 break;
2016         case tpo_union:
2017                 break;
2018         case tpo_array:
2019                 break;
2020         case tpo_enumeration:
2021                 break;
2022         case tpo_pointer:
2023                 break;
2024         case tpo_primitive:
2025                 break;
2026         default:
2027                 break;
2028         } /* switch type */
2029 }
2030 #endif
2031
2032 static void print_typespecific_vcgattr(FILE *F, ir_type *tp)
2033 {
2034         switch (get_type_tpop_code(tp)) {
2035         case tpo_class:
2036                 fprintf(F, " " TYPE_CLASS_NODE_ATTR);
2037                 break;
2038         case tpo_struct:
2039                 fprintf(F, " " TYPE_METH_NODE_ATTR);
2040                 break;
2041         case tpo_method:
2042                 break;
2043         case tpo_union:
2044                 break;
2045         case tpo_array:
2046                 break;
2047         case tpo_enumeration:
2048                 break;
2049         case tpo_pointer:
2050                 break;
2051         case tpo_primitive:
2052                 break;
2053         default:
2054                 break;
2055         } /* switch type */
2056 }
2057
2058 int dump_type_node(FILE *F, ir_type *tp)
2059 {
2060         int bad = 0;
2061
2062         fprintf(F, "node: {title: ");
2063         PRINT_TYPEID(tp);
2064         ir_fprintf(F, " label: \"%s %+F\" info1: \"", get_type_tpop_name(tp), tp);
2065         dump_type_to_file(F, tp, dump_verbosity_max);
2066         fprintf(F, "\"\n");
2067         print_type_dbg_info(F, get_type_dbg_info(tp));
2068         print_typespecific_vcgattr(F, tp);
2069         fprintf(F, "}\n");
2070
2071         return bad;
2072 }
2073
2074
2075 static void dump_entity_node(FILE *F, ir_entity *ent)
2076 {
2077         fprintf(F, "node: {title: \"");
2078         PRINT_ENTID(ent); fprintf(F, "\"");
2079         fprintf(F, DEFAULT_TYPE_ATTRIBUTE);
2080         fprintf(F, "label: ");
2081         fprintf(F, "\"%s\" ", get_ent_dump_name(ent));
2082
2083         print_vcg_color(F, ird_color_entity);
2084         fprintf(F, "\n info1: \"");
2085
2086         dump_entity_to_file(F, ent, dump_verbosity_entattrs | dump_verbosity_entconsts);
2087
2088         fprintf(F, "\"\n");
2089         print_dbg_info(F, get_entity_dbg_info(ent));
2090         fprintf(F, "}\n");
2091 }
2092
2093 static void dump_enum_item(FILE *F, ir_type *tp, int pos)
2094 {
2095         char buf[1024];
2096         ir_enum_const *ec = get_enumeration_const(tp, pos);
2097         ident         *id = get_enumeration_const_nameid(ec);
2098         tarval        *tv = get_enumeration_value(ec);
2099
2100         if (tv)
2101                 tarval_snprintf(buf, sizeof(buf), tv);
2102         else
2103                 strncpy(buf, "<not set>", sizeof(buf));
2104         fprintf(F, "node: {title: \"");
2105         PRINT_ITEMID(tp, pos); fprintf(F, "\"");
2106         fprintf(F, DEFAULT_ENUM_ITEM_ATTRIBUTE);
2107         fprintf(F, "label: ");
2108         fprintf(F, "\"enum item %s\" " ENUM_ITEM_NODE_ATTR, get_id_str(id));
2109         fprintf(F, "\n info1: \"value: %s\"}\n", buf);
2110 }
2111
2112 /**
2113  * Dumps a new style initializer.
2114  */
2115 static void dump_entity_initializer(FILE *F, const ir_entity *ent)
2116 {
2117         /* TODO */
2118         (void) F;
2119         (void) ent;
2120 }
2121
2122 /** Dumps a type or entity and it's edges. */
2123 static void dump_type_info(type_or_ent tore, void *env)
2124 {
2125         FILE *F = env;
2126         int i = 0;  /* to shutup gcc */
2127
2128         /* dump this type or entity */
2129
2130         switch (get_kind(tore.ent)) {
2131         case k_entity: {
2132                 ir_entity *ent = tore.ent;
2133                 ir_node *value;
2134                 /* The node */
2135                 dump_entity_node(F, ent);
2136                 /* The Edges */
2137                 /* skip this to reduce graph.  Member edge of type is parallel to this edge. *
2138                 fprintf(F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
2139                 ENT_OWN_EDGE_ATTR "}\n", ent, get_entity_owner(ent));*/
2140                 print_ent_type_edge(F,ent, get_entity_type(ent), ENT_TYPE_EDGE_ATTR);
2141                 if (is_Class_type(get_entity_owner(ent))) {
2142                         for (i = get_entity_n_overwrites(ent) - 1; i >= 0; --i)
2143                                 print_ent_ent_edge(F,ent, get_entity_overwrites(ent, i), 0, -1, ENT_OVERWRITES_EDGE_ATTR);
2144                 }
2145                 /* attached subgraphs */
2146                 if (const_entities) {
2147                         if (ent->initializer != NULL) {
2148                                 /* new style initializers */
2149                                 dump_entity_initializer(F, ent);
2150                         } else if (entity_has_compound_ent_values(ent)) {
2151                                 /* old style compound entity values */
2152                                 for (i = get_compound_ent_n_values(ent) - 1; i >= 0; --i) {
2153                                         value = get_compound_ent_value(ent, i);
2154                                         if (value) {
2155                                                 print_ent_node_edge(F, ent, value, ENT_VALUE_EDGE_ATTR, i);
2156                                                 dump_const_expression(F, value);
2157                                                 print_ent_ent_edge(F, ent, get_compound_ent_value_member(ent, i), 0, -1, ENT_CORR_EDGE_ATTR, i);
2158                                                 /*
2159                                                 fprintf(F, "edge: { sourcename: \"%p\" targetname: \"%p\" "
2160                                                 ENT_CORR_EDGE_ATTR  "}\n", GET_ENTID(ent),
2161                                                 get_compound_ent_value_member(ent, i), i);
2162                                                 */
2163                                         }
2164                                 }
2165                         }
2166                 }
2167                 break;
2168         }
2169         case k_type: {
2170                 ir_type *tp = tore.typ;
2171                 dump_type_node(F, tp);
2172                 /* and now the edges */
2173                 switch (get_type_tpop_code(tp)) {
2174                 case tpo_class:
2175                         for (i = get_class_n_supertypes(tp) - 1; i >= 0; --i)
2176                                 print_type_type_edge(F, tp, get_class_supertype(tp, i), TYPE_SUPER_EDGE_ATTR);
2177                         for (i = get_class_n_members(tp) - 1; i >= 0; --i)
2178                                 print_type_ent_edge(F, tp, get_class_member(tp, i), TYPE_MEMBER_EDGE_ATTR);
2179                         break;
2180                 case tpo_struct:
2181                         for (i = get_struct_n_members(tp) - 1; i >= 0; --i)
2182                                 print_type_ent_edge(F, tp, get_struct_member(tp, i), TYPE_MEMBER_EDGE_ATTR);
2183                         break;
2184                 case tpo_method:
2185                         for (i = get_method_n_params(tp) - 1; i >= 0; --i)
2186                                 print_type_type_edge(F, tp, get_method_param_type(tp, i), METH_PAR_EDGE_ATTR,i);
2187                         for (i = get_method_n_ress(tp) - 1; i >= 0; --i)
2188                                 print_type_type_edge(F, tp, get_method_res_type(tp, i), METH_RES_EDGE_ATTR,i);
2189                         break;
2190                 case tpo_union:
2191                         for (i = get_union_n_members(tp) - 1; i >= 0; --i)
2192                                 print_type_ent_edge(F, tp, get_union_member(tp, i), UNION_EDGE_ATTR);
2193                         break;
2194                 case tpo_array:
2195                         print_type_type_edge(F, tp, get_array_element_type(tp), ARR_ELT_TYPE_EDGE_ATTR);
2196                         print_type_ent_edge(F, tp, get_array_element_entity(tp), ARR_ENT_EDGE_ATTR);
2197                         for (i = get_array_n_dimensions(tp) - 1; i >= 0; --i) {
2198                                 ir_node *upper = get_array_upper_bound(tp, i);
2199                                 ir_node *lower = get_array_lower_bound(tp, i);
2200                                 print_node_type_edge(F, upper, tp, "label: \"upper %d\"", get_array_order(tp, i));
2201                                 print_node_type_edge(F, lower, tp, "label: \"lower %d\"", get_array_order(tp, i));
2202                                 dump_const_expression(F, upper);
2203                                 dump_const_expression(F, lower);
2204                         }
2205                         break;
2206                 case tpo_enumeration:
2207                         for (i = get_enumeration_n_enums(tp) - 1; i >= 0; --i) {
2208                                 dump_enum_item(F, tp, i);
2209                                 print_enum_item_edge(F, tp, i, "label: \"item %d\"", i);
2210                         }
2211                         break;
2212                 case tpo_pointer:
2213                         print_type_type_edge(F, tp, get_pointer_points_to_type(tp), PTR_PTS_TO_EDGE_ATTR);
2214                         break;
2215                 case tpo_primitive:
2216                         break;
2217                 default:
2218                         break;
2219                 } /* switch type */
2220                 break; /* case k_type */
2221         }
2222         default:
2223                 printf(" *** irdump,  dump_type_info(l.%i), faulty type.\n", __LINE__);
2224         } /* switch kind_or_entity */
2225 }
2226
2227 typedef struct _h_env {
2228         int dump_ent;
2229         FILE *f;
2230 } h_env_t;
2231
2232 /** For dumping class hierarchies.
2233  * Dumps a class type node and a superclass edge.
2234  * If env->dump_ent dumps entities of classes and overwrites edges.
2235  */
2236 static void dump_class_hierarchy_node(type_or_ent tore, void *ctx)
2237 {
2238         h_env_t *env = ctx;
2239         FILE *F = env->f;
2240         int i = 0;  /* to shutup gcc */
2241
2242         /* dump this type or entity */
2243         switch (get_kind(tore.ent)) {
2244         case k_entity: {
2245                 ir_entity *ent = tore.ent;
2246                 if (get_entity_owner(ent) == get_glob_type()) break;
2247                 if (!is_Method_type(get_entity_type(ent)))
2248                         break;  /* GL */
2249                 if (env->dump_ent && is_Class_type(get_entity_owner(ent))) {
2250                         /* The node */
2251                         dump_entity_node(F, ent);
2252                         /* The edges */
2253                         print_type_ent_edge(F, get_entity_owner(ent), ent, TYPE_MEMBER_EDGE_ATTR);
2254                         for (i = get_entity_n_overwrites(ent) - 1; i >= 0; --i)
2255                                 print_ent_ent_edge(F, get_entity_overwrites(ent, i), ent, 0, -1, ENT_OVERWRITES_EDGE_ATTR);
2256                 }
2257                 break;
2258         }
2259         case k_type: {
2260                 ir_type *tp = tore.typ;
2261                 if (tp == get_glob_type())
2262                         break;
2263                 switch (get_type_tpop_code(tp)) {
2264                 case tpo_class:
2265                         dump_type_node(F, tp);
2266                         /* and now the edges */
2267                         for (i = get_class_n_supertypes(tp) - 1; i >= 0; --i) {
2268                                 print_type_type_edge(F,tp,get_class_supertype(tp, i),TYPE_SUPER_EDGE_ATTR);
2269                         }
2270                         break;
2271                 default: break;
2272                 } /* switch type */
2273                 break; /* case k_type */
2274         }
2275         default:
2276                 printf(" *** irdump,  dump_class_hierarchy_node(l.%i), faulty type.\n", __LINE__);
2277         } /* switch kind_or_entity */
2278 }
2279
2280 /*******************************************************************/
2281 /* dump analysis information that is expressed in graph terms.     */
2282 /*******************************************************************/
2283
2284 /* dump out edges */
2285 static void dump_out_edge(ir_node *n, void *env)
2286 {
2287         FILE *F = env;
2288         int i;
2289         for (i = get_irn_n_outs(n) - 1; i >= 0; --i) {
2290                 ir_node *succ = get_irn_out(n, i);
2291                 assert(succ);
2292                 print_node_edge_kind(F, succ);
2293                 fprintf(F, "{sourcename: \"");
2294                 PRINT_NODEID(n);
2295                 fprintf(F, "\" targetname: \"");
2296                 PRINT_NODEID(succ);
2297                 fprintf(F, "\" color: red linestyle: dashed");
2298                 fprintf(F, "}\n");
2299         }
2300 }
2301
2302 static inline void dump_loop_label(FILE *F, ir_loop *loop)
2303 {
2304         fprintf(F, "loop %d, %d sons, %d nodes",
2305                 get_loop_depth(loop), get_loop_n_sons(loop), get_loop_n_nodes(loop));
2306 }
2307
2308 static inline void dump_loop_info(FILE *F, ir_loop *loop)
2309 {
2310         fprintf(F, " info1: \"");
2311         fprintf(F, " loop nr: %d", get_loop_loop_nr(loop));
2312 #ifdef DEBUG_libfirm   /* GL @@@ debug analyses */
2313         fprintf(F, "\n The loop was analyzed %d times.", PTR_TO_INT(get_loop_link(loop)));
2314 #endif
2315         fprintf(F, "\"");
2316 }
2317
2318 static inline void dump_loop_node(FILE *F, ir_loop *loop)
2319 {
2320         fprintf(F, "node: {title: \"");
2321         PRINT_LOOPID(loop);
2322         fprintf(F, "\" label: \"");
2323         dump_loop_label(F, loop);
2324         fprintf(F, "\" ");
2325         dump_loop_info(F, loop);
2326         fprintf(F, "}\n");
2327 }
2328
2329 static inline void dump_loop_node_edge(FILE *F, ir_loop *loop, int i)
2330 {
2331         assert(loop);
2332         fprintf(F, "edge: {sourcename: \"");
2333         PRINT_LOOPID(loop);
2334         fprintf(F, "\" targetname: \"");
2335         PRINT_NODEID(get_loop_node(loop, i));
2336         fprintf(F, "\" color: green");
2337         fprintf(F, "}\n");
2338 }
2339
2340 static inline void dump_loop_son_edge(FILE *F, ir_loop *loop, int i)
2341 {
2342         assert(loop);
2343         fprintf(F, "edge: {sourcename: \"");
2344         PRINT_LOOPID(loop);
2345         fprintf(F, "\" targetname: \"");
2346         PRINT_LOOPID(get_loop_son(loop, i));
2347         fprintf(F, "\" color: darkgreen label: \"%d\"}\n",
2348                 get_loop_element_pos(loop, get_loop_son(loop, i)));
2349 }
2350
2351 static void dump_loops(FILE *F, ir_loop *loop)
2352 {
2353         int i;
2354         /* dump this loop node */
2355         dump_loop_node(F, loop);
2356
2357         /* dump edges to nodes in loop -- only if it is a real loop */
2358         if (get_loop_depth(loop) != 0) {
2359                 for (i = get_loop_n_nodes(loop) - 1; i >= 0; --i) {
2360                         dump_loop_node_edge(F, loop, i);
2361                 }
2362         }
2363         for (i = get_loop_n_sons(loop) - 1; i >= 0; --i) {
2364                 dump_loops(F, get_loop_son(loop, i));
2365                 dump_loop_son_edge(F, loop, i);
2366         }
2367 }
2368
2369 static inline void dump_loop_nodes_into_graph(FILE *F, ir_graph *irg)
2370 {
2371         ir_loop *loop = get_irg_loop(irg);
2372
2373         if (loop != NULL) {
2374                 ir_graph *rem = current_ir_graph;
2375                 current_ir_graph = irg;
2376
2377                 dump_loops(F, loop);
2378
2379                 current_ir_graph = rem;
2380         }
2381 }
2382
2383
2384 /**
2385  * dumps the VCG header
2386  */
2387 void dump_vcg_header(FILE *F, const char *name, const char *layout, const char *orientation)
2388 {
2389         int   i;
2390         char *label;
2391
2392         init_colors();
2393
2394         label = edge_label ? "yes" : "no";
2395         if (! layout)     layout = "Compilergraph";
2396         if (!orientation) orientation = "bottom_to_top";
2397
2398         /* print header */
2399         fprintf(F,
2400                 "graph: { title: \"ir graph of %s\"\n"
2401                 "display_edge_labels: %s\n"
2402                 "layoutalgorithm: mindepth //$ \"%s\"\n"
2403                 "manhattan_edges: yes\n"
2404                 "port_sharing: no\n"
2405                 "orientation: %s\n"
2406                 "classname 1:  \"intrablock Data\"\n"
2407                 "classname 2:  \"Block\"\n"
2408                 "classname 3:  \"Entity type\"\n"
2409                 "classname 4:  \"Entity owner\"\n"
2410                 "classname 5:  \"Method Param\"\n"
2411                 "classname 6:  \"Method Res\"\n"
2412                 "classname 7:  \"Super\"\n"
2413                 "classname 8:  \"Union\"\n"
2414                 "classname 9:  \"Points-to\"\n"
2415                 "classname 10: \"Array Element Type\"\n"
2416                 "classname 11: \"Overwrites\"\n"
2417                 "classname 12: \"Member\"\n"
2418                 "classname 13: \"Control Flow\"\n"
2419                 "classname 14: \"intrablock Memory\"\n"
2420                 "classname 15: \"Dominators\"\n"
2421                 "classname 16: \"interblock Data\"\n"
2422                 "classname 17: \"interblock Memory\"\n"
2423                 "classname 18: \"Exception Control Flow for Interval Analysis\"\n"
2424                 "classname 19: \"Postdominators\"\n"
2425                 "classname 20: \"Keep Alive\"\n"
2426                 "classname 21: \"Out Edges\"\n"
2427                 "classname 22: \"Macro Block Edges\"\n"
2428                 //"classname 23: \"NoInput Nodes\"\n"
2429                 "infoname 1: \"Attribute\"\n"
2430                 "infoname 2: \"Verification errors\"\n"
2431                 "infoname 3: \"Debug info\"\n",
2432                 name, label, layout, orientation);
2433
2434         for (i = 0; i < ird_color_count; ++i) {
2435                 if (color_rgb[i] != NULL) {
2436                         fprintf(F, "colorentry %s: %s\n", color_names[i], color_rgb[i]);
2437                 }
2438         }
2439         fprintf(F, "\n");        /* a separator */
2440 }
2441
2442 /**
2443  * open a vcg file
2444  *
2445  * @param irg     The graph to be dumped
2446  * @param suffix1 first filename suffix
2447  * @param suffix2 second filename suffix
2448  */
2449 FILE *vcg_open(const ir_graph *irg, const char *suffix1, const char *suffix2)
2450 {
2451         FILE *F;
2452         const char *nm = get_irg_dump_name(irg);
2453         int len = strlen(nm), i, j;
2454         char *fname;  /* filename to put the vcg information in */
2455
2456         if (!suffix1) suffix1 = "";
2457         if (!suffix2) suffix2 = "";
2458
2459         /* open file for vcg graph */
2460         fname = XMALLOCN(char, len * 2 + strlen(suffix1) + strlen(suffix2) + 5);
2461
2462         /* strncpy (fname, nm, len); */     /* copy the filename */
2463         j = 0;
2464         for (i = 0; i < len; ++i) {  /* replace '/' in the name: escape by @. */
2465                 if (nm[i] == '/') {
2466                         fname[j] = '@'; j++; fname[j] = '1'; j++;
2467                 } else if (nm[i] == '@') {
2468                         fname[j] = '@'; j++; fname[j] = '2'; j++;
2469                 } else {
2470                         fname[j] = nm[i]; j++;
2471                 }
2472         }
2473         fname[j] = '\0';
2474         strcat(fname, suffix1);  /* append file suffix */
2475         strcat(fname, suffix2);  /* append file suffix */
2476         strcat(fname, ".vcg");   /* append the .vcg suffix */
2477
2478         /* vcg really expect only a <CR> at end of line, so
2479          * the "b"inary mode is what you mean (and even needed for Win32)
2480          */
2481         F = fopen(fname, "wb");  /* open file for writing */
2482         if (!F) {
2483                 perror(fname);
2484         }
2485         xfree(fname);
2486
2487         return F;
2488 }
2489
2490 /**
2491  * open a vcg file
2492  *
2493  * @param name    prefix file name
2494  * @param suffix  filename suffix
2495  */
2496 FILE *vcg_open_name(const char *name, const char *suffix)
2497 {
2498         FILE *F;
2499         char *fname;  /* filename to put the vcg information in */
2500         int i, j, len = strlen(name);
2501
2502         if (!suffix) suffix = "";
2503
2504         /** open file for vcg graph */
2505         fname = XMALLOCN(char, len * 2 + 5 + strlen(suffix));
2506         /* strcpy (fname, name);*/    /* copy the filename */
2507         j = 0;
2508         for (i = 0; i < len; ++i) {  /* replace '/' in the name: escape by @. */
2509                 if (name[i] == '/') {
2510                         fname[j] = '@'; j++; fname[j] = '1'; j++;
2511                 } else if (name[i] == '@') {
2512                         fname[j] = '@'; j++; fname[j] = '2'; j++;
2513                 } else {
2514                         fname[j] = name[i]; j++;
2515                 }
2516         }
2517         fname[j] = '\0';
2518         strcat(fname, suffix);
2519         strcat(fname, ".vcg");  /* append the .vcg suffix */
2520
2521         /* vcg really expect only a <CR> at end of line, so
2522          * the "b"inary mode is what you mean (and even needed for Win32)
2523          */
2524         F = fopen(fname, "wb");  /* open file for writing */
2525         if (!F) {
2526                 perror(fname);
2527         }
2528         xfree(fname);
2529
2530         return F;
2531 }
2532
2533 /**
2534  * Dumps the vcg file footer
2535  */
2536 void dump_vcg_footer(FILE *F)
2537 {
2538         fprintf(F, "}\n");
2539 }
2540
2541 /************************************************************************/
2542 /************************************************************************/
2543 /* Routines that dump all or parts of the firm representation to a file */
2544 /************************************************************************/
2545 /************************************************************************/
2546
2547 /************************************************************************/
2548 /* Dump ir graphs, different formats and additional information.        */
2549 /************************************************************************/
2550
2551 typedef void (*do_dump_graph_func) (ir_graph *irg, FILE *out);
2552
2553 static void do_dump(ir_graph *irg, const char *suffix, const char *suffix_ip,
2554                     const char *suffix_nonip, do_dump_graph_func dump_func)
2555 {
2556         FILE       *out;
2557         ir_graph   *rem;
2558         const char *suffix1;
2559
2560         if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg))))
2561                 return;
2562
2563         rem = current_ir_graph;
2564         current_ir_graph = irg;
2565         (void) suffix_ip;
2566 #ifdef INTERPROCEDURAL_VIEW
2567         if (get_interprocedural_view())
2568                 suffix1 = suffix_ip;
2569         else
2570 #endif
2571                 suffix1 = suffix_nonip;
2572         current_ir_graph = rem;
2573
2574         out = vcg_open(irg, suffix, suffix1);
2575         if (out != NULL) {
2576                 dump_func(irg, out);
2577                 fclose(out);
2578         }
2579 }
2580
2581 void dump_ir_graph_file(ir_graph *irg, FILE *out)
2582 {
2583         if (dump_backedge_information_flag
2584                         && get_irg_loopinfo_state(irg) != loopinfo_consistent) {
2585                 construct_backedges(irg);
2586         }
2587
2588         dump_vcg_header(out, get_irg_dump_name(irg), NULL, NULL);
2589
2590         /* call the dump graph hook */
2591         if (dump_ir_graph_hook) {
2592                 if (dump_ir_graph_hook(out, irg)) {
2593                         return;
2594                 }
2595         }
2596
2597         /* walk over the graph */
2598         /* dump_whole_node must be called in post visiting predecessors */
2599         ird_walk_graph(irg, NULL, dump_whole_node, out);
2600
2601         /* dump the out edges in a separate walk */
2602         if ((dump_out_edge_flag) && (get_irg_outs_state(irg) != outs_none)) {
2603                 irg_out_walk(get_irg_start(irg), dump_out_edge, NULL, out);
2604         }
2605
2606         dump_vcg_footer(out);
2607 }
2608
2609 /** Routine to dump a graph, blocks as conventional nodes.  */
2610 void dump_ir_graph(ir_graph *irg, const char *suffix )
2611 {
2612         do_dump(irg, suffix, "-pure-ip", "-pure", dump_ir_graph_file);
2613 }
2614
2615 void dump_ir_block_graph_file(ir_graph *irg, FILE *out)
2616 {
2617         int i;
2618
2619         dump_vcg_header(out, get_irg_dump_name(irg), NULL, NULL);
2620
2621         construct_block_lists(irg);
2622
2623         /*
2624          * If we are in the interprocedural view, we dump not
2625          * only the requested irg but also all irgs that can be reached
2626          * from irg.
2627          */
2628         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
2629                 ir_graph *g = get_irp_irg(i);
2630                 ir_node **arr = ird_get_irg_link(g);
2631                 if (arr) {
2632                         dump_graph_from_list(out, g);
2633                         DEL_ARR_F(arr);
2634                 }
2635         }
2636
2637         dump_vcg_footer(out);
2638 }
2639
2640 /* Dump a firm graph without explicit block nodes. */
2641 void dump_ir_block_graph(ir_graph *irg, const char *suffix)
2642 {
2643         do_dump(irg, suffix, "-ip", "", dump_ir_block_graph_file);
2644 }
2645
2646 void dump_ir_extblock_graph_file(ir_graph *irg, FILE *F)
2647 {
2648         int        i;
2649         ir_entity *ent = get_irg_entity(irg);
2650
2651         if (get_irg_extblk_state(irg) != extblk_valid)
2652                 compute_extbb(irg);
2653
2654         dump_vcg_header(F, get_irg_dump_name(irg), NULL, NULL);
2655
2656         construct_extblock_lists(irg);
2657
2658         fprintf(F, "graph: { title: \"");
2659         PRINT_IRGID(irg);
2660         fprintf(F, "\" label: \"%s\" status:clustered color: white \n",
2661                 get_ent_dump_name(ent));
2662
2663         dump_graph_info(F, irg);
2664         print_dbg_info(F, get_entity_dbg_info(ent));
2665
2666         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
2667                 ir_graph *irg     = get_irp_irg(i);
2668                 list_tuple *lists = ird_get_irg_link(irg);
2669
2670                 if (lists) {
2671                         /* dump the extended blocks first */
2672                         if (ARR_LEN(lists->extbb_list)) {
2673                                 ird_set_irg_link(irg, lists->extbb_list);
2674                                 dump_extblock_graph(F, irg);
2675                         }
2676
2677                         /* we may have blocks without extended blocks, bad for instance */
2678                         if (ARR_LEN(lists->blk_list)) {
2679                                 ird_set_irg_link(irg, lists->blk_list);
2680                                 dump_block_graph(F, irg);
2681                         }
2682
2683                         DEL_ARR_F(lists->extbb_list);
2684                         DEL_ARR_F(lists->blk_list);
2685                         xfree(lists);
2686                 }
2687         }
2688
2689         /* Close the vcg information for the irg */
2690         fprintf(F, "}\n\n");
2691
2692         dump_vcg_footer(F);
2693         free_extbb(irg);
2694 }
2695
2696 /* Dump a firm graph without explicit block nodes but grouped in extended blocks. */
2697 void dump_ir_extblock_graph(ir_graph *irg, const char *suffix)
2698 {
2699         do_dump(irg, suffix, "-ip", "", dump_ir_extblock_graph_file);
2700 }
2701
2702 void dump_ir_graph_w_types_file(ir_graph *irg, FILE *out)
2703 {
2704         ir_graph *rem = current_ir_graph;
2705         int       rem_dump_const_local;
2706
2707         rem                  = current_ir_graph;
2708         current_ir_graph     = irg;
2709         rem_dump_const_local = dump_const_local;
2710         /* dumping types does not work with local nodes */
2711         dump_const_local = 0;
2712
2713         dump_vcg_header(out, get_irg_dump_name(irg), NULL, NULL);
2714
2715         /* dump common ir graph */
2716         irg_walk(get_irg_end(irg), NULL, dump_whole_node, out);
2717         /* dump type info */
2718         type_walk_irg(irg, dump_type_info, NULL, out);
2719         inc_irg_visited(get_const_code_irg());
2720         /* dump edges from graph to type info */
2721         irg_walk(get_irg_end(irg), dump_node2type_edges, NULL, out);
2722
2723         dump_vcg_footer(out);
2724         dump_const_local = rem_dump_const_local;
2725         current_ir_graph = rem;
2726 }
2727
2728 /* dumps a graph with type information */
2729 void dump_ir_graph_w_types(ir_graph *irg, const char *suffix)
2730 {
2731         do_dump(irg, suffix, "-pure-wtypes-ip", "-pure-wtypes",
2732                 dump_ir_graph_w_types_file);
2733 }
2734
2735 void dump_ir_block_graph_w_types_file(ir_graph *irg, FILE *out)
2736 {
2737         int       i;
2738         int       rem_dump_const_local;
2739         ir_graph *rem = current_ir_graph;
2740
2741         rem_dump_const_local = dump_const_local;
2742         /* dumping types does not work with local nodes */
2743         dump_const_local = 0;
2744
2745         dump_vcg_header(out, get_irg_dump_name(irg), NULL, NULL);
2746
2747         /* dump common blocked ir graph */
2748         construct_block_lists(irg);
2749
2750         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
2751                 ir_node **arr = ird_get_irg_link(get_irp_irg(i));
2752                 if (arr) {
2753                         dump_graph_from_list(out, get_irp_irg(i));
2754                         DEL_ARR_F(arr);
2755                 }
2756         }
2757
2758         /* dump type info */
2759         current_ir_graph = irg;
2760         type_walk_irg(irg, dump_type_info, NULL, out);
2761         inc_irg_visited(get_const_code_irg());
2762
2763         /* dump edges from graph to type info */
2764         irg_walk(get_irg_end(irg), dump_node2type_edges, NULL, out);
2765
2766         dump_vcg_footer(out);
2767         dump_const_local = rem_dump_const_local;
2768         current_ir_graph = rem;
2769 }
2770
2771 void dump_ir_block_graph_w_types(ir_graph *irg, const char *suffix)
2772 {
2773         do_dump(irg, suffix, "-wtypes-ip", "-wtypes",
2774                 dump_ir_block_graph_w_types_file);
2775 }
2776
2777 /*---------------------------------------------------------------------*/
2778 /* The following routines dump a control flow graph.                   */
2779 /*---------------------------------------------------------------------*/
2780
2781 static void dump_block_to_cfg(ir_node *block, void *env)
2782 {
2783         FILE *F = env;
2784         int i, fl = 0;
2785         ir_node *pred;
2786
2787         if (is_Block(block)) {
2788                 /* This is a block. Dump a node for the block. */
2789                 fprintf(F, "node: {title: \""); PRINT_NODEID(block);
2790                 fprintf(F, "\" label: \"");
2791                 if (block == get_irg_start_block(get_irn_irg(block)))
2792                         fprintf(F, "Start ");
2793                 if (block == get_irg_end_block(get_irn_irg(block)))
2794                         fprintf(F, "End ");
2795
2796                 fprintf(F, "%s ", get_op_name(get_irn_op(block)));
2797                 PRINT_NODEID(block);
2798                 fprintf(F, "\" ");
2799                 fprintf(F, "info1:\"");
2800
2801                 /* the generic version. */
2802                 dump_irnode_to_file(F, block);
2803
2804                 /* Check whether we have bad predecessors to color the block. */
2805                 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i)
2806                         if ((fl = is_Bad(get_Block_cfgpred(block, i))))
2807                                 break;
2808
2809                 fprintf(F, "\"");  /* closing quote of info */
2810
2811                 if ((block == get_irg_start_block(get_irn_irg(block))) ||
2812                         (block == get_irg_end_block(get_irn_irg(block)))     )
2813                         fprintf(F, " color:blue ");
2814                 else if (fl)
2815                         fprintf(F, " color:yellow ");
2816
2817                 fprintf(F, "}\n");
2818                 /* Dump the edges */
2819                 for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i)
2820                         if (!is_Bad(skip_Proj(get_Block_cfgpred(block, i)))) {
2821                                 pred = get_nodes_block(skip_Proj(get_Block_cfgpred(block, i)));
2822                                 fprintf(F, "edge: { sourcename: \"");
2823                                 PRINT_NODEID(block);
2824                                 fprintf(F, "\" targetname: \"");
2825                                 PRINT_NODEID(pred);
2826                                 fprintf(F, "\"}\n");
2827                         }
2828
2829                 /* Dump dominator/postdominator edge */
2830                 if (dump_dominator_information_flag) {
2831                         if (get_irg_dom_state(current_ir_graph) == dom_consistent && get_Block_idom(block)) {
2832                                 pred = get_Block_idom(block);
2833                                 fprintf(F, "edge: { sourcename: \"");
2834                                 PRINT_NODEID(block);
2835                                 fprintf(F, "\" targetname: \"");
2836                                 PRINT_NODEID(pred);
2837                                 fprintf(F, "\" " DOMINATOR_EDGE_ATTR "}\n");
2838                         }
2839                         if (get_irg_postdom_state(current_ir_graph) == dom_consistent && get_Block_ipostdom(block)) {
2840                                 pred = get_Block_ipostdom(block);
2841                                 fprintf(F, "edge: { sourcename: \"");
2842                                 PRINT_NODEID(block);
2843                                 fprintf(F, "\" targetname: \"");
2844                                 PRINT_NODEID(pred);
2845                                 fprintf(F, "\" " POSTDOMINATOR_EDGE_ATTR "}\n");
2846                         }
2847                 }
2848         }
2849 }
2850
2851 void dump_cfg(ir_graph *irg, const char *suffix)
2852 {
2853         FILE *f;
2854         /* if a filter is set, dump only the irg's that match the filter */
2855         if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg))))
2856                 return;
2857
2858         f = vcg_open(irg, suffix, "-cfg");
2859         if (f != NULL) {
2860                 ir_graph *rem = current_ir_graph;
2861 #ifdef INTERPROCEDURAL_VIEW
2862                 int ipv = get_interprocedural_view();
2863 #endif
2864
2865                 current_ir_graph = irg;
2866                 dump_vcg_header(f, get_irg_dump_name(irg), NULL, NULL);
2867
2868 #ifdef INTERPROCEDURAL_VIEW
2869                 if (ipv) {
2870                         printf("Warning: dumping cfg not in interprocedural view!\n");
2871                         set_interprocedural_view(0);
2872                 }
2873 #endif
2874
2875                 /* walk over the blocks in the graph */
2876                 irg_block_walk(get_irg_end(irg), dump_block_to_cfg, NULL, f);
2877                 dump_node(f, get_irg_bad(irg));
2878
2879 #ifdef INTERPROCEDURAL_VIEW
2880                 set_interprocedural_view(ipv);
2881 #endif
2882                 dump_vcg_footer(f);
2883                 fclose(f);
2884                 current_ir_graph = rem;
2885         }
2886 }
2887
2888
2889 static void descend_and_dump(FILE *F, ir_node *n, int depth, pset *mark_set)
2890 {
2891         if (pset_find_ptr(mark_set, n))
2892                 return;
2893
2894         pset_insert_ptr(mark_set, n);
2895
2896         if (depth > 0) {
2897                 int i, start = is_Block(n) ? 0 : -1;
2898                 dump_whole_node(n, F);
2899                 for (i = start; i < get_irn_arity(n); ++i)
2900                         descend_and_dump(F, get_irn_n(n, i), depth-1, mark_set);
2901         } else {
2902                 dump_node(F, n);
2903                 /* Don't dump edges to nodes further out.  These might be edges to
2904                    nodes we already dumped, if there is a shorter path to these. */
2905         }
2906 }
2907
2908 static int subgraph_counter = 0;
2909 void dump_subgraph(ir_node *root, int depth, const char *suffix)
2910 {
2911         FILE *F;
2912         char buf[32];
2913
2914         sprintf(buf, "-subg_%03d", subgraph_counter++);
2915         F = vcg_open(get_irn_irg(root), suffix, buf);
2916         if (F != NULL) {
2917                 pset *mark_set = pset_new_ptr(1);
2918                 dump_vcg_header(F, get_irg_dump_name(get_irn_irg(root)), NULL, NULL);
2919                 descend_and_dump(F, root, depth, mark_set);
2920                 dump_vcg_footer(F);
2921                 fclose(F);
2922                 del_pset(mark_set);
2923         }
2924 }
2925
2926 void dump_callgraph(const char *suffix)
2927 {
2928         FILE *F = vcg_open_name("Callgraph", suffix);
2929
2930         if (F != NULL) {
2931                 int i, rem = edge_label;
2932                 //int colorize;
2933                 edge_label = 1;
2934                 dump_vcg_header(F, "Callgraph", "Hierarchiv", NULL);
2935
2936                 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
2937                         ir_graph *irg = get_irp_irg(i);
2938                         ir_entity *ent = get_irg_entity(irg);
2939                         int j;
2940                         int n_callees = get_irg_n_callees(irg);
2941
2942                         dump_entity_node(F, ent);
2943                         for (j = 0; j < n_callees; ++j) {
2944                                 ir_entity *c = get_irg_entity(get_irg_callee(irg, j));
2945                                 //if (id_is_prefix(prefix, get_entity_ld_ident(c))) continue;
2946                                 int be = is_irg_callee_backedge(irg, j);
2947                                 char *attr;
2948                                 attr = (be) ?
2949                                         "label:\"recursion %d\"" :
2950                                 "label:\"calls %d\"";
2951                                 print_ent_ent_edge(F, ent, c, be, ird_color_entity, attr, get_irg_callee_loop_depth(irg, j));
2952                         }
2953                 }
2954
2955                 edge_label = rem;
2956                 dump_vcg_footer(F);
2957                 fclose(F);
2958         }
2959 }
2960
2961 #if 0
2962 /* Dump all irgs in interprocedural view to a single file. */
2963 void dump_all_cg_block_graph(const char *suffix)
2964 {
2965         FILE *f = vcg_open_name("All_graphs", suffix);
2966
2967         if (f != NULL) {
2968                 int i;
2969                 int rem_view = get_interprocedural_view();
2970
2971                 set_interprocedural_view(1);
2972                 dump_vcg_header(f, "All_graphs", NULL);
2973
2974                 /* collect nodes in all irgs reachable in call graph*/
2975                 for (i = get_irp_n_irgs() - 1; i >= 0; --i)
2976                         ird_set_irg_link(get_irp_irg(i), NULL);
2977
2978                 cg_walk(clear_link, collect_node, NULL);
2979
2980                 /* dump all graphs */
2981                 for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
2982                         current_ir_graph = get_irp_irg(i);
2983                         assert(ird_get_irg_link(current_ir_graph));
2984                         dump_graph_from_list(f, current_ir_graph);
2985                         DEL_ARR_F(ird_get_irg_link(current_ir_graph));
2986                 }
2987
2988                 dump_vcg_footer(f);
2989                 fclose(f);
2990                 set_interprocedural_view(rem_view);
2991         }
2992 }
2993 #endif
2994
2995 /*---------------------------------------------------------------------*/
2996 /* the following routines dumps type information without any ir nodes. */
2997 /*---------------------------------------------------------------------*/
2998
2999 void dump_type_graph(ir_graph *irg, const char *suffix)
3000 {
3001         FILE *f;
3002
3003         /* if a filter is set, dump only the irg's that match the filter */
3004         if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg)))) return;
3005
3006         f = vcg_open(irg, suffix, "-type");
3007         if (f != NULL) {
3008                 ir_graph *rem = current_ir_graph;
3009                 current_ir_graph = irg;
3010
3011                 dump_vcg_header(f, get_irg_dump_name(irg), "Hierarchic", NULL);
3012
3013                 /* walk over the blocks in the graph */
3014                 type_walk_irg(irg, dump_type_info, NULL, f);
3015                 /* The walker for the const code can be called several times for the
3016                    same (sub) expression.  So that no nodes are dumped several times
3017                    we decrease the visited flag of the corresponding graph after each
3018                    walk.  So now increase it finally. */
3019                 inc_irg_visited(get_const_code_irg());
3020
3021                 dump_vcg_footer(f);
3022                 fclose(f);
3023                 current_ir_graph = rem;
3024         }
3025 }
3026
3027 void dump_all_types(const char *suffix)
3028 {
3029         FILE *f = vcg_open_name("All_types", suffix);
3030         if (f != NULL) {
3031                 dump_vcg_header(f, "All_types", "Hierarchic", NULL);
3032                 type_walk(dump_type_info, NULL, f);
3033                 inc_irg_visited(get_const_code_irg());
3034
3035                 dump_vcg_footer(f);
3036                 fclose(f);
3037         }
3038 }
3039
3040 void dump_class_hierarchy(int entities, const char *suffix)
3041 {
3042         FILE *f = vcg_open_name("class_hierarchy", suffix);
3043
3044         if (f != NULL) {
3045                 h_env_t env;
3046                 env.f        = f;
3047                 env.dump_ent = entities;
3048                 dump_vcg_header(f, "class_hierarchy", "Hierarchic", NULL);
3049                 type_walk(dump_class_hierarchy_node, NULL, &env);
3050
3051                 dump_vcg_footer(f);
3052                 fclose(f);
3053         }
3054 }
3055
3056 /*---------------------------------------------------------------------*/
3057 /* dumps all graphs with the graph-dumper passed. Possible dumpers:    */
3058 /*  dump_ir_graph                                                      */
3059 /*  dump_ir_block_graph                                                */
3060 /*  dump_cfg                                                           */
3061 /*  dump_type_graph                                                    */
3062 /*  dump_ir_graph_w_types                                              */
3063 /*---------------------------------------------------------------------*/
3064
3065 void dump_all_ir_graphs(dump_graph_func *dmp_grph, const char *suffix)
3066 {
3067         int i;
3068         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
3069                 dmp_grph(get_irp_irg(i), suffix);
3070 }
3071
3072 struct pass_t {
3073         ir_prog_pass_t  pass;
3074         dump_graph_func *dump_graph;
3075         char            suffix[1];
3076 };
3077
3078 /**
3079  * Wrapper around dump_all_ir_graphs().
3080  */
3081 static int dump_all_ir_graphs_wrapper(ir_prog *irp, void *context)
3082 {
3083         struct pass_t *pass = context;
3084
3085         (void)irp;
3086         dump_all_ir_graphs(pass->dump_graph, pass->suffix);
3087         return 0;
3088 }
3089
3090 ir_prog_pass_t *dump_all_ir_graph_pass(const char *name,
3091                                        dump_graph_func *dump_graph,
3092                                        const char *suffix)
3093 {
3094         size_t         len   = strlen(suffix);
3095         struct pass_t  *pass = xmalloc(sizeof(*pass) + len);
3096         ir_prog_pass_t *res  = def_prog_pass_constructor(
3097                 &pass->pass, name ? name : "dump_all_graphs", dump_all_ir_graphs_wrapper);
3098
3099         /* this pass does not change anything, so neither dump nor verify is needed. */
3100         res->dump_irprog   = ir_prog_no_dump;
3101         res->verify_irprog = ir_prog_no_verify;
3102
3103         pass->dump_graph = dump_graph;
3104         strcpy(pass->suffix, suffix);
3105
3106         return res;
3107 }
3108
3109 /*--------------------------------------------------------------------------------*
3110  * Dumps a stand alone loop graph with firm nodes which belong to one loop node   *
3111  * packed together in one subgraph/box                                            *
3112  *--------------------------------------------------------------------------------*/
3113
3114 static void dump_loops_standalone(FILE *F, ir_loop *loop)
3115 {
3116         int i = 0, loop_node_started = 0, son_number = 0, first = 0;
3117         loop_element le;
3118         ir_loop *son = NULL;
3119
3120         /* Dump a new loop node. */
3121         dump_loop_node(F, loop);
3122
3123         /* Dump the loop elements. */
3124         for (i = 0; i < get_loop_n_elements(loop); i++) {
3125                 le = get_loop_element(loop, i);
3126                 son = le.son;
3127                 if (get_kind(son) == k_ir_loop) {
3128
3129                         /* We are a loop son -> Recurse */
3130
3131                         if (loop_node_started) { /* Close the "firm-nodes" node first if we started one. */
3132                                 fprintf(F, "\" }\n");
3133                                 fprintf(F, "edge: {sourcename: \"");
3134                                 PRINT_LOOPID(loop);
3135                                 fprintf(F, "\" targetname: \"");
3136                                 PRINT_LOOPID(loop);
3137                                 fprintf(F, "-%d-nodes\" label:\"%d...%d\"}\n", first, first, i-1);
3138                                 loop_node_started = 0;
3139                         }
3140                         dump_loop_son_edge(F, loop, son_number++);
3141                         dump_loops_standalone(F, son);
3142                 } else if (get_kind(son) == k_ir_node) {
3143                         /* We are a loop node -> Collect firm nodes */
3144
3145                         ir_node *n = le.node;
3146                         int bad = 0;
3147
3148                         if (!loop_node_started) {
3149                                 /* Start a new node which contains all firm nodes of the current loop */
3150                                 fprintf(F, "node: { title: \"");
3151                                 PRINT_LOOPID(loop);
3152                                 fprintf(F, "-%d-nodes\" color: lightyellow label: \"", i);
3153                                 loop_node_started = 1;
3154                                 first = i;
3155                         } else
3156                                 fprintf(F, "\n");
3157
3158                         bad |= dump_node_label(F, n);
3159                         /* Causes indeterministic output: if (is_Block(n)) fprintf(F, "\t ->%d", (int)get_irn_link(n)); */
3160                         if (has_backedges(n)) fprintf(F, "\t loop head!");
3161                 } else { /* for callgraph loop tree */
3162                         ir_graph *n;
3163                         assert(get_kind(son) == k_ir_graph);
3164
3165                         /* We are a loop node -> Collect firm graphs */
3166                         n = le.irg;
3167                         if (!loop_node_started) {
3168                                 /* Start a new node which contains all firm nodes of the current loop */
3169                                 fprintf(F, "node: { title: \"");
3170                                 PRINT_LOOPID(loop);
3171                                 fprintf(F, "-%d-nodes\" color: lightyellow label: \"", i);
3172                                 loop_node_started = 1;
3173                                 first = i;
3174                         } else
3175                                 fprintf(F, "\n");
3176                         fprintf(F, " %s", get_irg_dump_name(n));
3177                         /* fprintf(F, " %s (depth %d)", get_irg_dump_name(n), n->callgraph_weighted_loop_depth); */
3178                 }
3179         }
3180
3181         if (loop_node_started) {
3182                 fprintf(F, "\" }\n");
3183                 fprintf(F, "edge: {sourcename: \"");
3184                 PRINT_LOOPID(loop);
3185                 fprintf(F, "\" targetname: \"");
3186                 PRINT_LOOPID(loop);
3187                 fprintf(F, "-%d-nodes\" label:\"%d...%d\"}\n", first, first, i-1);
3188                 loop_node_started = 0;
3189         }
3190 }
3191
3192 void dump_loop_tree(ir_graph *irg, const char *suffix)
3193 {
3194         FILE *f;
3195
3196         /* if a filter is set, dump only the irg's that match the filter */
3197         if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg))))
3198                 return;
3199
3200         f = vcg_open(irg, suffix, "-looptree");
3201         if (f != NULL) {
3202                 ir_graph *rem = current_ir_graph;
3203                 int el_rem = edge_label;
3204
3205                 current_ir_graph = irg;
3206                 edge_label = 1;
3207
3208                 dump_vcg_header(f, get_irg_dump_name(irg), "Tree", "top_to_bottom");
3209
3210                 if (get_irg_loop(irg))
3211                         dump_loops_standalone(f, get_irg_loop(irg));
3212
3213                 dump_vcg_footer(f);
3214                 fclose(f);
3215
3216                 edge_label = el_rem;
3217                 current_ir_graph = rem;
3218         }
3219 }
3220
3221 void dump_callgraph_loop_tree(const char *suffix)
3222 {
3223         FILE *F;
3224         F = vcg_open_name("Callgraph_looptree", suffix);
3225         dump_vcg_header(F, "callgraph looptree", "Tree", "top_to_bottom");
3226         dump_loops_standalone(F, irp->outermost_cg_loop);
3227         dump_vcg_footer(F);
3228         fclose(F);
3229 }
3230
3231
3232 /*----------------------------------------------------------------------------*/
3233 /* Dumps the firm nodes in the loop tree to a graph along with the loop nodes.*/
3234 /*----------------------------------------------------------------------------*/
3235
3236 static void collect_nodeloop(FILE *F, ir_loop *loop, eset *loopnodes)
3237 {
3238         int i, son_number = 0, node_number = 0;
3239
3240         if (dump_loop_information_flag) dump_loop_node(F, loop);
3241
3242         for (i = 0; i < get_loop_n_elements(loop); i++) {
3243                 loop_element le = get_loop_element(loop, i);
3244                 if (*(le.kind) == k_ir_loop) {
3245                         if (dump_loop_information_flag) dump_loop_son_edge(F, loop, son_number++);
3246                         /* Recur */
3247                         collect_nodeloop(F, le.son, loopnodes);
3248                 } else {
3249                         if (dump_loop_information_flag) dump_loop_node_edge(F, loop, node_number++);
3250                         eset_insert(loopnodes, le.node);
3251                 }
3252         }
3253 }
3254
3255 static void collect_nodeloop_external_nodes(ir_loop *loop, eset *loopnodes,
3256                                             eset *extnodes)
3257 {
3258         int i, j, start;
3259
3260         for (i = 0; i < get_loop_n_elements(loop); i++) {
3261                 loop_element le = get_loop_element(loop, i);
3262                 if (*(le.kind) == k_ir_loop) {
3263                         /* Recur */
3264                         collect_nodeloop_external_nodes(le.son, loopnodes, extnodes);
3265                 } else {
3266                         if (is_Block(le.node)) start = 0; else start = -1;
3267                         for (j = start; j < get_irn_arity(le.node); j++) {
3268                                 ir_node *pred = get_irn_n(le.node, j);
3269                                 if (!eset_contains(loopnodes, pred)) {
3270                                         eset_insert(extnodes, pred);
3271                                         if (!is_Block(pred)) {
3272                                                 pred = get_nodes_block(pred);
3273                                                 if (!eset_contains(loopnodes, pred)) eset_insert(extnodes, pred);
3274                                         }
3275                                 }
3276                         }
3277                 }
3278         }
3279 }
3280
3281 void dump_loop(ir_loop *l, const char *suffix)
3282 {
3283         FILE *F;
3284         char name[50];
3285
3286         snprintf(name, sizeof(name), "loop_%d", get_loop_loop_nr(l));
3287         F = vcg_open_name(name, suffix);
3288         if (F != NULL) {
3289                 eset *loopnodes = eset_create();
3290                 eset *extnodes = eset_create();
3291                 ir_node *n, *b;
3292
3293                 dump_vcg_header(F, name, NULL, NULL);
3294
3295                 /* collect all nodes to dump */
3296                 collect_nodeloop(F, l, loopnodes);
3297                 collect_nodeloop_external_nodes(l, loopnodes, extnodes);
3298
3299                 /* build block lists */
3300                 for (n = eset_first(loopnodes); n != NULL; n = eset_next(loopnodes))
3301                         set_irn_link(n, NULL);
3302                 for (n = eset_first(extnodes); n != NULL; n = eset_next(extnodes))
3303                         set_irn_link(n, NULL);
3304                 for (n = eset_first(loopnodes); n != NULL; n = eset_next(loopnodes)) {
3305                         if (!is_Block(n)) {
3306                                 b = get_nodes_block(n);
3307                                 set_irn_link(n, get_irn_link(b));
3308                                 set_irn_link(b, n);
3309                         }
3310                 }
3311                 for (n = eset_first(extnodes); n != NULL; n = eset_next(extnodes)) {
3312                         if (!is_Block(n)) {
3313                                 b = get_nodes_block(n);
3314                                 set_irn_link(n, get_irn_link(b));
3315                                 set_irn_link(b, n);
3316                         }
3317                 }
3318
3319                 for (b = eset_first(loopnodes); b != NULL; b = eset_next(loopnodes)) {
3320                         if (is_Block(b)) {
3321                                 fprintf(F, "graph: { title: \"");
3322                                 PRINT_NODEID(b);
3323                                 fprintf(F, "\"  label: \"");
3324                                 dump_node_opcode(F, b);
3325                                 fprintf(F, " %ld:%d", get_irn_node_nr(b), get_irn_idx(b));
3326                                 fprintf(F, "\" status:clustered color:yellow\n");
3327
3328                                 /* dump the blocks edges */
3329                                 dump_ir_data_edges(F, b);
3330
3331                                 /* dump the nodes that go into the block */
3332                                 for (n = get_irn_link(b); n; n = get_irn_link(n)) {
3333                                         if (eset_contains(extnodes, n))
3334                                                 overrule_nodecolor = ird_color_block_inout;
3335                                         dump_node(F, n);
3336                                         overrule_nodecolor = ird_color_default_node;
3337                                         if (!eset_contains(extnodes, n)) dump_ir_data_edges(F, n);
3338                                 }
3339
3340                                 /* Close the vcg information for the block */
3341                                 fprintf(F, "}\n");
3342                                 dump_const_node_local(F, b);
3343                                 fprintf(F, "\n");
3344                         }
3345                 }
3346                 for (b = eset_first(extnodes); b != NULL; b = eset_next(extnodes)) {
3347                         if (is_Block(b)) {
3348                                 fprintf(F, "graph: { title: \"");
3349                                 PRINT_NODEID(b);
3350                                 fprintf(F, "\"  label: \"");
3351                                 dump_node_opcode(F, b);
3352                                 fprintf(F, " %ld:%d", get_irn_node_nr(b), get_irn_idx(b));
3353                                 fprintf(F, "\" status:clustered color:lightblue\n");
3354
3355                                 /* dump the nodes that go into the block */
3356                                 for (n = get_irn_link(b); n; n = get_irn_link(n)) {
3357                                         if (!eset_contains(loopnodes, n))
3358                                                 overrule_nodecolor = ird_color_block_inout;
3359                                         dump_node(F, n);
3360                                         overrule_nodecolor = ird_color_default_node;
3361                                         if (eset_contains(loopnodes, n)) dump_ir_data_edges(F, n);
3362                                 }
3363
3364                                 /* Close the vcg information for the block */
3365                                 fprintf(F, "}\n");
3366                                 dump_const_node_local(F, b);
3367                                 fprintf(F, "\n");
3368                         }
3369                 }
3370                 eset_destroy(loopnodes);
3371                 eset_destroy(extnodes);
3372
3373                 dump_vcg_footer(F);
3374                 fclose(F);
3375         }
3376 }