OSDN Git Service

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