1 /* Output routines for GCC for Hitachi Super-H.
2 Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Contributed by Steve Chamberlain (sac@cygnus.com).
22 Improved by Jim Wilson (wilson@cygnus.com). */
31 #include "insn-flags.h"
34 #include "hard-reg-set.h"
36 #include "insn-attr.h"
38 #define MSW (TARGET_LITTLE_ENDIAN ? 1 : 0)
39 #define LSW (TARGET_LITTLE_ENDIAN ? 0 : 1)
41 /* ??? The pragma interrupt support will not work for SH3. */
42 /* This is set by #pragma interrupt and #pragma trapa, and causes gcc to
43 output code for the next function appropriate for an interrupt handler. */
46 /* This is set by the trap_exit attribute for functions. It specifies
47 a trap number to be used in a trapa instruction at function exit
48 (instead of an rte instruction). */
51 /* This is used by the sp_switch attribute for functions. It specifies
52 a variable holding the address of the stack the interrupt function
53 should switch to/from at entry/exit. */
56 /* This is set by #pragma trapa, and is similar to the above, except that
57 the compiler doesn't emit code to preserve all registers. */
58 static int pragma_trapa
;
60 /* This is set by #pragma nosave_low_regs. This is useful on the SH3,
61 which has a separate set of low regs for User and Supervisor modes.
62 This should only be used for the lowest level of interrupts. Higher levels
63 of interrupts must save the registers in case they themselves are
65 int pragma_nosave_low_regs
;
67 /* This is used for communication between SETUP_INCOMING_VARARGS and
68 sh_expand_prologue. */
69 int current_function_anonymous_args
;
71 /* Global variables from toplev.c and final.c that are used within, but
72 not declared in any header file. */
73 extern char *version_string
;
74 extern int *insn_addresses
;
76 /* Global variables for machine-dependent things. */
78 /* Which cpu are we scheduling for. */
79 enum processor_type sh_cpu
;
81 /* Saved operands from the last compare to use when we generate an scc
87 /* Provides the class number of the smallest class containing
90 int regno_reg_class
[FIRST_PSEUDO_REGISTER
] =
92 R0_REGS
, GENERAL_REGS
, GENERAL_REGS
, GENERAL_REGS
,
93 GENERAL_REGS
, GENERAL_REGS
, GENERAL_REGS
, GENERAL_REGS
,
94 GENERAL_REGS
, GENERAL_REGS
, GENERAL_REGS
, GENERAL_REGS
,
95 GENERAL_REGS
, GENERAL_REGS
, GENERAL_REGS
, GENERAL_REGS
,
96 GENERAL_REGS
, PR_REGS
, T_REGS
, NO_REGS
,
97 MAC_REGS
, MAC_REGS
, FPUL_REGS
, GENERAL_REGS
,
98 FP0_REGS
,FP_REGS
, FP_REGS
, FP_REGS
,
99 FP_REGS
, FP_REGS
, FP_REGS
, FP_REGS
,
100 FP_REGS
, FP_REGS
, FP_REGS
, FP_REGS
,
101 FP_REGS
, FP_REGS
, FP_REGS
, FP_REGS
,
104 /* Provide reg_class from a letter such as appears in the machine
107 enum reg_class reg_class_from_letter
[] =
109 /* a */ NO_REGS
, /* b */ NO_REGS
, /* c */ NO_REGS
, /* d */ NO_REGS
,
110 /* e */ NO_REGS
, /* f */ FP_REGS
, /* g */ NO_REGS
, /* h */ NO_REGS
,
111 /* i */ NO_REGS
, /* j */ NO_REGS
, /* k */ NO_REGS
, /* l */ PR_REGS
,
112 /* m */ NO_REGS
, /* n */ NO_REGS
, /* o */ NO_REGS
, /* p */ NO_REGS
,
113 /* q */ NO_REGS
, /* r */ NO_REGS
, /* s */ NO_REGS
, /* t */ T_REGS
,
114 /* u */ NO_REGS
, /* v */ NO_REGS
, /* w */ FP0_REGS
, /* x */ MAC_REGS
,
115 /* y */ FPUL_REGS
, /* z */ R0_REGS
118 /* Print the operand address in x to the stream. */
121 print_operand_address (stream
, x
)
125 switch (GET_CODE (x
))
128 fprintf (stream
, "@%s", reg_names
[REGNO (x
)]);
133 rtx base
= XEXP (x
, 0);
134 rtx index
= XEXP (x
, 1);
136 switch (GET_CODE (index
))
139 fprintf (stream
, "@(%d,%s)", INTVAL (index
),
140 reg_names
[REGNO (base
)]);
144 fprintf (stream
, "@(r0,%s)",
145 reg_names
[MAX (REGNO (base
), REGNO (index
))]);
156 fprintf (stream
, "@-%s", reg_names
[REGNO (XEXP (x
, 0))]);
160 fprintf (stream
, "@%s+", reg_names
[REGNO (XEXP (x
, 0))]);
164 output_addr_const (stream
, x
);
169 /* Print operand x (an rtx) in assembler syntax to file stream
170 according to modifier code.
172 '.' print a .s if insn needs delay slot
173 '@' print trap, rte or rts depending upon pragma interruptness
174 '#' output a nop if there is nothing to put in the delay slot
175 'O' print a constant without the #
176 'R' print the LSW of a dp value - changes if in little endian
177 'S' print the MSW of a dp value - changes if in little endian
178 'T' print the next word of a dp value - same as 'R' in big endian mode. */
181 print_operand (stream
, x
, code
)
190 && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence
, 0, 0)))
191 fprintf (stream
, ".s");
195 fprintf (stream
, "trapa #%d", trap_exit
);
196 else if (pragma_interrupt
)
197 fprintf (stream
, "rte");
199 fprintf (stream
, "rts");
202 /* Output a nop if there's nothing in the delay slot. */
203 if (dbr_sequence_length () == 0)
204 fprintf (stream
, "\n\tnop");
207 output_addr_const (stream
, x
);
210 fputs (reg_names
[REGNO (x
) + LSW
], (stream
));
213 fputs (reg_names
[REGNO (x
) + MSW
], (stream
));
216 /* Next word of a double. */
217 switch (GET_CODE (x
))
220 fputs (reg_names
[REGNO (x
) + 1], (stream
));
223 print_operand_address (stream
,
224 XEXP (adj_offsettable_operand (x
, 4), 0));
229 switch (GET_CODE (x
))
232 fputs (reg_names
[REGNO (x
)], (stream
));
235 output_address (XEXP (x
, 0));
239 output_addr_const (stream
, x
);
246 /* Emit code to perform a block move. Choose the best method.
248 OPERANDS[0] is the destination.
249 OPERANDS[1] is the source.
250 OPERANDS[2] is the size.
251 OPERANDS[3] is the alignment safe to use. */
254 expand_block_move (operands
)
257 int align
= INTVAL (operands
[3]);
258 int constp
= (GET_CODE (operands
[2]) == CONST_INT
);
259 int bytes
= (constp
? INTVAL (operands
[2]) : 0);
261 /* If it isn't a constant number of bytes, or if it doesn't have 4 byte
262 alignment, or if it isn't a multiple of 4 bytes, then fail. */
263 if (! constp
|| align
< 4 || (bytes
% 4 != 0))
271 rtx r4
= gen_rtx (REG
, SImode
, 4);
272 rtx r5
= gen_rtx (REG
, SImode
, 5);
274 sprintf (entry
, "__movstrSI%d", bytes
);
275 entry_name
= get_identifier (entry
);
278 = copy_to_mode_reg (Pmode
,
279 gen_rtx (SYMBOL_REF
, Pmode
,
280 IDENTIFIER_POINTER (entry_name
)));
281 emit_insn (gen_move_insn (r4
, XEXP (operands
[0], 0)));
282 emit_insn (gen_move_insn (r5
, XEXP (operands
[1], 0)));
283 emit_insn (gen_block_move_real (func_addr_rtx
));
287 /* This is the same number of bytes as a memcpy call, but to a different
288 less common function name, so this will occasionally use more space. */
289 if (! TARGET_SMALLCODE
)
293 int final_switch
, while_loop
;
294 rtx r4
= gen_rtx (REG
, SImode
, 4);
295 rtx r5
= gen_rtx (REG
, SImode
, 5);
296 rtx r6
= gen_rtx (REG
, SImode
, 6);
298 entry_name
= get_identifier ("__movstr");
300 = copy_to_mode_reg (Pmode
,
301 gen_rtx (SYMBOL_REF
, Pmode
,
302 IDENTIFIER_POINTER (entry_name
)));
303 emit_insn (gen_move_insn (r4
, XEXP (operands
[0], 0)));
304 emit_insn (gen_move_insn (r5
, XEXP (operands
[1], 0)));
306 /* r6 controls the size of the move. 16 is decremented from it
307 for each 64 bytes moved. Then the negative bit left over is used
308 as an index into a list of move instructions. e.g., a 72 byte move
309 would be set up with size(r6) = 14, for one iteration through the
310 big while loop, and a switch of -2 for the last part. */
312 final_switch
= 16 - ((bytes
/ 4) % 16);
313 while_loop
= ((bytes
/ 4) / 16 - 1) * 16;
314 emit_insn (gen_move_insn (r6
, GEN_INT (while_loop
+ final_switch
)));
315 emit_insn (gen_block_lump_real (func_addr_rtx
));
322 /* Prepare operands for a move define_expand; specifically, one of the
323 operands must be in a register. */
326 prepare_move_operands (operands
, mode
)
328 enum machine_mode mode
;
330 if (! reload_in_progress
&& ! reload_completed
)
332 /* Copy the source to a register if both operands aren't registers. */
333 if (! register_operand (operands
[0], mode
)
334 && ! register_operand (operands
[1], mode
))
335 operands
[1] = copy_to_mode_reg (mode
, operands
[1]);
337 /* This case can happen while generating code to move the result
338 of a library call to the target. Reject `st r0,@(rX,rY)' because
339 reload will fail to find a spill register for rX, since r0 is already
340 being used for the source. */
341 else if (GET_CODE (operands
[1]) == REG
&& REGNO (operands
[1]) == 0
342 && GET_CODE (operands
[0]) == MEM
343 && GET_CODE (XEXP (operands
[0], 0)) == PLUS
344 && GET_CODE (XEXP (XEXP (operands
[0], 0), 1)) == REG
)
345 operands
[1] = copy_to_mode_reg (mode
, operands
[1]);
351 /* Prepare the operands for an scc instruction; make sure that the
352 compare has been done. */
354 prepare_scc_operands (code
)
357 rtx t_reg
= gen_rtx (REG
, SImode
, T_REG
);
358 enum rtx_code oldcode
= code
;
359 enum machine_mode mode
;
361 /* First need a compare insn. */
365 /* It isn't possible to handle this case. */
382 rtx tmp
= sh_compare_op0
;
383 sh_compare_op0
= sh_compare_op1
;
384 sh_compare_op1
= tmp
;
387 mode
= GET_MODE (sh_compare_op0
);
388 if (mode
== VOIDmode
)
389 mode
= GET_MODE (sh_compare_op1
);
391 sh_compare_op0
= force_reg (mode
, sh_compare_op0
);
392 if (code
!= EQ
&& code
!= NE
393 && (sh_compare_op1
!= const0_rtx
394 || code
== GTU
|| code
== GEU
|| code
== LTU
|| code
== LEU
))
395 sh_compare_op1
= force_reg (mode
, sh_compare_op1
);
397 /* ??? This should be `mode' not `SImode' in the compare, but that would
398 require fixing the branch patterns too. */
399 emit_insn (gen_rtx (SET
, VOIDmode
, t_reg
,
400 gen_rtx (code
, SImode
, sh_compare_op0
,
406 /* Called from the md file, set up the operands of a compare instruction. */
409 from_compare (operands
, code
)
413 if (code
!= EQ
&& code
!= NE
)
415 enum machine_mode mode
= GET_MODE (sh_compare_op0
);
416 if (mode
== VOIDmode
)
417 mode
= GET_MODE (sh_compare_op1
);
419 /* Force args into regs, since we can't use constants here. */
420 sh_compare_op0
= force_reg (mode
, sh_compare_op0
);
421 if (sh_compare_op1
!= const0_rtx
422 || code
== GTU
|| code
== GEU
|| code
== LTU
|| code
== LEU
)
423 sh_compare_op1
= force_reg (mode
, sh_compare_op1
);
425 operands
[1] = sh_compare_op0
;
426 operands
[2] = sh_compare_op1
;
429 /* Functions to output assembly code. */
431 /* Return a sequence of instructions to perform DI or DF move.
433 Since the SH cannot move a DI or DF in one instruction, we have
434 to take care when we see overlapping source and dest registers. */
437 output_movedouble (insn
, operands
, mode
)
440 enum machine_mode mode
;
442 rtx dst
= operands
[0];
443 rtx src
= operands
[1];
445 if (GET_CODE (dst
) == MEM
446 && GET_CODE (XEXP (dst
, 0)) == PRE_DEC
)
447 return "mov.l %T1,%0\n\tmov.l %1,%0";
449 if (register_operand (dst
, mode
)
450 && register_operand (src
, mode
))
452 if (REGNO (src
) == MACH_REG
)
453 return "sts mach,%S0\n\tsts macl,%R0";
455 /* When mov.d r1,r2 do r2->r3 then r1->r2;
456 when mov.d r1,r0 do r1->r0 then r2->r1. */
458 if (REGNO (src
) + 1 == REGNO (dst
))
459 return "mov %T1,%T0\n\tmov %1,%0";
461 return "mov %1,%0\n\tmov %T1,%T0";
463 else if (GET_CODE (src
) == CONST_INT
)
465 if (INTVAL (src
) < 0)
466 output_asm_insn ("mov #-1,%S0", operands
);
468 output_asm_insn ("mov #0,%S0", operands
);
472 else if (GET_CODE (src
) == MEM
)
475 int dreg
= REGNO (dst
);
476 rtx inside
= XEXP (src
, 0);
478 if (GET_CODE (inside
) == REG
)
479 ptrreg
= REGNO (inside
);
480 else if (GET_CODE (inside
) == SUBREG
)
481 ptrreg
= REGNO (SUBREG_REG (inside
)) + SUBREG_WORD (inside
);
482 else if (GET_CODE (inside
) == PLUS
)
484 ptrreg
= REGNO (XEXP (inside
, 0));
485 /* ??? A r0+REG address shouldn't be possible here, because it isn't
486 an offsettable address. Unfortunately, offsettable addresses use
487 QImode to check the offset, and a QImode offsettable address
488 requires r0 for the other operand, which is not currently
489 supported, so we can't use the 'o' constraint.
490 Thus we must check for and handle r0+REG addresses here.
491 We punt for now, since this is likely very rare. */
492 if (GET_CODE (XEXP (inside
, 1)) == REG
)
495 else if (GET_CODE (inside
) == LABEL_REF
)
496 return "mov.l %1,%0\n\tmov.l %1+4,%T0";
497 else if (GET_CODE (inside
) == POST_INC
)
498 return "mov.l %1,%0\n\tmov.l %1,%T0";
502 /* Work out the safe way to copy. Copy into the second half first. */
504 return "mov.l %T1,%T0\n\tmov.l %1,%0";
507 return "mov.l %1,%0\n\tmov.l %T1,%T0";
510 /* Print an instruction which would have gone into a delay slot after
511 another instruction, but couldn't because the other instruction expanded
512 into a sequence where putting the slot insn at the end wouldn't work. */
518 final_scan_insn (XVECEXP (insn
, 0, 1), asm_out_file
, optimize
, 0, 1);
520 INSN_DELETED_P (XVECEXP (insn
, 0, 1)) = 1;
523 /* We can't tell if we need a register as a scratch for the jump
524 until after branch shortening, and then it's too late to allocate a
525 register the 'proper' way. These instruction sequences are rare
526 anyway, so to avoid always using a reg up from our limited set, we'll
527 grab one when we need one on output. */
529 /* ??? Should fix compiler so that using a clobber scratch in jump
530 instructions works, and then this will be unnecessary. */
533 output_far_jump (insn
, op
)
537 rtx thislab
= gen_label_rtx ();
539 /* Output the delay slot insn first if any. */
540 if (dbr_sequence_length ())
541 print_slot (final_sequence
);
543 output_asm_insn ("mov.l r13,@-r15", 0);
544 output_asm_insn ("mov.l %O0,r13", &thislab
);
545 output_asm_insn ("jmp @r13", 0);
546 output_asm_insn ("mov.l @r15+,r13", 0);
547 output_asm_insn (".align 2", 0);
548 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file
, "L", CODE_LABEL_NUMBER (thislab
));
549 output_asm_insn (".long %O0", &op
);
553 /* Local label counter, used for constants in the pool and inside
558 /* Output code for ordinary branches. */
561 output_branch (logic
, insn
, operands
)
567 int length
= get_attr_length (insn
);
570 /* Undo the effects of ADJUST_INSN_LENGTH, so that we get the real
571 length. If NEXT_INSN (PREV_INSN (insn)) != insn, then the insn
572 is inside a sequence, and ADJUST_INSN_LENGTH was not called on
574 if (PREV_INSN (insn
) == NULL
575 || NEXT_INSN (PREV_INSN (insn
)) == insn
)
577 adjusted_length
= length
;
578 ADJUST_INSN_LENGTH (insn
, adjusted_length
);
579 length
-= (adjusted_length
- length
);
585 /* A branch with an unfilled delay slot. */
587 /* Simple branch in range -252..+258 bytes */
588 return logic
? "bt%. %l0" : "bf%. %l0";
591 /* A branch with an unfilled delay slot. */
593 /* Branch in range -4092..+4098 bytes. */
595 /* The call to print_slot will clobber the operands. */
596 rtx op0
= operands
[0];
598 /* If the instruction in the delay slot is annulled (true), then
599 there is no delay slot where we can put it now. The only safe
600 place for it is after the label. */
604 fprintf (asm_out_file
, "\tb%c%s\tLF%d\n", logic
? 'f' : 't',
605 INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence
, 0, 0))
607 if (! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence
, 0, 0)))
608 print_slot (final_sequence
);
611 fprintf (asm_out_file
, "\tb%c\tLF%d\n", logic
? 'f' : 't', label
);
613 output_asm_insn ("bra %l0", &op0
);
614 fprintf (asm_out_file
, "\tnop\n");
615 fprintf (asm_out_file
, "LF%d:\n", label
);
618 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence
, 0, 0)))
619 print_slot (final_sequence
);
624 /* A branch with an unfilled delay slot. */
626 /* Branches a long way away. */
628 /* The call to print_slot will clobber the operands. */
629 rtx op0
= operands
[0];
631 /* If the instruction in the delay slot is annulled (true), then
632 there is no delay slot where we can put it now. The only safe
633 place for it is after the label. */
637 fprintf (asm_out_file
, "\tb%c%s\tLF%d\n", logic
? 'f' : 't',
638 INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence
, 0, 0))
640 if (! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence
, 0, 0)))
641 print_slot (final_sequence
);
644 fprintf (asm_out_file
, "\tb%c\tLF%d\n", logic
? 'f' : 't', label
);
646 output_far_jump (insn
, op0
);
647 fprintf (asm_out_file
, "LF%d:\n", label
);
650 && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence
, 0, 0)))
651 print_slot (final_sequence
);
659 /* Output to FILE the start of the assembler file. */
662 output_file_start (file
)
667 output_file_directive (file
, main_input_filename
);
669 /* Switch to the data section so that the coffsem symbol and the
670 gcc2_compiled. symbol aren't in the text section. */
673 if (TARGET_LITTLE_ENDIAN
)
674 fprintf (file
, "\t.little\n");
677 /* Actual number of instructions used to make a shift by N. */
678 static char ashiftrt_insns
[] =
679 { 0,1,2,3,4,5,8,8,8,8,8,8,8,8,8,8,2,3,4,5,8,8,8,8,8,8,8,8,8,8,8,2};
681 /* Left shift and logical right shift are the same. */
682 static char shift_insns
[] =
683 { 0,1,1,2,2,3,3,4,1,2,2,3,3,4,3,3,1,2,2,3,3,4,3,3,2,3,3,4,4,4,3,3};
685 /* Individual shift amounts needed to get the above length sequences.
686 One bit right shifts clobber the T bit, so when possible, put one bit
687 shifts in the middle of the sequence, so the ends are eligible for
688 branch delay slots. */
689 static short shift_amounts
[32][5] = {
690 {0}, {1}, {2}, {2, 1},
691 {2, 2}, {2, 1, 2}, {2, 2, 2}, {2, 2, 1, 2},
692 {8}, {8, 1}, {8, 2}, {8, 1, 2},
693 {8, 2, 2}, {8, 2, 1, 2}, {8, -2, 8}, {8, -1, 8},
694 {16}, {16, 1}, {16, 2}, {16, 1, 2},
695 {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
696 {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
697 {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
699 /* Likewise, but for shift amounts < 16, up to three highmost bits
700 might be clobbered. This is typically used when combined with some
701 kind of sign or zero extension. */
703 static char ext_shift_insns
[] =
704 { 0,1,1,2,2,3,2,2,1,2,2,3,3,3,2,2,1,2,2,3,3,4,3,3,2,3,3,4,4,4,3,3};
706 static short ext_shift_amounts
[32][4] = {
707 {0}, {1}, {2}, {2, 1},
708 {2, 2}, {2, 1, 2}, {8, -2}, {8, -1},
709 {8}, {8, 1}, {8, 2}, {8, 1, 2},
710 {8, 2, 2}, {16, -2, -1}, {16, -2}, {16, -1},
711 {16}, {16, 1}, {16, 2}, {16, 1, 2},
712 {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
713 {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
714 {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
716 /* Assuming we have a value that has been sign-extended by at least one bit,
717 can we use the ext_shift_amounts with the last shift turned to an arithmetic shift
718 to shift it by N without data loss, and quicker than by other means? */
719 #define EXT_SHIFT_SIGNED(n) (((n) | 8) == 15)
721 /* This is used in length attributes in sh.md to help compute the length
722 of arbitrary constant shift instructions. */
725 shift_insns_rtx (insn
)
728 rtx set_src
= SET_SRC (XVECEXP (PATTERN (insn
), 0, 0));
729 int shift_count
= INTVAL (XEXP (set_src
, 1));
730 enum rtx_code shift_code
= GET_CODE (set_src
);
735 return ashiftrt_insns
[shift_count
];
738 return shift_insns
[shift_count
];
744 /* Return the cost of a shift. */
750 int value
= INTVAL (XEXP (x
, 1));
752 /* If shift by a non constant, then this will be expensive. */
753 if (GET_CODE (XEXP (x
, 1)) != CONST_INT
)
757 /* If not an sh3 then we don't even have an instruction for it. */
761 /* Otherwise, return the true cost in instructions. */
762 if (GET_CODE (x
) == ASHIFTRT
)
764 int cost
= ashiftrt_insns
[value
];
765 /* If SH3, then we put the constant in a reg and use shad. */
766 if (TARGET_SH3
&& cost
> 3)
771 return shift_insns
[value
];
774 /* Return the cost of an AND operation. */
782 /* Anding with a register is a single cycle and instruction. */
783 if (GET_CODE (XEXP (x
, 1)) != CONST_INT
)
786 i
= INTVAL (XEXP (x
, 1));
787 /* These constants are single cycle extu.[bw] instructions. */
788 if (i
== 0xff || i
== 0xffff)
790 /* Constants that can be used in an and immediate instruction is a single
791 cycle, but this requires r0, so make it a little more expensive. */
792 if (CONST_OK_FOR_L (i
))
794 /* Constants that can be loaded with a mov immediate and an and.
795 This case is probably unnecessary. */
796 if (CONST_OK_FOR_I (i
))
798 /* Any other constants requires a 2 cycle pc-relative load plus an and.
799 This case is probably unnecessary. */
803 /* Return the cost of a multiply. */
810 /* We have a mul insn, so we can never take more than the mul and the
811 read of the mac reg, but count more because of the latency and extra
813 if (TARGET_SMALLCODE
)
818 /* If we're aiming at small code, then just count the number of
819 insns in a multiply call sequence. */
820 if (TARGET_SMALLCODE
)
823 /* Otherwise count all the insns in the routine we'd be calling too. */
827 /* Code to expand a shift. */
830 gen_ashift (type
, n
, reg
)
835 /* Negative values here come from the shift_amounts array. */
848 emit_insn (gen_ashrsi3_k (reg
, reg
, GEN_INT (n
)));
852 emit_insn (gen_lshrsi3_m (reg
, reg
, GEN_INT (n
)));
854 emit_insn (gen_lshrsi3_k (reg
, reg
, GEN_INT (n
)));
857 emit_insn (gen_ashlsi3_k (reg
, reg
, GEN_INT (n
)));
862 /* Same for HImode */
865 gen_ashift_hi (type
, n
, reg
)
870 /* Negative values here come from the shift_amounts array. */
883 emit_insn (gen_ashrhi3_k (reg
, reg
, GEN_INT (n
)));
887 emit_insn (gen_lshrhi3_m (reg
, reg
, GEN_INT (n
)));
889 emit_insn (gen_lshrhi3_k (reg
, reg
, GEN_INT (n
)));
892 emit_insn (gen_ashlhi3_k (reg
, reg
, GEN_INT (n
)));
897 /* Output RTL to split a constant shift into its component SH constant
898 shift instructions. */
901 gen_shifty_op (code
, operands
)
905 int value
= INTVAL (operands
[2]);
908 /* Truncate the shift count in case it is out of bounds. */
909 value
= value
& 0x1f;
913 if (code
== LSHIFTRT
)
915 emit_insn (gen_rotlsi3_1 (operands
[0], operands
[0]));
916 emit_insn (gen_movt (operands
[0]));
919 else if (code
== ASHIFT
)
921 /* There is a two instruction sequence for 31 bit left shifts,
922 but it requires r0. */
923 if (GET_CODE (operands
[0]) == REG
&& REGNO (operands
[0]) == 0)
925 emit_insn (gen_andsi3 (operands
[0], operands
[0], const1_rtx
));
926 emit_insn (gen_rotlsi3_31 (operands
[0], operands
[0]));
933 /* This can happen when not optimizing. We must output something here
934 to prevent the compiler from aborting in final.c after the try_split
936 emit_insn (gen_nop ());
940 max
= shift_insns
[value
];
941 for (i
= 0; i
< max
; i
++)
942 gen_ashift (code
, shift_amounts
[value
][i
], operands
[0]);
945 /* Same as above, but optimized for values where the topmost bits don't
949 gen_shifty_hi_op (code
, operands
)
953 int value
= INTVAL (operands
[2]);
957 /* This operation is used by and_shl for SImode values with a few
958 high bits known to be cleared. */
962 emit_insn (gen_nop ());
966 gen_fun
= GET_MODE (operands
[0]) == HImode
? gen_ashift_hi
: gen_ashift
;
969 max
= ext_shift_insns
[value
];
970 for (i
= 0; i
< max
; i
++)
971 gen_fun (code
, ext_shift_amounts
[value
][i
], operands
[0]);
974 /* When shifting right, emit the shifts in reverse order, so that
975 solitary negative values come first. */
976 for (i
= ext_shift_insns
[value
] - 1; i
>= 0; i
--)
977 gen_fun (code
, ext_shift_amounts
[value
][i
], operands
[0]);
980 /* Output RTL for an arithmetic right shift. */
982 /* ??? Rewrite to use super-optimizer sequences. */
985 expand_ashiftrt (operands
)
995 if (GET_CODE (operands
[2]) != CONST_INT
)
997 rtx count
= copy_to_mode_reg (SImode
, operands
[2]);
998 emit_insn (gen_negsi2 (count
, count
));
999 emit_insn (gen_ashrsi3_d (operands
[0], operands
[1], count
));
1002 else if (ashiftrt_insns
[INTVAL (operands
[2])] > 3)
1004 rtx count
= force_reg (SImode
, GEN_INT (- INTVAL (operands
[2])));
1005 emit_insn (gen_ashrsi3_d (operands
[0], operands
[1], count
));
1009 if (GET_CODE (operands
[2]) != CONST_INT
)
1012 value
= INTVAL (operands
[2]);
1016 emit_insn (gen_ashrsi2_31 (operands
[0], operands
[1]));
1019 else if (value
>= 16 && value
<= 19)
1021 wrk
= gen_reg_rtx (SImode
);
1022 emit_insn (gen_ashrsi2_16 (wrk
, operands
[1]));
1025 gen_ashift (ASHIFTRT
, 1, wrk
);
1026 emit_move_insn (operands
[0], wrk
);
1029 /* Expand a short sequence inline, longer call a magic routine. */
1030 else if (value
<= 5)
1032 wrk
= gen_reg_rtx (SImode
);
1033 emit_move_insn (wrk
, operands
[1]);
1035 gen_ashift (ASHIFTRT
, 1, wrk
);
1036 emit_move_insn (operands
[0], wrk
);
1040 wrk
= gen_reg_rtx (Pmode
);
1042 /* Load the value into an arg reg and call a helper. */
1043 emit_move_insn (gen_rtx (REG
, SImode
, 4), operands
[1]);
1044 sprintf (func
, "__ashiftrt_r4_%d", value
);
1045 func_name
= get_identifier (func
);
1046 emit_move_insn (wrk
, gen_rtx (SYMBOL_REF
, Pmode
,
1047 IDENTIFIER_POINTER (func_name
)));
1048 emit_insn (gen_ashrsi3_n (GEN_INT (value
), wrk
));
1049 emit_move_insn (operands
[0], gen_rtx (REG
, SImode
, 4));
1053 /* Try to find a good way to implement the combiner pattern
1054 [(set (match_operand:SI 0 "register_operand" "r")
1055 (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
1056 (match_operand:SI 2 "const_int_operand" "n"))
1057 (match_operand:SI 3 "const_int_operand" "n"))) .
1058 LEFT_RTX is operand 2 in the above pattern, and MASK_RTX is operand 3.
1059 return 0 for simple right / left or left/right shift combination.
1060 return 1 for a combination of shifts with zero_extend.
1061 return 2 for a combination of shifts with an AND that needs r0.
1062 return 3 for a combination of shifts with an AND that needs an extra
1063 scratch register, when the three highmost bits of the AND mask are clear.
1064 return 4 for a combination of shifts with an AND that needs an extra
1065 scratch register, when any of the three highmost bits of the AND mask
1067 If ATTRP is set, store an initial right shift width in ATTRP[0],
1068 and the instruction length in ATTRP[1] . These values are not valid
1070 When ATTRP is set and returning 1, ATTRP[2] gets set to the index into
1071 shift_amounts for the last shift value that is to be used before the
1074 shl_and_kind (left_rtx
, mask_rtx
, attrp
)
1075 rtx left_rtx
, mask_rtx
;
1078 unsigned HOST_WIDE_INT mask
, lsb
, mask2
, lsb2
;
1079 int left
= INTVAL (left_rtx
), right
;
1081 int cost
, best_cost
= 10000;
1082 int best_right
= 0, best_len
= 0;
1086 if (left
< 0 || left
> 31)
1088 if (GET_CODE (mask_rtx
) == CONST_INT
)
1089 mask
= (unsigned HOST_WIDE_INT
) INTVAL (mask_rtx
) >> left
;
1091 mask
= (unsigned HOST_WIDE_INT
) GET_MODE_MASK (SImode
) >> left
;
1092 /* Can this be expressed as a right shift / left shift pair ? */
1093 lsb
= ((mask
^ (mask
- 1)) >> 1) + 1;
1094 right
= exact_log2 (lsb
);
1095 mask2
= ~(mask
+ lsb
- 1);
1096 lsb2
= ((mask2
^ (mask2
- 1)) >> 1) + 1;
1097 /* mask has no zeroes but trailing zeroes <==> ! mask2 */
1099 best_cost
= shift_insns
[right
] + shift_insns
[right
+ left
];
1100 /* mask has no trailing zeroes <==> ! right */
1101 else if (! right
&& mask2
== ~(lsb2
- 1))
1103 int late_right
= exact_log2 (lsb2
);
1104 best_cost
= shift_insns
[left
+ late_right
] + shift_insns
[late_right
];
1106 /* Try to use zero extend */
1107 if (mask2
== ~(lsb2
- 1))
1111 for (width
= 8; width
<= 16; width
+= 8)
1113 /* Can we zero-extend right away? */
1114 if (lsb2
== (HOST_WIDE_INT
)1 << width
)
1117 = 1 + ext_shift_insns
[right
] + ext_shift_insns
[left
+ right
];
1118 if (cost
< best_cost
)
1129 /* ??? Could try to put zero extend into initial right shift,
1130 or even shift a bit left before the right shift. */
1131 /* Determine value of first part of left shift, to get to the
1132 zero extend cut-off point. */
1133 first
= width
- exact_log2 (lsb2
) + right
;
1134 if (first
>= 0 && right
+ left
- first
>= 0)
1136 cost
= ext_shift_insns
[right
] + ext_shift_insns
[first
] + 1
1137 + ext_shift_insns
[right
+ left
- first
];
1138 if (cost
< best_cost
)
1150 /* Try to use r0 AND pattern */
1151 for (i
= 0; i
<= 2; i
++)
1155 if (! CONST_OK_FOR_L (mask
>> i
))
1157 cost
= (i
!= 0) + 2 + ext_shift_insns
[left
+ i
];
1158 if (cost
< best_cost
)
1163 best_len
= cost
- 1;
1166 /* Try to use a scratch register to hold the AND operand. */
1167 can_ext
= ((mask
<< left
) & 0xe0000000) == 0;
1168 for (i
= 0; i
<= 2; i
++)
1172 cost
= (i
!= 0) + (CONST_OK_FOR_I (mask
>> i
) ? 2 : 3)
1173 + (can_ext
? ext_shift_insns
: shift_insns
)[left
+ i
];
1174 if (cost
< best_cost
)
1179 best_len
= cost
- 1 - ! CONST_OK_FOR_I (mask
>> i
);
1185 attrp
[0] = best_right
;
1186 attrp
[1] = best_len
;
1191 /* This is used in length attributes of the unnamed instructions
1192 corresponding to shl_and_kind return values of 1 and 2. */
1194 shl_and_length (insn
)
1197 rtx set_src
, left_rtx
, mask_rtx
;
1200 set_src
= SET_SRC (XVECEXP (PATTERN (insn
), 0, 0));
1201 left_rtx
= XEXP (XEXP (set_src
, 0), 1);
1202 mask_rtx
= XEXP (set_src
, 1);
1203 shl_and_kind (left_rtx
, mask_rtx
, attributes
);
1204 return attributes
[1];
1207 /* This is used in length attribute of the and_shl_scratch instruction. */
1210 shl_and_scr_length (insn
)
1213 rtx set_src
= SET_SRC (XVECEXP (PATTERN (insn
), 0, 0));
1214 int len
= shift_insns
[INTVAL (XEXP (set_src
, 1))];
1215 rtx op
= XEXP (set_src
, 0);
1216 len
+= shift_insns
[INTVAL (XEXP (op
, 1))] + 1;
1217 op
= XEXP (XEXP (op
, 0), 0);
1218 return len
+ shift_insns
[INTVAL (XEXP (op
, 1))];
1221 /* Generating rtl? */
1222 extern int rtx_equal_function_value_matters
;
1224 /* Generate rtl for instructions for which shl_and_kind advised a particular
1225 method of generating them, i.e. returned zero. */
1228 gen_shl_and (dest
, left_rtx
, mask_rtx
, source
)
1229 rtx dest
, left_rtx
, mask_rtx
, source
;
1232 unsigned HOST_WIDE_INT mask
;
1233 int kind
= shl_and_kind (left_rtx
, mask_rtx
, attributes
);
1234 int right
, total_shift
;
1235 int (*shift_gen_fun
) PROTO((int, rtx
*)) = gen_shifty_hi_op
;
1237 right
= attributes
[0];
1238 total_shift
= INTVAL (left_rtx
) + right
;
1239 mask
= (unsigned HOST_WIDE_INT
) INTVAL (mask_rtx
) >> total_shift
;
1246 int first
= attributes
[2];
1251 emit_insn ((mask
<< right
) == 0xff
1252 ? gen_zero_extendqisi2(dest
,
1253 gen_lowpart (QImode
, source
))
1254 : gen_zero_extendhisi2(dest
,
1255 gen_lowpart (HImode
, source
)));
1259 emit_insn (gen_movsi (dest
, source
));
1263 operands
[2] = GEN_INT (right
);
1264 gen_shifty_hi_op (LSHIFTRT
, operands
);
1268 operands
[2] = GEN_INT (first
);
1269 gen_shifty_hi_op (ASHIFT
, operands
);
1270 total_shift
-= first
;
1274 emit_insn (mask
== 0xff
1275 ? gen_zero_extendqisi2(dest
, gen_lowpart (QImode
, dest
))
1276 : gen_zero_extendhisi2(dest
, gen_lowpart (HImode
, dest
)));
1277 if (total_shift
> 0)
1279 operands
[2] = GEN_INT (total_shift
);
1280 gen_shifty_hi_op (ASHIFT
, operands
);
1285 shift_gen_fun
= gen_shifty_op
;
1288 /* If the topmost bit that matters is set, set the topmost bits
1289 that don't matter. This way, we might be able to get a shorter
1291 if (mask
& ((HOST_WIDE_INT
)1 << 31 - total_shift
))
1292 mask
|= (HOST_WIDE_INT
)~0 << (31 - total_shift
);
1293 /* Don't expand fine-grained when combining, because that will
1294 make the pattern fail. */
1295 if (rtx_equal_function_value_matters
1296 || reload_in_progress
|| reload_completed
)
1302 emit_insn (gen_lshrsi3 (dest
, source
, GEN_INT (right
)));
1305 emit_insn (gen_andsi3 (dest
, source
, GEN_INT (mask
)));
1310 operands
[2] = GEN_INT (total_shift
);
1311 shift_gen_fun (ASHIFT
, operands
);
1318 if (kind
!= 4 && total_shift
< 16)
1320 neg
= -ext_shift_amounts
[total_shift
][1];
1322 neg
-= ext_shift_amounts
[total_shift
][2];
1326 emit_insn (gen_and_shl_scratch (dest
, source
,
1329 GEN_INT (total_shift
+ neg
),
1331 emit_insn (gen_movsi (dest
, dest
));
1338 /* Try to find a good way to implement the combiner pattern
1339 [(set (match_operand:SI 0 "register_operand" "=r")
1340 (sign_extract:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
1341 (match_operand:SI 2 "const_int_operand" "n")
1342 (match_operand:SI 3 "const_int_operand" "n")
1344 (clobber (reg:SI 18))]
1345 LEFT_RTX is operand 2 in the above pattern, and SIZE_RTX is operand 3.
1346 return 0 for simple left / right shift combination.
1347 return 1 for left shift / 8 bit sign extend / left shift.
1348 return 2 for left shift / 16 bit sign extend / left shift.
1349 return 3 for left shift / 8 bit sign extend / shift / sign extend.
1350 return 4 for left shift / 16 bit sign extend / shift / sign extend.
1351 return 5 for left shift / 16 bit sign extend / right shift
1352 return 6 for < 8 bit sign extend / left shift.
1353 return 7 for < 8 bit sign extend / left shift / single right shift.
1354 If COSTP is nonzero, assign the calculated cost to *COSTP. */
1357 shl_sext_kind (left_rtx
, size_rtx
, costp
)
1358 rtx left_rtx
, size_rtx
;
1361 int left
, size
, insize
, ext
;
1362 int cost
, best_cost
;
1365 left
= INTVAL (left_rtx
);
1366 size
= INTVAL (size_rtx
);
1367 insize
= size
- left
;
1370 /* Default to left / right shift. */
1372 best_cost
= shift_insns
[32 - insize
] + ashiftrt_insns
[32 - size
];
1375 /* 16 bit shift / sign extend / 16 bit shift */
1376 cost
= shift_insns
[16 - insize
] + 1 + ashiftrt_insns
[16 - size
];
1377 /* If ashiftrt_insns[16 - size] is 8, this choice will be overridden
1378 below, by alternative 3 or something even better. */
1379 if (cost
< best_cost
)
1385 /* Try a plain sign extend between two shifts. */
1386 for (ext
= 16; ext
>= insize
; ext
-= 8)
1390 cost
= ext_shift_insns
[ext
- insize
] + 1 + shift_insns
[size
- ext
];
1391 if (cost
< best_cost
)
1397 /* Check if we can do a sloppy shift with a final signed shift
1398 restoring the sign. */
1399 if (EXT_SHIFT_SIGNED (size
- ext
))
1400 cost
= ext_shift_insns
[ext
- insize
] + ext_shift_insns
[size
- ext
] + 1;
1401 /* If not, maybe it's still cheaper to do the second shift sloppy,
1402 and do a final sign extend? */
1403 else if (size
<= 16)
1404 cost
= ext_shift_insns
[ext
- insize
] + 1
1405 + ext_shift_insns
[size
> ext
? size
- ext
: ext
- size
] + 1;
1408 if (cost
< best_cost
)
1410 kind
= ext
/ 8U + 2;
1414 /* Check if we can sign extend in r0 */
1417 cost
= 3 + shift_insns
[left
];
1418 if (cost
< best_cost
)
1423 /* Try the same with a final signed shift. */
1426 cost
= 3 + ext_shift_insns
[left
+ 1] + 1;
1427 if (cost
< best_cost
)
1436 /* Try to use a dynamic shift. */
1437 cost
= shift_insns
[32 - insize
] + 3;
1438 if (cost
< best_cost
)
1449 /* Function to be used in the length attribute of the instructions
1450 implementing this pattern. */
1453 shl_sext_length (insn
)
1456 rtx set_src
, left_rtx
, size_rtx
;
1459 set_src
= SET_SRC (XVECEXP (PATTERN (insn
), 0, 0));
1460 left_rtx
= XEXP (XEXP (set_src
, 0), 1);
1461 size_rtx
= XEXP (set_src
, 1);
1462 shl_sext_kind (left_rtx
, size_rtx
, &cost
);
1466 /* Generate rtl for this pattern */
1469 gen_shl_sext (dest
, left_rtx
, size_rtx
, source
)
1470 rtx dest
, left_rtx
, size_rtx
, source
;
1473 int left
, size
, insize
, cost
;
1476 kind
= shl_sext_kind (left_rtx
, size_rtx
, &cost
);
1477 left
= INTVAL (left_rtx
);
1478 size
= INTVAL (size_rtx
);
1479 insize
= size
- left
;
1487 int ext
= kind
& 1 ? 8 : 16;
1488 int shift2
= size
- ext
;
1490 /* Don't expand fine-grained when combining, because that will
1491 make the pattern fail. */
1492 if (! rtx_equal_function_value_matters
1493 && ! reload_in_progress
&& ! reload_completed
)
1495 emit_insn (gen_shl_sext_ext (dest
, source
, left_rtx
, size_rtx
));
1496 emit_insn (gen_movsi (dest
, source
));
1500 emit_insn (gen_movsi (dest
, source
));
1504 operands
[2] = GEN_INT (ext
- insize
);
1505 gen_shifty_hi_op (ASHIFT
, operands
);
1508 ? gen_extendqisi2(dest
, gen_lowpart (QImode
, dest
))
1509 : gen_extendhisi2(dest
, gen_lowpart (HImode
, dest
)));
1514 operands
[2] = GEN_INT (shift2
);
1515 gen_shifty_op (ASHIFT
, operands
);
1522 if (EXT_SHIFT_SIGNED (shift2
))
1524 operands
[2] = GEN_INT (shift2
+ 1);
1525 gen_shifty_op (ASHIFT
, operands
);
1526 operands
[2] = GEN_INT (1);
1527 gen_shifty_op (ASHIFTRT
, operands
);
1530 operands
[2] = GEN_INT (shift2
);
1531 gen_shifty_hi_op (ASHIFT
, operands
);
1535 operands
[2] = GEN_INT (-shift2
);
1536 gen_shifty_hi_op (LSHIFTRT
, operands
);
1538 emit_insn (size
<= 8
1539 ? gen_extendqisi2 (dest
, gen_lowpart (QImode
, dest
))
1540 : gen_extendhisi2 (dest
, gen_lowpart (HImode
, dest
)));
1547 emit_insn (gen_shl_sext_ext (dest
, source
, GEN_INT (16 - insize
),
1549 /* Don't use gen_ashrsi3 because it generates new pseudos. */
1551 gen_ashift (ASHIFTRT
, 1, dest
);
1556 /* Don't expand fine-grained when combining, because that will
1557 make the pattern fail. */
1558 if (! rtx_equal_function_value_matters
1559 && ! reload_in_progress
&& ! reload_completed
)
1561 emit_insn (gen_shl_sext_ext (dest
, source
, left_rtx
, size_rtx
));
1562 emit_insn (gen_movsi (dest
, source
));
1565 emit_insn (gen_andsi3 (dest
, source
, GEN_INT ((1 << insize
) - 1)));
1566 emit_insn (gen_xorsi3 (dest
, dest
, GEN_INT (1 << (insize
- 1))));
1567 emit_insn (gen_addsi3 (dest
, dest
, GEN_INT (-1 << (insize
- 1))));
1569 operands
[2] = kind
== 7 ? GEN_INT (left
+ 1) : left_rtx
;
1570 gen_shifty_op (ASHIFT
, operands
);
1572 emit_insn (gen_ashrsi3_k (dest
, dest
, GEN_INT (1)));
1580 /* The SH cannot load a large constant into a register, constants have to
1581 come from a pc relative load. The reference of a pc relative load
1582 instruction must be less than 1k infront of the instruction. This
1583 means that we often have to dump a constant inside a function, and
1584 generate code to branch around it.
1586 It is important to minimize this, since the branches will slow things
1587 down and make things bigger.
1589 Worst case code looks like:
1607 We fix this by performing a scan before scheduling, which notices which
1608 instructions need to have their operands fetched from the constant table
1609 and builds the table.
1613 scan, find an instruction which needs a pcrel move. Look forward, find the
1614 last barrier which is within MAX_COUNT bytes of the requirement.
1615 If there isn't one, make one. Process all the instructions between
1616 the find and the barrier.
1618 In the above example, we can tell that L3 is within 1k of L1, so
1619 the first move can be shrunk from the 3 insn+constant sequence into
1620 just 1 insn, and the constant moved to L3 to make:
1631 Then the second move becomes the target for the shortening process. */
1635 rtx value
; /* Value in table. */
1636 rtx label
; /* Label of value. */
1637 enum machine_mode mode
; /* Mode of value. */
1640 /* The maximum number of constants that can fit into one pool, since
1641 the pc relative range is 0...1020 bytes and constants are at least 4
1644 #define MAX_POOL_SIZE (1020/4)
1645 static pool_node pool_vector
[MAX_POOL_SIZE
];
1646 static int pool_size
;
1648 /* ??? If we need a constant in HImode which is the truncated value of a
1649 constant we need in SImode, we could combine the two entries thus saving
1650 two bytes. Is this common enough to be worth the effort of implementing
1653 /* ??? This stuff should be done at the same time that we shorten branches.
1654 As it is now, we must assume that all branches are the maximum size, and
1655 this causes us to almost always output constant pools sooner than
1658 /* Add a constant to the pool and return its label. */
1661 add_constant (x
, mode
)
1663 enum machine_mode mode
;
1668 /* First see if we've already got it. */
1669 for (i
= 0; i
< pool_size
; i
++)
1671 if (x
->code
== pool_vector
[i
].value
->code
1672 && mode
== pool_vector
[i
].mode
)
1674 if (x
->code
== CODE_LABEL
)
1676 if (XINT (x
, 3) != XINT (pool_vector
[i
].value
, 3))
1679 if (rtx_equal_p (x
, pool_vector
[i
].value
))
1680 return pool_vector
[i
].label
;
1684 /* Need a new one. */
1685 pool_vector
[pool_size
].value
= x
;
1686 lab
= gen_label_rtx ();
1687 pool_vector
[pool_size
].mode
= mode
;
1688 pool_vector
[pool_size
].label
= lab
;
1693 /* Output the literal table. */
1702 /* Do two passes, first time dump out the HI sized constants. */
1704 for (i
= 0; i
< pool_size
; i
++)
1706 pool_node
*p
= &pool_vector
[i
];
1708 if (p
->mode
== HImode
)
1712 scan
= emit_insn_after (gen_align_2 (), scan
);
1715 scan
= emit_label_after (p
->label
, scan
);
1716 scan
= emit_insn_after (gen_consttable_2 (p
->value
), scan
);
1722 for (i
= 0; i
< pool_size
; i
++)
1724 pool_node
*p
= &pool_vector
[i
];
1735 scan
= emit_label_after (gen_label_rtx (), scan
);
1736 scan
= emit_insn_after (gen_align_4 (), scan
);
1738 scan
= emit_label_after (p
->label
, scan
);
1739 scan
= emit_insn_after (gen_consttable_4 (p
->value
), scan
);
1746 scan
= emit_label_after (gen_label_rtx (), scan
);
1747 scan
= emit_insn_after (gen_align_4 (), scan
);
1749 scan
= emit_label_after (p
->label
, scan
);
1750 scan
= emit_insn_after (gen_consttable_8 (p
->value
), scan
);
1758 scan
= emit_insn_after (gen_consttable_end (), scan
);
1759 scan
= emit_barrier_after (scan
);
1763 /* Return non-zero if constant would be an ok source for a
1764 mov.w instead of a mov.l. */
1770 return (GET_CODE (src
) == CONST_INT
1771 && INTVAL (src
) >= -32768
1772 && INTVAL (src
) <= 32767);
1775 /* Non-zero if the insn is a move instruction which needs to be fixed. */
1777 /* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
1778 CONST_DOUBLE input value is CONST_OK_FOR_I. For a SFmode move, we don't
1779 need to fix it if the input value is CONST_OK_FOR_I. */
1785 if (GET_CODE (insn
) == INSN
)
1787 rtx pat
= PATTERN (insn
);
1788 if (GET_CODE (pat
) == PARALLEL
)
1789 pat
= XVECEXP (pat
, 0, 0);
1790 if (GET_CODE (pat
) == SET
1791 /* We can load any 8 bit value if we don't care what the high
1792 order bits end up as. */
1793 && GET_MODE (SET_DEST (pat
)) != QImode
1794 && CONSTANT_P (SET_SRC (pat
))
1795 && ! (GET_CODE (SET_SRC (pat
)) == CONST_DOUBLE
1796 && (fp_zero_operand (SET_SRC (pat
))
1797 || fp_one_operand (SET_SRC (pat
)))
1798 && GET_CODE (SET_DEST (pat
)) == REG
1799 && REGNO (SET_DEST (pat
)) >= FIRST_FP_REG
1800 && REGNO (SET_DEST (pat
)) <= LAST_FP_REG
)
1801 && (GET_CODE (SET_SRC (pat
)) != CONST_INT
1802 || ! CONST_OK_FOR_I (INTVAL (SET_SRC (pat
)))))
1809 /* Find the last barrier from insn FROM which is close enough to hold the
1810 constant pool. If we can't find one, then create one near the end of
1813 /* ??? It would be good to put constant pool tables between a case jump and
1814 the jump table. This fails for two reasons. First, there is no
1815 barrier after the case jump. This is a bug in the casesi pattern.
1816 Second, inserting the table here may break the mova instruction that
1817 loads the jump table address, by moving the jump table too far away.
1818 We fix that problem by never outputting the constant pool between a mova
1829 rtx found_barrier
= 0;
1834 /* For HImode: range is 510, add 4 because pc counts from address of
1835 second instruction after this one, subtract 2 for the jump instruction
1836 that we may need to emit before the table, subtract 2 for the instruction
1837 that fills the jump delay slot (in very rare cases, reorg will take an
1838 instruction from after the constant pool or will leave the delay slot
1839 empty). This gives 510.
1840 For SImode: range is 1020, add 4 because pc counts from address of
1841 second instruction after this one, subtract 2 in case pc is 2 byte
1842 aligned, subtract 2 for the jump instruction that we may need to emit
1843 before the table, subtract 2 for the instruction that fills the jump
1844 delay slot. This gives 1018. */
1846 /* If not optimizing, then it is possible that the jump instruction we add
1847 won't be shortened, and thus will have a length of 14 instead of 2.
1848 We must adjust the limits downwards to account for this, giving a limit
1849 of 1008 for SImode and 500 for HImode. */
1862 /* If not optimizing for space, then the constant pool will be
1863 aligned to a 4 to 16 byte boundary. We must make room for that
1864 alignment that by reducing the limits.
1865 ??? It would be better to not align the constant pool, but
1866 ASM_OUTPUT_ALIGN_CODE does not make any provision for basing the
1867 alignment on the instruction. */
1869 if (! TARGET_SMALLCODE
)
1871 if (TARGET_SH3
|| TARGET_SH3E
)
1883 while (from
&& count_si
< si_limit
&& count_hi
< hi_limit
)
1885 int inc
= get_attr_length (from
);
1887 if (GET_CODE (from
) == BARRIER
)
1888 found_barrier
= from
;
1890 if (broken_move (from
))
1892 rtx pat
= PATTERN (from
);
1893 rtx src
= SET_SRC (pat
);
1894 rtx dst
= SET_DEST (pat
);
1895 enum machine_mode mode
= GET_MODE (dst
);
1897 /* We must explicitly check the mode, because sometimes the
1898 front end will generate code to load unsigned constants into
1899 HImode targets without properly sign extending them. */
1900 if (mode
== HImode
|| (mode
== SImode
&& hi_const (src
)))
1903 /* We put the short constants before the long constants, so
1904 we must count the length of short constants in the range
1905 for the long constants. */
1906 /* ??? This isn't optimal, but is easy to do. */
1914 if (GET_CODE (from
) == INSN
1915 && GET_CODE (PATTERN (from
)) == SET
1916 && GET_CODE (SET_SRC (PATTERN (from
))) == UNSPEC
1917 && XINT (SET_SRC (PATTERN (from
)), 1) == 1)
1919 else if (GET_CODE (from
) == JUMP_INSN
1920 && (GET_CODE (PATTERN (from
)) == ADDR_VEC
1921 || GET_CODE (PATTERN (from
)) == ADDR_DIFF_VEC
))
1928 from
= NEXT_INSN (from
);
1931 /* Insert the constant pool table before the mova instruction, to prevent
1932 the mova label reference from going out of range. */
1936 if (! found_barrier
)
1938 /* We didn't find a barrier in time to dump our stuff,
1939 so we'll make one. */
1940 rtx label
= gen_label_rtx ();
1942 /* If we exceeded the range, then we must back up over the last
1943 instruction we looked at. Otherwise, we just need to undo the
1944 NEXT_INSN at the end of the loop. */
1945 if (count_hi
> hi_limit
|| count_si
> si_limit
)
1946 from
= PREV_INSN (PREV_INSN (from
));
1948 from
= PREV_INSN (from
);
1950 /* Walk back to be just before any jump or label.
1951 Putting it before a label reduces the number of times the branch
1952 around the constant pool table will be hit. Putting it before
1953 a jump makes it more likely that the bra delay slot will be
1955 while (GET_CODE (from
) == JUMP_INSN
|| GET_CODE (from
) == NOTE
1956 || GET_CODE (from
) == CODE_LABEL
)
1957 from
= PREV_INSN (from
);
1959 from
= emit_jump_insn_after (gen_jump (label
), from
);
1960 JUMP_LABEL (from
) = label
;
1961 LABEL_NUSES (label
) = 1;
1962 found_barrier
= emit_barrier_after (from
);
1963 emit_label_after (label
, found_barrier
);
1966 return found_barrier
;
1969 /* If the instruction INSN is implemented by a special function, and we can
1970 positively find the register that is used to call the sfunc, and this
1971 register is not used anywhere else in this instruction - except as the
1972 destination of a set, return this register; else, return 0. */
1974 sfunc_uses_reg (insn
)
1978 rtx pattern
, part
, reg_part
, reg
;
1980 if (GET_CODE (insn
) != INSN
)
1982 pattern
= PATTERN (insn
);
1983 if (GET_CODE (pattern
) != PARALLEL
|| get_attr_type (insn
) != TYPE_SFUNC
)
1986 for (reg_part
= 0, i
= XVECLEN (pattern
, 0) - 1; i
>= 1; i
--)
1988 part
= XVECEXP (pattern
, 0, i
);
1989 if (GET_CODE (part
) == USE
)
1994 reg
= XEXP (reg_part
, 0);
1995 for (i
= XVECLEN (pattern
, 0) - 1; i
>= 0; i
--)
1997 part
= XVECEXP (pattern
, 0, i
);
1998 if (part
== reg_part
)
2000 if (reg_mentioned_p (reg
, ((GET_CODE (part
) == SET
2001 && GET_CODE (SET_DEST (part
)) == REG
)
2002 ? SET_SRC (part
) : part
)))
2008 /* See if the only way in which INSN uses REG is by calling it, or by
2009 setting it while calling it. Set *SET to a SET rtx if the register
2013 noncall_uses_reg (reg
, insn
, set
)
2022 reg2
= sfunc_uses_reg (insn
);
2023 if (reg2
&& REGNO (reg2
) == REGNO (reg
))
2025 pattern
= single_set (insn
);
2027 && GET_CODE (SET_DEST (pattern
)) == REG
2028 && REGNO (reg
) == REGNO (SET_DEST (pattern
)))
2032 if (GET_CODE (insn
) != CALL_INSN
)
2034 /* We don't use rtx_equal_p because we don't care if the mode is
2036 pattern
= single_set (insn
);
2038 && GET_CODE (SET_DEST (pattern
)) == REG
2039 && REGNO (reg
) == REGNO (SET_DEST (pattern
)))
2045 par
= PATTERN (insn
);
2046 if (GET_CODE (par
) == PARALLEL
)
2047 for (i
= XVECLEN (par
, 0) - 1; i
>= 0; i
--)
2049 part
= XVECEXP (par
, 0, i
);
2050 if (GET_CODE (part
) != SET
&& reg_mentioned_p (reg
, part
))
2053 return reg_mentioned_p (reg
, SET_SRC (pattern
));
2059 pattern
= PATTERN (insn
);
2061 if (GET_CODE (pattern
) == PARALLEL
)
2065 for (i
= XVECLEN (pattern
, 0) - 1; i
>= 1; i
--)
2066 if (reg_mentioned_p (reg
, XVECEXP (pattern
, 0, i
)))
2068 pattern
= XVECEXP (pattern
, 0, 0);
2071 if (GET_CODE (pattern
) == SET
)
2073 if (reg_mentioned_p (reg
, SET_DEST (pattern
)))
2075 /* We don't use rtx_equal_p, because we don't care if the
2076 mode is different. */
2077 if (GET_CODE (SET_DEST (pattern
)) != REG
2078 || REGNO (reg
) != REGNO (SET_DEST (pattern
)))
2084 pattern
= SET_SRC (pattern
);
2087 if (GET_CODE (pattern
) != CALL
2088 || GET_CODE (XEXP (pattern
, 0)) != MEM
2089 || ! rtx_equal_p (reg
, XEXP (XEXP (pattern
, 0), 0)))
2095 /* Exported to toplev.c.
2097 Do a final pass over the function, just before delayed branch
2101 machine_dependent_reorg (first
)
2106 /* If relaxing, generate pseudo-ops to associate function calls with
2107 the symbols they call. It does no harm to not generate these
2108 pseudo-ops. However, when we can generate them, it enables to
2109 linker to potentially relax the jsr to a bsr, and eliminate the
2110 register load and, possibly, the constant pool entry. */
2114 /* Remove all REG_LABEL notes. We want to use them for our own
2115 purposes. This works because none of the remaining passes
2116 need to look at them.
2118 ??? But it may break in the future. We should use a machine
2119 dependent REG_NOTE, or some other approach entirely. */
2120 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
2122 if (GET_RTX_CLASS (GET_CODE (insn
)) == 'i')
2126 while ((note
= find_reg_note (insn
, REG_LABEL
, NULL_RTX
)) != 0)
2127 remove_note (insn
, note
);
2131 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
2133 rtx pattern
, reg
, link
, set
, scan
, dies
, label
;
2134 int rescan
= 0, foundinsn
= 0;
2136 if (GET_CODE (insn
) == CALL_INSN
)
2138 pattern
= PATTERN (insn
);
2140 if (GET_CODE (pattern
) == PARALLEL
)
2141 pattern
= XVECEXP (pattern
, 0, 0);
2142 if (GET_CODE (pattern
) == SET
)
2143 pattern
= SET_SRC (pattern
);
2145 if (GET_CODE (pattern
) != CALL
2146 || GET_CODE (XEXP (pattern
, 0)) != MEM
)
2149 reg
= XEXP (XEXP (pattern
, 0), 0);
2153 reg
= sfunc_uses_reg (insn
);
2158 if (GET_CODE (reg
) != REG
)
2161 /* This is a function call via REG. If the only uses of REG
2162 between the time that it is set and the time that it dies
2163 are in function calls, then we can associate all the
2164 function calls with the setting of REG. */
2166 for (link
= LOG_LINKS (insn
); link
; link
= XEXP (link
, 1))
2168 if (REG_NOTE_KIND (link
) != 0)
2170 set
= single_set (XEXP (link
, 0));
2171 if (set
&& rtx_equal_p (reg
, SET_DEST (set
)))
2173 link
= XEXP (link
, 0);
2180 /* ??? Sometimes global register allocation will have
2181 deleted the insn pointed to by LOG_LINKS. Try
2182 scanning backward to find where the register is set. */
2183 for (scan
= PREV_INSN (insn
);
2184 scan
&& GET_CODE (scan
) != CODE_LABEL
;
2185 scan
= PREV_INSN (scan
))
2187 if (GET_RTX_CLASS (GET_CODE (scan
)) != 'i')
2190 if (! reg_mentioned_p (reg
, scan
))
2193 if (noncall_uses_reg (reg
, scan
, &set
))
2207 /* The register is set at LINK. */
2209 /* We can only optimize the function call if the register is
2210 being set to a symbol. In theory, we could sometimes
2211 optimize calls to a constant location, but the assembler
2212 and linker do not support that at present. */
2213 if (GET_CODE (SET_SRC (set
)) != SYMBOL_REF
2214 && GET_CODE (SET_SRC (set
)) != LABEL_REF
)
2217 /* Scan forward from LINK to the place where REG dies, and
2218 make sure that the only insns which use REG are
2219 themselves function calls. */
2221 /* ??? This doesn't work for call targets that were allocated
2222 by reload, since there may not be a REG_DEAD note for the
2226 for (scan
= NEXT_INSN (link
); scan
; scan
= NEXT_INSN (scan
))
2230 /* Don't try to trace forward past a CODE_LABEL if we haven't
2231 seen INSN yet. Ordinarily, we will only find the setting insn
2232 in LOG_LINKS if it is in the same basic block. However,
2233 cross-jumping can insert code labels in between the load and
2234 the call, and can result in situations where a single call
2235 insn may have two targets depending on where we came from. */
2237 if (GET_CODE (scan
) == CODE_LABEL
&& ! foundinsn
)
2240 if (GET_RTX_CLASS (GET_CODE (scan
)) != 'i')
2243 /* Don't try to trace forward past a JUMP. To optimize
2244 safely, we would have to check that all the
2245 instructions at the jump destination did not use REG. */
2247 if (GET_CODE (scan
) == JUMP_INSN
)
2250 if (! reg_mentioned_p (reg
, scan
))
2253 if (noncall_uses_reg (reg
, scan
, &scanset
))
2260 && (GET_CODE (scan
) == CALL_INSN
|| sfunc_uses_reg (scan
)))
2262 /* There is a function call to this register other
2263 than the one we are checking. If we optimize
2264 this call, we need to rescan again below. */
2268 /* ??? We shouldn't have to worry about SCANSET here.
2269 We should just be able to check for a REG_DEAD note
2270 on a function call. However, the REG_DEAD notes are
2271 apparently not dependable around libcalls; c-torture
2272 execute/920501-2 is a test case. If SCANSET is set,
2273 then this insn sets the register, so it must have
2274 died earlier. Unfortunately, this will only handle
2275 the cases in which the register is, in fact, set in a
2278 /* ??? We shouldn't have to use FOUNDINSN here.
2279 However, the LOG_LINKS fields are apparently not
2280 entirely reliable around libcalls;
2281 newlib/libm/math/e_pow.c is a test case. Sometimes
2282 an insn will appear in LOG_LINKS even though it is
2283 not the most recent insn which sets the register. */
2287 || find_reg_note (scan
, REG_DEAD
, reg
)))
2296 /* Either there was a branch, or some insn used REG
2297 other than as a function call address. */
2301 /* Create a code label, and put it in a REG_LABEL note on
2302 the insn which sets the register, and on each call insn
2303 which uses the register. In final_prescan_insn we look
2304 for the REG_LABEL notes, and output the appropriate label
2307 label
= gen_label_rtx ();
2308 REG_NOTES (link
) = gen_rtx (EXPR_LIST
, REG_LABEL
, label
,
2310 REG_NOTES (insn
) = gen_rtx (EXPR_LIST
, REG_LABEL
, label
,
2319 scan
= NEXT_INSN (scan
);
2321 && ((GET_CODE (scan
) == CALL_INSN
2322 && reg_mentioned_p (reg
, scan
))
2323 || ((reg2
= sfunc_uses_reg (scan
))
2324 && REGNO (reg2
) == REGNO (reg
))))
2325 REG_NOTES (scan
) = gen_rtx (EXPR_LIST
, REG_LABEL
,
2326 label
, REG_NOTES (scan
));
2328 while (scan
!= dies
);
2333 /* Scan the function looking for move instructions which have to be
2334 changed to pc-relative loads and insert the literal tables. */
2336 for (insn
= first
; insn
; insn
= NEXT_INSN (insn
))
2338 if (broken_move (insn
))
2341 /* Scan ahead looking for a barrier to stick the constant table
2343 rtx barrier
= find_barrier (insn
);
2345 /* Now find all the moves between the points and modify them. */
2346 for (scan
= insn
; scan
!= barrier
; scan
= NEXT_INSN (scan
))
2348 if (broken_move (scan
))
2350 rtx
*patp
= &PATTERN (scan
), pat
= *patp
;
2355 enum machine_mode mode
;
2357 if (GET_CODE (pat
) == PARALLEL
)
2358 patp
= &XVECEXP (pat
, 0, 0), pat
= *patp
;
2359 src
= SET_SRC (pat
);
2360 dst
= SET_DEST (pat
);
2361 mode
= GET_MODE (dst
);
2363 if (mode
== SImode
&& hi_const (src
))
2368 while (GET_CODE (dst
) == SUBREG
)
2370 offset
+= SUBREG_WORD (dst
);
2371 dst
= SUBREG_REG (dst
);
2373 dst
= gen_rtx (REG
, HImode
, REGNO (dst
) + offset
);
2376 lab
= add_constant (src
, mode
);
2377 newsrc
= gen_rtx (MEM
, mode
,
2378 gen_rtx (LABEL_REF
, VOIDmode
, lab
));
2379 RTX_UNCHANGING_P (newsrc
) = 1;
2380 *patp
= gen_rtx (SET
, VOIDmode
, dst
, newsrc
);
2381 INSN_CODE (scan
) = -1;
2384 dump_table (barrier
);
2389 /* Dump out instruction addresses, which is useful for debugging the
2390 constant pool table stuff.
2392 If relaxing, output the label and pseudo-ops used to link together
2393 calls and the instruction which set the registers. */
2395 /* ??? This is unnecessary, and probably should be deleted. This makes
2396 the insn_addresses declaration above unnecessary. */
2398 /* ??? The addresses printed by this routine for insns are nonsense for
2399 insns which are inside of a sequence where none of the inner insns have
2400 variable length. This is because the second pass of shorten_branches
2401 does not bother to update them. */
2404 final_prescan_insn (insn
, opvec
, noperands
)
2409 if (TARGET_DUMPISIZE
)
2410 fprintf (asm_out_file
, "\n! at %04x\n", insn_addresses
[INSN_UID (insn
)]);
2416 note
= find_reg_note (insn
, REG_LABEL
, NULL_RTX
);
2421 pattern
= PATTERN (insn
);
2422 if (GET_CODE (pattern
) == PARALLEL
)
2423 pattern
= XVECEXP (pattern
, 0, 0);
2424 if (GET_CODE (pattern
) == CALL
2425 || (GET_CODE (pattern
) == SET
2426 && (GET_CODE (SET_SRC (pattern
)) == CALL
2427 || get_attr_type (insn
) == TYPE_SFUNC
)))
2428 fprintf (asm_out_file
, "\t.uses L%d\n",
2429 CODE_LABEL_NUMBER (XEXP (note
, 0)));
2430 else if (GET_CODE (pattern
) == SET
)
2431 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file
, "L",
2432 CODE_LABEL_NUMBER (XEXP (note
, 0)));
2439 /* Dump out any constants accumulated in the final pass. These will
2440 will only be labels. */
2443 output_jump_label_table ()
2449 fprintf (asm_out_file
, "\t.align 2\n");
2450 for (i
= 0; i
< pool_size
; i
++)
2452 pool_node
*p
= &pool_vector
[i
];
2454 ASM_OUTPUT_INTERNAL_LABEL (asm_out_file
, "L",
2455 CODE_LABEL_NUMBER (p
->label
));
2456 output_asm_insn (".long %O0", &p
->value
);
2464 /* A full frame looks like:
2468 [ if current_function_anonymous_args
2481 local-0 <- fp points here. */
2483 /* Number of bytes pushed for anonymous args, used to pass information
2484 between expand_prologue and expand_epilogue. */
2486 static int extra_push
;
2488 /* Adjust the stack by SIZE bytes. REG holds the rtl of the register
2489 to be adjusted, and TEMP, if nonnegative, holds the register number
2490 of a general register that we may clobber. */
2493 output_stack_adjust (size
, reg
, temp
)
2500 if (CONST_OK_FOR_I (size
))
2501 emit_insn (gen_addsi3 (reg
, reg
, GEN_INT (size
)));
2502 /* Try to do it with two partial adjustments; however, we must make
2503 sure that the stack is properly aligned at all times, in case
2504 an interrupt occurs between the two partial adjustments. */
2505 else if (CONST_OK_FOR_I (size
/ 2 & -4)
2506 && CONST_OK_FOR_I (size
- (size
/ 2 & -4)))
2508 emit_insn (gen_addsi3 (reg
, reg
, GEN_INT (size
/ 2 & -4)));
2509 emit_insn (gen_addsi3 (reg
, reg
, GEN_INT (size
- (size
/ 2 & -4))));
2515 /* If TEMP is invalid, we could temporarily save a general
2516 register to MACL. However, there is currently no need
2517 to handle this case, so just abort when we see it. */
2520 const_reg
= gen_rtx (REG
, SImode
, temp
);
2522 /* If SIZE is negative, subtract the positive value.
2523 This sometimes allows a constant pool entry to be shared
2524 between prologue and epilogue code. */
2527 emit_insn (gen_movsi (const_reg
, GEN_INT (-size
)));
2528 emit_insn (gen_subsi3 (reg
, reg
, const_reg
));
2532 emit_insn (gen_movsi (const_reg
, GEN_INT (size
)));
2533 emit_insn (gen_addsi3 (reg
, reg
, const_reg
));
2539 /* Output RTL to push register RN onto the stack. */
2546 if ((rn
>= FIRST_FP_REG
&& rn
<= LAST_FP_REG
)
2548 x
= emit_insn (gen_push_e (gen_rtx (REG
, SFmode
, rn
)));
2550 x
= emit_insn (gen_push (gen_rtx (REG
, SImode
, rn
)));
2552 REG_NOTES (x
) = gen_rtx (EXPR_LIST
, REG_INC
,
2553 gen_rtx(REG
, SImode
, STACK_POINTER_REGNUM
), 0);
2556 /* Output RTL to pop register RN from the stack. */
2563 if ((rn
>= FIRST_FP_REG
&& rn
<= LAST_FP_REG
)
2565 x
= emit_insn (gen_pop_e (gen_rtx (REG
, SFmode
, rn
)));
2567 x
= emit_insn (gen_pop (gen_rtx (REG
, SImode
, rn
)));
2569 REG_NOTES (x
) = gen_rtx (EXPR_LIST
, REG_INC
,
2570 gen_rtx(REG
, SImode
, STACK_POINTER_REGNUM
), 0);
2573 /* Generate code to push the regs specified in the mask, and return
2574 the number of bytes the insns take. */
2577 push_regs (mask
, mask2
)
2582 for (i
= 0; i
< 32; i
++)
2583 if (mask
& (1 << i
))
2585 for (i
= 32; i
< FIRST_PSEUDO_REGISTER
; i
++)
2586 if (mask2
& (1 << (i
- 32)))
2590 /* Work out the registers which need to be saved, both as a mask and a
2593 If doing a pragma interrupt function, then push all regs used by the
2594 function, and if we call another function (we can tell by looking at PR),
2595 make sure that all the regs it clobbers are safe too. */
2598 calc_live_regs (count_ptr
, live_regs_mask2
)
2600 int *live_regs_mask2
;
2603 int live_regs_mask
= 0;
2606 *live_regs_mask2
= 0;
2607 for (reg
= 0; reg
< FIRST_PSEUDO_REGISTER
; reg
++)
2609 if (pragma_interrupt
&& ! pragma_trapa
)
2611 /* Need to save all the regs ever live. */
2612 if ((regs_ever_live
[reg
]
2613 || (call_used_regs
[reg
]
2614 && (! fixed_regs
[reg
] || reg
== MACH_REG
|| reg
== MACL_REG
)
2615 && regs_ever_live
[PR_REG
]))
2616 && reg
!= STACK_POINTER_REGNUM
&& reg
!= ARG_POINTER_REGNUM
2617 && reg
!= RETURN_ADDRESS_POINTER_REGNUM
2618 && reg
!= T_REG
&& reg
!= GBR_REG
)
2621 *live_regs_mask2
|= 1 << (reg
- 32);
2623 live_regs_mask
|= 1 << reg
;
2629 /* Only push those regs which are used and need to be saved. */
2630 if (regs_ever_live
[reg
] && ! call_used_regs
[reg
])
2633 *live_regs_mask2
|= 1 << (reg
- 32);
2635 live_regs_mask
|= (1 << reg
);
2642 return live_regs_mask
;
2645 /* Code to generate prologue and epilogue sequences */
2648 sh_expand_prologue ()
2652 int live_regs_mask2
;
2653 live_regs_mask
= calc_live_regs (&d
, &live_regs_mask2
);
2655 /* We have pretend args if we had an object sent partially in registers
2656 and partially on the stack, e.g. a large structure. */
2657 output_stack_adjust (-current_function_pretend_args_size
,
2658 stack_pointer_rtx
, 3);
2662 /* This is set by SETUP_VARARGS to indicate that this is a varargs
2663 routine. Clear it here so that the next function isn't affected. */
2664 if (current_function_anonymous_args
)
2666 current_function_anonymous_args
= 0;
2668 /* This is not used by the SH3E calling convention */
2671 /* Push arg regs as if they'd been provided by caller in stack. */
2672 for (i
= 0; i
< NPARM_REGS(SImode
); i
++)
2674 int rn
= NPARM_REGS(SImode
) + FIRST_PARM_REG
- i
- 1;
2675 if (i
> (NPARM_REGS(SImode
)
2676 - current_function_args_info
.arg_count
[(int) SH_ARG_INT
]
2677 - current_function_varargs
))
2685 /* If we're supposed to switch stacks at function entry, do so now. */
2687 emit_insn (gen_sp_switch_1 ());
2689 push_regs (live_regs_mask
, live_regs_mask2
);
2691 output_stack_adjust (-get_frame_size (), stack_pointer_rtx
, 3);
2693 if (frame_pointer_needed
)
2694 emit_insn (gen_movsi (frame_pointer_rtx
, stack_pointer_rtx
));
2698 sh_expand_epilogue ()
2703 int live_regs_mask2
;
2704 live_regs_mask
= calc_live_regs (&d
, &live_regs_mask2
);
2706 if (frame_pointer_needed
)
2708 /* We deliberately make the add dependent on the frame_pointer,
2709 to ensure that instruction scheduling won't move the stack pointer
2710 adjust before instructions reading from the frame. This can fail
2711 if there is an interrupt which then writes to the stack. */
2712 output_stack_adjust (get_frame_size (), frame_pointer_rtx
, 7);
2713 emit_insn (gen_movsi (stack_pointer_rtx
, frame_pointer_rtx
));
2716 output_stack_adjust (get_frame_size (), stack_pointer_rtx
, 7);
2718 /* Pop all the registers. */
2720 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
2722 int j
= (FIRST_PSEUDO_REGISTER
- 1) - i
;
2723 if (j
< 32 && (live_regs_mask
& (1 << j
)))
2725 else if (j
>= 32 && (live_regs_mask2
& (1 << (j
- 32))))
2729 output_stack_adjust (extra_push
+ current_function_pretend_args_size
,
2730 stack_pointer_rtx
, 7);
2732 /* Switch back to the normal stack if necessary. */
2734 emit_insn (gen_sp_switch_2 ());
2737 /* Clear variables at function end. */
2740 function_epilogue (stream
, size
)
2744 trap_exit
= pragma_interrupt
= pragma_trapa
= pragma_nosave_low_regs
= 0;
2745 sp_switch
= NULL_RTX
;
2749 sh_builtin_saveregs (arglist
)
2752 tree fntype
= TREE_TYPE (current_function_decl
);
2753 /* First unnamed integer register. */
2754 int first_intreg
= current_function_args_info
.arg_count
[(int) SH_ARG_INT
];
2755 /* Number of integer registers we need to save. */
2756 int n_intregs
= MAX (0, NPARM_REGS (SImode
) - first_intreg
);
2757 /* First unnamed SFmode float reg */
2758 int first_floatreg
= current_function_args_info
.arg_count
[(int) SH_ARG_FLOAT
];
2759 /* Number of SFmode float regs to save. */
2760 int n_floatregs
= MAX (0, NPARM_REGS (SFmode
) - first_floatreg
);
2761 int ptrsize
= GET_MODE_SIZE (Pmode
);
2762 rtx valist
, regbuf
, fpregs
;
2765 /* Allocate block of memory for the regs. */
2766 /* ??? If n_intregs + n_floatregs == 0, should we allocate at least 1 byte?
2767 Or can assign_stack_local accept a 0 SIZE argument? */
2768 bufsize
= (n_intregs
* UNITS_PER_WORD
) + (n_floatregs
* UNITS_PER_WORD
);
2770 regbuf
= assign_stack_local (BLKmode
, bufsize
, 0);
2771 MEM_IN_STRUCT_P (regbuf
) = 1;
2774 This is optimized to only save the regs that are necessary. Explicitly
2775 named args need not be saved. */
2777 move_block_from_reg (BASE_ARG_REG (SImode
) + first_intreg
,
2778 gen_rtx (MEM
, BLKmode
,
2779 plus_constant (XEXP (regbuf
, 0),
2780 n_floatregs
* UNITS_PER_WORD
)),
2781 n_intregs
, n_intregs
* UNITS_PER_WORD
);
2784 This is optimized to only save the regs that are necessary. Explicitly
2785 named args need not be saved.
2786 We explicitly build a pointer to the buffer because it halves the insn
2787 count when not optimizing (otherwise the pointer is built for each reg
2790 fpregs
= gen_reg_rtx (Pmode
);
2791 emit_move_insn (fpregs
, XEXP (regbuf
, 0));
2792 for (regno
= first_floatreg
; regno
< NPARM_REGS (SFmode
); regno
++)
2793 emit_move_insn (gen_rtx (MEM
, SFmode
,
2794 plus_constant (fpregs
,
2795 GET_MODE_SIZE (SFmode
)
2796 * (regno
- first_floatreg
))),
2797 gen_rtx (REG
, SFmode
,
2798 BASE_ARG_REG (SFmode
) + regno
));
2800 /* Return the address of the regbuf. */
2801 return XEXP (regbuf
, 0);
2804 /* Define the offset between two registers, one to be eliminated, and
2805 the other its replacement, at the start of a routine. */
2808 initial_elimination_offset (from
, to
)
2813 int total_saved_regs_space
;
2814 int total_auto_space
= get_frame_size ();
2816 int live_regs_mask
, live_regs_mask2
;
2817 live_regs_mask
= calc_live_regs (®s_saved
, &live_regs_mask2
);
2819 total_saved_regs_space
= (regs_saved
) * 4;
2821 if (from
== ARG_POINTER_REGNUM
&& to
== FRAME_POINTER_REGNUM
)
2822 return total_saved_regs_space
+ total_auto_space
;
2824 if (from
== ARG_POINTER_REGNUM
&& to
== STACK_POINTER_REGNUM
)
2825 return total_saved_regs_space
+ total_auto_space
;
2827 /* Initial gap between fp and sp is 0. */
2828 if (from
== FRAME_POINTER_REGNUM
&& to
== STACK_POINTER_REGNUM
)
2831 if (from
== RETURN_ADDRESS_POINTER_REGNUM
2832 && (to
== FRAME_POINTER_REGNUM
|| to
== STACK_POINTER_REGNUM
))
2835 for (i
= PR_REG
+1; i
< 32; i
++)
2836 if (live_regs_mask
& (1 << i
))
2838 for (i
= 32; i
< FIRST_PSEUDO_REGISTER
; i
++)
2839 if (live_regs_mask2
& (1 << (i
- 32)))
2841 return n
+ total_auto_space
;
2847 /* Handle machine specific pragmas to be semi-compatible with Hitachi
2851 handle_pragma (file
, t
)
2856 register char *pname
;
2858 if (TREE_CODE (t
) != IDENTIFIER_NODE
)
2861 pname
= IDENTIFIER_POINTER (t
);
2862 if (strcmp (pname
, "interrupt") == 0)
2863 pragma_interrupt
= retval
= 1;
2864 else if (strcmp (pname
, "trapa") == 0)
2865 pragma_interrupt
= pragma_trapa
= retval
= 1;
2866 else if (strcmp (pname
, "nosave_low_regs") == 0)
2867 pragma_nosave_low_regs
= retval
= 1;
2871 /* Return nonzero if ATTR is a valid attribute for DECL.
2872 ATTRIBUTES are any existing attributes and ARGS are the arguments
2875 Supported attributes:
2877 interrupt_handler -- specifies this function is an interrupt handler.
2879 sp_switch -- specifies an alternate stack for an interrupt handler
2882 trap_exit -- use a trapa to exit an interrupt function intead of
2883 an rte instruction. */
2886 sh_valid_machine_decl_attribute (decl
, attributes
, attr
, args
)
2894 if (TREE_CODE (decl
) != FUNCTION_DECL
)
2897 if (is_attribute_p ("interrupt_handler", attr
))
2899 pragma_interrupt
= 1;
2903 if (is_attribute_p ("sp_switch", attr
))
2905 /* The sp_switch attribute only has meaning for interrupt functions. */
2906 if (!pragma_interrupt
)
2909 /* sp_switch must have an argument. */
2910 if (!args
|| TREE_CODE (args
) != TREE_LIST
)
2913 /* The argument must be a constant string. */
2914 if (TREE_CODE (TREE_VALUE (args
)) != STRING_CST
)
2917 sp_switch
= gen_rtx (SYMBOL_REF
, VOIDmode
,
2918 TREE_STRING_POINTER (TREE_VALUE (args
)));
2922 if (is_attribute_p ("trap_exit", attr
))
2924 /* The trap_exit attribute only has meaning for interrupt functions. */
2925 if (!pragma_interrupt
)
2928 /* trap_exit must have an argument. */
2929 if (!args
|| TREE_CODE (args
) != TREE_LIST
)
2932 /* The argument must be a constant integer. */
2933 if (TREE_CODE (TREE_VALUE (args
)) != INTEGER_CST
)
2936 trap_exit
= TREE_INT_CST_LOW (TREE_VALUE (args
));
2942 /* Predicates used by the templates. */
2944 /* Returns 1 if OP is MACL, MACH or PR. The input must be a REG rtx.
2945 Used only in general_movsrc_operand. */
2948 system_reg_operand (op
, mode
)
2950 enum machine_mode mode
;
2962 /* Returns 1 if OP can be source of a simple move operation.
2963 Same as general_operand, but a LABEL_REF is valid, PRE_DEC is
2964 invalid as are subregs of system registers. */
2967 general_movsrc_operand (op
, mode
)
2969 enum machine_mode mode
;
2971 if (GET_CODE (op
) == MEM
)
2973 rtx inside
= XEXP (op
, 0);
2974 if (GET_CODE (inside
) == CONST
)
2975 inside
= XEXP (inside
, 0);
2977 if (GET_CODE (inside
) == LABEL_REF
)
2980 if (GET_CODE (inside
) == PLUS
2981 && GET_CODE (XEXP (inside
, 0)) == LABEL_REF
2982 && GET_CODE (XEXP (inside
, 1)) == CONST_INT
)
2985 /* Only post inc allowed. */
2986 if (GET_CODE (inside
) == PRE_DEC
)
2990 if ((mode
== QImode
|| mode
== HImode
)
2991 && (GET_CODE (op
) == SUBREG
2992 && GET_CODE (XEXP (op
, 0)) == REG
2993 && system_reg_operand (XEXP (op
, 0), mode
)))
2996 return general_operand (op
, mode
);
2999 /* Returns 1 if OP can be a destination of a move.
3000 Same as general_operand, but no preinc allowed. */
3003 general_movdst_operand (op
, mode
)
3005 enum machine_mode mode
;
3007 /* Only pre dec allowed. */
3008 if (GET_CODE (op
) == MEM
&& GET_CODE (XEXP (op
, 0)) == POST_INC
)
3011 return general_operand (op
, mode
);
3014 /* Returns 1 if OP is a normal arithmetic register. */
3017 arith_reg_operand (op
, mode
)
3019 enum machine_mode mode
;
3021 if (register_operand (op
, mode
))
3025 if (GET_CODE (op
) == REG
)
3027 else if (GET_CODE (op
) == SUBREG
&& GET_CODE (SUBREG_REG (op
)) == REG
)
3028 regno
= REGNO (SUBREG_REG (op
));
3032 return (regno
!= T_REG
&& regno
!= PR_REG
&& regno
!= FPUL_REG
3033 && regno
!= MACH_REG
&& regno
!= MACL_REG
);
3038 /* Returns 1 if OP is a valid source operand for an arithmetic insn. */
3041 arith_operand (op
, mode
)
3043 enum machine_mode mode
;
3045 if (arith_reg_operand (op
, mode
))
3048 if (GET_CODE (op
) == CONST_INT
&& CONST_OK_FOR_I (INTVAL (op
)))
3054 /* Returns 1 if OP is a valid source operand for a compare insn. */
3057 arith_reg_or_0_operand (op
, mode
)
3059 enum machine_mode mode
;
3061 if (arith_reg_operand (op
, mode
))
3064 if (GET_CODE (op
) == CONST_INT
&& CONST_OK_FOR_N (INTVAL (op
)))
3070 /* Returns 1 if OP is a valid source operand for a logical operation. */
3073 logical_operand (op
, mode
)
3075 enum machine_mode mode
;
3077 if (arith_reg_operand (op
, mode
))
3080 if (GET_CODE (op
) == CONST_INT
&& CONST_OK_FOR_L (INTVAL (op
)))
3086 /* Nonzero if OP is a floating point value with value 0.0. */
3089 fp_zero_operand (op
)
3094 if (GET_MODE (op
) != SFmode
)
3097 REAL_VALUE_FROM_CONST_DOUBLE (r
, op
);
3098 return REAL_VALUES_EQUAL (r
, dconst0
) && ! REAL_VALUE_MINUS_ZERO (r
);
3101 /* Nonzero if OP is a floating point value with value 1.0. */
3109 if (GET_MODE (op
) != SFmode
)
3112 REAL_VALUE_FROM_CONST_DOUBLE (r
, op
);
3113 return REAL_VALUES_EQUAL (r
, dconst1
);
3116 /* Return non-zero if REG is not used after INSN.
3117 We assume REG is a reload reg, and therefore does
3118 not live past labels. It may live past calls or jumps though. */
3120 reg_unused_after (reg
, insn
)
3127 /* If the reg is set by this instruction, then it is safe for our
3128 case. Disregard the case where this is a store to memory, since
3129 we are checking a register used in the store address. */
3130 set
= single_set (insn
);
3131 if (set
&& GET_CODE (SET_DEST (set
)) != MEM
3132 && reg_overlap_mentioned_p (reg
, SET_DEST (set
)))
3135 while (insn
= NEXT_INSN (insn
))
3137 code
= GET_CODE (insn
);
3140 /* If this is a label that existed before reload, then the register
3141 if dead here. However, if this is a label added by reorg, then
3142 the register may still be live here. We can't tell the difference,
3143 so we just ignore labels completely. */
3144 if (code
== CODE_LABEL
)
3149 if (code
== JUMP_INSN
)
3152 /* If this is a sequence, we must handle them all at once.
3153 We could have for instance a call that sets the target register,
3154 and a insn in a delay slot that uses the register. In this case,
3155 we must return 0. */
3156 else if (code
== INSN
&& GET_CODE (PATTERN (insn
)) == SEQUENCE
)
3161 for (i
= 0; i
< XVECLEN (PATTERN (insn
), 0); i
++)
3163 rtx this_insn
= XVECEXP (PATTERN (insn
), 0, i
);
3164 rtx set
= single_set (this_insn
);
3166 if (GET_CODE (this_insn
) == CALL_INSN
)
3168 else if (GET_CODE (this_insn
) == JUMP_INSN
)
3170 if (INSN_ANNULLED_BRANCH_P (this_insn
))
3175 if (set
&& reg_overlap_mentioned_p (reg
, SET_SRC (set
)))
3177 if (set
&& reg_overlap_mentioned_p (reg
, SET_DEST (set
)))
3179 if (GET_CODE (SET_DEST (set
)) != MEM
)
3185 && reg_overlap_mentioned_p (reg
, PATTERN (this_insn
)))
3190 else if (code
== JUMP_INSN
)
3193 else if (GET_RTX_CLASS (code
) == 'i')
3195 rtx set
= single_set (insn
);
3197 if (set
&& reg_overlap_mentioned_p (reg
, SET_SRC (set
)))
3199 if (set
&& reg_overlap_mentioned_p (reg
, SET_DEST (set
)))
3200 return GET_CODE (SET_DEST (set
)) != MEM
;
3201 if (set
== 0 && reg_overlap_mentioned_p (reg
, PATTERN (insn
)))
3205 if (code
== CALL_INSN
&& call_used_regs
[REGNO (reg
)])