OSDN Git Service

Align code at the start of loops and after branches. Don't accept
[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.  */
560   adjusted_length = length;
561   ADJUST_INSN_LENGTH (insn, adjusted_length);
562   length -= (adjusted_length - length);
563
564   switch (length)
565     {
566     case 2:
567       /* A branch with an unfilled delay slot.  */
568     case 4:
569       /* Simple branch in range -252..+258 bytes */
570       return logic ? "bt%.      %l0" : "bf%.    %l0";
571
572     case 6:
573       /* A branch with an unfilled delay slot.  */
574     case 8:
575       /* Branch in range -4092..+4098 bytes.  */
576       {
577         /* The call to print_slot will clobber the operands.  */
578         rtx op0 = operands[0];
579
580         /* If the instruction in the delay slot is annulled (true), then
581            there is no delay slot where we can put it now.  The only safe
582            place for it is after the label.  */
583
584         if (final_sequence)
585           {
586             fprintf (asm_out_file, "\tb%c%s\tLF%d\n", logic ? 'f' : 't',
587                      INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
588                      ? "" : ".s", label);
589             if (! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
590               print_slot (final_sequence);
591           }
592         else
593           fprintf (asm_out_file, "\tb%c\tLF%d\n", logic ? 'f' : 't', label);
594
595         output_asm_insn ("bra   %l0", &op0);
596         fprintf (asm_out_file, "\tnop\n");
597         fprintf (asm_out_file, "LF%d:\n", label);
598
599         if (final_sequence
600             && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
601           print_slot (final_sequence);
602       }
603       return "";
604
605     case 16:
606       /* A branch with an unfilled delay slot.  */
607     case 18:
608       /* Branches a long way away.  */
609       {
610         /* The call to print_slot will clobber the operands.  */
611         rtx op0 = operands[0];
612
613         /* If the instruction in the delay slot is annulled (true), then
614            there is no delay slot where we can put it now.  The only safe
615            place for it is after the label.  */
616
617         if (final_sequence)
618           {
619             fprintf (asm_out_file, "\tb%c%s\tLF%d\n", logic ? 'f' : 't',
620                      INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
621                      ? "" : ".s", label);
622             if (! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
623               print_slot (final_sequence);
624           }
625         else
626           fprintf (asm_out_file, "\tb%c\tLF%d\n", logic ? 'f' : 't', label);
627
628         output_far_jump (insn, op0);
629         fprintf (asm_out_file, "LF%d:\n", label);
630
631         if (final_sequence
632             && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
633           print_slot (final_sequence);
634       }
635       return "";
636     }
637
638   abort ();
639 }
640 \f
641 /* Output to FILE the start of the assembler file.  */
642
643 void
644 output_file_start (file)
645      FILE *file;
646 {
647   register int pos;
648
649   output_file_directive (file, main_input_filename);
650
651   /* Switch to the data section so that the coffsem symbol and the
652      gcc2_compiled. symbol aren't in the text section.  */
653   data_section ();
654
655   if (TARGET_LITTLE_ENDIAN)
656     fprintf (file, "\t.little\n");
657 }
658 \f
659 /* Actual number of instructions used to make a shift by N.  */
660 static char ashiftrt_insns[] =
661   { 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};
662
663 /* Left shift and logical right shift are the same.  */
664 static char shift_insns[]    =
665   { 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};
666
667 /* Individual shift amounts needed to get the above length sequences.
668    One bit right shifts clobber the T bit, so when possible, put one bit
669    shifts in the middle of the sequence, so the ends are eligible for
670    branch delay slots.  */
671 static short shift_amounts[32][5] = {
672   {0}, {1}, {2}, {2, 1},
673   {2, 2}, {2, 1, 2}, {2, 2, 2}, {2, 2, 1, 2},
674   {8}, {8, 1}, {8, 2}, {8, 1, 2},
675   {8, 2, 2}, {8, 2, 1, 2}, {8, -2, 8}, {8, -1, 8},
676   {16}, {16, 1}, {16, 2}, {16, 1, 2},
677   {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
678   {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
679   {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
680
681 /* This is used in length attributes in sh.md to help compute the length
682    of arbitrary constant shift instructions.  */
683
684 int
685 shift_insns_rtx (insn)
686      rtx insn;
687 {
688   rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
689   int shift_count = INTVAL (XEXP (set_src, 1));
690   enum rtx_code shift_code = GET_CODE (set_src);
691
692   switch (shift_code)
693     {
694     case ASHIFTRT:
695       return ashiftrt_insns[shift_count];
696     case LSHIFTRT:
697     case ASHIFT:
698       return shift_insns[shift_count];
699     default:
700       abort();
701     }
702 }
703
704 /* Return the cost of a shift.  */
705
706 int
707 shiftcosts (x)
708      rtx x;
709 {
710   int value = INTVAL (XEXP (x, 1));
711
712   /* If shift by a non constant, then this will be expensive.  */
713   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
714     {
715       if (TARGET_SH3)
716         return 2;
717       /* If not an sh3 then we don't even have an instruction for it.  */
718       return 20;
719     }
720
721   /* Otherwise, return the true cost in instructions.  */
722   if (GET_CODE (x) == ASHIFTRT)
723     {
724       int cost = ashiftrt_insns[value];
725       /* If SH3, then we put the constant in a reg and use shad.  */
726       if (TARGET_SH3 && cost > 3)
727         cost = 3;
728       return cost;
729     }
730   else
731     return shift_insns[value];
732 }
733
734 /* Return the cost of an AND operation.  */
735
736 int
737 andcosts (x)
738      rtx x;
739 {
740   int i;
741
742   /* Anding with a register is a single cycle and instruction.  */
743   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
744     return 1;
745
746   i = INTVAL (XEXP (x, 1));
747   /* These constants are single cycle extu.[bw] instructions.  */
748   if (i == 0xff || i == 0xffff)
749     return 1;
750   /* Constants that can be used in an and immediate instruction is a single
751      cycle, but this requires r0, so make it a little more expensive.  */
752   if (CONST_OK_FOR_L (i))
753     return 2;
754   /* Constants that can be loaded with a mov immediate and an and.
755      This case is probably unnecessary.  */
756   if (CONST_OK_FOR_I (i))
757     return 2;
758   /* Any other constants requires a 2 cycle pc-relative load plus an and.
759      This case is probably unnecessary.  */
760   return 3;
761 }
762
763 /* Return the cost of a multiply.  */
764 int
765 multcosts (x)
766      rtx x;
767 {
768   if (TARGET_SH2)
769     {
770       /* We have a mul insn, so we can never take more than the mul and the
771          read of the mac reg, but count more because of the latency and extra
772          reg usage.  */
773       if (TARGET_SMALLCODE)
774         return 2;
775       return 3;
776     }
777
778   /* If we're aiming at small code, then just count the number of
779      insns in a multiply call sequence.  */
780   if (TARGET_SMALLCODE)
781     return 5;
782
783   /* Otherwise count all the insns in the routine we'd be calling too.  */
784   return 20;
785 }
786
787 /* Code to expand a shift.  */
788
789 void
790 gen_ashift (type, n, reg)
791      int type;
792      int n;
793      rtx reg;
794 {
795   /* Negative values here come from the shift_amounts array.  */
796   if (n < 0)
797     {
798       if (type == ASHIFT)
799         type = LSHIFTRT;
800       else
801         type = ASHIFT;
802       n = -n;
803     }
804
805   switch (type)
806     {
807     case ASHIFTRT:
808       emit_insn (gen_ashrsi3_k (reg, reg, GEN_INT (n)));
809       break;
810     case LSHIFTRT:
811       if (n == 1)
812         emit_insn (gen_lshrsi3_m (reg, reg, GEN_INT (n)));
813       else
814         emit_insn (gen_lshrsi3_k (reg, reg, GEN_INT (n)));
815       break;
816     case ASHIFT:
817       emit_insn (gen_ashlsi3_k (reg, reg, GEN_INT (n)));
818       break;
819     }
820 }
821
822 /* Output RTL to split a constant shift into its component SH constant
823    shift instructions.  */
824    
825 int
826 gen_shifty_op (code, operands)
827      int code;
828      rtx *operands;
829 {
830   int value = INTVAL (operands[2]);
831   int max, i;
832
833   /* Truncate the shift count in case it is out of bounds.  */
834   value = value & 0x1f;
835  
836   if (value == 31)
837     {
838       if (code == LSHIFTRT)
839         {
840           emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
841           emit_insn (gen_movt (operands[0]));
842           return;
843         }
844       else if (code == ASHIFT)
845         {
846           /* There is a two instruction sequence for 31 bit left shifts,
847              but it requires r0.  */
848           if (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 0)
849             {
850               emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
851               emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
852               return;
853             }
854         }
855     }
856   else if (value == 0)
857     {
858       /* This can happen when not optimizing.  We must output something here
859          to prevent the compiler from aborting in final.c after the try_split
860          call.  */
861       emit_insn (gen_nop ());
862       return;
863     }
864
865   max = shift_insns[value];
866   for (i = 0; i < max; i++)
867     gen_ashift (code, shift_amounts[value][i], operands[0]);
868 }
869
870 /* Output RTL for an arithmetic right shift.  */
871
872 /* ??? Rewrite to use super-optimizer sequences.  */
873
874 int
875 expand_ashiftrt (operands)
876      rtx *operands;
877 {
878   rtx wrk;
879   char func[18];
880   tree func_name;
881   int value;
882
883   if (TARGET_SH3)
884     {
885       if (GET_CODE (operands[2]) != CONST_INT)
886         {
887           rtx count = copy_to_mode_reg (SImode, operands[2]);
888           emit_insn (gen_negsi2 (count, count));
889           emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
890           return 1;
891         }
892       else if (ashiftrt_insns[INTVAL (operands[2])] > 3)
893         {
894           rtx count = force_reg (SImode, GEN_INT (- INTVAL (operands[2])));
895           emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
896           return 1;
897         }
898     }
899   if (GET_CODE (operands[2]) != CONST_INT)
900     return 0;
901
902   value = INTVAL (operands[2]);
903
904   if (value == 31)
905     {
906       emit_insn (gen_ashrsi2_31 (operands[0], operands[1]));
907       return 1;
908     }
909   else if (value >= 16 && value <= 19)
910     {
911       wrk = gen_reg_rtx (SImode);
912       emit_insn (gen_ashrsi2_16 (wrk, operands[1]));
913       value -= 16;
914       while (value--)
915         gen_ashift (ASHIFTRT, 1, wrk);
916       emit_move_insn (operands[0], wrk);
917       return 1;
918     }
919   /* Expand a short sequence inline, longer call a magic routine.  */
920   else if (value <= 5)
921     {
922       wrk = gen_reg_rtx (SImode);
923       emit_move_insn (wrk, operands[1]);
924       while (value--)
925         gen_ashift (ASHIFTRT, 1, wrk);
926       emit_move_insn (operands[0], wrk);
927       return 1;
928     }
929
930   wrk = gen_reg_rtx (Pmode);
931
932   /* Load the value into an arg reg and call a helper.  */
933   emit_move_insn (gen_rtx (REG, SImode, 4), operands[1]);
934   sprintf (func, "__ashiftrt_r4_%d", value);
935   func_name = get_identifier (func);
936   emit_move_insn (wrk, gen_rtx (SYMBOL_REF, Pmode,
937                                 IDENTIFIER_POINTER (func_name)));
938   emit_insn (gen_ashrsi3_n (GEN_INT (value), wrk));
939   emit_move_insn (operands[0], gen_rtx (REG, SImode, 4));
940   return 1;
941 }
942 \f
943 /* The SH cannot load a large constant into a register, constants have to
944    come from a pc relative load.  The reference of a pc relative load
945    instruction must be less than 1k infront of the instruction.  This
946    means that we often have to dump a constant inside a function, and
947    generate code to branch around it.
948
949    It is important to minimize this, since the branches will slow things
950    down and make things bigger.
951
952    Worst case code looks like:
953
954    mov.l L1,rn
955    bra   L2
956    nop
957    align
958    L1:   .long value
959    L2:
960    ..
961
962    mov.l L3,rn
963    bra   L4
964    nop
965    align
966    L3:   .long value
967    L4:
968    ..
969
970    We fix this by performing a scan before scheduling, which notices which
971    instructions need to have their operands fetched from the constant table
972    and builds the table.
973
974    The algorithm is:
975
976    scan, find an instruction which needs a pcrel move.  Look forward, find the
977    last barrier which is within MAX_COUNT bytes of the requirement.
978    If there isn't one, make one.  Process all the instructions between
979    the find and the barrier.
980
981    In the above example, we can tell that L3 is within 1k of L1, so
982    the first move can be shrunk from the 3 insn+constant sequence into
983    just 1 insn, and the constant moved to L3 to make:
984
985    mov.l        L1,rn
986    ..
987    mov.l        L3,rn
988    bra          L4
989    nop
990    align
991    L3:.long value
992    L4:.long value
993
994    Then the second move becomes the target for the shortening process.  */
995
996 typedef struct
997 {
998   rtx value;                    /* Value in table.  */
999   rtx label;                    /* Label of value.  */
1000   enum machine_mode mode;       /* Mode of value.  */
1001 } pool_node;
1002
1003 /* The maximum number of constants that can fit into one pool, since
1004    the pc relative range is 0...1020 bytes and constants are at least 4
1005    bytes long.  */
1006
1007 #define MAX_POOL_SIZE (1020/4)
1008 static pool_node pool_vector[MAX_POOL_SIZE];
1009 static int pool_size;
1010
1011 /* ??? If we need a constant in HImode which is the truncated value of a
1012    constant we need in SImode, we could combine the two entries thus saving
1013    two bytes.  Is this common enough to be worth the effort of implementing
1014    it?  */
1015
1016 /* ??? This stuff should be done at the same time that we shorten branches.
1017    As it is now, we must assume that all branches are the maximum size, and
1018    this causes us to almost always output constant pools sooner than
1019    necessary.  */
1020
1021 /* Add a constant to the pool and return its label.  */
1022
1023 static rtx
1024 add_constant (x, mode)
1025      rtx x;
1026      enum machine_mode mode;
1027 {
1028   int i;
1029   rtx lab;
1030
1031   /* First see if we've already got it.  */
1032   for (i = 0; i < pool_size; i++)
1033     {
1034       if (x->code == pool_vector[i].value->code
1035           && mode == pool_vector[i].mode)
1036         {
1037           if (x->code == CODE_LABEL)
1038             {
1039               if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
1040                 continue;
1041             }
1042           if (rtx_equal_p (x, pool_vector[i].value))
1043             return pool_vector[i].label;
1044         }
1045     }
1046
1047   /* Need a new one.  */
1048   pool_vector[pool_size].value = x;
1049   lab = gen_label_rtx ();
1050   pool_vector[pool_size].mode = mode;
1051   pool_vector[pool_size].label = lab;
1052   pool_size++;
1053   return lab;
1054 }
1055
1056 /* Output the literal table.  */
1057
1058 static void
1059 dump_table (scan)
1060      rtx scan;
1061 {
1062   int i;
1063   int need_align = 1;
1064
1065   /* Do two passes, first time dump out the HI sized constants.  */
1066
1067   for (i = 0; i < pool_size; i++)
1068     {
1069       pool_node *p = &pool_vector[i];
1070
1071       if (p->mode == HImode)
1072         {
1073           if (need_align)
1074             {
1075               scan = emit_insn_after (gen_align_2 (), scan);
1076               need_align = 0;
1077             }
1078           scan = emit_label_after (p->label, scan);
1079           scan = emit_insn_after (gen_consttable_2 (p->value), scan);
1080         }
1081     }
1082
1083   need_align = 1;
1084
1085   for (i = 0; i < pool_size; i++)
1086     {
1087       pool_node *p = &pool_vector[i];
1088
1089       switch (p->mode)
1090         {
1091         case HImode:
1092           break;
1093         case SImode:
1094           if (need_align)
1095             {
1096               need_align = 0;
1097               scan = emit_label_after (gen_label_rtx (), scan);
1098               scan = emit_insn_after (gen_align_4 (), scan);
1099             }
1100           scan = emit_label_after (p->label, scan);
1101           scan = emit_insn_after (gen_consttable_4 (p->value), scan);
1102           break;
1103         case DImode:
1104           if (need_align)
1105             {
1106               need_align = 0;
1107               scan = emit_label_after (gen_label_rtx (), scan);
1108               scan = emit_insn_after (gen_align_4 (), scan);
1109             }
1110           scan = emit_label_after (p->label, scan);
1111           scan = emit_insn_after (gen_consttable_8 (p->value), scan);
1112           break;
1113         default:
1114           abort ();
1115           break;
1116         }
1117     }
1118
1119   scan = emit_insn_after (gen_consttable_end (), scan);
1120   scan = emit_barrier_after (scan);
1121   pool_size = 0;
1122 }
1123
1124 /* Return non-zero if constant would be an ok source for a
1125    mov.w instead of a mov.l.  */
1126
1127 static int
1128 hi_const (src)
1129      rtx src;
1130 {
1131   return (GET_CODE (src) == CONST_INT
1132           && INTVAL (src) >= -32768
1133           && INTVAL (src) <= 32767);
1134 }
1135
1136 /* Non-zero if the insn is a move instruction which needs to be fixed.  */
1137
1138 /* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
1139    CONST_DOUBLE input value is CONST_OK_FOR_I.  For a SFmode move, we don't
1140    need to fix it if the input value is CONST_OK_FOR_I.  */
1141
1142 static int
1143 broken_move (insn)
1144      rtx insn;
1145 {
1146   if (GET_CODE (insn) == INSN
1147       && GET_CODE (PATTERN (insn)) == SET
1148       /* We can load any 8 bit value if we don't care what the high
1149          order bits end up as.  */
1150       && GET_MODE (SET_DEST (PATTERN (insn))) != QImode
1151       && CONSTANT_P (SET_SRC (PATTERN (insn)))
1152       && ! (GET_CODE (SET_SRC (PATTERN (insn))) == CONST_DOUBLE
1153             && (fp_zero_operand (SET_SRC (PATTERN (insn)))
1154                 || fp_one_operand (SET_SRC (PATTERN (insn))))
1155             && GET_CODE (SET_DEST (PATTERN (insn))) == REG
1156             && REGNO (SET_DEST (PATTERN (insn))) >= FIRST_FP_REG
1157             && REGNO (SET_DEST (PATTERN (insn))) <= LAST_FP_REG)
1158       && (GET_CODE (SET_SRC (PATTERN (insn))) != CONST_INT
1159           || ! CONST_OK_FOR_I (INTVAL (SET_SRC (PATTERN (insn))))))
1160     return 1;
1161
1162   return 0;
1163 }
1164
1165 /* Find the last barrier from insn FROM which is close enough to hold the
1166    constant pool.  If we can't find one, then create one near the end of
1167    the range.  */
1168
1169 /* ??? It would be good to put constant pool tables between a case jump and
1170    the jump table.  This fails for two reasons.  First, there is no
1171    barrier after the case jump.  This is a bug in the casesi pattern.
1172    Second, inserting the table here may break the mova instruction that
1173    loads the jump table address, by moving the jump table too far away.
1174    We fix that problem by never outputting the constant pool between a mova
1175    and its label.  */
1176
1177 static rtx
1178 find_barrier (from)
1179      rtx from;
1180 {
1181   int count_si = 0;
1182   int count_hi = 0;
1183   int found_hi = 0;
1184   int found_si = 0;
1185   rtx found_barrier = 0;
1186   rtx found_mova = 0;
1187   int si_limit;
1188   int hi_limit;
1189
1190   /* For HImode: range is 510, add 4 because pc counts from address of
1191      second instruction after this one, subtract 2 for the jump instruction
1192      that we may need to emit before the table, subtract 2 for the instruction
1193      that fills the jump delay slot (in very rare cases, reorg will take an
1194      instruction from after the constant pool or will leave the delay slot
1195      empty).  This gives 510.
1196      For SImode: range is 1020, add 4 because pc counts from address of
1197      second instruction after this one, subtract 2 in case pc is 2 byte
1198      aligned, subtract 2 for the jump instruction that we may need to emit
1199      before the table, subtract 2 for the instruction that fills the jump
1200      delay slot.  This gives 1018.  */
1201
1202   /* If not optimizing, then it is possible that the jump instruction we add
1203      won't be shortened, and thus will have a length of 14 instead of 2.
1204      We must adjust the limits downwards to account for this, giving a limit
1205      of 1008 for SImode and 500 for HImode.  */
1206
1207   if (optimize)
1208     {
1209       si_limit = 1018;
1210       hi_limit = 510;
1211     }
1212   else
1213     {
1214       si_limit = 1008;
1215       hi_limit = 500;
1216     }
1217
1218   /* If not optimizing for space, then the constant pool will be
1219      aligned to a 4 to 16 byte boundary.  We must make room for that
1220      alignment that by reducing the limits.
1221      ??? It would be better to not align the constant pool, but
1222      ASM_OUTPUT_ALIGN_CODE does not make any provision for basing the
1223      alignment on the instruction.  */
1224
1225   if (! TARGET_SMALLCODE)
1226     {
1227       if (TARGET_SH3 || TARGET_SH3E)
1228         {
1229           si_limit -= 14;
1230           hi_limit -= 14;
1231         }
1232       else
1233         {
1234           si_limit -= 2;
1235           hi_limit -= 2;
1236         }
1237     }
1238
1239   while (from && count_si < si_limit && count_hi < hi_limit)
1240     {
1241       int inc = get_attr_length (from);
1242
1243       if (GET_CODE (from) == BARRIER)
1244         found_barrier = from;
1245
1246       if (broken_move (from))
1247         {
1248           rtx pat = PATTERN (from);
1249           rtx src = SET_SRC (pat);
1250           rtx dst = SET_DEST (pat);
1251           enum machine_mode mode = GET_MODE (dst);
1252
1253           /* We must explicitly check the mode, because sometimes the
1254              front end will generate code to load unsigned constants into
1255              HImode targets without properly sign extending them.  */
1256           if (mode == HImode || (mode == SImode && hi_const (src)))
1257             {
1258               found_hi = 1;
1259               /* We put the short constants before the long constants, so
1260                  we must count the length of short constants in the range
1261                  for the long constants.  */
1262               /* ??? This isn't optimal, but is easy to do.  */
1263               if (found_si)
1264                 count_si += 2;
1265             }
1266           else
1267             found_si = 1;
1268         }
1269
1270       if (GET_CODE (from) == INSN
1271           && GET_CODE (PATTERN (from)) == SET
1272           && GET_CODE (SET_SRC (PATTERN (from))) == UNSPEC
1273           && XINT (SET_SRC (PATTERN (from)), 1) == 1)
1274         found_mova = from;
1275       else if (GET_CODE (from) == JUMP_INSN
1276                && (GET_CODE (PATTERN (from)) == ADDR_VEC
1277                    || GET_CODE (PATTERN (from)) == ADDR_DIFF_VEC))
1278         found_mova = 0;
1279
1280       if (found_si)
1281         count_si += inc;
1282       if (found_hi)
1283         count_hi += inc;
1284       from = NEXT_INSN (from);
1285     }
1286
1287   /* Insert the constant pool table before the mova instruction, to prevent
1288      the mova label reference from going out of range.  */
1289   if (found_mova)
1290     from = found_mova;
1291
1292   if (! found_barrier)
1293     {
1294       /* We didn't find a barrier in time to dump our stuff,
1295          so we'll make one.  */
1296       rtx label = gen_label_rtx ();
1297
1298       /* If we exceeded the range, then we must back up over the last
1299          instruction we looked at.  Otherwise, we just need to undo the
1300          NEXT_INSN at the end of the loop.  */
1301       if (count_hi > hi_limit || count_si > si_limit)
1302         from = PREV_INSN (PREV_INSN (from));
1303       else
1304         from = PREV_INSN (from);
1305
1306       /* Walk back to be just before any jump or label.
1307          Putting it before a label reduces the number of times the branch
1308          around the constant pool table will be hit.  Putting it before
1309          a jump makes it more likely that the bra delay slot will be
1310          filled.  */
1311       while (GET_CODE (from) == JUMP_INSN || GET_CODE (from) == NOTE
1312              || GET_CODE (from) == CODE_LABEL)
1313         from = PREV_INSN (from);
1314
1315       from = emit_jump_insn_after (gen_jump (label), from);
1316       JUMP_LABEL (from) = label;
1317       LABEL_NUSES (label) = 1;
1318       found_barrier = emit_barrier_after (from);
1319       emit_label_after (label, found_barrier);
1320     }
1321
1322   return found_barrier;
1323 }
1324
1325 /* See if the only way in which INSN uses REG is by calling it, or by
1326    setting it while calling it.  Set *SET to a SET rtx if the register
1327    is set by INSN.  */
1328
1329 static int
1330 noncall_uses_reg (reg, insn, set)
1331      rtx reg;
1332      rtx insn;
1333      rtx *set;
1334 {
1335   rtx pattern;
1336
1337   *set = NULL_RTX;
1338
1339   if (GET_CODE (insn) != CALL_INSN)
1340     {
1341       /* We don't use rtx_equal_p because we don't care if the mode is
1342          different.  */
1343       pattern = single_set (insn);
1344       if (pattern
1345           && GET_CODE (SET_DEST (pattern)) == REG
1346           && REGNO (reg) == REGNO (SET_DEST (pattern)))
1347         {
1348           *set = pattern;
1349           return 0;
1350         }
1351
1352       return 1;
1353     }
1354
1355   pattern = PATTERN (insn);
1356
1357   if (GET_CODE (pattern) == PARALLEL)
1358     {
1359       int i;
1360
1361       for (i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
1362         if (reg_mentioned_p (reg, XVECEXP (pattern, 0, i)))
1363           return 1;
1364       pattern = XVECEXP (pattern, 0, 0);
1365     }
1366
1367   if (GET_CODE (pattern) == SET)
1368     {
1369       if (reg_mentioned_p (reg, SET_DEST (pattern)))
1370         {
1371           /* We don't use rtx_equal_p, because we don't care if the
1372              mode is different.  */
1373           if (GET_CODE (SET_DEST (pattern)) != REG
1374               || REGNO (reg) != REGNO (SET_DEST (pattern)))
1375             return 1;
1376
1377           *set = pattern;
1378         }
1379
1380       pattern = SET_SRC (pattern);
1381     }
1382
1383   if (GET_CODE (pattern) != CALL
1384       || GET_CODE (XEXP (pattern, 0)) != MEM
1385       || ! rtx_equal_p (reg, XEXP (XEXP (pattern, 0), 0)))
1386     return 1;
1387
1388   return 0;
1389 }
1390
1391 /* Exported to toplev.c.
1392
1393    Do a final pass over the function, just before delayed branch
1394    scheduling.  */
1395
1396 void
1397 machine_dependent_reorg (first)
1398      rtx first;
1399 {
1400   rtx insn;
1401
1402   /* If relaxing, generate pseudo-ops to associate function calls with
1403      the symbols they call.  It does no harm to not generate these
1404      pseudo-ops.  However, when we can generate them, it enables to
1405      linker to potentially relax the jsr to a bsr, and eliminate the
1406      register load and, possibly, the constant pool entry.  */
1407
1408   if (TARGET_RELAX)
1409     {
1410       /* Remove all REG_LABEL notes.  We want to use them for our own
1411          purposes.  This works because none of the remaining passes
1412          need to look at them.
1413
1414          ??? But it may break in the future.  We should use a machine
1415          dependent REG_NOTE, or some other approach entirely.  */
1416       for (insn = first; insn; insn = NEXT_INSN (insn))
1417         {
1418           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1419             {
1420               rtx note;
1421
1422               while ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != 0)
1423                 remove_note (insn, note);
1424             }
1425         }
1426
1427       for (insn = first; insn; insn = NEXT_INSN (insn))
1428         {
1429           rtx pattern, reg, link, set, scan, dies, label;
1430           int rescan = 0, foundinsn = 0;
1431
1432           if (GET_CODE (insn) != CALL_INSN)
1433             continue;
1434
1435           pattern = PATTERN (insn);
1436
1437           if (GET_CODE (pattern) == PARALLEL)
1438             pattern = XVECEXP (pattern, 0, 0);
1439           if (GET_CODE (pattern) == SET)
1440             pattern = SET_SRC (pattern);
1441
1442           if (GET_CODE (pattern) != CALL
1443               || GET_CODE (XEXP (pattern, 0)) != MEM)
1444             continue;
1445
1446           reg = XEXP (XEXP (pattern, 0), 0);
1447           if (GET_CODE (reg) != REG)
1448             continue;
1449
1450           /* This is a function call via REG.  If the only uses of REG
1451              between the time that it is set and the time that it dies
1452              are in function calls, then we can associate all the
1453              function calls with the setting of REG.  */
1454
1455           for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1456             {
1457               set = single_set (XEXP (link, 0));
1458               if (set && rtx_equal_p (reg, SET_DEST (set)))
1459                 {
1460                   link = XEXP (link, 0);
1461                   break;
1462                 }
1463             }
1464
1465           if (! link)
1466             {
1467               /* ??? Sometimes global register allocation will have
1468                  deleted the insn pointed to by LOG_LINKS.  Try
1469                  scanning backward to find where the register is set.  */
1470               for (scan = PREV_INSN (insn);
1471                    scan && GET_CODE (scan) != CODE_LABEL;
1472                    scan = PREV_INSN (scan))
1473                 {
1474                   if (GET_RTX_CLASS (GET_CODE (scan)) != 'i')
1475                     continue;
1476
1477                   if (! reg_mentioned_p (reg, scan))
1478                     continue;
1479
1480                   if (noncall_uses_reg (reg, scan, &set))
1481                     break;
1482
1483                   if (set)
1484                     {
1485                       link = scan;
1486                       break;
1487                     }
1488                 }
1489             }
1490
1491           if (! link)
1492             continue;
1493
1494           /* The register is set at LINK.  */
1495
1496           /* We can only optimize the function call if the register is
1497              being set to a symbol.  In theory, we could sometimes
1498              optimize calls to a constant location, but the assembler
1499              and linker do not support that at present.  */
1500           if (GET_CODE (SET_SRC (set)) != SYMBOL_REF
1501               && GET_CODE (SET_SRC (set)) != LABEL_REF)
1502             continue;
1503
1504           /* Scan forward from LINK to the place where REG dies, and
1505              make sure that the only insns which use REG are
1506              themselves function calls.  */
1507
1508           /* ??? This doesn't work for call targets that were allocated
1509              by reload, since there may not be a REG_DEAD note for the
1510              register.  */
1511
1512           dies = NULL_RTX;
1513           for (scan = NEXT_INSN (link); scan; scan = NEXT_INSN (scan))
1514             {
1515               rtx scanset;
1516
1517               /* Don't try to trace forward past a CODE_LABEL if we haven't
1518                  seen INSN yet.  Ordinarily, we will only find the setting insn
1519                  in LOG_LINKS if it is in the same basic block.  However,
1520                  cross-jumping can insert code labels in between the load and
1521                  the call, and can result in situations where a single call
1522                  insn may have two targets depending on where we came from.  */
1523
1524               if (GET_CODE (scan) == CODE_LABEL && ! foundinsn)
1525                 break;
1526
1527               if (GET_RTX_CLASS (GET_CODE (scan)) != 'i')
1528                 continue;
1529
1530               /* Don't try to trace forward past a JUMP.  To optimize
1531                  safely, we would have to check that all the
1532                  instructions at the jump destination did not use REG.  */
1533
1534               if (GET_CODE (scan) == JUMP_INSN)
1535                 break;
1536
1537               if (! reg_mentioned_p (reg, scan))
1538                 continue;
1539
1540               if (noncall_uses_reg (reg, scan, &scanset))
1541                 break;
1542
1543               if (scan == insn)
1544                 foundinsn = 1;
1545
1546               if (scan != insn && GET_CODE (scan) == CALL_INSN)
1547                 {
1548                   /* There is a function call to this register other
1549                      than the one we are checking.  If we optimize
1550                      this call, we need to rescan again below.  */
1551                   rescan = 1;
1552                 }
1553
1554               /* ??? We shouldn't have to worry about SCANSET here.
1555                  We should just be able to check for a REG_DEAD note
1556                  on a function call.  However, the REG_DEAD notes are
1557                  apparently not dependable around libcalls; c-torture
1558                  execute/920501-2 is a test case.  If SCANSET is set,
1559                  then this insn sets the register, so it must have
1560                  died earlier.  Unfortunately, this will only handle
1561                  the cases in which the register is, in fact, set in a
1562                  later insn.  */
1563
1564               /* ??? We shouldn't have to use FOUNDINSN here.
1565                  However, the LOG_LINKS fields are apparently not
1566                  entirely reliable around libcalls;
1567                  newlib/libm/math/e_pow.c is a test case.  Sometimes
1568                  an insn will appear in LOG_LINKS even though it is
1569                  not the most recent insn which sets the register. */
1570
1571               if (foundinsn
1572                   && (scanset
1573                       || find_reg_note (scan, REG_DEAD, reg)))
1574                 {
1575                   dies = scan;
1576                   break;
1577                 }
1578             }
1579
1580           if (! dies)
1581             {
1582               /* Either there was a branch, or some insn used REG
1583                  other than as a function call address.  */
1584               continue;
1585             }
1586
1587           /* Create a code label, and put it in a REG_LABEL note on
1588              the insn which sets the register, and on each call insn
1589              which uses the register.  In final_prescan_insn we look
1590              for the REG_LABEL notes, and output the appropriate label
1591              or pseudo-op.  */
1592
1593           label = gen_label_rtx ();
1594           REG_NOTES (link) = gen_rtx (EXPR_LIST, REG_LABEL, label,
1595                                       REG_NOTES (link));
1596           REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL, label,
1597                                       REG_NOTES (insn));
1598           if (rescan)
1599             {
1600               scan = link;
1601               do
1602                 {
1603                   scan = NEXT_INSN (scan);
1604                   if (scan != insn
1605                       && GET_CODE (scan) == CALL_INSN
1606                       && reg_mentioned_p (reg, scan))
1607                     REG_NOTES (scan) = gen_rtx (EXPR_LIST, REG_LABEL,
1608                                                 label, REG_NOTES (scan));
1609                 }
1610               while (scan != dies);
1611             }
1612         }
1613     }
1614
1615   /* Scan the function looking for move instructions which have to be
1616      changed to pc-relative loads and insert the literal tables.  */
1617
1618   for (insn = first; insn; insn = NEXT_INSN (insn))
1619     {
1620       if (broken_move (insn))
1621         {
1622           rtx scan;
1623           /* Scan ahead looking for a barrier to stick the constant table
1624              behind.  */
1625           rtx barrier = find_barrier (insn);
1626
1627           /* Now find all the moves between the points and modify them.  */
1628           for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
1629             {
1630               if (broken_move (scan))
1631                 {
1632                   rtx pat = PATTERN (scan);
1633                   rtx src = SET_SRC (pat);
1634                   rtx dst = SET_DEST (pat);
1635                   enum machine_mode mode = GET_MODE (dst);
1636                   rtx lab;
1637                   rtx newinsn;
1638                   rtx newsrc;
1639
1640                   if (mode == SImode && hi_const (src))
1641                     {
1642                       int offset = 0;
1643
1644                       mode = HImode;
1645                       while (GET_CODE (dst) == SUBREG)
1646                         {
1647                           offset += SUBREG_WORD (dst);
1648                           dst = SUBREG_REG (dst);
1649                         }
1650                       dst = gen_rtx (REG, HImode, REGNO (dst) + offset);
1651                     }
1652
1653                   lab = add_constant (src, mode);
1654                   newsrc = gen_rtx (MEM, mode,
1655                                     gen_rtx (LABEL_REF, VOIDmode, lab));
1656                   RTX_UNCHANGING_P (newsrc) = 1;
1657                   newinsn = emit_insn_after (gen_rtx (SET, VOIDmode,
1658                                                       dst, newsrc), scan);
1659                   REG_NOTES (newinsn) = REG_NOTES (scan);
1660                   REG_NOTES (scan) = NULL_RTX;
1661                   /* If not optimizing, then delete_insn doesn't remove the
1662                      insn from the chain, and hence is not useful.  We
1663                      convert the instruction to a NOTE in that case.  */
1664                   if (optimize)
1665                     delete_insn (scan);
1666                   else
1667                     {
1668                       PUT_CODE (scan, NOTE);
1669                       NOTE_LINE_NUMBER (scan) = NOTE_INSN_DELETED;
1670                       NOTE_SOURCE_FILE (insn) = 0;
1671                     }
1672                   scan = newinsn;
1673                 }
1674             }
1675           dump_table (barrier);
1676         }
1677     }
1678 }
1679
1680 /* Dump out instruction addresses, which is useful for debugging the
1681    constant pool table stuff.
1682
1683    If relaxing, output the label and pseudo-ops used to link together
1684    calls and the instruction which set the registers.  */
1685
1686 /* ??? This is unnecessary, and probably should be deleted.  This makes
1687    the insn_addresses declaration above unnecessary.  */
1688
1689 /* ??? The addresses printed by this routine for insns are nonsense for
1690    insns which are inside of a sequence where none of the inner insns have
1691    variable length.  This is because the second pass of shorten_branches
1692    does not bother to update them.  */
1693
1694 void
1695 final_prescan_insn (insn, opvec, noperands)
1696      rtx insn;
1697      rtx *opvec;
1698      int noperands;
1699 {
1700   if (TARGET_DUMPISIZE)
1701     fprintf (asm_out_file, "\n! at %04x\n", insn_addresses[INSN_UID (insn)]);
1702
1703   if (TARGET_RELAX)
1704     {
1705       rtx note;
1706
1707       note = find_reg_note (insn, REG_LABEL, NULL_RTX);
1708       if (note)
1709         {
1710           rtx pattern;
1711
1712           pattern = PATTERN (insn);
1713           if (GET_CODE (pattern) == PARALLEL)
1714             pattern = XVECEXP (pattern, 0, 0);
1715           if (GET_CODE (pattern) == CALL
1716               || (GET_CODE (pattern) == SET
1717                   && GET_CODE (SET_SRC (pattern)) == CALL))
1718             fprintf (asm_out_file, "\t.uses L%d\n",
1719                      CODE_LABEL_NUMBER (XEXP (note, 0)));
1720           else if (GET_CODE (pattern) == SET)
1721             ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
1722                                        CODE_LABEL_NUMBER (XEXP (note, 0)));
1723           else
1724             abort ();
1725         }
1726     }
1727 }
1728
1729 /* Dump out any constants accumulated in the final pass.  These will
1730    will only be labels.  */
1731
1732 char *
1733 output_jump_label_table ()
1734 {
1735   int i;
1736
1737   if (pool_size)
1738     {
1739       fprintf (asm_out_file, "\t.align 2\n");
1740       for (i = 0; i < pool_size; i++)
1741         {
1742           pool_node *p = &pool_vector[i];
1743
1744           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
1745                                      CODE_LABEL_NUMBER (p->label));
1746           output_asm_insn (".long       %O0", &p->value);
1747         }
1748       pool_size = 0;
1749     }
1750
1751   return "";
1752 }
1753 \f
1754 /* A full frame looks like:
1755
1756    arg-5
1757    arg-4
1758    [ if current_function_anonymous_args
1759    arg-3
1760    arg-2
1761    arg-1
1762    arg-0 ]
1763    saved-fp
1764    saved-r10
1765    saved-r11
1766    saved-r12
1767    saved-pr
1768    local-n
1769    ..
1770    local-1
1771    local-0        <- fp points here.  */
1772
1773 /* Number of bytes pushed for anonymous args, used to pass information
1774    between expand_prologue and expand_epilogue.  */
1775
1776 static int extra_push;
1777
1778 /* Adjust the stack and return the number of bytes taken to do it.  */
1779
1780 static void
1781 output_stack_adjust (size, reg)
1782      int size;
1783      rtx reg;
1784 {
1785   if (size)
1786     {
1787       rtx val = GEN_INT (size);
1788       rtx insn;
1789
1790       if (! CONST_OK_FOR_I (size))
1791         {
1792           rtx reg = gen_rtx (REG, SImode, 3);
1793           emit_insn (gen_movsi (reg, val));
1794           val = reg;
1795         }
1796
1797       insn = gen_addsi3 (reg, reg, val);
1798       emit_insn (insn);
1799     }
1800 }
1801
1802 /* Output RTL to push register RN onto the stack.  */
1803
1804 static void
1805 push (rn)
1806      int rn;
1807 {
1808   rtx x;
1809   if ((rn >= FIRST_FP_REG && rn <= LAST_FP_REG)
1810       || rn == FPUL_REG)
1811     x = emit_insn (gen_push_e (gen_rtx (REG, SFmode, rn)));
1812   else
1813     x = emit_insn (gen_push (gen_rtx (REG, SImode, rn)));
1814
1815   REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
1816                            gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
1817 }
1818
1819 /* Output RTL to pop register RN from the stack.  */
1820
1821 static void
1822 pop (rn)
1823      int rn;
1824 {
1825   rtx x;
1826   if ((rn >= FIRST_FP_REG && rn <= LAST_FP_REG)
1827       || rn == FPUL_REG)
1828     x = emit_insn (gen_pop_e (gen_rtx (REG, SFmode, rn)));
1829   else
1830     x = emit_insn (gen_pop (gen_rtx (REG, SImode, rn)));
1831     
1832   REG_NOTES (x) = gen_rtx (EXPR_LIST, REG_INC,
1833                            gen_rtx(REG, SImode, STACK_POINTER_REGNUM), 0);
1834 }
1835
1836 /* Generate code to push the regs specified in the mask, and return
1837    the number of bytes the insns take.  */
1838
1839 static void
1840 push_regs (mask, mask2)
1841      int mask, mask2;
1842 {
1843   int i;
1844
1845   for (i = 0; i < 32; i++)
1846     if (mask & (1 << i))
1847       push (i);
1848   for (i = 32; i < FIRST_PSEUDO_REGISTER; i++)
1849     if (mask2 & (1 << (i - 32)))
1850       push (i);
1851 }
1852
1853 /* Work out the registers which need to be saved, both as a mask and a
1854    count.
1855
1856    If doing a pragma interrupt function, then push all regs used by the
1857    function, and if we call another function (we can tell by looking at PR),
1858    make sure that all the regs it clobbers are safe too.  */
1859
1860 static int
1861 calc_live_regs (count_ptr, live_regs_mask2)
1862      int *count_ptr;
1863      int *live_regs_mask2;
1864 {
1865   int reg;
1866   int live_regs_mask = 0;
1867   int count = 0;
1868
1869   *live_regs_mask2 = 0;
1870   for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
1871     {
1872       if (pragma_interrupt && ! pragma_trapa)
1873         {
1874           /* Need to save all the regs ever live.  */
1875           if ((regs_ever_live[reg]
1876                || (call_used_regs[reg] && regs_ever_live[PR_REG]))
1877               && reg != STACK_POINTER_REGNUM && reg != ARG_POINTER_REGNUM
1878               && reg != T_REG && reg != GBR_REG)
1879             {
1880               if (reg >= 32)
1881                 *live_regs_mask2 |= 1 << (reg - 32);
1882               else
1883                 live_regs_mask |= 1 << reg;
1884               count++;
1885             }
1886         }
1887       else
1888         {
1889           /* Only push those regs which are used and need to be saved.  */
1890           if (regs_ever_live[reg] && ! call_used_regs[reg])
1891             {
1892               if (reg >= 32)
1893                 *live_regs_mask2 |= 1 << (reg - 32);
1894               else
1895                 live_regs_mask |= (1 << reg);
1896               count++;
1897             }
1898         }
1899     }
1900
1901   *count_ptr = count;
1902   return live_regs_mask;
1903 }
1904
1905 /* Code to generate prologue and epilogue sequences */
1906
1907 void
1908 sh_expand_prologue ()
1909 {
1910   int live_regs_mask;
1911   int d, i;
1912   int live_regs_mask2;
1913   live_regs_mask = calc_live_regs (&d, &live_regs_mask2);
1914
1915   /* We have pretend args if we had an object sent partially in registers
1916      and partially on the stack, e.g. a large structure.  */
1917   output_stack_adjust (-current_function_pretend_args_size, stack_pointer_rtx);
1918
1919   extra_push = 0;
1920
1921   /* This is set by SETUP_VARARGS to indicate that this is a varargs
1922      routine.  Clear it here so that the next function isn't affected. */
1923   if (current_function_anonymous_args)
1924     {
1925       current_function_anonymous_args = 0;
1926
1927       /* This is not used by the SH3E calling convention  */
1928       if (!TARGET_SH3E)
1929         {
1930           /* Push arg regs as if they'd been provided by caller in stack.  */
1931           for (i = 0; i < NPARM_REGS(SImode); i++)
1932             {
1933               int rn = NPARM_REGS(SImode) + FIRST_PARM_REG - i - 1;
1934               if (i > (NPARM_REGS(SImode) 
1935                        - current_function_args_info.arg_count[(int) SH_ARG_INT]
1936                        - current_function_varargs))
1937                 break;
1938               push (rn);
1939               extra_push += 4;
1940             }
1941         }
1942     }
1943
1944   push_regs (live_regs_mask, live_regs_mask2);
1945
1946   output_stack_adjust (-get_frame_size (), stack_pointer_rtx);
1947
1948   if (frame_pointer_needed)
1949     emit_insn (gen_movsi (frame_pointer_rtx, stack_pointer_rtx));
1950 }
1951
1952 void
1953 sh_expand_epilogue ()
1954 {
1955   int live_regs_mask;
1956   int d, i;
1957
1958   int live_regs_mask2;
1959   live_regs_mask = calc_live_regs (&d, &live_regs_mask2);
1960
1961   if (frame_pointer_needed)
1962     {
1963       /* We deliberately make the add dependent on the frame_pointer,
1964          to ensure that instruction scheduling won't move the stack pointer
1965          adjust before instructions reading from the frame.  This can fail
1966          if there is an interrupt which then writes to the stack.  */
1967       output_stack_adjust (get_frame_size (), frame_pointer_rtx);
1968       emit_insn (gen_movsi (stack_pointer_rtx, frame_pointer_rtx));
1969     }
1970   else
1971     output_stack_adjust (get_frame_size (), stack_pointer_rtx);
1972
1973   /* Pop all the registers.  */
1974
1975   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1976     {
1977       int j = (FIRST_PSEUDO_REGISTER - 1) - i;
1978       if (j < 32 && (live_regs_mask & (1 << j)))
1979         pop (j);
1980       else if (j >= 32 && (live_regs_mask2 & (1 << (j - 32))))
1981         pop (j);
1982     }
1983
1984   output_stack_adjust (extra_push + current_function_pretend_args_size,
1985                        stack_pointer_rtx);
1986 }
1987
1988 /* Clear variables at function end.  */
1989
1990 void
1991 function_epilogue (stream, size)
1992      FILE *stream;
1993      int size;
1994 {
1995   pragma_interrupt = pragma_trapa = pragma_nosave_low_regs = 0;
1996 }
1997
1998 rtx
1999 sh_builtin_saveregs (arglist)
2000      tree arglist;
2001 {
2002   tree fntype = TREE_TYPE (current_function_decl);
2003   /* First unnamed integer register.  */
2004   int first_intreg = current_function_args_info.arg_count[(int) SH_ARG_INT];
2005   /* Number of integer registers we need to save.  */
2006   int n_intregs = MAX (0, NPARM_REGS (SImode) - first_intreg);
2007   /* First unnamed SFmode float reg */
2008   int first_floatreg = current_function_args_info.arg_count[(int) SH_ARG_FLOAT];
2009   /* Number of SFmode float regs to save.  */
2010   int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
2011   int ptrsize = GET_MODE_SIZE (Pmode);
2012   rtx valist, regbuf, fpregs;
2013   int bufsize, regno;
2014
2015   /* Allocate block of memory for the regs. */
2016   /* ??? If n_intregs + n_floatregs == 0, should we allocate at least 1 byte?
2017      Or can assign_stack_local accept a 0 SIZE argument?  */
2018   bufsize = (n_intregs * UNITS_PER_WORD) + (n_floatregs * UNITS_PER_WORD);
2019
2020   regbuf = assign_stack_local (BLKmode, bufsize, 0);
2021   MEM_IN_STRUCT_P (regbuf) = 1;
2022
2023   /* Save int args.
2024      This is optimized to only save the regs that are necessary.  Explicitly
2025      named args need not be saved.  */
2026   if (n_intregs > 0)
2027     move_block_from_reg (BASE_ARG_REG (SImode) + first_intreg,
2028                          gen_rtx (MEM, BLKmode, 
2029                                 plus_constant (XEXP (regbuf, 0),
2030                                         n_floatregs * UNITS_PER_WORD)), 
2031                          n_intregs, n_intregs * UNITS_PER_WORD);
2032
2033   /* Save float args.
2034      This is optimized to only save the regs that are necessary.  Explicitly
2035      named args need not be saved.
2036      We explicitly build a pointer to the buffer because it halves the insn
2037      count when not optimizing (otherwise the pointer is built for each reg
2038      saved).  */
2039
2040   fpregs = gen_reg_rtx (Pmode);
2041   emit_move_insn (fpregs, XEXP (regbuf, 0));
2042   for (regno = first_floatreg; regno < NPARM_REGS (SFmode); regno ++)
2043     emit_move_insn (gen_rtx (MEM, SFmode,
2044                              plus_constant (fpregs,
2045                                             GET_MODE_SIZE (SFmode)
2046                                             * (regno - first_floatreg))),
2047                     gen_rtx (REG, SFmode,
2048                              BASE_ARG_REG (SFmode) + regno));
2049
2050   /* Return the address of the regbuf.  */
2051   return XEXP (regbuf, 0);
2052 }
2053
2054 /* Define the offset between two registers, one to be eliminated, and
2055    the other its replacement, at the start of a routine.  */
2056
2057 int
2058 initial_elimination_offset (from, to)
2059      int from;
2060      int to;
2061 {
2062   int regs_saved;
2063   int total_saved_regs_space;
2064   int total_auto_space = get_frame_size ();
2065
2066   int live_regs_mask2;
2067   calc_live_regs (&regs_saved, &live_regs_mask2);
2068
2069   total_saved_regs_space = (regs_saved) * 4;
2070
2071   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
2072     return total_saved_regs_space + total_auto_space;
2073
2074   if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
2075     return total_saved_regs_space + total_auto_space;
2076
2077   /* Initial gap between fp and sp is 0.  */
2078   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
2079     return 0;
2080
2081   abort ();
2082 }
2083 \f
2084 /* Handle machine specific pragmas to be semi-compatible with Hitachi
2085    compiler.  */
2086
2087 int
2088 handle_pragma (file, t)
2089      FILE *file;
2090      tree t;
2091 {
2092   int retval = 0;
2093   register char *pname;
2094
2095   if (TREE_CODE (t) != IDENTIFIER_NODE)
2096     return 0;
2097
2098   pname = IDENTIFIER_POINTER (t);
2099   if (strcmp (pname, "interrupt") == 0)
2100     pragma_interrupt = retval = 1;
2101   else if (strcmp (pname, "trapa") == 0)
2102     pragma_interrupt = pragma_trapa = retval = 1;
2103   else if (strcmp (pname, "nosave_low_regs") == 0)
2104     pragma_nosave_low_regs = retval = 1;
2105
2106   return retval;
2107 }
2108 \f
2109 /* Predicates used by the templates.  */
2110
2111 /* Returns 1 if OP is MACL, MACH or PR.  The input must be a REG rtx.
2112    Used only in general_movsrc_operand.  */
2113
2114 int
2115 system_reg_operand (op, mode)
2116      rtx op;
2117      enum machine_mode mode;
2118 {
2119   switch (REGNO (op))
2120     {
2121     case PR_REG:
2122     case MACL_REG:
2123     case MACH_REG:
2124       return 1;
2125     }
2126   return 0;
2127 }
2128
2129 /* Returns 1 if OP can be source of a simple move operation.
2130    Same as general_operand, but a LABEL_REF is valid, PRE_DEC is
2131    invalid as are subregs of system registers.  */
2132
2133 int
2134 general_movsrc_operand (op, mode)
2135      rtx op;
2136      enum machine_mode mode;
2137 {
2138   if (GET_CODE (op) == MEM)
2139     {
2140       rtx inside = XEXP (op, 0);
2141       if (GET_CODE (inside) == CONST)
2142         inside = XEXP (inside, 0);
2143
2144       if (GET_CODE (inside) == LABEL_REF)
2145         return 1;
2146
2147       if (GET_CODE (inside) == PLUS
2148           && GET_CODE (XEXP (inside, 0)) == LABEL_REF
2149           && GET_CODE (XEXP (inside, 1)) == CONST_INT)
2150         return 1;
2151
2152       /* Only post inc allowed.  */
2153       if (GET_CODE (inside) == PRE_DEC)
2154         return 0;
2155     }
2156
2157   if ((mode == QImode || mode == HImode)
2158       && (GET_CODE (op) == SUBREG
2159           && GET_CODE (XEXP (op, 0)) == REG
2160           && system_reg_operand (XEXP (op, 0), mode)))
2161     return 0;
2162
2163   return general_operand (op, mode);
2164 }
2165
2166 /* Returns 1 if OP can be a destination of a move.
2167    Same as general_operand, but no preinc allowed.  */
2168
2169 int
2170 general_movdst_operand (op, mode)
2171      rtx op;
2172      enum machine_mode mode;
2173 {
2174   /* Only pre dec allowed.  */
2175   if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
2176     return 0;
2177
2178   return general_operand (op, mode);
2179 }
2180
2181 /* Returns 1 if OP is a normal arithmetic register.  */
2182
2183 int
2184 arith_reg_operand (op, mode)
2185      rtx op;
2186      enum machine_mode mode;
2187 {
2188   if (register_operand (op, mode))
2189     {
2190       if (GET_CODE (op) == REG)
2191         return (REGNO (op) != T_REG
2192                 && REGNO (op) != PR_REG
2193                 && REGNO (op) != FPUL_REG
2194                 && REGNO (op) != MACH_REG
2195                 && REGNO (op) != MACL_REG);
2196       return 1;
2197     }
2198   return 0;
2199 }
2200
2201 /* Returns 1 if OP is a valid source operand for an arithmetic insn.  */
2202
2203 int
2204 arith_operand (op, mode)
2205      rtx op;
2206      enum machine_mode mode;
2207 {
2208   if (arith_reg_operand (op, mode))
2209     return 1;
2210
2211   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I (INTVAL (op)))
2212     return 1;
2213
2214   return 0;
2215 }
2216
2217 /* Returns 1 if OP is a valid source operand for a compare insn.  */
2218
2219 int
2220 arith_reg_or_0_operand (op, mode)
2221      rtx op;
2222      enum machine_mode mode;
2223 {
2224   if (arith_reg_operand (op, mode))
2225     return 1;
2226
2227   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_N (INTVAL (op)))
2228     return 1;
2229
2230   return 0;
2231 }
2232
2233 /* Returns 1 if OP is a valid source operand for a logical operation.  */
2234
2235 int
2236 logical_operand (op, mode)
2237      rtx op;
2238      enum machine_mode mode;
2239 {
2240   if (arith_reg_operand (op, mode))
2241     return 1;
2242
2243   if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_L (INTVAL (op)))
2244     return 1;
2245
2246   return 0;
2247 }
2248
2249 /* Nonzero if OP is a floating point value with value 0.0.  */
2250
2251 int
2252 fp_zero_operand (op)
2253      rtx op;
2254 {
2255   REAL_VALUE_TYPE r;
2256
2257   if (GET_MODE (op) != SFmode)
2258     return 0;
2259
2260   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
2261   return REAL_VALUES_EQUAL (r, dconst0) && ! REAL_VALUE_MINUS_ZERO (r);
2262 }
2263
2264 /* Nonzero if OP is a floating point value with value 1.0.  */
2265
2266 int
2267 fp_one_operand (op)
2268      rtx op;
2269 {
2270   REAL_VALUE_TYPE r;
2271
2272   if (GET_MODE (op) != SFmode)
2273     return 0;
2274
2275   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
2276   return REAL_VALUES_EQUAL (r, dconst1);
2277 }
2278 \f
2279 /* Return non-zero if REG is not used after INSN.
2280    We assume REG is a reload reg, and therefore does
2281    not live past labels.  It may live past calls or jumps though.  */
2282 int
2283 reg_unused_after (reg, insn)
2284      rtx reg;
2285      rtx insn;
2286 {
2287   enum rtx_code code;
2288   rtx set;
2289
2290   /* If the reg is set by this instruction, then it is safe for our
2291      case.  Disregard the case where this is a store to memory, since
2292      we are checking a register used in the store address.  */
2293   set = single_set (insn);
2294   if (set && GET_CODE (SET_DEST (set)) != MEM
2295       && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2296     return 1;
2297
2298   while (insn = NEXT_INSN (insn))
2299     {
2300       code = GET_CODE (insn);
2301
2302 #if 0
2303       /* If this is a label that existed before reload, then the register
2304          if dead here.  However, if this is a label added by reorg, then
2305          the register may still be live here.  We can't tell the difference,
2306          so we just ignore labels completely.  */
2307       if (code == CODE_LABEL)
2308         return 1;
2309       /* else */
2310 #endif
2311
2312       if (code == JUMP_INSN)
2313         return 0;
2314
2315       /* If this is a sequence, we must handle them all at once.
2316          We could have for instance a call that sets the target register,
2317          and a insn in a delay slot that uses the register.  In this case,
2318          we must return 0.  */
2319       else if (code == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
2320         {
2321           int i;
2322           int retval = 0;
2323
2324           for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2325             {
2326               rtx this_insn = XVECEXP (PATTERN (insn), 0, i);
2327               rtx set = single_set (this_insn);
2328
2329               if (GET_CODE (this_insn) == CALL_INSN)
2330                 code = CALL_INSN;
2331
2332               if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
2333                 return 0;
2334               if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2335                 {
2336                   if (GET_CODE (SET_DEST (set)) != MEM)
2337                     retval = 1;
2338                   else
2339                     return 0;
2340                 }
2341               if (set == 0
2342                   && reg_overlap_mentioned_p (reg, PATTERN (this_insn)))
2343                 return 0;
2344             }
2345           if (retval == 1)
2346             return 1;
2347         }
2348       else if (GET_RTX_CLASS (code) == 'i')
2349         {
2350           rtx set = single_set (insn);
2351
2352           if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
2353             return 0;
2354           if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
2355             return GET_CODE (SET_DEST (set)) != MEM;
2356           if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
2357             return 0;
2358         }
2359
2360       if (code == CALL_INSN && call_used_regs[REGNO (reg)])
2361         return 1;
2362     }
2363   return 1;
2364 }