OSDN Git Service

(reg_unused_after): Handle JUMP_INSN inside a sequence.
[pf3gnuchains/gcc-fork.git] / gcc / config / sh / sh.c
1 /* Output routines for GCC for Hitachi Super-H.
2    Copyright (C) 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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)
9 any later version.
10
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.
15
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.  */
20
21 /* Contributed by Steve Chamberlain (sac@cygnus.com).
22    Improved by Jim Wilson (wilson@cygnus.com).  */
23
24 #include "config.h"
25
26 #include <stdio.h>
27
28 #include "rtl.h"
29 #include "tree.h"
30 #include "flags.h"
31 #include "insn-flags.h"
32 #include "expr.h"
33 #include "regs.h"
34 #include "hard-reg-set.h"
35 #include "output.h"
36 #include "insn-attr.h"
37
38 #define MSW (TARGET_LITTLE_ENDIAN ? 1 : 0)
39 #define LSW (TARGET_LITTLE_ENDIAN ? 0 : 1)
40
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.  */
44 int pragma_interrupt;
45
46 /* This is set by #pragma trapa, and is similar to the above, except that
47    the compiler doesn't emit code to preserve all registers.  */
48 static int pragma_trapa;
49
50 /* This is set by #pragma nosave_low_regs.  This is useful on the SH3,
51    which has a separate set of low regs for User and Supervisor modes.
52    This should only be used for the lowest level of interrupts.  Higher levels
53    of interrupts must save the registers in case they themselves are
54    interrupted.  */
55 int pragma_nosave_low_regs;
56
57 /* This is used for communication between SETUP_INCOMING_VARARGS and
58    sh_expand_prologue.  */
59 int current_function_anonymous_args;
60
61 /* Global variables from toplev.c and final.c that are used within, but
62    not declared in any header file.  */
63 extern char *version_string;
64 extern int *insn_addresses;
65
66 /* Global variables for machine-dependent things. */
67
68 /* Which cpu are we scheduling for.  */
69 enum processor_type sh_cpu;
70
71 /* Saved operands from the last compare to use when we generate an scc
72    or bcc insn.  */
73
74 rtx sh_compare_op0;
75 rtx sh_compare_op1;
76
77 /* Provides the class number of the smallest class containing
78    reg number.  */
79
80 int regno_reg_class[FIRST_PSEUDO_REGISTER] =
81 {
82   R0_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
83   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
84   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
85   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
86   GENERAL_REGS, PR_REGS, T_REGS, NO_REGS,
87   MAC_REGS, MAC_REGS, FPUL_REGS, NO_REGS,
88   FP0_REGS,FP_REGS, FP_REGS, FP_REGS,
89   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
90   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
91   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
92 };
93
94 /* Provide reg_class from a letter such as appears in the machine
95    description.  */
96
97 enum reg_class reg_class_from_letter[] =
98 {
99   /* a */ NO_REGS, /* b */ NO_REGS, /* c */ NO_REGS, /* d */ NO_REGS,
100   /* e */ NO_REGS, /* f */ FP_REGS, /* g */ NO_REGS, /* h */ NO_REGS,
101   /* i */ NO_REGS, /* j */ NO_REGS, /* k */ NO_REGS, /* l */ PR_REGS,
102   /* m */ NO_REGS, /* n */ NO_REGS, /* o */ NO_REGS, /* p */ NO_REGS,
103   /* q */ NO_REGS, /* r */ NO_REGS, /* s */ NO_REGS, /* t */ T_REGS,
104   /* u */ NO_REGS, /* v */ NO_REGS, /* w */ FP0_REGS, /* x */ MAC_REGS,
105   /* y */ FPUL_REGS, /* z */ R0_REGS
106 };
107 \f
108 /* Print the operand address in x to the stream.  */
109
110 void
111 print_operand_address (stream, x)
112      FILE *stream;
113      rtx x;
114 {
115   switch (GET_CODE (x))
116     {
117     case REG:
118       fprintf (stream, "@%s", reg_names[REGNO (x)]);
119       break;
120
121     case PLUS:
122       {
123         rtx base = XEXP (x, 0);
124         rtx index = XEXP (x, 1);
125
126         switch (GET_CODE (index))
127           {
128           case CONST_INT:
129             fprintf (stream, "@(%d,%s)", INTVAL (index),
130                      reg_names[REGNO (base)]);
131             break;
132
133           case REG:
134             fprintf (stream, "@(r0,%s)",
135                      reg_names[MAX (REGNO (base), REGNO (index))]);
136             break;
137
138           default:
139             debug_rtx (x);
140             abort ();
141           }
142       }
143       break;
144
145     case PRE_DEC:
146       fprintf (stream, "@-%s", reg_names[REGNO (XEXP (x, 0))]);
147       break;
148
149     case POST_INC:
150       fprintf (stream, "@%s+", reg_names[REGNO (XEXP (x, 0))]);
151       break;
152
153     default:
154       output_addr_const (stream, x);
155       break;
156     }
157 }
158
159 /* Print operand x (an rtx) in assembler syntax to file stream
160    according to modifier code.
161
162    '.'  print a .s if insn needs delay slot
163    '@'  print rte or rts depending upon pragma interruptness
164    '#'  output a nop if there is nothing to put in the delay slot
165    'O'  print a constant without the #
166    'R'  print the LSW of a dp value - changes if in little endian
167    'S'  print the MSW of a dp value - changes if in little endian
168    'T'  print the next word of a dp value - same as 'R' in big endian mode.  */
169
170 void
171 print_operand (stream, x, code)
172      FILE *stream;
173      rtx x;
174      int code;
175 {
176   switch (code)
177     {
178     case '.':
179       if (final_sequence
180           && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
181         fprintf (stream, ".s");
182       break;
183     case '@':
184       if (pragma_interrupt)
185         fprintf (stream, "rte");
186       else
187         fprintf (stream, "rts");
188       break;
189     case '#':
190       /* Output a nop if there's nothing in the delay slot.  */
191       if (dbr_sequence_length () == 0)
192         fprintf (stream, "\n\tnop");
193       break;
194     case 'O':
195       output_addr_const (stream, x);
196       break;
197     case 'R':
198       fputs (reg_names[REGNO (x) + LSW], (stream));
199       break;
200     case 'S':
201       fputs (reg_names[REGNO (x) + MSW], (stream));
202       break;
203     case 'T':
204       /* Next word of a double.  */
205       switch (GET_CODE (x))
206         {
207         case REG:
208           fputs (reg_names[REGNO (x) + 1], (stream));
209           break;
210         case MEM:
211           print_operand_address (stream,
212                                  XEXP (adj_offsettable_operand (x, 4), 0));
213           break;
214         }
215       break;
216     default:
217       switch (GET_CODE (x))
218         {
219         case REG:
220           fputs (reg_names[REGNO (x)], (stream));
221           break;
222         case MEM:
223           output_address (XEXP (x, 0));
224           break;
225         default:
226           fputc ('#', stream);
227           output_addr_const (stream, x);
228           break;
229         }
230       break;
231     }
232 }
233 \f
234 /* Emit code to perform a block move.  Choose the best method.
235
236    OPERANDS[0] is the destination.
237    OPERANDS[1] is the source.
238    OPERANDS[2] is the size.
239    OPERANDS[3] is the alignment safe to use.  */
240
241 int
242 expand_block_move (operands)
243      rtx *operands;
244 {
245   int align = INTVAL (operands[3]);
246   int constp = (GET_CODE (operands[2]) == CONST_INT);
247   int bytes = (constp ? INTVAL (operands[2]) : 0);
248
249   /* If it isn't a constant number of bytes, or if it doesn't have 4 byte
250      alignment, or if it isn't a multiple of 4 bytes, then fail.  */
251   if (! constp || align < 4 || (bytes % 4 != 0))
252     return 0;
253
254   if (bytes < 64)
255     {
256       char entry[30];
257       tree entry_name;
258       rtx func_addr_rtx;
259       rtx r4 = gen_rtx (REG, SImode, 4);
260       rtx r5 = gen_rtx (REG, SImode, 5);
261
262       sprintf (entry, "__movstrSI%d", bytes);
263       entry_name = get_identifier (entry);
264
265       func_addr_rtx
266         = copy_to_mode_reg (Pmode,
267                             gen_rtx (SYMBOL_REF, Pmode,
268                                      IDENTIFIER_POINTER (entry_name)));
269       emit_insn (gen_move_insn (r4, XEXP (operands[0], 0)));
270       emit_insn (gen_move_insn (r5, XEXP (operands[1], 0)));
271       emit_insn (gen_block_move_real (func_addr_rtx));
272       return 1;
273     }
274
275   /* This is the same number of bytes as a memcpy call, but to a different
276      less common function name, so this will occasionally use more space.  */
277   if (! TARGET_SMALLCODE)
278     {
279       tree entry_name;
280       rtx func_addr_rtx;
281       int final_switch, while_loop;
282       rtx r4 = gen_rtx (REG, SImode, 4);
283       rtx r5 = gen_rtx (REG, SImode, 5);
284       rtx r6 = gen_rtx (REG, SImode, 6);
285
286       entry_name = get_identifier ("__movstr");
287       func_addr_rtx
288         = copy_to_mode_reg (Pmode,
289                             gen_rtx (SYMBOL_REF, Pmode,
290                                      IDENTIFIER_POINTER (entry_name)));
291       emit_insn (gen_move_insn (r4, XEXP (operands[0], 0)));
292       emit_insn (gen_move_insn (r5, XEXP (operands[1], 0)));
293
294       /* r6 controls the size of the move.  16 is decremented from it
295          for each 64 bytes moved.  Then the negative bit left over is used
296          as an index into a list of move instructions.  e.g., a 72 byte move
297          would be set up with size(r6) = 14, for one iteration through the
298          big while loop, and a switch of -2 for the last part.  */
299
300       final_switch = 16 - ((bytes / 4) % 16);
301       while_loop = ((bytes / 4) / 16 - 1) * 16;
302       emit_insn (gen_move_insn (r6, GEN_INT (while_loop + final_switch)));
303       emit_insn (gen_block_lump_real (func_addr_rtx));
304       return 1;
305     }
306
307   return 0;
308 }
309
310 /* Prepare operands for a move define_expand; specifically, one of the
311    operands must be in a register.  */
312
313 int
314 prepare_move_operands (operands, mode)
315      rtx operands[];
316      enum machine_mode mode;
317 {
318   if (! reload_in_progress && ! reload_completed)
319     {
320       /* Copy the source to a register if both operands aren't registers.  */
321       if (! register_operand (operands[0], mode)
322           && ! register_operand (operands[1], mode))
323         operands[1] = copy_to_mode_reg (mode, operands[1]);
324
325       /* This case can happen while generating code to move the result
326          of a library call to the target.  Reject `st r0,@(rX,rY)' because
327          reload will fail to find a spill register for rX, since r0 is already
328          being used for the source.  */
329       else if (GET_CODE (operands[1]) == REG && REGNO (operands[1]) == 0
330                && GET_CODE (operands[0]) == MEM
331                && GET_CODE (XEXP (operands[0], 0)) == PLUS
332                && GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == REG)
333         operands[1] = copy_to_mode_reg (mode, operands[1]);
334     }
335
336   return 0;
337 }
338
339 /* Prepare the operands for an scc instruction; make sure that the
340    compare has been done.  */
341 rtx
342 prepare_scc_operands (code)
343      enum rtx_code code;
344 {
345   rtx t_reg = gen_rtx (REG, SImode, T_REG);
346   enum rtx_code oldcode = code;
347   enum machine_mode mode;
348
349   /* First need a compare insn.  */
350   switch (code)
351     {
352     case NE:
353       /* It isn't possible to handle this case.  */
354       abort ();
355     case LT:
356       code = GT;
357       break;
358     case LE:
359       code = GE;
360       break;
361     case LTU:
362       code = GTU;
363       break;
364     case LEU:
365       code = GEU;
366       break;
367     }
368   if (code != oldcode)
369     {
370       rtx tmp = sh_compare_op0;
371       sh_compare_op0 = sh_compare_op1;
372       sh_compare_op1 = tmp;
373     }
374
375   mode = GET_MODE (sh_compare_op0);
376   if (mode == VOIDmode)
377     mode = GET_MODE (sh_compare_op1);
378
379   sh_compare_op0 = force_reg (mode, sh_compare_op0);
380   if (code != EQ && code != NE
381       && (sh_compare_op1 != const0_rtx
382           || code == GTU  || code == GEU || code == LTU || code == LEU))
383     sh_compare_op1 = force_reg (mode, sh_compare_op1);
384
385   /* ??? This should be `mode' not `SImode' in the compare, but that would
386      require fixing the branch patterns too.  */
387   emit_insn (gen_rtx (SET, VOIDmode, t_reg,
388                       gen_rtx (code, SImode, sh_compare_op0,
389                                sh_compare_op1)));
390
391   return t_reg;
392 }
393
394 /* Called from the md file, set up the operands of a compare instruction.  */
395
396 void
397 from_compare (operands, code)
398      rtx *operands;
399      int code;
400 {
401   if (code != EQ && code != NE)
402     {
403       enum machine_mode mode = GET_MODE (sh_compare_op0);
404       if (mode == VOIDmode)
405         mode = GET_MODE (sh_compare_op1);
406
407       /* Force args into regs, since we can't use constants here.  */
408       sh_compare_op0 = force_reg (mode, sh_compare_op0);
409       if (sh_compare_op1 != const0_rtx
410           || code == GTU  || code == GEU || code == LTU || code == LEU)
411         sh_compare_op1 = force_reg (mode, sh_compare_op1);
412     }
413   operands[1] = sh_compare_op0;
414   operands[2] = sh_compare_op1;
415 }
416 \f
417 /* Functions to output assembly code.  */
418
419 /* Return a sequence of instructions to perform DI or DF move.
420
421    Since the SH cannot move a DI or DF in one instruction, we have
422    to take care when we see overlapping source and dest registers.  */
423
424 char *
425 output_movedouble (insn, operands, mode)
426      rtx insn;
427      rtx operands[];
428      enum machine_mode mode;
429 {
430   rtx dst = operands[0];
431   rtx src = operands[1];
432
433   if (GET_CODE (dst) == MEM
434       && GET_CODE (XEXP (dst, 0)) == PRE_DEC)
435     return "mov.l       %T1,%0\n\tmov.l %1,%0";
436
437   if (register_operand (dst, mode)
438       && register_operand (src, mode))
439     {
440       if (REGNO (src) == MACH_REG)
441         return "sts     mach,%S0\n\tsts macl,%R0";
442
443       /* When mov.d r1,r2 do r2->r3 then r1->r2;
444          when mov.d r1,r0 do r1->r0 then r2->r1.  */
445
446       if (REGNO (src) + 1 == REGNO (dst))
447         return "mov     %T1,%T0\n\tmov  %1,%0";
448       else
449         return "mov     %1,%0\n\tmov    %T1,%T0";
450     }
451   else if (GET_CODE (src) == CONST_INT)
452     {
453       if (INTVAL (src) < 0)
454         output_asm_insn ("mov   #-1,%S0", operands);
455       else
456         output_asm_insn ("mov   #0,%S0", operands);
457
458       return "mov       %1,%R0";
459     }
460   else if (GET_CODE (src) == MEM)
461     {
462       int ptrreg = -1;
463       int dreg = REGNO (dst);
464       rtx inside = XEXP (src, 0);
465
466       if (GET_CODE (inside) == REG)
467         ptrreg = REGNO (inside);
468       else if (GET_CODE (inside) == SUBREG)
469         ptrreg = REGNO (SUBREG_REG (inside)) + SUBREG_WORD (inside);
470       else if (GET_CODE (inside) == PLUS)
471         {
472           ptrreg = REGNO (XEXP (inside, 0));
473           /* ??? A r0+REG address shouldn't be possible here, because it isn't
474              an offsettable address.  Unfortunately, offsettable addresses use
475              QImode to check the offset, and a QImode offsettable address
476              requires r0 for the other operand, which is not currently
477              supported, so we can't use the 'o' constraint.
478              Thus we must check for and handle r0+REG addresses here.
479              We punt for now, since this is likely very rare.  */
480           if (GET_CODE (XEXP (inside, 1)) == REG)
481             abort ();
482         }
483       else if (GET_CODE (inside) == LABEL_REF)
484         return "mov.l   %1,%0\n\tmov.l  %1+4,%T0";
485       else if (GET_CODE (inside) == POST_INC)
486         return "mov.l   %1,%0\n\tmov.l  %1,%T0";
487       else
488         abort ();
489
490       /* Work out the safe way to copy.  Copy into the second half first.  */
491       if (dreg == ptrreg)
492         return "mov.l   %T1,%T0\n\tmov.l        %1,%0";
493     }
494
495   return "mov.l %1,%0\n\tmov.l  %T1,%T0";
496 }
497
498 /* Print an instruction which would have gone into a delay slot after
499    another instruction, but couldn't because the other instruction expanded
500    into a sequence where putting the slot insn at the end wouldn't work.  */
501
502 static void
503 print_slot (insn)
504      rtx insn;
505 {
506   final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file, optimize, 0, 1);
507
508   INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
509 }
510
511 /* We can't tell if we need a register as a scratch for the jump
512    until after branch shortening, and then it's too late to allocate a
513    register the 'proper' way.  These instruction sequences are rare
514    anyway, so to avoid always using a reg up from our limited set, we'll
515    grab one when we need one on output.  */
516
517 /* ??? Should fix compiler so that using a clobber scratch in jump
518    instructions works, and then this will be unnecessary.  */
519
520 char *
521 output_far_jump (insn, op)
522      rtx insn;
523      rtx op;
524 {
525   rtx thislab = gen_label_rtx ();
526
527   /* Output the delay slot insn first if any.  */
528   if (dbr_sequence_length ())
529     print_slot (final_sequence);
530
531   output_asm_insn ("mov.l       r13,@-r15", 0);
532   output_asm_insn ("mov.l       %O0,r13", &thislab);
533   output_asm_insn ("jmp @r13", 0);
534   output_asm_insn ("mov.l       @r15+,r13", 0);
535   output_asm_insn (".align      2", 0);
536   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (thislab));
537   output_asm_insn (".long       %O0", &op);
538   return "";
539 }
540
541 /* Local label counter, used for constants in the pool and inside
542    pattern branches.  */
543
544 static int lf = 100;
545
546 /* Output code for ordinary branches.  */
547
548 char *
549 output_branch (logic, insn, operands)
550      int logic;
551      rtx insn;
552      rtx *operands;
553 {
554   int label = lf++;
555   int length = get_attr_length (insn);
556   int adjusted_length;
557
558   /* Undo the effects of ADJUST_INSN_LENGTH, so that we get the real
559      length.  If NEXT_INSN (PREV_INSN (insn)) != insn, then the insn
560      is inside a sequence, and ADJUST_INSN_LENGTH was not called on
561      it.  */
562   if (PREV_INSN (insn) == NULL
563       || NEXT_INSN (PREV_INSN (insn)) == insn)
564     {
565       adjusted_length = length;
566       ADJUST_INSN_LENGTH (insn, adjusted_length);
567       length -= (adjusted_length - length);
568     }
569
570   switch (length)
571     {
572     case 2:
573       /* A branch with an unfilled delay slot.  */
574     case 4:
575       /* Simple branch in range -252..+258 bytes */
576       return logic ? "bt%.      %l0" : "bf%.    %l0";
577
578     case 6:
579       /* A branch with an unfilled delay slot.  */
580     case 8:
581       /* Branch in range -4092..+4098 bytes.  */
582       {
583         /* The call to print_slot will clobber the operands.  */
584         rtx op0 = operands[0];
585
586         /* If the instruction in the delay slot is annulled (true), then
587            there is no delay slot where we can put it now.  The only safe
588            place for it is after the label.  */
589
590         if (final_sequence)
591           {
592             fprintf (asm_out_file, "\tb%c%s\tLF%d\n", logic ? 'f' : 't',
593                      INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
594                      ? "" : ".s", label);
595             if (! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
596               print_slot (final_sequence);
597           }
598         else
599           fprintf (asm_out_file, "\tb%c\tLF%d\n", logic ? 'f' : 't', label);
600
601         output_asm_insn ("bra   %l0", &op0);
602         fprintf (asm_out_file, "\tnop\n");
603         fprintf (asm_out_file, "LF%d:\n", label);
604
605         if (final_sequence
606             && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
607           print_slot (final_sequence);
608       }
609       return "";
610
611     case 16:
612       /* A branch with an unfilled delay slot.  */
613     case 18:
614       /* Branches a long way away.  */
615       {
616         /* The call to print_slot will clobber the operands.  */
617         rtx op0 = operands[0];
618
619         /* If the instruction in the delay slot is annulled (true), then
620            there is no delay slot where we can put it now.  The only safe
621            place for it is after the label.  */
622
623         if (final_sequence)
624           {
625             fprintf (asm_out_file, "\tb%c%s\tLF%d\n", logic ? 'f' : 't',
626                      INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
627                      ? "" : ".s", label);
628             if (! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
629               print_slot (final_sequence);
630           }
631         else
632           fprintf (asm_out_file, "\tb%c\tLF%d\n", logic ? 'f' : 't', label);
633
634         output_far_jump (insn, op0);
635         fprintf (asm_out_file, "LF%d:\n", label);
636
637         if (final_sequence
638             && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
639           print_slot (final_sequence);
640       }
641       return "";
642     }
643
644   abort ();
645 }
646 \f
647 /* Output to FILE the start of the assembler file.  */
648
649 void
650 output_file_start (file)
651      FILE *file;
652 {
653   register int pos;
654
655   output_file_directive (file, main_input_filename);
656
657   /* Switch to the data section so that the coffsem symbol and the
658      gcc2_compiled. symbol aren't in the text section.  */
659   data_section ();
660
661   if (TARGET_LITTLE_ENDIAN)
662     fprintf (file, "\t.little\n");
663 }
664 \f
665 /* Actual number of instructions used to make a shift by N.  */
666 static char ashiftrt_insns[] =
667   { 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};
668
669 /* Left shift and logical right shift are the same.  */
670 static char shift_insns[]    =
671   { 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};
672
673 /* Individual shift amounts needed to get the above length sequences.
674    One bit right shifts clobber the T bit, so when possible, put one bit
675    shifts in the middle of the sequence, so the ends are eligible for
676    branch delay slots.  */
677 static short shift_amounts[32][5] = {
678   {0}, {1}, {2}, {2, 1},
679   {2, 2}, {2, 1, 2}, {2, 2, 2}, {2, 2, 1, 2},
680   {8}, {8, 1}, {8, 2}, {8, 1, 2},
681   {8, 2, 2}, {8, 2, 1, 2}, {8, -2, 8}, {8, -1, 8},
682   {16}, {16, 1}, {16, 2}, {16, 1, 2},
683   {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
684   {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
685   {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
686
687 /* This is used in length attributes in sh.md to help compute the length
688    of arbitrary constant shift instructions.  */
689
690 int
691 shift_insns_rtx (insn)
692      rtx insn;
693 {
694   rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
695   int shift_count = INTVAL (XEXP (set_src, 1));
696   enum rtx_code shift_code = GET_CODE (set_src);
697
698   switch (shift_code)
699     {
700     case ASHIFTRT:
701       return ashiftrt_insns[shift_count];
702     case LSHIFTRT:
703     case ASHIFT:
704       return shift_insns[shift_count];
705     default:
706       abort();
707     }
708 }
709
710 /* Return the cost of a shift.  */
711
712 int
713 shiftcosts (x)
714      rtx x;
715 {
716   int value = INTVAL (XEXP (x, 1));
717
718   /* If shift by a non constant, then this will be expensive.  */
719   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
720     {
721       if (TARGET_SH3)
722         return 2;
723       /* If not an sh3 then we don't even have an instruction for it.  */
724       return 20;
725     }
726
727   /* Otherwise, return the true cost in instructions.  */
728   if (GET_CODE (x) == ASHIFTRT)
729     {
730       int cost = ashiftrt_insns[value];
731       /* If SH3, then we put the constant in a reg and use shad.  */
732       if (TARGET_SH3 && cost > 3)
733         cost = 3;
734       return cost;
735     }
736   else
737     return shift_insns[value];
738 }
739
740 /* Return the cost of an AND operation.  */
741
742 int
743 andcosts (x)
744      rtx x;
745 {
746   int i;
747
748   /* Anding with a register is a single cycle and instruction.  */
749   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
750     return 1;
751
752   i = INTVAL (XEXP (x, 1));
753   /* These constants are single cycle extu.[bw] instructions.  */
754   if (i == 0xff || i == 0xffff)
755     return 1;
756   /* Constants that can be used in an and immediate instruction is a single
757      cycle, but this requires r0, so make it a little more expensive.  */
758   if (CONST_OK_FOR_L (i))
759     return 2;
760   /* Constants that can be loaded with a mov immediate and an and.
761      This case is probably unnecessary.  */
762   if (CONST_OK_FOR_I (i))
763     return 2;
764   /* Any other constants requires a 2 cycle pc-relative load plus an and.
765      This case is probably unnecessary.  */
766   return 3;
767 }
768
769 /* Return the cost of a multiply.  */
770 int
771 multcosts (x)
772      rtx x;
773 {
774   if (TARGET_SH2)
775     {
776       /* We have a mul insn, so we can never take more than the mul and the
777          read of the mac reg, but count more because of the latency and extra
778          reg usage.  */
779       if (TARGET_SMALLCODE)
780         return 2;
781       return 3;
782     }
783
784   /* If we're aiming at small code, then just count the number of
785      insns in a multiply call sequence.  */
786   if (TARGET_SMALLCODE)
787     return 5;
788
789   /* Otherwise count all the insns in the routine we'd be calling too.  */
790   return 20;
791 }
792
793 /* Code to expand a shift.  */
794
795 void
796 gen_ashift (type, n, reg)
797      int type;
798      int n;
799      rtx reg;
800 {
801   /* Negative values here come from the shift_amounts array.  */
802   if (n < 0)
803     {
804       if (type == ASHIFT)
805         type = LSHIFTRT;
806       else
807         type = ASHIFT;
808       n = -n;
809     }
810
811   switch (type)
812     {
813     case ASHIFTRT:
814       emit_insn (gen_ashrsi3_k (reg, reg, GEN_INT (n)));
815       break;
816     case LSHIFTRT:
817       if (n == 1)
818         emit_insn (gen_lshrsi3_m (reg, reg, GEN_INT (n)));
819       else
820         emit_insn (gen_lshrsi3_k (reg, reg, GEN_INT (n)));
821       break;
822     case ASHIFT:
823       emit_insn (gen_ashlsi3_k (reg, reg, GEN_INT (n)));
824       break;
825     }
826 }
827
828 /* Output RTL to split a constant shift into its component SH constant
829    shift instructions.  */
830    
831 int
832 gen_shifty_op (code, operands)
833      int code;
834      rtx *operands;
835 {
836   int value = INTVAL (operands[2]);
837   int max, i;
838
839   /* Truncate the shift count in case it is out of bounds.  */
840   value = value & 0x1f;
841  
842   if (value == 31)
843     {
844       if (code == LSHIFTRT)
845         {
846           emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
847           emit_insn (gen_movt (operands[0]));
848           return;
849         }
850       else if (code == ASHIFT)
851         {
852           /* There is a two instruction sequence for 31 bit left shifts,
853              but it requires r0.  */
854           if (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 0)
855             {
856               emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
857               emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
858               return;
859             }
860         }
861     }
862   else if (value == 0)
863     {
864       /* This can happen when not optimizing.  We must output something here
865          to prevent the compiler from aborting in final.c after the try_split
866          call.  */
867       emit_insn (gen_nop ());
868       return;
869     }
870
871   max = shift_insns[value];
872   for (i = 0; i < max; i++)
873     gen_ashift (code, shift_amounts[value][i], operands[0]);
874 }
875
876 /* Output RTL for an arithmetic right shift.  */
877
878 /* ??? Rewrite to use super-optimizer sequences.  */
879
880 int
881 expand_ashiftrt (operands)
882      rtx *operands;
883 {
884   rtx wrk;
885   char func[18];
886   tree func_name;
887   int value;
888
889   if (TARGET_SH3)
890     {
891       if (GET_CODE (operands[2]) != CONST_INT)
892         {
893           rtx count = copy_to_mode_reg (SImode, operands[2]);
894           emit_insn (gen_negsi2 (count, count));
895           emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
896           return 1;
897         }
898       else if (ashiftrt_insns[INTVAL (operands[2])] > 3)
899         {
900           rtx count = force_reg (SImode, GEN_INT (- INTVAL (operands[2])));
901           emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
902           return 1;
903         }
904     }
905   if (GET_CODE (operands[2]) != CONST_INT)
906     return 0;
907
908   value = INTVAL (operands[2]);
909
910   if (value == 31)
911     {
912       emit_insn (gen_ashrsi2_31 (operands[0], operands[1]));
913       return 1;
914     }
915   else if (value >= 16 && value <= 19)
916     {
917       wrk = gen_reg_rtx (SImode);
918       emit_insn (gen_ashrsi2_16 (wrk, operands[1]));
919       value -= 16;
920       while (value--)
921         gen_ashift (ASHIFTRT, 1, wrk);
922       emit_move_insn (operands[0], wrk);
923       return 1;
924     }
925   /* Expand a short sequence inline, longer call a magic routine.  */
926   else if (value <= 5)
927     {
928       wrk = gen_reg_rtx (SImode);
929       emit_move_insn (wrk, operands[1]);
930       while (value--)
931         gen_ashift (ASHIFTRT, 1, wrk);
932       emit_move_insn (operands[0], wrk);
933       return 1;
934     }
935
936   wrk = gen_reg_rtx (Pmode);
937
938   /* Load the value into an arg reg and call a helper.  */
939   emit_move_insn (gen_rtx (REG, SImode, 4), operands[1]);
940   sprintf (func, "__ashiftrt_r4_%d", value);
941   func_name = get_identifier (func);
942   emit_move_insn (wrk, gen_rtx (SYMBOL_REF, Pmode,
943                                 IDENTIFIER_POINTER (func_name)));
944   emit_insn (gen_ashrsi3_n (GEN_INT (value), wrk));
945   emit_move_insn (operands[0], gen_rtx (REG, SImode, 4));
946   return 1;
947 }
948 \f
949 /* The SH cannot load a large constant into a register, constants have to
950    come from a pc relative load.  The reference of a pc relative load
951    instruction must be less than 1k infront of the instruction.  This
952    means that we often have to dump a constant inside a function, and
953    generate code to branch around it.
954
955    It is important to minimize this, since the branches will slow things
956    down and make things bigger.
957
958    Worst case code looks like:
959
960    mov.l L1,rn
961    bra   L2
962    nop
963    align
964    L1:   .long value
965    L2:
966    ..
967
968    mov.l L3,rn
969    bra   L4
970    nop
971    align
972    L3:   .long value
973    L4:
974    ..
975
976    We fix this by performing a scan before scheduling, which notices which
977    instructions need to have their operands fetched from the constant table
978    and builds the table.
979
980    The algorithm is:
981
982    scan, find an instruction which needs a pcrel move.  Look forward, find the
983    last barrier which is within MAX_COUNT bytes of the requirement.
984    If there isn't one, make one.  Process all the instructions between
985    the find and the barrier.
986
987    In the above example, we can tell that L3 is within 1k of L1, so
988    the first move can be shrunk from the 3 insn+constant sequence into
989    just 1 insn, and the constant moved to L3 to make:
990
991    mov.l        L1,rn
992    ..
993    mov.l        L3,rn
994    bra          L4
995    nop
996    align
997    L3:.long value
998    L4:.long value
999
1000    Then the second move becomes the target for the shortening process.  */
1001
1002 typedef struct
1003 {
1004   rtx value;                    /* Value in table.  */
1005   rtx label;                    /* Label of value.  */
1006   enum machine_mode mode;       /* Mode of value.  */
1007 } pool_node;
1008
1009 /* The maximum number of constants that can fit into one pool, since
1010    the pc relative range is 0...1020 bytes and constants are at least 4
1011    bytes long.  */
1012
1013 #define MAX_POOL_SIZE (1020/4)
1014 static pool_node pool_vector[MAX_POOL_SIZE];
1015 static int pool_size;
1016
1017 /* ??? If we need a constant in HImode which is the truncated value of a
1018    constant we need in SImode, we could combine the two entries thus saving
1019    two bytes.  Is this common enough to be worth the effort of implementing
1020    it?  */
1021
1022 /* ??? This stuff should be done at the same time that we shorten branches.
1023    As it is now, we must assume that all branches are the maximum size, and
1024    this causes us to almost always output constant pools sooner than
1025    necessary.  */
1026
1027 /* Add a constant to the pool and return its label.  */
1028
1029 static rtx
1030 add_constant (x, mode)
1031      rtx x;
1032      enum machine_mode mode;
1033 {
1034   int i;
1035   rtx lab;
1036
1037   /* First see if we've already got it.  */
1038   for (i = 0; i < pool_size; i++)
1039     {
1040       if (x->code == pool_vector[i].value->code
1041           && mode == pool_vector[i].mode)
1042         {
1043           if (x->code == CODE_LABEL)
1044             {
1045               if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
1046                 continue;
1047             }
1048           if (rtx_equal_p (x, pool_vector[i].value))
1049             return pool_vector[i].label;
1050         }
1051     }
1052
1053   /* Need a new one.  */
1054   pool_vector[pool_size].value = x;
1055   lab = gen_label_rtx ();
1056   pool_vector[pool_size].mode = mode;
1057   pool_vector[pool_size].label = lab;
1058   pool_size++;
1059   return lab;
1060 }
1061
1062 /* Output the literal table.  */
1063
1064 static void
1065 dump_table (scan)
1066      rtx scan;
1067 {
1068   int i;
1069   int need_align = 1;
1070
1071   /* Do two passes, first time dump out the HI sized constants.  */
1072
1073   for (i = 0; i < pool_size; i++)
1074     {
1075       pool_node *p = &pool_vector[i];
1076
1077       if (p->mode == HImode)
1078         {
1079           if (need_align)
1080             {
1081               scan = emit_insn_after (gen_align_2 (), scan);
1082               need_align = 0;
1083             }
1084           scan = emit_label_after (p->label, scan);
1085           scan = emit_insn_after (gen_consttable_2 (p->value), scan);
1086         }
1087     }
1088
1089   need_align = 1;
1090
1091   for (i = 0; i < pool_size; i++)
1092     {
1093       pool_node *p = &pool_vector[i];
1094
1095       switch (p->mode)
1096         {
1097         case HImode:
1098           break;
1099         case SImode:
1100           if (need_align)
1101             {
1102               need_align = 0;
1103               scan = emit_label_after (gen_label_rtx (), scan);
1104               scan = emit_insn_after (gen_align_4 (), scan);
1105             }
1106           scan = emit_label_after (p->label, scan);
1107           scan = emit_insn_after (gen_consttable_4 (p->value), scan);
1108           break;
1109         case DImode:
1110           if (need_align)
1111             {
1112               need_align = 0;
1113               scan = emit_label_after (gen_label_rtx (), scan);
1114               scan = emit_insn_after (gen_align_4 (), scan);
1115             }
1116           scan = emit_label_after (p->label, scan);
1117           scan = emit_insn_after (gen_consttable_8 (p->value), scan);
1118           break;
1119         default:
1120           abort ();
1121           break;
1122         }
1123     }
1124
1125   scan = emit_insn_after (gen_consttable_end (), scan);
1126   scan = emit_barrier_after (scan);
1127   pool_size = 0;
1128 }
1129
1130 /* Return non-zero if constant would be an ok source for a
1131    mov.w instead of a mov.l.  */
1132
1133 static int
1134 hi_const (src)
1135      rtx src;
1136 {
1137   return (GET_CODE (src) == CONST_INT
1138           && INTVAL (src) >= -32768
1139           && INTVAL (src) <= 32767);
1140 }
1141
1142 /* Non-zero if the insn is a move instruction which needs to be fixed.  */
1143
1144 /* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
1145    CONST_DOUBLE input value is CONST_OK_FOR_I.  For a SFmode move, we don't
1146    need to fix it if the input value is CONST_OK_FOR_I.  */
1147
1148 static int
1149 broken_move (insn)
1150      rtx insn;
1151 {
1152   if (GET_CODE (insn) == INSN
1153       && GET_CODE (PATTERN (insn)) == SET
1154       /* We can load any 8 bit value if we don't care what the high
1155          order bits end up as.  */
1156       && GET_MODE (SET_DEST (PATTERN (insn))) != QImode
1157       && CONSTANT_P (SET_SRC (PATTERN (insn)))
1158       && ! (GET_CODE (SET_SRC (PATTERN (insn))) == CONST_DOUBLE
1159             && (fp_zero_operand (SET_SRC (PATTERN (insn)))
1160                 || fp_one_operand (SET_SRC (PATTERN (insn))))
1161             && GET_CODE (SET_DEST (PATTERN (insn))) == REG
1162             && REGNO (SET_DEST (PATTERN (insn))) >= FIRST_FP_REG
1163             && REGNO (SET_DEST (PATTERN (insn))) <= LAST_FP_REG)
1164       && (GET_CODE (SET_SRC (PATTERN (insn))) != CONST_INT
1165           || ! CONST_OK_FOR_I (INTVAL (SET_SRC (PATTERN (insn))))))
1166     return 1;
1167
1168   return 0;
1169 }
1170
1171 /* Find the last barrier from insn FROM which is close enough to hold the
1172    constant pool.  If we can't find one, then create one near the end of
1173    the range.  */
1174
1175 /* ??? It would be good to put constant pool tables between a case jump and
1176    the jump table.  This fails for two reasons.  First, there is no
1177    barrier after the case jump.  This is a bug in the casesi pattern.
1178    Second, inserting the table here may break the mova instruction that
1179    loads the jump table address, by moving the jump table too far away.
1180    We fix that problem by never outputting the constant pool between a mova
1181    and its label.  */
1182
1183 static rtx
1184 find_barrier (from)
1185      rtx from;
1186 {
1187   int count_si = 0;
1188   int count_hi = 0;
1189   int found_hi = 0;
1190   int found_si = 0;
1191   rtx found_barrier = 0;
1192   rtx found_mova = 0;
1193   int si_limit;
1194   int hi_limit;
1195
1196   /* For HImode: range is 510, add 4 because pc counts from address of
1197      second instruction after this one, subtract 2 for the jump instruction
1198      that we may need to emit before the table, subtract 2 for the instruction
1199      that fills the jump delay slot (in very rare cases, reorg will take an
1200      instruction from after the constant pool or will leave the delay slot
1201      empty).  This gives 510.
1202      For SImode: range is 1020, add 4 because pc counts from address of
1203      second instruction after this one, subtract 2 in case pc is 2 byte
1204      aligned, subtract 2 for the jump instruction that we may need to emit
1205      before the table, subtract 2 for the instruction that fills the jump
1206      delay slot.  This gives 1018.  */
1207
1208   /* If not optimizing, then it is possible that the jump instruction we add
1209      won't be shortened, and thus will have a length of 14 instead of 2.
1210      We must adjust the limits downwards to account for this, giving a limit
1211      of 1008 for SImode and 500 for HImode.  */
1212
1213   if (optimize)
1214     {
1215       si_limit = 1018;
1216       hi_limit = 510;
1217     }
1218   else
1219     {
1220       si_limit = 1008;
1221       hi_limit = 500;
1222     }
1223
1224   /* If not optimizing for space, then the constant pool will be
1225      aligned to a 4 to 16 byte boundary.  We must make room for that
1226      alignment that by reducing the limits.
1227      ??? It would be better to not align the constant pool, but
1228      ASM_OUTPUT_ALIGN_CODE does not make any provision for basing the
1229      alignment on the instruction.  */
1230
1231   if (! TARGET_SMALLCODE)
1232     {
1233       if (TARGET_SH3 || TARGET_SH3E)
1234         {
1235           si_limit -= 14;
1236           hi_limit -= 14;
1237         }
1238       else
1239         {
1240           si_limit -= 2;
1241           hi_limit -= 2;
1242         }
1243     }
1244
1245   while (from && count_si < si_limit && count_hi < hi_limit)
1246     {
1247       int inc = get_attr_length (from);
1248
1249       if (GET_CODE (from) == BARRIER)
1250         found_barrier = from;
1251
1252       if (broken_move (from))
1253         {
1254           rtx pat = PATTERN (from);
1255           rtx src = SET_SRC (pat);
1256           rtx dst = SET_DEST (pat);
1257           enum machine_mode mode = GET_MODE (dst);
1258
1259           /* We must explicitly check the mode, because sometimes the
1260              front end will generate code to load unsigned constants into
1261              HImode targets without properly sign extending them.  */
1262           if (mode == HImode || (mode == SImode && hi_const (src)))
1263             {
1264               found_hi = 1;
1265               /* We put the short constants before the long constants, so
1266                  we must count the length of short constants in the range
1267                  for the long constants.  */
1268               /* ??? This isn't optimal, but is easy to do.  */
1269               if (found_si)
1270                 count_si += 2;
1271             }
1272           else
1273             found_si = 1;
1274         }
1275
1276       if (GET_CODE (from) == INSN
1277           && GET_CODE (PATTERN (from)) == SET
1278           && GET_CODE (SET_SRC (PATTERN (from))) == UNSPEC
1279           && XINT (SET_SRC (PATTERN (from)), 1) == 1)
1280         found_mova = from;
1281       else if (GET_CODE (from) == JUMP_INSN
1282                && (GET_CODE (PATTERN (from)) == ADDR_VEC
1283                    || GET_CODE (PATTERN (from)) == ADDR_DIFF_VEC))
1284         found_mova = 0;
1285
1286       if (found_si)
1287         count_si += inc;
1288       if (found_hi)
1289         count_hi += inc;
1290       from = NEXT_INSN (from);
1291     }
1292
1293   /* Insert the constant pool table before the mova instruction, to prevent
1294      the mova label reference from going out of range.  */
1295   if (found_mova)
1296     from = found_mova;
1297
1298   if (! found_barrier)
1299     {
1300       /* We didn't find a barrier in time to dump our stuff,
1301          so we'll make one.  */
1302       rtx label = gen_label_rtx ();
1303
1304       /* If we exceeded the range, then we must back up over the last
1305          instruction we looked at.  Otherwise, we just need to undo the
1306          NEXT_INSN at the end of the loop.  */
1307       if (count_hi > hi_limit || count_si > si_limit)
1308         from = PREV_INSN (PREV_INSN (from));
1309       else
1310         from = PREV_INSN (from);
1311
1312       /* Walk back to be just before any jump or label.
1313          Putting it before a label reduces the number of times the branch
1314          around the constant pool table will be hit.  Putting it before
1315          a jump makes it more likely that the bra delay slot will be
1316          filled.  */
1317       while (GET_CODE (from) == JUMP_INSN || GET_CODE (from) == NOTE
1318              || GET_CODE (from) == CODE_LABEL)
1319         from = PREV_INSN (from);
1320
1321       from = emit_jump_insn_after (gen_jump (label), from);
1322       JUMP_LABEL (from) = label;
1323       LABEL_NUSES (label) = 1;
1324       found_barrier = emit_barrier_after (from);
1325       emit_label_after (label, found_barrier);
1326     }
1327
1328   return found_barrier;
1329 }
1330
1331 /* See if the only way in which INSN uses REG is by calling it, or by
1332    setting it while calling it.  Set *SET to a SET rtx if the register
1333    is set by INSN.  */
1334
1335 static int
1336 noncall_uses_reg (reg, insn, set)
1337      rtx reg;
1338      rtx insn;
1339      rtx *set;
1340 {
1341   rtx pattern;
1342
1343   *set = NULL_RTX;
1344
1345   if (GET_CODE (insn) != CALL_INSN)
1346     {
1347       /* We don't use rtx_equal_p because we don't care if the mode is
1348          different.  */
1349       pattern = single_set (insn);
1350       if (pattern
1351           && GET_CODE (SET_DEST (pattern)) == REG
1352           && REGNO (reg) == REGNO (SET_DEST (pattern)))
1353         {
1354           *set = pattern;
1355           return 0;
1356         }
1357
1358       return 1;
1359     }
1360
1361   pattern = PATTERN (insn);
1362
1363   if (GET_CODE (pattern) == PARALLEL)
1364     {
1365       int i;
1366
1367       for (i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
1368         if (reg_mentioned_p (reg, XVECEXP (pattern, 0, i)))
1369           return 1;
1370       pattern = XVECEXP (pattern, 0, 0);
1371     }
1372
1373   if (GET_CODE (pattern) == SET)
1374     {
1375       if (reg_mentioned_p (reg, SET_DEST (pattern)))
1376         {
1377           /* We don't use rtx_equal_p, because we don't care if the
1378              mode is different.  */
1379           if (GET_CODE (SET_DEST (pattern)) != REG
1380               || REGNO (reg) != REGNO (SET_DEST (pattern)))
1381             return 1;
1382
1383           *set = pattern;
1384         }
1385
1386       pattern = SET_SRC (pattern);
1387     }
1388
1389   if (GET_CODE (pattern) != CALL
1390       || GET_CODE (XEXP (pattern, 0)) != MEM
1391       || ! rtx_equal_p (reg, XEXP (XEXP (pattern, 0), 0)))
1392     return 1;
1393
1394   return 0;
1395 }
1396
1397 /* Exported to toplev.c.
1398
1399    Do a final pass over the function, just before delayed branch
1400    scheduling.  */
1401
1402 void
1403 machine_dependent_reorg (first)
1404      rtx first;
1405 {
1406   rtx insn;
1407
1408   /* If relaxing, generate pseudo-ops to associate function calls with
1409      the symbols they call.  It does no harm to not generate these
1410      pseudo-ops.  However, when we can generate them, it enables to
1411      linker to potentially relax the jsr to a bsr, and eliminate the
1412      register load and, possibly, the constant pool entry.  */
1413
1414   if (TARGET_RELAX)
1415     {
1416       /* Remove all REG_LABEL notes.  We want to use them for our own
1417          purposes.  This works because none of the remaining passes
1418          need to look at them.
1419
1420          ??? But it may break in the future.  We should use a machine
1421          dependent REG_NOTE, or some other approach entirely.  */
1422       for (insn = first; insn; insn = NEXT_INSN (insn))
1423         {
1424           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1425             {
1426               rtx note;
1427
1428               while ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != 0)
1429                 remove_note (insn, note);
1430             }
1431         }
1432
1433       for (insn = first; insn; insn = NEXT_INSN (insn))
1434         {
1435           rtx pattern, reg, link, set, scan, dies, label;
1436           int rescan = 0, foundinsn = 0;
1437
1438           if (GET_CODE (insn) != CALL_INSN)
1439             continue;
1440
1441           pattern = PATTERN (insn);
1442
1443           if (GET_CODE (pattern) == PARALLEL)
1444             pattern = XVECEXP (pattern, 0, 0);
1445           if (GET_CODE (pattern) == SET)
1446             pattern = SET_SRC (pattern);
1447
1448           if (GET_CODE (pattern) != CALL
1449               || GET_CODE (XEXP (pattern, 0)) != MEM)
1450             continue;
1451
1452           reg = XEXP (XEXP (pattern, 0), 0);
1453           if (GET_CODE (reg) != REG)
1454             continue;
1455
1456           /* This is a function call via REG.  If the only uses of REG
1457              between the time that it is set and the time that it dies
1458              are in function calls, then we can associate all the
1459              function calls with the setting of REG.  */
1460
1461           for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1462             {
1463               if (REG_NOTE_KIND (link) != 0)
1464                 continue;
1465               set = single_set (XEXP (link, 0));
1466               if (set && rtx_equal_p (reg, SET_DEST (set)))
1467                 {
1468                   link = XEXP (link, 0);
1469                   break;
1470                 }
1471             }
1472
1473           if (! link)
1474             {
1475               /* ??? Sometimes global register allocation will have
1476                  deleted the insn pointed to by LOG_LINKS.  Try
1477                  scanning backward to find where the register is set.  */
1478               for (scan = PREV_INSN (insn);
1479                    scan && GET_CODE (scan) != CODE_LABEL;
1480                    scan = PREV_INSN (scan))
1481                 {
1482                   if (GET_RTX_CLASS (GET_CODE (scan)) != 'i')
1483                     continue;
1484
1485                   if (! reg_mentioned_p (reg, scan))
1486                     continue;
1487
1488                   if (noncall_uses_reg (reg, scan, &set))
1489                     break;
1490
1491                   if (set)
1492                     {
1493                       link = scan;
1494                       break;
1495                     }
1496                 }
1497             }
1498
1499           if (! link)
1500             continue;
1501
1502           /* The register is set at LINK.  */
1503
1504           /* We can only optimize the function call if the register is
1505              being set to a symbol.  In theory, we could sometimes
1506              optimize calls to a constant location, but the assembler
1507              and linker do not support that at present.  */
1508           if (GET_CODE (SET_SRC (set)) != SYMBOL_REF
1509               && GET_CODE (SET_SRC (set)) != LABEL_REF)
1510             continue;
1511
1512           /* Scan forward from LINK to the place where REG dies, and
1513              make sure that the only insns which use REG are
1514              themselves function calls.  */
1515
1516           /* ??? This doesn't work for call targets that were allocated
1517              by reload, since there may not be a REG_DEAD note for the
1518              register.  */
1519
1520           dies = NULL_RTX;
1521           for (scan = NEXT_INSN (link); scan; scan = NEXT_INSN (scan))
1522             {
1523               rtx scanset;
1524
1525               /* Don't try to trace forward past a CODE_LABEL if we haven't
1526                  seen INSN yet.  Ordinarily, we will only find the setting insn
1527                  in LOG_LINKS if it is in the same basic block.  However,
1528                  cross-jumping can insert code labels in between the load and
1529                  the call, and can result in situations where a single call
1530                  insn may have two targets depending on where we came from.  */
1531
1532               if (GET_CODE (scan) == CODE_LABEL && ! foundinsn)
1533                 break;
1534
1535               if (GET_RTX_CLASS (GET_CODE (scan)) != 'i')
1536                 continue;
1537
1538               /* Don't try to trace forward past a JUMP.  To optimize
1539                  safely, we would have to check that all the
1540                  instructions at the jump destination did not use REG.  */
1541
1542               if (GET_CODE (scan) == JUMP_INSN)
1543                 break;
1544
1545               if (! reg_mentioned_p (reg, scan))
1546                 continue;
1547
1548               if (noncall_uses_reg (reg, scan, &scanset))
1549                 break;
1550
1551               if (scan == insn)
1552                 foundinsn = 1;
1553
1554               if (scan != insn && GET_CODE (scan) == CALL_INSN)
1555                 {
1556                   /* There is a function call to this register other
1557                      than the one we are checking.  If we optimize
1558                      this call, we need to rescan again below.  */
1559                   rescan = 1;
1560                 }
1561
1562               /* ??? We shouldn't have to worry about SCANSET here.
1563                  We should just be able to check for a REG_DEAD note
1564                  on a function call.  However, the REG_DEAD notes are
1565                  apparently not dependable around libcalls; c-torture
1566                  execute/920501-2 is a test case.  If SCANSET is set,
1567                  then this insn sets the register, so it must have
1568                  died earlier.  Unfortunately, this will only handle
1569                  the cases in which the register is, in fact, set in a
1570                  later insn.  */
1571
1572               /* ??? We shouldn't have to use FOUNDINSN here.
1573                  However, the LOG_LINKS fields are apparently not
1574                  entirely reliable around libcalls;
1575                  newlib/libm/math/e_pow.c is a test case.  Sometimes
1576                  an insn will appear in LOG_LINKS even though it is
1577                  not the most recent insn which sets the register. */
1578
1579               if (foundinsn
1580                   && (scanset
1581                       || find_reg_note (scan, REG_DEAD, reg)))
1582                 {
1583                   dies = scan;
1584                   break;
1585                 }
1586             }
1587
1588           if (! dies)
1589             {
1590               /* Either there was a branch, or some insn used REG
1591                  other than as a function call address.  */
1592               continue;
1593             }
1594
1595           /* Create a code label, and put it in a REG_LABEL note on
1596              the insn which sets the register, and on each call insn
1597              which uses the register.  In final_prescan_insn we look
1598              for the REG_LABEL notes, and output the appropriate label
1599              or pseudo-op.  */
1600
1601           label = gen_label_rtx ();
1602           REG_NOTES (link) = gen_rtx (EXPR_LIST, REG_LABEL, label,
1603                                       REG_NOTES (link));
1604           REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, label,
1605                                       REG_NOTES (insn));
1606           if (rescan)
1607             {
1608               scan = link;
1609               do
1610                 {
1611                   scan = NEXT_INSN (scan);
1612                   if (scan != insn
1613                       && GET_CODE (scan) == CALL_INSN
1614                       && reg_mentioned_p (reg, scan))
1615                     REG_NOTES (scan) = gen_rtx (EXPR_LIST, REG_LABEL,
1616                                                 label, REG_NOTES (scan));
1617                 }
1618               while (scan != dies);
1619             }
1620         }
1621     }
1622
1623   /* Scan the function looking for move instructions which have to be
1624      changed to pc-relative loads and insert the literal tables.  */
1625
1626   for (insn = first; insn; insn = NEXT_INSN (insn))
1627     {
1628       if (broken_move (insn))
1629         {
1630           rtx scan;
1631           /* Scan ahead looking for a barrier to stick the constant table
1632              behind.  */
1633           rtx barrier = find_barrier (insn);
1634
1635           /* Now find all the moves between the points and modify them.  */
1636           for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
1637             {
1638               if (broken_move (scan))
1639                 {
1640                   rtx pat = PATTERN (scan);
1641                   rtx src = SET_SRC (pat);
1642                   rtx dst = SET_DEST (pat);
1643                   enum machine_mode mode = GET_MODE (dst);
1644                   rtx lab;
1645                   rtx newinsn;
1646                   rtx newsrc;
1647
1648                   if (mode == SImode && hi_const (src))
1649                     {
1650                       int offset = 0;
1651
1652                       mode = HImode;
1653                       while (GET_CODE (dst) == SUBREG)
1654                         {
1655                           offset += SUBREG_WORD (dst);
1656                           dst = SUBREG_REG (dst);
1657                         }
1658                       dst = gen_rtx (REG, HImode, REGNO (dst) + offset);
1659                     }
1660
1661                   lab = add_constant (src, mode);
1662                   newsrc = gen_rtx (MEM, mode,
1663                                     gen_rtx (LABEL_REF, VOIDmode, lab));
1664                   RTX_UNCHANGING_P (newsrc) = 1;
1665                   newinsn = emit_insn_after (gen_rtx (SET, VOIDmode,
1666                                                       dst, newsrc), scan);
1667                   REG_NOTES (newinsn) = REG_NOTES (scan);
1668                   REG_NOTES (scan) = NULL_RTX;
1669                   /* If not optimizing, then delete_insn doesn't remove the
1670                      insn from the chain, and hence is not useful.  We
1671                      convert the instruction to a NOTE in that case.  */
1672                   if (optimize)
1673                     delete_insn (scan);
1674                   else
1675                     {
1676                       PUT_CODE (scan, NOTE);
1677                       NOTE_LINE_NUMBER (scan) = NOTE_INSN_DELETED;
1678                       NOTE_SOURCE_FILE (insn) = 0;
1679                     }
1680                   scan = newinsn;
1681                 }
1682             }
1683           dump_table (barrier);
1684         }
1685     }
1686 }
1687
1688 /* Dump out instruction addresses, which is useful for debugging the
1689    constant pool table stuff.
1690
1691    If relaxing, output the label and pseudo-ops used to link together
1692    calls and the instruction which set the registers.  */
1693
1694 /* ??? This is unnecessary, and probably should be deleted.  This makes
1695    the insn_addresses declaration above unnecessary.  */
1696
1697 /* ??? The addresses printed by this routine for insns are nonsense for
1698    insns which are inside of a sequence where none of the inner insns have
1699    variable length.  This is because the second pass of shorten_branches
1700    does not bother to update them.  */
1701
1702 void
1703 final_prescan_insn (insn, opvec, noperands)
1704      rtx insn;
1705      rtx *opvec;
1706      int noperands;
1707 {
1708   if (TARGET_DUMPISIZE)
1709     fprintf (asm_out_file, "\n! at %04x\n", insn_addresses[INSN_UID (insn)]);
1710
1711   if (TARGET_RELAX)
1712     {
1713       rtx note;
1714
1715       note = find_reg_note (insn, REG_LABEL, NULL_RTX);
1716       if (note)
1717         {
1718           rtx pattern;
1719
1720           pattern = PATTERN (insn);
1721           if (GET_CODE (pattern) == PARALLEL)
1722             pattern = XVECEXP (pattern, 0, 0);
1723           if (GET_CODE (pattern) == CALL
1724               || (GET_CODE (pattern) == SET
1725                   && GET_CODE (SET_SRC (pattern)) == CALL))
1726             fprintf (asm_out_file, "\t.uses L%d\n",
1727                      CODE_LABEL_NUMBER (XEXP (note, 0)));
1728           else if (GET_CODE (pattern) == SET)
1729             ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
1730                                        CODE_LABEL_NUMBER (XEXP (note, 0)));
1731           else
1732             abort ();
1733         }
1734     }
1735 }
1736
1737 /* Dump out any constants accumulated in the final pass.  These will
1738    will only be labels.  */
1739
1740 char *
1741 output_jump_label_table ()
1742 {
1743   int i;
1744
1745   if (pool_size)
1746     {
1747       fprintf (asm_out_file, "\t.align 2\n");
1748       for (i = 0; i < pool_size; i++)
1749         {
1750           pool_node *p = &pool_vector[i];
1751
1752           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
1753                                      CODE_LABEL_NUMBER (p->label));
1754           output_asm_insn (".long       %O0", &p->value);
1755         }
1756       pool_size = 0;
1757     }
1758
1759   return "";
1760 }
1761 \f
1762 /* A full frame looks like:
1763
1764    arg-5
1765    arg-4
1766    [ if current_function_anonymous_args
1767    arg-3
1768    arg-2
1769    arg-1
1770    arg-0 ]
1771    saved-fp
1772    saved-r10
1773    saved-r11
1774    saved-r12
1775    saved-pr
1776    local-n
1777    ..
1778    local-1
1779    local-0        <- fp points here.  */
1780
1781 /* Number of bytes pushed for anonymous args, used to pass information
1782    between expand_prologue and expand_epilogue.  */
1783
1784 static int extra_push;
1785
1786 /* Adjust the stack and return the number of bytes taken to do it.  */
1787
1788 static void
1789 output_stack_adjust (size, reg)
1790      int size;
1791      rtx reg;
1792 {
1793   if (size)
1794     {
1795       rtx val = GEN_INT (size);
1796       rtx insn;
1797
1798       if (! CONST_OK_FOR_I (size))
1799         {
1800           rtx reg = gen_rtx (REG, SImode, 3);
1801           emit_insn (gen_movsi (reg, val));
1802           val = reg;
1803         }
1804
1805       insn = gen_addsi3 (reg, reg, val);
1806       emit_insn (insn);
1807     }
1808 }
1809
1810 /* Output RTL to push register RN onto the stack.  */
1811
1812 static void
1813 push (rn)
1814      int rn;
1815 {
1816   rtx x;
1817   if ((rn >= FIRST_FP_REG && rn <= LAST_FP_REG)
1818       || rn == FPUL_REG)
1819     x = emit_insn (gen_push_e (gen_rtx (REG, SFmode, rn)));
1820   else
1821     x = emit_insn (gen_push (gen_rtx (REG, SImode, rn)));
1822
1823   REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
1824                            gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
1825 }
1826
1827 /* Output RTL to pop register RN from the stack.  */
1828
1829 static void
1830 pop (rn)
1831      int rn;
1832 {
1833   rtx x;
1834   if ((rn >= FIRST_FP_REG && rn <= LAST_FP_REG)
1835       || rn == FPUL_REG)
1836     x = emit_insn (gen_pop_e (gen_rtx (REG, SFmode, rn)));
1837   else
1838     x = emit_insn (gen_pop (gen_rtx (REG, SImode, rn)));
1839     
1840   REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
1841                            gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
1842 }
1843
1844 /* Generate code to push the regs specified in the mask, and return
1845    the number of bytes the insns take.  */
1846
1847 static void
1848 push_regs (mask, mask2)
1849      int mask, mask2;
1850 {
1851   int i;
1852
1853   for (i = 0; i < 32; i++)
1854     if (mask & (1 << i))
1855       push (i);
1856   for (i = 32; i < FIRST_PSEUDO_REGISTER; i++)
1857     if (mask2 & (1 << (i - 32)))
1858       push (i);
1859 }
1860
1861 /* Work out the registers which need to be saved, both as a mask and a
1862    count.
1863
1864    If doing a pragma interrupt function, then push all regs used by the
1865    function, and if we call another function (we can tell by looking at PR),
1866    make sure that all the regs it clobbers are safe too.  */
1867
1868 static int
1869 calc_live_regs (count_ptr, live_regs_mask2)
1870      int *count_ptr;
1871      int *live_regs_mask2;
1872 {
1873   int reg;
1874   int live_regs_mask = 0;
1875   int count = 0;
1876
1877   *live_regs_mask2 = 0;
1878   for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
1879     {
1880       if (pragma_interrupt && ! pragma_trapa)
1881         {
1882           /* Need to save all the regs ever live.  */
1883           if ((regs_ever_live[reg]
1884                || (call_used_regs[reg] && ! fixed_regs[reg]
1885                    && regs_ever_live[PR_REG])
1886                || reg == MACH_REG
1887                || reg == MACL_REG)
1888               && reg != STACK_POINTER_REGNUM && reg != ARG_POINTER_REGNUM
1889               && reg != T_REG && reg != GBR_REG)
1890             {
1891               if (reg >= 32)
1892                 *live_regs_mask2 |= 1 << (reg - 32);
1893               else
1894                 live_regs_mask |= 1 << reg;
1895               count++;
1896             }
1897         }
1898       else
1899         {
1900           /* Only push those regs which are used and need to be saved.  */
1901           if (regs_ever_live[reg] && ! call_used_regs[reg])
1902             {
1903               if (reg >= 32)
1904                 *live_regs_mask2 |= 1 << (reg - 32);
1905               else
1906                 live_regs_mask |= (1 << reg);
1907               count++;
1908             }
1909         }
1910     }
1911
1912   *count_ptr = count;
1913   return live_regs_mask;
1914 }
1915
1916 /* Code to generate prologue and epilogue sequences */
1917
1918 void
1919 sh_expand_prologue ()
1920 {
1921   int live_regs_mask;
1922   int d, i;
1923   int live_regs_mask2;
1924   live_regs_mask = calc_live_regs (&d, &live_regs_mask2);
1925
1926   /* We have pretend args if we had an object sent partially in registers
1927      and partially on the stack, e.g. a large structure.  */
1928   output_stack_adjust (-current_function_pretend_args_size, stack_pointer_rtx);
1929
1930   extra_push = 0;
1931
1932   /* This is set by SETUP_VARARGS to indicate that this is a varargs
1933      routine.  Clear it here so that the next function isn't affected. */
1934   if (current_function_anonymous_args)
1935     {
1936       current_function_anonymous_args = 0;
1937
1938       /* This is not used by the SH3E calling convention  */
1939       if (!TARGET_SH3E)
1940         {
1941           /* Push arg regs as if they'd been provided by caller in stack.  */
1942           for (i = 0; i < NPARM_REGS(SImode); i++)
1943             {
1944               int rn = NPARM_REGS(SImode) + FIRST_PARM_REG - i - 1;
1945               if (i > (NPARM_REGS(SImode) 
1946                        - current_function_args_info.arg_count[(int) SH_ARG_INT]
1947                        - current_function_varargs))
1948                 break;
1949               push (rn);
1950               extra_push += 4;
1951             }
1952         }
1953     }
1954
1955   push_regs (live_regs_mask, live_regs_mask2);
1956
1957   output_stack_adjust (-get_frame_size (), stack_pointer_rtx);
1958
1959   if (frame_pointer_needed)
1960     emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
1961 }
1962
1963 void
1964 sh_expand_epilogue ()
1965 {
1966   int live_regs_mask;
1967   int d, i;
1968
1969   int live_regs_mask2;
1970   live_regs_mask = calc_live_regs (&d, &live_regs_mask2);
1971
1972   if (frame_pointer_needed)
1973     {
1974       /* We deliberately make the add dependent on the frame_pointer,
1975          to ensure that instruction scheduling won't move the stack pointer
1976          adjust before instructions reading from the frame.  This can fail
1977          if there is an interrupt which then writes to the stack.  */
1978       output_stack_adjust (get_frame_size (), frame_pointer_rtx);
1979       emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
1980     }
1981   else
1982     output_stack_adjust (get_frame_size (), stack_pointer_rtx);
1983
1984   /* Pop all the registers.  */
1985
1986   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1987     {
1988       int j = (FIRST_PSEUDO_REGISTER - 1) - i;
1989       if (j < 32 && (live_regs_mask & (1 << j)))
1990         pop (j);
1991       else if (j >= 32 && (live_regs_mask2 & (1 << (j - 32))))
1992         pop (j);
1993     }
1994
1995   output_stack_adjust (extra_push + current_function_pretend_args_size,
1996                        stack_pointer_rtx);
1997 }
1998
1999 /* Clear variables at function end.  */
2000
2001 void
2002 function_epilogue (stream, size)
2003      FILE *stream;
2004      int size;
2005 {
2006   pragma_interrupt = pragma_trapa = pragma_nosave_low_regs = 0;
2007 }
2008
2009 rtx
2010 sh_builtin_saveregs (arglist)
2011      tree arglist;
2012 {
2013   tree fntype = TREE_TYPE (current_function_decl);
2014   /* First unnamed integer register.  */
2015   int first_intreg = current_function_args_info.arg_count[(int) SH_ARG_INT];
2016   /* Number of integer registers we need to save.  */
2017   int n_intregs = MAX (0, NPARM_REGS (SImode) - first_intreg);
2018   /* First unnamed SFmode float reg */
2019   int first_floatreg = current_function_args_info.arg_count[(int) SH_ARG_FLOAT];
2020   /* Number of SFmode float regs to save.  */
2021   int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
2022   int ptrsize = GET_MODE_SIZE (Pmode);
2023   rtx valist, regbuf, fpregs;
2024   int bufsize, regno;
2025
2026   /* Allocate block of memory for the regs. */
2027   /* ??? If n_intregs + n_floatregs == 0, should we allocate at least 1 byte?
2028      Or can assign_stack_local accept a 0 SIZE argument?  */
2029   bufsize = (n_intregs * UNITS_PER_WORD) + (n_floatregs * UNITS_PER_WORD);
2030
2031   regbuf = assign_stack_local (BLKmode, bufsize, 0);
2032   MEM_IN_STRUCT_P (regbuf) = 1;
2033
2034   /* Save int args.
2035      This is optimized to only save the regs that are necessary.  Explicitly
2036      named args need not be saved.  */
2037   if (n_intregs > 0)
2038     move_block_from_reg (BASE_ARG_REG (SImode) + first_intreg,
2039                          gen_rtx (MEM, BLKmode, 
2040                                 plus_constant (XEXP (regbuf, 0),
2041                                         n_floatregs * UNITS_PER_WORD)), 
2042                          n_intregs, n_intregs * UNITS_PER_WORD);
2043
2044   /* Save float args.
2045      This is optimized to only save the regs that are necessary.  Explicitly
2046      named args need not be saved.
2047      We explicitly build a pointer to the buffer because it halves the insn
2048      count when not optimizing (otherwise the pointer is built for each reg
2049      saved).  */
2050
2051   fpregs = gen_reg_rtx (Pmode);
2052   emit_move_insn (fpregs, XEXP (regbuf, 0));
2053   for (regno = first_floatreg; regno < NPARM_REGS (SFmode); regno ++)
2054     emit_move_insn (gen_rtx (MEM, SFmode,
2055                              plus_constant (fpregs,
2056                                             GET_MODE_SIZE (SFmode)
2057                                             * (regno - first_floatreg))),
2058                     gen_rtx (REG, SFmode,
2059                              BASE_ARG_REG (SFmode) + regno));
2060
2061   /* Return the address of the regbuf.  */
2062   return XEXP (regbuf, 0);
2063 }
2064
2065 /* Define the offset between two registers, one to be eliminated, and
2066    the other its replacement, at the start of a routine.  */
2067
2068 int
2069 initial_elimination_offset (from, to)
2070      int from;
2071      int to;
2072 {
2073   int regs_saved;
2074   int total_saved_regs_space;
2075   int total_auto_space = get_frame_size ();
2076
2077   int live_regs_mask2;
2078   calc_live_regs (&regs_saved, &live_regs_mask2);
2079
2080   total_saved_regs_space = (regs_saved) * 4;
2081
2082   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
2083     return total_saved_regs_space + total_auto_space;
2084
2085   if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
2086     return total_saved_regs_space + total_auto_space;
2087
2088   /* Initial gap between fp and sp is 0.  */
2089   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
2090     return 0;
2091
2092   abort ();
2093 }
2094 \f
2095 /* Handle machine specific pragmas to be semi-compatible with Hitachi
2096    compiler.  */
2097
2098 int
2099 handle_pragma (file, t)
2100      FILE *file;
2101      tree t;
2102 {
2103   int retval = 0;
2104   register char *pname;
2105
2106   if (TREE_CODE (t) != IDENTIFIER_NODE)
2107     return 0;
2108
2109   pname = IDENTIFIER_POINTER (t);
2110   if (strcmp (pname, "interrupt") == 0)
2111     pragma_interrupt = retval = 1;
2112   else if (strcmp (pname, "trapa") == 0)
2113     pragma_interrupt = pragma_trapa = retval = 1;
2114   else if (strcmp (pname, "nosave_low_regs") == 0)
2115     pragma_nosave_low_regs = retval = 1;
2116
2117   return retval;
2118 }
2119 \f
2120 /* Predicates used by the templates.  */
2121
2122 /* Returns 1 if OP is MACL, MACH or PR.  The input must be a REG rtx.
2123    Used only in general_movsrc_operand.  */
2124
2125 int
2126 system_reg_operand (op, mode)
2127      rtx op;
2128      enum machine_mode mode;
2129 {
2130   switch (REGNO (op))
2131     {
2132     case PR_REG:
2133     case MACL_REG:
2134     case MACH_REG:
2135       return 1;
2136     }
2137   return 0;
2138 }
2139
2140 /* Returns 1 if OP can be source of a simple move operation.
2141    Same as general_operand, but a LABEL_REF is valid, PRE_DEC is
2142    invalid as are subregs of system registers.  */
2143
2144 int
2145 general_movsrc_operand (op, mode)
2146      rtx op;
2147      enum machine_mode mode;
2148 {
2149   if (GET_CODE (op) == MEM)
2150     {
2151       rtx inside = XEXP (op, 0);
2152       if (GET_CODE (inside) == CONST)
2153         inside = XEXP (inside, 0);
2154
2155       if (GET_CODE (inside) == LABEL_REF)
2156         return 1;
2157
2158       if (GET_CODE (inside) == PLUS
2159           && GET_CODE (XEXP (inside, 0)) == LABEL_REF
2160           && GET_CODE (XEXP (inside, 1)) == CONST_INT)
2161         return 1;
2162
2163       /* Only post inc allowed.  */
2164       if (GET_CODE (inside) == PRE_DEC)
2165         return 0;
2166     }
2167
2168   if ((mode == QImode || mode == HImode)
2169       && (GET_CODE (op) == SUBREG
2170           && GET_CODE (XEXP (op, 0)) == REG
2171           && system_reg_operand (XEXP (op, 0), mode)))
2172     return 0;
2173
2174   return general_operand (op, mode);
2175 }
2176
2177 /* Returns 1 if OP can be a destination of a move.
2178    Same as general_operand, but no preinc allowed.  */
2179
2180 int
2181 general_movdst_operand (op, mode)
2182      rtx op;
2183      enum machine_mode mode;
2184 {
2185   /* Only pre dec allowed.  */
2186   if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
2187     return 0;
2188
2189   return general_operand (op, mode);
2190 }
2191
2192 /* Returns 1 if OP is a normal arithmetic register.  */
2193
2194 int
2195 arith_reg_operand (op, mode)
2196      rtx op;
2197      enum machine_mode mode;
2198 {
2199   if (register_operand (op, mode))
2200     {
2201       if (GET_CODE (op) == REG)
2202         return (REGNO (op) != T_REG
2203                 && REGNO (op) != PR_REG
2204                 && REGNO (op) != FPUL_REG
2205                 && REGNO (op) != MACH_REG
2206                 && REGNO (op) != MACL_REG);
2207       return 1;
2208     }
2209   return 0;
2210 }
2211
2212 /* Returns 1 if OP is a valid source operand for an arithmetic insn.  */
2213
2214 int
2215 arith_operand (op, mode)
2216      rtx op;
2217      enum machine_mode mode;
2218 {
2219   if (arith_reg_operand (op, mode))
2220     return 1;
2221
2222   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I (INTVAL (op)))
2223     return 1;
2224
2225   return 0;
2226 }
2227
2228 /* Returns 1 if OP is a valid source operand for a compare insn.  */
2229
2230 int
2231 arith_reg_or_0_operand (op, mode)
2232      rtx op;
2233      enum machine_mode mode;
2234 {
2235   if (arith_reg_operand (op, mode))
2236     return 1;
2237
2238   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_N (INTVAL (op)))
2239     return 1;
2240
2241   return 0;
2242 }
2243
2244 /* Returns 1 if OP is a valid source operand for a logical operation.  */
2245
2246 int
2247 logical_operand (op, mode)
2248      rtx op;
2249      enum machine_mode mode;
2250 {
2251   if (arith_reg_operand (op, mode))
2252     return 1;
2253
2254   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_L (INTVAL (op)))
2255     return 1;
2256
2257   return 0;
2258 }
2259
2260 /* Nonzero if OP is a floating point value with value 0.0.  */
2261
2262 int
2263 fp_zero_operand (op)
2264      rtx op;
2265 {
2266   REAL_VALUE_TYPE r;
2267
2268   if (GET_MODE (op) != SFmode)
2269     return 0;
2270
2271   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
2272   return REAL_VALUES_EQUAL (r, dconst0) && ! REAL_VALUE_MINUS_ZERO (r);
2273 }
2274
2275 /* Nonzero if OP is a floating point value with value 1.0.  */
2276
2277 int
2278 fp_one_operand (op)
2279      rtx op;
2280 {
2281   REAL_VALUE_TYPE r;
2282
2283   if (GET_MODE (op) != SFmode)
2284     return 0;
2285
2286   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
2287   return REAL_VALUES_EQUAL (r, dconst1);
2288 }
2289 \f
2290 /* Return non-zero if REG is not used after INSN.
2291    We assume REG is a reload reg, and therefore does
2292    not live past labels.  It may live past calls or jumps though.  */
2293 int
2294 reg_unused_after (reg, insn)
2295      rtx reg;
2296      rtx insn;
2297 {
2298   enum rtx_code code;
2299   rtx set;
2300
2301   /* If the reg is set by this instruction, then it is safe for our
2302      case.  Disregard the case where this is a store to memory, since
2303      we are checking a register used in the store address.  */
2304   set = single_set (insn);
2305   if (set && GET_CODE (SET_DEST (set)) != MEM
2306       && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2307     return 1;
2308
2309   while (insn = NEXT_INSN (insn))
2310     {
2311       code = GET_CODE (insn);
2312
2313 #if 0
2314       /* If this is a label that existed before reload, then the register
2315          if dead here.  However, if this is a label added by reorg, then
2316          the register may still be live here.  We can't tell the difference,
2317          so we just ignore labels completely.  */
2318       if (code == CODE_LABEL)
2319         return 1;
2320       /* else */
2321 #endif
2322
2323       if (code == JUMP_INSN)
2324         return 0;
2325
2326       /* If this is a sequence, we must handle them all at once.
2327          We could have for instance a call that sets the target register,
2328          and a insn in a delay slot that uses the register.  In this case,
2329          we must return 0.  */
2330       else if (code == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
2331         {
2332           int i;
2333           int retval = 0;
2334
2335           for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2336             {
2337               rtx this_insn = XVECEXP (PATTERN (insn), 0, i);
2338               rtx set = single_set (this_insn);
2339
2340               if (GET_CODE (this_insn) == CALL_INSN)
2341                 code = CALL_INSN;
2342               else if (GET_CODE (this_insn) == JUMP_INSN)
2343                 {
2344                   if (INSN_ANNULLED_BRANCH_P (this_insn))
2345                     return 0;
2346                   code = JUMP_INSN;
2347                 }
2348
2349               if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
2350                 return 0;
2351               if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2352                 {
2353                   if (GET_CODE (SET_DEST (set)) != MEM)
2354                     retval = 1;
2355                   else
2356                     return 0;
2357                 }
2358               if (set == 0
2359                   && reg_overlap_mentioned_p (reg, PATTERN (this_insn)))
2360                 return 0;
2361             }
2362           if (retval == 1)
2363             return 1;
2364           else if (code == JUMP_INSN)
2365             return 0;
2366         }
2367       else if (GET_RTX_CLASS (code) == 'i')
2368         {
2369           rtx set = single_set (insn);
2370
2371           if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
2372             return 0;
2373           if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2374             return GET_CODE (SET_DEST (set)) != MEM;
2375           if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
2376             return 0;
2377         }
2378
2379       if (code == CALL_INSN && call_used_regs[REGNO (reg)])
2380         return 1;
2381     }
2382   return 1;
2383 }