OSDN Git Service

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