OSDN Git Service

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