OSDN Git Service

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