OSDN Git Service

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