OSDN Git Service

* sh-protos.h (sh_expand_unop_v2sf): Move inside #ifdef RTX_CODE guard.
[pf3gnuchains/gcc-fork.git] / gcc / config / sh / sh.c
1 /* Output routines for GCC for Hitachi / SuperH SH.
2    Copyright (C) 1993, 1994, 1995, 1997, 1997, 1998, 1999, 2000, 2001, 2002
3    Free Software Foundation, Inc.
4    Contributed by Steve Chamberlain (sac@cygnus.com).
5    Improved by Jim Wilson (wilson@cygnus.com). 
6
7 This file is part of GNU CC.
8
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "insn-config.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "expr.h"
31 #include "optabs.h"
32 #include "function.h"
33 #include "regs.h"
34 #include "hard-reg-set.h"
35 #include "output.h"
36 #include "insn-attr.h"
37 #include "toplev.h"
38 #include "recog.h"
39 #include "c-pragma.h"
40 #include "integrate.h"
41 #include "tm_p.h"
42 #include "target.h"
43 #include "target-def.h"
44 #include "real.h"
45 #include "langhooks.h"
46
47 int code_for_indirect_jump_scratch = CODE_FOR_indirect_jump_scratch;
48
49 #define MSW (TARGET_LITTLE_ENDIAN ? 1 : 0)
50 #define LSW (TARGET_LITTLE_ENDIAN ? 0 : 1)
51
52 /* These are some macros to abstract register modes.  */
53 #define CONST_OK_FOR_ADD(size) \
54   (TARGET_SHMEDIA ? CONST_OK_FOR_P (size) : CONST_OK_FOR_I (size))
55 #define GEN_MOV (*(TARGET_SHMEDIA64 ? gen_movdi : gen_movsi))
56 #define GEN_ADD3 (*(TARGET_SHMEDIA64 ? gen_adddi3 : gen_addsi3))
57 #define GEN_SUB3 (*(TARGET_SHMEDIA64 ? gen_subdi3 : gen_subsi3))
58
59 /* Set to 1 by expand_prologue() when the function is an interrupt handler.  */
60 int current_function_interrupt;
61
62 /* ??? The pragma interrupt support will not work for SH3.  */
63 /* This is set by #pragma interrupt and #pragma trapa, and causes gcc to
64    output code for the next function appropriate for an interrupt handler.  */
65 int pragma_interrupt;
66
67 /* This is set by the trap_exit attribute for functions.   It specifies
68    a trap number to be used in a trapa instruction at function exit
69    (instead of an rte instruction).  */
70 int trap_exit;
71
72 /* This is used by the sp_switch attribute for functions.  It specifies
73    a variable holding the address of the stack the interrupt function
74    should switch to/from at entry/exit.  */
75 rtx sp_switch;
76
77 /* This is set by #pragma trapa, and is similar to the above, except that
78    the compiler doesn't emit code to preserve all registers.  */
79 static int pragma_trapa;
80
81 /* This is set by #pragma nosave_low_regs.  This is useful on the SH3,
82    which has a separate set of low regs for User and Supervisor modes.
83    This should only be used for the lowest level of interrupts.  Higher levels
84    of interrupts must save the registers in case they themselves are
85    interrupted.  */
86 int pragma_nosave_low_regs;
87
88 /* This is used for communication between SETUP_INCOMING_VARARGS and
89    sh_expand_prologue.  */
90 int current_function_anonymous_args;
91
92 /* Global variables for machine-dependent things.  */
93
94 /* Which cpu are we scheduling for.  */
95 enum processor_type sh_cpu;
96
97 /* Saved operands from the last compare to use when we generate an scc
98    or bcc insn.  */
99
100 rtx sh_compare_op0;
101 rtx sh_compare_op1;
102
103 /* Provides the class number of the smallest class containing
104    reg number.  */
105
106 int regno_reg_class[FIRST_PSEUDO_REGISTER] =
107 {
108   R0_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
109   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
110   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
111   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
112   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
113   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
114   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
115   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
116   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
117   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
118   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
119   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
120   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
121   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
122   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
123   GENERAL_REGS, GENERAL_REGS, GENERAL_REGS, GENERAL_REGS,
124   FP0_REGS,FP_REGS, FP_REGS, FP_REGS,
125   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
126   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
127   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
128   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
129   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
130   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
131   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
132   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
133   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
134   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
135   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
136   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
137   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
138   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
139   FP_REGS, FP_REGS, FP_REGS, FP_REGS,
140   TARGET_REGS, TARGET_REGS, TARGET_REGS, TARGET_REGS,
141   TARGET_REGS, TARGET_REGS, TARGET_REGS, TARGET_REGS,
142   DF_REGS, DF_REGS, DF_REGS, DF_REGS,
143   DF_REGS, DF_REGS, DF_REGS, DF_REGS,
144   NO_REGS, GENERAL_REGS, PR_REGS, T_REGS,
145   MAC_REGS, MAC_REGS, FPUL_REGS, FPSCR_REGS,
146   GENERAL_REGS,
147 };
148
149 char sh_register_names[FIRST_PSEUDO_REGISTER] \
150   [MAX_REGISTER_NAME_LENGTH + 1] = SH_REGISTER_NAMES_INITIALIZER;
151
152 char sh_additional_register_names[ADDREGNAMES_SIZE] \
153   [MAX_ADDITIONAL_REGISTER_NAME_LENGTH + 1]
154   = SH_ADDITIONAL_REGISTER_NAMES_INITIALIZER;
155
156 /* Provide reg_class from a letter such as appears in the machine
157    description.  */
158
159 const enum reg_class reg_class_from_letter[] =
160 {
161   /* a */ ALL_REGS, /* b */ TARGET_REGS, /* c */ FPSCR_REGS, /* d */ DF_REGS,
162   /* e */ NO_REGS, /* f */ FP_REGS, /* g */ NO_REGS, /* h */ NO_REGS,
163   /* i */ NO_REGS, /* j */ NO_REGS, /* k */ SIBCALL_REGS, /* l */ PR_REGS,
164   /* m */ NO_REGS, /* n */ NO_REGS, /* o */ NO_REGS, /* p */ NO_REGS,
165   /* q */ NO_REGS, /* r */ NO_REGS, /* s */ NO_REGS, /* t */ T_REGS,
166   /* u */ NO_REGS, /* v */ NO_REGS, /* w */ FP0_REGS, /* x */ MAC_REGS,
167   /* y */ FPUL_REGS, /* z */ R0_REGS
168 };
169
170 int assembler_dialect;
171
172 static void split_branches PARAMS ((rtx));
173 static int branch_dest PARAMS ((rtx));
174 static void force_into PARAMS ((rtx, rtx));
175 static void print_slot PARAMS ((rtx));
176 static rtx add_constant PARAMS ((rtx, enum machine_mode, rtx));
177 static void dump_table PARAMS ((rtx));
178 static int hi_const PARAMS ((rtx));
179 static int broken_move PARAMS ((rtx));
180 static int mova_p PARAMS ((rtx));
181 static rtx find_barrier PARAMS ((int, rtx, rtx));
182 static int noncall_uses_reg PARAMS ((rtx, rtx, rtx *));
183 static rtx gen_block_redirect PARAMS ((rtx, int, int));
184 static void output_stack_adjust PARAMS ((int, rtx, int, rtx (*) (rtx)));
185 static rtx frame_insn PARAMS ((rtx));
186 static rtx push PARAMS ((int));
187 static void pop PARAMS ((int));
188 static void push_regs PARAMS ((HOST_WIDE_INT *));
189 static void calc_live_regs PARAMS ((int *, HOST_WIDE_INT *));
190 static void mark_use PARAMS ((rtx, rtx *));
191 static HOST_WIDE_INT rounded_frame_size PARAMS ((int));
192 static rtx mark_constant_pool_use PARAMS ((rtx));
193 const struct attribute_spec sh_attribute_table[];
194 static tree sh_handle_interrupt_handler_attribute PARAMS ((tree *, tree, tree, int, bool *));
195 static tree sh_handle_sp_switch_attribute PARAMS ((tree *, tree, tree, int, bool *));
196 static tree sh_handle_trap_exit_attribute PARAMS ((tree *, tree, tree, int, bool *));
197 static void sh_output_function_epilogue PARAMS ((FILE *, HOST_WIDE_INT));
198 static void sh_insert_attributes PARAMS ((tree, tree *));
199 static int sh_adjust_cost PARAMS ((rtx, rtx, rtx, int));
200 static int sh_use_dfa_interface PARAMS ((void));
201 static int sh_issue_rate PARAMS ((void));
202
203 static bool sh_cannot_modify_jumps_p PARAMS ((void));
204 static bool sh_ms_bitfield_layout_p PARAMS ((tree));
205
206 static void sh_encode_section_info PARAMS ((tree, int));
207 static const char *sh_strip_name_encoding PARAMS ((const char *));
208 static void sh_init_builtins (void);
209 static void sh_media_init_builtins (void);
210 static rtx sh_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
211
212 \f
213 /* Initialize the GCC target structure.  */
214 #undef TARGET_ATTRIBUTE_TABLE
215 #define TARGET_ATTRIBUTE_TABLE sh_attribute_table
216
217 /* The next two are used for debug info when compiling with -gdwarf.  */
218 #undef TARGET_ASM_UNALIGNED_HI_OP
219 #define TARGET_ASM_UNALIGNED_HI_OP "\t.uaword\t"
220 #undef TARGET_ASM_UNALIGNED_SI_OP
221 #define TARGET_ASM_UNALIGNED_SI_OP "\t.ualong\t"
222
223 /* These are NULLed out on non-SH5 in OVERRIDE_OPTIONS.  */
224 #undef TARGET_ASM_UNALIGNED_DI_OP
225 #define TARGET_ASM_UNALIGNED_DI_OP "\t.uaquad\t"
226 #undef TARGET_ASM_ALIGNED_DI_OP
227 #define TARGET_ASM_ALIGNED_DI_OP "\t.quad\t"
228
229 #undef TARGET_ASM_FUNCTION_EPILOGUE
230 #define TARGET_ASM_FUNCTION_EPILOGUE sh_output_function_epilogue
231
232 #undef TARGET_INSERT_ATTRIBUTES
233 #define TARGET_INSERT_ATTRIBUTES sh_insert_attributes
234
235 #undef TARGET_SCHED_ADJUST_COST
236 #define TARGET_SCHED_ADJUST_COST sh_adjust_cost
237
238 #undef TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE 
239 #define TARGET_SCHED_USE_DFA_PIPELINE_INTERFACE \
240                                 sh_use_dfa_interface
241 #undef TARGET_SCHED_ISSUE_RATE
242 #define TARGET_SCHED_ISSUE_RATE sh_issue_rate
243
244 #undef TARGET_CANNOT_MODIFY_JUMPS_P
245 #define TARGET_CANNOT_MODIFY_JUMPS_P sh_cannot_modify_jumps_p
246
247 #undef TARGET_MS_BITFIELD_LAYOUT_P
248 #define TARGET_MS_BITFIELD_LAYOUT_P sh_ms_bitfield_layout_p
249
250 #undef TARGET_ENCODE_SECTION_INFO
251 #define TARGET_ENCODE_SECTION_INFO sh_encode_section_info
252 #undef TARGET_STRIP_NAME_ENCODING
253 #define TARGET_STRIP_NAME_ENCODING sh_strip_name_encoding
254
255 #undef TARGET_INIT_BUILTINS
256 #define TARGET_INIT_BUILTINS sh_init_builtins
257 #undef TARGET_EXPAND_BUILTIN
258 #define TARGET_EXPAND_BUILTIN sh_expand_builtin
259
260 struct gcc_target targetm = TARGET_INITIALIZER;
261 \f
262 /* Print the operand address in x to the stream.  */
263
264 void
265 print_operand_address (stream, x)
266      FILE *stream;
267      rtx x;
268 {
269   switch (GET_CODE (x))
270     {
271     case REG:
272     case SUBREG:
273       fprintf (stream, "@%s", reg_names[true_regnum (x)]);
274       break;
275
276     case PLUS:
277       {
278         rtx base = XEXP (x, 0);
279         rtx index = XEXP (x, 1);
280
281         switch (GET_CODE (index))
282           {
283           case CONST_INT:
284             fprintf (stream, "@(%d,%s)", (int) INTVAL (index),
285                      reg_names[true_regnum (base)]);
286             break;
287
288           case REG:
289           case SUBREG:
290             {
291               int base_num = true_regnum (base);
292               int index_num = true_regnum (index);
293
294               fprintf (stream, "@(r0,%s)",
295                        reg_names[MAX (base_num, index_num)]);
296               break;
297             }
298
299           default:
300             debug_rtx (x);
301             abort ();
302           }
303       }
304       break;
305
306     case PRE_DEC:
307       fprintf (stream, "@-%s", reg_names[true_regnum (XEXP (x, 0))]);
308       break;
309
310     case POST_INC:
311       fprintf (stream, "@%s+", reg_names[true_regnum (XEXP (x, 0))]);
312       break;
313
314     default:
315       x = mark_constant_pool_use (x);
316       output_addr_const (stream, x);
317       break;
318     }
319 }
320
321 /* Print operand x (an rtx) in assembler syntax to file stream
322    according to modifier code.
323
324    '.'  print a .s if insn needs delay slot
325    ','  print LOCAL_LABEL_PREFIX
326    '@'  print trap, rte or rts depending upon pragma interruptness
327    '#'  output a nop if there is nothing to put in the delay slot
328    '''  print likelyhood suffix (/u for unlikely).
329    'O'  print a constant without the #
330    'R'  print the LSW of a dp value - changes if in little endian
331    'S'  print the MSW of a dp value - changes if in little endian
332    'T'  print the next word of a dp value - same as 'R' in big endian mode.
333    'M'  print an `x' if `m' will print `base,index'.
334    'N'  print 'r63' if the operand is (const_int 0).
335    'm'  print a pair `base,offset' or `base,index', for LD and ST.
336    'u'  prints the lowest 16 bits of CONST_INT, as an unsigned value.
337    'o'  output an operator.  */
338
339 void
340 print_operand (stream, x, code)
341      FILE *stream;
342      rtx x;
343      int code;
344 {
345   switch (code)
346     {
347     case '.':
348       if (final_sequence
349           && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
350         fprintf (stream, ASSEMBLER_DIALECT ? "/s" : ".s");
351       break;
352     case ',':
353       fprintf (stream, "%s", LOCAL_LABEL_PREFIX);
354       break;
355     case '@':
356       if (trap_exit)
357         fprintf (stream, "trapa #%d", trap_exit);
358       else if (sh_cfun_interrupt_handler_p ())
359         fprintf (stream, "rte");
360       else
361         fprintf (stream, "rts");
362       break;
363     case '#':
364       /* Output a nop if there's nothing in the delay slot.  */
365       if (dbr_sequence_length () == 0)
366         fprintf (stream, "\n\tnop");
367       break;
368     case '\'':
369       {
370         rtx note = find_reg_note (current_output_insn, REG_BR_PROB, 0);
371
372         if (note && INTVAL (XEXP (note, 0)) * 2 < REG_BR_PROB_BASE)
373           fputs ("/u", stream);
374         break;
375       }
376     case 'O':
377       x = mark_constant_pool_use (x);
378       output_addr_const (stream, x);
379       break;
380     case 'R':
381       fputs (reg_names[REGNO (x) + LSW], (stream));
382       break;
383     case 'S':
384       fputs (reg_names[REGNO (x) + MSW], (stream));
385       break;
386     case 'T':
387       /* Next word of a double.  */
388       switch (GET_CODE (x))
389         {
390         case REG:
391           fputs (reg_names[REGNO (x) + 1], (stream));
392           break;
393         case MEM:
394           if (GET_CODE (XEXP (x, 0)) != PRE_DEC
395               && GET_CODE (XEXP (x, 0)) != POST_INC)
396             x = adjust_address (x, SImode, 4);
397           print_operand_address (stream, XEXP (x, 0));
398           break;
399         default:
400           break;
401         }
402       break;
403     case 'o':
404       switch (GET_CODE (x))
405         {
406         case PLUS:  fputs ("add", stream); break;
407         case MINUS: fputs ("sub", stream); break;
408         case MULT:  fputs ("mul", stream); break;
409         case DIV:   fputs ("div", stream); break;
410         case EQ:    fputs ("eq",  stream); break;
411         case NE:    fputs ("ne",  stream); break;
412         case GT:  case LT:  fputs ("gt",  stream); break;
413         case GE:  case LE:  fputs ("ge",  stream); break;
414         case GTU: case LTU: fputs ("gtu", stream); break;
415         case GEU: case LEU: fputs ("geu", stream); break;
416         default:
417           break;
418         }
419       break;
420     case 'M':
421       if (GET_CODE (x) == MEM
422           && GET_CODE (XEXP (x, 0)) == PLUS
423           && (GET_CODE (XEXP (XEXP (x, 0), 1)) == REG
424               || GET_CODE (XEXP (XEXP (x, 0), 1)) == SUBREG))
425         fputc ('x', stream);
426       break;
427
428     case 'm':
429       if (GET_CODE (x) != MEM)
430         abort ();
431       x = XEXP (x, 0);
432       switch (GET_CODE (x))
433         {
434         case REG:
435         case SUBREG:
436           print_operand (stream, x, 0);
437           fputs (", 0", stream);
438           break;
439
440         case PLUS:
441           print_operand (stream, XEXP (x, 0), 0);
442           fputs (", ", stream);
443           print_operand (stream, XEXP (x, 1), 0);
444           break;
445
446         default:
447           abort ();
448         }
449       break;
450
451     case 'N':
452       if (x == CONST0_RTX (GET_MODE (x)))
453         {
454           fprintf ((stream), "r63");
455           break;
456         }
457       goto default_output;
458     case 'u':
459       if (GET_CODE (x) == CONST_INT)
460         {
461           fprintf ((stream), "%u", (unsigned) INTVAL (x) & (0x10000 - 1));
462           break;
463         }
464       /* Fall through.  */
465
466     default_output:
467     default:
468       switch (GET_CODE (x))
469         {
470           /* FIXME: We need this on SHmedia32 because reload generates
471              some sign-extended HI or QI loads into DImode registers
472              but, because Pmode is SImode, the address ends up with a
473              subreg:SI of the DImode register.  Maybe reload should be
474              fixed so as to apply alter_subreg to such loads?  */
475         case SUBREG:
476           if (SUBREG_BYTE (x) != 0
477               || GET_CODE (SUBREG_REG (x)) != REG)
478             abort ();
479
480           x = SUBREG_REG (x);
481           /* Fall through.  */
482
483         case REG:
484           if (FP_REGISTER_P (REGNO (x))
485               && GET_MODE (x) == V16SFmode)
486             fprintf ((stream), "mtrx%s", reg_names[REGNO (x)] + 2);
487           else if (FP_REGISTER_P (REGNO (x))
488                    && GET_MODE (x) == V4SFmode)
489             fprintf ((stream), "fv%s", reg_names[REGNO (x)] + 2);
490           else if (GET_CODE (x) == REG
491                    && GET_MODE (x) == V2SFmode)
492             fprintf ((stream), "fp%s", reg_names[REGNO (x)] + 2);
493           else if (FP_REGISTER_P (REGNO (x))
494                    && GET_MODE_SIZE (GET_MODE (x)) > 4)
495             fprintf ((stream), "d%s", reg_names[REGNO (x)] + 1);
496           else
497             fputs (reg_names[REGNO (x)], (stream));
498           break;
499
500         case MEM:
501           output_address (XEXP (x, 0));
502           break;
503           
504         case CONST:
505           if (TARGET_SHMEDIA
506               && GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
507               && GET_MODE (XEXP (x, 0)) == DImode
508               && GET_CODE (XEXP (XEXP (x, 0), 0)) == TRUNCATE
509               && GET_MODE (XEXP (XEXP (x, 0), 0)) == HImode)
510             {
511               rtx val = XEXP (XEXP (XEXP (x, 0), 0), 0);
512
513               fputc ('(', stream);
514               if (GET_CODE (val) == ASHIFTRT)
515                 {
516                   fputc ('(', stream);
517                   if (GET_CODE (XEXP (val, 0)) == CONST)
518                     fputc ('(', stream);
519                   output_addr_const (stream, XEXP (val, 0));
520                   if (GET_CODE (XEXP (val, 0)) == CONST)
521                     fputc (')', stream);
522                   fputs (" >> ", stream);
523                   output_addr_const (stream, XEXP (val, 1));
524                   fputc (')', stream);
525                 }
526               else
527                 {
528                   if (GET_CODE (val) == CONST)
529                     fputc ('(', stream);
530                   output_addr_const (stream, val);
531                   if (GET_CODE (val) == CONST)
532                     fputc (')', stream);
533                 }
534               fputs (" & 65535)", stream);
535               break;
536             }
537
538           /* Fall through.  */
539         default:
540           if (TARGET_SH1)
541             fputc ('#', stream);
542           output_addr_const (stream, x);
543           break;
544         }
545       break;
546     }
547 }
548 \f
549 /* Like force_operand, but guarantees that VALUE ends up in TARGET.  */
550 static void
551 force_into (value, target)
552      rtx value, target;
553 {
554   value = force_operand (value, target);
555   if (! rtx_equal_p (value, target))
556     emit_insn (gen_move_insn (target, value));
557 }
558
559 /* Emit code to perform a block move.  Choose the best method.
560
561    OPERANDS[0] is the destination.
562    OPERANDS[1] is the source.
563    OPERANDS[2] is the size.
564    OPERANDS[3] is the alignment safe to use.  */
565
566 int
567 expand_block_move (operands)
568      rtx *operands;
569 {
570   int align = INTVAL (operands[3]);
571   int constp = (GET_CODE (operands[2]) == CONST_INT);
572   int bytes = (constp ? INTVAL (operands[2]) : 0);
573
574   /* If it isn't a constant number of bytes, or if it doesn't have 4 byte
575      alignment, or if it isn't a multiple of 4 bytes, then fail.  */
576   if (! constp || align < 4 || (bytes % 4 != 0))
577     return 0;
578
579   if (TARGET_HARD_SH4)
580     {
581       if (bytes < 12)
582         return 0;
583       else if (bytes == 12)
584         {
585           tree entry_name;
586           rtx sym;
587           rtx func_addr_rtx;
588           rtx r4 = gen_rtx (REG, SImode, 4);
589           rtx r5 = gen_rtx (REG, SImode, 5);
590
591           entry_name = get_identifier ("__movstrSI12_i4");
592
593           sym = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (entry_name));
594           func_addr_rtx = copy_to_mode_reg (Pmode, sym);
595           force_into (XEXP (operands[0], 0), r4);
596           force_into (XEXP (operands[1], 0), r5);
597           emit_insn (gen_block_move_real_i4 (func_addr_rtx));
598           return 1;
599         }
600       else if (! TARGET_SMALLCODE)
601         {
602           tree entry_name;
603           rtx sym;
604           rtx func_addr_rtx;
605           int dwords;
606           rtx r4 = gen_rtx (REG, SImode, 4);
607           rtx r5 = gen_rtx (REG, SImode, 5);
608           rtx r6 = gen_rtx (REG, SImode, 6);
609
610           entry_name = get_identifier (bytes & 4
611                                        ? "__movstr_i4_odd"
612                                        : "__movstr_i4_even");
613           sym = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (entry_name));
614           func_addr_rtx = copy_to_mode_reg (Pmode, sym);
615           force_into (XEXP (operands[0], 0), r4);
616           force_into (XEXP (operands[1], 0), r5);
617
618           dwords = bytes >> 3;
619           emit_insn (gen_move_insn (r6, GEN_INT (dwords - 1)));
620           emit_insn (gen_block_lump_real_i4 (func_addr_rtx));
621           return 1;
622         }
623       else
624         return 0;
625     }
626   if (bytes < 64)
627     {
628       char entry[30];
629       tree entry_name;
630       rtx sym;
631       rtx func_addr_rtx;
632       rtx r4 = gen_rtx_REG (SImode, 4);
633       rtx r5 = gen_rtx_REG (SImode, 5);
634
635       sprintf (entry, "__movstrSI%d", bytes);
636       entry_name = get_identifier (entry);
637       sym = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (entry_name));
638       func_addr_rtx = copy_to_mode_reg (Pmode, sym);
639       force_into (XEXP (operands[0], 0), r4);
640       force_into (XEXP (operands[1], 0), r5);
641       emit_insn (gen_block_move_real (func_addr_rtx));
642       return 1;
643     }
644
645   /* This is the same number of bytes as a memcpy call, but to a different
646      less common function name, so this will occasionally use more space.  */
647   if (! TARGET_SMALLCODE)
648     {
649       tree entry_name;
650       rtx sym;
651       rtx func_addr_rtx;
652       int final_switch, while_loop;
653       rtx r4 = gen_rtx_REG (SImode, 4);
654       rtx r5 = gen_rtx_REG (SImode, 5);
655       rtx r6 = gen_rtx_REG (SImode, 6);
656
657       entry_name = get_identifier ("__movstr");
658       sym = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (entry_name));
659       func_addr_rtx = copy_to_mode_reg (Pmode, sym);
660       force_into (XEXP (operands[0], 0), r4);
661       force_into (XEXP (operands[1], 0), r5);
662
663       /* r6 controls the size of the move.  16 is decremented from it
664          for each 64 bytes moved.  Then the negative bit left over is used
665          as an index into a list of move instructions.  e.g., a 72 byte move
666          would be set up with size(r6) = 14, for one iteration through the
667          big while loop, and a switch of -2 for the last part.  */
668
669       final_switch = 16 - ((bytes / 4) % 16);
670       while_loop = ((bytes / 4) / 16 - 1) * 16;
671       emit_insn (gen_move_insn (r6, GEN_INT (while_loop + final_switch)));
672       emit_insn (gen_block_lump_real (func_addr_rtx));
673       return 1;
674     }
675
676   return 0;
677 }
678
679 /* Prepare operands for a move define_expand; specifically, one of the
680    operands must be in a register.  */
681
682 int
683 prepare_move_operands (operands, mode)
684      rtx operands[];
685      enum machine_mode mode;
686 {
687   if ((mode == SImode || mode == DImode) && flag_pic)
688     {
689       rtx temp;
690       if (SYMBOLIC_CONST_P (operands[1]))
691         {
692           if (GET_CODE (operands[0]) == MEM)
693             operands[1] = force_reg (Pmode, operands[1]);
694           else if (GET_CODE (operands[1]) == LABEL_REF
695                    && target_reg_operand (operands[0], mode))
696             /* It's ok.  */;
697           else
698             {
699               temp = no_new_pseudos ? operands[0] : gen_reg_rtx (Pmode);
700               operands[1] = legitimize_pic_address (operands[1], mode, temp);
701             }
702         }
703       else if (GET_CODE (operands[1]) == CONST
704                && GET_CODE (XEXP (operands[1], 0)) == PLUS
705                && SYMBOLIC_CONST_P (XEXP (XEXP (operands[1], 0), 0)))
706         {
707           temp = no_new_pseudos ? operands[0] : gen_reg_rtx (Pmode);
708           temp = legitimize_pic_address (XEXP (XEXP (operands[1], 0), 0),
709                                          mode, temp);
710           operands[1] = expand_binop (mode, add_optab, temp,
711                                       XEXP (XEXP (operands[1], 0), 1),
712                                       no_new_pseudos ? temp
713                                       : gen_reg_rtx (Pmode),
714                                       0, OPTAB_LIB_WIDEN);
715         }
716     }
717
718   if (! reload_in_progress && ! reload_completed)
719     {
720       /* Copy the source to a register if both operands aren't registers.  */
721       if (! register_operand (operands[0], mode)
722           && ! register_operand (operands[1], mode))
723         operands[1] = copy_to_mode_reg (mode, operands[1]);
724
725       /* This case can happen while generating code to move the result
726          of a library call to the target.  Reject `st r0,@(rX,rY)' because
727          reload will fail to find a spill register for rX, since r0 is already
728          being used for the source.  */
729       else if (GET_CODE (operands[1]) == REG && REGNO (operands[1]) == 0
730                && GET_CODE (operands[0]) == MEM
731                && GET_CODE (XEXP (operands[0], 0)) == PLUS
732                && GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == REG)
733         operands[1] = copy_to_mode_reg (mode, operands[1]);
734     }
735
736   return 0;
737 }
738
739 /* Prepare the operands for an scc instruction; make sure that the
740    compare has been done.  */
741 rtx
742 prepare_scc_operands (code)
743      enum rtx_code code;
744 {
745   rtx t_reg = gen_rtx_REG (SImode, T_REG);
746   enum rtx_code oldcode = code;
747   enum machine_mode mode;
748
749   /* First need a compare insn.  */
750   switch (code)
751     {
752     case NE:
753       /* It isn't possible to handle this case.  */
754       abort ();
755     case LT:
756       code = GT;
757       break;
758     case LE:
759       code = GE;
760       break;
761     case LTU:
762       code = GTU;
763       break;
764     case LEU:
765       code = GEU;
766       break;
767     default:
768       break;
769     }
770   if (code != oldcode)
771     {
772       rtx tmp = sh_compare_op0;
773       sh_compare_op0 = sh_compare_op1;
774       sh_compare_op1 = tmp;
775     }
776
777   mode = GET_MODE (sh_compare_op0);
778   if (mode == VOIDmode)
779     mode = GET_MODE (sh_compare_op1);
780
781   sh_compare_op0 = force_reg (mode, sh_compare_op0);
782   if ((code != EQ && code != NE
783        && (sh_compare_op1 != const0_rtx
784            || code == GTU  || code == GEU || code == LTU || code == LEU))
785       || (mode == DImode && sh_compare_op1 != const0_rtx)
786       || (TARGET_SH3E && GET_MODE_CLASS (mode) == MODE_FLOAT))
787     sh_compare_op1 = force_reg (mode, sh_compare_op1);
788
789   if (TARGET_SH4 && GET_MODE_CLASS (mode) == MODE_FLOAT)
790     (mode == SFmode ? emit_sf_insn : emit_df_insn)
791      (gen_rtx (PARALLEL, VOIDmode, gen_rtvec (2,
792                 gen_rtx (SET, VOIDmode, t_reg,
793                          gen_rtx (code, SImode,
794                                   sh_compare_op0, sh_compare_op1)),
795                 gen_rtx (USE, VOIDmode, get_fpscr_rtx ()))));
796   else
797     emit_insn (gen_rtx (SET, VOIDmode, t_reg,
798                         gen_rtx (code, SImode, sh_compare_op0,
799                                  sh_compare_op1)));
800
801   return t_reg;
802 }
803
804 /* Called from the md file, set up the operands of a compare instruction.  */
805
806 void
807 from_compare (operands, code)
808      rtx *operands;
809      int code;
810 {
811   enum machine_mode mode = GET_MODE (sh_compare_op0);
812   rtx insn;
813   if (mode == VOIDmode)
814     mode = GET_MODE (sh_compare_op1);
815   if (code != EQ
816       || mode == DImode
817       || (TARGET_SH3E && GET_MODE_CLASS (mode) == MODE_FLOAT))
818     {
819       /* Force args into regs, since we can't use constants here.  */
820       sh_compare_op0 = force_reg (mode, sh_compare_op0);
821       if (sh_compare_op1 != const0_rtx
822           || code == GTU  || code == GEU
823           || (TARGET_SH3E && GET_MODE_CLASS (mode) == MODE_FLOAT))
824         sh_compare_op1 = force_reg (mode, sh_compare_op1);
825     }
826   if (TARGET_SH3E && GET_MODE_CLASS (mode) == MODE_FLOAT && code == GE)
827     {
828       from_compare (operands, GT);
829       insn = gen_ieee_ccmpeqsf_t (sh_compare_op0, sh_compare_op1);
830     }
831   else
832     insn = gen_rtx_SET (VOIDmode,
833                         gen_rtx_REG (SImode, T_REG),
834                         gen_rtx (code, SImode, sh_compare_op0,
835                                  sh_compare_op1));
836   if (TARGET_SH4 && GET_MODE_CLASS (mode) == MODE_FLOAT)
837     {
838       insn = gen_rtx (PARALLEL, VOIDmode,
839                       gen_rtvec (2, insn,
840                                  gen_rtx (USE, VOIDmode, get_fpscr_rtx ())));
841       (mode == SFmode ? emit_sf_insn : emit_df_insn) (insn);
842     }
843   else
844     emit_insn (insn);
845 }
846 \f
847 /* Functions to output assembly code.  */
848
849 /* Return a sequence of instructions to perform DI or DF move.
850
851    Since the SH cannot move a DI or DF in one instruction, we have
852    to take care when we see overlapping source and dest registers.  */
853
854 const char *
855 output_movedouble (insn, operands, mode)
856      rtx insn ATTRIBUTE_UNUSED;
857      rtx operands[];
858      enum machine_mode mode;
859 {
860   rtx dst = operands[0];
861   rtx src = operands[1];
862
863   if (GET_CODE (dst) == MEM
864       && GET_CODE (XEXP (dst, 0)) == PRE_DEC)
865     return "mov.l       %T1,%0\n\tmov.l %1,%0";
866
867   if (register_operand (dst, mode)
868       && register_operand (src, mode))
869     {
870       if (REGNO (src) == MACH_REG)
871         return "sts     mach,%S0\n\tsts macl,%R0";
872
873       /* When mov.d r1,r2 do r2->r3 then r1->r2;
874          when mov.d r1,r0 do r1->r0 then r2->r1.  */
875
876       if (REGNO (src) + 1 == REGNO (dst))
877         return "mov     %T1,%T0\n\tmov  %1,%0";
878       else
879         return "mov     %1,%0\n\tmov    %T1,%T0";
880     }
881   else if (GET_CODE (src) == CONST_INT)
882     {
883       if (INTVAL (src) < 0)
884         output_asm_insn ("mov   #-1,%S0", operands);
885       else
886         output_asm_insn ("mov   #0,%S0", operands);
887
888       return "mov       %1,%R0";
889     }
890   else if (GET_CODE (src) == MEM)
891     {
892       int ptrreg = -1;
893       int dreg = REGNO (dst);
894       rtx inside = XEXP (src, 0);
895
896       if (GET_CODE (inside) == REG)
897         ptrreg = REGNO (inside);
898       else if (GET_CODE (inside) == SUBREG)
899         ptrreg = subreg_regno (inside);
900       else if (GET_CODE (inside) == PLUS)
901         {
902           ptrreg = REGNO (XEXP (inside, 0));
903           /* ??? A r0+REG address shouldn't be possible here, because it isn't
904              an offsettable address.  Unfortunately, offsettable addresses use
905              QImode to check the offset, and a QImode offsettable address
906              requires r0 for the other operand, which is not currently
907              supported, so we can't use the 'o' constraint.
908              Thus we must check for and handle r0+REG addresses here.
909              We punt for now, since this is likely very rare.  */
910           if (GET_CODE (XEXP (inside, 1)) == REG)
911             abort ();
912         }
913       else if (GET_CODE (inside) == LABEL_REF)
914         return "mov.l   %1,%0\n\tmov.l  %1+4,%T0";
915       else if (GET_CODE (inside) == POST_INC)
916         return "mov.l   %1,%0\n\tmov.l  %1,%T0";
917       else
918         abort ();
919
920       /* Work out the safe way to copy.  Copy into the second half first.  */
921       if (dreg == ptrreg)
922         return "mov.l   %T1,%T0\n\tmov.l        %1,%0";
923     }
924
925   return "mov.l %1,%0\n\tmov.l  %T1,%T0";
926 }
927
928 /* Print an instruction which would have gone into a delay slot after
929    another instruction, but couldn't because the other instruction expanded
930    into a sequence where putting the slot insn at the end wouldn't work.  */
931
932 static void
933 print_slot (insn)
934      rtx insn;
935 {
936   final_scan_insn (XVECEXP (insn, 0, 1), asm_out_file, optimize, 0, 1);
937
938   INSN_DELETED_P (XVECEXP (insn, 0, 1)) = 1;
939 }
940
941 const char *
942 output_far_jump (insn, op)
943      rtx insn;
944      rtx op;
945 {
946   struct { rtx lab, reg, op; } this;
947   rtx braf_base_lab = NULL_RTX;
948   const char *jump;
949   int far;
950   int offset = branch_dest (insn) - INSN_ADDRESSES (INSN_UID (insn));
951
952   this.lab = gen_label_rtx ();
953
954   if (TARGET_SH2
955       && offset >= -32764
956       && offset - get_attr_length (insn) <= 32766)
957     {
958       far = 0;
959       jump = "mov.w     %O0,%1; braf    %1";
960     }
961   else
962     {
963       far = 1;
964       if (flag_pic)
965         {
966           if (TARGET_SH2)
967             jump = "mov.l       %O0,%1; braf    %1";
968           else
969             jump = "mov.l       r0,@-r15; mova  %O0,r0; mov.l   @r0,%1; add     r0,%1; mov.l    @r15+,r0; jmp   @%1";
970         }
971       else
972         jump = "mov.l   %O0,%1; jmp     @%1";
973     }
974   /* If we have a scratch register available, use it.  */
975   if (GET_CODE (PREV_INSN (insn)) == INSN
976       && INSN_CODE (PREV_INSN (insn)) == CODE_FOR_indirect_jump_scratch)
977     {
978       this.reg = SET_DEST (PATTERN (PREV_INSN (insn)));
979       if (REGNO (this.reg) == R0_REG && flag_pic && ! TARGET_SH2)
980         jump = "mov.l   r1,@-r15; mova  %O0,r0; mov.l   @r0,r1; add     r1,r0; mov.l    @r15+,r1; jmp   @%1";
981       output_asm_insn (jump, &this.lab);
982       if (dbr_sequence_length ())
983         print_slot (final_sequence);
984       else
985         output_asm_insn ("nop", 0);
986     }
987   else
988     {
989       /* Output the delay slot insn first if any.  */
990       if (dbr_sequence_length ())
991         print_slot (final_sequence);
992
993       this.reg = gen_rtx_REG (SImode, 13);
994       /* We must keep the stack aligned to 8-byte boundaries on SH5.
995          Fortunately, MACL is fixed and call-clobbered, and we never
996          need its value across jumps, so save r13 in it instead of in
997          the stack.  */
998       if (TARGET_SH5)
999         output_asm_insn ("lds   r13, macl", 0);
1000       else
1001         output_asm_insn ("mov.l r13,@-r15", 0);
1002       output_asm_insn (jump, &this.lab);
1003       if (TARGET_SH5)
1004         output_asm_insn ("sts   macl, r13", 0);
1005       else
1006         output_asm_insn ("mov.l @r15+,r13", 0);
1007     }
1008   if (far && flag_pic && TARGET_SH2)
1009     {
1010       braf_base_lab = gen_label_rtx ();
1011       ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
1012                                  CODE_LABEL_NUMBER (braf_base_lab));
1013     }
1014   if (far)
1015     output_asm_insn (".align    2", 0);
1016   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L", CODE_LABEL_NUMBER (this.lab));
1017   this.op = op;
1018   if (far && flag_pic)
1019     {
1020       if (TARGET_SH2)
1021         this.lab = braf_base_lab;
1022       output_asm_insn (".long   %O2-%O0", &this.lab);
1023     }
1024   else
1025     output_asm_insn (far ? ".long       %O2" : ".word %O2-%O0", &this.lab);
1026   return "";
1027 }
1028
1029 /* Local label counter, used for constants in the pool and inside
1030    pattern branches.  */
1031
1032 static int lf = 100;
1033
1034 /* Output code for ordinary branches.  */
1035
1036 const char *
1037 output_branch (logic, insn, operands)
1038      int logic;
1039      rtx insn;
1040      rtx *operands;
1041 {
1042   switch (get_attr_length (insn))
1043     {
1044     case 6:
1045       /* This can happen if filling the delay slot has caused a forward
1046          branch to exceed its range (we could reverse it, but only
1047          when we know we won't overextend other branches; this should
1048          best be handled by relaxation).
1049          It can also happen when other condbranches hoist delay slot insn
1050          from their destination, thus leading to code size increase.
1051          But the branch will still be in the range -4092..+4098 bytes.  */
1052
1053       if (! TARGET_RELAX)
1054         {
1055           int label = lf++;
1056           /* The call to print_slot will clobber the operands.  */
1057           rtx op0 = operands[0];
1058     
1059           /* If the instruction in the delay slot is annulled (true), then
1060              there is no delay slot where we can put it now.  The only safe
1061              place for it is after the label.  final will do that by default.  */
1062     
1063           if (final_sequence
1064               && ! INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0)))
1065             {
1066               asm_fprintf (asm_out_file, "\tb%s%ss\t%LLF%d\n", logic ? "f" : "t",
1067                            ASSEMBLER_DIALECT ? "/" : ".", label);
1068               print_slot (final_sequence);
1069             }
1070           else
1071             asm_fprintf (asm_out_file, "\tb%s\t%LLF%d\n", logic ? "f" : "t", label);
1072     
1073           output_asm_insn ("bra\t%l0", &op0);
1074           fprintf (asm_out_file, "\tnop\n");
1075           ASM_OUTPUT_INTERNAL_LABEL(asm_out_file, "LF", label);
1076     
1077           return "";
1078         }
1079       /* When relaxing, handle this like a short branch.  The linker
1080          will fix it up if it still doesn't fit after relaxation.  */
1081     case 2:
1082       return logic ? "bt%.\t%l0" : "bf%.\t%l0";
1083     default:
1084       /* There should be no longer branches now - that would
1085          indicate that something has destroyed the branches set
1086          up in machine_dependent_reorg.  */
1087       abort ();
1088     }
1089 }
1090
1091 const char *
1092 output_branchy_insn (code, template, insn, operands)
1093      enum rtx_code code;
1094      const char *template;
1095      rtx insn;
1096      rtx *operands;
1097 {
1098   rtx next_insn = NEXT_INSN (insn);
1099
1100   if (next_insn && GET_CODE (next_insn) == JUMP_INSN && condjump_p (next_insn))
1101     {
1102       rtx src = SET_SRC (PATTERN (next_insn));
1103       if (GET_CODE (src) == IF_THEN_ELSE && GET_CODE (XEXP (src, 0)) != code)
1104         {
1105           /* Following branch not taken */
1106           operands[9] = gen_label_rtx ();
1107           emit_label_after (operands[9], next_insn);
1108           INSN_ADDRESSES_NEW (operands[9],
1109                               INSN_ADDRESSES (INSN_UID (next_insn))
1110                               + get_attr_length (next_insn));
1111           return template;
1112         }
1113       else
1114         {
1115           int offset = (branch_dest (next_insn)
1116                         - INSN_ADDRESSES (INSN_UID (next_insn)) + 4);
1117           if (offset >= -252 && offset <= 258)
1118             {
1119               if (GET_CODE (src) == IF_THEN_ELSE)
1120                 /* branch_true */
1121                 src = XEXP (src, 1);
1122               operands[9] = src;
1123               return template;
1124             }
1125         }
1126     }
1127   operands[9] = gen_label_rtx ();
1128   emit_label_after (operands[9], insn);
1129   INSN_ADDRESSES_NEW (operands[9],
1130                       INSN_ADDRESSES (INSN_UID (insn))
1131                       + get_attr_length (insn));
1132   return template;
1133 }
1134
1135 const char *
1136 output_ieee_ccmpeq (insn, operands)
1137      rtx insn, *operands;
1138 {
1139   return output_branchy_insn (NE, "bt\t%l9\\;fcmp/eq\t%1,%0", insn, operands);
1140 }
1141 \f
1142 /* Output to FILE the start of the assembler file.  */
1143
1144 void
1145 output_file_start (file)
1146      FILE *file;
1147 {
1148   output_file_directive (file, main_input_filename);
1149
1150   /* Switch to the data section so that the coffsem symbol
1151      isn't in the text section.  */
1152   data_section ();
1153
1154   if (TARGET_LITTLE_ENDIAN)
1155     fprintf (file, "\t.little\n");
1156
1157   if (TARGET_SHCOMPACT)
1158     fprintf (file, "\t.mode\tSHcompact\n");
1159   else if (TARGET_SHMEDIA)
1160     fprintf (file, "\t.mode\tSHmedia\n\t.abi\t%i\n",
1161              TARGET_SHMEDIA64 ? 64 : 32);
1162 }
1163 \f
1164 /* Actual number of instructions used to make a shift by N.  */
1165 static const char ashiftrt_insns[] =
1166   { 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};
1167
1168 /* Left shift and logical right shift are the same.  */
1169 static const char shift_insns[]    =
1170   { 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};
1171
1172 /* Individual shift amounts needed to get the above length sequences.
1173    One bit right shifts clobber the T bit, so when possible, put one bit
1174    shifts in the middle of the sequence, so the ends are eligible for
1175    branch delay slots.  */
1176 static const short shift_amounts[32][5] = {
1177   {0}, {1}, {2}, {2, 1},
1178   {2, 2}, {2, 1, 2}, {2, 2, 2}, {2, 2, 1, 2},
1179   {8}, {8, 1}, {8, 2}, {8, 1, 2},
1180   {8, 2, 2}, {8, 2, 1, 2}, {8, -2, 8}, {8, -1, 8},
1181   {16}, {16, 1}, {16, 2}, {16, 1, 2},
1182   {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
1183   {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
1184   {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
1185
1186 /* Likewise, but for shift amounts < 16, up to three highmost bits
1187    might be clobbered.  This is typically used when combined with some
1188    kind of sign or zero extension.  */
1189    
1190 static const char ext_shift_insns[]    =
1191   { 0,1,1,2,2,3,2,2,1,2,2,3,3,3,2,2,1,2,2,3,3,4,3,3,2,3,3,4,4,4,3,3};
1192
1193 static const short ext_shift_amounts[32][4] = {
1194   {0}, {1}, {2}, {2, 1},
1195   {2, 2}, {2, 1, 2}, {8, -2}, {8, -1},
1196   {8}, {8, 1}, {8, 2}, {8, 1, 2},
1197   {8, 2, 2}, {16, -2, -1}, {16, -2}, {16, -1},
1198   {16}, {16, 1}, {16, 2}, {16, 1, 2},
1199   {16, 2, 2}, {16, 2, 1, 2}, {16, -2, 8}, {16, -1, 8},
1200   {16, 8}, {16, 1, 8}, {16, 8, 2}, {16, 8, 1, 2},
1201   {16, 8, 2, 2}, {16, -1, -2, 16}, {16, -2, 16}, {16, -1, 16}};
1202
1203 /* Assuming we have a value that has been sign-extended by at least one bit,
1204    can we use the ext_shift_amounts with the last shift turned to an arithmetic shift
1205    to shift it by N without data loss, and quicker than by other means?  */
1206 #define EXT_SHIFT_SIGNED(n) (((n) | 8) == 15)
1207
1208 /* This is used in length attributes in sh.md to help compute the length
1209    of arbitrary constant shift instructions.  */
1210
1211 int
1212 shift_insns_rtx (insn)
1213      rtx insn;
1214 {
1215   rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1216   int shift_count = INTVAL (XEXP (set_src, 1));
1217   enum rtx_code shift_code = GET_CODE (set_src);
1218
1219   switch (shift_code)
1220     {
1221     case ASHIFTRT:
1222       return ashiftrt_insns[shift_count];
1223     case LSHIFTRT:
1224     case ASHIFT:
1225       return shift_insns[shift_count];
1226     default:
1227       abort();
1228     }
1229 }
1230
1231 /* Return the cost of a shift.  */
1232
1233 int
1234 shiftcosts (x)
1235      rtx x;
1236 {
1237   int value;
1238
1239   if (TARGET_SHMEDIA)
1240     return 1;
1241
1242   if (GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
1243     {
1244       if (GET_MODE (x) == DImode
1245           && GET_CODE (XEXP (x, 1)) == CONST_INT
1246           && INTVAL (XEXP (x, 1)) == 1)
1247         return 2;
1248
1249       /* Everything else is invalid, because there is no pattern for it.  */
1250       return 10000;
1251     }
1252   /* If shift by a non constant, then this will be expensive.  */
1253   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
1254     return SH_DYNAMIC_SHIFT_COST;
1255
1256   value = INTVAL (XEXP (x, 1));
1257
1258   /* Otherwise, return the true cost in instructions.  */
1259   if (GET_CODE (x) == ASHIFTRT)
1260     {
1261       int cost = ashiftrt_insns[value];
1262       /* If SH3, then we put the constant in a reg and use shad.  */
1263       if (cost > 1 + SH_DYNAMIC_SHIFT_COST)
1264         cost = 1 + SH_DYNAMIC_SHIFT_COST;
1265       return cost;
1266     }
1267   else
1268     return shift_insns[value];
1269 }
1270
1271 /* Return the cost of an AND operation.  */
1272
1273 int
1274 andcosts (x)
1275      rtx x;
1276 {
1277   int i;
1278
1279   /* Anding with a register is a single cycle and instruction.  */
1280   if (GET_CODE (XEXP (x, 1)) != CONST_INT)
1281     return 1;
1282
1283   i = INTVAL (XEXP (x, 1));
1284
1285   if (TARGET_SHMEDIA)
1286     {
1287       if ((GET_CODE (XEXP (x, 1)) == CONST_INT
1288            && CONST_OK_FOR_J (INTVAL (XEXP (x, 1))))
1289           || EXTRA_CONSTRAINT_S (XEXP (x, 1)))
1290         return 1;
1291       else
1292         return 2;
1293     }
1294
1295   /* These constants are single cycle extu.[bw] instructions.  */
1296   if (i == 0xff || i == 0xffff)
1297     return 1;
1298   /* Constants that can be used in an and immediate instruction is a single
1299      cycle, but this requires r0, so make it a little more expensive.  */
1300   if (CONST_OK_FOR_L (i))
1301     return 2;
1302   /* Constants that can be loaded with a mov immediate and an and.
1303      This case is probably unnecessary.  */
1304   if (CONST_OK_FOR_I (i))
1305     return 2;
1306   /* Any other constants requires a 2 cycle pc-relative load plus an and.
1307      This case is probably unnecessary.  */
1308   return 3;
1309 }
1310
1311 /* Return the cost of an addition or a subtraction.  */
1312
1313 int
1314 addsubcosts (x)
1315      rtx x;
1316 {
1317   /* Adding a register is a single cycle insn.  */
1318   if (GET_CODE (XEXP (x, 1)) == REG
1319       || GET_CODE (XEXP (x, 1)) == SUBREG)
1320     return 1;
1321
1322   /* Likewise for small constants.  */
1323   if (GET_CODE (XEXP (x, 1)) == CONST_INT
1324       && CONST_OK_FOR_ADD (INTVAL (XEXP (x, 1))))
1325     return 1;
1326
1327   if (TARGET_SHMEDIA)
1328     switch (GET_CODE (XEXP (x, 1)))
1329       {
1330       case CONST:
1331       case LABEL_REF:
1332       case SYMBOL_REF:
1333         return TARGET_SHMEDIA64 ? 5 : 3;
1334
1335       case CONST_INT:
1336         if (CONST_OK_FOR_J (INTVAL (XEXP (x, 1))))
1337           return 2;
1338         else if (CONST_OK_FOR_J (INTVAL (XEXP (x, 1)) >> 16))
1339           return 3;
1340         else if (CONST_OK_FOR_J ((INTVAL (XEXP (x, 1)) >> 16) >> 16))
1341           return 4;
1342
1343         /* Fall through.  */
1344       default:
1345           return 5;
1346       }
1347
1348   /* Any other constant requires a 2 cycle pc-relative load plus an
1349      addition.  */
1350   return 3;
1351 }
1352
1353 /* Return the cost of a multiply.  */
1354 int
1355 multcosts (x)
1356      rtx x ATTRIBUTE_UNUSED;
1357 {
1358   if (TARGET_SHMEDIA)
1359     return 3;
1360
1361   if (TARGET_SH2)
1362     {
1363       /* We have a mul insn, so we can never take more than the mul and the
1364          read of the mac reg, but count more because of the latency and extra
1365          reg usage.  */
1366       if (TARGET_SMALLCODE)
1367         return 2;
1368       return 3;
1369     }
1370
1371   /* If we're aiming at small code, then just count the number of
1372      insns in a multiply call sequence.  */
1373   if (TARGET_SMALLCODE)
1374     return 5;
1375
1376   /* Otherwise count all the insns in the routine we'd be calling too.  */
1377   return 20;
1378 }
1379
1380 /* Code to expand a shift.  */
1381
1382 void
1383 gen_ashift (type, n, reg)
1384      int type;
1385      int n;
1386      rtx reg;
1387 {
1388   /* Negative values here come from the shift_amounts array.  */
1389   if (n < 0)
1390     {
1391       if (type == ASHIFT)
1392         type = LSHIFTRT;
1393       else
1394         type = ASHIFT;
1395       n = -n;
1396     }
1397
1398   switch (type)
1399     {
1400     case ASHIFTRT:
1401       emit_insn (gen_ashrsi3_k (reg, reg, GEN_INT (n)));
1402       break;
1403     case LSHIFTRT:
1404       if (n == 1)
1405         emit_insn (gen_lshrsi3_m (reg, reg, GEN_INT (n)));
1406       else
1407         emit_insn (gen_lshrsi3_k (reg, reg, GEN_INT (n)));
1408       break;
1409     case ASHIFT:
1410       emit_insn (gen_ashlsi3_std (reg, reg, GEN_INT (n)));
1411       break;
1412     }
1413 }
1414
1415 /* Same for HImode */
1416
1417 void
1418 gen_ashift_hi (type, n, reg)
1419      int type;
1420      int n;
1421      rtx reg;
1422 {
1423   /* Negative values here come from the shift_amounts array.  */
1424   if (n < 0)
1425     {
1426       if (type == ASHIFT)
1427         type = LSHIFTRT;
1428       else
1429         type = ASHIFT;
1430       n = -n;
1431     }
1432
1433   switch (type)
1434     {
1435     case ASHIFTRT:
1436     case LSHIFTRT:
1437       /* We don't have HImode right shift operations because using the
1438          ordinary 32 bit shift instructions for that doesn't generate proper
1439          zero/sign extension.
1440          gen_ashift_hi is only called in contexts where we know that the
1441          sign extension works out correctly.  */
1442       {
1443         int offset = 0;
1444         if (GET_CODE (reg) == SUBREG)
1445           {
1446             offset = SUBREG_BYTE (reg);
1447             reg = SUBREG_REG (reg);
1448           }
1449         gen_ashift (type, n, gen_rtx_SUBREG (SImode, reg, offset));
1450         break;
1451       }
1452     case ASHIFT:
1453       emit_insn (gen_ashlhi3_k (reg, reg, GEN_INT (n)));
1454       break;
1455     }
1456 }
1457
1458 /* Output RTL to split a constant shift into its component SH constant
1459    shift instructions.  */
1460    
1461 void
1462 gen_shifty_op (code, operands)
1463      int code;
1464      rtx *operands;
1465 {
1466   int value = INTVAL (operands[2]);
1467   int max, i;
1468
1469   /* Truncate the shift count in case it is out of bounds.  */
1470   value = value & 0x1f;
1471  
1472   if (value == 31)
1473     {
1474       if (code == LSHIFTRT)
1475         {
1476           emit_insn (gen_rotlsi3_1 (operands[0], operands[0]));
1477           emit_insn (gen_movt (operands[0]));
1478           return;
1479         }
1480       else if (code == ASHIFT)
1481         {
1482           /* There is a two instruction sequence for 31 bit left shifts,
1483              but it requires r0.  */
1484           if (GET_CODE (operands[0]) == REG && REGNO (operands[0]) == 0)
1485             {
1486               emit_insn (gen_andsi3 (operands[0], operands[0], const1_rtx));
1487               emit_insn (gen_rotlsi3_31 (operands[0], operands[0]));
1488               return;
1489             }
1490         }
1491     }
1492   else if (value == 0)
1493     {
1494       /* This can happen when not optimizing.  We must output something here
1495          to prevent the compiler from aborting in final.c after the try_split
1496          call.  */
1497       emit_insn (gen_nop ());
1498       return;
1499     }
1500
1501   max = shift_insns[value];
1502   for (i = 0; i < max; i++)
1503     gen_ashift (code, shift_amounts[value][i], operands[0]);
1504 }
1505    
1506 /* Same as above, but optimized for values where the topmost bits don't
1507    matter.  */
1508
1509 void
1510 gen_shifty_hi_op (code, operands)
1511      int code;
1512      rtx *operands;
1513 {
1514   int value = INTVAL (operands[2]);
1515   int max, i;
1516   void (*gen_fun) PARAMS ((int, int, rtx));
1517
1518   /* This operation is used by and_shl for SImode values with a few
1519      high bits known to be cleared.  */
1520   value &= 31;
1521   if (value == 0)
1522     {
1523       emit_insn (gen_nop ());
1524       return;
1525     }
1526
1527   gen_fun = GET_MODE (operands[0]) == HImode ? gen_ashift_hi : gen_ashift;
1528   if (code == ASHIFT)
1529     {
1530       max = ext_shift_insns[value];
1531       for (i = 0; i < max; i++)
1532         gen_fun (code, ext_shift_amounts[value][i], operands[0]);
1533     }
1534   else
1535     /* When shifting right, emit the shifts in reverse order, so that
1536        solitary negative values come first.  */
1537     for (i = ext_shift_insns[value] - 1; i >= 0; i--)
1538       gen_fun (code, ext_shift_amounts[value][i], operands[0]);
1539 }
1540
1541 /* Output RTL for an arithmetic right shift.  */
1542
1543 /* ??? Rewrite to use super-optimizer sequences.  */
1544
1545 int
1546 expand_ashiftrt (operands)
1547      rtx *operands;
1548 {
1549   rtx sym;
1550   rtx wrk;
1551   char func[18];
1552   tree func_name;
1553   int value;
1554
1555   if (TARGET_SH3)
1556     {
1557       if (GET_CODE (operands[2]) != CONST_INT)
1558         {
1559           rtx count = copy_to_mode_reg (SImode, operands[2]);
1560           emit_insn (gen_negsi2 (count, count));
1561           emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
1562           return 1;
1563         }
1564       else if (ashiftrt_insns[INTVAL (operands[2]) & 31]
1565                > 1 + SH_DYNAMIC_SHIFT_COST)
1566         {
1567           rtx count
1568             = force_reg (SImode, GEN_INT (- (INTVAL (operands[2]) & 31)));
1569           emit_insn (gen_ashrsi3_d (operands[0], operands[1], count));
1570           return 1;
1571         }
1572     }
1573   if (GET_CODE (operands[2]) != CONST_INT)
1574     return 0;
1575
1576   value = INTVAL (operands[2]) & 31;
1577
1578   if (value == 31)
1579     {
1580       emit_insn (gen_ashrsi2_31 (operands[0], operands[1]));
1581       return 1;
1582     }
1583   else if (value >= 16 && value <= 19)
1584     {
1585       wrk = gen_reg_rtx (SImode);
1586       emit_insn (gen_ashrsi2_16 (wrk, operands[1]));
1587       value -= 16;
1588       while (value--)
1589         gen_ashift (ASHIFTRT, 1, wrk);
1590       emit_move_insn (operands[0], wrk);
1591       return 1;
1592     }
1593   /* Expand a short sequence inline, longer call a magic routine.  */
1594   else if (value <= 5)
1595     {
1596       wrk = gen_reg_rtx (SImode);
1597       emit_move_insn (wrk, operands[1]);
1598       while (value--)
1599         gen_ashift (ASHIFTRT, 1, wrk);
1600       emit_move_insn (operands[0], wrk);
1601       return 1;
1602     }
1603
1604   wrk = gen_reg_rtx (Pmode);
1605
1606   /* Load the value into an arg reg and call a helper.  */
1607   emit_move_insn (gen_rtx_REG (SImode, 4), operands[1]);
1608   sprintf (func, "__ashiftrt_r4_%d", value);
1609   func_name = get_identifier (func);
1610   sym = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (func_name));
1611   emit_move_insn (wrk, sym);
1612   emit_insn (gen_ashrsi3_n (GEN_INT (value), wrk));
1613   emit_move_insn (operands[0], gen_rtx_REG (SImode, 4));
1614   return 1;
1615 }
1616
1617 int
1618 sh_dynamicalize_shift_p (count)
1619      rtx count;
1620 {
1621   return shift_insns[INTVAL (count)] > 1 + SH_DYNAMIC_SHIFT_COST;
1622 }
1623
1624 /* Try to find a good way to implement the combiner pattern
1625   [(set (match_operand:SI 0 "register_operand" "r")
1626         (and:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
1627                            (match_operand:SI 2 "const_int_operand" "n"))
1628                 (match_operand:SI 3 "const_int_operand" "n"))) .
1629   LEFT_RTX is operand 2 in the above pattern, and MASK_RTX is operand 3.
1630   return 0 for simple right / left or left/right shift combination.
1631   return 1 for a combination of shifts with zero_extend.
1632   return 2 for a combination of shifts with an AND that needs r0.
1633   return 3 for a combination of shifts with an AND that needs an extra
1634     scratch register, when the three highmost bits of the AND mask are clear.
1635   return 4 for a combination of shifts with an AND that needs an extra
1636     scratch register, when any of the three highmost bits of the AND mask
1637     is set.
1638   If ATTRP is set, store an initial right shift width in ATTRP[0],
1639   and the instruction length in ATTRP[1] .  These values are not valid
1640   when returning 0.
1641   When ATTRP is set and returning 1, ATTRP[2] gets set to the index into
1642   shift_amounts for the last shift value that is to be used before the
1643   sign extend.  */
1644 int
1645 shl_and_kind (left_rtx, mask_rtx, attrp)
1646      rtx left_rtx, mask_rtx;
1647      int *attrp;
1648 {
1649   unsigned HOST_WIDE_INT mask, lsb, mask2, lsb2;
1650   int left = INTVAL (left_rtx), right;
1651   int best = 0;
1652   int cost, best_cost = 10000;
1653   int best_right = 0, best_len = 0;
1654   int i;
1655   int can_ext;
1656
1657   if (left < 0 || left > 31)
1658     return 0;
1659   if (GET_CODE (mask_rtx) == CONST_INT)
1660     mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> left;
1661   else
1662     mask = (unsigned HOST_WIDE_INT) GET_MODE_MASK (SImode) >> left;
1663   /* Can this be expressed as a right shift / left shift pair ? */
1664   lsb = ((mask ^ (mask - 1)) >> 1) + 1;
1665   right = exact_log2 (lsb);
1666   mask2 = ~(mask + lsb - 1);
1667   lsb2 = ((mask2 ^ (mask2 - 1)) >> 1) + 1;
1668   /* mask has no zeroes but trailing zeroes <==> ! mask2 */
1669   if (! mask2)
1670     best_cost = shift_insns[right] + shift_insns[right + left];
1671   /* mask has no trailing zeroes <==> ! right */
1672   else if (! right && mask2 == ~(lsb2 - 1))
1673     {
1674       int late_right = exact_log2 (lsb2);
1675       best_cost = shift_insns[left + late_right] + shift_insns[late_right];
1676     }
1677   /* Try to use zero extend */
1678   if (mask2 == ~(lsb2 - 1))
1679     {
1680       int width, first;
1681
1682       for (width = 8; width <= 16; width += 8)
1683         {
1684           /* Can we zero-extend right away? */
1685           if (lsb2 == (unsigned HOST_WIDE_INT)1 << width)
1686             {
1687               cost
1688                 = 1 + ext_shift_insns[right] + ext_shift_insns[left + right];
1689               if (cost < best_cost)
1690                 {
1691                   best = 1;
1692                   best_cost = cost;
1693                   best_right = right;
1694                   best_len = cost;
1695                   if (attrp)
1696                     attrp[2] = -1;
1697                 }
1698               continue;
1699             }
1700           /* ??? Could try to put zero extend into initial right shift,
1701              or even shift a bit left before the right shift.  */
1702           /* Determine value of first part of left shift, to get to the
1703              zero extend cut-off point.  */
1704           first = width - exact_log2 (lsb2) + right;
1705           if (first >= 0 && right + left - first >= 0)
1706             {
1707               cost = ext_shift_insns[right] + ext_shift_insns[first] + 1
1708                 + ext_shift_insns[right + left - first];
1709               if (cost < best_cost)
1710                 {
1711                   best = 1;
1712                   best_cost = cost;
1713                   best_right = right;
1714                   best_len = cost;
1715                   if (attrp)
1716                     attrp[2] = first;
1717                   }
1718             }
1719         }
1720     }
1721   /* Try to use r0 AND pattern */
1722   for (i = 0; i <= 2; i++)
1723     {
1724       if (i > right)
1725         break;
1726       if (! CONST_OK_FOR_L (mask >> i))
1727         continue;
1728       cost = (i != 0) + 2 + ext_shift_insns[left + i];
1729       if (cost < best_cost)
1730         {
1731           best = 2;
1732           best_cost = cost;
1733           best_right = i;
1734           best_len = cost - 1;
1735         }
1736     }
1737   /* Try to use a scratch register to hold the AND operand.  */
1738   can_ext = ((mask << left) & ((unsigned HOST_WIDE_INT)3 << 30)) == 0;
1739   for (i = 0; i <= 2; i++)
1740     {
1741       if (i > right)
1742         break;
1743       cost = (i != 0) + (CONST_OK_FOR_I (mask >> i) ? 2 : 3)
1744         + (can_ext ? ext_shift_insns : shift_insns)[left + i];
1745       if (cost < best_cost)
1746         {
1747           best = 4 - can_ext;
1748           best_cost = cost;
1749           best_right = i;
1750           best_len = cost - 1 - ! CONST_OK_FOR_I (mask >> i);
1751         }
1752     }
1753
1754   if (attrp)
1755     {
1756       attrp[0] = best_right;
1757       attrp[1] = best_len;
1758     }
1759   return best;
1760 }
1761
1762 /* This is used in length attributes of the unnamed instructions
1763    corresponding to shl_and_kind return values of 1 and 2.  */
1764 int
1765 shl_and_length (insn)
1766      rtx insn;
1767 {
1768   rtx set_src, left_rtx, mask_rtx;
1769   int attributes[3];
1770
1771   set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1772   left_rtx = XEXP (XEXP (set_src, 0), 1);
1773   mask_rtx = XEXP (set_src, 1);
1774   shl_and_kind (left_rtx, mask_rtx, attributes);
1775   return attributes[1];
1776 }
1777
1778 /* This is used in length attribute of the and_shl_scratch instruction.  */
1779
1780 int
1781 shl_and_scr_length (insn)
1782      rtx insn;
1783 {
1784   rtx set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
1785   int len = shift_insns[INTVAL (XEXP (set_src, 1))];
1786   rtx op = XEXP (set_src, 0);
1787   len += shift_insns[INTVAL (XEXP (op, 1))] + 1;
1788   op = XEXP (XEXP (op, 0), 0);
1789   return len + shift_insns[INTVAL (XEXP (op, 1))];
1790 }
1791
1792 /* Generating rtl? */
1793 extern int rtx_equal_function_value_matters;
1794
1795 /* Generate rtl for instructions for which shl_and_kind advised a particular
1796    method of generating them, i.e. returned zero.  */
1797
1798 int
1799 gen_shl_and (dest, left_rtx, mask_rtx, source)
1800      rtx dest, left_rtx, mask_rtx, source;
1801 {
1802   int attributes[3];
1803   unsigned HOST_WIDE_INT mask;
1804   int kind = shl_and_kind (left_rtx, mask_rtx, attributes);
1805   int right, total_shift;
1806   void (*shift_gen_fun) PARAMS ((int, rtx*)) = gen_shifty_hi_op;
1807
1808   right = attributes[0];
1809   total_shift = INTVAL (left_rtx) + right;
1810   mask = (unsigned HOST_WIDE_INT) INTVAL (mask_rtx) >> total_shift;
1811   switch (kind)
1812     {
1813     default:
1814       return -1;
1815     case 1:
1816       {
1817         int first = attributes[2];
1818         rtx operands[3];
1819
1820         if (first < 0)
1821           {
1822             emit_insn ((mask << right) <= 0xff
1823                        ? gen_zero_extendqisi2(dest,
1824                                               gen_lowpart (QImode, source))
1825                        : gen_zero_extendhisi2(dest,
1826                                               gen_lowpart (HImode, source)));
1827             source = dest;
1828           }
1829         if (source != dest)
1830           emit_insn (gen_movsi (dest, source));
1831         operands[0] = dest;
1832         if (right)
1833           {
1834             operands[2] = GEN_INT (right);
1835             gen_shifty_hi_op (LSHIFTRT, operands);
1836           }
1837         if (first > 0)
1838           {
1839             operands[2] = GEN_INT (first);
1840             gen_shifty_hi_op (ASHIFT, operands);
1841             total_shift -= first;
1842             mask <<= first;
1843           }
1844         if (first >= 0)
1845           emit_insn (mask <= 0xff
1846                      ? gen_zero_extendqisi2(dest, gen_lowpart (QImode, dest))
1847                      : gen_zero_extendhisi2(dest, gen_lowpart (HImode, dest)));
1848         if (total_shift > 0)
1849           {
1850             operands[2] = GEN_INT (total_shift);
1851             gen_shifty_hi_op (ASHIFT, operands);
1852           }
1853         break;
1854       }
1855     case 4:
1856       shift_gen_fun = gen_shifty_op;
1857     case 3:
1858       /* If the topmost bit that matters is set, set the topmost bits
1859          that don't matter.  This way, we might be able to get a shorter
1860          signed constant.  */
1861       if (mask & ((HOST_WIDE_INT)1 << (31 - total_shift)))
1862         mask |= (HOST_WIDE_INT)~0 << (31 - total_shift);
1863     case 2:
1864       /* Don't expand fine-grained when combining, because that will
1865          make the pattern fail.  */
1866       if (rtx_equal_function_value_matters
1867           || reload_in_progress || reload_completed)
1868         {
1869           rtx operands[3];
1870   
1871           /* Cases 3 and 4 should be handled by this split
1872              only while combining  */
1873           if (kind > 2)
1874             abort ();
1875           if (right)
1876             {
1877               emit_insn (gen_lshrsi3 (dest, source, GEN_INT (right)));
1878               source = dest;
1879             }
1880           emit_insn (gen_andsi3 (dest, source, GEN_INT (mask)));
1881           if (total_shift)
1882             {
1883               operands[0] = dest;
1884               operands[1] = dest;
1885               operands[2] = GEN_INT (total_shift);
1886               shift_gen_fun (ASHIFT, operands);
1887             }
1888           break;
1889         }
1890       else
1891         {
1892           int neg = 0;
1893           if (kind != 4 && total_shift < 16)
1894             {
1895               neg = -ext_shift_amounts[total_shift][1];
1896               if (neg > 0)
1897                 neg -= ext_shift_amounts[total_shift][2];
1898               else
1899                 neg = 0;
1900             }
1901           emit_insn (gen_and_shl_scratch (dest, source,
1902                                           GEN_INT (right),
1903                                           GEN_INT (mask),
1904                                           GEN_INT (total_shift + neg),
1905                                           GEN_INT (neg)));
1906           emit_insn (gen_movsi (dest, dest));
1907           break;
1908         }
1909     }
1910   return 0;
1911 }
1912
1913 /* Try to find a good way to implement the combiner pattern
1914   [(set (match_operand:SI 0 "register_operand" "=r")
1915         (sign_extract:SI (ashift:SI (match_operand:SI 1 "register_operand" "r")
1916                                     (match_operand:SI 2 "const_int_operand" "n")
1917                          (match_operand:SI 3 "const_int_operand" "n")
1918                          (const_int 0)))
1919    (clobber (reg:SI T_REG))]
1920   LEFT_RTX is operand 2 in the above pattern, and SIZE_RTX is operand 3.
1921   return 0 for simple left / right shift combination.
1922   return 1 for left shift / 8 bit sign extend / left shift.
1923   return 2 for left shift / 16 bit sign extend / left shift.
1924   return 3 for left shift / 8 bit sign extend / shift / sign extend.
1925   return 4 for left shift / 16 bit sign extend / shift / sign extend.
1926   return 5 for left shift / 16 bit sign extend / right shift
1927   return 6 for < 8 bit sign extend / left shift.
1928   return 7 for < 8 bit sign extend / left shift / single right shift.
1929   If COSTP is nonzero, assign the calculated cost to *COSTP.  */
1930
1931 int
1932 shl_sext_kind (left_rtx, size_rtx, costp)
1933      rtx left_rtx, size_rtx;
1934      int *costp;
1935 {
1936   int left, size, insize, ext;
1937   int cost, best_cost;
1938   int kind;
1939
1940   left = INTVAL (left_rtx);
1941   size = INTVAL (size_rtx);
1942   insize = size - left;
1943   if (insize <= 0)
1944     abort ();
1945   /* Default to left / right shift.  */
1946   kind = 0;
1947   best_cost = shift_insns[32 - insize] + ashiftrt_insns[32 - size];
1948   if (size <= 16)
1949     {
1950       /* 16 bit shift / sign extend / 16 bit shift */
1951       cost = shift_insns[16 - insize] + 1 + ashiftrt_insns[16 - size];
1952       /* If ashiftrt_insns[16 - size] is 8, this choice will be overridden
1953          below, by alternative 3 or something even better.  */
1954       if (cost < best_cost)
1955         {
1956           kind = 5;
1957           best_cost = cost;
1958         }
1959     }
1960   /* Try a plain sign extend between two shifts.  */
1961   for (ext = 16; ext >= insize; ext -= 8)
1962     {
1963       if (ext <= size)
1964         {
1965           cost = ext_shift_insns[ext - insize] + 1 + shift_insns[size - ext];
1966           if (cost < best_cost)
1967             {
1968               kind = ext / (unsigned) 8;
1969               best_cost = cost;
1970             }
1971         }
1972       /* Check if we can do a sloppy shift with a final signed shift
1973          restoring the sign.  */
1974       if (EXT_SHIFT_SIGNED (size - ext))
1975         cost = ext_shift_insns[ext - insize] + ext_shift_insns[size - ext] + 1;
1976       /* If not, maybe it's still cheaper to do the second shift sloppy,
1977          and do a final sign extend?  */
1978       else if (size <= 16)
1979         cost = ext_shift_insns[ext - insize] + 1
1980           + ext_shift_insns[size > ext ? size - ext : ext - size] + 1;
1981       else
1982         continue;
1983       if (cost < best_cost)
1984         {
1985           kind = ext / (unsigned) 8 + 2;
1986           best_cost = cost;
1987         }
1988     }
1989   /* Check if we can sign extend in r0 */
1990   if (insize < 8)
1991     {
1992       cost = 3 + shift_insns[left];
1993       if (cost < best_cost)
1994         {
1995           kind = 6;
1996           best_cost = cost;
1997         }
1998       /* Try the same with a final signed shift.  */
1999       if (left < 31)
2000         {
2001           cost = 3 + ext_shift_insns[left + 1] + 1;
2002           if (cost < best_cost)
2003             {
2004               kind = 7;
2005               best_cost = cost;
2006             }
2007         }
2008     }
2009   if (TARGET_SH3)
2010     {
2011       /* Try to use a dynamic shift.  */
2012       cost = shift_insns[32 - insize] + 1 + SH_DYNAMIC_SHIFT_COST;
2013       if (cost < best_cost)
2014         {
2015           kind = 0;
2016           best_cost = cost;
2017         }
2018     }
2019   if (costp)
2020     *costp = cost;
2021   return kind;
2022 }
2023
2024 /* Function to be used in the length attribute of the instructions
2025    implementing this pattern.  */
2026
2027 int
2028 shl_sext_length (insn)
2029      rtx insn;
2030 {
2031   rtx set_src, left_rtx, size_rtx;
2032   int cost;
2033
2034   set_src = SET_SRC (XVECEXP (PATTERN (insn), 0, 0));
2035   left_rtx = XEXP (XEXP (set_src, 0), 1);
2036   size_rtx = XEXP (set_src, 1);
2037   shl_sext_kind (left_rtx, size_rtx, &cost);
2038   return cost;
2039 }
2040
2041 /* Generate rtl for this pattern */
2042
2043 int
2044 gen_shl_sext (dest, left_rtx, size_rtx, source)
2045      rtx dest, left_rtx, size_rtx, source;
2046 {
2047   int kind;
2048   int left, size, insize, cost;
2049   rtx operands[3];
2050
2051   kind = shl_sext_kind (left_rtx, size_rtx, &cost);
2052   left = INTVAL (left_rtx);
2053   size = INTVAL (size_rtx);
2054   insize = size - left;
2055   switch (kind)
2056     {
2057     case 1:
2058     case 2:
2059     case 3:
2060     case 4:
2061       {
2062         int ext = kind & 1 ? 8 : 16;
2063         int shift2 = size - ext;
2064
2065         /* Don't expand fine-grained when combining, because that will
2066            make the pattern fail.  */
2067         if (! rtx_equal_function_value_matters
2068             && ! reload_in_progress && ! reload_completed)
2069           {
2070             emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
2071             emit_insn (gen_movsi (dest, source));
2072             break;
2073           }
2074         if (dest != source)
2075           emit_insn (gen_movsi (dest, source));
2076         operands[0] = dest;
2077         if (ext - insize)
2078           {
2079             operands[2] = GEN_INT (ext - insize);
2080             gen_shifty_hi_op (ASHIFT, operands);
2081           }
2082         emit_insn (kind & 1
2083                    ? gen_extendqisi2(dest, gen_lowpart (QImode, dest))
2084                    : gen_extendhisi2(dest, gen_lowpart (HImode, dest)));
2085         if (kind <= 2)
2086           {
2087             if (shift2)
2088               {
2089                 operands[2] = GEN_INT (shift2);
2090                 gen_shifty_op (ASHIFT, operands);
2091               }
2092           }
2093         else
2094           {
2095             if (shift2 > 0)
2096               {
2097                 if (EXT_SHIFT_SIGNED (shift2))
2098                   {
2099                     operands[2] = GEN_INT (shift2 + 1);
2100                     gen_shifty_op (ASHIFT, operands);
2101                     operands[2] = GEN_INT (1);
2102                     gen_shifty_op (ASHIFTRT, operands);
2103                     break;
2104                   }
2105                 operands[2] = GEN_INT (shift2);
2106                 gen_shifty_hi_op (ASHIFT, operands);
2107               }
2108             else if (shift2)
2109               {
2110                 operands[2] = GEN_INT (-shift2);
2111                 gen_shifty_hi_op (LSHIFTRT, operands);
2112               }
2113             emit_insn (size <= 8
2114                        ? gen_extendqisi2 (dest, gen_lowpart (QImode, dest))
2115                        : gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
2116           }
2117         break;
2118       }
2119     case 5:
2120       {
2121         int i = 16 - size;
2122         if (! rtx_equal_function_value_matters
2123             && ! reload_in_progress && ! reload_completed)
2124           emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
2125         else
2126           {
2127             operands[0] = dest;
2128             operands[2] = GEN_INT (16 - insize);
2129             gen_shifty_hi_op (ASHIFT, operands);
2130             emit_insn (gen_extendhisi2 (dest, gen_lowpart (HImode, dest)));
2131           }
2132         /* Don't use gen_ashrsi3 because it generates new pseudos.  */
2133         while (--i >= 0)
2134           gen_ashift (ASHIFTRT, 1, dest);
2135         break;
2136       }
2137     case 6:
2138     case 7:
2139       /* Don't expand fine-grained when combining, because that will
2140          make the pattern fail.  */
2141       if (! rtx_equal_function_value_matters
2142           && ! reload_in_progress && ! reload_completed)
2143         {
2144           emit_insn (gen_shl_sext_ext (dest, source, left_rtx, size_rtx));
2145           emit_insn (gen_movsi (dest, source));
2146           break;
2147         }
2148       emit_insn (gen_andsi3 (dest, source, GEN_INT ((1 << insize) - 1)));
2149       emit_insn (gen_xorsi3 (dest, dest, GEN_INT (1 << (insize - 1))));
2150       emit_insn (gen_addsi3 (dest, dest, GEN_INT (-1 << (insize - 1))));
2151       operands[0] = dest;
2152       operands[2] = kind == 7 ? GEN_INT (left + 1) : left_rtx;
2153       gen_shifty_op (ASHIFT, operands);
2154       if (kind == 7)
2155         emit_insn (gen_ashrsi3_k (dest, dest, GEN_INT (1)));
2156       break;
2157     default:
2158       return -1;
2159     }
2160   return 0;
2161 }
2162
2163 /* Prefix a symbol_ref name with "datalabel".  */
2164
2165 rtx
2166 gen_datalabel_ref (sym)
2167      rtx sym;
2168 {
2169   if (GET_CODE (sym) == LABEL_REF)
2170     return gen_rtx_CONST (GET_MODE (sym),
2171                           gen_rtx_UNSPEC (GET_MODE (sym),
2172                                           gen_rtvec (1, sym),
2173                                           UNSPEC_DATALABEL));
2174     
2175   if (GET_CODE (sym) != SYMBOL_REF)
2176     abort ();
2177
2178   XSTR (sym, 0) = concat (SH_DATALABEL_ENCODING, XSTR (sym, 0), NULL);
2179
2180   return sym;
2181 }
2182
2183 \f
2184 /* The SH cannot load a large constant into a register, constants have to
2185    come from a pc relative load.  The reference of a pc relative load
2186    instruction must be less than 1k infront of the instruction.  This
2187    means that we often have to dump a constant inside a function, and
2188    generate code to branch around it.
2189
2190    It is important to minimize this, since the branches will slow things
2191    down and make things bigger.
2192
2193    Worst case code looks like:
2194
2195    mov.l L1,rn
2196    bra   L2
2197    nop
2198    align
2199    L1:   .long value
2200    L2:
2201    ..
2202
2203    mov.l L3,rn
2204    bra   L4
2205    nop
2206    align
2207    L3:   .long value
2208    L4:
2209    ..
2210
2211    We fix this by performing a scan before scheduling, which notices which
2212    instructions need to have their operands fetched from the constant table
2213    and builds the table.
2214
2215    The algorithm is:
2216
2217    scan, find an instruction which needs a pcrel move.  Look forward, find the
2218    last barrier which is within MAX_COUNT bytes of the requirement.
2219    If there isn't one, make one.  Process all the instructions between
2220    the find and the barrier.
2221
2222    In the above example, we can tell that L3 is within 1k of L1, so
2223    the first move can be shrunk from the 3 insn+constant sequence into
2224    just 1 insn, and the constant moved to L3 to make:
2225
2226    mov.l        L1,rn
2227    ..
2228    mov.l        L3,rn
2229    bra          L4
2230    nop
2231    align
2232    L3:.long value
2233    L4:.long value
2234
2235    Then the second move becomes the target for the shortening process.  */
2236
2237 typedef struct
2238 {
2239   rtx value;                    /* Value in table.  */
2240   rtx label;                    /* Label of value.  */
2241   rtx wend;                     /* End of window.  */
2242   enum machine_mode mode;       /* Mode of value.  */
2243 } pool_node;
2244
2245 /* The maximum number of constants that can fit into one pool, since
2246    the pc relative range is 0...1020 bytes and constants are at least 4
2247    bytes long.  */
2248
2249 #define MAX_POOL_SIZE (1020/4)
2250 static pool_node pool_vector[MAX_POOL_SIZE];
2251 static int pool_size;
2252 static rtx pool_window_label;
2253 static int pool_window_last;
2254
2255 /* ??? If we need a constant in HImode which is the truncated value of a
2256    constant we need in SImode, we could combine the two entries thus saving
2257    two bytes.  Is this common enough to be worth the effort of implementing
2258    it?  */
2259
2260 /* ??? This stuff should be done at the same time that we shorten branches.
2261    As it is now, we must assume that all branches are the maximum size, and
2262    this causes us to almost always output constant pools sooner than
2263    necessary.  */
2264
2265 /* Add a constant to the pool and return its label.  */
2266
2267 static rtx
2268 add_constant (x, mode, last_value)
2269      rtx x;
2270      enum machine_mode mode;
2271      rtx last_value;
2272 {
2273   int i;
2274   rtx lab, new, ref, newref;
2275
2276   /* First see if we've already got it.  */
2277   for (i = 0; i < pool_size; i++)
2278     {
2279       if (x->code == pool_vector[i].value->code
2280           && mode == pool_vector[i].mode)
2281         {
2282           if (x->code == CODE_LABEL)
2283             {
2284               if (XINT (x, 3) != XINT (pool_vector[i].value, 3))
2285                 continue;
2286             }
2287           if (rtx_equal_p (x, pool_vector[i].value))
2288             {
2289               lab = new = 0;
2290               if (! last_value
2291                   || ! i
2292                   || ! rtx_equal_p (last_value, pool_vector[i-1].value))
2293                 {
2294                   new = gen_label_rtx ();
2295                   LABEL_REFS (new) = pool_vector[i].label;
2296                   pool_vector[i].label = lab = new;
2297                 }
2298               if (lab && pool_window_label)
2299                 {
2300                   newref = gen_rtx_LABEL_REF (VOIDmode, pool_window_label);
2301                   ref = pool_vector[pool_window_last].wend;
2302                   LABEL_NEXTREF (newref) = ref;
2303                   pool_vector[pool_window_last].wend = newref;
2304                 }
2305               if (new)
2306                 pool_window_label = new;
2307               pool_window_last = i;
2308               return lab;
2309             }
2310         }
2311     }
2312
2313   /* Need a new one.  */
2314   pool_vector[pool_size].value = x;
2315   if (last_value && rtx_equal_p (last_value, pool_vector[pool_size - 1].value))
2316     lab = 0;
2317   else
2318     lab = gen_label_rtx ();
2319   pool_vector[pool_size].mode = mode;
2320   pool_vector[pool_size].label = lab;
2321   pool_vector[pool_size].wend = NULL_RTX;
2322   if (lab && pool_window_label)
2323     {
2324       newref = gen_rtx_LABEL_REF (VOIDmode, pool_window_label);
2325       ref = pool_vector[pool_window_last].wend;
2326       LABEL_NEXTREF (newref) = ref;
2327       pool_vector[pool_window_last].wend = newref;
2328     }
2329   if (lab)
2330     pool_window_label = lab;
2331   pool_window_last = pool_size;
2332   pool_size++;
2333   return lab;
2334 }
2335
2336 /* Output the literal table.  */
2337
2338 static void
2339 dump_table (scan)
2340      rtx scan;
2341 {
2342   int i;
2343   int need_align = 1;
2344   rtx lab, ref;
2345   int have_di = 0;
2346
2347   /* Do two passes, first time dump out the HI sized constants.  */
2348
2349   for (i = 0; i < pool_size; i++)
2350     {
2351       pool_node *p = &pool_vector[i];
2352
2353       if (p->mode == HImode)
2354         {
2355           if (need_align)
2356             {
2357               scan = emit_insn_after (gen_align_2 (), scan);
2358               need_align = 0;
2359             }
2360           for (lab = p->label; lab; lab = LABEL_REFS (lab))
2361             scan = emit_label_after (lab, scan);
2362           scan = emit_insn_after (gen_consttable_2 (p->value, const0_rtx),
2363                                   scan);
2364           for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2365             {
2366               lab = XEXP (ref, 0);
2367               scan = emit_insn_after (gen_consttable_window_end (lab), scan);
2368             }
2369         }
2370       else if (p->mode == DImode || p->mode == DFmode)
2371         have_di = 1;
2372     }
2373
2374   need_align = 1;
2375
2376   if (TARGET_SHCOMPACT && have_di)
2377     {
2378       rtx align_insn = NULL_RTX;
2379
2380       scan = emit_label_after (gen_label_rtx (), scan);
2381       scan = emit_insn_after (gen_align_log (GEN_INT (3)), scan);
2382       need_align = 0;
2383
2384       for (i = 0; i < pool_size; i++)
2385         {
2386           pool_node *p = &pool_vector[i];
2387
2388           switch (p->mode)
2389             {
2390             case HImode:
2391               break;
2392             case SImode:
2393             case SFmode:
2394               if (align_insn)
2395                 {
2396                   for (lab = p->label; lab; lab = LABEL_REFS (lab))
2397                     emit_label_before (lab, align_insn);
2398                   emit_insn_before (gen_consttable_4 (p->value, const0_rtx),
2399                                     align_insn);
2400                   for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2401                     {
2402                       lab = XEXP (ref, 0);
2403                       emit_insn_before (gen_consttable_window_end (lab),
2404                                        align_insn);
2405                     }
2406                   delete_insn (align_insn);
2407                   align_insn = NULL_RTX;
2408                   continue;
2409                 }
2410               else
2411                 {
2412                   for (lab = p->label; lab; lab = LABEL_REFS (lab))
2413                     scan = emit_label_after (lab, scan);
2414                   scan = emit_insn_after (gen_consttable_4 (p->value,
2415                                                             const0_rtx), scan);
2416                   need_align = ! need_align;
2417                 }
2418               break;
2419             case DFmode:
2420             case DImode:
2421               if (need_align)
2422                 {
2423                   scan = emit_insn_after (gen_align_log (GEN_INT (3)), scan);
2424                   align_insn = scan;
2425                   need_align = 0;
2426                 }
2427               for (lab = p->label; lab; lab = LABEL_REFS (lab))
2428                 scan = emit_label_after (lab, scan);
2429               scan = emit_insn_after (gen_consttable_8 (p->value, const0_rtx),
2430                                       scan);
2431               break;
2432             default:
2433               abort ();
2434               break;
2435             }
2436
2437           if (p->mode != HImode)
2438             {
2439               for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2440                 {
2441                   lab = XEXP (ref, 0);
2442                   scan = emit_insn_after (gen_consttable_window_end (lab),
2443                                           scan);
2444                 }
2445             }
2446         }
2447
2448       pool_size = 0;
2449     }
2450   
2451   for (i = 0; i < pool_size; i++)
2452     {
2453       pool_node *p = &pool_vector[i];
2454
2455       switch (p->mode)
2456         {
2457         case HImode:
2458           break;
2459         case SImode:
2460         case SFmode:
2461           if (need_align)
2462             {
2463               need_align = 0;
2464               scan = emit_label_after (gen_label_rtx (), scan);
2465               scan = emit_insn_after (gen_align_4 (), scan);
2466             }
2467           for (lab = p->label; lab; lab = LABEL_REFS (lab))
2468             scan = emit_label_after (lab, scan);
2469           scan = emit_insn_after (gen_consttable_4 (p->value, const0_rtx),
2470                                   scan);
2471           break;
2472         case DFmode:
2473         case DImode:
2474           if (need_align)
2475             {
2476               need_align = 0;
2477               scan = emit_label_after (gen_label_rtx (), scan);
2478               scan = emit_insn_after (gen_align_4 (), scan);
2479             }
2480           for (lab = p->label; lab; lab = LABEL_REFS (lab))
2481             scan = emit_label_after (lab, scan);
2482           scan = emit_insn_after (gen_consttable_8 (p->value, const0_rtx),
2483                                   scan);
2484           break;
2485         default:
2486           abort ();
2487           break;
2488         }
2489
2490       if (p->mode != HImode)
2491         {
2492           for (ref = p->wend; ref; ref = LABEL_NEXTREF (ref))
2493             {
2494               lab = XEXP (ref, 0);
2495               scan = emit_insn_after (gen_consttable_window_end (lab), scan);
2496             }
2497         }
2498     }
2499
2500   scan = emit_insn_after (gen_consttable_end (), scan);
2501   scan = emit_barrier_after (scan);
2502   pool_size = 0;
2503   pool_window_label = NULL_RTX;
2504   pool_window_last = 0;
2505 }
2506
2507 /* Return non-zero if constant would be an ok source for a
2508    mov.w instead of a mov.l.  */
2509
2510 static int
2511 hi_const (src)
2512      rtx src;
2513 {
2514   return (GET_CODE (src) == CONST_INT
2515           && INTVAL (src) >= -32768
2516           && INTVAL (src) <= 32767);
2517 }
2518
2519 /* Non-zero if the insn is a move instruction which needs to be fixed.  */
2520
2521 /* ??? For a DImode/DFmode moves, we don't need to fix it if each half of the
2522    CONST_DOUBLE input value is CONST_OK_FOR_I.  For a SFmode move, we don't
2523    need to fix it if the input value is CONST_OK_FOR_I.  */
2524
2525 static int
2526 broken_move (insn)
2527      rtx insn;
2528 {
2529   if (GET_CODE (insn) == INSN)
2530     {
2531       rtx pat = PATTERN (insn);
2532       if (GET_CODE (pat) == PARALLEL)
2533         pat = XVECEXP (pat, 0, 0);
2534       if (GET_CODE (pat) == SET
2535           /* We can load any 8 bit value if we don't care what the high
2536              order bits end up as.  */
2537           && GET_MODE (SET_DEST (pat)) != QImode
2538           && (CONSTANT_P (SET_SRC (pat))
2539               /* Match mova_const.  */
2540               || (GET_CODE (SET_SRC (pat)) == UNSPEC
2541                   && XINT (SET_SRC (pat), 1) == UNSPEC_MOVA
2542                   && GET_CODE (XVECEXP (SET_SRC (pat), 0, 0)) == CONST))
2543           && ! (TARGET_SH3E
2544                 && GET_CODE (SET_SRC (pat)) == CONST_DOUBLE
2545                 && (fp_zero_operand (SET_SRC (pat))
2546                     || fp_one_operand (SET_SRC (pat)))
2547                 /* ??? If this is a -m4 or -m4-single compilation, in general
2548                    we don't know the current setting of fpscr, so disable fldi.
2549                    There is an exception if this was a register-register move
2550                    before reload - and hence it was ascertained that we have
2551                    single precision setting - and in a post-reload optimization
2552                    we changed this to do a constant load.  In that case
2553                    we don't have an r0 clobber, hence we must use fldi.  */
2554                 && (! TARGET_SH4 || TARGET_FMOVD
2555                     || (GET_CODE (XEXP (XVECEXP (PATTERN (insn), 0, 2), 0))
2556                         == SCRATCH))
2557                 && GET_CODE (SET_DEST (pat)) == REG
2558                 && FP_REGISTER_P (REGNO (SET_DEST (pat))))
2559           && (GET_CODE (SET_SRC (pat)) != CONST_INT
2560               || ! CONST_OK_FOR_I (INTVAL (SET_SRC (pat)))))
2561         return 1;
2562     }
2563
2564   return 0;
2565 }
2566
2567 static int
2568 mova_p (insn)
2569      rtx insn;
2570 {
2571   return (GET_CODE (insn) == INSN
2572           && GET_CODE (PATTERN (insn)) == SET
2573           && GET_CODE (SET_SRC (PATTERN (insn))) == UNSPEC
2574           && XINT (SET_SRC (PATTERN (insn)), 1) == UNSPEC_MOVA
2575           /* Don't match mova_const.  */
2576           && GET_CODE (XVECEXP (SET_SRC (PATTERN (insn)), 0, 0)) == LABEL_REF);
2577 }
2578
2579 /* Find the last barrier from insn FROM which is close enough to hold the
2580    constant pool.  If we can't find one, then create one near the end of
2581    the range.  */
2582
2583 static rtx
2584 find_barrier (num_mova, mova, from)
2585      int num_mova;
2586      rtx mova, from;
2587 {
2588   int count_si = 0;
2589   int count_hi = 0;
2590   int found_hi = 0;
2591   int found_si = 0;
2592   int found_di = 0;
2593   int hi_align = 2;
2594   int si_align = 2;
2595   int leading_mova = num_mova;
2596   rtx barrier_before_mova, found_barrier = 0, good_barrier = 0;
2597   int si_limit;
2598   int hi_limit;
2599
2600   /* For HImode: range is 510, add 4 because pc counts from address of
2601      second instruction after this one, subtract 2 for the jump instruction
2602      that we may need to emit before the table, subtract 2 for the instruction
2603      that fills the jump delay slot (in very rare cases, reorg will take an
2604      instruction from after the constant pool or will leave the delay slot
2605      empty).  This gives 510.
2606      For SImode: range is 1020, add 4 because pc counts from address of
2607      second instruction after this one, subtract 2 in case pc is 2 byte
2608      aligned, subtract 2 for the jump instruction that we may need to emit
2609      before the table, subtract 2 for the instruction that fills the jump
2610      delay slot.  This gives 1018.  */
2611
2612   /* The branch will always be shortened now that the reference address for
2613      forward branches is the successor address, thus we need no longer make
2614      adjustments to the [sh]i_limit for -O0.  */
2615
2616   si_limit = 1018;
2617   hi_limit = 510;
2618
2619   while (from && count_si < si_limit && count_hi < hi_limit)
2620     {
2621       int inc = get_attr_length (from);
2622       int new_align = 1;
2623
2624       if (GET_CODE (from) == CODE_LABEL)
2625         {
2626           if (optimize)
2627             new_align = 1 << label_to_alignment (from);
2628           else if (GET_CODE (prev_nonnote_insn (from)) == BARRIER)
2629             new_align = 1 << barrier_align (from);
2630           else
2631             new_align = 1;
2632           inc = 0;
2633         }
2634
2635       if (GET_CODE (from) == BARRIER)
2636         {
2637
2638           found_barrier = from;
2639
2640           /* If we are at the end of the function, or in front of an alignment
2641              instruction, we need not insert an extra alignment.  We prefer
2642              this kind of barrier.  */
2643           if (barrier_align (from) > 2)
2644             good_barrier = from;
2645         }
2646
2647       if (broken_move (from))
2648         {
2649           rtx pat, src, dst;
2650           enum machine_mode mode;
2651
2652           pat = PATTERN (from);
2653           if (GET_CODE (pat) == PARALLEL)
2654             pat = XVECEXP (pat, 0, 0);
2655           src = SET_SRC (pat);
2656           dst = SET_DEST (pat);
2657           mode = GET_MODE (dst);
2658
2659           /* We must explicitly check the mode, because sometimes the
2660              front end will generate code to load unsigned constants into
2661              HImode targets without properly sign extending them.  */
2662           if (mode == HImode
2663               || (mode == SImode && hi_const (src) && REGNO (dst) != FPUL_REG))
2664             {
2665               found_hi += 2;
2666               /* We put the short constants before the long constants, so
2667                  we must count the length of short constants in the range
2668                  for the long constants.  */
2669               /* ??? This isn't optimal, but is easy to do.  */
2670               si_limit -= 2;
2671             }
2672           else
2673             {
2674               /* We dump DF/DI constants before SF/SI ones, because
2675                  the limit is the same, but the alignment requirements
2676                  are higher.  We may waste up to 4 additional bytes
2677                  for alignment, and the DF/DI constant may have
2678                  another SF/SI constant placed before it. */
2679               if (TARGET_SHCOMPACT
2680                   && ! found_di
2681                   && (mode == DFmode || mode == DImode))
2682                 {
2683                   found_di = 1;
2684                   si_limit -= 8;
2685                 }
2686               while (si_align > 2 && found_si + si_align - 2 > count_si)
2687                 si_align >>= 1;
2688               if (found_si > count_si)
2689                 count_si = found_si;
2690               found_si += GET_MODE_SIZE (mode);
2691               if (num_mova)
2692                 si_limit -= GET_MODE_SIZE (mode);
2693             }
2694
2695           /* See the code in machine_dependent_reorg, which has a similar if
2696              statement that generates a new mova insn in many cases.  */
2697           if (GET_CODE (dst) == REG && FP_ANY_REGISTER_P (REGNO (dst)))
2698             inc += 2;
2699         }
2700
2701       if (mova_p (from))
2702         {
2703           if (! num_mova++)
2704             {
2705               leading_mova = 0;
2706               mova = from;
2707               barrier_before_mova = good_barrier ? good_barrier : found_barrier;
2708             }
2709           if (found_si > count_si)
2710             count_si = found_si;
2711         }
2712       else if (GET_CODE (from) == JUMP_INSN
2713                && (GET_CODE (PATTERN (from)) == ADDR_VEC
2714                    || GET_CODE (PATTERN (from)) == ADDR_DIFF_VEC))
2715         {
2716           if (num_mova)
2717             num_mova--;
2718           if (barrier_align (next_real_insn (from)) == CACHE_LOG)
2719             {
2720               /* We have just passed the barrier in front of the
2721                  ADDR_DIFF_VEC, which is stored in found_barrier.  Since
2722                  the ADDR_DIFF_VEC is accessed as data, just like our pool
2723                  constants, this is a good opportunity to accommodate what
2724                  we have gathered so far.
2725                  If we waited any longer, we could end up at a barrier in
2726                  front of code, which gives worse cache usage for separated
2727                  instruction / data caches.  */
2728               good_barrier = found_barrier;
2729               break;
2730             }
2731           else
2732             {
2733               rtx body = PATTERN (from);
2734               inc = XVECLEN (body, 1) * GET_MODE_SIZE (GET_MODE (body));
2735             }
2736         }
2737       /* For the SH1, we generate alignments even after jumps-around-jumps.  */
2738       else if (GET_CODE (from) == JUMP_INSN
2739                && ! TARGET_SH2
2740                && ! TARGET_SMALLCODE)
2741         new_align = 4;
2742
2743       if (found_si)
2744         {
2745           count_si += inc;
2746           if (new_align > si_align)
2747             {
2748               si_limit -= (count_si - 1) & (new_align - si_align);
2749               si_align = new_align;
2750             }
2751           count_si = (count_si + new_align - 1) & -new_align;
2752         }
2753       if (found_hi)
2754         {
2755           count_hi += inc;
2756           if (new_align > hi_align)
2757             {
2758               hi_limit -= (count_hi - 1) & (new_align - hi_align);
2759               hi_align = new_align;
2760             }
2761           count_hi = (count_hi + new_align - 1) & -new_align;
2762         }
2763       from = NEXT_INSN (from);
2764     }
2765
2766   if (num_mova)
2767     {
2768       if (leading_mova)
2769         {
2770           /* Try as we might, the leading mova is out of range.  Change
2771              it into a load (which will become a pcload) and retry.  */
2772           SET_SRC (PATTERN (mova)) = XVECEXP (SET_SRC (PATTERN (mova)), 0, 0);
2773           INSN_CODE (mova) = -1;
2774           return find_barrier (0, 0, mova);
2775         }
2776       else
2777         {
2778           /* Insert the constant pool table before the mova instruction,
2779              to prevent the mova label reference from going out of range.  */
2780           from = mova;
2781           good_barrier = found_barrier = barrier_before_mova;
2782         }
2783     }
2784
2785   if (found_barrier)
2786     {
2787       if (good_barrier && next_real_insn (found_barrier))
2788         found_barrier = good_barrier;
2789     }
2790   else
2791     {
2792       /* We didn't find a barrier in time to dump our stuff,
2793          so we'll make one.  */
2794       rtx label = gen_label_rtx ();
2795
2796       /* If we exceeded the range, then we must back up over the last
2797          instruction we looked at.  Otherwise, we just need to undo the
2798          NEXT_INSN at the end of the loop.  */
2799       if (count_hi > hi_limit || count_si > si_limit)
2800         from = PREV_INSN (PREV_INSN (from));
2801       else
2802         from = PREV_INSN (from);
2803
2804       /* Walk back to be just before any jump or label.
2805          Putting it before a label reduces the number of times the branch
2806          around the constant pool table will be hit.  Putting it before
2807          a jump makes it more likely that the bra delay slot will be
2808          filled.  */
2809       while (GET_CODE (from) == JUMP_INSN || GET_CODE (from) == NOTE
2810              || GET_CODE (from) == CODE_LABEL)
2811         from = PREV_INSN (from);
2812
2813       from = emit_jump_insn_after (gen_jump (label), from);
2814       JUMP_LABEL (from) = label;
2815       LABEL_NUSES (label) = 1;
2816       found_barrier = emit_barrier_after (from);
2817       emit_label_after (label, found_barrier);
2818     }
2819
2820   return found_barrier;
2821 }
2822
2823 /* If the instruction INSN is implemented by a special function, and we can
2824    positively find the register that is used to call the sfunc, and this
2825    register is not used anywhere else in this instruction - except as the
2826    destination of a set, return this register; else, return 0.  */
2827 rtx
2828 sfunc_uses_reg (insn)
2829      rtx insn;
2830 {
2831   int i;
2832   rtx pattern, part, reg_part, reg;
2833
2834   if (GET_CODE (insn) != INSN)
2835     return 0;
2836   pattern = PATTERN (insn);
2837   if (GET_CODE (pattern) != PARALLEL || get_attr_type (insn) != TYPE_SFUNC)
2838     return 0;
2839
2840   for (reg_part = 0, i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
2841     {
2842       part = XVECEXP (pattern, 0, i);
2843       if (GET_CODE (part) == USE && GET_MODE (XEXP (part, 0)) == SImode)
2844         reg_part = part;
2845     }
2846   if (! reg_part)
2847     return 0;
2848   reg = XEXP (reg_part, 0);
2849   for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
2850     {
2851       part = XVECEXP (pattern, 0, i);
2852       if (part == reg_part || GET_CODE (part) == CLOBBER)
2853         continue;
2854       if (reg_mentioned_p (reg, ((GET_CODE (part) == SET
2855                                   && GET_CODE (SET_DEST (part)) == REG)
2856                                  ? SET_SRC (part) : part)))
2857         return 0;
2858     }
2859   return reg;
2860 }
2861
2862 /* See if the only way in which INSN uses REG is by calling it, or by
2863    setting it while calling it.  Set *SET to a SET rtx if the register
2864    is set by INSN.  */
2865
2866 static int
2867 noncall_uses_reg (reg, insn, set)
2868      rtx reg;
2869      rtx insn;
2870      rtx *set;
2871 {
2872   rtx pattern, reg2;
2873
2874   *set = NULL_RTX;
2875
2876   reg2 = sfunc_uses_reg (insn);
2877   if (reg2 && REGNO (reg2) == REGNO (reg))
2878     {
2879       pattern = single_set (insn);
2880       if (pattern
2881           && GET_CODE (SET_DEST (pattern)) == REG
2882           && REGNO (reg) == REGNO (SET_DEST (pattern)))
2883         *set = pattern;
2884       return 0;
2885     }
2886   if (GET_CODE (insn) != CALL_INSN)
2887     {
2888       /* We don't use rtx_equal_p because we don't care if the mode is
2889          different.  */
2890       pattern = single_set (insn);
2891       if (pattern
2892           && GET_CODE (SET_DEST (pattern)) == REG
2893           && REGNO (reg) == REGNO (SET_DEST (pattern)))
2894         {
2895           rtx par, part;
2896           int i;
2897
2898           *set = pattern;
2899           par = PATTERN (insn);
2900           if (GET_CODE (par) == PARALLEL)
2901             for (i = XVECLEN (par, 0) - 1; i >= 0; i--)
2902               {
2903                 part = XVECEXP (par, 0, i);
2904                 if (GET_CODE (part) != SET && reg_mentioned_p (reg, part))
2905                   return 1;
2906               }
2907           return reg_mentioned_p (reg, SET_SRC (pattern));
2908         }
2909
2910       return 1;
2911     }
2912
2913   pattern = PATTERN (insn);
2914
2915   if (GET_CODE (pattern) == PARALLEL)
2916     {
2917       int i;
2918
2919       for (i = XVECLEN (pattern, 0) - 1; i >= 1; i--)
2920         if (reg_mentioned_p (reg, XVECEXP (pattern, 0, i)))
2921           return 1;
2922       pattern = XVECEXP (pattern, 0, 0);
2923     }
2924
2925   if (GET_CODE (pattern) == SET)
2926     {
2927       if (reg_mentioned_p (reg, SET_DEST (pattern)))
2928         {
2929           /* We don't use rtx_equal_p, because we don't care if the
2930              mode is different.  */
2931           if (GET_CODE (SET_DEST (pattern)) != REG
2932               || REGNO (reg) != REGNO (SET_DEST (pattern)))
2933             return 1;
2934
2935           *set = pattern;
2936         }
2937
2938       pattern = SET_SRC (pattern);
2939     }
2940
2941   if (GET_CODE (pattern) != CALL
2942       || GET_CODE (XEXP (pattern, 0)) != MEM
2943       || ! rtx_equal_p (reg, XEXP (XEXP (pattern, 0), 0)))
2944     return 1;
2945
2946   return 0;
2947 }
2948
2949 /* Given a X, a pattern of an insn or a part of it, return a mask of used
2950    general registers.  Bits 0..15 mean that the respective registers
2951    are used as inputs in the instruction.  Bits 16..31 mean that the
2952    registers 0..15, respectively, are used as outputs, or are clobbered.
2953    IS_DEST should be set to 16 if X is the destination of a SET, else to 0.  */
2954 int
2955 regs_used (x, is_dest)
2956      rtx x; int is_dest;
2957 {
2958   enum rtx_code code;
2959   const char *fmt;
2960   int i, used = 0;
2961
2962   if (! x)
2963     return used;
2964   code = GET_CODE (x);
2965   switch (code)
2966     {
2967     case REG:
2968       if (REGNO (x) < 16)
2969         return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
2970                 << (REGNO (x) + is_dest));
2971       return 0;
2972     case SUBREG:
2973       {
2974         rtx y = SUBREG_REG (x);
2975      
2976         if (GET_CODE (y) != REG)
2977           break;
2978         if (REGNO (y) < 16)
2979           return (((1 << HARD_REGNO_NREGS (0, GET_MODE (x))) - 1)
2980                   << (REGNO (y) +
2981                       subreg_regno_offset (REGNO (y),
2982                                            GET_MODE (y),
2983                                            SUBREG_BYTE (x),
2984                                            GET_MODE (x)) + is_dest));
2985         return 0;
2986       }
2987     case SET:
2988       return regs_used (SET_SRC (x), 0) | regs_used (SET_DEST (x), 16);
2989     case RETURN:
2990       /* If there was a return value, it must have been indicated with USE.  */
2991       return 0x00ffff00;
2992     case CLOBBER:
2993       is_dest = 1;
2994       break;
2995     case MEM:
2996       is_dest = 0;
2997       break;
2998     case CALL:
2999       used |= 0x00ff00f0;
3000       break;
3001     default:
3002       break;
3003     }
3004
3005   fmt = GET_RTX_FORMAT (code);
3006
3007   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3008     {
3009       if (fmt[i] == 'E')
3010         {
3011           register int j;
3012           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3013             used |= regs_used (XVECEXP (x, i, j), is_dest);
3014         }
3015       else if (fmt[i] == 'e')
3016         used |= regs_used (XEXP (x, i), is_dest);
3017     }
3018   return used;
3019 }
3020
3021 /* Create an instruction that prevents redirection of a conditional branch
3022    to the destination of the JUMP with address ADDR.
3023    If the branch needs to be implemented as an indirect jump, try to find
3024    a scratch register for it.
3025    If NEED_BLOCK is 0, don't do anything unless we need a scratch register.
3026    If any preceding insn that doesn't fit into a delay slot is good enough,
3027    pass 1.  Pass 2 if a definite blocking insn is needed.
3028    -1 is used internally to avoid deep recursion.
3029    If a blocking instruction is made or recognized, return it.  */
3030    
3031 static rtx
3032 gen_block_redirect (jump, addr, need_block)
3033      rtx jump;
3034      int addr, need_block;
3035 {
3036   int dead = 0;
3037   rtx prev = prev_nonnote_insn (jump);
3038   rtx dest;
3039
3040   /* First, check if we already have an instruction that satisfies our need.  */
3041   if (prev && GET_CODE (prev) == INSN && ! INSN_DELETED_P (prev))
3042     {
3043       if (INSN_CODE (prev) == CODE_FOR_indirect_jump_scratch)
3044         return prev;
3045       if (GET_CODE (PATTERN (prev)) == USE
3046           || GET_CODE (PATTERN (prev)) == CLOBBER
3047           || get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
3048         prev = jump;
3049       else if ((need_block &= ~1) < 0)
3050         return prev;
3051       else if (recog_memoized (prev) == CODE_FOR_block_branch_redirect)
3052         need_block = 0;
3053     }
3054   /* We can't use JUMP_LABEL here because it might be undefined
3055      when not optimizing.  */
3056   dest = XEXP (SET_SRC (PATTERN (jump)), 0);
3057   /* If the branch is out of range, try to find a scratch register for it.  */
3058   if (optimize
3059       && (INSN_ADDRESSES (INSN_UID (dest)) - addr + (unsigned) 4092
3060           > 4092 + 4098))
3061     {
3062       rtx scan;
3063       /* Don't look for the stack pointer as a scratch register,
3064          it would cause trouble if an interrupt occurred.  */
3065       unsigned try = 0x7fff, used;
3066       int jump_left = flag_expensive_optimizations + 1;
3067     
3068       /* It is likely that the most recent eligible instruction is wanted for
3069          the delay slot.  Therefore, find out which registers it uses, and
3070          try to avoid using them.  */
3071          
3072       for (scan = jump; (scan = PREV_INSN (scan)); )
3073         {
3074           enum rtx_code code;
3075
3076           if (INSN_DELETED_P (scan))
3077             continue;
3078           code = GET_CODE (scan);
3079           if (code == CODE_LABEL || code == JUMP_INSN)
3080             break;
3081           if (code == INSN
3082               && GET_CODE (PATTERN (scan)) != USE
3083               && GET_CODE (PATTERN (scan)) != CLOBBER
3084               && get_attr_in_delay_slot (scan) == IN_DELAY_SLOT_YES)
3085             {
3086               try &= ~regs_used (PATTERN (scan), 0);
3087               break;
3088             }
3089         }
3090       for (used = dead = 0, scan = JUMP_LABEL (jump);
3091            (scan = NEXT_INSN (scan)); )
3092         {
3093           enum rtx_code code;
3094
3095           if (INSN_DELETED_P (scan))
3096             continue;
3097           code = GET_CODE (scan);
3098           if (GET_RTX_CLASS (code) == 'i')
3099             {
3100               used |= regs_used (PATTERN (scan), 0);
3101               if (code == CALL_INSN)
3102                 used |= regs_used (CALL_INSN_FUNCTION_USAGE (scan), 0);
3103               dead |= (used >> 16) & ~used;
3104               if (dead & try)
3105                 {
3106                   dead &= try;
3107                   break;
3108                 }
3109               if (code == JUMP_INSN)
3110                 {
3111                   if (jump_left-- && simplejump_p (scan))
3112                     scan = JUMP_LABEL (scan);
3113                   else
3114                     break;
3115                 }
3116             }
3117         }
3118       /* Mask out the stack pointer again, in case it was
3119          the only 'free' register we have found.  */
3120       dead &= 0x7fff;
3121     }
3122   /* If the immediate destination is still in range, check for possible
3123      threading with a jump beyond the delay slot insn.
3124      Don't check if we are called recursively; the jump has been or will be
3125      checked in a different invocation then.  */
3126         
3127   else if (optimize && need_block >= 0)
3128     {
3129       rtx next = next_active_insn (next_active_insn (dest));
3130       if (next && GET_CODE (next) == JUMP_INSN
3131           && GET_CODE (PATTERN (next)) == SET
3132           && recog_memoized (next) == CODE_FOR_jump)
3133         {
3134           dest = JUMP_LABEL (next);
3135           if (dest
3136               && (INSN_ADDRESSES (INSN_UID (dest)) - addr + (unsigned) 4092
3137                   > 4092 + 4098))
3138             gen_block_redirect (next, INSN_ADDRESSES (INSN_UID (next)), -1);
3139         }
3140     }
3141
3142   if (dead)
3143     {
3144       rtx reg = gen_rtx_REG (SImode, exact_log2 (dead & -dead));
3145
3146       /* It would be nice if we could convert the jump into an indirect
3147          jump / far branch right now, and thus exposing all constituent
3148          instructions to further optimization.  However, reorg uses
3149          simplejump_p to determine if there is an unconditional jump where
3150          it should try to schedule instructions from the target of the
3151          branch; simplejump_p fails for indirect jumps even if they have
3152          a JUMP_LABEL.  */
3153       rtx insn = emit_insn_before (gen_indirect_jump_scratch
3154                                    (reg, GEN_INT (INSN_UID (JUMP_LABEL (jump))))
3155                                    , jump);
3156       INSN_CODE (insn) = CODE_FOR_indirect_jump_scratch;
3157       return insn;
3158     }
3159   else if (need_block)
3160     /* We can't use JUMP_LABEL here because it might be undefined
3161        when not optimizing.  */
3162     return emit_insn_before (gen_block_branch_redirect
3163                       (GEN_INT (INSN_UID (XEXP (SET_SRC (PATTERN (jump)), 0))))
3164                       , jump);
3165   return prev;
3166 }
3167
3168 #define CONDJUMP_MIN -252
3169 #define CONDJUMP_MAX 262
3170 struct far_branch
3171 {
3172   /* A label (to be placed) in front of the jump
3173      that jumps to our ultimate destination.  */
3174   rtx near_label;
3175   /* Where we are going to insert it if we cannot move the jump any farther,
3176      or the jump itself if we have picked up an existing jump.  */
3177   rtx insert_place;
3178   /* The ultimate destination.  */
3179   rtx far_label;
3180   struct far_branch *prev;
3181   /* If the branch has already been created, its address;
3182      else the address of its first prospective user.  */
3183   int address;
3184 };
3185
3186 static void gen_far_branch PARAMS ((struct far_branch *));
3187 enum mdep_reorg_phase_e mdep_reorg_phase;
3188 static void
3189 gen_far_branch (bp)
3190      struct far_branch *bp;
3191 {
3192   rtx insn = bp->insert_place;
3193   rtx jump;
3194   rtx label = gen_label_rtx ();
3195
3196   emit_label_after (label, insn);
3197   if (bp->far_label)
3198     {
3199       jump = emit_jump_insn_after (gen_jump (bp->far_label), insn);
3200       LABEL_NUSES (bp->far_label)++;
3201     }
3202   else
3203     jump = emit_jump_insn_after (gen_return (), insn);
3204   /* Emit a barrier so that reorg knows that any following instructions
3205      are not reachable via a fall-through path.
3206      But don't do this when not optimizing, since we wouldn't supress the
3207      alignment for the barrier then, and could end up with out-of-range
3208      pc-relative loads.  */
3209   if (optimize)
3210     emit_barrier_after (jump);
3211   emit_label_after (bp->near_label, insn);
3212   JUMP_LABEL (jump) = bp->far_label;
3213   if (! invert_jump (insn, label, 1))
3214     abort ();
3215   /* Prevent reorg from undoing our splits.  */
3216   gen_block_redirect (jump, bp->address += 2, 2);
3217 }
3218
3219 /* Fix up ADDR_DIFF_VECs.  */
3220 void
3221 fixup_addr_diff_vecs (first)
3222      rtx first;
3223 {
3224   rtx insn;
3225
3226   for (insn = first; insn; insn = NEXT_INSN (insn))
3227     {
3228       rtx vec_lab, pat, prev, prevpat, x, braf_label;
3229
3230       if (GET_CODE (insn) != JUMP_INSN
3231           || GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
3232         continue;
3233       pat = PATTERN (insn);
3234       vec_lab = XEXP (XEXP (pat, 0), 0);
3235
3236       /* Search the matching casesi_jump_2.  */
3237       for (prev = vec_lab; ; prev = PREV_INSN (prev))
3238         {
3239           if (GET_CODE (prev) != JUMP_INSN)
3240             continue;
3241           prevpat = PATTERN (prev);
3242           if (GET_CODE (prevpat) != PARALLEL || XVECLEN (prevpat, 0) != 2)
3243             continue;
3244           x = XVECEXP (prevpat, 0, 1);
3245           if (GET_CODE (x) != USE)
3246             continue;
3247           x = XEXP (x, 0);
3248           if (GET_CODE (x) == LABEL_REF && XEXP (x, 0) == vec_lab)
3249             break;
3250         }
3251
3252       /* Emit the reference label of the braf where it belongs, right after
3253          the casesi_jump_2 (i.e. braf).  */
3254       braf_label = XEXP (XEXP (SET_SRC (XVECEXP (prevpat, 0, 0)), 1), 0);
3255       emit_label_after (braf_label, prev);
3256
3257       /* Fix up the ADDR_DIF_VEC to be relative
3258          to the reference address of the braf.  */
3259       XEXP (XEXP (pat, 0), 0) = braf_label;
3260     }
3261 }
3262
3263 /* BARRIER_OR_LABEL is either a BARRIER or a CODE_LABEL immediately following
3264    a barrier.  Return the base 2 logarithm of the desired alignment.  */
3265 int
3266 barrier_align (barrier_or_label)
3267      rtx barrier_or_label;
3268 {
3269   rtx next = next_real_insn (barrier_or_label), pat, prev;
3270   int slot, credit, jump_to_next;
3271  
3272   if (! next)
3273     return 0;
3274
3275   pat = PATTERN (next);
3276
3277   if (GET_CODE (pat) == ADDR_DIFF_VEC)
3278     return 2;
3279
3280   if (GET_CODE (pat) == UNSPEC_VOLATILE && XINT (pat, 1) == UNSPECV_ALIGN)
3281     /* This is a barrier in front of a constant table.  */
3282     return 0;
3283
3284   prev = prev_real_insn (barrier_or_label);
3285   if (GET_CODE (PATTERN (prev)) == ADDR_DIFF_VEC)
3286     {
3287       pat = PATTERN (prev);
3288       /* If this is a very small table, we want to keep the alignment after
3289          the table to the minimum for proper code alignment.  */
3290       return ((TARGET_SMALLCODE
3291                || ((unsigned) XVECLEN (pat, 1) * GET_MODE_SIZE (GET_MODE (pat))
3292                    <= (unsigned)1 << (CACHE_LOG - 2)))
3293               ? 1 << TARGET_SHMEDIA : CACHE_LOG);
3294     }
3295
3296   if (TARGET_SMALLCODE)
3297     return 0;
3298
3299   if (! TARGET_SH2 || ! optimize)
3300     return CACHE_LOG;
3301
3302   /* When fixing up pcloads, a constant table might be inserted just before
3303      the basic block that ends with the barrier.  Thus, we can't trust the
3304      instruction lengths before that.  */
3305   if (mdep_reorg_phase > SH_FIXUP_PCLOAD)
3306     {
3307       /* Check if there is an immediately preceding branch to the insn beyond
3308          the barrier.  We must weight the cost of discarding useful information
3309          from the current cache line when executing this branch and there is
3310          an alignment, against that of fetching unneeded insn in front of the
3311          branch target when there is no alignment.  */
3312
3313       /* There are two delay_slot cases to consider.  One is the simple case 
3314          where the preceding branch is to the insn beyond the barrier (simple 
3315          delay slot filling), and the other is where the preceding branch has 
3316          a delay slot that is a duplicate of the insn after the barrier 
3317          (fill_eager_delay_slots) and the branch is to the insn after the insn 
3318          after the barrier.  */
3319
3320       /* PREV is presumed to be the JUMP_INSN for the barrier under
3321          investigation.  Skip to the insn before it.  */
3322       prev = prev_real_insn (prev);
3323
3324       for (slot = 2, credit = (1 << (CACHE_LOG - 2)) + 2;
3325            credit >= 0 && prev && GET_CODE (prev) == INSN;
3326            prev = prev_real_insn (prev))
3327         {
3328           jump_to_next = 0;
3329           if (GET_CODE (PATTERN (prev)) == USE
3330               || GET_CODE (PATTERN (prev)) == CLOBBER)
3331             continue;
3332           if (GET_CODE (PATTERN (prev)) == SEQUENCE)
3333             {
3334               prev = XVECEXP (PATTERN (prev), 0, 1);
3335               if (INSN_UID (prev) == INSN_UID (next)) 
3336                 {
3337                   /* Delay slot was filled with insn at jump target.  */
3338                   jump_to_next = 1;
3339                   continue;
3340                 }
3341             }
3342
3343           if (slot &&
3344               get_attr_in_delay_slot (prev) == IN_DELAY_SLOT_YES)
3345             slot = 0;
3346           credit -= get_attr_length (prev);
3347         }
3348       if (prev
3349           && GET_CODE (prev) == JUMP_INSN
3350           && JUMP_LABEL (prev))
3351         {
3352           rtx x;
3353           if (jump_to_next
3354               || next_real_insn (JUMP_LABEL (prev)) == next
3355               /* If relax_delay_slots() decides NEXT was redundant
3356                  with some previous instruction, it will have
3357                  redirected PREV's jump to the following insn.  */
3358               || JUMP_LABEL (prev) == next_nonnote_insn (next)
3359               /* There is no upper bound on redundant instructions
3360                  that might have been skipped, but we must not put an
3361                  alignment where none had been before.  */
3362               || (x = (NEXT_INSN (NEXT_INSN (PREV_INSN (prev)))),           
3363                   (INSN_P (x) 
3364                    && (INSN_CODE (x) == CODE_FOR_block_branch_redirect
3365                        || INSN_CODE (x) == CODE_FOR_indirect_jump_scratch))))
3366             {
3367               rtx pat = PATTERN (prev);
3368               if (GET_CODE (pat) == PARALLEL)
3369                 pat = XVECEXP (pat, 0, 0);
3370               if (credit - slot >= (GET_CODE (SET_SRC (pat)) == PC ? 2 : 0))
3371                 return 0;
3372             }
3373         }
3374     }
3375   
3376   return CACHE_LOG;
3377 }
3378
3379 /* If we are inside a phony loop, almost any kind of label can turn up as the
3380    first one in the loop.  Aligning a braf label causes incorrect switch
3381    destination addresses; we can detect braf labels because they are
3382    followed by a BARRIER.
3383    Applying loop alignment to small constant or switch tables is a waste
3384    of space, so we suppress this too.  */
3385 int
3386 sh_loop_align (label)
3387      rtx label;
3388 {
3389   rtx next = label;
3390
3391   do
3392     next = next_nonnote_insn (next);
3393   while (next && GET_CODE (next) == CODE_LABEL);
3394
3395   if (! next
3396       || ! INSN_P (next)
3397       || GET_CODE (PATTERN (next)) == ADDR_DIFF_VEC
3398       || recog_memoized (next) == CODE_FOR_consttable_2)
3399     return 0;
3400
3401   if (TARGET_SH5)
3402     return 3;
3403
3404   return 2;
3405 }
3406
3407 /* Exported to toplev.c.
3408
3409    Do a final pass over the function, just before delayed branch
3410    scheduling.  */
3411
3412 void
3413 machine_dependent_reorg (first)
3414      rtx first;
3415 {
3416   rtx insn, mova;
3417   int num_mova;
3418   rtx r0_rtx = gen_rtx_REG (Pmode, 0);
3419   rtx r0_inc_rtx = gen_rtx_POST_INC (Pmode, r0_rtx);
3420
3421   /* We must split call insns before introducing `mova's.  If we're
3422      optimizing, they'll have already been split.  Otherwise, make
3423      sure we don't split them too late.  */
3424   if (! optimize)
3425     split_all_insns_noflow ();
3426
3427   if (TARGET_SHMEDIA)
3428     return;
3429
3430   /* If relaxing, generate pseudo-ops to associate function calls with
3431      the symbols they call.  It does no harm to not generate these
3432      pseudo-ops.  However, when we can generate them, it enables to
3433      linker to potentially relax the jsr to a bsr, and eliminate the
3434      register load and, possibly, the constant pool entry.  */
3435
3436   mdep_reorg_phase = SH_INSERT_USES_LABELS;
3437   if (TARGET_RELAX)
3438     {
3439       /* Remove all REG_LABEL notes.  We want to use them for our own
3440          purposes.  This works because none of the remaining passes
3441          need to look at them.
3442
3443          ??? But it may break in the future.  We should use a machine
3444          dependent REG_NOTE, or some other approach entirely.  */
3445       for (insn = first; insn; insn = NEXT_INSN (insn))
3446         {
3447           if (INSN_P (insn))
3448             {
3449               rtx note;
3450
3451               while ((note = find_reg_note (insn, REG_LABEL, NULL_RTX)) != 0)
3452                 remove_note (insn, note);
3453             }
3454         }
3455
3456       for (insn = first; insn; insn = NEXT_INSN (insn))
3457         {
3458           rtx pattern, reg, link, set, scan, dies, label;
3459           int rescan = 0, foundinsn = 0;
3460
3461           if (GET_CODE (insn) == CALL_INSN)
3462             {
3463               pattern = PATTERN (insn);
3464
3465               if (GET_CODE (pattern) == PARALLEL)
3466                 pattern = XVECEXP (pattern, 0, 0);
3467               if (GET_CODE (pattern) == SET)
3468                 pattern = SET_SRC (pattern);
3469
3470               if (GET_CODE (pattern) != CALL
3471                   || GET_CODE (XEXP (pattern, 0)) != MEM)
3472                 continue;
3473
3474               reg = XEXP (XEXP (pattern, 0), 0);
3475             }
3476           else
3477             {
3478               reg = sfunc_uses_reg (insn);
3479               if (! reg)
3480                 continue;
3481             }
3482
3483           if (GET_CODE (reg) != REG)
3484             continue;
3485
3486           /* This is a function call via REG.  If the only uses of REG
3487              between the time that it is set and the time that it dies
3488              are in function calls, then we can associate all the
3489              function calls with the setting of REG.  */
3490
3491           for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
3492             {
3493               if (REG_NOTE_KIND (link) != 0)
3494                 continue;
3495               set = single_set (XEXP (link, 0));
3496               if (set && rtx_equal_p (reg, SET_DEST (set)))
3497                 {
3498                   link = XEXP (link, 0);
3499                   break;
3500                 }
3501             }
3502
3503           if (! link)
3504             {
3505               /* ??? Sometimes global register allocation will have
3506                  deleted the insn pointed to by LOG_LINKS.  Try
3507                  scanning backward to find where the register is set.  */
3508               for (scan = PREV_INSN (insn);
3509                    scan && GET_CODE (scan) != CODE_LABEL;
3510                    scan = PREV_INSN (scan))
3511                 {
3512                   if (! INSN_P (scan))
3513                     continue;
3514
3515                   if (! reg_mentioned_p (reg, scan))
3516                     continue;
3517
3518                   if (noncall_uses_reg (reg, scan, &set))
3519                     break;
3520
3521                   if (set)
3522                     {
3523                       link = scan;
3524                       break;
3525                     }
3526                 }
3527             }
3528
3529           if (! link)
3530             continue;
3531
3532           /* The register is set at LINK.  */
3533
3534           /* We can only optimize the function call if the register is
3535              being set to a symbol.  In theory, we could sometimes
3536              optimize calls to a constant location, but the assembler
3537              and linker do not support that at present.  */
3538           if (GET_CODE (SET_SRC (set)) != SYMBOL_REF
3539               && GET_CODE (SET_SRC (set)) != LABEL_REF)
3540             continue;
3541
3542           /* Scan forward from LINK to the place where REG dies, and
3543              make sure that the only insns which use REG are
3544              themselves function calls.  */
3545
3546           /* ??? This doesn't work for call targets that were allocated
3547              by reload, since there may not be a REG_DEAD note for the
3548              register.  */
3549
3550           dies = NULL_RTX;
3551           for (scan = NEXT_INSN (link); scan; scan = NEXT_INSN (scan))
3552             {
3553               rtx scanset;
3554
3555               /* Don't try to trace forward past a CODE_LABEL if we haven't
3556                  seen INSN yet.  Ordinarily, we will only find the setting insn
3557                  in LOG_LINKS if it is in the same basic block.  However,
3558                  cross-jumping can insert code labels in between the load and
3559                  the call, and can result in situations where a single call
3560                  insn may have two targets depending on where we came from.  */
3561
3562               if (GET_CODE (scan) == CODE_LABEL && ! foundinsn)
3563                 break;
3564
3565               if (! INSN_P (scan))
3566                 continue;
3567
3568               /* Don't try to trace forward past a JUMP.  To optimize
3569                  safely, we would have to check that all the
3570                  instructions at the jump destination did not use REG.  */
3571
3572               if (GET_CODE (scan) == JUMP_INSN)
3573                 break;
3574
3575               if (! reg_mentioned_p (reg, scan))
3576                 continue;
3577
3578               if (noncall_uses_reg (reg, scan, &scanset))
3579                 break;
3580
3581               if (scan == insn)
3582                 foundinsn = 1;
3583
3584               if (scan != insn
3585                   && (GET_CODE (scan) == CALL_INSN || sfunc_uses_reg (scan)))
3586                 {
3587                   /* There is a function call to this register other
3588                      than the one we are checking.  If we optimize
3589                      this call, we need to rescan again below.  */
3590                   rescan = 1;
3591                 }
3592
3593               /* ??? We shouldn't have to worry about SCANSET here.
3594                  We should just be able to check for a REG_DEAD note
3595                  on a function call.  However, the REG_DEAD notes are
3596                  apparently not dependable around libcalls; c-torture
3597                  execute/920501-2 is a test case.  If SCANSET is set,
3598                  then this insn sets the register, so it must have
3599                  died earlier.  Unfortunately, this will only handle
3600                  the cases in which the register is, in fact, set in a
3601                  later insn.  */
3602
3603               /* ??? We shouldn't have to use FOUNDINSN here.
3604                  However, the LOG_LINKS fields are apparently not
3605                  entirely reliable around libcalls;
3606                  newlib/libm/math/e_pow.c is a test case.  Sometimes
3607                  an insn will appear in LOG_LINKS even though it is
3608                  not the most recent insn which sets the register.  */
3609
3610               if (foundinsn
3611                   && (scanset
3612                       || find_reg_note (scan, REG_DEAD, reg)))
3613                 {
3614                   dies = scan;
3615                   break;
3616                 }
3617             }
3618
3619           if (! dies)
3620             {
3621               /* Either there was a branch, or some insn used REG
3622                  other than as a function call address.  */
3623               continue;
3624             }
3625
3626           /* Create a code label, and put it in a REG_LABEL note on
3627              the insn which sets the register, and on each call insn
3628              which uses the register.  In final_prescan_insn we look
3629              for the REG_LABEL notes, and output the appropriate label
3630              or pseudo-op.  */
3631
3632           label = gen_label_rtx ();
3633           REG_NOTES (link) = gen_rtx_INSN_LIST (REG_LABEL, label,
3634                                                 REG_NOTES (link));
3635           REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, label,
3636                                                 REG_NOTES (insn));
3637           if (rescan)
3638             {
3639               scan = link;
3640               do
3641                 {
3642                   rtx reg2;
3643
3644                   scan = NEXT_INSN (scan);
3645                   if (scan != insn
3646                       && ((GET_CODE (scan) == CALL_INSN
3647                            && reg_mentioned_p (reg, scan))
3648                           || ((reg2 = sfunc_uses_reg (scan))
3649                               && REGNO (reg2) == REGNO (reg))))
3650                     REG_NOTES (scan)
3651                       = gen_rtx_INSN_LIST (REG_LABEL, label, REG_NOTES (scan));
3652                 }
3653               while (scan != dies);
3654             }
3655         }
3656     }
3657
3658   if (TARGET_SH2)
3659     fixup_addr_diff_vecs (first);
3660
3661   if (optimize)
3662     {
3663       mdep_reorg_phase = SH_SHORTEN_BRANCHES0;
3664       shorten_branches (first);
3665     }
3666   /* Scan the function looking for move instructions which have to be
3667      changed to pc-relative loads and insert the literal tables.  */
3668
3669   mdep_reorg_phase = SH_FIXUP_PCLOAD;
3670   for (insn = first, num_mova = 0; insn; insn = NEXT_INSN (insn))
3671     {
3672       if (mova_p (insn))
3673         {
3674           if (! num_mova++)
3675             mova = insn;
3676         }
3677       else if (GET_CODE (insn) == JUMP_INSN
3678                && GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
3679                && num_mova)
3680         {
3681           rtx scan;
3682           int total;
3683
3684           num_mova--;
3685
3686           /* Some code might have been inserted between the mova and
3687              its ADDR_DIFF_VEC.  Check if the mova is still in range.  */
3688           for (scan = mova, total = 0; scan != insn; scan = NEXT_INSN (scan))
3689             total += get_attr_length (scan);
3690
3691           /* range of mova is 1020, add 4 because pc counts from address of
3692              second instruction after this one, subtract 2 in case pc is 2
3693              byte aligned.  Possible alignment needed for the ADDR_DIFF_VEC
3694              cancels out with alignment effects of the mova itself.  */
3695           if (total > 1022)
3696             {
3697               /* Change the mova into a load, and restart scanning
3698                  there.  broken_move will then return true for mova.  */
3699               SET_SRC (PATTERN (mova))
3700                 = XVECEXP (SET_SRC (PATTERN (mova)), 0, 0);
3701               INSN_CODE (mova) = -1;
3702               insn = mova;
3703             }
3704         }
3705       if (broken_move (insn))
3706         {
3707           rtx scan;
3708           /* Scan ahead looking for a barrier to stick the constant table
3709              behind.  */
3710           rtx barrier = find_barrier (num_mova, mova, insn);
3711           rtx last_float_move, last_float = 0, *last_float_addr;
3712           int may_need_align = 1;
3713
3714           if (num_mova && ! mova_p (mova))
3715             {
3716               /* find_barrier had to change the first mova into a
3717                  pcload; thus, we have to start with this new pcload.  */
3718               insn = mova;
3719               num_mova = 0;
3720             }
3721           /* Now find all the moves between the points and modify them.  */
3722           for (scan = insn; scan != barrier; scan = NEXT_INSN (scan))
3723             {
3724               if (GET_CODE (scan) == CODE_LABEL)
3725                 last_float = 0;
3726               if (broken_move (scan))
3727                 {
3728                   rtx *patp = &PATTERN (scan), pat = *patp;
3729                   rtx src, dst;
3730                   rtx lab;
3731                   rtx newsrc;
3732                   enum machine_mode mode;
3733
3734                   if (GET_CODE (pat) == PARALLEL)
3735                     patp = &XVECEXP (pat, 0, 0), pat = *patp;
3736                   src = SET_SRC (pat);
3737                   dst = SET_DEST (pat);
3738                   mode = GET_MODE (dst);
3739
3740                   if (mode == SImode && hi_const (src)
3741                       && REGNO (dst) != FPUL_REG)
3742                     {
3743                       int offset = 0;
3744
3745                       mode = HImode;
3746                       while (GET_CODE (dst) == SUBREG)
3747                         {
3748                           offset += subreg_regno_offset (REGNO (SUBREG_REG (dst)),
3749                                                          GET_MODE (SUBREG_REG (dst)),
3750                                                          SUBREG_BYTE (dst),
3751                                                          GET_MODE (dst));
3752                           dst = SUBREG_REG (dst);
3753                         }
3754                       dst = gen_rtx_REG (HImode, REGNO (dst) + offset);
3755                     }
3756
3757                   if (GET_CODE (dst) == REG && FP_ANY_REGISTER_P (REGNO (dst)))
3758                     {
3759                       /* This must be an insn that clobbers r0.  */
3760                       rtx clobber = XVECEXP (PATTERN (scan), 0,
3761                                              XVECLEN (PATTERN (scan), 0) - 1);
3762
3763                       if (GET_CODE (clobber) != CLOBBER
3764                           || ! rtx_equal_p (XEXP (clobber, 0), r0_rtx))
3765                         abort ();
3766
3767                       if (last_float
3768                           && reg_set_between_p (r0_rtx, last_float_move, scan))
3769                         last_float = 0;
3770                       if (TARGET_SHCOMPACT)
3771                         {
3772                           /* The first SFmode constant after a DFmode
3773                              constant may be pulled before a sequence
3774                              of DFmode constants, so the second SFmode
3775                              needs a label, just in case.  */
3776                           if (GET_MODE_SIZE (mode) == 4)
3777                             {
3778                               if (last_float && may_need_align)
3779                                 last_float = 0;
3780                               may_need_align = 0;
3781                             }
3782                           if (last_float
3783                               && (GET_MODE_SIZE (GET_MODE (last_float))
3784                                   != GET_MODE_SIZE (mode)))
3785                             {
3786                               last_float = 0;
3787                               if (GET_MODE_SIZE (mode) == 4)
3788                                 may_need_align = 1;
3789                             }
3790                         }
3791                       lab = add_constant (src, mode, last_float);
3792                       if (lab)
3793                         emit_insn_before (gen_mova (lab), scan);
3794                       else
3795                         {
3796                           /* There will be a REG_UNUSED note for r0 on
3797                              LAST_FLOAT_MOVE; we have to change it to REG_INC,
3798                              lest reorg:mark_target_live_regs will not
3799                              consider r0 to be used, and we end up with delay
3800                              slot insn in front of SCAN that clobbers r0.  */
3801                           rtx note
3802                             = find_regno_note (last_float_move, REG_UNUSED, 0);
3803
3804                           /* If we are not optimizing, then there may not be
3805                              a note.  */
3806                           if (note)
3807                             PUT_MODE (note, REG_INC);
3808
3809                           *last_float_addr = r0_inc_rtx;
3810                         }
3811                       last_float_move = scan;
3812                       last_float = src;
3813                       newsrc = gen_rtx (MEM, mode,
3814                                         (((TARGET_SH4 && ! TARGET_FMOVD)
3815                                           || REGNO (dst) == FPUL_REG)
3816                                          ? r0_inc_rtx
3817                                          : r0_rtx));
3818                       last_float_addr = &XEXP (newsrc, 0);
3819
3820                       /* Remove the clobber of r0.  */
3821                       XEXP (clobber, 0) = gen_rtx_SCRATCH (Pmode);
3822                       RTX_UNCHANGING_P (newsrc) = 1;
3823                     }
3824                   /* This is a mova needing a label.  Create it.  */
3825                   else if (GET_CODE (src) == UNSPEC
3826                            && XINT (src, 1) == UNSPEC_MOVA
3827                            && GET_CODE (XVECEXP (src, 0, 0)) == CONST)
3828                     {
3829                       lab = add_constant (XVECEXP (src, 0, 0), mode, 0);
3830                       newsrc = gen_rtx_LABEL_REF (VOIDmode, lab);
3831                       newsrc = gen_rtx_UNSPEC (SImode,
3832                                                gen_rtvec (1, newsrc),
3833                                                UNSPEC_MOVA);
3834                     }
3835                   else
3836                     {
3837                       lab = add_constant (src, mode, 0);
3838                       newsrc = gen_rtx_MEM (mode,
3839                                             gen_rtx_LABEL_REF (VOIDmode, lab));
3840                       RTX_UNCHANGING_P (newsrc) = 1;
3841                     }
3842                   *patp = gen_rtx_SET (VOIDmode, dst, newsrc);
3843                   INSN_CODE (scan) = -1;
3844                 }
3845             }
3846           dump_table (barrier);
3847           insn = barrier;
3848         }
3849     }
3850
3851   mdep_reorg_phase = SH_SHORTEN_BRANCHES1;
3852   INSN_ADDRESSES_FREE ();
3853   split_branches (first);
3854
3855   /* The INSN_REFERENCES_ARE_DELAYED in sh.h is problematic because it
3856      also has an effect on the register that holds the addres of the sfunc.
3857      Insert an extra dummy insn in front of each sfunc that pretends to
3858      use this register.  */
3859   if (flag_delayed_branch)
3860     {
3861       for (insn = first; insn; insn = NEXT_INSN (insn))
3862         {
3863           rtx reg = sfunc_uses_reg (insn);
3864
3865           if (! reg)
3866             continue;
3867           emit_insn_before (gen_use_sfunc_addr (reg), insn);
3868         }
3869     }
3870 #if 0
3871   /* fpscr is not actually a user variable, but we pretend it is for the
3872      sake of the previous optimization passes, since we want it handled like
3873      one.  However, we don't have any debugging information for it, so turn
3874      it into a non-user variable now.  */
3875   if (TARGET_SH4)
3876     REG_USERVAR_P (get_fpscr_rtx ()) = 0;
3877 #endif
3878   mdep_reorg_phase = SH_AFTER_MDEP_REORG;
3879 }
3880
3881 int
3882 get_dest_uid (label, max_uid)
3883      rtx label;
3884      int max_uid;
3885 {
3886   rtx dest = next_real_insn (label);
3887   int dest_uid;
3888   if (! dest)
3889     /* This can happen for an undefined label.  */
3890     return 0;
3891   dest_uid = INSN_UID (dest);
3892   /* If this is a newly created branch redirection blocking instruction,
3893      we cannot index the branch_uid or insn_addresses arrays with its
3894      uid.  But then, we won't need to, because the actual destination is
3895      the following branch.  */
3896   while (dest_uid >= max_uid)
3897     {
3898       dest = NEXT_INSN (dest);
3899       dest_uid = INSN_UID (dest);
3900     }
3901   if (GET_CODE (dest) == JUMP_INSN && GET_CODE (PATTERN (dest)) == RETURN)
3902     return 0;
3903   return dest_uid;
3904 }
3905
3906 /* Split condbranches that are out of range.  Also add clobbers for
3907    scratch registers that are needed in far jumps.
3908    We do this before delay slot scheduling, so that it can take our
3909    newly created instructions into account.  It also allows us to
3910    find branches with common targets more easily.  */
3911
3912 static void
3913 split_branches (first)
3914      rtx first;
3915 {
3916   rtx insn;
3917   struct far_branch **uid_branch, *far_branch_list = 0;
3918   int max_uid = get_max_uid ();
3919
3920   /* Find out which branches are out of range.  */
3921   shorten_branches (first);
3922
3923   uid_branch = (struct far_branch **) alloca (max_uid * sizeof *uid_branch);
3924   memset ((char *) uid_branch, 0, max_uid * sizeof *uid_branch);
3925
3926   for (insn = first; insn; insn = NEXT_INSN (insn))
3927     if (! INSN_P (insn))
3928       continue;
3929     else if (INSN_DELETED_P (insn))
3930       {
3931         /* Shorten_branches would split this instruction again,
3932            so transform it into a note.  */
3933         PUT_CODE (insn, NOTE);
3934         NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
3935         NOTE_SOURCE_FILE (insn) = 0;
3936       }
3937     else if (GET_CODE (insn) == JUMP_INSN
3938              /* Don't mess with ADDR_DIFF_VEC */
3939              && (GET_CODE (PATTERN (insn)) == SET
3940                  || GET_CODE (PATTERN (insn)) == RETURN))
3941       {
3942         enum attr_type type = get_attr_type (insn);
3943         if (type == TYPE_CBRANCH)
3944           {
3945             rtx next, beyond;
3946     
3947             if (get_attr_length (insn) > 4)
3948               {
3949                 rtx src = SET_SRC (PATTERN (insn));
3950                 rtx olabel = XEXP (XEXP (src, 1), 0);
3951                 int addr = INSN_ADDRESSES (INSN_UID (insn));
3952                 rtx label = 0;
3953                 int dest_uid = get_dest_uid (olabel, max_uid);
3954                 struct far_branch *bp = uid_branch[dest_uid];
3955     
3956                 /* redirect_jump needs a valid JUMP_LABEL, and it might delete
3957                    the label if the LABEL_NUSES count drops to zero.  There is
3958                    always a jump_optimize pass that sets these values, but it
3959                    proceeds to delete unreferenced code, and then if not
3960                    optimizing, to un-delete the deleted instructions, thus
3961                    leaving labels with too low uses counts.  */
3962                 if (! optimize)
3963                   {
3964                     JUMP_LABEL (insn) = olabel;
3965                     LABEL_NUSES (olabel)++;
3966                   }
3967                 if (! bp)
3968                   {
3969                     bp = (struct far_branch *) alloca (sizeof *bp);
3970                     uid_branch[dest_uid] = bp;
3971                     bp->prev = far_branch_list;
3972                     far_branch_list = bp;
3973                     bp->far_label
3974                       = XEXP (XEXP (SET_SRC (PATTERN (insn)), 1), 0);
3975                     LABEL_NUSES (bp->far_label)++;
3976                   }
3977                 else
3978                   {
3979                     label = bp->near_label;
3980                     if (! label && bp->address - addr >= CONDJUMP_MIN)
3981                       {
3982                         rtx block = bp->insert_place;
3983
3984                         if (GET_CODE (PATTERN (block)) == RETURN)
3985                           block = PREV_INSN (block);
3986                         else
3987                           block = gen_block_redirect (block,
3988                                                       bp->address, 2);
3989                         label = emit_label_after (gen_label_rtx (),
3990                                                   PREV_INSN (block));
3991                         bp->near_label = label;
3992                       }
3993                     else if (label && ! NEXT_INSN (label))
3994                       {
3995                         if (addr + 2 - bp->address <= CONDJUMP_MAX)
3996                           bp->insert_place = insn;
3997                         else
3998                           gen_far_branch (bp);
3999                       }
4000                   }
4001                 if (! label
4002                     || (NEXT_INSN (label) && bp->address - addr < CONDJUMP_MIN))
4003                   {
4004                     bp->near_label = label = gen_label_rtx ();
4005                     bp->insert_place = insn;
4006                     bp->address = addr;
4007                   }
4008                 if (! redirect_jump (insn, label, 1))
4009                   abort ();
4010               }
4011             else
4012               {
4013                 /* get_attr_length (insn) == 2 */
4014                 /* Check if we have a pattern where reorg wants to redirect
4015                    the branch to a label from an unconditional branch that
4016                    is too far away.  */
4017                 /* We can't use JUMP_LABEL here because it might be undefined
4018                    when not optimizing.  */
4019                 /* A syntax error might cause beyond to be NULL_RTX.  */
4020                 beyond
4021                   = next_active_insn (XEXP (XEXP (SET_SRC (PATTERN (insn)), 1),
4022                                             0));
4023         
4024                 if (beyond
4025                     && (GET_CODE (beyond) == JUMP_INSN
4026                         || ((beyond = next_active_insn (beyond))
4027                             && GET_CODE (beyond) == JUMP_INSN))
4028                     && GET_CODE (PATTERN (beyond)) == SET
4029                     && recog_memoized (beyond) == CODE_FOR_jump
4030                     && ((INSN_ADDRESSES
4031                          (INSN_UID (XEXP (SET_SRC (PATTERN (beyond)), 0)))
4032                          - INSN_ADDRESSES (INSN_UID (insn)) + (unsigned) 252)
4033                         > 252 + 258 + 2))
4034                   gen_block_redirect (beyond,
4035                                       INSN_ADDRESSES (INSN_UID (beyond)), 1);
4036               }
4037     
4038             next = next_active_insn (insn);
4039
4040             if ((GET_CODE (next) == JUMP_INSN
4041                  || GET_CODE (next = next_active_insn (next)) == JUMP_INSN)
4042                 && GET_CODE (PATTERN (next)) == SET
4043                 && recog_memoized (next) == CODE_FOR_jump
4044                 && ((INSN_ADDRESSES
4045                      (INSN_UID (XEXP (SET_SRC (PATTERN (next)), 0)))
4046                      - INSN_ADDRESSES (INSN_UID (insn)) + (unsigned) 252)
4047                     > 252 + 258 + 2))
4048               gen_block_redirect (next, INSN_ADDRESSES (INSN_UID (next)), 1);
4049           }
4050         else if (type == TYPE_JUMP || type == TYPE_RETURN)
4051           {
4052             int addr = INSN_ADDRESSES (INSN_UID (insn));
4053             rtx far_label = 0;
4054             int dest_uid = 0;
4055             struct far_branch *bp;
4056
4057             if (type == TYPE_JUMP)
4058               {
4059                 far_label = XEXP (SET_SRC (PATTERN (insn)), 0);
4060                 dest_uid = get_dest_uid (far_label, max_uid);
4061                 if (! dest_uid)
4062                   {
4063                     /* Parse errors can lead to labels outside
4064                       the insn stream.  */
4065                     if (! NEXT_INSN (far_label))
4066                       continue;
4067
4068                     if (! optimize)
4069                       {
4070                         JUMP_LABEL (insn) = far_label;
4071                         LABEL_NUSES (far_label)++;
4072                       }
4073                     redirect_jump (insn, NULL_RTX, 1);
4074                     far_label = 0;
4075                   }
4076               }
4077             bp = uid_branch[dest_uid];
4078             if (! bp)
4079               {
4080                 bp = (struct far_branch *) alloca (sizeof *bp);
4081                 uid_branch[dest_uid] = bp;
4082                 bp->prev = far_branch_list;
4083                 far_branch_list = bp;
4084                 bp->near_label = 0;
4085                 bp->far_label = far_label;
4086                 if (far_label)
4087                   LABEL_NUSES (far_label)++;
4088               }
4089             else if (bp->near_label && ! NEXT_INSN (bp->near_label))
4090               if (addr - bp->address <= CONDJUMP_MAX)
4091                 emit_label_after (bp->near_label, PREV_INSN (insn));
4092               else
4093                 {
4094                   gen_far_branch (bp);
4095                   bp->near_label = 0;
4096                 }
4097             else
4098               bp->near_label = 0;
4099             bp->address = addr;
4100             bp->insert_place = insn;
4101             if (! far_label)
4102               emit_insn_before (gen_block_branch_redirect (const0_rtx), insn);
4103             else
4104               gen_block_redirect (insn, addr, bp->near_label ? 2 : 0);
4105           }
4106       }
4107   /* Generate all pending far branches,
4108      and free our references to the far labels.  */
4109   while (far_branch_list)
4110     {
4111       if (far_branch_list->near_label
4112           && ! NEXT_INSN (far_branch_list->near_label))
4113         gen_far_branch (far_branch_list);
4114       if (optimize
4115           && far_branch_list->far_label
4116           && ! --LABEL_NUSES (far_branch_list->far_label))
4117         delete_insn (far_branch_list->far_label);
4118       far_branch_list = far_branch_list->prev;
4119     }
4120
4121   /* Instruction length information is no longer valid due to the new
4122      instructions that have been generated.  */
4123   init_insn_lengths ();
4124 }
4125
4126 /* Dump out instruction addresses, which is useful for debugging the
4127    constant pool table stuff.
4128
4129    If relaxing, output the label and pseudo-ops used to link together
4130    calls and the instruction which set the registers.  */
4131
4132 /* ??? This is unnecessary, and probably should be deleted.  This makes
4133    the insn_addresses declaration above unnecessary.  */
4134
4135 /* ??? The addresses printed by this routine for insns are nonsense for
4136    insns which are inside of a sequence where none of the inner insns have
4137    variable length.  This is because the second pass of shorten_branches
4138    does not bother to update them.  */
4139
4140 void
4141 final_prescan_insn (insn, opvec, noperands)
4142      rtx insn;
4143      rtx *opvec ATTRIBUTE_UNUSED;
4144      int noperands ATTRIBUTE_UNUSED;
4145 {
4146   if (TARGET_DUMPISIZE)
4147     fprintf (asm_out_file, "\n! at %04x\n", INSN_ADDRESSES (INSN_UID (insn)));
4148
4149   if (TARGET_RELAX)
4150     {
4151       rtx note;
4152
4153       note = find_reg_note (insn, REG_LABEL, NULL_RTX);
4154       if (note)
4155         {
4156           rtx pattern;
4157
4158           pattern = PATTERN (insn);
4159           if (GET_CODE (pattern) == PARALLEL)
4160             pattern = XVECEXP (pattern, 0, 0);
4161           if (GET_CODE (pattern) == CALL
4162               || (GET_CODE (pattern) == SET
4163                   && (GET_CODE (SET_SRC (pattern)) == CALL
4164                       || get_attr_type (insn) == TYPE_SFUNC)))
4165             asm_fprintf (asm_out_file, "\t.uses %LL%d\n",
4166                          CODE_LABEL_NUMBER (XEXP (note, 0)));
4167           else if (GET_CODE (pattern) == SET)
4168             ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
4169                                        CODE_LABEL_NUMBER (XEXP (note, 0)));
4170           else
4171             abort ();
4172         }
4173     }
4174 }
4175
4176 /* Dump out any constants accumulated in the final pass.  These will
4177    only be labels.  */
4178
4179 const char *
4180 output_jump_label_table ()
4181 {
4182   int i;
4183
4184   if (pool_size)
4185     {
4186       fprintf (asm_out_file, "\t.align 2\n");
4187       for (i = 0; i < pool_size; i++)
4188         {
4189           pool_node *p = &pool_vector[i];
4190
4191           ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, "L",
4192                                      CODE_LABEL_NUMBER (p->label));
4193           output_asm_insn (".long       %O0", &p->value);
4194         }
4195       pool_size = 0;
4196     }
4197
4198   return "";
4199 }
4200 \f
4201 /* A full frame looks like:
4202
4203    arg-5
4204    arg-4
4205    [ if current_function_anonymous_args
4206    arg-3
4207    arg-2
4208    arg-1
4209    arg-0 ]
4210    saved-fp
4211    saved-r10
4212    saved-r11
4213    saved-r12
4214    saved-pr
4215    local-n
4216    ..
4217    local-1
4218    local-0        <- fp points here.  */
4219
4220 /* Number of bytes pushed for anonymous args, used to pass information
4221    between expand_prologue and expand_epilogue.  */
4222
4223 static int extra_push;
4224
4225 /* Adjust the stack by SIZE bytes.  REG holds the rtl of the register
4226   to be adjusted, and TEMP, if nonnegative, holds the register number
4227   of a general register that we may clobber.  */
4228
4229 static void
4230 output_stack_adjust (size, reg, temp, emit_fn)
4231      int size;
4232      rtx reg;
4233      int temp;
4234      rtx (*emit_fn) PARAMS ((rtx));
4235 {
4236   if (size)
4237     {
4238       HOST_WIDE_INT align = STACK_BOUNDARY / BITS_PER_UNIT;
4239
4240       if (size % align)
4241         abort ();
4242
4243       if (CONST_OK_FOR_ADD (size))
4244         emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size)));
4245       /* Try to do it with two partial adjustments; however, we must make
4246          sure that the stack is properly aligned at all times, in case
4247          an interrupt occurs between the two partial adjustments.  */
4248       else if (CONST_OK_FOR_ADD (size / 2 & -align)
4249                && CONST_OK_FOR_ADD (size - (size / 2 & -align)))
4250         {
4251           emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size / 2 & -align)));
4252           emit_fn (GEN_ADD3 (reg, reg, GEN_INT (size - (size / 2 & -align))));
4253         }
4254       else
4255         {
4256           rtx const_reg;
4257           rtx insn;
4258
4259           /* If TEMP is invalid, we could temporarily save a general
4260              register to MACL.  However, there is currently no need
4261              to handle this case, so just abort when we see it.  */
4262           if (temp < 0)
4263             abort ();
4264           const_reg = gen_rtx_REG (GET_MODE (reg), temp);
4265
4266           /* If SIZE is negative, subtract the positive value.
4267              This sometimes allows a constant pool entry to be shared
4268              between prologue and epilogue code.  */
4269           if (size < 0)
4270             {
4271               emit_insn (GEN_MOV (const_reg, GEN_INT (-size)));
4272               insn = emit_fn (GEN_SUB3 (reg, reg, const_reg));
4273             }
4274           else
4275             {
4276               emit_insn (GEN_MOV (const_reg, GEN_INT (size)));
4277               insn = emit_fn (GEN_ADD3 (reg, reg, const_reg));
4278             }
4279           if (emit_fn == frame_insn)
4280             REG_NOTES (insn)
4281               = (gen_rtx_EXPR_LIST
4282                  (REG_FRAME_RELATED_EXPR,
4283                   gen_rtx_SET (VOIDmode, reg,
4284                                gen_rtx_PLUS (SImode, reg, GEN_INT (size))),
4285                   REG_NOTES (insn)));
4286         }
4287     }
4288 }
4289
4290 static rtx
4291 frame_insn (x)
4292      rtx x;
4293 {
4294   x = emit_insn (x);
4295   RTX_FRAME_RELATED_P (x) = 1;
4296   return x;
4297 }
4298
4299 /* Output RTL to push register RN onto the stack.  */
4300
4301 static rtx
4302 push (rn)
4303      int rn;
4304 {
4305   rtx x;
4306   if (rn == FPUL_REG)
4307     x = gen_push_fpul ();
4308   else if (TARGET_SH4 && TARGET_FMOVD && ! TARGET_FPU_SINGLE
4309            && FP_OR_XD_REGISTER_P (rn))
4310     {
4311       if (FP_REGISTER_P (rn) && (rn - FIRST_FP_REG) & 1)
4312         return NULL_RTX;
4313       x = gen_push_4 (gen_rtx_REG (DFmode, rn));
4314     }
4315   else if (TARGET_SH3E && FP_REGISTER_P (rn))
4316     x = gen_push_e (gen_rtx_REG (SFmode, rn));
4317   else
4318     x = gen_push (gen_rtx_REG (SImode, rn));
4319
4320   x = frame_insn (x);
4321   REG_NOTES (x)
4322     = gen_rtx_EXPR_LIST (REG_INC,
4323                          gen_rtx_REG (SImode, STACK_POINTER_REGNUM), 0);
4324   return x;
4325 }
4326
4327 /* Output RTL to pop register RN from the stack.  */
4328
4329 static void
4330 pop (rn)
4331      int rn;
4332 {
4333   rtx x;
4334   if (rn == FPUL_REG)
4335     x = gen_pop_fpul ();
4336   else if (TARGET_SH4 && TARGET_FMOVD && ! TARGET_FPU_SINGLE
4337            && FP_OR_XD_REGISTER_P (rn))
4338     {
4339       if (FP_REGISTER_P (rn) && (rn - FIRST_FP_REG) & 1)
4340         return;
4341       x = gen_pop_4 (gen_rtx_REG (DFmode, rn));
4342     }
4343   else if (TARGET_SH3E && FP_REGISTER_P (rn))
4344     x = gen_pop_e (gen_rtx_REG (SFmode, rn));
4345   else
4346     x = gen_pop (gen_rtx_REG (SImode, rn));
4347     
4348   x = emit_insn (x);
4349   REG_NOTES (x)
4350     = gen_rtx_EXPR_LIST (REG_INC,
4351                          gen_rtx_REG (SImode, STACK_POINTER_REGNUM), 0);
4352 }
4353
4354 /* Generate code to push the regs specified in the mask.  */
4355
4356 static void
4357 push_regs (mask)
4358      HOST_WIDE_INT *mask;
4359 {
4360   int i;
4361
4362   /* Push PR last; this gives better latencies after the prologue, and
4363      candidates for the return delay slot when there are no general
4364      registers pushed.  */
4365   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4366     if (i != PR_REG && mask[i / 32] & (1 << (i % 32)))
4367       push (i);
4368   if (mask[PR_REG / 32] & (1 << (PR_REG % 32)))
4369     push (PR_REG);
4370 }
4371
4372 /* Work out the registers which need to be saved, both as a mask and a
4373    count of saved words.
4374
4375    If doing a pragma interrupt function, then push all regs used by the
4376    function, and if we call another function (we can tell by looking at PR),
4377    make sure that all the regs it clobbers are safe too.  */
4378
4379 static void
4380 calc_live_regs (count_ptr, live_regs_mask)
4381      int *count_ptr;
4382      HOST_WIDE_INT *live_regs_mask;
4383 {
4384   int reg;
4385   int count;
4386   int interrupt_handler;
4387   int pr_live;
4388
4389   interrupt_handler = sh_cfun_interrupt_handler_p ();
4390
4391   for (count = 0; 32 * count < FIRST_PSEUDO_REGISTER; count++)
4392     live_regs_mask[count] = 0;
4393   /* If we can save a lot of saves by switching to double mode, do that.  */
4394   if (TARGET_SH4 && TARGET_FMOVD && TARGET_FPU_SINGLE)
4395     for (count = 0, reg = FIRST_FP_REG; reg <= LAST_FP_REG; reg += 2)
4396       if (regs_ever_live[reg] && regs_ever_live[reg+1]
4397           && (! call_used_regs[reg] || (interrupt_handler && ! pragma_trapa))
4398           && ++count > 2)
4399         {
4400           target_flags &= ~FPU_SINGLE_BIT;
4401           break;
4402         }
4403   /* PR_MEDIA_REG is a general purpose register, thus global_alloc already
4404      knows how to use it.  That means the pseudo originally allocated for
4405      the initial value can become the PR_MEDIA_REG hard register, as seen for
4406      execute/20010122-1.c:test9.  */
4407   if (TARGET_SHMEDIA)
4408     pr_live = regs_ever_live[PR_MEDIA_REG];
4409   else
4410     {
4411       rtx pr_initial = has_hard_reg_initial_val (Pmode, PR_REG);
4412       pr_live = (pr_initial
4413                  ? REGNO (pr_initial) != (PR_REG) : regs_ever_live[PR_REG]);
4414     }
4415   /* Force PR to be live if the prologue has to call the SHmedia
4416      argument decoder or register saver.  */
4417   if (TARGET_SHCOMPACT
4418       && ((current_function_args_info.call_cookie
4419            & ~ CALL_COOKIE_RET_TRAMP (1))
4420           || current_function_has_nonlocal_label))
4421     pr_live = 1;
4422   for (count = 0, reg = FIRST_PSEUDO_REGISTER - 1; reg >= 0; reg--)
4423     {
4424       if (reg == (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG)
4425           ? pr_live
4426           : (interrupt_handler && ! pragma_trapa)
4427           ? (/* Need to save all the regs ever live.  */
4428              (regs_ever_live[reg]
4429               || (call_used_regs[reg]
4430                   && (! fixed_regs[reg] || reg == MACH_REG || reg == MACL_REG)
4431                   && pr_live))
4432              && reg != STACK_POINTER_REGNUM && reg != ARG_POINTER_REGNUM
4433              && reg != RETURN_ADDRESS_POINTER_REGNUM
4434              && reg != T_REG && reg != GBR_REG && reg != FPSCR_REG)
4435           : (/* Only push those regs which are used and need to be saved.  */
4436              regs_ever_live[reg] && ! call_used_regs[reg]))
4437         {
4438           live_regs_mask[reg / 32] |= 1 << (reg % 32);
4439           count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg));
4440
4441           if ((TARGET_SH4 || TARGET_SH5) && TARGET_FMOVD
4442               && GET_MODE_CLASS (REGISTER_NATURAL_MODE (reg)) == MODE_FLOAT)
4443             {
4444               if (FP_REGISTER_P (reg))
4445                 {
4446                   if (! TARGET_FPU_SINGLE && ! regs_ever_live[reg ^ 1])
4447                     {
4448                       live_regs_mask[(reg ^ 1) / 32] |= 1 << ((reg ^ 1) % 32);
4449                       count += GET_MODE_SIZE (REGISTER_NATURAL_MODE (reg ^ 1));
4450                     }
4451                 }
4452               else if (XD_REGISTER_P (reg))
4453                 {
4454                   /* Must switch to double mode to access these registers.  */
4455                   target_flags &= ~FPU_SINGLE_BIT;
4456                 }
4457             }
4458         }
4459     }
4460
4461   *count_ptr = count;
4462 }
4463
4464 /* Code to generate prologue and epilogue sequences */
4465
4466 /* PUSHED is the number of bytes that are bing pushed on the
4467    stack for register saves.  Return the frame size, padded
4468    appropriately so that the stack stays properly aligned.  */
4469 static HOST_WIDE_INT
4470 rounded_frame_size (pushed)
4471      int pushed;
4472 {
4473   HOST_WIDE_INT size = get_frame_size ();
4474   HOST_WIDE_INT align = STACK_BOUNDARY / BITS_PER_UNIT;
4475
4476   return ((size + pushed + align - 1) & -align) - pushed;
4477 }
4478
4479 /* Choose a call-clobbered target-branch register that remains
4480    unchanged along the whole function.  We set it up as the return
4481    value in the prologue.  */
4482 int
4483 sh_media_register_for_return ()
4484 {
4485   int regno;
4486   int tr0_used;
4487
4488   if (! current_function_is_leaf)
4489     return -1;
4490
4491   tr0_used = flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM];
4492
4493   for (regno = FIRST_TARGET_REG + tr0_used; regno <= LAST_TARGET_REG; regno++)
4494     if (call_used_regs[regno] && ! regs_ever_live[regno])
4495       return regno;
4496
4497   return -1;
4498 }
4499
4500 void
4501 sh_expand_prologue ()
4502 {
4503   HOST_WIDE_INT live_regs_mask[(FIRST_PSEUDO_REGISTER + 31) / 32];
4504   int d, i;
4505   int d_rounding = 0;
4506   int save_flags = target_flags;
4507
4508   current_function_interrupt = sh_cfun_interrupt_handler_p ();
4509
4510   /* We have pretend args if we had an object sent partially in registers
4511      and partially on the stack, e.g. a large structure.  */
4512   output_stack_adjust (-current_function_pretend_args_size
4513                        - current_function_args_info.stack_regs * 8,
4514                        stack_pointer_rtx, TARGET_SH5 ? 0 : 1, frame_insn);
4515
4516   extra_push = 0;
4517
4518   if (TARGET_SHCOMPACT && flag_pic && current_function_args_info.call_cookie)
4519     /* We're going to use the PIC register to load the address of the
4520        incoming-argument decoder and/or of the return trampoline from
4521        the GOT, so make sure the PIC register is preserved and
4522        initialized.  */
4523     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
4524
4525   if (TARGET_SHCOMPACT
4526       && (current_function_args_info.call_cookie & ~ CALL_COOKIE_RET_TRAMP(1)))
4527     {
4528       int reg;
4529
4530       /* First, make all registers with incoming arguments that will
4531          be pushed onto the stack live, so that register renaming
4532          doesn't overwrite them.  */
4533       for (reg = 0; reg < NPARM_REGS (SImode); reg++)
4534         if (CALL_COOKIE_STACKSEQ_GET (current_function_args_info.call_cookie)
4535             >= NPARM_REGS (SImode) - reg)
4536           for (; reg < NPARM_REGS (SImode); reg++)
4537             emit_insn (gen_shcompact_preserve_incoming_args
4538                        (gen_rtx_REG (SImode, FIRST_PARM_REG + reg)));
4539         else if (CALL_COOKIE_INT_REG_GET
4540                  (current_function_args_info.call_cookie, reg) == 1)
4541           emit_insn (gen_shcompact_preserve_incoming_args
4542                      (gen_rtx_REG (SImode, FIRST_PARM_REG + reg)));
4543
4544       emit_move_insn (gen_rtx_REG (Pmode, MACL_REG),
4545                       stack_pointer_rtx);
4546       emit_move_insn (gen_rtx_REG (SImode, R0_REG),
4547                       GEN_INT (current_function_args_info.call_cookie));
4548       emit_move_insn (gen_rtx_REG (SImode, MACH_REG),
4549                       gen_rtx_REG (SImode, R0_REG));
4550     }
4551   else if (TARGET_SHMEDIA)
4552     {
4553       int tr = sh_media_register_for_return ();
4554
4555       if (tr >= 0)
4556         {
4557           rtx insn = emit_move_insn (gen_rtx_REG (DImode, tr),
4558                                      gen_rtx_REG (DImode, PR_MEDIA_REG));
4559
4560           /* If this function only exits with sibcalls, this copy
4561              will be flagged as dead.  */
4562           REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
4563                                                 const0_rtx,
4564                                                 REG_NOTES (insn));
4565         }
4566     }
4567
4568   /* Emit the code for SETUP_VARARGS.  */
4569   if (current_function_stdarg)
4570     {
4571       /* This is not used by the SH3E calling convention  */
4572       if (TARGET_SH1 && ! TARGET_SH3E && ! TARGET_SH5 && ! TARGET_HITACHI)
4573         {
4574           /* Push arg regs as if they'd been provided by caller in stack.  */
4575           for (i = 0; i < NPARM_REGS(SImode); i++)
4576             {
4577               int rn = NPARM_REGS(SImode) + FIRST_PARM_REG - i - 1;
4578               rtx insn;
4579
4580               if (i >= (NPARM_REGS(SImode) 
4581                         - current_function_args_info.arg_count[(int) SH_ARG_INT]
4582                         ))
4583                 break;
4584               insn = push (rn);
4585               RTX_FRAME_RELATED_P (insn) = 0;
4586               extra_push += 4;
4587             }
4588         }
4589     }
4590
4591   /* If we're supposed to switch stacks at function entry, do so now.  */
4592   if (sp_switch)
4593     emit_insn (gen_sp_switch_1 ());
4594
4595   calc_live_regs (&d, live_regs_mask);
4596   /* ??? Maybe we could save some switching if we can move a mode switch
4597      that already happens to be at the function start into the prologue.  */
4598   if (target_flags != save_flags)
4599     emit_insn (gen_toggle_sz ());
4600     
4601   if (TARGET_SH5)
4602     {
4603       int i;
4604       int offset;
4605       int align;
4606       rtx r0 = gen_rtx_REG (Pmode, R0_REG);
4607       int offset_in_r0 = -1;
4608       int sp_in_r0 = 0;
4609
4610       if (d % (STACK_BOUNDARY / BITS_PER_UNIT))
4611         d_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
4612                       - d % (STACK_BOUNDARY / BITS_PER_UNIT));
4613
4614       offset = d + d_rounding;
4615       output_stack_adjust (-offset, stack_pointer_rtx, 1, frame_insn);
4616
4617       /* We loop twice: first, we save 8-byte aligned registers in the
4618          higher addresses, that are known to be aligned.  Then, we
4619          proceed to saving 32-bit registers that don't need 8-byte
4620          alignment.  */
4621       for (align = 1; align >= 0; align--)
4622         for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
4623           if (live_regs_mask[i/32] & (1 << (i % 32)))
4624             {
4625               enum machine_mode mode = REGISTER_NATURAL_MODE (i);
4626               int reg = i;
4627               rtx reg_rtx, mem_rtx, pre_dec = NULL_RTX;
4628
4629               if (mode == SFmode && (i % 2) == 1
4630                   && ! TARGET_FPU_SINGLE && FP_REGISTER_P (i)
4631                   && (live_regs_mask[(i ^ 1) / 32] & (1 << ((i ^ 1) % 32))))
4632                 {
4633                   mode = DFmode;
4634                   i--;
4635                   reg--;
4636                 }
4637                 
4638               /* If we're doing the aligned pass and this is not aligned,
4639                  or we're doing the unaligned pass and this is aligned,
4640                  skip it.  */
4641               if ((GET_MODE_SIZE (mode) % (STACK_BOUNDARY / BITS_PER_UNIT)
4642                    == 0) != align)
4643                 continue;
4644
4645               offset -= GET_MODE_SIZE (mode);
4646
4647               reg_rtx = gen_rtx_REG (mode, reg);
4648
4649               mem_rtx = gen_rtx_MEM (mode,
4650                                      gen_rtx_PLUS (Pmode,
4651                                                    stack_pointer_rtx,
4652                                                    GEN_INT (offset)));
4653
4654               GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (mem_rtx, 0), try_pre_dec);
4655
4656               mem_rtx = NULL_RTX;
4657
4658             try_pre_dec:
4659               do
4660                 if (HAVE_PRE_DECREMENT
4661                     && (offset_in_r0 - offset == GET_MODE_SIZE (mode)
4662                         || mem_rtx == NULL_RTX
4663                         || i == PR_REG || SPECIAL_REGISTER_P (i)))
4664                   {
4665                     pre_dec = gen_rtx_MEM (mode,
4666                                            gen_rtx_PRE_DEC (Pmode, r0));
4667
4668                     GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (pre_dec, 0),
4669                                               pre_dec_ok);
4670
4671                     pre_dec = NULL_RTX;
4672
4673                     break;
4674
4675                   pre_dec_ok:
4676                     mem_rtx = NULL_RTX;
4677                     offset += GET_MODE_SIZE (mode);
4678                   }
4679               while (0);
4680
4681               if (mem_rtx != NULL_RTX)
4682                 goto addr_ok;
4683
4684               if (offset_in_r0 == -1)
4685                 {
4686                   emit_move_insn (r0, GEN_INT (offset));
4687                   offset_in_r0 = offset;
4688                 }
4689               else if (offset != offset_in_r0)
4690                 {
4691                   emit_move_insn (r0,
4692                                   gen_rtx_PLUS
4693                                   (Pmode, r0,
4694                                    GEN_INT (offset - offset_in_r0)));
4695                   offset_in_r0 += offset - offset_in_r0;
4696                 }
4697                                                   
4698               if (pre_dec != NULL_RTX)
4699                 {
4700                   if (! sp_in_r0)
4701                     {
4702                       emit_move_insn (r0,
4703                                       gen_rtx_PLUS
4704                                       (Pmode, r0, stack_pointer_rtx));
4705                       sp_in_r0 = 1;
4706                     }
4707
4708                   offset -= GET_MODE_SIZE (mode);
4709                   offset_in_r0 -= GET_MODE_SIZE (mode);
4710
4711                   mem_rtx = pre_dec;
4712                 }
4713               else if (sp_in_r0)
4714                 mem_rtx = gen_rtx_MEM (mode, r0);
4715               else
4716                 mem_rtx = gen_rtx_MEM (mode,
4717                                        gen_rtx_PLUS (Pmode,
4718                                                      stack_pointer_rtx,
4719                                                      r0));
4720
4721               /* We must not use an r0-based address for target-branch
4722                  registers or for special registers without pre-dec
4723                  memory addresses, since we store their values in r0
4724                  first.  */
4725               if (TARGET_REGISTER_P (i)
4726                   || ((i == PR_REG || SPECIAL_REGISTER_P (i))
4727                       && mem_rtx != pre_dec))
4728                 abort ();
4729
4730             addr_ok:
4731               if (TARGET_REGISTER_P (i)
4732                   || ((i == PR_REG || SPECIAL_REGISTER_P (i))
4733                       && mem_rtx != pre_dec))
4734                 {
4735                   rtx r0mode = gen_rtx_REG (GET_MODE (reg_rtx), R0_REG);
4736
4737                   emit_move_insn (r0mode, reg_rtx);
4738
4739                   offset_in_r0 = -1;
4740                   sp_in_r0 = 0;
4741
4742                   reg_rtx = r0mode;
4743                 }
4744
4745               emit_move_insn (mem_rtx, reg_rtx);
4746             }
4747
4748       if (offset != d_rounding)
4749         abort ();
4750     }
4751   else
4752     push_regs (live_regs_mask);
4753
4754   if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
4755     {
4756       rtx insn = get_last_insn ();
4757       rtx last = emit_insn (gen_GOTaddr2picreg ());
4758
4759       /* Mark these insns as possibly dead.  Sometimes, flow2 may
4760          delete all uses of the PIC register.  In this case, let it
4761          delete the initialization too.  */
4762       do
4763         {
4764           insn = NEXT_INSN (insn);
4765
4766           REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
4767                                                 const0_rtx,
4768                                                 REG_NOTES (insn));
4769         }
4770       while (insn != last);
4771     }
4772
4773   if (SHMEDIA_REGS_STACK_ADJUST ())
4774     {
4775       emit_move_insn (gen_rtx_REG (Pmode, R0_REG),
4776                       gen_rtx_SYMBOL_REF (Pmode,
4777                                           TARGET_FPU_ANY
4778                                           ? "__GCC_push_shmedia_regs"
4779                                           : "__GCC_push_shmedia_regs_nofpu"));
4780       /* This must NOT go through the PLT, otherwise mach and macl
4781          may be clobbered.  */
4782       emit_insn (gen_shmedia_save_restore_regs_compact
4783                  (GEN_INT (-SHMEDIA_REGS_STACK_ADJUST ())));
4784     }
4785
4786   if (target_flags != save_flags)
4787     {
4788       rtx insn = emit_insn (gen_toggle_sz ());
4789
4790       /* If we're lucky, a mode switch in the function body will
4791          overwrite fpscr, turning this insn dead.  Tell flow this
4792          insn is ok to delete.  */
4793       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD,
4794                                             const0_rtx,
4795                                             REG_NOTES (insn));
4796     }
4797
4798   target_flags = save_flags;
4799
4800   output_stack_adjust (-rounded_frame_size (d) + d_rounding,
4801                        stack_pointer_rtx, TARGET_SH5 ? 0 : 1, frame_insn);
4802
4803   if (frame_pointer_needed)
4804     frame_insn (GEN_MOV (frame_pointer_rtx, stack_pointer_rtx));
4805
4806   if (TARGET_SHCOMPACT
4807       && (current_function_args_info.call_cookie & ~ CALL_COOKIE_RET_TRAMP(1)))
4808     {
4809       /* This must NOT go through the PLT, otherwise mach and macl
4810          may be clobbered.  */
4811       emit_move_insn (gen_rtx_REG (Pmode, R0_REG),
4812                       gen_rtx_SYMBOL_REF (Pmode,
4813                                           "__GCC_shcompact_incoming_args"));
4814       emit_insn (gen_shcompact_incoming_args ());
4815     }
4816 }
4817
4818 void
4819 sh_expand_epilogue ()
4820 {
4821   HOST_WIDE_INT live_regs_mask[(FIRST_PSEUDO_REGISTER + 31) / 32];
4822   int d, i;
4823   int d_rounding = 0;
4824
4825   int save_flags = target_flags;
4826   int frame_size;
4827
4828   calc_live_regs (&d, live_regs_mask);
4829
4830   if (TARGET_SH5 && d % (STACK_BOUNDARY / BITS_PER_UNIT))
4831     d_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
4832                   - d % (STACK_BOUNDARY / BITS_PER_UNIT));
4833
4834   frame_size = rounded_frame_size (d) - d_rounding;
4835
4836   if (frame_pointer_needed)
4837     {
4838       output_stack_adjust (frame_size, frame_pointer_rtx, 7, emit_insn);
4839
4840       /* We must avoid moving the stack pointer adjustment past code
4841          which reads from the local frame, else an interrupt could
4842          occur after the SP adjustment and clobber data in the local
4843          frame.  */
4844       emit_insn (gen_blockage ());
4845       emit_insn (GEN_MOV (stack_pointer_rtx, frame_pointer_rtx));
4846     }
4847   else if (frame_size)
4848     {
4849       /* We must avoid moving the stack pointer adjustment past code
4850          which reads from the local frame, else an interrupt could
4851          occur after the SP adjustment and clobber data in the local
4852          frame.  */
4853       emit_insn (gen_blockage ());
4854       output_stack_adjust (frame_size, stack_pointer_rtx, 7, emit_insn);
4855     }
4856
4857   if (SHMEDIA_REGS_STACK_ADJUST ())
4858     {
4859       emit_move_insn (gen_rtx_REG (Pmode, R0_REG),
4860                       gen_rtx_SYMBOL_REF (Pmode,
4861                                           TARGET_FPU_ANY
4862                                           ? "__GCC_pop_shmedia_regs"
4863                                           : "__GCC_pop_shmedia_regs_nofpu"));
4864       /* This must NOT go through the PLT, otherwise mach and macl
4865          may be clobbered.  */
4866       emit_insn (gen_shmedia_save_restore_regs_compact
4867                  (GEN_INT (SHMEDIA_REGS_STACK_ADJUST ())));
4868     }
4869
4870   /* Pop all the registers.  */
4871
4872   if (target_flags != save_flags)
4873     emit_insn (gen_toggle_sz ());
4874   if (TARGET_SH5)
4875     {
4876       int offset = d_rounding;
4877       int offset_in_r0 = -1;
4878       int sp_in_r0 = 0;
4879       int align;
4880       rtx r0 = gen_rtx_REG (Pmode, R0_REG);
4881       
4882       /* We loop twice: first, we save 8-byte aligned registers in the
4883          higher addresses, that are known to be aligned.  Then, we
4884          proceed to saving 32-bit registers that don't need 8-byte
4885          alignment.  */
4886       for (align = 0; align <= 1; align++)
4887         for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4888           if (live_regs_mask[i/32] & (1 << (i % 32)))
4889             {
4890               enum machine_mode mode = REGISTER_NATURAL_MODE (i);
4891               int reg = i;
4892               rtx reg_rtx, mem_rtx, post_inc = NULL_RTX, insn;
4893
4894               if (mode == SFmode && (i % 2) == 0
4895                   && ! TARGET_FPU_SINGLE && FP_REGISTER_P (i)
4896                   && (live_regs_mask[(i ^ 1) / 32] & (1 << ((i ^ 1) % 32))))
4897                 {
4898                   mode = DFmode;
4899                   i++;
4900                 }
4901
4902               /* If we're doing the aligned pass and this is not aligned,
4903                  or we're doing the unaligned pass and this is aligned,
4904                  skip it.  */
4905               if ((GET_MODE_SIZE (mode) % (STACK_BOUNDARY / BITS_PER_UNIT)
4906                    == 0) != align)
4907                 continue;
4908
4909               reg_rtx = gen_rtx_REG (mode, reg);
4910
4911               mem_rtx = gen_rtx_MEM (mode,
4912                                      gen_rtx_PLUS (Pmode,
4913                                                    stack_pointer_rtx,
4914                                                    GEN_INT (offset)));
4915
4916               GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (mem_rtx, 0), try_post_inc);
4917
4918               mem_rtx = NULL_RTX;
4919
4920             try_post_inc:
4921               do
4922                 if (HAVE_POST_INCREMENT
4923                     && (offset == offset_in_r0
4924                         || (offset + GET_MODE_SIZE (mode) != d + d_rounding
4925                             && mem_rtx == NULL_RTX)
4926                         || i == PR_REG || SPECIAL_REGISTER_P (i)))
4927                   {
4928                     post_inc = gen_rtx_MEM (mode,
4929                                             gen_rtx_POST_INC (Pmode, r0));
4930
4931                     GO_IF_LEGITIMATE_ADDRESS (mode, XEXP (post_inc, 0),
4932                                               post_inc_ok);
4933
4934                     post_inc = NULL_RTX;
4935
4936                     break;
4937                     
4938                   post_inc_ok:
4939                     mem_rtx = NULL_RTX;
4940                   }
4941               while (0);
4942               
4943               if (mem_rtx != NULL_RTX)
4944                 goto addr_ok;
4945
4946               if (offset_in_r0 == -1)
4947                 {
4948                   emit_move_insn (r0, GEN_INT (offset));
4949                   offset_in_r0 = offset;
4950                 }
4951               else if (offset != offset_in_r0)
4952                 {
4953                   emit_move_insn (r0,
4954                                   gen_rtx_PLUS
4955                                   (Pmode, r0,
4956                                    GEN_INT (offset - offset_in_r0)));
4957                   offset_in_r0 += offset - offset_in_r0;
4958                 }
4959                   
4960               if (post_inc != NULL_RTX)
4961                 {
4962                   if (! sp_in_r0)
4963                     {
4964                       emit_move_insn (r0,
4965                                       gen_rtx_PLUS
4966                                       (Pmode, r0, stack_pointer_rtx));
4967                       sp_in_r0 = 1;
4968                     }
4969                   
4970                   mem_rtx = post_inc;
4971
4972                   offset_in_r0 += GET_MODE_SIZE (mode);
4973                 }
4974               else if (sp_in_r0)
4975                 mem_rtx = gen_rtx_MEM (mode, r0);
4976               else
4977                 mem_rtx = gen_rtx_MEM (mode,
4978                                        gen_rtx_PLUS (Pmode,
4979                                                      stack_pointer_rtx,
4980                                                      r0));
4981
4982               if ((i == PR_REG || SPECIAL_REGISTER_P (i))
4983                   && mem_rtx != post_inc)
4984                 abort ();
4985
4986             addr_ok:
4987               if ((i == PR_REG || SPECIAL_REGISTER_P (i))
4988                   && mem_rtx != post_inc)
4989                 {
4990                   insn = emit_move_insn (r0, mem_rtx);
4991                   mem_rtx = r0;
4992                 }
4993               else if (TARGET_REGISTER_P (i))
4994                 {
4995                   rtx r1 = gen_rtx_REG (mode, R1_REG);
4996
4997                   insn = emit_move_insn (r1, mem_rtx);
4998                   mem_rtx = r1;
4999                 }
5000
5001               insn = emit_move_insn (reg_rtx, mem_rtx);
5002
5003               offset += GET_MODE_SIZE (mode);
5004             }
5005
5006       if (offset != d + d_rounding)
5007         abort ();
5008
5009       goto finish;
5010     }
5011   else
5012     d = 0;
5013   if (live_regs_mask[PR_REG / 32] & (1 << (PR_REG % 32)))
5014     pop (PR_REG);
5015   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5016     {
5017       int j = (FIRST_PSEUDO_REGISTER - 1) - i;
5018
5019       if (j != PR_REG && live_regs_mask[j / 32] & (1 << (j % 32)))
5020         pop (j);
5021     }
5022  finish:
5023   if (target_flags != save_flags)
5024     emit_insn (gen_toggle_sz ());
5025   target_flags = save_flags;
5026
5027   output_stack_adjust (extra_push + current_function_pretend_args_size
5028                        + d + d_rounding
5029                        + current_function_args_info.stack_regs * 8,
5030                        stack_pointer_rtx, 7, emit_insn);
5031
5032   /* Switch back to the normal stack if necessary.  */
5033   if (sp_switch)
5034     emit_insn (gen_sp_switch_2 ());
5035
5036   /* Tell flow the insn that pops PR isn't dead.  */
5037   /* PR_REG will never be live in SHmedia mode, and we don't need to
5038      USE PR_MEDIA_REG, since it will be explicitly copied to TR0_REG
5039      by the return pattern.  */
5040   if (live_regs_mask[PR_REG / 32] & (1 << (PR_REG % 32)))
5041     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, PR_REG)));
5042 }
5043
5044 static int sh_need_epilogue_known = 0;
5045
5046 int
5047 sh_need_epilogue ()
5048 {
5049   if (! sh_need_epilogue_known)
5050     {
5051       rtx epilogue;
5052
5053       start_sequence ();
5054       sh_expand_epilogue ();
5055       epilogue = get_insns ();
5056       end_sequence ();
5057       sh_need_epilogue_known = (epilogue == NULL ? -1 : 1);
5058     }
5059   return sh_need_epilogue_known > 0;
5060 }
5061
5062 /* Clear variables at function end.  */
5063
5064 static void
5065 sh_output_function_epilogue (file, size)
5066      FILE *file ATTRIBUTE_UNUSED;
5067      HOST_WIDE_INT size ATTRIBUTE_UNUSED;
5068 {
5069   trap_exit = pragma_interrupt = pragma_trapa = pragma_nosave_low_regs = 0;
5070   sh_need_epilogue_known = 0;
5071   sp_switch = NULL_RTX;
5072 }
5073
5074 rtx
5075 sh_builtin_saveregs ()
5076 {
5077   /* First unnamed integer register.  */
5078   int first_intreg = current_function_args_info.arg_count[(int) SH_ARG_INT];
5079   /* Number of integer registers we need to save.  */
5080   int n_intregs = MAX (0, NPARM_REGS (SImode) - first_intreg);
5081   /* First unnamed SFmode float reg */
5082   int first_floatreg = current_function_args_info.arg_count[(int) SH_ARG_FLOAT];
5083   /* Number of SFmode float regs to save.  */
5084   int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
5085   rtx regbuf, fpregs;
5086   int bufsize, regno;
5087   HOST_WIDE_INT alias_set;
5088
5089   if (TARGET_SH5)
5090     {
5091       if (n_intregs)
5092         {
5093           int pushregs = n_intregs;
5094
5095           while (pushregs < NPARM_REGS (SImode) - 1
5096                  && (CALL_COOKIE_INT_REG_GET
5097                         (current_function_args_info.call_cookie,
5098                          NPARM_REGS (SImode) - pushregs)
5099                      == 1))
5100             {
5101               current_function_args_info.call_cookie
5102                 &= ~ CALL_COOKIE_INT_REG (NPARM_REGS (SImode)
5103                                           - pushregs, 1);
5104               pushregs++;
5105             }
5106
5107           if (pushregs == NPARM_REGS (SImode))
5108             current_function_args_info.call_cookie
5109               |= (CALL_COOKIE_INT_REG (0, 1)
5110                   | CALL_COOKIE_STACKSEQ (pushregs - 1));
5111           else
5112             current_function_args_info.call_cookie
5113               |= CALL_COOKIE_STACKSEQ (pushregs);
5114
5115           current_function_pretend_args_size += 8 * n_intregs;
5116         }
5117       if (TARGET_SHCOMPACT)
5118         return const0_rtx;
5119     }
5120   
5121   if (! TARGET_SH3E && ! TARGET_SH4 && ! TARGET_SH5)
5122     {
5123       error ("__builtin_saveregs not supported by this subtarget");
5124       return const0_rtx;
5125     }
5126
5127   if (TARGET_SHMEDIA)
5128     n_floatregs = 0;
5129
5130   /* Allocate block of memory for the regs.  */
5131   /* ??? If n_intregs + n_floatregs == 0, should we allocate at least 1 byte?
5132      Or can assign_stack_local accept a 0 SIZE argument?  */
5133   bufsize = (n_intregs * UNITS_PER_WORD) + (n_floatregs * UNITS_PER_WORD);
5134
5135   if (TARGET_SHMEDIA)
5136     regbuf = gen_rtx_MEM (BLKmode,
5137                           gen_rtx_REG (Pmode, ARG_POINTER_REGNUM));
5138   else if (n_floatregs & 1)
5139     {
5140       rtx addr;
5141
5142       regbuf = assign_stack_local (BLKmode, bufsize + UNITS_PER_WORD, 0);
5143       addr = copy_to_mode_reg (Pmode, XEXP (regbuf, 0));
5144       emit_insn (gen_iorsi3 (addr, addr, GEN_INT (UNITS_PER_WORD)));
5145       regbuf = change_address (regbuf, BLKmode, addr);
5146     }
5147   else
5148     regbuf = assign_stack_local (BLKmode, bufsize, 0);
5149   alias_set = get_varargs_alias_set ();
5150   set_mem_alias_set (regbuf, alias_set);
5151
5152   /* Save int args.
5153      This is optimized to only save the regs that are necessary.  Explicitly
5154      named args need not be saved.  */
5155   if (n_intregs > 0)
5156     move_block_from_reg (BASE_ARG_REG (SImode) + first_intreg,
5157                          adjust_address (regbuf, BLKmode,
5158                                          n_floatregs * UNITS_PER_WORD),
5159                          n_intregs, n_intregs * UNITS_PER_WORD);
5160
5161   if (TARGET_SHMEDIA)
5162     /* Return the address of the regbuf.  */
5163     return XEXP (regbuf, 0);
5164
5165   /* Save float args.
5166      This is optimized to only save the regs that are necessary.  Explicitly
5167      named args need not be saved.
5168      We explicitly build a pointer to the buffer because it halves the insn
5169      count when not optimizing (otherwise the pointer is built for each reg
5170      saved).
5171      We emit the moves in reverse order so that we can use predecrement.  */
5172
5173   fpregs = gen_reg_rtx (Pmode);
5174   emit_move_insn (fpregs, XEXP (regbuf, 0));
5175   emit_insn (gen_addsi3 (fpregs, fpregs,
5176                          GEN_INT (n_floatregs * UNITS_PER_WORD)));
5177   if (TARGET_SH4)
5178     {
5179       rtx mem;
5180       for (regno = NPARM_REGS (DFmode) - 2; regno >= first_floatreg; regno -= 2)
5181         {
5182           emit_insn (gen_addsi3 (fpregs, fpregs,
5183                                  GEN_INT (-2 * UNITS_PER_WORD)));
5184           mem = gen_rtx_MEM (DFmode, fpregs);
5185           set_mem_alias_set (mem, alias_set);
5186           emit_move_insn (mem, 
5187                           gen_rtx (REG, DFmode, BASE_ARG_REG (DFmode) + regno));
5188         }
5189       regno = first_floatreg;
5190       if (regno & 1)
5191         {
5192           emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (- UNITS_PER_WORD)));
5193           mem = gen_rtx_MEM (SFmode, fpregs);
5194           set_mem_alias_set (mem, alias_set);
5195           emit_move_insn (mem,
5196                           gen_rtx (REG, SFmode, BASE_ARG_REG (SFmode) + regno
5197                                                 - (TARGET_LITTLE_ENDIAN != 0)));
5198         }
5199     }
5200   else
5201     for (regno = NPARM_REGS (SFmode) - 1; regno >= first_floatreg; regno--)
5202       {
5203         rtx mem;
5204
5205         emit_insn (gen_addsi3 (fpregs, fpregs, GEN_INT (- UNITS_PER_WORD)));
5206         mem = gen_rtx_MEM (SFmode, fpregs);
5207         set_mem_alias_set (mem, alias_set);
5208         emit_move_insn (mem,
5209                         gen_rtx_REG (SFmode, BASE_ARG_REG (SFmode) + regno));
5210       }
5211
5212   /* Return the address of the regbuf.  */
5213   return XEXP (regbuf, 0);
5214 }
5215
5216 /* Define the `__builtin_va_list' type for the ABI.  */
5217
5218 tree
5219 sh_build_va_list ()
5220 {
5221   tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
5222   tree record;
5223
5224   if (TARGET_SH5 || (! TARGET_SH3E && ! TARGET_SH4) || TARGET_HITACHI)
5225     return ptr_type_node;
5226
5227   record = make_node (RECORD_TYPE);
5228
5229   f_next_o = build_decl (FIELD_DECL, get_identifier ("__va_next_o"),
5230                          ptr_type_node);
5231   f_next_o_limit = build_decl (FIELD_DECL,
5232                                get_identifier ("__va_next_o_limit"),
5233                                ptr_type_node);
5234   f_next_fp = build_decl (FIELD_DECL, get_identifier ("__va_next_fp"),
5235                           ptr_type_node);
5236   f_next_fp_limit = build_decl (FIELD_DECL,
5237                                 get_identifier ("__va_next_fp_limit"),
5238                                 ptr_type_node);
5239   f_next_stack = build_decl (FIELD_DECL, get_identifier ("__va_next_stack"),
5240                              ptr_type_node);
5241
5242   DECL_FIELD_CONTEXT (f_next_o) = record;
5243   DECL_FIELD_CONTEXT (f_next_o_limit) = record;
5244   DECL_FIELD_CONTEXT (f_next_fp) = record;
5245   DECL_FIELD_CONTEXT (f_next_fp_limit) = record;
5246   DECL_FIELD_CONTEXT (f_next_stack) = record;
5247
5248   TYPE_FIELDS (record) = f_next_o;
5249   TREE_CHAIN (f_next_o) = f_next_o_limit;
5250   TREE_CHAIN (f_next_o_limit) = f_next_fp;
5251   TREE_CHAIN (f_next_fp) = f_next_fp_limit;
5252   TREE_CHAIN (f_next_fp_limit) = f_next_stack;
5253
5254   layout_type (record);
5255
5256   return record;
5257 }
5258
5259 /* Implement `va_start' for varargs and stdarg.  */
5260
5261 void
5262 sh_va_start (valist, nextarg)
5263      tree valist;
5264      rtx nextarg;
5265 {
5266   tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
5267   tree next_o, next_o_limit, next_fp, next_fp_limit, next_stack;
5268   tree t, u;
5269   int nfp, nint;
5270
5271   if (TARGET_SH5)
5272     {
5273       expand_builtin_saveregs ();
5274       std_expand_builtin_va_start (valist, nextarg);
5275       return;
5276     }
5277
5278   if ((! TARGET_SH3E && ! TARGET_SH4) || TARGET_HITACHI)
5279     {
5280       std_expand_builtin_va_start (valist, nextarg);
5281       return;
5282     }
5283
5284   f_next_o = TYPE_FIELDS (va_list_type_node);
5285   f_next_o_limit = TREE_CHAIN (f_next_o);
5286   f_next_fp = TREE_CHAIN (f_next_o_limit);
5287   f_next_fp_limit = TREE_CHAIN (f_next_fp);
5288   f_next_stack = TREE_CHAIN (f_next_fp_limit);
5289
5290   next_o = build (COMPONENT_REF, TREE_TYPE (f_next_o), valist, f_next_o);
5291   next_o_limit = build (COMPONENT_REF, TREE_TYPE (f_next_o_limit),
5292                         valist, f_next_o_limit);
5293   next_fp = build (COMPONENT_REF, TREE_TYPE (f_next_fp), valist, f_next_fp);
5294   next_fp_limit = build (COMPONENT_REF, TREE_TYPE (f_next_fp_limit),
5295                          valist, f_next_fp_limit);
5296   next_stack = build (COMPONENT_REF, TREE_TYPE (f_next_stack),
5297                       valist, f_next_stack);
5298
5299   /* Call __builtin_saveregs.  */
5300   u = make_tree (ptr_type_node, expand_builtin_saveregs ());
5301   t = build (MODIFY_EXPR, ptr_type_node, next_fp, u);
5302   TREE_SIDE_EFFECTS (t) = 1;
5303   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5304
5305   nfp = current_function_args_info.arg_count[SH_ARG_FLOAT];
5306   if (nfp < 8)
5307     nfp = 8 - nfp;
5308   else
5309     nfp = 0;
5310   u = fold (build (PLUS_EXPR, ptr_type_node, u,
5311                    build_int_2 (UNITS_PER_WORD * nfp, 0)));
5312   t = build (MODIFY_EXPR, ptr_type_node, next_fp_limit, u);
5313   TREE_SIDE_EFFECTS (t) = 1;
5314   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5315
5316   t = build (MODIFY_EXPR, ptr_type_node, next_o, u);
5317   TREE_SIDE_EFFECTS (t) = 1;
5318   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5319
5320   nint = current_function_args_info.arg_count[SH_ARG_INT];
5321   if (nint < 4)
5322     nint = 4 - nint;
5323   else
5324     nint = 0;
5325   u = fold (build (PLUS_EXPR, ptr_type_node, u,
5326                    build_int_2 (UNITS_PER_WORD * nint, 0)));
5327   t = build (MODIFY_EXPR, ptr_type_node, next_o_limit, u);
5328   TREE_SIDE_EFFECTS (t) = 1;
5329   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5330
5331   u = make_tree (ptr_type_node, nextarg);
5332   t = build (MODIFY_EXPR, ptr_type_node, next_stack, u);
5333   TREE_SIDE_EFFECTS (t) = 1;
5334   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5335 }
5336
5337 /* Implement `va_arg'.  */
5338
5339 rtx
5340 sh_va_arg (valist, type)
5341      tree valist, type;
5342 {
5343   HOST_WIDE_INT size, rsize;
5344   tree tmp, pptr_type_node;
5345   rtx addr_rtx, r;
5346   rtx result;
5347   int pass_by_ref = MUST_PASS_IN_STACK (TYPE_MODE (type), type);
5348
5349   size = int_size_in_bytes (type);
5350   rsize = (size + UNITS_PER_WORD - 1) & -UNITS_PER_WORD;
5351   pptr_type_node = build_pointer_type (ptr_type_node);
5352
5353   if (pass_by_ref)
5354     type = build_pointer_type (type);
5355
5356   if (! TARGET_SH5 && (TARGET_SH3E || TARGET_SH4) && ! TARGET_HITACHI)
5357     {
5358       tree f_next_o, f_next_o_limit, f_next_fp, f_next_fp_limit, f_next_stack;
5359       tree next_o, next_o_limit, next_fp, next_fp_limit, next_stack;
5360       int pass_as_float;
5361       rtx lab_false, lab_over;
5362
5363       f_next_o = TYPE_FIELDS (va_list_type_node);
5364       f_next_o_limit = TREE_CHAIN (f_next_o);
5365       f_next_fp = TREE_CHAIN (f_next_o_limit);
5366       f_next_fp_limit = TREE_CHAIN (f_next_fp);
5367       f_next_stack = TREE_CHAIN (f_next_fp_limit);
5368
5369       next_o = build (COMPONENT_REF, TREE_TYPE (f_next_o), valist, f_next_o);
5370       next_o_limit = build (COMPONENT_REF, TREE_TYPE (f_next_o_limit),
5371                             valist, f_next_o_limit);
5372       next_fp = build (COMPONENT_REF, TREE_TYPE (f_next_fp),
5373                        valist, f_next_fp);
5374       next_fp_limit = build (COMPONENT_REF, TREE_TYPE (f_next_fp_limit),
5375                              valist, f_next_fp_limit);
5376       next_stack = build (COMPONENT_REF, TREE_TYPE (f_next_stack),
5377                           valist, f_next_stack);
5378
5379       if (TARGET_SH4)
5380         {
5381           pass_as_float = ((TREE_CODE (type) == REAL_TYPE && size <= 8)
5382                            || (TREE_CODE (type) == COMPLEX_TYPE
5383                                && TREE_CODE (TREE_TYPE (type)) == REAL_TYPE
5384                                && size <= 16));
5385         }
5386       else
5387         {
5388           pass_as_float = (TREE_CODE (type) == REAL_TYPE && size == 4);
5389         }
5390
5391       addr_rtx = gen_reg_rtx (Pmode);
5392       lab_false = gen_label_rtx ();
5393       lab_over = gen_label_rtx ();
5394
5395       if (pass_as_float)
5396         {
5397           int first_floatreg
5398             = current_function_args_info.arg_count[(int) SH_ARG_FLOAT];
5399           int n_floatregs = MAX (0, NPARM_REGS (SFmode) - first_floatreg);
5400
5401           emit_cmp_and_jump_insns (expand_expr (next_fp, NULL_RTX, Pmode,
5402                                                 EXPAND_NORMAL),
5403                                    expand_expr (next_fp_limit, NULL_RTX,
5404                                                 Pmode, EXPAND_NORMAL),
5405                                    GE, const1_rtx, Pmode, 1, lab_false);
5406
5407           if (TYPE_ALIGN (type) > BITS_PER_WORD
5408               || (((TREE_CODE (type) == REAL_TYPE && size == 8) || size == 16)
5409                   && (n_floatregs & 1)))
5410             {
5411               tmp = build (BIT_AND_EXPR, ptr_type_node, next_fp,
5412                            build_int_2 (UNITS_PER_WORD, 0));
5413               tmp = build (PLUS_EXPR, ptr_type_node, next_fp, tmp);
5414               tmp = build (MODIFY_EXPR, ptr_type_node, next_fp, tmp);
5415               TREE_SIDE_EFFECTS (tmp) = 1;
5416               expand_expr (tmp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5417             }
5418
5419           tmp = build1 (ADDR_EXPR, pptr_type_node, next_fp);
5420           r = expand_expr (tmp, addr_rtx, Pmode, EXPAND_NORMAL);
5421           if (r != addr_rtx)
5422             emit_move_insn (addr_rtx, r);
5423
5424           emit_jump_insn (gen_jump (lab_over));
5425           emit_barrier ();
5426           emit_label (lab_false);
5427
5428           tmp = build1 (ADDR_EXPR, pptr_type_node, next_stack);
5429           r = expand_expr (tmp, addr_rtx, Pmode, EXPAND_NORMAL);
5430           if (r != addr_rtx)
5431             emit_move_insn (addr_rtx, r);
5432         }
5433       else
5434         {
5435           tmp = build (PLUS_EXPR, ptr_type_node, next_o,
5436                        build_int_2 (rsize, 0));
5437           
5438           emit_cmp_and_jump_insns (expand_expr (tmp, NULL_RTX, Pmode,
5439                                                 EXPAND_NORMAL),
5440                                    expand_expr (next_o_limit, NULL_RTX,
5441                                                 Pmode, EXPAND_NORMAL),
5442                                    GT, const1_rtx, Pmode, 1, lab_false);
5443
5444           tmp = build1 (ADDR_EXPR, pptr_type_node, next_o);
5445           r = expand_expr (tmp, addr_rtx, Pmode, EXPAND_NORMAL);
5446           if (r != addr_rtx)
5447             emit_move_insn (addr_rtx, r);
5448
5449           emit_jump_insn (gen_jump (lab_over));
5450           emit_barrier ();
5451           emit_label (lab_false);
5452
5453           if (size > 4 && ! TARGET_SH4)
5454             {
5455               tmp = build (MODIFY_EXPR, ptr_type_node, next_o, next_o_limit);
5456               TREE_SIDE_EFFECTS (tmp) = 1;
5457               expand_expr (tmp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5458             }
5459
5460           tmp = build1 (ADDR_EXPR, pptr_type_node, next_stack);
5461           r = expand_expr (tmp, addr_rtx, Pmode, EXPAND_NORMAL);
5462           if (r != addr_rtx)
5463             emit_move_insn (addr_rtx, r);
5464         }
5465
5466       emit_label (lab_over);
5467
5468       tmp = make_tree (pptr_type_node, addr_rtx);
5469       valist = build1 (INDIRECT_REF, ptr_type_node, tmp);
5470     }
5471
5472   /* ??? In va-sh.h, there had been code to make values larger than
5473      size 8 indirect.  This does not match the FUNCTION_ARG macros.  */
5474
5475   result = std_expand_builtin_va_arg (valist, type);
5476   if (pass_by_ref)
5477     {
5478 #ifdef POINTERS_EXTEND_UNSIGNED
5479       if (GET_MODE (addr) != Pmode)
5480         addr = convert_memory_address (Pmode, result);
5481 #endif
5482       result = gen_rtx_MEM (ptr_mode, force_reg (Pmode, result));
5483       set_mem_alias_set (result, get_varargs_alias_set ());
5484     }
5485   /* ??? expand_builtin_va_arg will also set the alias set of the dereferenced
5486      argument to the varargs alias set.  */
5487   return result;
5488 }
5489
5490 /* Define the offset between two registers, one to be eliminated, and
5491    the other its replacement, at the start of a routine.  */
5492
5493 int
5494 initial_elimination_offset (from, to)
5495      int from;
5496      int to;
5497 {
5498   int regs_saved;
5499   int regs_saved_rounding = 0;
5500   int total_saved_regs_space;
5501   int total_auto_space;
5502   int save_flags = target_flags;
5503   int copy_flags;
5504
5505   HOST_WIDE_INT live_regs_mask[(FIRST_PSEUDO_REGISTER + 31) / 32];
5506   calc_live_regs (&regs_saved, live_regs_mask);
5507   regs_saved += SHMEDIA_REGS_STACK_ADJUST ();
5508   if (TARGET_SH5 && regs_saved % (STACK_BOUNDARY / BITS_PER_UNIT))
5509     regs_saved_rounding = ((STACK_BOUNDARY / BITS_PER_UNIT)
5510                            - regs_saved % (STACK_BOUNDARY / BITS_PER_UNIT));
5511
5512   total_auto_space = rounded_frame_size (regs_saved) - regs_saved_rounding;
5513   copy_flags = target_flags;
5514   target_flags = save_flags;
5515
5516   total_saved_regs_space = regs_saved + regs_saved_rounding;
5517
5518   if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
5519     return total_saved_regs_space + total_auto_space
5520       + current_function_args_info.byref_regs * 8;
5521
5522   if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
5523     return total_saved_regs_space + total_auto_space
5524       + current_function_args_info.byref_regs * 8;
5525
5526   /* Initial gap between fp and sp is 0.  */
5527   if (from == FRAME_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
5528     return 0;
5529
5530   if (from == RETURN_ADDRESS_POINTER_REGNUM
5531       && (to == FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM))
5532     {
5533       if (TARGET_SH5)
5534         {
5535           int i, n = total_saved_regs_space;
5536           int align;
5537           int pr_reg = TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG;
5538           
5539           n += total_auto_space;
5540
5541           /* If it wasn't saved, there's not much we can do.  */
5542           if ((live_regs_mask[pr_reg / 32] & (1 << (pr_reg % 32))) == 0)
5543             return n;
5544
5545           target_flags = copy_flags;
5546
5547           /* We loop twice: first, check 8-byte aligned registers,
5548              that are stored in the higher addresses, that are known
5549              to be aligned.  Then, check 32-bit registers that don't
5550              need 8-byte alignment.  */
5551           for (align = 1; align >= 0; align--)
5552             for (i = FIRST_PSEUDO_REGISTER - 1; i >= 0; i--)
5553               if (live_regs_mask[i/32] & (1 << (i % 32)))
5554                 {
5555                   enum machine_mode mode = REGISTER_NATURAL_MODE (i);
5556
5557                   if (mode == SFmode && (i % 2) == 1
5558                       && ! TARGET_FPU_SINGLE && FP_REGISTER_P (i)
5559                       && (live_regs_mask[(i ^ 1) / 32]
5560                           & (1 << ((i ^ 1) % 32))))
5561                     {
5562                       mode = DFmode;
5563                       i--;
5564                     }
5565                 
5566                   /* If we're doing the aligned pass and this is not aligned,
5567                      or we're doing the unaligned pass and this is aligned,
5568                      skip it.  */
5569                   if ((GET_MODE_SIZE (mode) % (STACK_BOUNDARY / BITS_PER_UNIT)
5570                        == 0) != align)
5571                     continue;
5572
5573                   n -= GET_MODE_SIZE (mode);
5574
5575                   if (i == pr_reg)
5576                     {
5577                       target_flags = save_flags;
5578                       return n;
5579                     }
5580                 }
5581
5582           abort ();
5583         }
5584       else
5585         return total_auto_space;
5586     }
5587
5588   abort ();
5589 }
5590 \f
5591 /* Handle machine specific pragmas to be semi-compatible with Hitachi
5592    compiler.  */
5593
5594 void
5595 sh_pr_interrupt (pfile)
5596      cpp_reader *pfile ATTRIBUTE_UNUSED;
5597 {
5598   pragma_interrupt = 1;
5599 }
5600
5601 void
5602 sh_pr_trapa (pfile)
5603      cpp_reader *pfile ATTRIBUTE_UNUSED;
5604 {
5605   pragma_interrupt = pragma_trapa = 1;
5606 }
5607
5608 void
5609 sh_pr_nosave_low_regs (pfile)
5610      cpp_reader *pfile ATTRIBUTE_UNUSED;
5611 {
5612   pragma_nosave_low_regs = 1;
5613 }
5614
5615 /* Generate 'handle_interrupt' attribute for decls */
5616
5617 static void
5618 sh_insert_attributes (node, attributes)
5619      tree node;
5620      tree * attributes;
5621 {
5622   if (! pragma_interrupt
5623       || TREE_CODE (node) != FUNCTION_DECL)
5624     return;
5625
5626   /* We are only interested in fields.  */
5627   if (TREE_CODE_CLASS (TREE_CODE (node)) != 'd')
5628     return;
5629
5630   /* Add a 'handle_interrupt' attribute.  */
5631   * attributes = tree_cons (get_identifier ("interrupt_handler"), NULL, * attributes);
5632
5633   return;
5634 }
5635
5636 /* Supported attributes:
5637
5638    interrupt_handler -- specifies this function is an interrupt handler.
5639
5640    sp_switch -- specifies an alternate stack for an interrupt handler
5641    to run on.
5642
5643    trap_exit -- use a trapa to exit an interrupt function instead of
5644    an rte instruction.  */
5645
5646 const struct attribute_spec sh_attribute_table[] =
5647 {
5648   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
5649   { "interrupt_handler", 0, 0, true,  false, false, sh_handle_interrupt_handler_attribute },
5650   { "sp_switch",         1, 1, true,  false, false, sh_handle_sp_switch_attribute },
5651   { "trap_exit",         1, 1, true,  false, false, sh_handle_trap_exit_attribute },
5652   { NULL,                0, 0, false, false, false, NULL }
5653 };
5654
5655 /* Handle an "interrupt_handler" attribute; arguments as in
5656    struct attribute_spec.handler.  */
5657 static tree
5658 sh_handle_interrupt_handler_attribute (node, name, args, flags, no_add_attrs)
5659      tree *node;
5660      tree name;
5661      tree args ATTRIBUTE_UNUSED;
5662      int flags ATTRIBUTE_UNUSED;
5663      bool *no_add_attrs;
5664 {
5665   if (TREE_CODE (*node) != FUNCTION_DECL)
5666     {
5667       warning ("`%s' attribute only applies to functions",
5668                IDENTIFIER_POINTER (name));
5669       *no_add_attrs = true;
5670     }
5671
5672   return NULL_TREE;
5673 }
5674
5675 /* Handle an "sp_switch" attribute; arguments as in
5676    struct attribute_spec.handler.  */
5677 static tree
5678 sh_handle_sp_switch_attribute (node, name, args, flags, no_add_attrs)
5679      tree *node;
5680      tree name;
5681      tree args;
5682      int flags ATTRIBUTE_UNUSED;
5683      bool *no_add_attrs;
5684 {
5685   if (TREE_CODE (*node) != FUNCTION_DECL)
5686     {
5687       warning ("`%s' attribute only applies to functions",
5688                IDENTIFIER_POINTER (name));
5689       *no_add_attrs = true;
5690     }
5691   else if (!pragma_interrupt)
5692     {
5693       /* The sp_switch attribute only has meaning for interrupt functions.  */
5694       warning ("`%s' attribute only applies to interrupt functions",
5695                IDENTIFIER_POINTER (name));
5696       *no_add_attrs = true;
5697     }
5698   else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
5699     {
5700       /* The argument must be a constant string.  */
5701       warning ("`%s' attribute argument not a string constant",
5702                IDENTIFIER_POINTER (name));
5703       *no_add_attrs = true;
5704     }
5705   else
5706     {
5707       sp_switch = gen_rtx_SYMBOL_REF (VOIDmode,
5708                                       TREE_STRING_POINTER (TREE_VALUE (args)));
5709     }
5710
5711   return NULL_TREE;
5712 }
5713
5714 /* Handle an "trap_exit" attribute; arguments as in
5715    struct attribute_spec.handler.  */
5716 static tree
5717 sh_handle_trap_exit_attribute (node, name, args, flags, no_add_attrs)
5718      tree *node;
5719      tree name;
5720      tree args;
5721      int flags ATTRIBUTE_UNUSED;
5722      bool *no_add_attrs;
5723 {
5724   if (TREE_CODE (*node) != FUNCTION_DECL)
5725     {
5726       warning ("`%s' attribute only applies to functions",
5727                IDENTIFIER_POINTER (name));
5728       *no_add_attrs = true;
5729     }
5730   else if (!pragma_interrupt)
5731     {
5732       /* The trap_exit attribute only has meaning for interrupt functions.  */
5733       warning ("`%s' attribute only applies to interrupt functions",
5734                IDENTIFIER_POINTER (name));
5735       *no_add_attrs = true;
5736     }
5737   else if (TREE_CODE (TREE_VALUE (args)) != INTEGER_CST)
5738     {
5739       /* The argument must be a constant integer.  */
5740       warning ("`%s' attribute argument not an integer constant",
5741                IDENTIFIER_POINTER (name));
5742       *no_add_attrs = true;
5743     }
5744   else
5745     {
5746       trap_exit = TREE_INT_CST_LOW (TREE_VALUE (args));
5747     }
5748
5749   return NULL_TREE;
5750 }
5751
5752 int
5753 sh_cfun_interrupt_handler_p (void)
5754 {
5755   return (lookup_attribute ("interrupt_handler",
5756                             DECL_ATTRIBUTES (current_function_decl))
5757           != NULL_TREE);
5758 }
5759 \f
5760 /* Predicates used by the templates.  */
5761
5762 /* Returns 1 if OP is MACL, MACH or PR.  The input must be a REG rtx.
5763    Used only in general_movsrc_operand.  */
5764
5765 int
5766 system_reg_operand (op, mode)
5767      rtx op;
5768      enum machine_mode mode ATTRIBUTE_UNUSED;
5769 {
5770   switch (REGNO (op))
5771     {
5772     case PR_REG:
5773     case MACL_REG:
5774     case MACH_REG:
5775       return 1;
5776     }
5777   return 0;
5778 }
5779
5780 /* Returns 1 if OP can be source of a simple move operation.
5781    Same as general_operand, but a LABEL_REF is valid, PRE_DEC is
5782    invalid as are subregs of system registers.  */
5783
5784 int
5785 general_movsrc_operand (op, mode)
5786      rtx op;
5787      enum machine_mode mode;
5788 {
5789   if (GET_CODE (op) == MEM)
5790     {
5791       rtx inside = XEXP (op, 0);
5792       if (GET_CODE (inside) == CONST)
5793         inside = XEXP (inside, 0);
5794
5795       if (GET_CODE (inside) == LABEL_REF)
5796         return 1;
5797
5798       if (GET_CODE (inside) == PLUS
5799           && GET_CODE (XEXP (inside, 0)) == LABEL_REF
5800           && GET_CODE (XEXP (inside, 1)) == CONST_INT)
5801         return 1;
5802
5803       /* Only post inc allowed.  */
5804       if (GET_CODE (inside) == PRE_DEC)
5805         return 0;
5806     }
5807
5808   if ((mode == QImode || mode == HImode)
5809       && (GET_CODE (op) == SUBREG
5810           && GET_CODE (XEXP (op, 0)) == REG
5811           && system_reg_operand (XEXP (op, 0), mode)))
5812     return 0;
5813
5814   return general_operand (op, mode);
5815 }
5816
5817 /* Returns 1 if OP can be a destination of a move.
5818    Same as general_operand, but no preinc allowed.  */
5819
5820 int
5821 general_movdst_operand (op, mode)
5822      rtx op;
5823      enum machine_mode mode;
5824 {
5825   /* Only pre dec allowed.  */
5826   if (GET_CODE (op) == MEM && GET_CODE (XEXP (op, 0)) == POST_INC)
5827     return 0;
5828
5829   return general_operand (op, mode);
5830 }
5831
5832 /* Returns 1 if OP is a normal arithmetic register.  */
5833
5834 int
5835 arith_reg_operand (op, mode)
5836      rtx op;
5837      enum machine_mode mode;
5838 {
5839   if (register_operand (op, mode))
5840     {
5841       int regno;
5842
5843       if (GET_CODE (op) == REG)
5844         regno = REGNO (op);
5845       else if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
5846         regno = REGNO (SUBREG_REG (op));
5847       else
5848         return 1;
5849
5850       return (regno != T_REG && regno != PR_REG
5851               && ! TARGET_REGISTER_P (regno)
5852               && (regno != FPUL_REG || TARGET_SH4)
5853               && regno != MACH_REG && regno != MACL_REG);
5854     }
5855   return 0;
5856 }
5857
5858 /* Like above, but for DImode destinations: forbid paradoxical DImode subregs,
5859    because this would lead to missing sign extensions when truncating from
5860    DImode to SImode.  */
5861 int
5862 arith_reg_dest (op, mode)
5863      rtx op;
5864      enum machine_mode mode;
5865 {
5866   if (mode == DImode && GET_CODE (op) == SUBREG
5867       && GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))) < 8)
5868     return 0;
5869   return arith_reg_operand (op, mode);
5870 }
5871
5872 int
5873 int_gpr_dest (op, mode)
5874      rtx op;
5875      enum machine_mode mode ATTRIBUTE_UNUSED;
5876 {
5877   enum machine_mode op_mode = GET_MODE (op);
5878
5879   if (GET_MODE_CLASS (op_mode) != MODE_INT
5880       || GET_MODE_SIZE (op_mode) >= UNITS_PER_WORD)
5881     return 0;
5882   if (! reload_completed)
5883     return 0;
5884   return true_regnum (op) <= LAST_GENERAL_REG;
5885 }
5886
5887 int
5888 fp_arith_reg_operand (op, mode)
5889      rtx op;
5890      enum machine_mode mode;
5891 {
5892   if (register_operand (op, mode))
5893     {
5894       int regno;
5895
5896       if (GET_CODE (op) == REG)
5897         regno = REGNO (op);
5898       else if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == REG)
5899         regno = REGNO (SUBREG_REG (op));
5900       else
5901         return 1;
5902
5903       return (regno >= FIRST_PSEUDO_REGISTER
5904               || FP_REGISTER_P (regno));
5905     }
5906   return 0;
5907 }
5908
5909 /* Returns 1 if OP is a valid source operand for an arithmetic insn.  */
5910
5911 int
5912 arith_operand (op, mode)
5913      rtx op;
5914      enum machine_mode mode;
5915 {
5916   if (arith_reg_operand (op, mode))
5917     return 1;
5918
5919   if (TARGET_SHMEDIA)
5920     {
5921       /* FIXME: We should be checking whether the CONST_INT fits in a
5922          CONST_OK_FOR_J here, but this causes reload_cse to crash when
5923          attempting to transform a sequence of two 64-bit sets of the
5924          same register from literal constants into a set and an add,
5925          when the difference is too wide for an add.  */
5926       if (GET_CODE (op) == CONST_INT
5927           || EXTRA_CONSTRAINT_S (op))
5928         return 1;
5929       else
5930         return 0;
5931     }
5932   else if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_I (INTVAL (op)))
5933     return 1;
5934
5935   return 0;
5936 }
5937
5938 /* Returns 1 if OP is a valid source operand for a compare insn.  */
5939
5940 int
5941 arith_reg_or_0_operand (op, mode)
5942      rtx op;
5943      enum machine_mode mode;
5944 {
5945   if (arith_reg_operand (op, mode))
5946     return 1;
5947
5948   if (EXTRA_CONSTRAINT_U (op))
5949     return 1;
5950
5951   return 0;
5952 }
5953
5954 /* Return 1 if OP is a valid source operand for an SHmedia operation
5955    that takes either a register or a 6-bit immediate.  */
5956
5957 int
5958 shmedia_6bit_operand (op, mode)
5959      rtx op;
5960      enum machine_mode mode;
5961 {
5962   return (arith_reg_operand (op, mode)
5963           || (GET_CODE (op) == CONST_INT && CONST_OK_FOR_O (INTVAL (op))));
5964 }
5965
5966 /* Returns 1 if OP is a valid source operand for a logical operation.  */
5967
5968 int
5969 logical_operand (op, mode)
5970      rtx op;
5971      enum machine_mode mode;
5972 {
5973   if (arith_reg_operand (op, mode))
5974     return 1;
5975
5976   if (TARGET_SHMEDIA)
5977     {
5978       if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_P (INTVAL (op)))
5979         return 1;
5980       else
5981         return 0;
5982     }
5983   else if (GET_CODE (op) == CONST_INT && CONST_OK_FOR_L (INTVAL (op)))
5984     return 1;
5985
5986   return 0;
5987 }
5988
5989 int
5990 and_operand (op, mode)
5991      rtx op;
5992      enum machine_mode mode;
5993 {
5994   if (logical_operand (op, mode))
5995     return 1;
5996
5997   /* Check mshflo.l / mshflhi.l opportunities.  */
5998   if (TARGET_SHMEDIA
5999       && mode == DImode
6000       && GET_CODE (op) == CONST_INT
6001       && (INTVAL (op) == (unsigned) 0xffffffff
6002           || INTVAL (op) == (HOST_WIDE_INT) -1 << 32))
6003         return 1;
6004
6005   return 0;
6006 }
6007
6008 /* Nonzero if OP is a floating point value with value 0.0.  */
6009
6010 int
6011 fp_zero_operand (op)
6012      rtx op;
6013 {
6014   REAL_VALUE_TYPE r;
6015
6016   if (GET_MODE (op) != SFmode)
6017     return 0;
6018
6019   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
6020   return REAL_VALUES_EQUAL (r, dconst0) && ! REAL_VALUE_MINUS_ZERO (r);
6021 }
6022
6023 /* Nonzero if OP is a floating point value with value 1.0.  */
6024
6025 int
6026 fp_one_operand (op)
6027      rtx op;
6028 {
6029   REAL_VALUE_TYPE r;
6030
6031   if (GET_MODE (op) != SFmode)
6032     return 0;
6033
6034   REAL_VALUE_FROM_CONST_DOUBLE (r, op);
6035   return REAL_VALUES_EQUAL (r, dconst1);
6036 }
6037
6038 /* For -m4 and -m4-single-only, mode switching is used.  If we are
6039    compiling without -mfmovd, movsf_ie isn't taken into account for
6040    mode switching.  We could check in machine_dependent_reorg for
6041    cases where we know we are in single precision mode, but there is
6042    interface to find that out during reload, so we must avoid
6043    choosing an fldi alternative during reload and thus failing to
6044    allocate a scratch register for the constant loading.  */
6045 int
6046 fldi_ok ()
6047 {
6048   return ! TARGET_SH4 || TARGET_FMOVD || reload_completed;
6049 }
6050
6051 int
6052 tertiary_reload_operand (op, mode)
6053      rtx op;
6054      enum machine_mode mode ATTRIBUTE_UNUSED;
6055 {
6056   enum rtx_code code = GET_CODE (op);
6057   return code == MEM || (TARGET_SH4 && code == CONST_DOUBLE);
6058 }
6059
6060 int
6061 fpscr_operand (op, mode)
6062      rtx op;
6063      enum machine_mode mode ATTRIBUTE_UNUSED;
6064 {
6065   return (GET_CODE (op) == REG && REGNO (op) == FPSCR_REG
6066           && GET_MODE (op) == PSImode);
6067 }
6068
6069 int
6070 fpul_operand (op, mode)
6071      rtx op;
6072      enum machine_mode mode;
6073 {
6074   if (TARGET_SHMEDIA)
6075     return fp_arith_reg_operand (op, mode);
6076
6077   return (GET_CODE (op) == REG
6078           && (REGNO (op) == FPUL_REG || REGNO (op) >= FIRST_PSEUDO_REGISTER)
6079           && GET_MODE (op) == mode);
6080 }
6081
6082 int
6083 symbol_ref_operand (op, mode)
6084      rtx op;
6085      enum machine_mode mode ATTRIBUTE_UNUSED;
6086 {
6087   return (GET_CODE (op) == SYMBOL_REF);
6088 }
6089
6090 int
6091 commutative_float_operator (op, mode)
6092      rtx op;
6093      enum machine_mode mode;
6094 {
6095   if (GET_MODE (op) != mode)
6096     return 0;
6097   switch (GET_CODE (op))
6098     {
6099     case PLUS:
6100     case MULT:
6101       return 1;
6102     default:
6103       break;
6104     }
6105   return 0;
6106 }
6107
6108 int
6109 noncommutative_float_operator (op, mode)
6110      rtx op;
6111      enum machine_mode mode;
6112 {
6113   if (GET_MODE (op) != mode)
6114     return 0;
6115   switch (GET_CODE (op))
6116     {
6117     case MINUS:
6118     case DIV:
6119       return 1;
6120     default:
6121       break;
6122     }
6123   return 0;
6124 }
6125
6126 int
6127 unary_float_operator (op, mode)
6128      rtx op;
6129      enum machine_mode mode;
6130 {
6131   if (GET_MODE (op) != mode)
6132     return 0;
6133   switch (GET_CODE (op))
6134     {
6135     case ABS:
6136     case NEG:
6137     case SQRT:
6138       return 1;
6139     default:
6140       break;
6141     }
6142   return 0;
6143 }
6144
6145 int
6146 binary_float_operator (op, mode)
6147      rtx op;
6148      enum machine_mode mode;
6149 {
6150   if (GET_MODE (op) != mode)
6151     return 0;
6152   switch (GET_CODE (op))
6153     {
6154     case PLUS:
6155     case MINUS:
6156     case MULT:
6157     case DIV:
6158       return 1;
6159     default:
6160       break;
6161     }
6162   return 0;
6163 }
6164
6165 int
6166 equality_comparison_operator (op, mode)
6167      rtx op;
6168      enum machine_mode mode;
6169 {
6170   return ((mode == VOIDmode || GET_MODE (op) == mode)
6171           && (GET_CODE (op) == EQ || GET_CODE (op) == NE));
6172 }
6173
6174 int greater_comparison_operator (op, mode)
6175      rtx op;
6176      enum machine_mode mode;
6177 {
6178   if (mode != VOIDmode && GET_MODE (op) == mode)
6179     return 0;
6180   switch (GET_CODE (op))
6181     {
6182     case GT:
6183     case GE:
6184     case GTU:
6185     case GEU:
6186       return 1;
6187     default:
6188       return 0;
6189     }
6190 }
6191
6192 int less_comparison_operator (op, mode)
6193      rtx op;
6194      enum machine_mode mode;
6195 {
6196   if (mode != VOIDmode && GET_MODE (op) == mode)
6197     return 0;
6198   switch (GET_CODE (op))
6199     {
6200     case LT:
6201     case LE:
6202     case LTU:
6203     case LEU:
6204       return 1;
6205     default:
6206       return 0;
6207     }
6208 }
6209
6210 /* Accept pseudos and branch target registers.  */
6211 int
6212 target_reg_operand (op, mode)
6213      rtx op;
6214      enum machine_mode mode;
6215 {
6216   if (mode != DImode
6217       || GET_MODE (op) != DImode)
6218     return 0;
6219
6220   if (GET_CODE (op) == SUBREG)
6221     op = XEXP (op, 0);
6222
6223   if (GET_CODE (op) != REG)
6224     return 0;
6225
6226   /* We must protect ourselves from matching pseudos that are virtual
6227      register, because they will eventually be replaced with hardware
6228      registers that aren't branch-target registers.  */
6229   if (REGNO (op) > LAST_VIRTUAL_REGISTER
6230       || TARGET_REGISTER_P (REGNO (op)))
6231     return 1;
6232
6233   return 0;
6234 }
6235
6236 /* Same as target_reg_operand, except that label_refs and symbol_refs
6237    are accepted before reload.  */
6238 int
6239 target_operand (op, mode)
6240      rtx op;
6241      enum machine_mode mode;
6242 {
6243   if (mode != DImode)
6244     return 0;
6245
6246   if ((GET_MODE (op) == DImode || GET_MODE (op) == VOIDmode)
6247       && EXTRA_CONSTRAINT_T (op))
6248     return ! reload_completed;
6249
6250   return target_reg_operand (op, mode);
6251 }
6252
6253 int
6254 mextr_bit_offset (op, mode)
6255      rtx op;
6256      enum machine_mode mode ATTRIBUTE_UNUSED;
6257 {
6258   HOST_WIDE_INT i;
6259
6260   if (GET_CODE (op) != CONST_INT)
6261     return 0;
6262   i = INTVAL (op);
6263   return i >= 1*8 && i <= 7*8 && (i & 7) == 0;
6264 }
6265
6266 int
6267 extend_reg_operand (op, mode)
6268      rtx op;
6269      enum machine_mode mode;
6270 {
6271   return (GET_CODE (op) == TRUNCATE
6272           ? arith_operand
6273           : arith_reg_operand) (op, mode);
6274 }
6275
6276 int
6277 trunc_hi_operand (op, mode)
6278      rtx op;
6279      enum machine_mode mode;
6280 {
6281   enum machine_mode op_mode = GET_MODE (op);
6282
6283   if (op_mode != SImode && op_mode != DImode
6284       && op_mode != V4HImode && op_mode != V2SImode)
6285     return 0;
6286   return extend_reg_operand (op, mode);
6287 }
6288
6289 int
6290 extend_reg_or_0_operand (op, mode)
6291      rtx op;
6292      enum machine_mode mode;
6293 {
6294   return (GET_CODE (op) == TRUNCATE
6295           ? arith_operand
6296           : arith_reg_or_0_operand) (op, mode);
6297 }
6298
6299 int
6300 general_extend_operand (op, mode)
6301      rtx op;
6302      enum machine_mode mode;
6303 {
6304   return (GET_CODE (op) == TRUNCATE
6305           ? arith_operand
6306           : nonimmediate_operand) (op, mode);
6307 }
6308
6309 int
6310 inqhi_operand (op, mode)
6311      rtx op;
6312      enum machine_mode mode;
6313 {
6314   if (GET_CODE (op) != TRUNCATE || mode != GET_MODE (op))
6315     return 0;
6316   op = XEXP (op, 0);
6317   /* Can't use true_regnum here because copy_cost wants to know about
6318      SECONDARY_INPUT_RELOAD_CLASS.  */
6319   return GET_CODE (op) == REG && FP_REGISTER_P (REGNO (op));
6320 }
6321
6322 int
6323 sh_rep_vec (v, mode)
6324      rtx v;
6325      enum machine_mode mode;
6326 {
6327   int i;
6328   rtx x, y;
6329
6330   if ((GET_CODE (v) != CONST_VECTOR && GET_CODE (v) != PARALLEL)
6331       || (GET_MODE (v) != mode && mode != VOIDmode))
6332     return 0;
6333   i = XVECLEN (v, 0) - 2;
6334   x = XVECEXP (v, 0, i + 1);
6335   if (GET_MODE_UNIT_SIZE (mode) == 1)
6336     {
6337       y = XVECEXP (v, 0, i);
6338       for (i -= 2 ; i >= 0; i -= 2)
6339         if (! rtx_equal_p (XVECEXP (v, 0, i + 1), x)
6340             || ! rtx_equal_p (XVECEXP (v, 0, i), y))
6341           return 0;
6342     }
6343   else
6344     for (; i >= 0; i--)
6345       if (XVECEXP (v, 0, i) != x)
6346         return 0;
6347   return 1;
6348 }
6349
6350 /* Determine if V is a constant vector matching MODE with only one element
6351    that is not a sign extension.  Two byte-sized elements count as one.  */
6352 int
6353 sh_1el_vec (v, mode)
6354      rtx v;
6355      enum machine_mode mode;
6356 {
6357   int unit_size;
6358   int i, last, least, sign_ix;
6359   rtx sign;
6360
6361   if (GET_CODE (v) != CONST_VECTOR
6362       || (GET_MODE (v) != mode && mode != VOIDmode))
6363     return 0;
6364   /* Determine numbers of last and of least significat elements.  */
6365   last = XVECLEN (v, 0) - 1;
6366   least = TARGET_LITTLE_ENDIAN ? 0 : last;
6367   if (GET_CODE (XVECEXP (v, 0, least)) != CONST_INT)
6368     return 0;
6369   sign_ix = least;
6370   if (GET_MODE_UNIT_SIZE (mode) == 1)
6371     sign_ix = TARGET_LITTLE_ENDIAN ? 1 : last - 1;
6372   if (GET_CODE (XVECEXP (v, 0, sign_ix)) != CONST_INT)
6373     return 0;
6374   unit_size = GET_MODE_UNIT_SIZE (GET_MODE (v));
6375   sign = (INTVAL (XVECEXP (v, 0, sign_ix)) >> (unit_size * BITS_PER_UNIT - 1)
6376           ? constm1_rtx : const0_rtx);
6377   i = XVECLEN (v, 0) - 1;
6378   do
6379     if (i != least && i != sign_ix && XVECEXP (v, 0, i) != sign)
6380       return 0;
6381   while (--i);
6382   return 1;
6383 }
6384
6385 int
6386 sh_const_vec (v, mode)
6387      rtx v;
6388      enum machine_mode mode;
6389 {
6390   int i;
6391
6392   if (GET_CODE (v) != CONST_VECTOR
6393       || (GET_MODE (v) != mode && mode != VOIDmode))
6394     return 0;
6395   i = XVECLEN (v, 0) - 1;
6396   for (; i >= 0; i--)
6397     if (GET_CODE (XVECEXP (v, 0, i)) != CONST_INT)
6398       return 0;
6399   return 1;
6400 }
6401 \f
6402 /* Return the destination address of a branch.  */
6403    
6404 static int
6405 branch_dest (branch)
6406      rtx branch;
6407 {
6408   rtx dest = SET_SRC (PATTERN (branch));
6409   int dest_uid;
6410
6411   if (GET_CODE (dest) == IF_THEN_ELSE)
6412     dest = XEXP (dest, 1);
6413   dest = XEXP (dest, 0);
6414   dest_uid = INSN_UID (dest);
6415   return INSN_ADDRESSES (dest_uid);
6416 }
6417 \f
6418 /* Return non-zero if REG is not used after INSN.
6419    We assume REG is a reload reg, and therefore does
6420    not live past labels.  It may live past calls or jumps though.  */
6421 int
6422 reg_unused_after (reg, insn)
6423      rtx reg;
6424      rtx insn;
6425 {
6426   enum rtx_code code;
6427   rtx set;
6428
6429   /* If the reg is set by this instruction, then it is safe for our
6430      case.  Disregard the case where this is a store to memory, since
6431      we are checking a register used in the store address.  */
6432   set = single_set (insn);
6433   if (set && GET_CODE (SET_DEST (set)) != MEM
6434       && reg_overlap_mentioned_p (reg, SET_DEST (set)))
6435     return 1;
6436
6437   while ((insn = NEXT_INSN (insn)))
6438     {
6439       code = GET_CODE (insn);
6440
6441 #if 0
6442       /* If this is a label that existed before reload, then the register
6443          if dead here.  However, if this is a label added by reorg, then
6444          the register may still be live here.  We can't tell the difference,
6445          so we just ignore labels completely.  */
6446       if (code == CODE_LABEL)
6447         return 1;
6448       /* else */
6449 #endif
6450
6451       if (code == JUMP_INSN)
6452         return 0;
6453
6454       /* If this is a sequence, we must handle them all at once.
6455          We could have for instance a call that sets the target register,
6456          and an insn in a delay slot that uses the register.  In this case,
6457          we must return 0.  */
6458       else if (code == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
6459         {
6460           int i;
6461           int retval = 0;
6462
6463           for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
6464             {
6465               rtx this_insn = XVECEXP (PATTERN (insn), 0, i);
6466               rtx set = single_set (this_insn);
6467
6468               if (GET_CODE (this_insn) == CALL_INSN)
6469                 code = CALL_INSN;
6470               else if (GET_CODE (this_insn) == JUMP_INSN)
6471                 {
6472                   if (INSN_ANNULLED_BRANCH_P (this_insn))
6473                     return 0;
6474                   code = JUMP_INSN;
6475                 }
6476
6477               if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
6478                 return 0;
6479               if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
6480                 {
6481                   if (GET_CODE (SET_DEST (set)) != MEM)
6482                     retval = 1;
6483                   else
6484                     return 0;
6485                 }
6486               if (set == 0
6487                   && reg_overlap_mentioned_p (reg, PATTERN (this_insn)))
6488                 return 0;
6489             }
6490           if (retval == 1)
6491             return 1;
6492           else if (code == JUMP_INSN)
6493             return 0;
6494         }
6495       else if (GET_RTX_CLASS (code) == 'i')
6496         {
6497           rtx set = single_set (insn);
6498
6499           if (set && reg_overlap_mentioned_p (reg, SET_SRC (set)))
6500             return 0;
6501           if (set && reg_overlap_mentioned_p (reg, SET_DEST (set)))
6502             return GET_CODE (SET_DEST (set)) != MEM;
6503           if (set == 0 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
6504             return 0;
6505         }
6506
6507       if (code == CALL_INSN && call_used_regs[REGNO (reg)])
6508         return 1;
6509     }
6510   return 1;
6511 }
6512 \f
6513 #include "ggc.h"
6514
6515 static GTY(()) rtx fpscr_rtx;
6516 rtx
6517 get_fpscr_rtx ()
6518 {
6519   if (! fpscr_rtx)
6520     {
6521       fpscr_rtx = gen_rtx (REG, PSImode, FPSCR_REG);
6522       REG_USERVAR_P (fpscr_rtx) = 1;
6523       mark_user_reg (fpscr_rtx);
6524     }
6525   if (! reload_completed || mdep_reorg_phase != SH_AFTER_MDEP_REORG)
6526     mark_user_reg (fpscr_rtx);
6527   return fpscr_rtx;
6528 }
6529
6530 void
6531 emit_sf_insn (pat)
6532      rtx pat;
6533 {
6534   emit_insn (pat);
6535 }
6536
6537 void
6538 emit_df_insn (pat)
6539      rtx pat;
6540 {
6541   emit_insn (pat);
6542 }
6543
6544 void
6545 expand_sf_unop (fun, operands)
6546      rtx (*fun) PARAMS ((rtx, rtx, rtx));
6547      rtx *operands;
6548 {
6549   emit_sf_insn ((*fun) (operands[0], operands[1], get_fpscr_rtx ()));
6550 }
6551
6552 void
6553 expand_sf_binop (fun, operands)
6554      rtx (*fun) PARAMS ((rtx, rtx, rtx, rtx));
6555      rtx *operands;
6556 {
6557   emit_sf_insn ((*fun) (operands[0], operands[1], operands[2],
6558                          get_fpscr_rtx ()));
6559 }
6560
6561 void
6562 expand_df_unop (fun, operands)
6563      rtx (*fun) PARAMS ((rtx, rtx, rtx));
6564      rtx *operands;
6565 {
6566   emit_df_insn ((*fun) (operands[0], operands[1], get_fpscr_rtx ()));
6567 }
6568
6569 void
6570 expand_df_binop (fun, operands)
6571      rtx (*fun) PARAMS ((rtx, rtx, rtx, rtx));
6572      rtx *operands;
6573 {
6574   emit_df_insn ((*fun) (operands[0], operands[1], operands[2],
6575                          get_fpscr_rtx ()));
6576 }
6577 \f
6578 /* ??? gcc does flow analysis strictly after common subexpression
6579    elimination.  As a result, common subespression elimination fails
6580    when there are some intervening statements setting the same register.
6581    If we did nothing about this, this would hurt the precision switching
6582    for SH4 badly.  There is some cse after reload, but it is unable to
6583    undo the extra register pressure from the unused instructions, and
6584    it cannot remove auto-increment loads.
6585
6586    A C code example that shows this flow/cse weakness for (at least) SH
6587    and sparc (as of gcc ss-970706) is this:
6588
6589 double
6590 f(double a)
6591 {
6592   double d;
6593   d = 0.1;
6594   a += d;
6595   d = 1.1;
6596   d = 0.1;
6597   a *= d;
6598   return a;
6599 }
6600
6601    So we add another pass before common subexpression elimination, to
6602    remove assignments that are dead due to a following assignment in the
6603    same basic block.  */
6604
6605 static void 
6606 mark_use (x, reg_set_block)
6607      rtx x, *reg_set_block;
6608 {
6609   enum rtx_code code;
6610
6611   if (! x)
6612     return;
6613   code = GET_CODE (x);
6614   switch (code)
6615     {
6616     case REG:
6617       {
6618         int regno = REGNO (x);
6619         int nregs = (regno < FIRST_PSEUDO_REGISTER
6620                      ? HARD_REGNO_NREGS (regno, GET_MODE (x))
6621                      : 1);
6622         do
6623           {
6624             reg_set_block[regno + nregs - 1] = 0;
6625           }
6626         while (--nregs);
6627         break;
6628       }
6629     case SET:
6630       {
6631         rtx dest = SET_DEST (x);
6632
6633         if (GET_CODE (dest) == SUBREG)
6634           dest = SUBREG_REG (dest);
6635         if (GET_CODE (dest) != REG)
6636           mark_use (dest, reg_set_block);
6637         mark_use (SET_SRC (x), reg_set_block);
6638         break;
6639       }
6640     case CLOBBER:
6641       break;
6642     default:
6643       {
6644         const char *fmt = GET_RTX_FORMAT (code);
6645         int i, j;
6646         for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6647           {
6648             if (fmt[i] == 'e')
6649               mark_use (XEXP (x, i), reg_set_block);
6650             else if (fmt[i] == 'E')
6651               for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6652                 mark_use (XVECEXP (x, i, j), reg_set_block);
6653           }
6654         break;
6655       }
6656     }
6657 }
6658 \f
6659 static rtx get_free_reg PARAMS ((HARD_REG_SET));
6660
6661 /* This function returns a register to use to load the address to load
6662    the fpscr from.  Currently it always returns r1 or r7, but when we are
6663    able to use pseudo registers after combine, or have a better mechanism
6664    for choosing a register, it should be done here.  */
6665 /* REGS_LIVE is the liveness information for the point for which we
6666    need this allocation.  In some bare-bones exit blocks, r1 is live at the
6667    start.  We can even have all of r0..r3 being live:
6668 __complex__ long long f (double d) { if (d == 0) return 2; else return 3; }
6669    INSN before which new insns are placed with will clobber the register
6670    we return.  If a basic block consists only of setting the return value
6671    register to a pseudo and using that register, the return value is not
6672    live before or after this block, yet we we'll insert our insns right in
6673    the middle.  */
6674
6675 static rtx
6676 get_free_reg (regs_live)
6677      HARD_REG_SET regs_live;
6678 {
6679   if (! TEST_HARD_REG_BIT (regs_live, 1))
6680     return gen_rtx_REG (Pmode, 1);
6681
6682   /* Hard reg 1 is live; since this is a SMALL_REGISTER_CLASSES target,
6683      there shouldn't be anything but a jump before the function end.  */
6684   if (! TEST_HARD_REG_BIT (regs_live, 7))
6685     return gen_rtx_REG (Pmode, 7);
6686
6687   abort ();
6688 }
6689
6690 /* This function will set the fpscr from memory. 
6691    MODE is the mode we are setting it to.  */
6692 void
6693 fpscr_set_from_mem (mode, regs_live)
6694      int mode;
6695      HARD_REG_SET regs_live;
6696 {
6697   enum attr_fp_mode fp_mode = mode;
6698   rtx addr_reg = get_free_reg (regs_live);
6699
6700   if (fp_mode == (enum attr_fp_mode) NORMAL_MODE (FP_MODE))
6701     emit_insn (gen_fpu_switch1 (addr_reg));
6702   else
6703     emit_insn (gen_fpu_switch0 (addr_reg));
6704 }
6705
6706 /* Is the given character a logical line separator for the assembler?  */
6707 #ifndef IS_ASM_LOGICAL_LINE_SEPARATOR
6708 #define IS_ASM_LOGICAL_LINE_SEPARATOR(C) ((C) == ';')
6709 #endif
6710
6711 int
6712 sh_insn_length_adjustment (insn)
6713      rtx insn;
6714 {
6715   /* Instructions with unfilled delay slots take up an extra two bytes for
6716      the nop in the delay slot.  */
6717   if (((GET_CODE (insn) == INSN
6718         && GET_CODE (PATTERN (insn)) != USE
6719         && GET_CODE (PATTERN (insn)) != CLOBBER)
6720        || GET_CODE (insn) == CALL_INSN
6721        || (GET_CODE (insn) == JUMP_INSN
6722            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
6723            && GET_CODE (PATTERN (insn)) != ADDR_VEC))
6724       && GET_CODE (PATTERN (NEXT_INSN (PREV_INSN (insn)))) != SEQUENCE
6725       && get_attr_needs_delay_slot (insn) == NEEDS_DELAY_SLOT_YES)
6726     return 2;
6727
6728   /* sh-dsp parallel processing insn take four bytes instead of two.  */
6729      
6730   if (GET_CODE (insn) == INSN)
6731     {
6732       int sum = 0;
6733       rtx body = PATTERN (insn);
6734       const char *template;
6735       char c;
6736       int maybe_label = 1;
6737
6738       if (GET_CODE (body) == ASM_INPUT)
6739         template = XSTR (body, 0);
6740       else if (asm_noperands (body) >= 0)
6741         template
6742           = decode_asm_operands (body, NULL, NULL, NULL, NULL);
6743       else
6744         return 0;
6745       do
6746         {
6747           int ppi_adjust = 0;
6748
6749           do
6750             c = *template++;
6751           while (c == ' ' || c == '\t');
6752           /* all sh-dsp parallel-processing insns start with p.
6753              The only non-ppi sh insn starting with p is pref.
6754              The only ppi starting with pr is prnd.  */
6755           if ((c == 'p' || c == 'P') && strncasecmp ("re", template, 2))
6756             ppi_adjust = 2;
6757           /* The repeat pseudo-insn expands two three insns, a total of
6758              six bytes in size.  */
6759           else if ((c == 'r' || c == 'R')
6760                    && ! strncasecmp ("epeat", template, 5))
6761             ppi_adjust = 4;
6762           while (c && c != '\n' && ! IS_ASM_LOGICAL_LINE_SEPARATOR (c))
6763             {
6764               /* If this is a label, it is obviously not a ppi insn.  */
6765               if (c == ':' && maybe_label)
6766                 {
6767                   ppi_adjust = 0;
6768                   break;
6769                 }
6770               else if (c == '\'' || c == '"')
6771                 maybe_label = 0;
6772               c = *template++;
6773             }
6774           sum += ppi_adjust;
6775           maybe_label = c != ':';
6776         }
6777       while (c);
6778       return sum;
6779     }
6780   return 0;
6781 }
6782 \f
6783 /* Return TRUE if X references a SYMBOL_REF or LABEL_REF whose symbol
6784    isn't protected by a PIC unspec.  */
6785 int
6786 nonpic_symbol_mentioned_p (x)
6787      rtx x;
6788 {
6789   register const char *fmt;
6790   register int i;
6791
6792   if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF
6793       || GET_CODE (x) == PC)
6794     return 1;
6795
6796   /* We don't want to look into the possible MEM location of a
6797      CONST_DOUBLE, since we're not going to use it, in general.  */
6798   if (GET_CODE (x) == CONST_DOUBLE)
6799     return 0;
6800
6801   if (GET_CODE (x) == UNSPEC
6802       && (XINT (x, 1) == UNSPEC_PIC
6803           || XINT (x, 1) == UNSPEC_GOT
6804           || XINT (x, 1) == UNSPEC_GOTOFF
6805           || XINT (x, 1) == UNSPEC_GOTPLT
6806           || XINT (x, 1) == UNSPEC_PLT))
6807       return 0;
6808
6809   fmt = GET_RTX_FORMAT (GET_CODE (x));
6810   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6811     {
6812       if (fmt[i] == 'E')
6813         {
6814           register int j;
6815
6816           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6817             if (nonpic_symbol_mentioned_p (XVECEXP (x, i, j)))
6818               return 1;
6819         }
6820       else if (fmt[i] == 'e' && nonpic_symbol_mentioned_p (XEXP (x, i)))
6821         return 1;
6822     }
6823
6824   return 0;
6825 }
6826
6827 /* Convert a non-PIC address in `orig' to a PIC address using @GOT or
6828    @GOTOFF in `reg'.  */
6829 rtx
6830 legitimize_pic_address (orig, mode, reg)
6831      rtx orig;
6832      enum machine_mode mode ATTRIBUTE_UNUSED;
6833      rtx reg;
6834 {
6835   if (GET_CODE (orig) == LABEL_REF
6836       || (GET_CODE (orig) == SYMBOL_REF
6837           && (CONSTANT_POOL_ADDRESS_P (orig)
6838               /* SYMBOL_REF_FLAG is set on static symbols.  */
6839               || SYMBOL_REF_FLAG (orig))))
6840     {
6841       if (reg == 0)
6842         reg = gen_reg_rtx (Pmode);
6843
6844       emit_insn (gen_symGOTOFF2reg (reg, orig));
6845       return reg;
6846     }
6847   else if (GET_CODE (orig) == SYMBOL_REF)
6848     {
6849       if (reg == 0)
6850         reg = gen_reg_rtx (Pmode);
6851
6852       emit_insn (gen_symGOT2reg (reg, orig));
6853       return reg;
6854     }
6855   return orig;
6856 }
6857
6858 /* Mark the use of a constant in the literal table. If the constant
6859    has multiple labels, make it unique.  */
6860 static rtx
6861 mark_constant_pool_use (x)
6862      rtx x;
6863 {
6864   rtx insn, lab, pattern;
6865
6866   if (x == NULL)
6867     return x;
6868
6869   switch (GET_CODE (x))
6870     {
6871     case LABEL_REF:
6872       x = XEXP (x, 0);
6873     case CODE_LABEL:
6874       break;
6875     default:
6876       return x;
6877     }
6878
6879   /* Get the first label in the list of labels for the same constant
6880      and delete another labels in the list.  */
6881   lab = x;
6882   for (insn = PREV_INSN (x); insn; insn = PREV_INSN (insn))
6883     {
6884       if (GET_CODE (insn) != CODE_LABEL
6885           || LABEL_REFS (insn) != NEXT_INSN (insn))
6886         break;
6887       lab = insn;
6888     }
6889
6890   for (insn = LABEL_REFS (lab); insn; insn = LABEL_REFS (insn))
6891     INSN_DELETED_P (insn) = 1;
6892
6893   /* Mark constants in a window.  */
6894   for (insn = NEXT_INSN (x); insn; insn = NEXT_INSN (insn))
6895     {
6896       if (GET_CODE (insn) != INSN)
6897         continue;
6898
6899       pattern = PATTERN (insn);
6900       if (GET_CODE (pattern) != UNSPEC_VOLATILE)
6901         continue;
6902
6903       switch (XINT (pattern, 1))
6904         {
6905         case UNSPECV_CONST2:
6906         case UNSPECV_CONST4:
6907         case UNSPECV_CONST8:
6908           XVECEXP (pattern, 0, 1) = const1_rtx;
6909           break;
6910         case UNSPECV_WINDOW_END:
6911           if (XVECEXP (pattern, 0, 0) == x)
6912             return lab;
6913           break;
6914         case UNSPECV_CONST_END:
6915           return lab;
6916         default:
6917           break;
6918         }
6919     }
6920
6921   return lab;
6922 }
6923 \f
6924 /* Return true if it's possible to redirect BRANCH1 to the destination
6925    of an unconditional jump BRANCH2.  We only want to do this if the
6926    resulting branch will have a short displacement.  */
6927 int 
6928 sh_can_redirect_branch (branch1, branch2)
6929      rtx branch1;
6930      rtx branch2;
6931 {
6932   if (flag_expensive_optimizations && simplejump_p (branch2))
6933     {
6934       rtx dest = XEXP (SET_SRC (single_set (branch2)), 0);
6935       rtx insn;
6936       int distance;
6937       
6938       for (distance = 0, insn = NEXT_INSN (branch1); 
6939            insn && distance < 256; 
6940            insn = PREV_INSN (insn))
6941         {
6942           if (insn == dest)    
6943             return 1;
6944           else
6945             distance += get_attr_length (insn);
6946         }
6947       for (distance = 0, insn = NEXT_INSN (branch1); 
6948            insn && distance < 256; 
6949            insn = NEXT_INSN (insn))
6950         {
6951           if (insn == dest)    
6952             return 1;
6953           else
6954             distance += get_attr_length (insn);
6955         }
6956     }
6957   return 0;
6958 }
6959
6960 /* Return non-zero if register old_reg can be renamed to register new_reg.  */
6961 int
6962 sh_hard_regno_rename_ok (old_reg, new_reg)
6963      unsigned int old_reg ATTRIBUTE_UNUSED;
6964      unsigned int new_reg;
6965 {
6966
6967 /* Interrupt functions can only use registers that have already been
6968    saved by the prologue, even if they would normally be
6969    call-clobbered.  */
6970
6971   if (sh_cfun_interrupt_handler_p () && !regs_ever_live[new_reg])
6972      return 0;
6973
6974    return 1;
6975 }
6976
6977 /* A C statement (sans semicolon) to update the integer variable COST
6978    based on the relationship between INSN that is dependent on
6979    DEP_INSN through the dependence LINK.  The default is to make no
6980    adjustment to COST.  This can be used for example to specify to
6981    the scheduler that an output- or anti-dependence does not incur
6982    the same cost as a data-dependence.  */
6983 static int
6984 sh_adjust_cost (insn, link, dep_insn, cost)
6985      rtx insn;
6986      rtx link ATTRIBUTE_UNUSED;
6987      rtx dep_insn;
6988      int cost;
6989 {
6990   rtx reg;
6991
6992   if (TARGET_SHMEDIA)
6993     {
6994       /* On SHmedia, if the dependence is an anti-dependence or
6995          output-dependence, there is no cost. */              
6996       if (REG_NOTE_KIND (link) != 0)
6997         cost = 0;
6998
6999       if (get_attr_is_mac_media (insn)
7000           && get_attr_is_mac_media (dep_insn))
7001         cost = 1;
7002     }
7003   else if (GET_CODE(insn) == CALL_INSN)
7004     {
7005       /* The only input for a call that is timing-critical is the
7006          function's address.  */
7007       rtx call = PATTERN (insn);
7008
7009       if (GET_CODE (call) == PARALLEL)
7010         call = XVECEXP (call, 0 ,0);
7011       if (GET_CODE (call) == SET)
7012         call = SET_SRC (call);
7013       if (GET_CODE (call) == CALL && GET_CODE (XEXP (call, 0)) == MEM
7014           && ! reg_set_p (XEXP (XEXP (call, 0), 0), dep_insn))
7015         cost = 0;
7016     }
7017   /* All sfunc calls are parallels with at least four components.
7018      Exploit this to avoid unnecessary calls to sfunc_uses_reg.  */
7019   else if (GET_CODE (PATTERN (insn)) == PARALLEL
7020            && XVECLEN (PATTERN (insn), 0) >= 4
7021            && (reg = sfunc_uses_reg (insn)))
7022     {
7023       /* Likewise, the most timing critical input for an sfuncs call
7024          is the function address.  However, sfuncs typically start
7025          using their arguments pretty quickly.
7026          Assume a four cycle delay before they are needed.  */
7027       if (! reg_set_p (reg, dep_insn))
7028         cost -= TARGET_SUPERSCALAR ? 40 : 4;
7029     }
7030   /* Adjust load_si / pcload_si type insns latency.  Use the known
7031      nominal latency and form of the insn to speed up the check.  */
7032   else if (cost == 3
7033            && GET_CODE (PATTERN (dep_insn)) == SET
7034            /* Latency for dmpy type insns is also 3, so check the that
7035               it's actually a move insn.  */
7036            && general_movsrc_operand (SET_SRC (PATTERN (dep_insn)), SImode))
7037     cost = 2;
7038   else if (cost == 30
7039            && GET_CODE (PATTERN (dep_insn)) == SET
7040            && GET_MODE (SET_SRC (PATTERN (dep_insn))) == SImode)
7041     cost = 20;
7042
7043   return cost;
7044 }
7045
7046 /* For use by ALLOCATE_INITIAL_VALUE.  Note that sh.md contains some
7047    'special function' patterns (type sfunc) that clobber pr, but that
7048    do not look like function calls to leaf_function_p.  Hence we must
7049    do this extra check.  */
7050 int
7051 sh_pr_n_sets ()
7052 {
7053   return REG_N_SETS (TARGET_SHMEDIA ? PR_MEDIA_REG : PR_REG);
7054 }
7055
7056 /* This Function Returns non zero if DFA based scheduler
7057    interface is to be used.At present supported only for
7058    SH4.  */
7059 static int
7060 sh_use_dfa_interface()
7061 {
7062         if (TARGET_SH4)
7063                 return 1;
7064         else
7065                 return 0;
7066 }
7067
7068 /* This function returns "2" that signifies dual issue 
7069    for SH4 processor.To be used by DFA pipeline description.  */
7070 static int
7071 sh_issue_rate()
7072 {
7073         if(TARGET_SH4)
7074                 return 2;
7075         else
7076                 return 1;
7077 }
7078
7079 /* SHmedia requires registers for branches, so we can't generate new
7080    branches past reload.  */
7081 static bool
7082 sh_cannot_modify_jumps_p ()
7083 {
7084   return (TARGET_SHMEDIA && (reload_in_progress || reload_completed));
7085 }
7086
7087 static bool
7088 sh_ms_bitfield_layout_p (record_type)
7089      tree record_type ATTRIBUTE_UNUSED;
7090 {
7091   return TARGET_SH5;
7092 }
7093
7094 /* If using PIC, mark a SYMBOL_REF for a non-global symbol so that we
7095    may access it using GOTOFF instead of GOT.  */
7096
7097 static void
7098 sh_encode_section_info (decl, first)
7099      tree decl;
7100      int first;
7101 {
7102   rtx rtl, symbol;
7103
7104   if (DECL_P (decl))
7105     rtl = DECL_RTL (decl);
7106   else
7107     rtl = TREE_CST_RTL (decl);
7108   if (GET_CODE (rtl) != MEM)
7109     return;
7110   symbol = XEXP (rtl, 0);
7111   if (GET_CODE (symbol) != SYMBOL_REF)
7112     return;
7113
7114   if (flag_pic)
7115     SYMBOL_REF_FLAG (symbol) = (*targetm.binds_local_p) (decl);
7116
7117   if (TARGET_SH5 && first && TREE_CODE (decl) != FUNCTION_DECL)
7118     XEXP (rtl, 0) = gen_datalabel_ref (symbol);
7119 }
7120
7121 /* Undo the effects of the above.  */
7122
7123 static const char *
7124 sh_strip_name_encoding (str)
7125      const char *str;
7126 {
7127   STRIP_DATALABEL_ENCODING (str, str);
7128   str += *str == '*';
7129   return str;
7130 }
7131
7132 \f
7133 /* 
7134    On the SH1..SH4, the trampoline looks like
7135    2 0002 D202                  mov.l   l2,r2
7136    1 0000 D301                  mov.l   l1,r3
7137    3 0004 422B                  jmp     @r2
7138    4 0006 0009                  nop
7139    5 0008 00000000      l1:     .long   area
7140    6 000c 00000000      l2:     .long   function
7141
7142    SH5 (compact) uses r1 instead of r3 for the static chain.  */
7143
7144
7145 /* Emit RTL insns to initialize the variable parts of a trampoline.
7146    FNADDR is an RTX for the address of the function's pure code.
7147    CXT is an RTX for the static chain value for the function.  */
7148
7149 void
7150 sh_initialize_trampoline (tramp, fnaddr, cxt)
7151      rtx tramp, fnaddr, cxt;
7152 {
7153   if (TARGET_SHMEDIA64)
7154     {
7155       rtx tramp_templ;
7156       int fixed_len;
7157
7158       rtx movi1 = GEN_INT (0xcc000010);
7159       rtx shori1 = GEN_INT (0xc8000010);
7160       rtx src, dst;
7161
7162       /* The following trampoline works within a +- 128 KB range for cxt:
7163          ptb/u cxt,tr1; movi fnaddr >> 48,r0; shori fnaddr >> 32,r0;
7164          shori fnaddr >> 16,r0; shori fnaddr,r0; ptabs/l r0,tr0
7165          gettr tr1,r1; blink tr0,r63  */
7166       /* Address rounding makes it hard to compute the exact bounds of the
7167          offset for this trampoline, but we have a rather generous offset
7168          range, so frame_offset should do fine as an upper bound.  */
7169       if (cxt == virtual_stack_vars_rtx && frame_offset < 0x20000)
7170         {
7171           /* ??? could optimize this trampoline initialization
7172              by writing DImode words with two insns each.  */
7173           rtx mask = force_reg (DImode, GEN_INT (0x3fffc00));
7174           rtx insn = gen_rtx_MINUS (DImode, cxt, tramp);
7175           insn = gen_rtx_ASHIFT (DImode, insn, GEN_INT (10-2));
7176           insn = gen_rtx_AND (DImode, insn, mask);
7177           /* Or in ptb/u .,tr1 pattern */
7178           insn = gen_rtx_IOR (DImode, insn, gen_int_mode (0xec000010, SImode));
7179           insn = force_operand (insn, NULL_RTX);
7180           insn = gen_lowpart (SImode, insn);
7181           emit_move_insn (gen_rtx_MEM (SImode, tramp), insn);
7182           insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (38));
7183           insn = gen_rtx_AND (DImode, insn, mask);
7184           insn = force_operand (gen_rtx_IOR (DImode, movi1, insn), NULL_RTX);
7185           insn = gen_lowpart (SImode, insn);
7186           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 4)), insn);
7187           insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (22));
7188           insn = gen_rtx_AND (DImode, insn, mask);
7189           insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
7190           insn = gen_lowpart (SImode, insn);
7191           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 8)), insn);
7192           insn = gen_rtx_LSHIFTRT (DImode, fnaddr, GEN_INT (6));
7193           insn = gen_rtx_AND (DImode, insn, mask);
7194           insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
7195           insn = gen_lowpart (SImode, insn);
7196           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 12)),
7197                           insn);
7198           insn = gen_rtx_ASHIFT (DImode, fnaddr, GEN_INT (10));
7199           insn = gen_rtx_AND (DImode, insn, mask);
7200           insn = force_operand (gen_rtx_IOR (DImode, shori1, insn), NULL_RTX);
7201           insn = gen_lowpart (SImode, insn);
7202           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 16)),
7203                           insn);
7204           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 20)),
7205                           GEN_INT (0x6bf10600));
7206           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 24)),
7207                           GEN_INT (0x4415fc10));
7208           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 28)),
7209                           GEN_INT (0x4401fff0));
7210           emit_insn (gen_ic_invalidate_line (tramp));
7211           return;
7212         }
7213       tramp_templ = gen_rtx_SYMBOL_REF (Pmode,"__GCC_nested_trampoline");
7214       fixed_len = TRAMPOLINE_SIZE - 2 * GET_MODE_SIZE (Pmode);
7215
7216       tramp_templ = gen_datalabel_ref (tramp_templ);
7217       dst = gen_rtx_MEM (BLKmode, tramp);
7218       src = gen_rtx_MEM (BLKmode, tramp_templ);
7219       set_mem_align (dst, 256);
7220       set_mem_align (src, 64);
7221       emit_block_move (dst, src, GEN_INT (fixed_len));
7222
7223       emit_move_insn (gen_rtx_MEM (Pmode, plus_constant (tramp, fixed_len)),
7224                       fnaddr);
7225       emit_move_insn (gen_rtx_MEM (Pmode,
7226                                    plus_constant (tramp,
7227                                                   fixed_len
7228                                                   + GET_MODE_SIZE (Pmode))), 
7229                       cxt);
7230       emit_insn (gen_ic_invalidate_line (tramp));
7231       return;
7232     }
7233   else if (TARGET_SHMEDIA)
7234     {
7235       /* movi fnaddr >> 16,r1; shori fnaddr,r1; ptabs/l r1,tr0
7236          movi cxt >> 16,r1; shori cxt,r1; blink tr0,r63  */
7237       rtx quad0 = gen_reg_rtx (DImode), cxtload = gen_reg_rtx (DImode);
7238       rtx quad1 = gen_reg_rtx (DImode), quad2 = gen_reg_rtx (DImode);
7239       /* movi 0,r1: 0xcc000010 shori 0,r1: c8000010  concatenated,
7240          rotated 10 right, and higer 16 bit of every 32 selected.  */
7241       rtx movishori
7242         = force_reg (V2HImode, (simplify_gen_subreg
7243                                 (V2HImode, GEN_INT (0x4330432), SImode, 0)));
7244       rtx ptabs = force_reg (DImode, GEN_INT (0x6bf10600));
7245       rtx blink = force_reg (DImode, GEN_INT (0x4401fff0));
7246
7247       tramp = force_reg (Pmode, tramp);
7248       fnaddr = force_reg (SImode, fnaddr);
7249       cxt = force_reg (SImode, cxt);
7250       emit_insn (gen_mshflo_w_x (gen_rtx_SUBREG (V4HImode, quad0, 0),
7251                                  gen_rtx_SUBREG (V2HImode, fnaddr, 0),
7252                                  movishori));
7253       emit_insn (gen_rotldi3_mextr (quad0, quad0,
7254                                     GEN_INT (TARGET_LITTLE_ENDIAN ? 24 : 56)));
7255       emit_insn (gen_ashldi3_media (quad0, quad0, GEN_INT (2)));
7256       emit_move_insn (gen_rtx_MEM (DImode, tramp), quad0);
7257       emit_insn (gen_mshflo_w_x (gen_rtx_SUBREG (V4HImode, cxtload, 0),
7258                                  gen_rtx_SUBREG (V2HImode, cxt, 0),
7259                                  movishori));
7260       emit_insn (gen_rotldi3_mextr (cxtload, cxtload,
7261                                     GEN_INT (TARGET_LITTLE_ENDIAN ? 24 : 56)));
7262       emit_insn (gen_ashldi3_media (cxtload, cxtload, GEN_INT (2)));
7263       if (TARGET_LITTLE_ENDIAN)
7264         {
7265           emit_insn (gen_mshflo_l_di (quad1, ptabs, cxtload));
7266           emit_insn (gen_mextr4 (quad2, cxtload, blink));
7267         }
7268       else
7269         {
7270           emit_insn (gen_mextr4 (quad1, cxtload, ptabs));
7271           emit_insn (gen_mshflo_l_di (quad2, blink, cxtload));
7272         }
7273       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, 8)), quad1);
7274       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, 16)), quad2);
7275       emit_insn (gen_ic_invalidate_line (tramp));
7276       return;
7277     }
7278   else if (TARGET_SHCOMPACT)
7279     {
7280       emit_insn (gen_initialize_trampoline (tramp, cxt, fnaddr));
7281       return;
7282     }
7283   emit_move_insn (gen_rtx_MEM (SImode, tramp),
7284                   gen_int_mode (TARGET_LITTLE_ENDIAN ? 0xd301d202 : 0xd202d301,
7285                                 SImode));
7286   emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 4)),
7287                   gen_int_mode (TARGET_LITTLE_ENDIAN ? 0x0009422b : 0x422b0009,
7288                                 SImode));
7289   emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 8)),
7290                   cxt);
7291   emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 12)),
7292                   fnaddr);
7293   if (TARGET_HARVARD)
7294     {
7295       if (TARGET_USERMODE)
7296         emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__ic_invalidate"),
7297                            0, VOIDmode, 1, tramp, SImode);
7298       else
7299         emit_insn (gen_ic_invalidate_line (tramp));
7300     }
7301 }
7302
7303 \f
7304 /* Machine specific built-in functions.  */
7305
7306 struct builtin_description
7307 {
7308   const enum insn_code icode;
7309   const char *const name;
7310   int signature;
7311 };
7312
7313 /* describe number and signedness of arguments; arg[0] == result
7314    (1: unsigned, 2: signed, 4: don't care, 8: pointer 0: no argument */
7315 static const char signature_args[][4] =
7316 {
7317 #define SH_BLTIN_V2SI2 0
7318   { 4, 4 },
7319 #define SH_BLTIN_V4HI2 1
7320   { 4, 4 },
7321 #define SH_BLTIN_V2SI3 2
7322   { 4, 4, 4 },
7323 #define SH_BLTIN_V4HI3 3
7324   { 4, 4, 4 },
7325 #define SH_BLTIN_V8QI3 4
7326   { 4, 4, 4 },
7327 #define SH_BLTIN_MAC_HISI 5
7328   { 1, 4, 4, 1 },
7329 #define SH_BLTIN_SH_HI 6
7330   { 4, 4, 1 },
7331 #define SH_BLTIN_SH_SI 7
7332   { 4, 4, 1 },
7333 #define SH_BLTIN_V4HI2V2SI 8
7334   { 4, 4, 4 },
7335 #define SH_BLTIN_V4HI2V8QI 9
7336   { 4, 4, 4 },
7337 #define SH_BLTIN_SISF 10
7338   { 4, 2 },
7339 #define SH_BLTIN_LDUA_L 11
7340   { 2, 8 },
7341 #define SH_BLTIN_LDUA_Q 12
7342   { 1, 8 },
7343 #define SH_BLTIN_STUA_L 13
7344   { 0, 8, 2 },
7345 #define SH_BLTIN_STUA_Q 14
7346   { 0, 8, 1 },
7347 #define SH_BLTIN_UDI 15
7348   { 0, 8, 1 },
7349 #define SH_BLTIN_NUM_SHARED_SIGNATURES 16
7350 #define SH_BLTIN_2 16
7351 #define SH_BLTIN_SU 16
7352   { 1, 2 },
7353 #define SH_BLTIN_3 17
7354 #define SH_BLTIN_SUS 17
7355   { 2, 2, 1 },
7356 #define SH_BLTIN_PSSV 18
7357   { 0, 8, 2, 2 },
7358 #define SH_BLTIN_XXUU 19
7359 #define SH_BLTIN_UUUU 19
7360   { 1, 1, 1, 1 },
7361 #define SH_BLTIN_PV 20
7362   { 0, 8 },
7363 };
7364 /* mcmv: operands considered unsigned. */
7365 /* mmulsum_wq, msad_ubq: result considered unsigned long long.  */
7366 /* mperm: control value considered unsigned int. */
7367 /* mshalds, mshard, mshards, mshlld, mshlrd: shift count is unsigned int. */
7368 /* mshards_q: returns signed short.  */
7369 /* nsb: takes long long arg, returns unsigned char.  */
7370 static const struct builtin_description bdesc[] =
7371 {
7372   { CODE_FOR_absv2si2,  "__builtin_absv2si2", SH_BLTIN_V2SI2 },
7373   { CODE_FOR_absv4hi2,  "__builtin_absv4hi2", SH_BLTIN_V4HI2 },
7374   { CODE_FOR_addv2si3,  "__builtin_addv2si3", SH_BLTIN_V2SI3 },
7375   { CODE_FOR_addv4hi3,  "__builtin_addv4hi3", SH_BLTIN_V4HI3 },
7376   { CODE_FOR_ssaddv2si3,"__builtin_ssaddv2si3", SH_BLTIN_V2SI3 },
7377   { CODE_FOR_usaddv8qi3,"__builtin_usaddv8qi3", SH_BLTIN_V8QI3 },
7378   { CODE_FOR_ssaddv4hi3,"__builtin_ssaddv4hi3", SH_BLTIN_V4HI3 },
7379 #if 0
7380   { CODE_FOR_alloco32,  "__builtin_sh_media_ALLOCO", SH_BLTIN_PV },
7381   { CODE_FOR_alloco64,  "__builtin_sh_media_ALLOCO", SH_BLTIN_PV },
7382 #endif
7383   { CODE_FOR_negcmpeqv8qi,"__builtin_sh_media_MCMPEQ_B", SH_BLTIN_V8QI3 },
7384   { CODE_FOR_negcmpeqv2si,"__builtin_sh_media_MCMPEQ_L", SH_BLTIN_V2SI3 },
7385   { CODE_FOR_negcmpeqv4hi,"__builtin_sh_media_MCMPEQ_W", SH_BLTIN_V4HI3 },
7386   { CODE_FOR_negcmpgtuv8qi,"__builtin_sh_media_MCMPGT_UB", SH_BLTIN_V8QI3 },
7387   { CODE_FOR_negcmpgtv2si,"__builtin_sh_media_MCMPGT_L", SH_BLTIN_V2SI3 },
7388   { CODE_FOR_negcmpgtv4hi,"__builtin_sh_media_MCMPGT_W", SH_BLTIN_V4HI3 },
7389   { CODE_FOR_mcmv,      "__builtin_sh_media_MCMV", SH_BLTIN_UUUU },
7390   { CODE_FOR_mcnvs_lw,  "__builtin_sh_media_MCNVS_LW", SH_BLTIN_3 },
7391   { CODE_FOR_mcnvs_wb,  "__builtin_sh_media_MCNVS_WB", SH_BLTIN_V4HI2V8QI },
7392   { CODE_FOR_mcnvs_wub, "__builtin_sh_media_MCNVS_WUB", SH_BLTIN_V4HI2V8QI },
7393   { CODE_FOR_mextr1,    "__builtin_sh_media_MEXTR1", SH_BLTIN_UDI },
7394   { CODE_FOR_mextr2,    "__builtin_sh_media_MEXTR2", SH_BLTIN_UDI },
7395   { CODE_FOR_mextr3,    "__builtin_sh_media_MEXTR3", SH_BLTIN_UDI },
7396   { CODE_FOR_mextr4,    "__builtin_sh_media_MEXTR4", SH_BLTIN_UDI },
7397   { CODE_FOR_mextr5,    "__builtin_sh_media_MEXTR5", SH_BLTIN_UDI },
7398   { CODE_FOR_mextr6,    "__builtin_sh_media_MEXTR6", SH_BLTIN_UDI },
7399   { CODE_FOR_mextr7,    "__builtin_sh_media_MEXTR7", SH_BLTIN_UDI },
7400   { CODE_FOR_mmacfx_wl, "__builtin_sh_media_MMACFX_WL", SH_BLTIN_MAC_HISI },
7401   { CODE_FOR_mmacnfx_wl,"__builtin_sh_media_MMACNFX_WL", SH_BLTIN_MAC_HISI },
7402   { CODE_FOR_mulv2si3,  "__builtin_mulv2si3", SH_BLTIN_V2SI3, },
7403   { CODE_FOR_mulv4hi3,  "__builtin_mulv4hi3", SH_BLTIN_V4HI3 },
7404   { CODE_FOR_mmulfx_l,  "__builtin_sh_media_MMULFX_L", SH_BLTIN_V2SI3 },
7405   { CODE_FOR_mmulfx_w,  "__builtin_sh_media_MMULFX_W", SH_BLTIN_V4HI3 },
7406   { CODE_FOR_mmulfxrp_w,"__builtin_sh_media_MMULFXRP_W", SH_BLTIN_V4HI3 },
7407   { CODE_FOR_mmulhi_wl, "__builtin_sh_media_MMULHI_WL", SH_BLTIN_V4HI2V2SI },
7408   { CODE_FOR_mmullo_wl, "__builtin_sh_media_MMULLO_WL", SH_BLTIN_V4HI2V2SI },
7409   { CODE_FOR_mmulsum_wq,"__builtin_sh_media_MMULSUM_WQ", SH_BLTIN_XXUU },
7410   { CODE_FOR_mperm_w,   "__builtin_sh_media_MPERM_W", SH_BLTIN_SH_HI },
7411   { CODE_FOR_msad_ubq,  "__builtin_sh_media_MSAD_UBQ", SH_BLTIN_XXUU },
7412   { CODE_FOR_mshalds_l, "__builtin_sh_media_MSHALDS_L", SH_BLTIN_SH_SI },
7413   { CODE_FOR_mshalds_w, "__builtin_sh_media_MSHALDS_W", SH_BLTIN_SH_HI },
7414   { CODE_FOR_ashrv2si3, "__builtin_ashrv2si3", SH_BLTIN_SH_SI },
7415   { CODE_FOR_ashrv4hi3, "__builtin_ashrv4hi3", SH_BLTIN_SH_HI },
7416   { CODE_FOR_mshards_q, "__builtin_sh_media_MSHARDS_Q", SH_BLTIN_SUS },
7417   { CODE_FOR_mshfhi_b,  "__builtin_sh_media_MSHFHI_B", SH_BLTIN_V8QI3 },
7418   { CODE_FOR_mshfhi_l,  "__builtin_sh_media_MSHFHI_L", SH_BLTIN_V2SI3 },
7419   { CODE_FOR_mshfhi_w,  "__builtin_sh_media_MSHFHI_W", SH_BLTIN_V4HI3 },
7420   { CODE_FOR_mshflo_b,  "__builtin_sh_media_MSHFLO_B", SH_BLTIN_V8QI3 },
7421   { CODE_FOR_mshflo_l,  "__builtin_sh_media_MSHFLO_L", SH_BLTIN_V2SI3 },
7422   { CODE_FOR_mshflo_w,  "__builtin_sh_media_MSHFLO_W", SH_BLTIN_V4HI3 },
7423   { CODE_FOR_ashlv2si3, "__builtin_ashlv2si3", SH_BLTIN_SH_SI },
7424   { CODE_FOR_ashlv4hi3, "__builtin_ashlv4hi3", SH_BLTIN_SH_HI },
7425   { CODE_FOR_lshrv2si3, "__builtin_lshrv2si3", SH_BLTIN_SH_SI },
7426   { CODE_FOR_lshrv4hi3, "__builtin_lshrv4hi3", SH_BLTIN_SH_HI },
7427   { CODE_FOR_subv2si3,  "__builtin_subv2si3", SH_BLTIN_V2SI3 },
7428   { CODE_FOR_subv4hi3,  "__builtin_subv4hi3", SH_BLTIN_V4HI3 },
7429   { CODE_FOR_sssubv2si3,"__builtin_sssubv2si3", SH_BLTIN_V2SI3 },
7430   { CODE_FOR_ussubv8qi3,"__builtin_ussubv8qi3", SH_BLTIN_V8QI3 },
7431   { CODE_FOR_sssubv4hi3,"__builtin_sssubv4hi3", SH_BLTIN_V4HI3 },
7432   { CODE_FOR_fcosa_s,   "__builtin_sh_media_FCOSA_S", SH_BLTIN_SISF },
7433   { CODE_FOR_fsina_s,   "__builtin_sh_media_FSINA_S", SH_BLTIN_SISF },
7434   { CODE_FOR_fipr,      "__builtin_sh_media_FIPR_S", SH_BLTIN_3 },
7435   { CODE_FOR_ftrv,      "__builtin_sh_media_FTRV_S", SH_BLTIN_3 },
7436   { CODE_FOR_fsrra_s,   "__builtin_sh_media_FSRRA_S", SH_BLTIN_2 },
7437 #if 0
7438   { CODE_FOR_ldhi_l,    "__builtin_sh_media_LDHI_L", SH_BLTIN_LDUA_L },
7439   { CODE_FOR_ldhi_q,    "__builtin_sh_media_LDHI_Q", SH_BLTIN_LDUA_Q },
7440   { CODE_FOR_ldlo_l,    "__builtin_sh_media_LDLO_L", SH_BLTIN_LDUA_L },
7441   { CODE_FOR_ldlo_q,    "__builtin_sh_media_LDLO_Q", SH_BLTIN_LDUA_Q },
7442   { CODE_FOR_sthi_l,    "__builtin_sh_media_STHI_L", SH_BLTIN_STUA_L },
7443   { CODE_FOR_sthi_q,    "__builtin_sh_media_STHI_Q", SH_BLTIN_STUA_Q },
7444   { CODE_FOR_stlo_l,    "__builtin_sh_media_STLO_L", SH_BLTIN_STUA_L },
7445   { CODE_FOR_stlo_q,    "__builtin_sh_media_STLO_Q", SH_BLTIN_STUA_Q },
7446   { CODE_FOR_ldhi_l64,  "__builtin_sh_media_LDHI_L", SH_BLTIN_LDUA_L },
7447   { CODE_FOR_ldhi_q64,  "__builtin_sh_media_LDHI_Q", SH_BLTIN_LDUA_Q },
7448   { CODE_FOR_ldlo_l64,  "__builtin_sh_media_LDLO_L", SH_BLTIN_LDUA_L },
7449   { CODE_FOR_ldlo_q64,  "__builtin_sh_media_LDLO_Q", SH_BLTIN_LDUA_Q },
7450   { CODE_FOR_sthi_l64,  "__builtin_sh_media_STHI_L", SH_BLTIN_STUA_L },
7451   { CODE_FOR_sthi_q64,  "__builtin_sh_media_STHI_Q", SH_BLTIN_STUA_Q },
7452   { CODE_FOR_stlo_l64,  "__builtin_sh_media_STLO_L", SH_BLTIN_STUA_L },
7453   { CODE_FOR_stlo_q64,  "__builtin_sh_media_STLO_Q", SH_BLTIN_STUA_Q },
7454 #endif
7455   { CODE_FOR_nsb,       "__builtin_sh_media_NSB", SH_BLTIN_SU },
7456   { CODE_FOR_byterev,   "__builtin_sh_media_BYTEREV", SH_BLTIN_2 },
7457 #if 0
7458   { CODE_FOR_prefetch32,"__builtin_sh_media_PREFO", SH_BLTIN_PSSV },
7459   { CODE_FOR_prefetch64,"__builtin_sh_media_PREFO", SH_BLTIN_PSSV }
7460 #endif
7461 };
7462
7463 static void
7464 sh_media_init_builtins ()
7465 {
7466   tree shared[SH_BLTIN_NUM_SHARED_SIGNATURES];
7467   const struct builtin_description *d;
7468
7469   memset (shared, 0, sizeof shared);
7470   for (d = bdesc; d - bdesc < (int) (sizeof bdesc / sizeof bdesc[0]); d++)
7471     {
7472       tree type, arg_type;
7473       int signature = d->signature;
7474       int i;
7475
7476       if (signature < SH_BLTIN_NUM_SHARED_SIGNATURES && shared[signature])
7477         type = shared[signature];
7478       else
7479         {
7480           int has_result = signature_args[signature][0] != 0;
7481
7482           if (signature_args[signature][1] == 8
7483               && (insn_data[d->icode].operand[has_result].mode != Pmode))
7484             continue;
7485           if (! TARGET_FPU_ANY
7486               && FLOAT_MODE_P (insn_data[d->icode].operand[0].mode))
7487             continue;
7488           type = void_list_node;
7489           for (i = 3; ; i--)
7490             {
7491               int arg = signature_args[signature][i];
7492               int opno = i - 1 + has_result;
7493
7494               if (arg == 8)
7495                 arg_type = ptr_type_node;
7496               else if (arg)
7497                 arg_type = ((*lang_hooks.types.type_for_mode)
7498                             (insn_data[d->icode].operand[opno].mode,
7499                              (arg & 1)));
7500               else if (i)
7501                 continue;
7502               else
7503                 arg_type = void_type_node;
7504               if (i == 0)
7505                 break;
7506               type = tree_cons (NULL_TREE, arg_type, type);
7507             }
7508           type = build_function_type (arg_type, type);
7509           if (signature < SH_BLTIN_NUM_SHARED_SIGNATURES)
7510             shared[signature] = type;
7511         }
7512       builtin_function (d->name, type, d - bdesc, BUILT_IN_MD,
7513                         NULL, NULL_TREE);
7514     }
7515 }
7516
7517 static void
7518 sh_init_builtins ()
7519 {
7520   if (TARGET_SHMEDIA)
7521     sh_media_init_builtins ();
7522 }
7523
7524 /* Expand an expression EXP that calls a built-in function,
7525    with result going to TARGET if that's convenient
7526    (and in mode MODE if that's convenient).
7527    SUBTARGET may be used as the target for computing one of EXP's operands.
7528    IGNORE is nonzero if the value is to be ignored.  */
7529
7530 static rtx
7531 sh_expand_builtin (exp, target, subtarget, mode, ignore)
7532      tree exp;
7533      rtx target;
7534      rtx subtarget ATTRIBUTE_UNUSED;
7535      enum machine_mode mode ATTRIBUTE_UNUSED;
7536      int ignore;
7537 {
7538   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
7539   tree arglist = TREE_OPERAND (exp, 1);
7540   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
7541   const struct builtin_description *d = &bdesc[fcode];
7542   enum insn_code icode = d->icode;
7543   int signature = d->signature;
7544   enum machine_mode tmode = VOIDmode;
7545   int nop = 0, i;
7546   rtx op[4];
7547   rtx pat;
7548
7549   if (signature_args[signature][0])
7550     {
7551       if (ignore)
7552         return 0;
7553
7554       tmode = insn_data[icode].operand[0].mode;
7555       if (! target
7556           || GET_MODE (target) != tmode
7557           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
7558         target = gen_reg_rtx (tmode);
7559       op[nop++] = target;
7560     }
7561   else
7562     target = 0;
7563
7564   for (i = 1; i <= 3; i++, nop++)
7565     {
7566       tree arg;
7567       enum machine_mode opmode, argmode;
7568
7569       if (! signature_args[signature][i])
7570         break;
7571       arg = TREE_VALUE (arglist);
7572       arglist = TREE_CHAIN (arglist);
7573       opmode = insn_data[icode].operand[nop].mode;
7574       argmode = TYPE_MODE (TREE_TYPE (arg));
7575       if (argmode != opmode)
7576         arg = build1 (NOP_EXPR,
7577                       (*lang_hooks.types.type_for_mode) (opmode, 0), arg);
7578       op[nop] = expand_expr (arg, NULL_RTX, opmode, 0);
7579       if (! (*insn_data[icode].operand[nop].predicate) (op[nop], opmode))
7580         op[nop] = copy_to_mode_reg (opmode, op[nop]);
7581     }
7582
7583   switch (nop)
7584     {
7585     case 1:
7586       pat = (*insn_data[d->icode].genfun) (op[0]);
7587       break;
7588     case 2:
7589       pat = (*insn_data[d->icode].genfun) (op[0], op[1]);
7590       break;
7591     case 3:
7592       pat = (*insn_data[d->icode].genfun) (op[0], op[1], op[2]);
7593       break;
7594     case 4:
7595       pat = (*insn_data[d->icode].genfun) (op[0], op[1], op[2], op[3]);
7596       break;
7597     }
7598   if (! pat)
7599     return 0;
7600   emit_insn (pat);
7601   return target;
7602 }
7603
7604 void
7605 sh_expand_unop_v2sf (code, op0, op1)
7606      enum rtx_code code;
7607      rtx op0, op1;
7608 {
7609   rtx sel0 = const0_rtx;
7610   rtx sel1 = const1_rtx;
7611   rtx (*fn) (rtx, rtx, rtx, rtx, rtx) = gen_unary_sf_op;
7612   rtx op = gen_rtx_fmt_e (code, SFmode, op1);
7613
7614   emit_insn ((*fn) (op0, op1, op, sel0, sel0));
7615   emit_insn ((*fn) (op0, op1, op, sel1, sel1));
7616 }
7617
7618 void
7619 sh_expand_binop_v2sf (code, op0, op1, op2)
7620      enum rtx_code code;
7621      rtx op0, op1, op2;
7622 {
7623   rtx sel0 = const0_rtx;
7624   rtx sel1 = const1_rtx;
7625   rtx (*fn) (rtx, rtx, rtx, rtx, rtx, rtx, rtx) = gen_binary_sf_op;
7626   rtx op = gen_rtx_fmt_ee (code, SFmode, op1, op2);
7627
7628   emit_insn ((*fn) (op0, op1, op2, op, sel0, sel0, sel0));
7629   emit_insn ((*fn) (op0, op1, op2, op, sel1, sel1, sel1));
7630 }
7631
7632 #include "gt-sh.h"