OSDN Git Service

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