2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / print-rtl.c
blob9e1df933e923dda6719db2c04c8fc19956b79cb8
1 /* Print RTL for GCC.
2 Copyright (C) 1987, 1988, 1992, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file is compiled twice: once for the generator programs,
23 once for the compiler. */
24 #ifdef GENERATOR_FILE
25 #include "bconfig.h"
26 #else
27 #include "config.h"
28 #endif
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "rtl.h"
35 /* These headers all define things which are not available in
36 generator programs. */
37 #ifndef GENERATOR_FILE
38 #include "tree.h"
39 #include "real.h"
40 #include "flags.h"
41 #include "hard-reg-set.h"
42 #include "basic-block.h"
43 #endif
45 static FILE *outfile;
47 static int sawclose = 0;
49 static int indent;
51 static void print_rtx (const_rtx);
53 /* String printed at beginning of each RTL when it is dumped.
54 This string is set to ASM_COMMENT_START when the RTL is dumped in
55 the assembly output file. */
56 const char *print_rtx_head = "";
58 /* Nonzero means suppress output of instruction numbers
59 in debugging dumps.
60 This must be defined here so that programs like gencodes can be linked. */
61 int flag_dump_unnumbered = 0;
63 #ifdef GENERATOR_FILE
64 /* Nonzero means use simplified format without flags, modes, etc. */
65 int flag_dump_simple_rtl = 0;
66 #endif
68 /* Nonzero if we are dumping graphical description. */
69 int dump_for_graph;
71 #ifndef GENERATOR_FILE
72 static void
73 print_decl_name (FILE *outfile, const_tree node)
75 if (DECL_NAME (node))
76 fputs (IDENTIFIER_POINTER (DECL_NAME (node)), outfile);
77 else
79 if (TREE_CODE (node) == LABEL_DECL && LABEL_DECL_UID (node) != -1)
80 fprintf (outfile, "L.%d", (int) LABEL_DECL_UID (node));
81 else
83 char c = TREE_CODE (node) == CONST_DECL ? 'C' : 'D';
84 fprintf (outfile, "%c.%u", c, DECL_UID (node));
89 void
90 print_mem_expr (FILE *outfile, const_tree expr)
92 if (TREE_CODE (expr) == COMPONENT_REF)
94 if (TREE_OPERAND (expr, 0))
95 print_mem_expr (outfile, TREE_OPERAND (expr, 0));
96 else
97 fputs (" <variable>", outfile);
98 fputc ('.', outfile);
99 print_decl_name (outfile, TREE_OPERAND (expr, 1));
101 else if (TREE_CODE (expr) == INDIRECT_REF)
103 fputs (" (*", outfile);
104 print_mem_expr (outfile, TREE_OPERAND (expr, 0));
105 fputs (")", outfile);
107 else if (TREE_CODE (expr) == ALIGN_INDIRECT_REF)
109 fputs (" (A*", outfile);
110 print_mem_expr (outfile, TREE_OPERAND (expr, 0));
111 fputs (")", outfile);
113 else if (TREE_CODE (expr) == MISALIGNED_INDIRECT_REF)
115 fputs (" (M*", outfile);
116 print_mem_expr (outfile, TREE_OPERAND (expr, 0));
117 fputs (")", outfile);
119 else if (TREE_CODE (expr) == RESULT_DECL)
120 fputs (" <result>", outfile);
121 else
123 fputc (' ', outfile);
124 print_decl_name (outfile, expr);
127 #endif
129 /* Print IN_RTX onto OUTFILE. This is the recursive part of printing. */
131 static void
132 print_rtx (const_rtx in_rtx)
134 int i = 0;
135 int j;
136 const char *format_ptr;
137 int is_insn;
139 if (sawclose)
141 if (flag_dump_simple_rtl)
142 fputc (' ', outfile);
143 else
144 fprintf (outfile, "\n%s%*s", print_rtx_head, indent * 2, "");
145 sawclose = 0;
148 if (in_rtx == 0)
150 fputs ("(nil)", outfile);
151 sawclose = 1;
152 return;
154 else if (GET_CODE (in_rtx) > NUM_RTX_CODE)
156 fprintf (outfile, "(??? bad code %d\n)", GET_CODE (in_rtx));
157 sawclose = 1;
158 return;
161 is_insn = INSN_P (in_rtx);
163 /* When printing in VCG format we write INSNs, NOTE, LABEL, and BARRIER
164 in separate nodes and therefore have to handle them special here. */
165 if (dump_for_graph
166 && (is_insn || NOTE_P (in_rtx)
167 || LABEL_P (in_rtx) || BARRIER_P (in_rtx)))
169 i = 3;
170 indent = 0;
172 else
174 /* Print name of expression code. */
175 if (flag_dump_simple_rtl && GET_CODE (in_rtx) == CONST_INT)
176 fputc ('(', outfile);
177 else
178 fprintf (outfile, "(%s", GET_RTX_NAME (GET_CODE (in_rtx)));
180 if (! flag_dump_simple_rtl)
182 if (RTX_FLAG (in_rtx, in_struct))
183 fputs ("/s", outfile);
185 if (RTX_FLAG (in_rtx, volatil))
186 fputs ("/v", outfile);
188 if (RTX_FLAG (in_rtx, unchanging))
189 fputs ("/u", outfile);
191 if (RTX_FLAG (in_rtx, frame_related))
192 fputs ("/f", outfile);
194 if (RTX_FLAG (in_rtx, jump))
195 fputs ("/j", outfile);
197 if (RTX_FLAG (in_rtx, call))
198 fputs ("/c", outfile);
200 if (RTX_FLAG (in_rtx, return_val))
201 fputs ("/i", outfile);
203 /* Print REG_NOTE names for EXPR_LIST and INSN_LIST. */
204 if (GET_CODE (in_rtx) == EXPR_LIST
205 || GET_CODE (in_rtx) == INSN_LIST)
206 fprintf (outfile, ":%s",
207 GET_REG_NOTE_NAME (GET_MODE (in_rtx)));
209 /* For other rtl, print the mode if it's not VOID. */
210 else if (GET_MODE (in_rtx) != VOIDmode)
211 fprintf (outfile, ":%s", GET_MODE_NAME (GET_MODE (in_rtx)));
215 #ifndef GENERATOR_FILE
216 if (GET_CODE (in_rtx) == CONST_DOUBLE && FLOAT_MODE_P (GET_MODE (in_rtx)))
217 i = 5;
218 #endif
220 /* Get the format string and skip the first elements if we have handled
221 them already. */
222 format_ptr = GET_RTX_FORMAT (GET_CODE (in_rtx)) + i;
223 for (; i < GET_RTX_LENGTH (GET_CODE (in_rtx)); i++)
224 switch (*format_ptr++)
226 const char *str;
228 case 'T':
229 str = XTMPL (in_rtx, i);
230 goto string;
232 case 'S':
233 case 's':
234 str = XSTR (in_rtx, i);
235 string:
237 if (str == 0)
238 fputs (dump_for_graph ? " \\\"\\\"" : " \"\"", outfile);
239 else
241 if (dump_for_graph)
242 fprintf (outfile, " (\\\"%s\\\")", str);
243 else
244 fprintf (outfile, " (\"%s\")", str);
246 sawclose = 1;
247 break;
249 /* 0 indicates a field for internal use that should not be printed.
250 An exception is the third field of a NOTE, where it indicates
251 that the field has several different valid contents. */
252 case '0':
253 if (i == 1 && REG_P (in_rtx))
255 if (REGNO (in_rtx) != ORIGINAL_REGNO (in_rtx))
256 fprintf (outfile, " [%d]", ORIGINAL_REGNO (in_rtx));
258 #ifndef GENERATOR_FILE
259 else if (i == 1 && GET_CODE (in_rtx) == SYMBOL_REF)
261 int flags = SYMBOL_REF_FLAGS (in_rtx);
262 if (flags)
263 fprintf (outfile, " [flags 0x%x]", flags);
265 else if (i == 2 && GET_CODE (in_rtx) == SYMBOL_REF)
267 tree decl = SYMBOL_REF_DECL (in_rtx);
268 if (decl)
269 print_node_brief (outfile, "", decl, 0);
271 #endif
272 else if (i == 4 && NOTE_P (in_rtx))
274 switch (NOTE_KIND (in_rtx))
276 case NOTE_INSN_EH_REGION_BEG:
277 case NOTE_INSN_EH_REGION_END:
278 if (flag_dump_unnumbered)
279 fprintf (outfile, " #");
280 else
281 fprintf (outfile, " %d", NOTE_EH_HANDLER (in_rtx));
282 sawclose = 1;
283 break;
285 case NOTE_INSN_BLOCK_BEG:
286 case NOTE_INSN_BLOCK_END:
287 #ifndef GENERATOR_FILE
288 dump_addr (outfile, " ", NOTE_BLOCK (in_rtx));
289 #endif
290 sawclose = 1;
291 break;
293 case NOTE_INSN_BASIC_BLOCK:
295 #ifndef GENERATOR_FILE
296 basic_block bb = NOTE_BASIC_BLOCK (in_rtx);
297 if (bb != 0)
298 fprintf (outfile, " [bb %d]", bb->index);
299 #endif
300 break;
303 case NOTE_INSN_DELETED_LABEL:
305 const char *label = NOTE_DELETED_LABEL_NAME (in_rtx);
306 if (label)
307 fprintf (outfile, " (\"%s\")", label);
308 else
309 fprintf (outfile, " \"\"");
311 break;
313 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
315 #ifndef GENERATOR_FILE
316 basic_block bb = NOTE_BASIC_BLOCK (in_rtx);
317 if (bb != 0)
318 fprintf (outfile, " [bb %d]", bb->index);
319 #endif
320 break;
323 case NOTE_INSN_VAR_LOCATION:
324 #ifndef GENERATOR_FILE
325 fprintf (outfile, " (");
326 print_mem_expr (outfile, NOTE_VAR_LOCATION_DECL (in_rtx));
327 fprintf (outfile, " ");
328 print_rtx (NOTE_VAR_LOCATION_LOC (in_rtx));
329 if (NOTE_VAR_LOCATION_STATUS (in_rtx) ==
330 VAR_INIT_STATUS_UNINITIALIZED)
331 fprintf (outfile, " [uninit]");
332 fprintf (outfile, ")");
333 #endif
334 break;
336 default:
337 break;
340 else if (i == 9 && JUMP_P (in_rtx) && XEXP (in_rtx, i) != NULL)
341 /* Output the JUMP_LABEL reference. */
342 fprintf (outfile, "\n -> %d", INSN_UID (XEXP (in_rtx, i)));
343 break;
345 case 'e':
346 do_e:
347 indent += 2;
348 if (!sawclose)
349 fprintf (outfile, " ");
350 print_rtx (XEXP (in_rtx, i));
351 indent -= 2;
352 break;
354 case 'E':
355 case 'V':
356 indent += 2;
357 if (sawclose)
359 fprintf (outfile, "\n%s%*s",
360 print_rtx_head, indent * 2, "");
361 sawclose = 0;
363 fputs (" [", outfile);
364 if (NULL != XVEC (in_rtx, i))
366 indent += 2;
367 if (XVECLEN (in_rtx, i))
368 sawclose = 1;
370 for (j = 0; j < XVECLEN (in_rtx, i); j++)
371 print_rtx (XVECEXP (in_rtx, i, j));
373 indent -= 2;
375 if (sawclose)
376 fprintf (outfile, "\n%s%*s", print_rtx_head, indent * 2, "");
378 fputs ("]", outfile);
379 sawclose = 1;
380 indent -= 2;
381 break;
383 case 'w':
384 if (! flag_dump_simple_rtl)
385 fprintf (outfile, " ");
386 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, XWINT (in_rtx, i));
387 if (! flag_dump_simple_rtl)
388 fprintf (outfile, " [" HOST_WIDE_INT_PRINT_HEX "]",
389 (unsigned HOST_WIDE_INT) XWINT (in_rtx, i));
390 break;
392 case 'i':
393 if (i == 4 && INSN_P (in_rtx))
395 #ifndef GENERATOR_FILE
396 /* Pretty-print insn locators. Ignore scoping as it is mostly
397 redundant with line number information and do not print anything
398 when there is no location information available. */
399 if (INSN_LOCATOR (in_rtx) && insn_file (in_rtx))
400 fprintf(outfile, " %s:%i", insn_file (in_rtx), insn_line (in_rtx));
401 #endif
403 else if (i == 6 && NOTE_P (in_rtx))
405 /* This field is only used for NOTE_INSN_DELETED_LABEL, and
406 other times often contains garbage from INSN->NOTE death. */
407 if (NOTE_KIND (in_rtx) == NOTE_INSN_DELETED_LABEL)
408 fprintf (outfile, " %d", XINT (in_rtx, i));
410 else
412 int value = XINT (in_rtx, i);
413 const char *name;
415 #ifndef GENERATOR_FILE
416 if (REG_P (in_rtx) && value < FIRST_PSEUDO_REGISTER)
417 fprintf (outfile, " %d %s", REGNO (in_rtx),
418 reg_names[REGNO (in_rtx)]);
419 else if (REG_P (in_rtx)
420 && value <= LAST_VIRTUAL_REGISTER)
422 if (value == VIRTUAL_INCOMING_ARGS_REGNUM)
423 fprintf (outfile, " %d virtual-incoming-args", value);
424 else if (value == VIRTUAL_STACK_VARS_REGNUM)
425 fprintf (outfile, " %d virtual-stack-vars", value);
426 else if (value == VIRTUAL_STACK_DYNAMIC_REGNUM)
427 fprintf (outfile, " %d virtual-stack-dynamic", value);
428 else if (value == VIRTUAL_OUTGOING_ARGS_REGNUM)
429 fprintf (outfile, " %d virtual-outgoing-args", value);
430 else if (value == VIRTUAL_CFA_REGNUM)
431 fprintf (outfile, " %d virtual-cfa", value);
432 else
433 fprintf (outfile, " %d virtual-reg-%d", value,
434 value-FIRST_VIRTUAL_REGISTER);
436 else
437 #endif
438 if (flag_dump_unnumbered
439 && (is_insn || NOTE_P (in_rtx)))
440 fputc ('#', outfile);
441 else
442 fprintf (outfile, " %d", value);
444 #ifndef GENERATOR_FILE
445 if (REG_P (in_rtx) && REG_ATTRS (in_rtx))
447 fputs (" [", outfile);
448 if (ORIGINAL_REGNO (in_rtx) != REGNO (in_rtx))
449 fprintf (outfile, "orig:%i", ORIGINAL_REGNO (in_rtx));
450 if (REG_EXPR (in_rtx))
451 print_mem_expr (outfile, REG_EXPR (in_rtx));
453 if (REG_OFFSET (in_rtx))
454 fprintf (outfile, "+" HOST_WIDE_INT_PRINT_DEC,
455 REG_OFFSET (in_rtx));
456 fputs (" ]", outfile);
458 #endif
460 if (is_insn && &INSN_CODE (in_rtx) == &XINT (in_rtx, i)
461 && XINT (in_rtx, i) >= 0
462 && (name = get_insn_name (XINT (in_rtx, i))) != NULL)
463 fprintf (outfile, " {%s}", name);
464 sawclose = 0;
466 break;
468 /* Print NOTE_INSN names rather than integer codes. */
470 case 'n':
471 fprintf (outfile, " %s", GET_NOTE_INSN_NAME (XINT (in_rtx, i)));
472 sawclose = 0;
473 break;
475 case 'u':
476 if (XEXP (in_rtx, i) != NULL)
478 rtx sub = XEXP (in_rtx, i);
479 enum rtx_code subc = GET_CODE (sub);
481 if (GET_CODE (in_rtx) == LABEL_REF)
483 if (subc == NOTE
484 && NOTE_KIND (sub) == NOTE_INSN_DELETED_LABEL)
486 if (flag_dump_unnumbered)
487 fprintf (outfile, " [# deleted]");
488 else
489 fprintf (outfile, " [%d deleted]", INSN_UID (sub));
490 sawclose = 0;
491 break;
494 if (subc != CODE_LABEL)
495 goto do_e;
498 if (flag_dump_unnumbered)
499 fputs (" #", outfile);
500 else
501 fprintf (outfile, " %d", INSN_UID (sub));
503 else
504 fputs (" 0", outfile);
505 sawclose = 0;
506 break;
508 case 'b':
509 #ifndef GENERATOR_FILE
510 if (XBITMAP (in_rtx, i) == NULL)
511 fputs (" {null}", outfile);
512 else
513 bitmap_print (outfile, XBITMAP (in_rtx, i), " {", "}");
514 #endif
515 sawclose = 0;
516 break;
518 case 't':
519 #ifndef GENERATOR_FILE
520 dump_addr (outfile, " ", XTREE (in_rtx, i));
521 #endif
522 break;
524 case '*':
525 fputs (" Unknown", outfile);
526 sawclose = 0;
527 break;
529 case 'B':
530 #ifndef GENERATOR_FILE
531 if (XBBDEF (in_rtx, i))
532 fprintf (outfile, " %i", XBBDEF (in_rtx, i)->index);
533 #endif
534 break;
536 default:
537 gcc_unreachable ();
540 switch (GET_CODE (in_rtx))
542 #ifndef GENERATOR_FILE
543 case MEM:
544 fprintf (outfile, " [" HOST_WIDE_INT_PRINT_DEC,
545 (HOST_WIDE_INT) MEM_ALIAS_SET (in_rtx));
547 if (MEM_EXPR (in_rtx))
548 print_mem_expr (outfile, MEM_EXPR (in_rtx));
550 if (MEM_OFFSET (in_rtx))
551 fprintf (outfile, "+" HOST_WIDE_INT_PRINT_DEC,
552 INTVAL (MEM_OFFSET (in_rtx)));
554 if (MEM_SIZE (in_rtx))
555 fprintf (outfile, " S" HOST_WIDE_INT_PRINT_DEC,
556 INTVAL (MEM_SIZE (in_rtx)));
558 if (MEM_ALIGN (in_rtx) != 1)
559 fprintf (outfile, " A%u", MEM_ALIGN (in_rtx));
561 fputc (']', outfile);
562 break;
564 case CONST_DOUBLE:
565 if (FLOAT_MODE_P (GET_MODE (in_rtx)))
567 char s[60];
569 real_to_decimal (s, CONST_DOUBLE_REAL_VALUE (in_rtx),
570 sizeof (s), 0, 1);
571 fprintf (outfile, " %s", s);
573 real_to_hexadecimal (s, CONST_DOUBLE_REAL_VALUE (in_rtx),
574 sizeof (s), 0, 1);
575 fprintf (outfile, " [%s]", s);
577 break;
578 #endif
580 case CODE_LABEL:
581 fprintf (outfile, " [%d uses]", LABEL_NUSES (in_rtx));
582 switch (LABEL_KIND (in_rtx))
584 case LABEL_NORMAL: break;
585 case LABEL_STATIC_ENTRY: fputs (" [entry]", outfile); break;
586 case LABEL_GLOBAL_ENTRY: fputs (" [global entry]", outfile); break;
587 case LABEL_WEAK_ENTRY: fputs (" [weak entry]", outfile); break;
588 default: gcc_unreachable ();
590 break;
592 default:
593 break;
596 if (dump_for_graph
597 && (is_insn || NOTE_P (in_rtx)
598 || LABEL_P (in_rtx) || BARRIER_P (in_rtx)))
599 sawclose = 0;
600 else
602 fputc (')', outfile);
603 sawclose = 1;
607 /* Print an rtx on the current line of FILE. Initially indent IND
608 characters. */
610 void
611 print_inline_rtx (FILE *outf, const_rtx x, int ind)
613 int oldsaw = sawclose;
614 int oldindent = indent;
616 sawclose = 0;
617 indent = ind;
618 outfile = outf;
619 print_rtx (x);
620 sawclose = oldsaw;
621 indent = oldindent;
624 /* Call this function from the debugger to see what X looks like. */
626 void
627 debug_rtx (const_rtx x)
629 outfile = stderr;
630 sawclose = 0;
631 print_rtx (x);
632 fprintf (stderr, "\n");
635 /* Count of rtx's to print with debug_rtx_list.
636 This global exists because gdb user defined commands have no arguments. */
638 int debug_rtx_count = 0; /* 0 is treated as equivalent to 1 */
640 /* Call this function to print list from X on.
642 N is a count of the rtx's to print. Positive values print from the specified
643 rtx on. Negative values print a window around the rtx.
644 EG: -5 prints 2 rtx's on either side (in addition to the specified rtx). */
646 void
647 debug_rtx_list (const_rtx x, int n)
649 int i,count;
650 const_rtx insn;
652 count = n == 0 ? 1 : n < 0 ? -n : n;
654 /* If we are printing a window, back up to the start. */
656 if (n < 0)
657 for (i = count / 2; i > 0; i--)
659 if (PREV_INSN (x) == 0)
660 break;
661 x = PREV_INSN (x);
664 for (i = count, insn = x; i > 0 && insn != 0; i--, insn = NEXT_INSN (insn))
666 debug_rtx (insn);
667 fprintf (stderr, "\n");
671 /* Call this function to print an rtx list from START to END inclusive. */
673 void
674 debug_rtx_range (const_rtx start, const_rtx end)
676 while (1)
678 debug_rtx (start);
679 fprintf (stderr, "\n");
680 if (!start || start == end)
681 break;
682 start = NEXT_INSN (start);
686 /* Call this function to search an rtx list to find one with insn uid UID,
687 and then call debug_rtx_list to print it, using DEBUG_RTX_COUNT.
688 The found insn is returned to enable further debugging analysis. */
690 const_rtx
691 debug_rtx_find (const_rtx x, int uid)
693 while (x != 0 && INSN_UID (x) != uid)
694 x = NEXT_INSN (x);
695 if (x != 0)
697 debug_rtx_list (x, debug_rtx_count);
698 return x;
700 else
702 fprintf (stderr, "insn uid %d not found\n", uid);
703 return 0;
707 /* External entry point for printing a chain of insns
708 starting with RTX_FIRST onto file OUTF.
709 A blank line separates insns.
711 If RTX_FIRST is not an insn, then it alone is printed, with no newline. */
713 void
714 print_rtl (FILE *outf, const_rtx rtx_first)
716 const_rtx tmp_rtx;
718 outfile = outf;
719 sawclose = 0;
721 if (rtx_first == 0)
723 fputs (print_rtx_head, outf);
724 fputs ("(nil)\n", outf);
726 else
727 switch (GET_CODE (rtx_first))
729 case INSN:
730 case JUMP_INSN:
731 case CALL_INSN:
732 case NOTE:
733 case CODE_LABEL:
734 case BARRIER:
735 for (tmp_rtx = rtx_first; tmp_rtx != 0; tmp_rtx = NEXT_INSN (tmp_rtx))
737 fputs (print_rtx_head, outfile);
738 print_rtx (tmp_rtx);
739 fprintf (outfile, "\n");
741 break;
743 default:
744 fputs (print_rtx_head, outfile);
745 print_rtx (rtx_first);
749 /* Like print_rtx, except specify a file. */
750 /* Return nonzero if we actually printed anything. */
753 print_rtl_single (FILE *outf, const_rtx x)
755 outfile = outf;
756 sawclose = 0;
757 fputs (print_rtx_head, outfile);
758 print_rtx (x);
759 putc ('\n', outf);
760 return 1;
764 /* Like print_rtl except without all the detail; for example,
765 if RTX is a CONST_INT then print in decimal format. */
767 void
768 print_simple_rtl (FILE *outf, const_rtx x)
770 int save = flag_dump_simple_rtl;
772 flag_dump_simple_rtl = 1;
773 print_rtl (outf, x);
774 flag_dump_simple_rtl = save;