OSDN Git Service

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