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
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
32 #include "insn-attr.h"
34 #include "sched-int.h"
37 #ifdef INSN_SCHEDULING
38 /* target_units bitmask has 1 for each unit in the cpu. It should be
39 possible to compute this variable from the machine description.
40 But currently it is computed by examining the insn list. Since
41 this is only needed for visualization, it seems an acceptable
42 solution. (For understanding the mapping of bits to units, see
43 definition of function_units[] in "insn-attrtab.c".) The scheduler
44 using only DFA description should never use the following variable. */
46 static int target_units
= 0;
48 static char *safe_concat
PARAMS ((char *, char *, const char *));
49 static int get_visual_tbl_length
PARAMS ((void));
50 static void print_exp
PARAMS ((char *, rtx
, int));
51 static void print_value
PARAMS ((char *, rtx
, int));
52 static void print_pattern
PARAMS ((char *, rtx
, int));
54 /* Print names of units on which insn can/should execute, for debugging. */
57 insn_print_units (insn
)
61 int unit
= insn_unit (insn
);
64 fprintf (sched_dump
, "none");
66 fprintf (sched_dump
, "%s", function_units
[unit
].name
);
69 fprintf (sched_dump
, "[");
70 for (i
= 0, unit
= ~unit
; unit
; i
++, unit
>>= 1)
73 fprintf (sched_dump
, "%s", function_units
[i
].name
);
75 fprintf (sched_dump
, " ");
77 fprintf (sched_dump
, "]");
81 /* MAX_VISUAL_LINES is the maximum number of lines in visualization table
82 of a basic block. If more lines are needed, table is splitted to two.
83 n_visual_lines is the number of lines printed so far for a block.
84 visual_tbl contains the block visualization info.
85 vis_no_unit holds insns in a cycle that are not mapped to any unit. */
86 #define MAX_VISUAL_LINES 100
89 static unsigned visual_tbl_line_length
;
92 #define MAX_VISUAL_NO_UNIT 20
93 rtx vis_no_unit
[MAX_VISUAL_NO_UNIT
];
95 /* Finds units that are in use in this function. Required only
104 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
109 unit
= insn_unit (insn
);
112 target_units
|= ~unit
;
114 target_units
|= (1 << unit
);
118 /* Return the length of the visualization table. */
121 get_visual_tbl_length ()
127 if (targetm
.sched
.use_dfa_pipeline_interface
128 && (*targetm
.sched
.use_dfa_pipeline_interface
) ())
130 visual_tbl_line_length
= 1;
131 return 1; /* Can't return 0 because that will cause problems
135 /* Compute length of one field in line. */
136 s
= (char *) alloca (INSN_LEN
+ 6);
137 sprintf (s
, " %33s", "uname");
140 /* Compute length of one line. */
143 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
144 if (function_units
[unit
].bitmask
& target_units
)
145 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
148 n
+= strlen ("\n") + 2;
150 visual_tbl_line_length
= n
;
152 /* Compute length of visualization string. */
153 return (MAX_VISUAL_LINES
* n
);
156 /* Init block visualization debugging info. */
159 init_block_visualization ()
161 strcpy (visual_tbl
, "");
169 safe_concat (buf
, cur
, str
)
174 char *end
= buf
+ BUF_LEN
- 2; /* Leave room for null. */
183 while (cur
< end
&& (c
= *str
++) != '\0')
190 /* This recognizes rtx, I classified as expressions. These are always
191 represent some action on values or results of other expression, that
192 may be stored in objects representing values. */
195 print_exp (buf
, x
, verbose
)
203 const char *fun
= (char *) 0;
208 for (i
= 0; i
< 4; i
++)
214 switch (GET_CODE (x
))
218 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
219 && INTVAL (XEXP (x
, 1)) < 0)
222 op
[1] = GEN_INT (-INTVAL (XEXP (x
, 1)));
402 fun
= (verbose
) ? "sign_extract" : "sxt";
408 fun
= (verbose
) ? "zero_extract" : "zxt";
414 fun
= (verbose
) ? "sign_extend" : "sxn";
418 fun
= (verbose
) ? "zero_extend" : "zxn";
422 fun
= (verbose
) ? "float_extend" : "fxn";
426 fun
= (verbose
) ? "trunc" : "trn";
430 fun
= (verbose
) ? "float_trunc" : "ftr";
434 fun
= (verbose
) ? "float" : "flt";
438 fun
= (verbose
) ? "uns_float" : "ufl";
446 fun
= (verbose
) ? "uns_fix" : "ufx";
485 op
[0] = TRAP_CONDITION (x
);
494 case UNSPEC_VOLATILE
:
496 cur
= safe_concat (buf
, cur
, "unspec");
497 if (GET_CODE (x
) == UNSPEC_VOLATILE
)
498 cur
= safe_concat (buf
, cur
, "/v");
499 cur
= safe_concat (buf
, cur
, "[");
501 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
503 print_pattern (tmp
, XVECEXP (x
, 0, i
), verbose
);
504 cur
= safe_concat (buf
, cur
, sep
);
505 cur
= safe_concat (buf
, cur
, tmp
);
508 cur
= safe_concat (buf
, cur
, "] ");
509 sprintf (tmp
, "%d", XINT (x
, 1));
510 cur
= safe_concat (buf
, cur
, tmp
);
514 /* If (verbose) debug_rtx (x); */
515 st
[0] = GET_RTX_NAME (GET_CODE (x
));
519 /* Print this as a function? */
522 cur
= safe_concat (buf
, cur
, fun
);
523 cur
= safe_concat (buf
, cur
, "(");
526 for (i
= 0; i
< 4; i
++)
529 cur
= safe_concat (buf
, cur
, st
[i
]);
534 cur
= safe_concat (buf
, cur
, ",");
536 print_value (tmp
, op
[i
], verbose
);
537 cur
= safe_concat (buf
, cur
, tmp
);
542 cur
= safe_concat (buf
, cur
, ")");
545 /* Prints rtxes, I customly classified as values. They're constants,
546 registers, labels, symbols and memory accesses. */
549 print_value (buf
, x
, verbose
)
557 switch (GET_CODE (x
))
560 sprintf (t
, HOST_WIDE_INT_PRINT_HEX
, INTVAL (x
));
561 cur
= safe_concat (buf
, cur
, t
);
564 if (FLOAT_MODE_P (GET_MODE (x
)))
568 REAL_VALUE_FROM_CONST_DOUBLE (r
, x
);
569 REAL_VALUE_TO_DECIMAL(r
, t
, 6);
572 sprintf (t
, "<0x%lx,0x%lx>", (long) XWINT (x
, 2), (long) XWINT (x
, 3));
573 cur
= safe_concat (buf
, cur
, t
);
576 cur
= safe_concat (buf
, cur
, "\"");
577 cur
= safe_concat (buf
, cur
, XSTR (x
, 0));
578 cur
= safe_concat (buf
, cur
, "\"");
581 cur
= safe_concat (buf
, cur
, "`");
582 cur
= safe_concat (buf
, cur
, XSTR (x
, 0));
583 cur
= safe_concat (buf
, cur
, "'");
586 sprintf (t
, "L%d", INSN_UID (XEXP (x
, 0)));
587 cur
= safe_concat (buf
, cur
, t
);
590 print_value (t
, XEXP (x
, 0), verbose
);
591 cur
= safe_concat (buf
, cur
, "const(");
592 cur
= safe_concat (buf
, cur
, t
);
593 cur
= safe_concat (buf
, cur
, ")");
596 print_value (t
, XEXP (x
, 0), verbose
);
597 cur
= safe_concat (buf
, cur
, "high(");
598 cur
= safe_concat (buf
, cur
, t
);
599 cur
= safe_concat (buf
, cur
, ")");
602 if (REGNO (x
) < FIRST_PSEUDO_REGISTER
)
604 int c
= reg_names
[REGNO (x
)][0];
606 cur
= safe_concat (buf
, cur
, "%");
608 cur
= safe_concat (buf
, cur
, reg_names
[REGNO (x
)]);
612 sprintf (t
, "r%d", REGNO (x
));
613 cur
= safe_concat (buf
, cur
, t
);
617 print_value (t
, SUBREG_REG (x
), verbose
);
618 cur
= safe_concat (buf
, cur
, t
);
619 sprintf (t
, "#%d", SUBREG_BYTE (x
));
620 cur
= safe_concat (buf
, cur
, t
);
623 cur
= safe_concat (buf
, cur
, "scratch");
626 cur
= safe_concat (buf
, cur
, "cc0");
629 cur
= safe_concat (buf
, cur
, "pc");
632 print_value (t
, XEXP (x
, 0), verbose
);
633 cur
= safe_concat (buf
, cur
, "[");
634 cur
= safe_concat (buf
, cur
, t
);
635 cur
= safe_concat (buf
, cur
, "]");
638 print_exp (t
, x
, verbose
);
639 cur
= safe_concat (buf
, cur
, t
);
644 /* The next step in insn detalization, its pattern recognition. */
647 print_pattern (buf
, x
, verbose
)
652 char t1
[BUF_LEN
], t2
[BUF_LEN
], t3
[BUF_LEN
];
654 switch (GET_CODE (x
))
657 print_value (t1
, SET_DEST (x
), verbose
);
658 print_value (t2
, SET_SRC (x
), verbose
);
659 sprintf (buf
, "%s=%s", t1
, t2
);
662 sprintf (buf
, "return");
665 print_exp (buf
, x
, verbose
);
668 print_value (t1
, XEXP (x
, 0), verbose
);
669 sprintf (buf
, "clobber %s", t1
);
672 print_value (t1
, XEXP (x
, 0), verbose
);
673 sprintf (buf
, "use %s", t1
);
676 if (GET_CODE (COND_EXEC_TEST (x
)) == NE
677 && XEXP (COND_EXEC_TEST (x
), 1) == const0_rtx
)
678 print_value (t1
, XEXP (COND_EXEC_TEST (x
), 0), verbose
);
679 else if (GET_CODE (COND_EXEC_TEST (x
)) == EQ
680 && XEXP (COND_EXEC_TEST (x
), 1) == const0_rtx
)
683 print_value (t1
+ 1, XEXP (COND_EXEC_TEST (x
), 0), verbose
);
686 print_value (t1
, COND_EXEC_TEST (x
), verbose
);
687 print_pattern (t2
, COND_EXEC_CODE (x
), verbose
);
688 sprintf (buf
, "(%s) %s", t1
, t2
);
695 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
697 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
698 sprintf (t3
, "%s%s;", t1
, t2
);
701 sprintf (buf
, "%s}", t1
);
705 /* Should never see SEQUENCE codes until after reorg. */
709 sprintf (buf
, "asm {%s}", XSTR (x
, 0));
714 print_value (buf
, XEXP (x
, 0), verbose
);
717 print_value (t1
, TRAP_CONDITION (x
), verbose
);
718 sprintf (buf
, "trap_if %s", t1
);
724 sprintf (t1
, "unspec{");
725 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
727 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
728 sprintf (t3
, "%s%s;", t1
, t2
);
731 sprintf (buf
, "%s}", t1
);
734 case UNSPEC_VOLATILE
:
738 sprintf (t1
, "unspec/v{");
739 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
741 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
742 sprintf (t3
, "%s%s;", t1
, t2
);
745 sprintf (buf
, "%s}", t1
);
749 print_value (buf
, x
, verbose
);
751 } /* print_pattern */
753 /* This is the main function in rtl visualization mechanism. It
754 accepts an rtx and tries to recognize it as an insn, then prints it
755 properly in human readable form, resembling assembler mnemonics.
756 For every insn it prints its UID and BB the insn belongs too.
757 (Probably the last "option" should be extended somehow, since it
758 depends now on sched.c inner variables ...) */
761 print_insn (buf
, x
, verbose
)
769 switch (GET_CODE (x
))
772 print_pattern (t
, PATTERN (x
), verbose
);
774 sprintf (buf
, "%s: %s", (*current_sched_info
->print_insn
) (x
, 1),
777 sprintf (buf
, "%-4d %s", INSN_UID (x
), t
);
780 print_pattern (t
, PATTERN (x
), verbose
);
782 sprintf (buf
, "%s: jump %s", (*current_sched_info
->print_insn
) (x
, 1),
785 sprintf (buf
, "%-4d %s", INSN_UID (x
), t
);
789 if (GET_CODE (x
) == PARALLEL
)
791 x
= XVECEXP (x
, 0, 0);
792 print_pattern (t
, x
, verbose
);
795 strcpy (t
, "call <...>");
797 sprintf (buf
, "%s: %s", (*current_sched_info
->print_insn
) (x
, 1), t
);
799 sprintf (buf
, "%-4d %s", INSN_UID (insn
), t
);
802 sprintf (buf
, "L%d:", INSN_UID (x
));
805 sprintf (buf
, "i% 4d: barrier", INSN_UID (x
));
808 if (NOTE_LINE_NUMBER (x
) > 0)
809 sprintf (buf
, "%4d note \"%s\" %d", INSN_UID (x
),
810 NOTE_SOURCE_FILE (x
), NOTE_LINE_NUMBER (x
));
812 sprintf (buf
, "%4d %s", INSN_UID (x
),
813 GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (x
)));
818 sprintf (buf
, "Not an INSN at all\n");
822 sprintf (buf
, "i%-4d <What?>", INSN_UID (x
));
826 /* Print visualization debugging info. The scheduler using only DFA
827 description should never use the following function. */
830 print_block_visualization (s
)
836 fprintf (sched_dump
, "\n;; ==================== scheduling visualization %s \n", s
);
838 /* Print names of units. */
839 fprintf (sched_dump
, ";; %-8s", "clock");
840 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
841 if (function_units
[unit
].bitmask
& target_units
)
842 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
843 fprintf (sched_dump
, " %-33s", function_units
[unit
].name
);
844 fprintf (sched_dump
, " %-8s\n", "no-unit");
846 fprintf (sched_dump
, ";; %-8s", "=====");
847 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
848 if (function_units
[unit
].bitmask
& target_units
)
849 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
850 fprintf (sched_dump
, " %-33s", "==============================");
851 fprintf (sched_dump
, " %-8s\n", "=======");
853 /* Print insns in each cycle. */
854 fprintf (sched_dump
, "%s\n", visual_tbl
);
857 /* Print insns in the 'no_unit' column of visualization. */
860 visualize_no_unit (insn
)
863 if (n_vis_no_unit
< MAX_VISUAL_NO_UNIT
)
865 vis_no_unit
[n_vis_no_unit
] = insn
;
870 /* Print insns scheduled in clock, for visualization. */
873 visualize_scheduled_insns (clock
)
878 /* If no more room, split table into two. */
879 if (n_visual_lines
>= MAX_VISUAL_LINES
)
881 print_block_visualization ("(incomplete)");
882 init_block_visualization ();
887 sprintf (visual_tbl
+ strlen (visual_tbl
), ";; %-8d", clock
);
888 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
889 if (function_units
[unit
].bitmask
& target_units
)
890 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
892 int instance
= unit
+ i
* FUNCTION_UNITS_SIZE
;
893 rtx insn
= get_unit_last_insn (instance
);
895 /* Print insns that still keep the unit busy. */
897 && actual_hazard_this_instance (unit
, instance
, insn
, clock
, 0))
900 print_insn (str
, insn
, 0);
901 str
[INSN_LEN
] = '\0';
902 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-33s", str
);
905 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-33s", "------------------------------");
908 /* Print insns that are not assigned to any unit. */
909 for (i
= 0; i
< n_vis_no_unit
; i
++)
910 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-8d",
911 INSN_UID (vis_no_unit
[i
]));
914 sprintf (visual_tbl
+ strlen (visual_tbl
), "\n");
917 /* Print stalled cycles. */
920 visualize_stall_cycles (stalls
)
923 static const char *const prefix
= ";; ";
924 const char *suffix
= "\n";
927 /* If no more room, split table into two. */
928 if (n_visual_lines
>= MAX_VISUAL_LINES
)
930 print_block_visualization ("(incomplete)");
931 init_block_visualization ();
936 p
= visual_tbl
+ strlen (visual_tbl
);
938 p
+= strlen (prefix
);
940 if ((unsigned) stalls
>
941 visual_tbl_line_length
- strlen (prefix
) - strlen (suffix
))
944 stalls
= visual_tbl_line_length
- strlen (prefix
) - strlen (suffix
);
947 memset (p
, '.', stalls
);
953 /* Allocate data used for visualization during scheduling. */
958 visual_tbl
= xmalloc (get_visual_tbl_length ());
961 /* Free data used for visualization. */