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