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"
33 #include "sched-int.h"
36 #ifdef INSN_SCHEDULING
37 /* target_units bitmask has 1 for each unit in the cpu. It should be
38 possible to compute this variable from the machine description.
39 But currently it is computed by examining the insn list. Since
40 this is only needed for visualization, it seems an acceptable
41 solution. (For understanding the mapping of bits to units, see
42 definition of function_units[] in "insn-attrtab.c".) The scheduler
43 using only DFA description should never use the following variable. */
45 static int target_units
= 0;
47 static char *safe_concat
PARAMS ((char *, char *, const char *));
48 static int get_visual_tbl_length
PARAMS ((void));
49 static void print_exp
PARAMS ((char *, rtx
, int));
50 static void print_value
PARAMS ((char *, rtx
, int));
51 static void print_pattern
PARAMS ((char *, rtx
, int));
53 /* Print names of units on which insn can/should execute, for debugging. */
56 insn_print_units (insn
)
60 int unit
= insn_unit (insn
);
63 fprintf (sched_dump
, "none");
65 fprintf (sched_dump
, "%s", function_units
[unit
].name
);
68 fprintf (sched_dump
, "[");
69 for (i
= 0, unit
= ~unit
; unit
; i
++, unit
>>= 1)
72 fprintf (sched_dump
, "%s", function_units
[i
].name
);
74 fprintf (sched_dump
, " ");
76 fprintf (sched_dump
, "]");
80 /* MAX_VISUAL_LINES is the maximum number of lines in visualization table
81 of a basic block. If more lines are needed, table is splitted to two.
82 n_visual_lines is the number of lines printed so far for a block.
83 visual_tbl contains the block visualization info.
84 vis_no_unit holds insns in a cycle that are not mapped to any unit. */
85 #define MAX_VISUAL_LINES 100
88 static unsigned visual_tbl_line_length
;
91 #define MAX_VISUAL_NO_UNIT 20
92 rtx vis_no_unit
[MAX_VISUAL_NO_UNIT
];
94 /* Finds units that are in use in this function. Required only
103 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
108 unit
= insn_unit (insn
);
111 target_units
|= ~unit
;
113 target_units
|= (1 << unit
);
117 /* Return the length of the visualization table. */
120 get_visual_tbl_length ()
126 if (targetm
.sched
.use_dfa_pipeline_interface
127 && (*targetm
.sched
.use_dfa_pipeline_interface
) ())
129 visual_tbl_line_length
= 1;
130 return 1; /* Can't return 0 because that will cause problems
134 /* Compute length of one field in line. */
135 s
= (char *) alloca (INSN_LEN
+ 6);
136 sprintf (s
, " %33s", "uname");
139 /* Compute length of one line. */
142 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
143 if (function_units
[unit
].bitmask
& target_units
)
144 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
147 n
+= strlen ("\n") + 2;
149 visual_tbl_line_length
= n
;
151 /* Compute length of visualization string. */
152 return (MAX_VISUAL_LINES
* n
);
155 /* Init block visualization debugging info. */
158 init_block_visualization ()
160 strcpy (visual_tbl
, "");
168 safe_concat (buf
, cur
, str
)
173 char *end
= buf
+ BUF_LEN
- 2; /* Leave room for null. */
182 while (cur
< end
&& (c
= *str
++) != '\0')
189 /* This recognizes rtx, I classified as expressions. These are always
190 represent some action on values or results of other expression, that
191 may be stored in objects representing values. */
194 print_exp (buf
, x
, verbose
)
202 const char *fun
= (char *) 0;
207 for (i
= 0; i
< 4; i
++)
213 switch (GET_CODE (x
))
217 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
218 && INTVAL (XEXP (x
, 1)) < 0)
221 op
[1] = GEN_INT (-INTVAL (XEXP (x
, 1)));
401 fun
= (verbose
) ? "sign_extract" : "sxt";
407 fun
= (verbose
) ? "zero_extract" : "zxt";
413 fun
= (verbose
) ? "sign_extend" : "sxn";
417 fun
= (verbose
) ? "zero_extend" : "zxn";
421 fun
= (verbose
) ? "float_extend" : "fxn";
425 fun
= (verbose
) ? "trunc" : "trn";
429 fun
= (verbose
) ? "float_trunc" : "ftr";
433 fun
= (verbose
) ? "float" : "flt";
437 fun
= (verbose
) ? "uns_float" : "ufl";
445 fun
= (verbose
) ? "uns_fix" : "ufx";
484 op
[0] = TRAP_CONDITION (x
);
493 case UNSPEC_VOLATILE
:
495 cur
= safe_concat (buf
, cur
, "unspec");
496 if (GET_CODE (x
) == UNSPEC_VOLATILE
)
497 cur
= safe_concat (buf
, cur
, "/v");
498 cur
= safe_concat (buf
, cur
, "[");
500 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
502 print_pattern (tmp
, XVECEXP (x
, 0, i
), verbose
);
503 cur
= safe_concat (buf
, cur
, sep
);
504 cur
= safe_concat (buf
, cur
, tmp
);
507 cur
= safe_concat (buf
, cur
, "] ");
508 sprintf (tmp
, "%d", XINT (x
, 1));
509 cur
= safe_concat (buf
, cur
, tmp
);
513 /* If (verbose) debug_rtx (x); */
514 st
[0] = GET_RTX_NAME (GET_CODE (x
));
518 /* Print this as a function? */
521 cur
= safe_concat (buf
, cur
, fun
);
522 cur
= safe_concat (buf
, cur
, "(");
525 for (i
= 0; i
< 4; i
++)
528 cur
= safe_concat (buf
, cur
, st
[i
]);
533 cur
= safe_concat (buf
, cur
, ",");
535 print_value (tmp
, op
[i
], verbose
);
536 cur
= safe_concat (buf
, cur
, tmp
);
541 cur
= safe_concat (buf
, cur
, ")");
544 /* Prints rtxes, I customly classified as values. They're constants,
545 registers, labels, symbols and memory accesses. */
548 print_value (buf
, x
, verbose
)
556 switch (GET_CODE (x
))
559 sprintf (t
, HOST_WIDE_INT_PRINT_HEX
, INTVAL (x
));
560 cur
= safe_concat (buf
, cur
, t
);
563 sprintf (t
, "<0x%lx,0x%lx>", (long) XWINT (x
, 2), (long) XWINT (x
, 3));
564 cur
= safe_concat (buf
, cur
, t
);
567 cur
= safe_concat (buf
, cur
, "\"");
568 cur
= safe_concat (buf
, cur
, XSTR (x
, 0));
569 cur
= safe_concat (buf
, cur
, "\"");
572 cur
= safe_concat (buf
, cur
, "`");
573 cur
= safe_concat (buf
, cur
, XSTR (x
, 0));
574 cur
= safe_concat (buf
, cur
, "'");
577 sprintf (t
, "L%d", INSN_UID (XEXP (x
, 0)));
578 cur
= safe_concat (buf
, cur
, t
);
581 print_value (t
, XEXP (x
, 0), verbose
);
582 cur
= safe_concat (buf
, cur
, "const(");
583 cur
= safe_concat (buf
, cur
, t
);
584 cur
= safe_concat (buf
, cur
, ")");
587 print_value (t
, XEXP (x
, 0), verbose
);
588 cur
= safe_concat (buf
, cur
, "high(");
589 cur
= safe_concat (buf
, cur
, t
);
590 cur
= safe_concat (buf
, cur
, ")");
593 if (REGNO (x
) < FIRST_PSEUDO_REGISTER
)
595 int c
= reg_names
[REGNO (x
)][0];
597 cur
= safe_concat (buf
, cur
, "%");
599 cur
= safe_concat (buf
, cur
, reg_names
[REGNO (x
)]);
603 sprintf (t
, "r%d", REGNO (x
));
604 cur
= safe_concat (buf
, cur
, t
);
608 print_value (t
, SUBREG_REG (x
), verbose
);
609 cur
= safe_concat (buf
, cur
, t
);
610 sprintf (t
, "#%d", SUBREG_BYTE (x
));
611 cur
= safe_concat (buf
, cur
, t
);
614 cur
= safe_concat (buf
, cur
, "scratch");
617 cur
= safe_concat (buf
, cur
, "cc0");
620 cur
= safe_concat (buf
, cur
, "pc");
623 print_value (t
, XEXP (x
, 0), verbose
);
624 cur
= safe_concat (buf
, cur
, "[");
625 cur
= safe_concat (buf
, cur
, t
);
626 cur
= safe_concat (buf
, cur
, "]");
629 print_exp (t
, x
, verbose
);
630 cur
= safe_concat (buf
, cur
, t
);
635 /* The next step in insn detalization, its pattern recognition. */
638 print_pattern (buf
, x
, verbose
)
643 char t1
[BUF_LEN
], t2
[BUF_LEN
], t3
[BUF_LEN
];
645 switch (GET_CODE (x
))
648 print_value (t1
, SET_DEST (x
), verbose
);
649 print_value (t2
, SET_SRC (x
), verbose
);
650 sprintf (buf
, "%s=%s", t1
, t2
);
653 sprintf (buf
, "return");
656 print_exp (buf
, x
, verbose
);
659 print_value (t1
, XEXP (x
, 0), verbose
);
660 sprintf (buf
, "clobber %s", t1
);
663 print_value (t1
, XEXP (x
, 0), verbose
);
664 sprintf (buf
, "use %s", t1
);
667 if (GET_CODE (COND_EXEC_TEST (x
)) == NE
668 && XEXP (COND_EXEC_TEST (x
), 1) == const0_rtx
)
669 print_value (t1
, XEXP (COND_EXEC_TEST (x
), 0), verbose
);
670 else if (GET_CODE (COND_EXEC_TEST (x
)) == EQ
671 && XEXP (COND_EXEC_TEST (x
), 1) == const0_rtx
)
674 print_value (t1
+ 1, XEXP (COND_EXEC_TEST (x
), 0), verbose
);
677 print_value (t1
, COND_EXEC_TEST (x
), verbose
);
678 print_pattern (t2
, COND_EXEC_CODE (x
), verbose
);
679 sprintf (buf
, "(%s) %s", t1
, t2
);
686 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
688 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
689 sprintf (t3
, "%s%s;", t1
, t2
);
692 sprintf (buf
, "%s}", t1
);
700 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
702 print_insn (t2
, XVECEXP (x
, 0, i
), verbose
);
703 sprintf (t3
, "%s%s;", t1
, t2
);
706 sprintf (buf
, "%s%%}", t1
);
710 sprintf (buf
, "asm {%s}", XSTR (x
, 0));
715 print_value (buf
, XEXP (x
, 0), verbose
);
718 print_value (t1
, TRAP_CONDITION (x
), verbose
);
719 sprintf (buf
, "trap_if %s", t1
);
725 sprintf (t1
, "unspec{");
726 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
728 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
729 sprintf (t3
, "%s%s;", t1
, t2
);
732 sprintf (buf
, "%s}", t1
);
735 case UNSPEC_VOLATILE
:
739 sprintf (t1
, "unspec/v{");
740 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
742 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
743 sprintf (t3
, "%s%s;", t1
, t2
);
746 sprintf (buf
, "%s}", t1
);
750 print_value (buf
, x
, verbose
);
752 } /* print_pattern */
754 /* This is the main function in rtl visualization mechanism. It
755 accepts an rtx and tries to recognize it as an insn, then prints it
756 properly in human readable form, resembling assembler mnemonics.
757 For every insn it prints its UID and BB the insn belongs too.
758 (Probably the last "option" should be extended somehow, since it
759 depends now on sched.c inner variables ...) */
762 print_insn (buf
, x
, verbose
)
770 switch (GET_CODE (x
))
773 print_pattern (t
, PATTERN (x
), verbose
);
775 sprintf (buf
, "%s: %s", (*current_sched_info
->print_insn
) (x
, 1),
778 sprintf (buf
, "%-4d %s", INSN_UID (x
), t
);
781 print_pattern (t
, PATTERN (x
), verbose
);
783 sprintf (buf
, "%s: jump %s", (*current_sched_info
->print_insn
) (x
, 1),
786 sprintf (buf
, "%-4d %s", INSN_UID (x
), t
);
790 if (GET_CODE (x
) == PARALLEL
)
792 x
= XVECEXP (x
, 0, 0);
793 print_pattern (t
, x
, verbose
);
796 strcpy (t
, "call <...>");
798 sprintf (buf
, "%s: %s", (*current_sched_info
->print_insn
) (x
, 1), t
);
800 sprintf (buf
, "%-4d %s", INSN_UID (insn
), t
);
803 sprintf (buf
, "L%d:", INSN_UID (x
));
806 sprintf (buf
, "i% 4d: barrier", INSN_UID (x
));
809 if (NOTE_LINE_NUMBER (x
) > 0)
810 sprintf (buf
, "%4d note \"%s\" %d", INSN_UID (x
),
811 NOTE_SOURCE_FILE (x
), NOTE_LINE_NUMBER (x
));
813 sprintf (buf
, "%4d %s", INSN_UID (x
),
814 GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (x
)));
819 sprintf (buf
, "Not an INSN at all\n");
823 sprintf (buf
, "i%-4d <What?>", INSN_UID (x
));
827 /* Print visualization debugging info. The scheduler using only DFA
828 description should never use the following function. */
831 print_block_visualization (s
)
837 fprintf (sched_dump
, "\n;; ==================== scheduling visualization %s \n", s
);
839 /* Print names of units. */
840 fprintf (sched_dump
, ";; %-8s", "clock");
841 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
842 if (function_units
[unit
].bitmask
& target_units
)
843 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
844 fprintf (sched_dump
, " %-33s", function_units
[unit
].name
);
845 fprintf (sched_dump
, " %-8s\n", "no-unit");
847 fprintf (sched_dump
, ";; %-8s", "=====");
848 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
849 if (function_units
[unit
].bitmask
& target_units
)
850 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
851 fprintf (sched_dump
, " %-33s", "==============================");
852 fprintf (sched_dump
, " %-8s\n", "=======");
854 /* Print insns in each cycle. */
855 fprintf (sched_dump
, "%s\n", visual_tbl
);
858 /* Print insns in the 'no_unit' column of visualization. */
861 visualize_no_unit (insn
)
864 if (n_vis_no_unit
< MAX_VISUAL_NO_UNIT
)
866 vis_no_unit
[n_vis_no_unit
] = insn
;
871 /* Print insns scheduled in clock, for visualization. */
874 visualize_scheduled_insns (clock
)
879 /* If no more room, split table into two. */
880 if (n_visual_lines
>= MAX_VISUAL_LINES
)
882 print_block_visualization ("(incomplete)");
883 init_block_visualization ();
888 sprintf (visual_tbl
+ strlen (visual_tbl
), ";; %-8d", clock
);
889 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
890 if (function_units
[unit
].bitmask
& target_units
)
891 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
893 int instance
= unit
+ i
* FUNCTION_UNITS_SIZE
;
894 rtx insn
= get_unit_last_insn (instance
);
896 /* Print insns that still keep the unit busy. */
898 && actual_hazard_this_instance (unit
, instance
, insn
, clock
, 0))
901 print_insn (str
, insn
, 0);
902 str
[INSN_LEN
] = '\0';
903 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-33s", str
);
906 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-33s", "------------------------------");
909 /* Print insns that are not assigned to any unit. */
910 for (i
= 0; i
< n_vis_no_unit
; i
++)
911 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-8d",
912 INSN_UID (vis_no_unit
[i
]));
915 sprintf (visual_tbl
+ strlen (visual_tbl
), "\n");
918 /* Print stalled cycles. */
921 visualize_stall_cycles (stalls
)
924 static const char *const prefix
= ";; ";
925 const char *suffix
= "\n";
928 /* If no more room, split table into two. */
929 if (n_visual_lines
>= MAX_VISUAL_LINES
)
931 print_block_visualization ("(incomplete)");
932 init_block_visualization ();
937 p
= visual_tbl
+ strlen (visual_tbl
);
939 p
+= strlen (prefix
);
941 if ((unsigned) stalls
>
942 visual_tbl_line_length
- strlen (prefix
) - strlen (suffix
))
945 stalls
= visual_tbl_line_length
- strlen (prefix
) - strlen (suffix
);
948 memset (p
, '.', stalls
);
954 /* Allocate data used for visualization during scheduling. */
959 visual_tbl
= xmalloc (get_visual_tbl_length ());
962 /* Free data used for visualization. */