1 /* Instruction scheduling pass.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com) Enhanced by,
5 and currently maintained by, Jim Wilson (wilson@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include "coretypes.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
34 #include "insn-attr.h"
36 #include "sched-int.h"
39 #ifdef INSN_SCHEDULING
40 /* target_units bitmask has 1 for each unit in the cpu. It should be
41 possible to compute this variable from the machine description.
42 But currently it is computed by examining the insn list. Since
43 this is only needed for visualization, it seems an acceptable
44 solution. (For understanding the mapping of bits to units, see
45 definition of function_units[] in "insn-attrtab.c".) The scheduler
46 using only DFA description should never use the following variable. */
48 static int target_units
= 0;
50 static char *safe_concat (char *, char *, const char *);
51 static int get_visual_tbl_length (void);
52 static void print_exp (char *, rtx
, int);
53 static void print_value (char *, rtx
, int);
54 static void print_pattern (char *, rtx
, int);
56 /* Print names of units on which insn can/should execute, for debugging. */
59 insn_print_units (rtx insn
)
62 int unit
= insn_unit (insn
);
65 fprintf (sched_dump
, "none");
67 fprintf (sched_dump
, "%s", function_units
[unit
].name
);
70 fprintf (sched_dump
, "[");
71 for (i
= 0, unit
= ~unit
; unit
; i
++, unit
>>= 1)
74 fprintf (sched_dump
, "%s", function_units
[i
].name
);
76 fprintf (sched_dump
, " ");
78 fprintf (sched_dump
, "]");
82 /* MAX_VISUAL_LINES is the maximum number of lines in visualization table
83 of a basic block. If more lines are needed, table is split to two.
84 n_visual_lines is the number of lines printed so far for a block.
85 visual_tbl contains the block visualization info.
86 vis_no_unit holds insns in a cycle that are not mapped to any unit. */
87 #define MAX_VISUAL_LINES 100
90 static unsigned visual_tbl_line_length
;
93 #define MAX_VISUAL_NO_UNIT 20
94 rtx vis_no_unit
[MAX_VISUAL_NO_UNIT
];
96 /* Finds units that are in use in this function. Required only
100 init_target_units (void)
105 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
110 unit
= insn_unit (insn
);
113 target_units
|= ~unit
;
115 target_units
|= (1 << unit
);
119 /* Return the length of the visualization table. */
122 get_visual_tbl_length (void)
128 if (targetm
.sched
.use_dfa_pipeline_interface
129 && targetm
.sched
.use_dfa_pipeline_interface ())
131 visual_tbl_line_length
= 1;
132 return 1; /* Can't return 0 because that will cause problems
136 /* Compute length of one field in line. */
137 s
= alloca (INSN_LEN
+ 6);
138 sprintf (s
, " %33s", "uname");
141 /* Compute length of one line. */
144 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
145 if (function_units
[unit
].bitmask
& target_units
)
146 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
149 n
+= strlen ("\n") + 2;
151 visual_tbl_line_length
= n
;
153 /* Compute length of visualization string. */
154 return (MAX_VISUAL_LINES
* n
);
157 /* Init block visualization debugging info. */
160 init_block_visualization (void)
162 strcpy (visual_tbl
, "");
170 safe_concat (char *buf
, char *cur
, const char *str
)
172 char *end
= buf
+ BUF_LEN
- 2; /* Leave room for null. */
181 while (cur
< end
&& (c
= *str
++) != '\0')
188 /* This recognizes rtx, I classified as expressions. These are always
189 represent some action on values or results of other expression, that
190 may be stored in objects representing values. */
193 print_exp (char *buf
, rtx x
, int verbose
)
198 const char *fun
= (char *) 0;
203 for (i
= 0; i
< 4; i
++)
209 switch (GET_CODE (x
))
213 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
214 && INTVAL (XEXP (x
, 1)) < 0)
217 op
[1] = GEN_INT (-INTVAL (XEXP (x
, 1)));
397 fun
= (verbose
) ? "sign_extract" : "sxt";
403 fun
= (verbose
) ? "zero_extract" : "zxt";
409 fun
= (verbose
) ? "sign_extend" : "sxn";
413 fun
= (verbose
) ? "zero_extend" : "zxn";
417 fun
= (verbose
) ? "float_extend" : "fxn";
421 fun
= (verbose
) ? "trunc" : "trn";
425 fun
= (verbose
) ? "float_trunc" : "ftr";
429 fun
= (verbose
) ? "float" : "flt";
433 fun
= (verbose
) ? "uns_float" : "ufl";
441 fun
= (verbose
) ? "uns_fix" : "ufx";
480 op
[0] = TRAP_CONDITION (x
);
489 case UNSPEC_VOLATILE
:
491 cur
= safe_concat (buf
, cur
, "unspec");
492 if (GET_CODE (x
) == UNSPEC_VOLATILE
)
493 cur
= safe_concat (buf
, cur
, "/v");
494 cur
= safe_concat (buf
, cur
, "[");
496 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
498 print_pattern (tmp
, XVECEXP (x
, 0, i
), verbose
);
499 cur
= safe_concat (buf
, cur
, sep
);
500 cur
= safe_concat (buf
, cur
, tmp
);
503 cur
= safe_concat (buf
, cur
, "] ");
504 sprintf (tmp
, "%d", XINT (x
, 1));
505 cur
= safe_concat (buf
, cur
, tmp
);
509 /* If (verbose) debug_rtx (x); */
510 st
[0] = GET_RTX_NAME (GET_CODE (x
));
514 /* Print this as a function? */
517 cur
= safe_concat (buf
, cur
, fun
);
518 cur
= safe_concat (buf
, cur
, "(");
521 for (i
= 0; i
< 4; i
++)
524 cur
= safe_concat (buf
, cur
, st
[i
]);
529 cur
= safe_concat (buf
, cur
, ",");
531 print_value (tmp
, op
[i
], verbose
);
532 cur
= safe_concat (buf
, cur
, tmp
);
537 cur
= safe_concat (buf
, cur
, ")");
540 /* Prints rtxes, I customarily classified as values. They're constants,
541 registers, labels, symbols and memory accesses. */
544 print_value (char *buf
, rtx x
, int verbose
)
549 switch (GET_CODE (x
))
552 sprintf (t
, HOST_WIDE_INT_PRINT_HEX
, INTVAL (x
));
553 cur
= safe_concat (buf
, cur
, t
);
556 if (FLOAT_MODE_P (GET_MODE (x
)))
557 real_to_decimal (t
, CONST_DOUBLE_REAL_VALUE (x
), sizeof (t
), 0, 1);
559 sprintf (t
, "<0x%lx,0x%lx>", (long) XWINT (x
, 2), (long) XWINT (x
, 3));
560 cur
= safe_concat (buf
, cur
, t
);
563 cur
= safe_concat (buf
, cur
, "\"");
564 cur
= safe_concat (buf
, cur
, XSTR (x
, 0));
565 cur
= safe_concat (buf
, cur
, "\"");
568 cur
= safe_concat (buf
, cur
, "`");
569 cur
= safe_concat (buf
, cur
, XSTR (x
, 0));
570 cur
= safe_concat (buf
, cur
, "'");
573 sprintf (t
, "L%d", INSN_UID (XEXP (x
, 0)));
574 cur
= safe_concat (buf
, cur
, t
);
577 print_value (t
, XEXP (x
, 0), verbose
);
578 cur
= safe_concat (buf
, cur
, "const(");
579 cur
= safe_concat (buf
, cur
, t
);
580 cur
= safe_concat (buf
, cur
, ")");
583 print_value (t
, XEXP (x
, 0), verbose
);
584 cur
= safe_concat (buf
, cur
, "high(");
585 cur
= safe_concat (buf
, cur
, t
);
586 cur
= safe_concat (buf
, cur
, ")");
589 if (REGNO (x
) < FIRST_PSEUDO_REGISTER
)
591 int c
= reg_names
[REGNO (x
)][0];
593 cur
= safe_concat (buf
, cur
, "%");
595 cur
= safe_concat (buf
, cur
, reg_names
[REGNO (x
)]);
599 sprintf (t
, "r%d", REGNO (x
));
600 cur
= safe_concat (buf
, cur
, t
);
604 print_value (t
, SUBREG_REG (x
), verbose
);
605 cur
= safe_concat (buf
, cur
, t
);
606 sprintf (t
, "#%d", SUBREG_BYTE (x
));
607 cur
= safe_concat (buf
, cur
, t
);
610 cur
= safe_concat (buf
, cur
, "scratch");
613 cur
= safe_concat (buf
, cur
, "cc0");
616 cur
= safe_concat (buf
, cur
, "pc");
619 print_value (t
, XEXP (x
, 0), verbose
);
620 cur
= safe_concat (buf
, cur
, "[");
621 cur
= safe_concat (buf
, cur
, t
);
622 cur
= safe_concat (buf
, cur
, "]");
625 print_exp (t
, x
, verbose
);
626 cur
= safe_concat (buf
, cur
, t
);
631 /* The next step in insn detalization, its pattern recognition. */
634 print_pattern (char *buf
, rtx x
, int verbose
)
636 char t1
[BUF_LEN
], t2
[BUF_LEN
], t3
[BUF_LEN
];
638 switch (GET_CODE (x
))
641 print_value (t1
, SET_DEST (x
), verbose
);
642 print_value (t2
, SET_SRC (x
), verbose
);
643 sprintf (buf
, "%s=%s", t1
, t2
);
646 sprintf (buf
, "return");
649 print_exp (buf
, x
, verbose
);
652 print_value (t1
, XEXP (x
, 0), verbose
);
653 sprintf (buf
, "clobber %s", t1
);
656 print_value (t1
, XEXP (x
, 0), verbose
);
657 sprintf (buf
, "use %s", t1
);
660 if (GET_CODE (COND_EXEC_TEST (x
)) == NE
661 && XEXP (COND_EXEC_TEST (x
), 1) == const0_rtx
)
662 print_value (t1
, XEXP (COND_EXEC_TEST (x
), 0), verbose
);
663 else if (GET_CODE (COND_EXEC_TEST (x
)) == EQ
664 && XEXP (COND_EXEC_TEST (x
), 1) == const0_rtx
)
667 print_value (t1
+ 1, XEXP (COND_EXEC_TEST (x
), 0), verbose
);
670 print_value (t1
, COND_EXEC_TEST (x
), verbose
);
671 print_pattern (t2
, COND_EXEC_CODE (x
), verbose
);
672 sprintf (buf
, "(%s) %s", t1
, t2
);
679 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
681 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
682 sprintf (t3
, "%s%s;", t1
, t2
);
685 sprintf (buf
, "%s}", t1
);
689 /* Should never see SEQUENCE codes until after reorg. */
693 sprintf (buf
, "asm {%s}", XSTR (x
, 0));
698 print_value (buf
, XEXP (x
, 0), verbose
);
701 print_value (t1
, TRAP_CONDITION (x
), verbose
);
702 sprintf (buf
, "trap_if %s", t1
);
708 sprintf (t1
, "unspec{");
709 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
711 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
712 sprintf (t3
, "%s%s;", t1
, t2
);
715 sprintf (buf
, "%s}", t1
);
718 case UNSPEC_VOLATILE
:
722 sprintf (t1
, "unspec/v{");
723 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
725 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
726 sprintf (t3
, "%s%s;", t1
, t2
);
729 sprintf (buf
, "%s}", t1
);
733 print_value (buf
, x
, verbose
);
735 } /* print_pattern */
737 /* This is the main function in rtl visualization mechanism. It
738 accepts an rtx and tries to recognize it as an insn, then prints it
739 properly in human readable form, resembling assembler mnemonics.
740 For every insn it prints its UID and BB the insn belongs too.
741 (Probably the last "option" should be extended somehow, since it
742 depends now on sched.c inner variables ...) */
745 print_insn (char *buf
, rtx x
, int verbose
)
750 switch (GET_CODE (x
))
753 print_pattern (t
, PATTERN (x
), verbose
);
755 sprintf (buf
, "%s: %s", (*current_sched_info
->print_insn
) (x
, 1),
758 sprintf (buf
, "%-4d %s", INSN_UID (x
), t
);
761 print_pattern (t
, PATTERN (x
), verbose
);
763 sprintf (buf
, "%s: jump %s", (*current_sched_info
->print_insn
) (x
, 1),
766 sprintf (buf
, "%-4d %s", INSN_UID (x
), t
);
770 if (GET_CODE (x
) == PARALLEL
)
772 x
= XVECEXP (x
, 0, 0);
773 print_pattern (t
, x
, verbose
);
776 strcpy (t
, "call <...>");
778 sprintf (buf
, "%s: %s", (*current_sched_info
->print_insn
) (x
, 1), t
);
780 sprintf (buf
, "%-4d %s", INSN_UID (insn
), t
);
783 sprintf (buf
, "L%d:", INSN_UID (x
));
786 sprintf (buf
, "i% 4d: barrier", INSN_UID (x
));
789 if (NOTE_LINE_NUMBER (x
) > 0)
791 expanded_location xloc
;
792 NOTE_EXPANDED_LOCATION (xloc
, x
);
793 sprintf (buf
, "%4d note \"%s\" %d", INSN_UID (x
),
794 xloc
.file
, xloc
.line
);
797 sprintf (buf
, "%4d %s", INSN_UID (x
),
798 GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (x
)));
803 sprintf (buf
, "Not an INSN at all\n");
807 sprintf (buf
, "i%-4d <What?>", INSN_UID (x
));
811 /* Print visualization debugging info. The scheduler using only DFA
812 description should never use the following function. */
815 print_block_visualization (const char *s
)
820 fprintf (sched_dump
, "\n;; ==================== scheduling visualization %s \n", s
);
822 /* Print names of units. */
823 fprintf (sched_dump
, ";; %-8s", "clock");
824 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
825 if (function_units
[unit
].bitmask
& target_units
)
826 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
827 fprintf (sched_dump
, " %-33s", function_units
[unit
].name
);
828 fprintf (sched_dump
, " %-8s\n", "no-unit");
830 fprintf (sched_dump
, ";; %-8s", "=====");
831 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
832 if (function_units
[unit
].bitmask
& target_units
)
833 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
834 fprintf (sched_dump
, " %-33s", "==============================");
835 fprintf (sched_dump
, " %-8s\n", "=======");
837 /* Print insns in each cycle. */
838 fprintf (sched_dump
, "%s\n", visual_tbl
);
841 /* Print insns in the 'no_unit' column of visualization. */
844 visualize_no_unit (rtx insn
)
846 if (n_vis_no_unit
< MAX_VISUAL_NO_UNIT
)
848 vis_no_unit
[n_vis_no_unit
] = insn
;
853 /* Print insns scheduled in clock, for visualization. */
856 visualize_scheduled_insns (int clock
)
860 /* If no more room, split table into two. */
861 if (n_visual_lines
>= MAX_VISUAL_LINES
)
863 print_block_visualization ("(incomplete)");
864 init_block_visualization ();
869 sprintf (visual_tbl
+ strlen (visual_tbl
), ";; %-8d", clock
);
870 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
871 if (function_units
[unit
].bitmask
& target_units
)
872 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
874 int instance
= unit
+ i
* FUNCTION_UNITS_SIZE
;
875 rtx insn
= get_unit_last_insn (instance
);
877 /* Print insns that still keep the unit busy. */
879 && actual_hazard_this_instance (unit
, instance
, insn
, clock
, 0))
882 print_insn (str
, insn
, 0);
883 str
[INSN_LEN
] = '\0';
884 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-33s", str
);
887 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-33s", "------------------------------");
890 /* Print insns that are not assigned to any unit. */
891 for (i
= 0; i
< n_vis_no_unit
; i
++)
892 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-8d",
893 INSN_UID (vis_no_unit
[i
]));
896 sprintf (visual_tbl
+ strlen (visual_tbl
), "\n");
899 /* Print stalled cycles. */
902 visualize_stall_cycles (int stalls
)
904 static const char *const prefix
= ";; ";
905 const char *suffix
= "\n";
908 /* If no more room, split table into two. */
909 if (n_visual_lines
>= MAX_VISUAL_LINES
)
911 print_block_visualization ("(incomplete)");
912 init_block_visualization ();
917 p
= visual_tbl
+ strlen (visual_tbl
);
919 p
+= strlen (prefix
);
921 if ((unsigned) stalls
>
922 visual_tbl_line_length
- strlen (prefix
) - strlen (suffix
))
925 stalls
= visual_tbl_line_length
- strlen (prefix
) - strlen (suffix
);
928 memset (p
, '.', stalls
);
934 /* Allocate data used for visualization during scheduling. */
937 visualize_alloc (void)
939 visual_tbl
= xmalloc (get_visual_tbl_length ());
942 /* Free data used for visualization. */
945 visualize_free (void)