OSDN Git Service

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