OSDN Git Service

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