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"
35 #ifdef INSN_SCHEDULING
36 /* target_units bitmask has 1 for each unit in the cpu. It should be
37 possible to compute this variable from the machine description.
38 But currently it is computed by examining the insn list. Since
39 this is only needed for visualization, it seems an acceptable
40 solution. (For understanding the mapping of bits to units, see
41 definition of function_units[] in "insn-attrtab.c".) */
43 static int target_units
= 0;
45 static char *safe_concat
PARAMS ((char *, char *, const char *));
46 static int get_visual_tbl_length
PARAMS ((void));
47 static void print_exp
PARAMS ((char *, rtx
, int));
48 static void print_value
PARAMS ((char *, rtx
, int));
49 static void print_pattern
PARAMS ((char *, rtx
, int));
50 static void print_insn
PARAMS ((char *, rtx
, int));
52 /* Print names of units on which insn can/should execute, for debugging. */
55 insn_print_units (insn
)
59 int unit
= insn_unit (insn
);
62 fprintf (sched_dump
, "none");
64 fprintf (sched_dump
, "%s", function_units
[unit
].name
);
67 fprintf (sched_dump
, "[");
68 for (i
= 0, unit
= ~unit
; unit
; i
++, unit
>>= 1)
71 fprintf (sched_dump
, "%s", function_units
[i
].name
);
73 fprintf (sched_dump
, " ");
75 fprintf (sched_dump
, "]");
79 /* MAX_VISUAL_LINES is the maximum number of lines in visualization table
80 of a basic block. If more lines are needed, table is splitted to two.
81 n_visual_lines is the number of lines printed so far for a block.
82 visual_tbl contains the block visualization info.
83 vis_no_unit holds insns in a cycle that are not mapped to any unit. */
84 #define MAX_VISUAL_LINES 100
87 static unsigned visual_tbl_line_length
;
90 #define MAX_VISUAL_NO_UNIT 20
91 rtx vis_no_unit
[MAX_VISUAL_NO_UNIT
];
93 /* Finds units that are in use in this function. Required only
102 for (insn
= get_last_insn (); insn
; insn
= PREV_INSN (insn
))
107 unit
= insn_unit (insn
);
110 target_units
|= ~unit
;
112 target_units
|= (1 << unit
);
116 /* Return the length of the visualization table. */
119 get_visual_tbl_length ()
125 /* Compute length of one field in line. */
126 s
= (char *) alloca (INSN_LEN
+ 6);
127 sprintf (s
, " %33s", "uname");
130 /* Compute length of one line. */
133 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
134 if (function_units
[unit
].bitmask
& target_units
)
135 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
138 n
+= strlen ("\n") + 2;
140 visual_tbl_line_length
= n
;
142 /* Compute length of visualization string. */
143 return (MAX_VISUAL_LINES
* n
);
146 /* Init block visualization debugging info. */
149 init_block_visualization ()
151 strcpy (visual_tbl
, "");
159 safe_concat (buf
, cur
, str
)
164 char *end
= buf
+ BUF_LEN
- 2; /* Leave room for null. */
173 while (cur
< end
&& (c
= *str
++) != '\0')
180 /* This recognizes rtx, I classified as expressions. These are always
181 represent some action on values or results of other expression, that
182 may be stored in objects representing values. */
185 print_exp (buf
, x
, verbose
)
193 const char *fun
= (char *) 0;
198 for (i
= 0; i
< 4; i
++)
204 switch (GET_CODE (x
))
208 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
209 && INTVAL (XEXP (x
, 1)) < 0)
212 op
[1] = GEN_INT (-INTVAL (XEXP (x
, 1)));
392 fun
= (verbose
) ? "sign_extract" : "sxt";
398 fun
= (verbose
) ? "zero_extract" : "zxt";
404 fun
= (verbose
) ? "sign_extend" : "sxn";
408 fun
= (verbose
) ? "zero_extend" : "zxn";
412 fun
= (verbose
) ? "float_extend" : "fxn";
416 fun
= (verbose
) ? "trunc" : "trn";
420 fun
= (verbose
) ? "float_trunc" : "ftr";
424 fun
= (verbose
) ? "float" : "flt";
428 fun
= (verbose
) ? "uns_float" : "ufl";
436 fun
= (verbose
) ? "uns_fix" : "ufx";
475 op
[0] = TRAP_CONDITION (x
);
484 case UNSPEC_VOLATILE
:
486 cur
= safe_concat (buf
, cur
, "unspec");
487 if (GET_CODE (x
) == UNSPEC_VOLATILE
)
488 cur
= safe_concat (buf
, cur
, "/v");
489 cur
= safe_concat (buf
, cur
, "[");
491 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
493 print_pattern (tmp
, XVECEXP (x
, 0, i
), verbose
);
494 cur
= safe_concat (buf
, cur
, sep
);
495 cur
= safe_concat (buf
, cur
, tmp
);
498 cur
= safe_concat (buf
, cur
, "] ");
499 sprintf (tmp
, "%d", XINT (x
, 1));
500 cur
= safe_concat (buf
, cur
, tmp
);
504 /* If (verbose) debug_rtx (x); */
505 st
[0] = GET_RTX_NAME (GET_CODE (x
));
509 /* Print this as a function? */
512 cur
= safe_concat (buf
, cur
, fun
);
513 cur
= safe_concat (buf
, cur
, "(");
516 for (i
= 0; i
< 4; i
++)
519 cur
= safe_concat (buf
, cur
, st
[i
]);
524 cur
= safe_concat (buf
, cur
, ",");
526 print_value (tmp
, op
[i
], verbose
);
527 cur
= safe_concat (buf
, cur
, tmp
);
532 cur
= safe_concat (buf
, cur
, ")");
535 /* Prints rtxes, I customly classified as values. They're constants,
536 registers, labels, symbols and memory accesses. */
539 print_value (buf
, x
, verbose
)
547 switch (GET_CODE (x
))
550 sprintf (t
, HOST_WIDE_INT_PRINT_HEX
, INTVAL (x
));
551 cur
= safe_concat (buf
, cur
, t
);
554 sprintf (t
, "<0x%lx,0x%lx>", (long) XWINT (x
, 2), (long) XWINT (x
, 3));
555 cur
= safe_concat (buf
, cur
, t
);
558 cur
= safe_concat (buf
, cur
, "\"");
559 cur
= safe_concat (buf
, cur
, XSTR (x
, 0));
560 cur
= safe_concat (buf
, cur
, "\"");
563 cur
= safe_concat (buf
, cur
, "`");
564 cur
= safe_concat (buf
, cur
, XSTR (x
, 0));
565 cur
= safe_concat (buf
, cur
, "'");
568 sprintf (t
, "L%d", INSN_UID (XEXP (x
, 0)));
569 cur
= safe_concat (buf
, cur
, t
);
572 print_value (t
, XEXP (x
, 0), verbose
);
573 cur
= safe_concat (buf
, cur
, "const(");
574 cur
= safe_concat (buf
, cur
, t
);
575 cur
= safe_concat (buf
, cur
, ")");
578 print_value (t
, XEXP (x
, 0), verbose
);
579 cur
= safe_concat (buf
, cur
, "high(");
580 cur
= safe_concat (buf
, cur
, t
);
581 cur
= safe_concat (buf
, cur
, ")");
584 if (REGNO (x
) < FIRST_PSEUDO_REGISTER
)
586 int c
= reg_names
[REGNO (x
)][0];
588 cur
= safe_concat (buf
, cur
, "%");
590 cur
= safe_concat (buf
, cur
, reg_names
[REGNO (x
)]);
594 sprintf (t
, "r%d", REGNO (x
));
595 cur
= safe_concat (buf
, cur
, t
);
599 print_value (t
, SUBREG_REG (x
), verbose
);
600 cur
= safe_concat (buf
, cur
, t
);
601 sprintf (t
, "#%d", SUBREG_BYTE (x
));
602 cur
= safe_concat (buf
, cur
, t
);
605 cur
= safe_concat (buf
, cur
, "scratch");
608 cur
= safe_concat (buf
, cur
, "cc0");
611 cur
= safe_concat (buf
, cur
, "pc");
614 print_value (t
, XEXP (x
, 0), verbose
);
615 cur
= safe_concat (buf
, cur
, "[");
616 cur
= safe_concat (buf
, cur
, t
);
617 cur
= safe_concat (buf
, cur
, "]");
620 print_exp (t
, x
, verbose
);
621 cur
= safe_concat (buf
, cur
, t
);
626 /* The next step in insn detalization, its pattern recognition. */
629 print_pattern (buf
, x
, verbose
)
634 char t1
[BUF_LEN
], t2
[BUF_LEN
], t3
[BUF_LEN
];
636 switch (GET_CODE (x
))
639 print_value (t1
, SET_DEST (x
), verbose
);
640 print_value (t2
, SET_SRC (x
), verbose
);
641 sprintf (buf
, "%s=%s", t1
, t2
);
644 sprintf (buf
, "return");
647 print_exp (buf
, x
, verbose
);
650 print_value (t1
, XEXP (x
, 0), verbose
);
651 sprintf (buf
, "clobber %s", t1
);
654 print_value (t1
, XEXP (x
, 0), verbose
);
655 sprintf (buf
, "use %s", t1
);
658 if (GET_CODE (COND_EXEC_TEST (x
)) == NE
659 && XEXP (COND_EXEC_TEST (x
), 1) == const0_rtx
)
660 print_value (t1
, XEXP (COND_EXEC_TEST (x
), 0), verbose
);
661 else if (GET_CODE (COND_EXEC_TEST (x
)) == EQ
662 && XEXP (COND_EXEC_TEST (x
), 1) == const0_rtx
)
665 print_value (t1
+ 1, XEXP (COND_EXEC_TEST (x
), 0), verbose
);
668 print_value (t1
, COND_EXEC_TEST (x
), verbose
);
669 print_pattern (t2
, COND_EXEC_CODE (x
), verbose
);
670 sprintf (buf
, "(%s) %s", t1
, t2
);
677 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
679 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
680 sprintf (t3
, "%s%s;", t1
, t2
);
683 sprintf (buf
, "%s}", t1
);
691 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
693 print_insn (t2
, XVECEXP (x
, 0, i
), verbose
);
694 sprintf (t3
, "%s%s;", t1
, t2
);
697 sprintf (buf
, "%s%%}", t1
);
701 sprintf (buf
, "asm {%s}", XSTR (x
, 0));
706 print_value (buf
, XEXP (x
, 0), verbose
);
709 print_value (t1
, TRAP_CONDITION (x
), verbose
);
710 sprintf (buf
, "trap_if %s", t1
);
716 sprintf (t1
, "unspec{");
717 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
719 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
720 sprintf (t3
, "%s%s;", t1
, t2
);
723 sprintf (buf
, "%s}", t1
);
726 case UNSPEC_VOLATILE
:
730 sprintf (t1
, "unspec/v{");
731 for (i
= 0; i
< XVECLEN (x
, 0); i
++)
733 print_pattern (t2
, XVECEXP (x
, 0, i
), verbose
);
734 sprintf (t3
, "%s%s;", t1
, t2
);
737 sprintf (buf
, "%s}", t1
);
741 print_value (buf
, x
, verbose
);
743 } /* print_pattern */
745 /* This is the main function in rtl visualization mechanism. It
746 accepts an rtx and tries to recognize it as an insn, then prints it
747 properly in human readable form, resembling assembler mnemonics.
748 For every insn it prints its UID and BB the insn belongs too.
749 (Probably the last "option" should be extended somehow, since it
750 depends now on sched.c inner variables ...) */
753 print_insn (buf
, x
, verbose
)
761 switch (GET_CODE (x
))
764 print_pattern (t
, PATTERN (x
), verbose
);
766 sprintf (buf
, "%s: %s", (*current_sched_info
->print_insn
) (x
, 1),
769 sprintf (buf
, "%-4d %s", INSN_UID (x
), t
);
772 print_pattern (t
, PATTERN (x
), verbose
);
774 sprintf (buf
, "%s: jump %s", (*current_sched_info
->print_insn
) (x
, 1),
777 sprintf (buf
, "%-4d %s", INSN_UID (x
), t
);
781 if (GET_CODE (x
) == PARALLEL
)
783 x
= XVECEXP (x
, 0, 0);
784 print_pattern (t
, x
, verbose
);
787 strcpy (t
, "call <...>");
789 sprintf (buf
, "%s: %s", (*current_sched_info
->print_insn
) (x
, 1), t
);
791 sprintf (buf
, "%-4d %s", INSN_UID (insn
), t
);
794 sprintf (buf
, "L%d:", INSN_UID (x
));
797 sprintf (buf
, "i% 4d: barrier", INSN_UID (x
));
800 if (NOTE_LINE_NUMBER (x
) > 0)
801 sprintf (buf
, "%4d note \"%s\" %d", INSN_UID (x
),
802 NOTE_SOURCE_FILE (x
), NOTE_LINE_NUMBER (x
));
804 sprintf (buf
, "%4d %s", INSN_UID (x
),
805 GET_NOTE_INSN_NAME (NOTE_LINE_NUMBER (x
)));
810 sprintf (buf
, "Not an INSN at all\n");
814 sprintf (buf
, "i%-4d <What?>", INSN_UID (x
));
818 /* Print visualization debugging info. */
821 print_block_visualization (s
)
827 fprintf (sched_dump
, "\n;; ==================== scheduling visualization %s \n", s
);
829 /* Print names of units. */
830 fprintf (sched_dump
, ";; %-8s", "clock");
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", function_units
[unit
].name
);
835 fprintf (sched_dump
, " %-8s\n", "no-unit");
837 fprintf (sched_dump
, ";; %-8s", "=====");
838 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
839 if (function_units
[unit
].bitmask
& target_units
)
840 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
841 fprintf (sched_dump
, " %-33s", "==============================");
842 fprintf (sched_dump
, " %-8s\n", "=======");
844 /* Print insns in each cycle. */
845 fprintf (sched_dump
, "%s\n", visual_tbl
);
848 /* Print insns in the 'no_unit' column of visualization. */
851 visualize_no_unit (insn
)
854 if (n_vis_no_unit
< MAX_VISUAL_NO_UNIT
)
856 vis_no_unit
[n_vis_no_unit
] = insn
;
861 /* Print insns scheduled in clock, for visualization. */
864 visualize_scheduled_insns (clock
)
869 /* If no more room, split table into two. */
870 if (n_visual_lines
>= MAX_VISUAL_LINES
)
872 print_block_visualization ("(incomplete)");
873 init_block_visualization ();
878 sprintf (visual_tbl
+ strlen (visual_tbl
), ";; %-8d", clock
);
879 for (unit
= 0; unit
< FUNCTION_UNITS_SIZE
; unit
++)
880 if (function_units
[unit
].bitmask
& target_units
)
881 for (i
= 0; i
< function_units
[unit
].multiplicity
; i
++)
883 int instance
= unit
+ i
* FUNCTION_UNITS_SIZE
;
884 rtx insn
= get_unit_last_insn (instance
);
886 /* Print insns that still keep the unit busy. */
888 && actual_hazard_this_instance (unit
, instance
, insn
, clock
, 0))
891 print_insn (str
, insn
, 0);
892 str
[INSN_LEN
] = '\0';
893 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-33s", str
);
896 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-33s", "------------------------------");
899 /* Print insns that are not assigned to any unit. */
900 for (i
= 0; i
< n_vis_no_unit
; i
++)
901 sprintf (visual_tbl
+ strlen (visual_tbl
), " %-8d",
902 INSN_UID (vis_no_unit
[i
]));
905 sprintf (visual_tbl
+ strlen (visual_tbl
), "\n");
908 /* Print stalled cycles. */
911 visualize_stall_cycles (stalls
)
914 static const char *const prefix
= ";; ";
915 const char *suffix
= "\n";
918 /* If no more room, split table into two. */
919 if (n_visual_lines
>= MAX_VISUAL_LINES
)
921 print_block_visualization ("(incomplete)");
922 init_block_visualization ();
927 p
= visual_tbl
+ strlen (visual_tbl
);
929 p
+= strlen (prefix
);
931 if ((unsigned) stalls
>
932 visual_tbl_line_length
- strlen (prefix
) - strlen (suffix
))
935 stalls
= visual_tbl_line_length
- strlen (prefix
) - strlen (suffix
);
938 memset (p
, '.', stalls
);
944 /* Allocate data used for visualization during scheduling. */
949 visual_tbl
= xmalloc (get_visual_tbl_length ());
952 /* Free data used for visualization. */