1 /* Instruction scheduling pass.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2002 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
PARAMS ((char *, char *, const char *));
51 static int get_visual_tbl_length
PARAMS ((void));
52 static void print_exp
PARAMS ((char *, rtx
, int));
53 static void print_value
PARAMS ((char *, rtx
, int));
54 static void print_pattern
PARAMS ((char *, rtx
, int));
56 /* Print names of units on which insn can/should execute, for debugging. */
59 insn_print_units (insn
)
63 int unit
= insn_unit (insn
);
66 fprintf (sched_dump
, "none");
68 fprintf (sched_dump
, "%s", function_units
[unit
].name
);
71 fprintf (sched_dump
, "[");
72 for (i
= 0, unit
= ~unit
; unit
; i
++, unit
>>= 1)
75 fprintf (sched_dump
, "%s", function_units
[i
].name
);
77 fprintf (sched_dump
, " ");
79 fprintf (sched_dump
, "]");
83 /* MAX_VISUAL_LINES is the maximum number of lines in visualization table
84 of a basic block. If more lines are needed, table is splitted to two.
85 n_visual_lines is the number of lines printed so far for a block.
86 visual_tbl contains the block visualization info.
87 vis_no_unit holds insns in a cycle that are not mapped to any unit. */
88 #define MAX_VISUAL_LINES 100
91 static unsigned visual_tbl_line_length
;
94 #define MAX_VISUAL_NO_UNIT 20
95 rtx vis_no_unit
[MAX_VISUAL_NO_UNIT
];
97 /* Finds units that are in use in this function. Required only
106 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
111 unit
= insn_unit (insn
);
114 target_units
|= ~unit
;
116 target_units
|= (1 << unit
);
120 /* Return the length of the visualization table. */
123 get_visual_tbl_length ()
129 if (targetm
.sched
.use_dfa_pipeline_interface
130 && (*targetm
.sched
.use_dfa_pipeline_interface
) ())
132 visual_tbl_line_length
= 1;
133 return 1; /* Can't return 0 because that will cause problems
137 /* Compute length of one field in line. */
138 s
= (char *) alloca (INSN_LEN
+ 6);
139 sprintf (s
, " %33s", "uname");
142 /* Compute length of one line. */
145 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
146 if (function_units
[unit
].bitmask
& target_units
)
147 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
150 n
+= strlen ("\n") + 2;
152 visual_tbl_line_length
= n
;
154 /* Compute length of visualization string. */
155 return (MAX_VISUAL_LINES
* n
);
158 /* Init block visualization debugging info. */
161 init_block_visualization ()
163 strcpy (visual_tbl
, "");
171 safe_concat (buf
, cur
, str
)
176 char *end
= buf
+ BUF_LEN
- 2; /* Leave room for null. */
185 while (cur
< end
&& (c
= *str
++) != '\0')
192 /* This recognizes rtx, I classified as expressions. These are always
193 represent some action on values or results of other expression, that
194 may be stored in objects representing values. */
197 print_exp (buf
, x
, verbose
)
205 const char *fun
= (char *) 0;
210 for (i
= 0; i
< 4; i
++)
216 switch (GET_CODE (x
))
220 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
221 && INTVAL (XEXP (x
, 1)) < 0)
224 op
[1] = GEN_INT (-INTVAL (XEXP (x
, 1)));
404 fun
= (verbose
) ? "sign_extract" : "sxt";
410 fun
= (verbose
) ? "zero_extract" : "zxt";
416 fun
= (verbose
) ? "sign_extend" : "sxn";
420 fun
= (verbose
) ? "zero_extend" : "zxn";
424 fun
= (verbose
) ? "float_extend" : "fxn";
428 fun
= (verbose
) ? "trunc" : "trn";
432 fun
= (verbose
) ? "float_trunc" : "ftr";
436 fun
= (verbose
) ? "float" : "flt";
440 fun
= (verbose
) ? "uns_float" : "ufl";
448 fun
= (verbose
) ? "uns_fix" : "ufx";
487 op
[0] = TRAP_CONDITION (x
);
496 case UNSPEC_VOLATILE
:
498 cur
= safe_concat (buf
, cur
, "unspec");
499 if (GET_CODE (x
) == UNSPEC_VOLATILE
)
500 cur
= safe_concat (buf
, cur
, "/v");
501 cur
= safe_concat (buf
, cur
, "[");
503 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
505 print_pattern (tmp
, XVECEXP (x
, 0, i
), verbose
);
506 cur
= safe_concat (buf
, cur
, sep
);
507 cur
= safe_concat (buf
, cur
, tmp
);
510 cur
= safe_concat (buf
, cur
, "] ");
511 sprintf (tmp
, "%d", XINT (x
, 1));
512 cur
= safe_concat (buf
, cur
, tmp
);
516 /* If (verbose) debug_rtx (x); */
517 st
[0] = GET_RTX_NAME (GET_CODE (x
));
521 /* Print this as a function? */
524 cur
= safe_concat (buf
, cur
, fun
);
525 cur
= safe_concat (buf
, cur
, "(");
528 for (i
= 0; i
< 4; i
++)
531 cur
= safe_concat (buf
, cur
, st
[i
]);
536 cur
= safe_concat (buf
, cur
, ",");
538 print_value (tmp
, op
[i
], verbose
);
539 cur
= safe_concat (buf
, cur
, tmp
);
544 cur
= safe_concat (buf
, cur
, ")");
547 /* Prints rtxes, I customly classified as values. They're constants,
548 registers, labels, symbols and memory accesses. */
551 print_value (buf
, x
, verbose
)
559 switch (GET_CODE (x
))
562 sprintf (t
, HOST_WIDE_INT_PRINT_HEX
, INTVAL (x
));
563 cur
= safe_concat (buf
, cur
, t
);
566 if (FLOAT_MODE_P (GET_MODE (x
)))
567 real_to_decimal (t
, CONST_DOUBLE_REAL_VALUE (x
), sizeof (t
), 0, 1);
569 sprintf (t
, "<0x%lx,0x%lx>", (long) XWINT (x
, 2), (long) XWINT (x
, 3));
570 cur
= safe_concat (buf
, cur
, t
);
573 cur
= safe_concat (buf
, cur
, "\"");
574 cur
= safe_concat (buf
, cur
, XSTR (x
, 0));
575 cur
= safe_concat (buf
, cur
, "\"");
578 cur
= safe_concat (buf
, cur
, "`");
579 cur
= safe_concat (buf
, cur
, XSTR (x
, 0));
580 cur
= safe_concat (buf
, cur
, "'");
583 sprintf (t
, "L%d", INSN_UID (XEXP (x
, 0)));
584 cur
= safe_concat (buf
, cur
, t
);
587 print_value (t
, XEXP (x
, 0), verbose
);
588 cur
= safe_concat (buf
, cur
, "const(");
589 cur
= safe_concat (buf
, cur
, t
);
590 cur
= safe_concat (buf
, cur
, ")");
593 print_value (t
, XEXP (x
, 0), verbose
);
594 cur
= safe_concat (buf
, cur
, "high(");
595 cur
= safe_concat (buf
, cur
, t
);
596 cur
= safe_concat (buf
, cur
, ")");
599 if (REGNO (x
) < FIRST_PSEUDO_REGISTER
)
601 int c
= reg_names
[REGNO (x
)][0];
603 cur
= safe_concat (buf
, cur
, "%");
605 cur
= safe_concat (buf
, cur
, reg_names
[REGNO (x
)]);
609 sprintf (t
, "r%d", REGNO (x
));
610 cur
= safe_concat (buf
, cur
, t
);
614 print_value (t
, SUBREG_REG (x
), verbose
);
615 cur
= safe_concat (buf
, cur
, t
);
616 sprintf (t
, "#%d", SUBREG_BYTE (x
));
617 cur
= safe_concat (buf
, cur
, t
);
620 cur
= safe_concat (buf
, cur
, "scratch");
623 cur
= safe_concat (buf
, cur
, "cc0");
626 cur
= safe_concat (buf
, cur
, "pc");
629 print_value (t
, XEXP (x
, 0), verbose
);
630 cur
= safe_concat (buf
, cur
, "[");
631 cur
= safe_concat (buf
, cur
, t
);
632 cur
= safe_concat (buf
, cur
, "]");
635 print_exp (t
, x
, verbose
);
636 cur
= safe_concat (buf
, cur
, t
);
641 /* The next step in insn detalization, its pattern recognition. */
644 print_pattern (buf
, x
, verbose
)
649 char t1
[BUF_LEN
], t2
[BUF_LEN
], t3
[BUF_LEN
];
651 switch (GET_CODE (x
))
654 print_value (t1
, SET_DEST (x
), verbose
);
655 print_value (t2
, SET_SRC (x
), verbose
);
656 sprintf (buf
, "%s=%s", t1
, t2
);
659 sprintf (buf
, "return");
662 print_exp (buf
, x
, verbose
);
665 print_value (t1
, XEXP (x
, 0), verbose
);
666 sprintf (buf
, "clobber %s", t1
);
669 print_value (t1
, XEXP (x
, 0), verbose
);
670 sprintf (buf
, "use %s", t1
);
673 if (GET_CODE (COND_EXEC_TEST (x
)) == NE
674 && XEXP (COND_EXEC_TEST (x
), 1) == const0_rtx
)
675 print_value (t1
, XEXP (COND_EXEC_TEST (x
), 0), verbose
);
676 else if (GET_CODE (COND_EXEC_TEST (x
)) == EQ
677 && XEXP (COND_EXEC_TEST (x
), 1) == const0_rtx
)
680 print_value (t1
+ 1, XEXP (COND_EXEC_TEST (x
), 0), verbose
);
683 print_value (t1
, COND_EXEC_TEST (x
), verbose
);
684 print_pattern (t2
, COND_EXEC_CODE (x
), verbose
);
685 sprintf (buf
, "(%s) %s", t1
, t2
);
692 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
694 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
695 sprintf (t3
, "%s%s;", t1
, t2
);
698 sprintf (buf
, "%s}", t1
);
702 /* Should never see SEQUENCE codes until after reorg. */
706 sprintf (buf
, "asm {%s}", XSTR (x
, 0));
711 print_value (buf
, XEXP (x
, 0), verbose
);
714 print_value (t1
, TRAP_CONDITION (x
), verbose
);
715 sprintf (buf
, "trap_if %s", t1
);
721 sprintf (t1
, "unspec{");
722 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
724 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
725 sprintf (t3
, "%s%s;", t1
, t2
);
728 sprintf (buf
, "%s}", t1
);
731 case UNSPEC_VOLATILE
:
735 sprintf (t1
, "unspec/v{");
736 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
738 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
739 sprintf (t3
, "%s%s;", t1
, t2
);
742 sprintf (buf
, "%s}", t1
);
746 print_value (buf
, x
, verbose
);
748 } /* print_pattern */
750 /* This is the main function in rtl visualization mechanism. It
751 accepts an rtx and tries to recognize it as an insn, then prints it
752 properly in human readable form, resembling assembler mnemonics.
753 For every insn it prints its UID and BB the insn belongs too.
754 (Probably the last "option" should be extended somehow, since it
755 depends now on sched.c inner variables ...) */
758 print_insn (buf
, x
, verbose
)
766 switch (GET_CODE (x
))
769 print_pattern (t
, PATTERN (x
), verbose
);
771 sprintf (buf
, "%s: %s", (*current_sched_info
->print_insn
) (x
, 1),
774 sprintf (buf
, "%-4d %s", INSN_UID (x
), t
);
777 print_pattern (t
, PATTERN (x
), verbose
);
779 sprintf (buf
, "%s: jump %s", (*current_sched_info
->print_insn
) (x
, 1),
782 sprintf (buf
, "%-4d %s", INSN_UID (x
), t
);
786 if (GET_CODE (x
) == PARALLEL
)
788 x
= XVECEXP (x
, 0, 0);
789 print_pattern (t
, x
, verbose
);
792 strcpy (t
, "call <...>");
794 sprintf (buf
, "%s: %s", (*current_sched_info
->print_insn
) (x
, 1), t
);
796 sprintf (buf
, "%-4d %s", INSN_UID (insn
), t
);
799 sprintf (buf
, "L%d:", INSN_UID (x
));
802 sprintf (buf
, "i% 4d: barrier", INSN_UID (x
));
805 if (NOTE_LINE_NUMBER (x
) > 0)
806 sprintf (buf
, "%4d note \"%s\" %d", INSN_UID (x
),
807 NOTE_SOURCE_FILE (x
), NOTE_LINE_NUMBER (x
));
809 sprintf (buf
, "%4d %s", INSN_UID (x
),
810 GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (x
)));
815 sprintf (buf
, "Not an INSN at all\n");
819 sprintf (buf
, "i%-4d <What?>", INSN_UID (x
));
823 /* Print visualization debugging info. The scheduler using only DFA
824 description should never use the following function. */
827 print_block_visualization (s
)
833 fprintf (sched_dump
, "\n;; ==================== scheduling visualization %s \n", s
);
835 /* Print names of units. */
836 fprintf (sched_dump
, ";; %-8s", "clock");
837 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
838 if (function_units
[unit
].bitmask
& target_units
)
839 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
840 fprintf (sched_dump
, " %-33s", function_units
[unit
].name
);
841 fprintf (sched_dump
, " %-8s\n", "no-unit");
843 fprintf (sched_dump
, ";; %-8s", "=====");
844 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
845 if (function_units
[unit
].bitmask
& target_units
)
846 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
847 fprintf (sched_dump
, " %-33s", "==============================");
848 fprintf (sched_dump
, " %-8s\n", "=======");
850 /* Print insns in each cycle. */
851 fprintf (sched_dump
, "%s\n", visual_tbl
);
854 /* Print insns in the 'no_unit' column of visualization. */
857 visualize_no_unit (insn
)
860 if (n_vis_no_unit
< MAX_VISUAL_NO_UNIT
)
862 vis_no_unit
[n_vis_no_unit
] = insn
;
867 /* Print insns scheduled in clock, for visualization. */
870 visualize_scheduled_insns (clock
)
875 /* If no more room, split table into two. */
876 if (n_visual_lines
>= MAX_VISUAL_LINES
)
878 print_block_visualization ("(incomplete)");
879 init_block_visualization ();
884 sprintf (visual_tbl
+ strlen (visual_tbl
), ";; %-8d", clock
);
885 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
886 if (function_units
[unit
].bitmask
& target_units
)
887 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
889 int instance
= unit
+ i
* FUNCTION_UNITS_SIZE
;
890 rtx insn
= get_unit_last_insn (instance
);
892 /* Print insns that still keep the unit busy. */
894 && actual_hazard_this_instance (unit
, instance
, insn
, clock
, 0))
897 print_insn (str
, insn
, 0);
898 str
[INSN_LEN
] = '\0';
899 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-33s", str
);
902 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-33s", "------------------------------");
905 /* Print insns that are not assigned to any unit. */
906 for (i
= 0; i
< n_vis_no_unit
; i
++)
907 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-8d",
908 INSN_UID (vis_no_unit
[i
]));
911 sprintf (visual_tbl
+ strlen (visual_tbl
), "\n");
914 /* Print stalled cycles. */
917 visualize_stall_cycles (stalls
)
920 static const char *const prefix
= ";; ";
921 const char *suffix
= "\n";
924 /* If no more room, split table into two. */
925 if (n_visual_lines
>= MAX_VISUAL_LINES
)
927 print_block_visualization ("(incomplete)");
928 init_block_visualization ();
933 p
= visual_tbl
+ strlen (visual_tbl
);
935 p
+= strlen (prefix
);
937 if ((unsigned) stalls
>
938 visual_tbl_line_length
- strlen (prefix
) - strlen (suffix
))
941 stalls
= visual_tbl_line_length
- strlen (prefix
) - strlen (suffix
);
944 memset (p
, '.', stalls
);
950 /* Allocate data used for visualization during scheduling. */
955 visual_tbl
= xmalloc (get_visual_tbl_length ());
958 /* Free data used for visualization. */