cse.c (cse_basic_block): Only call find_reg_note if REG_NOTES not 0.
[gcc.git] / gcc / print-rtl.c
1 /* Print RTL for GNU C Compiler.
2 Copyright (C) 1987, 1988, 1992, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "real.h"
27 #include "flags.h"
28 #include "hard-reg-set.h"
29 #include "basic-block.h"
30
31 /* How to print out a register name.
32 We don't use PRINT_REG because some definitions of PRINT_REG
33 don't work here. */
34 #ifndef DEBUG_PRINT_REG
35 #define DEBUG_PRINT_REG(RTX, CODE, FILE) \
36 fprintf ((FILE), "%d %s", REGNO (RTX), reg_names[REGNO (RTX)])
37 #endif
38
39 /* Array containing all of the register names */
40
41 #ifdef DEBUG_REGISTER_NAMES
42 static const char * const reg_names[] = DEBUG_REGISTER_NAMES;
43 #else
44 static const char * const reg_names[] = REGISTER_NAMES;
45 #endif
46
47 static FILE *outfile;
48
49 static const char xspaces[] = " ";
50
51 static int sawclose = 0;
52
53 static int indent;
54
55 static void print_rtx PARAMS ((rtx));
56
57 /* Nonzero means suppress output of instruction numbers and line number
58 notes in debugging dumps.
59 This must be defined here so that programs like gencodes can be linked. */
60 int flag_dump_unnumbered = 0;
61
62 /* Nonzero if we are dumping graphical description. */
63 int dump_for_graph;
64
65 /* Print IN_RTX onto OUTFILE. This is the recursive part of printing. */
66
67 static void
68 print_rtx (in_rtx)
69 register rtx in_rtx;
70 {
71 register int i = 0;
72 register int j;
73 register const char *format_ptr;
74 register int is_insn;
75 rtx tem;
76
77 if (sawclose)
78 {
79 fprintf (outfile, "\n%s",
80 (xspaces + (sizeof xspaces - 1 - indent * 2)));
81 sawclose = 0;
82 }
83
84 if (in_rtx == 0)
85 {
86 fputs ("(nil)", outfile);
87 sawclose = 1;
88 return;
89 }
90
91 is_insn = (GET_RTX_CLASS (GET_CODE (in_rtx)) == 'i');
92
93 /* When printing in VCG format we write INSNs, NOTE, LABEL, and BARRIER
94 in separate nodes and therefore have to handle them special here. */
95 if (dump_for_graph &&
96 (is_insn || GET_CODE (in_rtx) == NOTE || GET_CODE (in_rtx) == CODE_LABEL
97 || GET_CODE (in_rtx) == BARRIER))
98 {
99 i = 3;
100 indent = 0;
101 }
102 else
103 {
104 /* print name of expression code */
105 fprintf (outfile, "(%s", GET_RTX_NAME (GET_CODE (in_rtx)));
106
107 if (in_rtx->in_struct)
108 fputs ("/s", outfile);
109
110 if (in_rtx->volatil)
111 fputs ("/v", outfile);
112
113 if (in_rtx->unchanging)
114 fputs ("/u", outfile);
115
116 if (in_rtx->integrated)
117 fputs ("/i", outfile);
118
119 if (in_rtx->frame_related)
120 fputs ("/f", outfile);
121
122 if (in_rtx->jump)
123 fputs ("/j", outfile);
124
125 if (in_rtx->call)
126 fputs ("/c", outfile);
127
128 if (GET_MODE (in_rtx) != VOIDmode)
129 {
130 /* Print REG_NOTE names for EXPR_LIST and INSN_LIST. */
131 if (GET_CODE (in_rtx) == EXPR_LIST || GET_CODE (in_rtx) == INSN_LIST)
132 fprintf (outfile, ":%s", GET_REG_NOTE_NAME (GET_MODE (in_rtx)));
133 else
134 fprintf (outfile, ":%s", GET_MODE_NAME (GET_MODE (in_rtx)));
135 }
136 }
137
138 /* Get the format string and skip the first elements if we have handled
139 them already. */
140 format_ptr = GET_RTX_FORMAT (GET_CODE (in_rtx)) + i;
141
142 for (; i < GET_RTX_LENGTH (GET_CODE (in_rtx)); i++)
143 switch (*format_ptr++)
144 {
145 case 'S':
146 case 's':
147 if (XSTR (in_rtx, i) == 0)
148 fputs (dump_for_graph ? " \\\"\\\"" : " \"\"", outfile);
149 else
150 {
151 if (dump_for_graph)
152 fprintf (outfile, " (\\\"%s\\\")", XSTR (in_rtx, i));
153 else
154 fprintf (outfile, " (\"%s\")", XSTR (in_rtx, i));
155 }
156 sawclose = 1;
157 break;
158
159 /* 0 indicates a field for internal use that should not be printed.
160 An exception is the third field of a NOTE, where it indicates
161 that the field has several different valid contents. */
162 case '0':
163 if (i == 3 && GET_CODE (in_rtx) == NOTE)
164 {
165 switch (NOTE_LINE_NUMBER (in_rtx))
166 {
167 case NOTE_INSN_EH_REGION_BEG:
168 case NOTE_INSN_EH_REGION_END:
169 if (flag_dump_unnumbered)
170 fprintf (outfile, " #");
171 else
172 fprintf (outfile, " %d", NOTE_EH_HANDLER (in_rtx));
173 sawclose = 1;
174 break;
175
176 case NOTE_INSN_BLOCK_BEG:
177 case NOTE_INSN_BLOCK_END:
178 fprintf (outfile, " ");
179 if (flag_dump_unnumbered)
180 fprintf (outfile, "#");
181 else
182 fprintf (outfile, HOST_PTR_PRINTF,
183 (char *) NOTE_BLOCK (in_rtx));
184 sawclose = 1;
185 break;
186
187 case NOTE_INSN_RANGE_BEG:
188 case NOTE_INSN_RANGE_END:
189 case NOTE_INSN_LIVE:
190 indent += 2;
191 if (!sawclose)
192 fprintf (outfile, " ");
193 print_rtx (NOTE_RANGE_INFO (in_rtx));
194 indent -= 2;
195 break;
196
197 case NOTE_INSN_BASIC_BLOCK:
198 {
199 basic_block bb = NOTE_BASIC_BLOCK (in_rtx);
200 if (bb != 0)
201 fprintf (outfile, " [bb %d]", bb->index);
202 break;
203 }
204
205 case NOTE_INSN_EXPECTED_VALUE:
206 indent += 2;
207 if (!sawclose)
208 fprintf (outfile, " ");
209 print_rtx (NOTE_EXPECTED_VALUE (in_rtx));
210 indent -= 2;
211 break;
212
213 case NOTE_INSN_DELETED_LABEL:
214 if (NOTE_SOURCE_FILE (in_rtx))
215 fprintf (outfile, " (\"%s\")", NOTE_SOURCE_FILE (in_rtx));
216 else
217 fprintf (outfile, " \"\"");
218 break;
219
220 default:
221 {
222 const char * const str = X0STR (in_rtx, i);
223
224 if (NOTE_LINE_NUMBER (in_rtx) < 0)
225 ;
226 else if (str == 0)
227 fputs (dump_for_graph ? " \\\"\\\"" : " \"\"", outfile);
228 else
229 {
230 if (dump_for_graph)
231 fprintf (outfile, " (\\\"%s\\\")", str);
232 else
233 fprintf (outfile, " (\"%s\")", str);
234 }
235 break;
236 }
237 }
238 }
239 break;
240
241 case 'e':
242 do_e:
243 indent += 2;
244 if (!sawclose)
245 fprintf (outfile, " ");
246 print_rtx (XEXP (in_rtx, i));
247 indent -= 2;
248 break;
249
250 case 'E':
251 case 'V':
252 indent += 2;
253 if (sawclose)
254 {
255 fprintf (outfile, "\n%s",
256 (xspaces + (sizeof xspaces - 1 - indent * 2)));
257 sawclose = 0;
258 }
259 fputs ("[ ", outfile);
260 if (NULL != XVEC (in_rtx, i))
261 {
262 indent += 2;
263 if (XVECLEN (in_rtx, i))
264 sawclose = 1;
265
266 for (j = 0; j < XVECLEN (in_rtx, i); j++)
267 print_rtx (XVECEXP (in_rtx, i, j));
268
269 indent -= 2;
270 }
271 if (sawclose)
272 fprintf (outfile, "\n%s",
273 (xspaces + (sizeof xspaces - 1 - indent * 2)));
274
275 fputs ("] ", outfile);
276 sawclose = 1;
277 indent -= 2;
278 break;
279
280 case 'w':
281 fprintf (outfile, " ");
282 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, XWINT (in_rtx, i));
283 fprintf (outfile, " [");
284 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX, XWINT (in_rtx, i));
285 fprintf (outfile, "]");
286 break;
287
288 case 'i':
289 {
290 register int value = XINT (in_rtx, i);
291 const char *name;
292
293 if (GET_CODE (in_rtx) == REG && value < FIRST_PSEUDO_REGISTER)
294 {
295 fputc (' ', outfile);
296 DEBUG_PRINT_REG (in_rtx, 0, outfile);
297 }
298 else if (GET_CODE (in_rtx) == REG && value <= LAST_VIRTUAL_REGISTER)
299 {
300 if (value == VIRTUAL_INCOMING_ARGS_REGNUM)
301 fprintf (outfile, " %d virtual-incoming-args", value);
302 else if (value == VIRTUAL_STACK_VARS_REGNUM)
303 fprintf (outfile, " %d virtual-stack-vars", value);
304 else if (value == VIRTUAL_STACK_DYNAMIC_REGNUM)
305 fprintf (outfile, " %d virtual-stack-dynamic", value);
306 else if (value == VIRTUAL_OUTGOING_ARGS_REGNUM)
307 fprintf (outfile, " %d virtual-outgoing-args", value);
308 else if (value == VIRTUAL_CFA_REGNUM)
309 fprintf (outfile, " %d virtual-cfa", value);
310 else
311 fprintf (outfile, " %d virtual-reg-%d", value,
312 value-FIRST_VIRTUAL_REGISTER);
313 }
314 else if (flag_dump_unnumbered
315 && (is_insn || GET_CODE (in_rtx) == NOTE))
316 fputc ('#', outfile);
317 else
318 fprintf (outfile, " %d", value);
319
320 if (is_insn && &INSN_CODE (in_rtx) == &XINT (in_rtx, i)
321 && XINT (in_rtx, i) >= 0
322 && (name = get_insn_name (XINT (in_rtx, i))) != NULL)
323 fprintf (outfile, " {%s}", name);
324 sawclose = 0;
325 }
326 break;
327
328 /* Print NOTE_INSN names rather than integer codes. */
329
330 case 'n':
331 if (XINT (in_rtx, i) >= NOTE_INSN_BIAS
332 && XINT (in_rtx, i) < NOTE_INSN_MAX)
333 fprintf (outfile, " %s", GET_NOTE_INSN_NAME (XINT (in_rtx, i)));
334 else
335 fprintf (outfile, " %d", XINT (in_rtx, i));
336 sawclose = 0;
337 break;
338
339 case 'u':
340 if (XEXP (in_rtx, i) != NULL)
341 {
342 rtx sub = XEXP (in_rtx, i);
343 enum rtx_code subc = GET_CODE (sub);
344
345 if (GET_CODE (in_rtx) == LABEL_REF)
346 {
347 if (subc == NOTE
348 && NOTE_LINE_NUMBER (sub) == NOTE_INSN_DELETED_LABEL)
349 {
350 if (flag_dump_unnumbered)
351 fprintf (outfile, " [# deleted]");
352 else
353 fprintf (outfile, " [%d deleted]", INSN_UID (sub));
354 sawclose = 0;
355 break;
356 }
357
358 if (subc != CODE_LABEL)
359 goto do_e;
360 }
361
362 if (flag_dump_unnumbered)
363 fputs (" #", outfile);
364 else
365 fprintf (outfile, " %d", INSN_UID (sub));
366 }
367 else
368 fputs (" 0", outfile);
369 sawclose = 0;
370 break;
371
372 case 'b':
373 if (XBITMAP (in_rtx, i) == NULL)
374 fputs (" {null}", outfile);
375 else
376 bitmap_print (outfile, XBITMAP (in_rtx, i), " {", "}");
377 sawclose = 0;
378 break;
379
380 case 't':
381 putc (' ', outfile);
382 fprintf (outfile, HOST_PTR_PRINTF, (char *) XTREE (in_rtx, i));
383 break;
384
385 case '*':
386 fputs (" Unknown", outfile);
387 sawclose = 0;
388 break;
389
390 default:
391 fprintf (stderr,
392 "switch format wrong in rtl.print_rtx(). format was: %c.\n",
393 format_ptr[-1]);
394 abort ();
395 }
396
397 switch (GET_CODE (in_rtx))
398 {
399 case MEM:
400 fprintf (outfile, " %d", MEM_ALIAS_SET (in_rtx));
401 break;
402
403 #if HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT && MAX_LONG_DOUBLE_TYPE_SIZE == 64
404 case CONST_DOUBLE:
405 if (FLOAT_MODE_P (GET_MODE (in_rtx)))
406 {
407 double val;
408 REAL_VALUE_FROM_CONST_DOUBLE (val, in_rtx);
409 fprintf (outfile, " [%.16g]", val);
410 }
411 break;
412 #endif
413
414 case CODE_LABEL:
415 fprintf (outfile, " [%d uses]", LABEL_NUSES (in_rtx));
416 if (LABEL_ALTERNATE_NAME (in_rtx))
417 fprintf (outfile, " [alternate name: %s]",
418 LABEL_ALTERNATE_NAME (in_rtx));
419 break;
420
421 case CALL_PLACEHOLDER:
422 for (tem = XEXP (in_rtx, 0); tem != 0; tem = NEXT_INSN (tem))
423 if (GET_CODE (tem) == CALL_INSN)
424 {
425 fprintf (outfile, " ");
426 print_rtx (tem);
427 break;
428 }
429 break;
430
431 default:
432 break;
433 }
434
435 if (dump_for_graph
436 && (is_insn || GET_CODE (in_rtx) == NOTE
437 || GET_CODE (in_rtx) == CODE_LABEL || GET_CODE (in_rtx) == BARRIER))
438 sawclose = 0;
439 else
440 {
441 fputc (')', outfile);
442 sawclose = 1;
443 }
444 }
445
446 /* Print an rtx on the current line of FILE. Initially indent IND
447 characters. */
448
449 void
450 print_inline_rtx (outf, x, ind)
451 FILE *outf;
452 rtx x;
453 int ind;
454 {
455 int oldsaw = sawclose;
456 int oldindent = indent;
457
458 sawclose = 0;
459 indent = ind;
460 outfile = outf;
461 print_rtx (x);
462 sawclose = oldsaw;
463 indent = oldindent;
464 }
465
466 /* Call this function from the debugger to see what X looks like. */
467
468 void
469 debug_rtx (x)
470 rtx x;
471 {
472 outfile = stderr;
473 print_rtx (x);
474 fprintf (stderr, "\n");
475 }
476
477 /* Count of rtx's to print with debug_rtx_list.
478 This global exists because gdb user defined commands have no arguments. */
479
480 int debug_rtx_count = 0; /* 0 is treated as equivalent to 1 */
481
482 /* Call this function to print list from X on.
483
484 N is a count of the rtx's to print. Positive values print from the specified
485 rtx on. Negative values print a window around the rtx.
486 EG: -5 prints 2 rtx's on either side (in addition to the specified rtx). */
487
488 void
489 debug_rtx_list (x, n)
490 rtx x;
491 int n;
492 {
493 int i,count;
494 rtx insn;
495
496 count = n == 0 ? 1 : n < 0 ? -n : n;
497
498 /* If we are printing a window, back up to the start. */
499
500 if (n < 0)
501 for (i = count / 2; i > 0; i--)
502 {
503 if (PREV_INSN (x) == 0)
504 break;
505 x = PREV_INSN (x);
506 }
507
508 for (i = count, insn = x; i > 0 && insn != 0; i--, insn = NEXT_INSN (insn))
509 debug_rtx (insn);
510 }
511
512 /* Call this function to print an rtx list from START to END inclusive. */
513
514 void
515 debug_rtx_range (start, end)
516 rtx start, end;
517 {
518 while (1)
519 {
520 debug_rtx (start);
521 if (!start || start == end)
522 break;
523 start = NEXT_INSN (start);
524 }
525 }
526
527 /* Call this function to search an rtx list to find one with insn uid UID,
528 and then call debug_rtx_list to print it, using DEBUG_RTX_COUNT.
529 The found insn is returned to enable further debugging analysis. */
530
531 rtx
532 debug_rtx_find (x, uid)
533 rtx x;
534 int uid;
535 {
536 while (x != 0 && INSN_UID (x) != uid)
537 x = NEXT_INSN (x);
538 if (x != 0)
539 {
540 debug_rtx_list (x, debug_rtx_count);
541 return x;
542 }
543 else
544 {
545 fprintf (stderr, "insn uid %d not found\n", uid);
546 return 0;
547 }
548 }
549
550 /* External entry point for printing a chain of insns
551 starting with RTX_FIRST onto file OUTF.
552 A blank line separates insns.
553
554 If RTX_FIRST is not an insn, then it alone is printed, with no newline. */
555
556 void
557 print_rtl (outf, rtx_first)
558 FILE *outf;
559 rtx rtx_first;
560 {
561 register rtx tmp_rtx;
562
563 outfile = outf;
564 sawclose = 0;
565
566 if (rtx_first == 0)
567 fputs ("(nil)\n", outf);
568 else
569 switch (GET_CODE (rtx_first))
570 {
571 case INSN:
572 case JUMP_INSN:
573 case CALL_INSN:
574 case NOTE:
575 case CODE_LABEL:
576 case BARRIER:
577 for (tmp_rtx = rtx_first; tmp_rtx != 0; tmp_rtx = NEXT_INSN (tmp_rtx))
578 if (! flag_dump_unnumbered
579 || GET_CODE (tmp_rtx) != NOTE || NOTE_LINE_NUMBER (tmp_rtx) < 0)
580 {
581 print_rtx (tmp_rtx);
582 fprintf (outfile, "\n");
583 }
584 break;
585
586 default:
587 print_rtx (rtx_first);
588 }
589 }
590
591 /* Like print_rtx, except specify a file. */
592 /* Return nonzero if we actually printed anything. */
593
594 int
595 print_rtl_single (outf, x)
596 FILE *outf;
597 rtx x;
598 {
599 outfile = outf;
600 sawclose = 0;
601 if (! flag_dump_unnumbered
602 || GET_CODE (x) != NOTE || NOTE_LINE_NUMBER (x) < 0)
603 {
604 print_rtx (x);
605 putc ('\n', outf);
606 return 1;
607 }
608 return 0;
609 }