1 /* Subroutines for insn-output.c for Matsushita MN10300 series
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Jeff Law (law@cygnus.com).
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "hard-reg-set.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-flags.h"
32 #include "insn-attr.h"
39 /* Global registers known to hold the value zero.
41 Normally we'd depend on CSE and combine to put zero into a
42 register and re-use it.
44 However, on the mn10x00 processors we implicitly use the constant
45 zero in tst instructions, so we might be able to do better by
46 loading the value into a register in the prologue, then re-useing
47 that register throughout the function.
49 We could perform similar optimizations for other constants, but with
50 gcse due soon, it doesn't seem worth the effort.
52 These variables hold a rtx for a register known to hold the value
53 zero throughout the entire function, or NULL if no register of
54 the appropriate class has such a value throughout the life of the
63 fprintf (file
, "#\tGCC For the Matsushita MN10300\n");
65 fprintf (file
, "# -O%d\n", optimize
);
67 fprintf (file
, "\n\n");
68 output_file_directive (file
, main_input_filename
);
72 /* Print operand X using operand code CODE to assembly language output file
76 print_operand (file
, x
, code
)
85 /* These are normal and reversed branches. */
86 switch (code
== 'b' ? GET_CODE (x
) : reverse_condition (GET_CODE (x
)))
101 fprintf (file
, "le");
104 fprintf (file
, "lt");
107 fprintf (file
, "cc");
110 fprintf (file
, "hi");
113 fprintf (file
, "ls");
116 fprintf (file
, "cs");
123 /* This is used for the operand to a call instruction;
124 if it's a REG, enclose it in parens, else output
125 the operand normally. */
126 if (GET_CODE (x
) == REG
)
129 print_operand (file
, x
, 0);
133 print_operand (file
, x
, 0);
136 /* These are the least significant word in a 64bit value. */
138 switch (GET_CODE (x
))
142 output_address (XEXP (x
, 0));
147 fprintf (file
, "%s", reg_names
[REGNO (x
)]);
152 reg_names
[REGNO (SUBREG_REG (x
)) + SUBREG_WORD (x
)]);
160 switch (GET_MODE (x
))
163 REAL_VALUE_FROM_CONST_DOUBLE (rv
, x
);
164 REAL_VALUE_TO_TARGET_DOUBLE (rv
, val
);
165 print_operand_address (file
, GEN_INT (val
[0]));
168 REAL_VALUE_FROM_CONST_DOUBLE (rv
, x
);
169 REAL_VALUE_TO_TARGET_SINGLE (rv
, val
[0]);
170 print_operand_address (file
, GEN_INT (val
[0]));
174 print_operand_address (file
,
175 GEN_INT (CONST_DOUBLE_LOW (x
)));
182 print_operand_address (file
, x
);
190 /* Similarly, but for the most significant word. */
192 switch (GET_CODE (x
))
196 x
= adj_offsettable_operand (x
, 4);
197 output_address (XEXP (x
, 0));
202 fprintf (file
, "%s", reg_names
[REGNO (x
) + 1]);
207 reg_names
[REGNO (SUBREG_REG (x
)) + SUBREG_WORD (x
)] + 1);
215 switch (GET_MODE (x
))
218 REAL_VALUE_FROM_CONST_DOUBLE (rv
, x
);
219 REAL_VALUE_TO_TARGET_DOUBLE (rv
, val
);
220 print_operand_address (file
, GEN_INT (val
[1]));
226 print_operand_address (file
,
227 GEN_INT (CONST_DOUBLE_HIGH (x
)));
235 print_operand_address (file
, GEN_INT (-1));
237 print_operand_address (file
, GEN_INT (0));
246 if (GET_CODE (XEXP (x
, 0)) == REG
)
247 output_address (gen_rtx (PLUS
, SImode
, XEXP (x
, 0), GEN_INT (0)));
249 output_address (XEXP (x
, 0));
254 output_address (GEN_INT ((~INTVAL (x
)) & 0xff));
258 switch (GET_CODE (x
))
262 output_address (XEXP (x
, 0));
271 fprintf (file
, "%s", reg_names
[REGNO (x
)]);
276 reg_names
[REGNO (SUBREG_REG (x
)) + SUBREG_WORD (x
)]);
279 /* This will only be single precision.... */
285 REAL_VALUE_FROM_CONST_DOUBLE (rv
, x
);
286 REAL_VALUE_TO_TARGET_SINGLE (rv
, val
);
287 print_operand_address (file
, GEN_INT (val
));
296 print_operand_address (file
, x
);
305 /* Output assembly language output for the address ADDR to FILE. */
308 print_operand_address (file
, addr
)
312 switch (GET_CODE (addr
))
315 if (addr
== stack_pointer_rtx
)
316 print_operand_address (file
, gen_rtx (PLUS
, SImode
,
320 print_operand (file
, addr
, 0);
325 if (REG_P (XEXP (addr
, 0))
326 && REG_OK_FOR_BASE_P (XEXP (addr
, 0)))
327 base
= XEXP (addr
, 0), index
= XEXP (addr
, 1);
328 else if (REG_P (XEXP (addr
, 1))
329 && REG_OK_FOR_BASE_P (XEXP (addr
, 1)))
330 base
= XEXP (addr
, 1), index
= XEXP (addr
, 0);
333 print_operand (file
, index
, 0);
335 print_operand (file
, base
, 0);;
339 output_addr_const (file
, addr
);
342 output_addr_const (file
, addr
);
348 can_use_return_insn ()
350 /* size includes the fixed stack space needed for function calls. */
351 int size
= get_frame_size () + current_function_outgoing_args_size
;
353 /* And space for the return pointer. */
354 size
+= current_function_outgoing_args_size
? 4 : 0;
356 return (reload_completed
358 && !regs_ever_live
[2]
359 && !regs_ever_live
[3]
360 && !regs_ever_live
[6]
361 && !regs_ever_live
[7]
362 && !frame_pointer_needed
);
365 /* Count the number of tst insns which compare a data or address
366 register with zero. */
368 count_tst_insns (dreg_countp
, areg_countp
)
374 /* Assume no tst insns exist. */
378 /* If not optimizing, then quit now. */
382 /* Walk through all the insns. */
383 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
387 /* Ignore anything that is not a normal INSN. */
388 if (GET_CODE (insn
) != INSN
)
391 /* Ignore anything that isn't a SET. */
392 pat
= PATTERN (insn
);
393 if (GET_CODE (pat
) != SET
)
396 /* Check for a tst insn. */
397 if (SET_DEST (pat
) == cc0_rtx
398 && GET_CODE (SET_SRC (pat
)) == REG
)
400 if (REGNO_REG_CLASS (REGNO (SET_SRC (pat
))) == DATA_REGS
)
403 if (REGNO_REG_CLASS (REGNO (SET_SRC (pat
))) == ADDRESS_REGS
)
407 /* Setting an address register to zero can also be optimized,
408 so count it just like a tst insn. */
409 if (GET_CODE (SET_DEST (pat
)) == REG
410 && GET_CODE (SET_SRC (pat
)) == CONST_INT
411 && INTVAL (SET_SRC (pat
)) == 0
412 && REGNO_REG_CLASS (REGNO (SET_DEST (pat
))) == ADDRESS_REGS
)
422 /* We need to end the current sequence so that count_tst_insns can
423 look at all the insns in this function. Normally this would be
424 unsafe, but it's OK in the prologue/epilogue expanders. */
427 /* Determine if it is profitable to put the value zero into a register
428 for the entire function. If so, set ZERO_DREG and ZERO_AREG. */
429 if (regs_ever_live
[2] || regs_ever_live
[3]
430 || regs_ever_live
[6] || regs_ever_live
[7]
431 || frame_pointer_needed
)
433 int dreg_count
, areg_count
;
435 /* Get a count of the number of tst insns which use address and
437 count_tst_insns (&dreg_count
, &areg_count
);
439 /* If there's more than one tst insn using a data register, then
440 this optimization is a win. */
442 && (!regs_ever_live
[2] || !regs_ever_live
[3]))
444 if (!regs_ever_live
[2])
446 regs_ever_live
[2] = 1;
447 zero_dreg
= gen_rtx (REG
, SImode
, 2);
451 regs_ever_live
[3] = 1;
452 zero_dreg
= gen_rtx (REG
, SImode
, 3);
456 zero_dreg
= NULL_RTX
;
458 /* If there's more than two tst insns using an address register,
459 then this optimization is a win. */
461 && (!regs_ever_live
[6] || !regs_ever_live
[7]))
463 if (!regs_ever_live
[6])
465 regs_ever_live
[6] = 1;
466 zero_areg
= gen_rtx (REG
, SImode
, 6);
470 regs_ever_live
[7] = 1;
471 zero_areg
= gen_rtx (REG
, SImode
, 7);
475 zero_areg
= NULL_RTX
;
479 zero_dreg
= NULL_RTX
;
480 zero_areg
= NULL_RTX
;
483 /* Start a new sequence. */
486 /* SIZE includes the fixed stack space needed for function calls. */
487 size
= get_frame_size () + current_function_outgoing_args_size
;
488 size
+= (current_function_outgoing_args_size
? 4 : 0);
490 /* If this is an old-style varargs function, then its arguments
491 need to be flushed back to the stack. */
492 if (current_function_varargs
)
494 emit_move_insn (gen_rtx (MEM
, SImode
,
495 gen_rtx (PLUS
, Pmode
, stack_pointer_rtx
,
497 gen_rtx (REG
, SImode
, 0));
498 emit_move_insn (gen_rtx (MEM
, SImode
,
499 gen_rtx (PLUS
, Pmode
, stack_pointer_rtx
,
501 gen_rtx (REG
, SImode
, 1));
504 /* And now store all the registers onto the stack with a
505 single two byte instruction. */
506 if (regs_ever_live
[2] || regs_ever_live
[3]
507 || regs_ever_live
[6] || regs_ever_live
[7]
508 || frame_pointer_needed
)
509 emit_insn (gen_store_movm ());
511 /* Now put the frame pointer into the frame pointer register. */
512 if (frame_pointer_needed
)
513 emit_move_insn (frame_pointer_rtx
, stack_pointer_rtx
);
515 /* Allocate stack for this frame. */
517 emit_insn (gen_addsi3 (stack_pointer_rtx
,
521 /* Load zeros into registers as needed. */
523 emit_move_insn (zero_dreg
, const0_rtx
);
526 emit_move_insn (zero_areg
, const0_rtx
);
534 /* SIZE includes the fixed stack space needed for function calls. */
535 size
= get_frame_size () + current_function_outgoing_args_size
;
536 size
+= (current_function_outgoing_args_size
? 4 : 0);
538 /* Cut back the stack. */
539 if (frame_pointer_needed
)
541 emit_move_insn (stack_pointer_rtx
, frame_pointer_rtx
);
544 else if ((regs_ever_live
[2] || regs_ever_live
[3]
545 || regs_ever_live
[6] || regs_ever_live
[7])
548 emit_insn (gen_addsi3 (stack_pointer_rtx
,
554 /* For simplicity, we just movm all the callee saved registers to
555 the stack with one instruction.
557 ?!? Only save registers which are actually used. Reduces
558 stack requireents and is faster. */
559 if (regs_ever_live
[2] || regs_ever_live
[3]
560 || regs_ever_live
[6] || regs_ever_live
[7]
561 || frame_pointer_needed
)
562 emit_jump_insn (gen_return_internal_regs (GEN_INT (size
)));
567 emit_insn (gen_addsi3 (stack_pointer_rtx
,
570 emit_jump_insn (gen_return_internal ());
574 emit_jump_insn (gen_return ());
579 /* Update the condition code from the insn. */
582 notice_update_cc (body
, insn
)
586 switch (get_attr_cc (insn
))
589 /* Insn does not affect CC at all. */
593 /* Insn does not change CC, but the 0'th operand has been changed. */
594 if (cc_status
.value1
!= 0
595 && reg_overlap_mentioned_p (recog_operand
[0], cc_status
.value1
))
596 cc_status
.value1
= 0;
600 /* Insn sets the Z,N flags of CC to recog_operand[0].
603 cc_status
.flags
|= CC_NO_CARRY
| CC_OVERFLOW_UNUSABLE
;
604 cc_status
.value1
= recog_operand
[0];
608 /* Insn sets the Z,N,V flags of CC to recog_operand[0].
611 cc_status
.flags
|= CC_NO_CARRY
;
612 cc_status
.value1
= recog_operand
[0];
616 /* The insn is a compare instruction. */
618 cc_status
.value1
= SET_SRC (body
);
622 /* The insn is a compare instruction. */
624 cc_status
.value1
= SET_SRC (body
);
625 cc_status
.flags
|= CC_INVERTED
;
629 /* Insn doesn't leave CC in a usable state. */
638 /* Return true if OP is a valid call operand. */
641 call_address_operand (op
, mode
)
643 enum machine_mode mode
;
645 return (GET_CODE (op
) == SYMBOL_REF
|| GET_CODE (op
) == REG
);
648 /* What (if any) secondary registers are needed to move IN with mode
649 MODE into a register from in register class CLASS.
651 We might be able to simplify this. */
653 secondary_reload_class (class, mode
, in
)
654 enum reg_class
class;
655 enum machine_mode mode
;
660 /* Memory loads less than a full word wide can't have an
661 address or stack pointer destination. They must use
662 a data register as an intermediate register. */
663 if (GET_CODE (in
) == MEM
664 && (mode
== QImode
|| mode
== HImode
)
665 && (class == ADDRESS_REGS
|| class == SP_REGS
))
668 /* We can't directly load sp + const_int into a data register;
669 we must use an address register as an intermediate. */
671 && class != ADDRESS_REGS
672 && class != SP_OR_ADDRESS_REGS
673 && (in
== stack_pointer_rtx
674 || (GET_CODE (in
) == PLUS
675 && (XEXP (in
, 0) == stack_pointer_rtx
676 || XEXP (in
, 1) == stack_pointer_rtx
))))
679 if (GET_CODE (in
) == PLUS
680 && (XEXP (in
, 0) == stack_pointer_rtx
681 || XEXP (in
, 1) == stack_pointer_rtx
))
685 /* Otherwise assume no secondary reloads are needed. */
690 initial_offset (from
, to
)
693 /* The difference between the argument pointer and the frame pointer
694 is the size of the callee register save area. */
695 if (from
== ARG_POINTER_REGNUM
&& to
== FRAME_POINTER_REGNUM
)
697 if (regs_ever_live
[2] || regs_ever_live
[3]
698 || regs_ever_live
[6] || regs_ever_live
[7]
699 || frame_pointer_needed
)
705 /* The difference between the argument pointer and the stack pointer is
706 the sum of the size of this function's frame, the callee register save
707 area, and the fixed stack space needed for function calls (if any). */
708 if (from
== ARG_POINTER_REGNUM
&& to
== STACK_POINTER_REGNUM
)
710 if (regs_ever_live
[2] || regs_ever_live
[3]
711 || regs_ever_live
[6] || regs_ever_live
[7]
712 || frame_pointer_needed
)
713 return (get_frame_size () + 16
714 + (current_function_outgoing_args_size
715 ? current_function_outgoing_args_size
+ 4 : 0));
717 return (get_frame_size ()
718 + (current_function_outgoing_args_size
719 ? current_function_outgoing_args_size
+ 4 : 0));
722 /* The difference between the frame pointer and stack pointer is the sum
723 of the size of this function's frame and the fixed stack space needed
724 for function calls (if any). */
725 if (from
== FRAME_POINTER_REGNUM
&& to
== STACK_POINTER_REGNUM
)
726 return (get_frame_size ()
727 + (current_function_outgoing_args_size
728 ? current_function_outgoing_args_size
+ 4 : 0));
733 /* Flush the argument registers to the stack for a stdarg function;
734 return the new argument pointer. */
736 mn10300_builtin_saveregs (arglist
)
740 tree fntype
= TREE_TYPE (current_function_decl
);
741 int argadj
= ((!(TYPE_ARG_TYPES (fntype
) != 0
742 && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype
)))
744 ? UNITS_PER_WORD
: 0);
747 offset
= plus_constant (current_function_arg_offset_rtx
, argadj
);
749 offset
= current_function_arg_offset_rtx
;
751 emit_move_insn (gen_rtx (MEM
, SImode
, current_function_internal_arg_pointer
),
752 gen_rtx (REG
, SImode
, 0));
753 emit_move_insn (gen_rtx (MEM
, SImode
,
755 (current_function_internal_arg_pointer
, 4)),
756 gen_rtx (REG
, SImode
, 1));
757 return copy_to_reg (expand_binop (Pmode
, add_optab
,
758 current_function_internal_arg_pointer
,
759 offset
, 0, 0, OPTAB_LIB_WIDEN
));
762 /* Return an RTX to represent where a value with mode MODE will be returned
763 from a function. If the result is 0, the argument is pushed. */
766 function_arg (cum
, mode
, type
, named
)
767 CUMULATIVE_ARGS
*cum
;
768 enum machine_mode mode
;
775 /* We only support using 2 data registers as argument registers. */
778 /* Figure out the size of the object to be passed. */
780 size
= int_size_in_bytes (type
);
782 size
= GET_MODE_SIZE (mode
);
784 /* Figure out the alignment of the object to be passed. */
787 cum
->nbytes
= (cum
->nbytes
+ 3) & ~3;
789 /* Don't pass this arg via a register if all the argument registers
791 if (cum
->nbytes
> nregs
* UNITS_PER_WORD
)
794 /* Don't pass this arg via a register if it would be split between
795 registers and memory. */
796 if (type
== NULL_TREE
797 && cum
->nbytes
+ size
> nregs
* UNITS_PER_WORD
)
800 switch (cum
->nbytes
/ UNITS_PER_WORD
)
803 result
= gen_rtx (REG
, mode
, 0);
806 result
= gen_rtx (REG
, mode
, 1);
815 /* Return the number of registers to use for an argument passed partially
816 in registers and partially in memory. */
819 function_arg_partial_nregs (cum
, mode
, type
, named
)
820 CUMULATIVE_ARGS
*cum
;
821 enum machine_mode mode
;
827 /* We only support using 2 data registers as argument registers. */
830 /* Figure out the size of the object to be passed. */
832 size
= int_size_in_bytes (type
);
834 size
= GET_MODE_SIZE (mode
);
836 /* Figure out the alignment of the object to be passed. */
839 cum
->nbytes
= (cum
->nbytes
+ 3) & ~3;
841 /* Don't pass this arg via a register if all the argument registers
843 if (cum
->nbytes
> nregs
* UNITS_PER_WORD
)
846 if (cum
->nbytes
+ size
<= nregs
* UNITS_PER_WORD
)
849 /* Don't pass this arg via a register if it would be split between
850 registers and memory. */
851 if (type
== NULL_TREE
852 && cum
->nbytes
+ size
> nregs
* UNITS_PER_WORD
)
855 return (nregs
* UNITS_PER_WORD
- cum
->nbytes
) / UNITS_PER_WORD
;
858 /* Output a tst insn. */
860 output_tst (operand
, insn
)
866 /* If we have a data register which is known to be zero throughout
867 the function, then use it instead of doing a search. */
868 if (zero_dreg
&& REGNO_REG_CLASS (REGNO (operand
)) == DATA_REGS
)
871 xoperands
[0] = operand
;
872 xoperands
[1] = zero_dreg
;
874 output_asm_insn ("cmp %1,%0", xoperands
);
878 /* Similarly for address registers. */
879 if (zero_areg
&& REGNO_REG_CLASS (REGNO (operand
)) == ADDRESS_REGS
)
882 xoperands
[0] = operand
;
883 xoperands
[1] = zero_areg
;
885 output_asm_insn ("cmp %1,%0", xoperands
);
889 /* We can save a byte if we can find a register which has the value
891 temp
= PREV_INSN (insn
);
892 while (optimize
&& temp
)
896 /* We allow the search to go through call insns. We record
897 the fact that we've past a CALL_INSN and reject matches which
898 use call clobbered registers. */
899 if (GET_CODE (temp
) == CODE_LABEL
900 || GET_CODE (temp
) == JUMP_INSN
901 || GET_CODE (temp
) == BARRIER
)
904 if (GET_CODE (temp
) == CALL_INSN
)
907 if (GET_CODE (temp
) == NOTE
)
909 temp
= PREV_INSN (temp
);
913 /* It must be an insn, see if it is a simple set. */
914 set
= single_set (temp
);
917 temp
= PREV_INSN (temp
);
921 /* Are we setting a data register to zero (this does not win for
924 If it's a call clobbered register, have we past a call?
926 Make sure the register we find isn't the same as ourself;
927 the mn10300 can't encode that. */
928 if (REG_P (SET_DEST (set
))
929 && SET_SRC (set
) == CONST0_RTX (GET_MODE (SET_DEST (set
)))
930 && !reg_set_between_p (SET_DEST (set
), temp
, insn
)
931 && (REGNO_REG_CLASS (REGNO (SET_DEST (set
)))
932 == REGNO_REG_CLASS (REGNO (operand
)))
933 && REGNO (SET_DEST (set
)) != REGNO (operand
)
935 || !call_used_regs
[REGNO (SET_DEST (set
))]))
938 xoperands
[0] = operand
;
939 xoperands
[1] = SET_DEST (set
);
941 output_asm_insn ("cmp %1,%0", xoperands
);
944 temp
= PREV_INSN (temp
);
950 impossible_plus_operand (op
, mode
)
952 enum machine_mode mode
;
954 extern rtx
*reg_equiv_mem
;
957 if (GET_CODE (op
) != PLUS
)
960 if (XEXP (op
, 0) == stack_pointer_rtx
961 || XEXP (op
, 1) == stack_pointer_rtx
)